Skip to:
Content

bbPress.org

Ticket #1906: 1906.diff

File 1906.diff, 22.6 KB (added by jmdodd, 12 years ago)

First pass; proposed fix for 1-5 of user-facing group forum.

  • bbp-includes/extend/buddypress.php

     
    226226                add_filter( 'bbp_get_favorites_permalink',     array( $this, 'get_favorites_permalink'     ), 10, 2 );
    227227                add_filter( 'bbp_get_subscriptions_permalink', array( $this, 'get_subscriptions_permalink' ), 10, 2 );
    228228
     229                /** Links and pagination **********************************************/
     230
    229231                // Map forum/topic/replys permalinks to their groups
    230232                add_filter( 'bbp_get_forum_permalink', array( $this, 'map_forum_permalink_to_group' ), 10, 2 );
    231233                add_filter( 'bbp_get_topic_permalink', array( $this, 'map_topic_permalink_to_group' ), 10, 2 );
    232234                add_filter( 'bbp_get_reply_permalink', array( $this, 'map_reply_permalink_to_group' ), 10, 2 );
    233235
     236                // Map reply edit links to their groups
     237                add_filter( 'bbp_get_reply_edit_url',  array( $this, 'map_reply_edit_url_to_group'  ), 10, 2 );
     238
     239                // Map assorted template function permalinks
     240                add_filter( 'post_link',               array( $this, 'post_link'                    ), 10, 3 );
     241                add_filter( 'page_link',               array( $this, 'page_link'                    ), 10, 3 );
     242                add_filter( 'post_type_link',          array( $this, 'post_type_link'               ), 10, 4 );
     243
    234244                // Canonical redirects for normal URLs
    235245                add_action( 'template_redirect', array( $this, 'redirect_canonical' ) );
    236246
     247                // Group forum pagination
     248                add_filter( 'bbp_topic_pagination', array( $this, 'topic_pagination' ) );
     249                add_filter( 'bbp_replies_pagination', array( $this, 'replies_pagination' ) );
     250
    237251                /** Mentions **********************************************************/
    238252
    239253                // Only link mentions if activity component is active
     
    850864        }
    851865
    852866        /**
    853          * Map a forum permalink to the corresponding group
     867         * Maybe map a bbPress forum/topic/reply permalink to the corresponding group
    854868         *
    855          * @since bbPress (r3802)
    856          * @param string $url
    857          * @param int $forum_id
     869         * @param int $post_id
     870         * @uses get_post
     871         * @uses bbp_is_reply
     872         * @uses bbp_get_reply_topic_id
     873         * @uses bbp_get_reply_forum_id
     874         * @uses bbp_is_topic
     875         * @uses bbp_get_topic_forum_id
     876         * @uses bbp_is_forum
     877         * @uses get_post_field
     878         * @uses bbp_get_forum_group_ids
     879         * @uses groups_get_group
     880         * @uses bp_get_group_admin_permalink
     881         * @uses bp_get_group_permalink
     882         * @return Bail early if not a group forum post
    858883         * @return string
    859884         */
    860         public function map_forum_permalink_to_group( $url, $forum_id ) {
    861                 $slug      = get_post_field( 'post_name', $forum_id );
    862                 $group_ids = bbp_get_forum_group_ids( $forum_id );
     885        private function maybe_map_permalink_to_group( $post_id ) {
     886                $post_id = absint( $post_id );
     887                $post    = get_post( $post_id );
     888                if ( !$post )
     889                        return false;
    863890
    864                 // If the topic is not associated with a group, don't mess with it
    865                 if ( !empty( $group_ids ) ) {
     891                // Bail early if not a bbPress post type
     892                $post_type = $post->post_type;
     893                $post_name = $post->post_name;
     894                if ( !in_array( $post_type, array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) )
     895                        return false;
    866896
    867                         // @todo Multiple group forums/forum groups
    868                         $group_id = $group_ids[0];
    869                         $group    = groups_get_group( array( 'group_id' => $group_id ) );
     897                // Reply
     898                if ( bbp_is_reply( $post_id ) ) {
     899                        $reply_id = $post_id;
     900                        $topic_id = bbp_get_reply_topic_id( $post_id );
     901                        $forum_id = bbp_get_reply_forum_id( $post_id );
    870902
    871                         if ( bp_is_group_admin_screen( $this->forums_slug ) ) {
    872                                 $group_permalink = bp_get_group_admin_permalink( $group );
    873                         } else {
    874                                 $group_permalink = bp_get_group_permalink( $group );
    875                         }
     903                // Topic
     904                } elseif ( bbp_is_topic( $post_id ) ) {
     905                        $topic_id = $post_id;
     906                        $forum_id = bbp_get_topic_forum_id( $post_id );
    876907
    877                         $url = trailingslashit( $group_permalink . $this->forums_slug . '/' . $slug );
     908                // Forum
     909                } elseif ( bbp_is_forum( $post_id ) ) {
     910                        $forum_id = $post_id;
     911
     912                // Bail
     913                } else {
     914                        return false;
    878915                }
    879916
    880                 return $url;
     917                // Get relevant slugs
     918                $forum_slug = get_post_field( 'post_name', $forum_id );
     919                $group_ids  = bbp_get_forum_group_ids( $forum_id );
     920
     921                // Bail if the post isn't associated with a group
     922                if ( empty( $group_ids ) )
     923                        return false;
     924
     925                // @todo Multiple group forums/forum groups
     926                $group_id = $group_ids[0];
     927                $group    = groups_get_group( array( 'group_id' => $group_id ) );
     928
     929                if ( bp_is_group_admin_screen( $this->forums_slug ) )
     930                        $group_permalink = bp_get_group_admin_permalink( $group );
     931                else
     932                        $group_permalink = bp_get_group_permalink( $group );
     933
     934                $url = trailingslashit( $group_permalink ) . $this->forums_slug . '/';
     935
     936                // Reply link (special case: should usually request the permalink for the reply's topic)
     937                if ( bbp_is_reply( $post_id ) ) {
     938                        $url .= $this->reply_slug . '/' . $post_name;
     939
     940                // Topic link
     941                } elseif ( bbp_is_topic( $post_id ) ) {
     942                        $url .= $this->topic_slug . '/' . $post_name;
     943
     944                // Forum link
     945                } elseif ( bbp_is_forum( $post_id ) ) {
     946                        $url .= $post_name;
     947                }
     948
     949                return trailingslashit( $url );
    881950        }
    882951
    883952        /**
    884          * Map a topic permalink to the current group forum
     953         * Map a forum permalink to its corresponding group
    885954         *
    886955         * @since bbPress (r3802)
    887956         * @param string $url
     957         * @param int $forum_id
     958         * @uses maybe_map_permalink_to_group
     959         * @return string
     960         */
     961        public function map_forum_permalink_to_group( $url, $forum_id ) {
     962                $new = $this->maybe_map_permalink_to_group( $forum_id );
     963                if ( !$new )
     964                        return $url;
     965
     966                return $new;
     967        }
     968
     969        /**
     970         * Map a topic permalink to its group forum
     971         *
     972         * @since bbPress (r3802)
     973         * @param string $url
    888974         * @param int $topic_id
     975         * @uses maybe_map_permalink_to_group
    889976         * @return string
    890977         */
    891978        public function map_topic_permalink_to_group( $url, $topic_id ) {
    892                 $slug      = get_post_field( 'post_name', $topic_id );
    893                 $forum_id  = bbp_get_topic_forum_id( $topic_id );
    894                 $group_ids = bbp_get_forum_group_ids( $forum_id );
     979                $new = $this->maybe_map_permalink_to_group( $topic_id );
     980                if ( !$new )
     981                        return $url;
     982               
     983                return $new;
     984        }
    895985
    896                 // If the topic is not associated with a group, don't mess with it
    897                 if ( !empty( $group_ids ) ) {
     986        /**
     987         * Map a reply permalink to its group forum
     988         *
     989         * @since bbPress (r3802)
     990         * @param string $url
     991         * @param int $reply_id
     992         * @uses maybe_map_permalink_to_group
     993         * @return string
     994         */
     995        public function map_reply_permalink_to_group( $url, $reply_id ) {
     996                $topic_id = bbp_get_reply_topic_id( $reply_id );
    898997
    899                         // @todo Multiple group forums/forum groups
    900                         $group_id = $group_ids[0];
    901                         $group    = groups_get_group( array( 'group_id' => $group_id ) );
     998                $new = $this->maybe_map_permalink_to_group( $topic_id );
     999                if ( !$new )
     1000                        return $url;
    9021001
    903                         if ( bp_is_group_admin_screen( $this->forums_slug ) ) {
    904                                 $group_permalink = bp_get_group_admin_permalink( $group );
    905                         } else {
    906                                 $group_permalink = bp_get_group_permalink( $group );
    907                         }
     1002                return $new;
     1003        }
    9081004
    909                         $url = trailingslashit( $group_permalink . $this->forums_slug . '/' . $this->topic_slug . '/' . $slug );
    910                 }
     1005        /**
     1006         * Map a reply edit link to its group forum
     1007         *
     1008         * @param string $url
     1009         * @param int $reply_id
     1010         * @uses maybe_map_permalink_to_group
     1011         * @return string
     1012         */
     1013        public function map_reply_edit_url_to_group( $url, $reply_id ) {
     1014                $bbp = bbpress();
    9111015
     1016                $new = $this->maybe_map_permalink_to_group( $reply_id );
     1017                if ( !$new )
     1018                        return $url;
     1019
     1020                $url = trailingslashit( $new ) . $bbp->edit_id  . '/';
    9121021                return $url;
    9131022        }
    9141023
    9151024        /**
    916          * Map a topic permalink to the current group forum
     1025         * Map a post link to its group forum
    9171026         *
    918          * @since bbPress (r3802)
    9191027         * @param string $url
    920          * @param int $reply_id
     1028         * @param obj $post
     1029         * @param boolean $leavename
     1030         * @uses maybe_map_permalink_to_group
    9211031         * @return string
    9221032         */
    923         public function map_reply_permalink_to_group( $url, $reply_id ) {
    924                 $slug      = get_post_field( 'post_name', $reply_id );
    925                 $forum_id  = bbp_get_reply_forum_id( $reply_id );
    926                 $group_ids = bbp_get_forum_group_ids( $forum_id );
     1033        public function post_link( $url, $post, $leavename ) {
     1034                $post_id = $post->ID;
    9271035
    928                 // If the topic is not associated with a group, don't mess with it
    929                 if ( !empty( $group_ids ) ) {
     1036                $new = $this->maybe_map_permalink_to_group( $post_id );
     1037                if ( !$new )
     1038                        return $url;
    9301039
    931                         // @todo Multiple group forums/forum groups
    932                         $group_id = $group_ids[0];
    933                         $group    = groups_get_group( array( 'group_id' => $group_id ) );
     1040                return $new;
     1041        }
    9341042
    935                         if ( bp_is_group_admin_screen( $this->forums_slug ) ) {
    936                                 $group_permalink = bp_get_group_admin_permalink( $group );
    937                         } else {
    938                                 $group_permalink = bp_get_group_permalink( $group );
    939                         }
     1043        /**
     1044         * Map a page link to its group forum
     1045         *
     1046         * @param string $url
     1047         * @param int $post_id
     1048         * @param $sample
     1049         * @uses maybe_map_permalink_to_group
     1050         * @return string
     1051         */
     1052        public function page_link( $url, $post_id, $sample ) {
     1053                $new = $this->maybe_map_permalink_to_group( $post_id );
     1054                if ( !$new )
     1055                        return $url;
    9401056
    941                         $url = trailingslashit( $group_permalink . $this->forums_slug . '/' . $this->topic_slug . '/' . $slug );
    942                 }
    943 
     1057                $url = $new;
    9441058                return $url;
    9451059        }
    9461060
    9471061        /**
     1062         * Map a custom post type link to its group forum
     1063         *
     1064         * @param string $url
     1065         * @param obj $post
     1066         * @param $leavename
     1067         * @param $sample
     1068         * @uses maybe_map_permalink_to_group
     1069         * @return string
     1070         */
     1071        public function post_type_link( $url, $post, $leavename, $sample ) {
     1072                $post_id = absint( $post->ID );
     1073
     1074                $new = $this->maybe_map_permalink_to_group( $post_id );
     1075                if ( !$new )
     1076                        return $url;
     1077
     1078                return $new;
     1079        }
     1080
     1081        /**
     1082         * Fix pagination of topics on forum view
     1083         *
     1084         * @param array $args
     1085         * @global $wp_rewrite
     1086         * @uses bbp_get_forum_id()
     1087         * @uses maybe_map_permalink_to_group
     1088         * @return array
     1089         */
     1090        public function topic_pagination( $args ) {
     1091                global $wp_rewrite;
     1092
     1093                // Get forum data
     1094                $forum_id = bbp_get_forum_id();
     1095                $new      = $this->maybe_map_permalink_to_group( $forum_id );
     1096                if ( !$new )
     1097                        return $args;
     1098
     1099                $base         = trailingslashit( $new ) . $wp_rewrite->pagination_base . '/%#%/';
     1100                $args['base'] = $base;
     1101                return $args;
     1102        }
     1103
     1104        /**
     1105         * Fix pagination of replies on topic view
     1106         *
     1107         * @param array $args
     1108         * @global $wp_rewrite
     1109         * @uses bbp_get_topic_id()
     1110         * @uses maybe_map_permalink_to_group
     1111         * @return array
     1112         */
     1113        public function replies_pagination( $args ) {
     1114                global $wp_rewrite;
     1115
     1116                // Get topic data
     1117                $topic_id = bbp_get_topic_id();
     1118                $new      = $this->maybe_map_permalink_to_group( $topic_id );
     1119                if ( !$new )
     1120                        return $args;
     1121
     1122                $base         = trailingslashit( $new ) . $wp_rewrite->pagination_base . '/%#%/';
     1123                $args['base'] = $base;
     1124                return $args;
     1125        }
     1126
     1127        /**
    9481128         * Ensure that forum content associated with a BuddyPress group can only be
    9491129         * viewed via the group URL.
    9501130         *
     
    9661146                        // @todo Multiple group forums/forum groups
    9671147                        $group_id            = $group_ids[0];
    9681148                        $group               = groups_get_group( array( 'group_id' => $group_id ) );
    969                         $group_permalink = bp_get_group_permalink( $group );
    970                         $redirect_to     = trailingslashit( $group_permalink . $this->forums_slug );
     1149                        $group_permalink     = bp_get_group_permalink( $group );
     1150                        $redirect_to         = trailingslashit( $group_permalink . $this->forums_slug );
    9711151
    9721152                        if ( !empty( $slug ) ) {
    9731153                                $redirect_to  = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug );
     
    13741554                                //bbp_trash_forum_handler();
    13751555                                break;
    13761556
    1377                         // Permanently delet a forum
     1557                        // Permanently delete a forum
    13781558                        case 'bbp-delete-forum' :
    13791559                                //bbp_delete_forum_handler();
    13801560                                break;
     
    15531733
    15541734                // Forum data
    15551735                $forum_ids  = bbp_get_group_forum_ids( bp_get_current_group_id() );
    1556                 $forum_args = array( 'post__in' => $forum_ids, 'post_parent' => null ); ?>
     1736                $forum_args = array( 'post__in' => $forum_ids, 'post_parent' => null );
    15571737
    1558                 <div id="bbpress-forums">
     1738                // Unset global queries
     1739                $bbp->forum_query = new StdClass;
     1740                $bbp->topic_query = new StdClass;
     1741                $bbp->reply_query = new StdClass;
    15591742
     1743                // Unset global ID's
     1744                $bbp->current_forum_id     = 0;
     1745                $bbp->current_topic_id     = 0;
     1746                $bbp->current_reply_id     = 0;
     1747                $bbp->current_topic_tag_id = 0;
     1748
     1749                // Reset the post data
     1750                wp_reset_postdata();
     1751
     1752                // Allow admins special views
     1753                $post_status = array( bbp_get_closed_status_id(), bbp_get_public_status_id() );
     1754                if ( is_super_admin() || current_user_can( 'moderate' ) || bp_is_item_admin() || bp_is_item_mod() )
     1755                        $post_status = array_merge( $post_status, array( bbp_get_spam_status_id(), bbp_get_trash_status_id() ) ); ?>
     1756
     1757                        <div id="bbpress-forums">
     1758
    15601759                        <?php
    15611760
    15621761                        // Looking at the group forum root
    1563                         if ( ! bp_action_variable( $offset ) ) :
     1762                        if ( !bp_action_variable( $offset ) ) :
    15641763
    1565                                 // Query forums and show them if
    1566                                 if ( !empty( $forum_ids ) && bbp_has_forums( $forum_args ) ) :
     1764                                // Query forums and show them if they exist
     1765                                if ( !empty( $forum_ids ) && bbp_has_forums( $forum_args ) ) : 
    15671766
    1568                                         bbp_the_forum();
    1569 
    15701767                                        // Only one forum found
    1571                                         if ( $bbp->forum_query->post_count == 1 ) :?>
     1768                                        if ( 1 == $bbp->forum_query->post_count && !bp_is_group_admin_screen( $this->slug ) ) :
     1769                                                // Get forum data
     1770                                                $forum_slug = bp_action_variable( $offset );
     1771                                                $forum_args = array( 'name' => $forum_slug, 'post_type' => bbp_get_forum_post_type() );
     1772                                                $forums     = get_posts( $forum_args );
    15721773
    1573                                                 <h3><?php _e( 'Forum', 'bbpress' ); ?></h3>
     1774                                                bbp_the_forum();
    15741775
    1575                                                 <?php bbp_set_query_name( 'bbp_single_forum' ); ?>
     1776                                                // Forum exists
     1777                                                if ( !empty( $forums ) ) :
     1778                                                        $forum = $forums[0];
    15761779
    1577                                                 <?php if ( bbp_has_topics( array( 'post_parent' => bbp_get_forum_id() ) ) ) : ?>
     1780                                                        // Set up forum data
     1781                                                        $forum_id = bbpress()->current_forum_id = $forum->ID;
     1782                                                        bbp_set_query_name( 'bbp_single_forum' ); ?>
    15781783
    1579                                                         <?php bbp_get_template_part( 'pagination', 'topics'    ); ?>
     1784                                                        <h3><?php bbp_forum_title(); ?></h3>
    15801785
    1581                                                         <?php bbp_get_template_part( 'loop',       'topics'    ); ?>
     1786                                                        <?php bbp_get_template_part( 'content', 'single-forum' ); ?>
    15821787
    1583                                                         <?php bbp_get_template_part( 'pagination', 'topics'    ); ?>
    1584 
    1585                                                         <?php bbp_get_template_part( 'form',       'topic'     ); ?>
    1586 
    15871788                                                <?php else : ?>
    15881789
    15891790                                                        <?php bbp_get_template_part( 'feedback',   'no-topics' ); ?>
    1590 
     1791       
    15911792                                                        <?php bbp_get_template_part( 'form',       'topic'     ); ?>
    15921793
    15931794                                                <?php endif;
    15941795
    1595                                         // More than 1 forum found
    1596                                         elseif ( $bbp->forum_query->post_count > 1 ) : ?>
     1796                                        // More than 1 forum found or group forum admin screen
     1797                                        elseif ( 1 < $bbp->forum_query->post_count || bp_is_group_admin_screen( $this->slug ) ) : ?>
    15971798
    15981799                                                <h3><?php _e( 'Forums', 'bbpress' ); ?></h3>
    15991800
     
    16261827                                                        <p><?php _e( 'This group does not currently have any forums.', 'bbpress' ); ?></p>
    16271828                                                </div>
    16281829
    1629                                                 <?php if ( bp_is_group_admin_screen( $this->slug ) ) :
    1630                                                         bbp_get_template_part( 'form', 'forum' );
    1631                                                 endif;
     1830                                                <?php if ( bp_is_group_admin_screen( $this->slug ) ) : ?>
     1831
     1832                                                        <?php bbp_get_template_part( 'form', 'forum' ); ?>
     1833
     1834                                                <?php endif;
     1835
    16321836                                        endif;
    16331837
    16341838                                // No forums found
     
    16381842                                                <p><?php _e( 'This group does not currently have any forums.', 'bbpress' ); ?></p>
    16391843                                        </div>
    16401844
    1641                                         <?php if ( bp_is_group_admin_screen( $this->slug ) ) :
    1642                                                 bbp_get_template_part( 'form', 'forum' );
    1643                                         endif;
     1845                                        <?php if ( bp_is_group_admin_screen( $this->slug ) ) : ?>
     1846
     1847                                                <?php bbp_get_template_part( 'form', 'forum' ); ?>
     1848
     1849                                        <?php endif;
     1850
    16441851                                endif;
    16451852
    16461853                        // Single forum
    1647                         elseif ( ( bp_action_variable( $offset ) != $this->slug ) && ( bp_action_variable( $offset ) != $this->topic_slug ) ) :
     1854                        elseif ( bp_action_variable( $offset ) != $this->slug && bp_action_variable( $offset ) != $this->topic_slug && bp_action_variable( $offset ) != $this->reply_slug ) :
    16481855
    1649                                 // Get the forum
    1650                                 $forum_post_type = bbp_get_forum_post_type();
    1651                                 $forum_slug      = bp_action_variable( $offset );
    1652                                 $forums          = $wpdb->get_row( "SELECT ID FROM {$wpdb->posts} WHERE post_name = '{$forum_slug}' AND post_type = '{$forum_post_type}'", ARRAY_N );
     1856                                // Get forum data
     1857                                $forum_slug = bp_action_variable( $offset );
     1858                                $forum_args = array( 'name' => $forum_slug, 'post_type' => bbp_get_forum_post_type() );
     1859                                $forums     = get_posts( $forum_args );
    16531860
    1654                                 // Forum exists
    1655                                 if ( !empty( $forums ) ) :
    1656                                         $forum_id              = $forums[0];
    1657                                         $bbp->current_forum_id = $forum_id;
    1658                                         bbp_set_query_name( 'bbp_single_forum' ); ?>
     1861                                // Forum exists
     1862                                if ( !empty( $forums ) ) :
     1863                                        $forum = $forums[0];
    16591864
    1660                                         <h3><?php bbp_forum_title(); ?></h3>
     1865                                        // Set up forum data
     1866                                        $forum_id = bbpress()->current_forum_id = $forum->ID;
    16611867
    1662                                         <?php bbp_get_template_part( 'content', 'single-forum' ); ?>
     1868                                        // Reset necessary forum_query attributes for forums loop to function
     1869                                        $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
     1870                                        $bbp->forum_query->in_the_loop             = true;
     1871                                        $bbp->forum_query->post                    = get_post( $forum_id );
    16631872
     1873                                        // Forum edit
     1874                                        if ( bp_action_variable( $offset + 1 ) == $bbp->edit_id ) :
     1875                                                global $wp_query, $post;
     1876                                                $wp_query->bbp_is_edit       = true;
     1877                                                $wp_query->bbp_is_forum_edit = true;
     1878                                                $post                        = $forum;
     1879                                                bbp_set_query_name( 'bbp_forum_form' ); ?>
     1880
     1881                                                <?php bbp_get_template_part( 'form', 'forum' ); ?>
     1882
     1883                                        <?php else :
     1884                                                bbp_set_query_name( 'bbp_single_forum' ); ?>
     1885
     1886                                                <h3><?php bbp_forum_title(); ?></h3>
     1887
     1888                                                <?php bbp_get_template_part( 'content', 'single-forum' ); ?>
     1889
     1890                                        <?php endif; ?>
     1891
    16641892                                <?php else : ?>
    16651893
    1666                                         <?php bbp_get_template_part( 'feedback', 'no-topics'  ); ?>
     1894                                        <?php bbp_get_template_part( 'feedback',   'no-topics' ); ?>
    16671895
    1668                                         <?php bbp_get_template_part( 'form',     'topic'       ); ?>
     1896                                        <?php bbp_get_template_part( 'form',       'topic'     ); ?>
    16691897
    16701898                                <?php endif;
    16711899
    16721900                        // Single topic
    1673                         elseif ( ( bp_action_variable( $offset ) != $this->slug ) && ( bp_action_variable( $offset ) == $this->topic_slug ) ) :
     1901                        elseif ( bp_action_variable( $offset ) == $this->topic_slug ) :
    16741902
    1675                                 // Get the topic
    1676                                 $topic_post_type = bbp_get_topic_post_type();
    1677                                 $topic_slug      = bp_action_variable( $offset + 1 );
    1678                                 $topics          = $wpdb->get_row( "SELECT ID FROM {$wpdb->posts} WHERE post_name = '{$topic_slug}' AND post_type = '{$topic_post_type}'", ARRAY_N );
     1903                                // Get topic data
     1904                                $topic_slug = bp_action_variable( $offset + 1 );
     1905                                $topic_args = array( 'name' => $topic_slug, 'post_type' => bbp_get_topic_post_type(), 'post_status' => $post_status );
     1906                                $topics     = get_posts( $topic_args );
    16791907
    16801908                                // Topic exists
    16811909                                if ( !empty( $topics ) ) :
    1682                                         $topic_id              = $topics[0];
    1683                                         $bbp->current_topic_id = $topic_id;
    1684                                         bbp_set_query_name( 'bbp_single_topic' ); ?>
     1910                                        $topic = $topics[0];
    16851911
    1686                                         <h3><?php bbp_topic_title(); ?></h3>
     1912                                        // Set up topic data
     1913                                        $topic_id = bbpress()->current_topic_id = $topic->ID;
     1914                                        $forum_id = bbp_get_topic_forum_id( $topic_id );
     1915       
     1916                                        // Reset necessary forum_query attributes for topics loop to function
     1917                                        $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
     1918                                        $bbp->forum_query->in_the_loop             = true;
     1919                                        $bbp->forum_query->post                    = get_post( $forum_id );
    16871920
    1688                                         <?php bbp_get_template_part( 'content', 'single-topic' ); ?>
     1921                                        // Reset necessary topic_query attributes for topics loop to function
     1922                                        $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
     1923                                        $bbp->topic_query->in_the_loop             = true;
     1924                                        $bbp->topic_query->post                    = $topic;
    16891925
    1690                                 <?php else : ?>
     1926                                        // Topic edit
     1927                                        if ( bp_action_variable( $offset + 2 ) == $bbp->edit_id ) :
     1928                                                global $wp_query, $post;
     1929                                                $wp_query->bbp_is_edit       = true;
     1930                                                $wp_query->bbp_is_topic_edit = true;
     1931                                                $post                        = $topic;
     1932       
     1933                                                // Merge
     1934                                                if ( !empty( $_GET['action'] ) && 'merge' == $_GET['action'] ) :
     1935                                                        bbp_set_query_name( 'bbp_topic_merge' ); ?>
    16911936
     1937                                                        <?php bbp_get_template_part( 'form', 'topic-merge' ); ?>
     1938
     1939                                                <?php // Split
     1940                                                elseif ( !empty( $_GET['action'] ) && 'split' == $_GET['action'] ) :
     1941                                                        bbp_set_query_name( 'bbp_topic_split' ); ?>
     1942
     1943                                                        <?php bbp_get_template_part( 'form', 'topic-split' ); ?>
     1944       
     1945                                                <?php // Edit
     1946                                                else :
     1947                                                        bbp_set_query_name( 'bbp_topic_form' ); ?>
     1948
     1949                                                        <?php bbp_get_template_part( 'form', 'topic' ); ?>
     1950
     1951                                                <?php endif;
     1952
     1953                                        else:
     1954
     1955                                                bbp_set_query_name( 'bbp_single_topic' ); ?>
     1956
     1957                                                <h3><?php bbp_topic_title(); ?></h3>
     1958
     1959                                                <?php bbp_get_template_part( 'content', 'single-topic' ); ?>
     1960
     1961                                        <?php endif;
     1962
     1963                                else : ?>
     1964
    16921965                                        <?php bbp_get_template_part( 'feedback', 'no-topics'   ); ?>
    16931966
    16941967                                        <?php bbp_get_template_part( 'form',     'topic'       ); ?>
    16951968
    16961969                                <?php endif;
     1970       
     1971                        // Single reply
     1972                        elseif ( bp_action_variable( $offset ) == $this->reply_slug ) :
    16971973
     1974                                // Get reply data
     1975                                $reply_slug = bp_action_variable( $offset + 1 );
     1976                                $reply_args = array( 'name' => $reply_slug, 'post_type' => bbp_get_reply_post_type() );
     1977                                $replies    = get_posts( $reply_args );
     1978                                if ( !$replies )
     1979                                        return;
     1980                                $reply = $replies[0];
     1981
     1982                                // Set up reply data
     1983                                $reply_id = bbpress()->current_reply_id = $reply->ID;
     1984                                $topic_id = bbp_get_reply_topic_id( $reply_id );
     1985                                $forum_id = bbp_get_reply_forum_id( $reply_id );
     1986
     1987                                // Reset necessary forum_query attributes for reply to function
     1988                                $bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
     1989                                $bbp->forum_query->in_the_loop             = true;
     1990                                $bbp->forum_query->post                    = get_post( $forum_id );
     1991
     1992                                // Reset necessary topic_query attributes for reply to function
     1993                                $bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
     1994                                $bbp->topic_query->in_the_loop             = true;
     1995                                $bbp->topic_query->post                    = get_post( $topic_id );
     1996
     1997                                // Reset necessary reply_query attributes for reply to function
     1998                                $bbp->reply_query->query_vars['post_type'] = bbp_get_reply_post_type();
     1999                                $bbp->reply_query->in_the_loop             = true;
     2000                                $bbp->reply_query->post                    = $reply;
     2001
     2002                                if ( bp_action_variable( $offset + 2 ) == $bbp->edit_id ) :
     2003                                        bbp_set_query_name( 'bbp_reply_form' );
     2004                                        global $wp_query, $post;
     2005                                        $wp_query->bbp_is_edit       = true;
     2006                                        $wp_query->bbp_is_reply_edit = true;
     2007                                        $post                        = $reply; ?>
     2008
     2009                                        <?php bbp_get_template_part( 'form', 'reply' ); ?>
     2010
     2011                                <?php endif;
     2012
    16982013                        endif; ?>
    16992014
    17002015                </div>