Skip to:
Content

bbPress.org

Changeset 7881


Ignore:
Timestamp:
10/08/2026 08:04:00 PM (6 weeks ago)
Author:
johnjamesjacoby
Message:

BuddyPress Groups: Restore pending topic access.

Route pending group topics by explicit topic ID while preserving their group forum context. Authors and users who can read pending topics can view and edit them with the normal group templates, while invalid and cross-group topic IDs remain unreadable.

Preserve pending-topic redirects and reply pagination without changing published topic permalinks.

In branches/2.6, for 2.6.20.

Props r-a-y.
Fixes #3430.

Location:
branches/2.6
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/CHANGELOG.md

    r7873 r7881  
    2525### Fixed
    2626
     27- Made pending BuddyPress group topics viewable to their authors and moderators
     28  inside the group forum.
    2729- Prevented unknown views from falling back to unrestricted topic queries.
    2830- Informed authors when newly submitted topics or replies are held for
  • branches/2.6/src/includes/extend/buddypress/functions.php

    r7694 r7881  
    138138                        }
    139139
     140                        $topic_args = array(
     141                                'name'            => bp_action_variable( 1 ),
     142                                'post_parent__in' => $forum_ids,
     143                                'post_status'     => array_keys( bbp_get_topic_statuses() ),
     144                                'post_type'       => bbp_get_topic_post_type(),
     145                                'numberposts'     => 1
     146                        );
     147
     148                        // Pending topics use an explicit ID within their group route.
     149                        if ( bbp_is_group_pending_topic() ) {
     150                                $topic_id = bbp_get_group_pending_topic_id();
     151
     152                                if ( ! empty( $topic_id ) ) {
     153                                        $topic_args['p'] = $topic_id;
     154                                } else {
     155                                        $topic_args['post__in'] = array( 0 );
     156                                }
     157
     158                                unset( $topic_args['name'] );
     159                        }
     160
    140161                        // Get a topic belonging to the current group's forums
    141                         $topic = get_posts(
    142                                 array(
    143                                         'name'            => bp_action_variable( 1 ),
    144                                         'post_parent__in' => $forum_ids,
    145                                         'post_status'     => array_keys( bbp_get_topic_statuses() ),
    146                                         'post_type'       => bbp_get_topic_post_type(),
    147                                         'numberposts'     => 1
    148                                 )
    149                         );
     162                        $topic = get_posts( $topic_args );
    150163
    151164                        // Add the title only when the topic and its forum are readable
    … …  
    650663        // Filter & return
    651664        return (bool) apply_filters( 'bbp_is_forum_group_forum', $retval, $forum_id, $group_ids );
     665}
     666
     667/** Group Topic Routing ******************************************************/
     668
     669/**
     670 * Return whether the current group route identifies a pending topic by ID.
     671 *
     672 * @since 2.6.20 bbPress (r7881)
     673 *
     674 * @return bool Whether this is a pending group topic route.
     675 */
     676function bbp_is_group_pending_topic() {
     677        return bp_is_group()
     678                && bp_is_current_action( 'forum' )
     679                && bp_is_action_variable( 'topic', 0 )
     680                && bp_is_action_variable( bbp_get_pending_status_id(), 1 )
     681                && isset( $_GET['bbp_topic_id'] );
     682}
     683
     684/**
     685 * Return the readable pending topic ID from the current group route.
     686 *
     687 * @since 2.6.20 bbPress (r7881)
     688 *
     689 * @return int Pending topic ID, or 0 when the route is invalid or unreadable.
     690 */
     691function bbp_get_group_pending_topic_id() {
     692
     693        // Bail if this is not a pending group topic route.
     694        if ( ! bbp_is_group_pending_topic() ) {
     695                return 0;
     696        }
     697
     698        $topic_id  = absint( wp_unslash( $_GET['bbp_topic_id'] ) );
     699        $forum_id  = bbp_get_topic_forum_id( $topic_id );
     700        $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() );
     701
     702        // Topic must be pending, belong to this group, and be readable.
     703        if ( ! bbp_is_topic_pending( $topic_id )
     704                || ! in_array( $forum_id, $forum_ids, true )
     705                || ! current_user_can( 'read_forum', $forum_id )
     706                || ! current_user_can( 'read_topic', $topic_id )
     707        ) {
     708                $topic_id = 0;
     709        }
     710
     711        return (int) apply_filters( 'bbp_get_group_pending_topic_id', $topic_id );
    652712}
    653713
  • branches/2.6/src/includes/extend/buddypress/groups.php

    r7854 r7881  
    134134                add_filter( 'bbp_get_topic_permalink',   array( $this, 'map_topic_permalink_to_group' ), 10, 2 );
    135135                add_filter( 'bbp_get_reply_permalink',   array( $this, 'map_reply_permalink_to_group' ), 10, 2 );
     136
     137                // Map pending topic edit links to their groups
     138                add_filter( 'bbp_get_topic_edit_url',     array( $this, 'map_topic_edit_url_to_group'  ), 10, 2 );
    136139
    137140                // Map reply edit links to their groups
    … …  
    14191422                                        add_filter( 'bbp_get_topic_stick_link', array( $this, 'hide_super_sticky_admin_link' ), 10, 2 );
    14201423
     1424                                        $topic_args = array(
     1425                                                'name'           => bp_action_variable( $offset + 1 ),
     1426                                                'posts_per_page' => 1,
     1427                                                'show_stickies'  => false
     1428                                        );
     1429
     1430                                        // Pending topics use an explicit ID within their group route.
     1431                                        if ( bbp_is_group_pending_topic() ) {
     1432                                                $topic_id                  = bbp_get_group_pending_topic_id();
     1433                                                $topic_args['post_parent'] = $forum_id;
     1434                                                $topic_args['post_status'] = bbp_get_pending_status_id();
     1435
     1436                                                if ( ! empty( $topic_id ) ) {
     1437                                                        $topic_args['p'] = $topic_id;
     1438                                                } else {
     1439                                                        $topic_args['post__in'] = array( 0 );
     1440                                                }
     1441
     1442                                                unset( $topic_args['name'] );
     1443                                        }
     1444
    14211445                                        // Get the topic
    1422                                         bbp_has_topics(
    1423                                                 array(
    1424                                                         'name'           => bp_action_variable( $offset + 1 ),
    1425                                                         'posts_per_page' => 1,
    1426                                                         'show_stickies'  => false
    1427                                                 )
    1428                                         );
     1446                                        bbp_has_topics( $topic_args );
    14291447
    14301448                                        // If no topic, 404
    … …  
    16341652         */
    16351653        public function new_topic_redirect_to( $redirect_url = '', $redirect_to = '', $topic_id = 0 ) {
    1636                 if ( bp_is_group() ) {
     1654                if ( bp_is_group() && ! bbp_is_topic_pending( $topic_id ) ) {
    16371655                        $topic        = bbp_get_topic( $topic_id );
    16381656                        $topic_hash   = '#post-' . $topic_id;
    … …  
    16501668        public function new_reply_redirect_to( $redirect_url = '', $redirect_to = '', $reply_id = 0 ) {
    16511669
    1652                 if ( bp_is_group() ) {
     1670                if ( bp_is_group() && ! bbp_is_topic_pending( bbp_get_reply_topic_id( $reply_id ) ) ) {
    16531671                        $topic_id       = bbp_get_reply_topic_id( $reply_id );
    16541672                        $topic          = bbp_get_topic( $topic_id );
    … …  
    18421860
    18431861        /**
     1862         * Get a forum route URL for a group.
     1863         *
     1864         * @since 2.6.20 bbPress (r7881)
     1865         *
     1866         * @param int   $group_id        Group ID.
     1867         * @param array $action_variables Group forum route parts.
     1868         * @return string
     1869         */
     1870        private function group_forum_url( $group_id = 0, $action_variables = array() ) {
     1871
     1872                // BuddyPress 12.0 and newer support URLs built from route parts.
     1873                if ( function_exists( 'bp_get_group_url' ) ) {
     1874                        return bp_get_group_url(
     1875                                $group_id,
     1876                                array(
     1877                                        'single_item_action'           => $this->slug,
     1878                                        'single_item_action_variables' => $action_variables,
     1879                                )
     1880                        );
     1881                }
     1882
     1883                // BuddyPress before 12.0 uses path-based group URLs.
     1884                $retval = trailingslashit( $this->group_url( $group_id ) ) . trailingslashit( $this->slug );
     1885
     1886                foreach ( $action_variables as $action_variable ) {
     1887                        $retval .= trailingslashit( $action_variable );
     1888                }
     1889
     1890                return user_trailingslashit( $retval );
     1891        }
     1892
     1893        /**
    18441894         * Get the management URL for a group.
    18451895         *
    … …  
    18771927         */
    18781928        private function maybe_map_permalink_to_group( $post_id = 0, $url = false ) {
     1929                $pending_topic_id = 0;
    18791930
    18801931                switch ( get_post_type( $post_id ) ) {
    … …  
    18891940                        case bbp_get_topic_post_type() :
    18901941                                $forum_id = bbp_get_topic_forum_id( $post_id );
    1891                                 $url_end  = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id );
     1942
     1943                                // Pending topics use an explicit ID instead of an empty post name.
     1944                                if ( bbp_is_topic_pending( $post_id ) ) {
     1945                                        $pending_topic_id = $post_id;
     1946                                        $url_end          = trailingslashit( $this->topic_slug ) . bbp_get_pending_status_id();
     1947
     1948                                // Published topics use their post name.
     1949                                } else {
     1950                                        $url_end = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id );
     1951                                }
    18921952                                break;
    18931953
    … …  
    19151975                $group    = groups_get_group( array( 'group_id' => $group_id ) );
    19161976
    1917                 // Admin
    1918                 $group_permalink = bp_is_group_admin_screen( $this->slug )
    1919                         ? $this->group_manage_url( $group )
    1920                         : $this->group_url( $group );
    1921 
    1922                 return trailingslashit( trailingslashit( trailingslashit( $group_permalink ) . $this->slug ) . $url_end );
     1977                // Build the group route.
     1978                if ( ! empty( $pending_topic_id ) ) {
     1979                        $retval = $this->group_forum_url(
     1980                                $group,
     1981                                array( $this->topic_slug, bbp_get_pending_status_id() )
     1982                        );
     1983                } else {
     1984                        $group_permalink = bp_is_group_admin_screen( $this->slug )
     1985                                ? $this->group_manage_url( $group )
     1986                                : $this->group_url( $group );
     1987
     1988                        $retval = trailingslashit( trailingslashit( trailingslashit( $group_permalink ) . $this->slug ) . $url_end );
     1989                }
     1990
     1991                // Add the pending topic ID without overloading its post name.
     1992                if ( ! empty( $pending_topic_id ) ) {
     1993                        $retval = add_query_arg( 'bbp_topic_id', $pending_topic_id, $retval );
     1994                        $retval = bbp_add_view_all( $retval, true );
     1995                }
     1996
     1997                return $retval;
    19231998        }
    19241999
    … …  
    19602035        public function map_reply_permalink_to_group( $url, $reply_id ) {
    19612036                return $this->maybe_map_permalink_to_group( bbp_get_reply_topic_id( $reply_id ), $url );
     2037        }
     2038
     2039        /**
     2040         * Map a pending topic edit URL to its group forum.
     2041         *
     2042         * @since 2.6.20 bbPress (r7881)
     2043         *
     2044         * @param string $url      Topic edit URL.
     2045         * @param int    $topic_id Topic ID.
     2046         * @return string Topic edit URL.
     2047         */
     2048        public function map_topic_edit_url_to_group( $url, $topic_id ) {
     2049
     2050                // Keep existing edit URLs for published and non-group topics.
     2051                if ( ! bbp_is_topic_pending( $topic_id ) || ! bbp_is_forum_group_forum( bbp_get_topic_forum_id( $topic_id ) ) ) {
     2052                        return $url;
     2053                }
     2054
     2055                $group_ids = bbp_get_forum_group_ids( bbp_get_topic_forum_id( $topic_id ) );
     2056
     2057                // Use the first group associated with this forum.
     2058                $group = groups_get_group( array( 'group_id' => $group_ids[0] ) );
     2059                $url   = $this->group_forum_url(
     2060                        $group,
     2061                        array( $this->topic_slug, bbp_get_pending_status_id(), bbp_get_edit_slug() )
     2062                );
     2063                $url   = add_query_arg( 'bbp_topic_id', $topic_id, $url );
     2064
     2065                return bbp_add_view_all( $url, true );
    19622066        }
    19632067
    … …  
    20582162                }
    20592163
    2060                 $args['base'] = trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
     2164                $args['base'] = bbp_is_topic_pending()
     2165                        ? add_query_arg( 'paged', '%#%', $new )
     2166                        : trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
    20612167
    20622168                return $args;
    … …  
    21442250                } elseif ( bbp_is_single_topic() ) {
    21452251                        $topic_id  = get_the_ID();
    2146                         $slug      = get_post_field( 'post_name', $topic_id );
     2252                        $slug      = bbp_is_topic_pending( $topic_id )
     2253                                ? bbp_get_pending_status_id()
     2254                                : get_post_field( 'post_name', $topic_id );
    21472255                        $forum_id  = bbp_get_topic_forum_id( $topic_id );
    21482256                        $group_ids = bbp_get_forum_group_ids( $forum_id );
    … …  
    21592267
    21602268                // Use the first group ID
    2161                 $group_id    = $group_ids[0];
    2162                 $group       = groups_get_group( array( 'group_id' => $group_id ) );
    2163                 $group_link  = trailingslashit( $this->group_url( $group ) );
    2164                 $redirect_to = trailingslashit( $group_link . $this->slug );
     2269                $group_id = $group_ids[0];
     2270                $group    = groups_get_group( array( 'group_id' => $group_id ) );
     2271
     2272                if ( bbp_is_single_topic() && bbp_is_topic_pending( $topic_id ) ) {
     2273                        $redirect_to = $this->group_forum_url(
     2274                                $group,
     2275                                array( $this->topic_slug, bbp_get_pending_status_id() )
     2276                        );
     2277                } else {
     2278                        $group_link  = trailingslashit( $this->group_url( $group ) );
     2279                        $redirect_to = trailingslashit( $group_link . $this->slug );
     2280                }
    21652281
    21662282                // Add topic slug to URL
    2167                 if ( bbp_is_single_topic() ) {
     2283                if ( bbp_is_single_topic() && ! bbp_is_topic_pending( $topic_id ) ) {
    21682284                        $redirect_to  = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug );
     2285                }
     2286
     2287                if ( bbp_is_single_topic() && bbp_is_topic_pending( $topic_id ) ) {
     2288                        $redirect_to = add_query_arg( 'bbp_topic_id', $topic_id, $redirect_to );
     2289                        $redirect_to = bbp_add_view_all( $redirect_to, true );
    21692290                }
    21702291
  • branches/2.6/tests/phpunit/testcases/extend/buddypress/groups.php

    r7862 r7881  
    1111
    1212        protected $old_post;
     13        protected $old_get;
    1314        protected $old_request;
    1415        protected $old_server;
    … …  
    5152
    5253                $this->old_post    = $_POST;
     54                $this->old_get     = $_GET;
    5355                $this->old_request = $_REQUEST;
    5456                $this->old_server  = $_SERVER;
    … …  
    6163        public function tearDown(): void {
    6264                $_POST            = $this->old_post;
     65                $_GET             = $this->old_get;
    6366                $_REQUEST         = $this->old_request;
    6467                $_SERVER          = $this->old_server;
    … …  
    846849                $this->group_extension = new BBP_Forums_Group_Extension();
    847850                add_filter( 'bbp_map_meta_caps', array( $this->group_extension, 'map_group_forum_meta_caps' ), 99, 4 );
     851        }
     852
     853        /**
     854         * @covers ::bbp_is_group_pending_topic
     855         * @covers ::bbp_get_group_pending_topic_id
     856         */
     857        public function test_pending_group_topic_route_respects_topic_and_group_permissions() {
     858                $creator_id    = $this->factory->user->create();
     859                $author_id     = $this->factory->user->create();
     860                $member_id     = $this->factory->user->create();
     861                $group_mod_id  = $this->factory->user->create();
     862                $moderator_id  = $this->factory->user->create();
     863                $keymaster_id  = $this->factory->user->create();
     864                $group_id       = $this->bp_factory->group->create( array( 'creator_id' => $creator_id ) );
     865                $other_group_id = $this->bp_factory->group->create();
     866                $forum_id       = $this->factory->forum->create();
     867                $other_forum_id = $this->factory->forum->create();
     868                $topic_id       = $this->factory->topic->create(
     869                        array(
     870                                'post_author' => $author_id,
     871                                'post_parent' => $forum_id,
     872                                'post_status' => bbp_get_pending_status_id(),
     873                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     874                        )
     875                );
     876                $published_topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     877                $other_topic_id     = $this->factory->topic->create(
     878                        array(
     879                                'post_parent' => $other_forum_id,
     880                                'post_status' => bbp_get_pending_status_id(),
     881                                'topic_meta'  => array( 'forum_id' => $other_forum_id ),
     882                        )
     883                );
     884
     885                foreach ( array( $creator_id, $author_id, $member_id, $group_mod_id ) as $user_id ) {
     886                        bbp_set_user_role( $user_id, bbp_get_participant_role() );
     887                }
     888                bbp_set_user_role( $moderator_id, bbp_get_moderator_role() );
     889                bbp_set_user_role( $keymaster_id, bbp_get_keymaster_role() );
     890
     891                groups_join_group( $group_id, $author_id );
     892                groups_join_group( $group_id, $member_id );
     893                groups_join_group( $group_id, $group_mod_id );
     894                groups_promote_member( $group_mod_id, $group_id, 'mod', $creator_id );
     895                $this->attach_forum_to_group( $forum_id, $group_id );
     896                $this->attach_forum_to_group( $other_forum_id, $other_group_id );
     897                $this->set_group_context( $group_id, $author_id );
     898
     899                buddypress()->is_single_item   = true;
     900                buddypress()->action_variables = array( $this->group_extension->topic_slug, bbp_get_pending_status_id() );
     901                $_GET['bbp_topic_id']          = $topic_id;
     902
     903                $this->assertTrue( bbp_is_group_pending_topic() );
     904                $this->assertSame( $topic_id, bbp_get_group_pending_topic_id() );
     905
     906                foreach ( array( $creator_id, $group_mod_id, $moderator_id, $keymaster_id ) as $user_id ) {
     907                        $this->set_current_user( $user_id );
     908                        unset( bbpress()->current_user->is_group_admin, bbpress()->current_user->is_group_mod, bbpress()->current_user->is_group_banned );
     909                        $this->assertSame( $topic_id, bbp_get_group_pending_topic_id() );
     910                }
     911
     912                foreach ( array( $member_id, 0 ) as $user_id ) {
     913                        $this->set_current_user( $user_id );
     914                        unset( bbpress()->current_user->is_group_admin, bbpress()->current_user->is_group_mod, bbpress()->current_user->is_group_banned );
     915                        $this->assertSame( 0, bbp_get_group_pending_topic_id() );
     916                }
     917
     918                $this->set_current_user( $keymaster_id );
     919                $_GET['bbp_topic_id'] = $other_topic_id;
     920                $this->assertSame( 0, bbp_get_group_pending_topic_id() );
     921
     922                $_GET['bbp_topic_id'] = $published_topic_id;
     923                $this->assertSame( 0, bbp_get_group_pending_topic_id() );
     924        }
     925
     926        /**
     927         * @covers ::BBP_Forums_Group_Extension::map_topic_permalink_to_group
     928         * @covers ::BBP_Forums_Group_Extension::map_topic_edit_url_to_group
     929         */
     930        public function test_pending_topic_uses_id_route_until_published() {
     931                $author_id = $this->factory->user->create();
     932                $group_id  = $this->bp_factory->group->create( array( 'creator_id' => $author_id ) );
     933                $forum_id  = $this->factory->forum->create();
     934                $topic_id  = $this->factory->topic->create(
     935                        array(
     936                                'post_author' => $author_id,
     937                                'post_parent' => $forum_id,
     938                                'post_status' => bbp_get_pending_status_id(),
     939                                'post_title'  => 'Pending group route',
     940                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     941                        )
     942                );
     943
     944                bbp_set_user_role( $author_id, bbp_get_participant_role() );
     945                $this->attach_forum_to_group( $forum_id, $group_id );
     946                $this->set_group_context( $group_id, $author_id );
     947
     948                $topic_url = wp_parse_url( html_entity_decode( bbp_get_topic_permalink( $topic_id ) ) );
     949                $edit_url  = wp_parse_url( html_entity_decode( bbp_get_topic_edit_url( $topic_id ) ) );
     950                parse_str( $topic_url['query'], $topic_query );
     951                parse_str( $edit_url['query'], $edit_query );
     952
     953                $topic_route = isset( $topic_query['bp_group_action_variables'] )
     954                        ? '/' . $topic_query['bp_group_action'] . '/' . implode( '/', $topic_query['bp_group_action_variables'] ) . '/'
     955                        : $topic_url['path'];
     956                $edit_route  = isset( $edit_query['bp_group_action_variables'] )
     957                        ? '/' . $edit_query['bp_group_action'] . '/' . implode( '/', $edit_query['bp_group_action_variables'] ) . '/'
     958                        : $edit_url['path'];
     959
     960                $this->assertStringEndsWith( '/forum/topic/pending/', $topic_route );
     961                $this->assertStringEndsWith( '/forum/topic/pending/edit/', $edit_route );
     962                $this->assertSame( (string) $topic_id, $topic_query['bbp_topic_id'] );
     963                $this->assertSame( 'all', $topic_query['view'] );
     964                $this->assertSame( (string) $topic_id, $edit_query['bbp_topic_id'] );
     965                $this->assertSame( 'all', $edit_query['view'] );
     966
     967                $filter_topic_id = function() use ( $topic_id ) {
     968                        return $topic_id;
     969                };
     970                add_filter( 'bbp_get_topic_id', $filter_topic_id );
     971                $pagination = $this->group_extension->replies_pagination( array() );
     972                remove_filter( 'bbp_get_topic_id', $filter_topic_id );
     973                $pagination_base = html_entity_decode( $pagination['base'] );
     974                $pagination_url  = wp_parse_url( $pagination_base );
     975                parse_str( $pagination_url['query'], $pagination_query );
     976
     977                $this->assertStringContainsString( 'paged=%#%', $pagination_base );
     978                $this->assertSame( (string) $topic_id, $pagination_query['bbp_topic_id'] );
     979                $this->assertSame( 'all', $pagination_query['view'] );
     980
     981                bbp_approve_topic( $topic_id );
     982                $published_url = wp_parse_url( html_entity_decode( bbp_get_topic_permalink( $topic_id ) ) );
     983                parse_str( $published_url['query'], $published_query );
     984                $published_route = isset( $published_query['bp_group'] ) ? $published_query['bp_group'] : $published_url['path'];
     985
     986                $this->assertStringEndsWith( '/forum/topic/pending-group-route/', $published_route );
     987                $this->assertArrayNotHasKey( 'bbp_topic_id', $published_query );
    848988        }
    849989
Note: See TracChangeset for help on using the changeset viewer.