Skip to:
Content

bbPress.org

Changeset 7878


Ignore:
Timestamp:
10/08/2026 07:58:39 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 trunk, for 2.7.

Props r-a-y.
See #3430.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.md

    r7872 r7878  
    4848### Fixed
    4949
     50- Made pending BuddyPress group topics viewable to their authors and moderators
     51  inside the group forum.
    5052- Prevented unknown views from falling back to unrestricted topic queries.
    5153- Informed authors when newly submitted topics or replies are held for
  • trunk/src/includes/extend/buddypress/functions.php

    r7693 r7878  
    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
    … …  
    667680        // Filter & return
    668681        return (bool) apply_filters( 'bbp_is_forum_group_forum', $retval, $forum_id, $group_ids );
     682}
     683
     684/** Group Topic Routing ******************************************************/
     685
     686/**
     687 * Return whether the current group route identifies a pending topic by ID.
     688 *
     689 * @since 2.6.20 bbPress (r7878)
     690 *
     691 * @return bool Whether this is a pending group topic route.
     692 */
     693function bbp_is_group_pending_topic() {
     694        return bp_is_group()
     695                && bp_is_current_action( 'forum' )
     696                && bp_is_action_variable( 'topic', 0 )
     697                && bp_is_action_variable( bbp_get_pending_status_id(), 1 )
     698                && isset( $_GET['bbp_topic_id'] );
     699}
     700
     701/**
     702 * Return the readable pending topic ID from the current group route.
     703 *
     704 * @since 2.6.20 bbPress (r7878)
     705 *
     706 * @return int Pending topic ID, or 0 when the route is invalid or unreadable.
     707 */
     708function bbp_get_group_pending_topic_id() {
     709
     710        // Bail if this is not a pending group topic route.
     711        if ( ! bbp_is_group_pending_topic() ) {
     712                return 0;
     713        }
     714
     715        $topic_id  = absint( wp_unslash( $_GET['bbp_topic_id'] ) );
     716        $forum_id  = bbp_get_topic_forum_id( $topic_id );
     717        $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() );
     718
     719        // Topic must be pending, belong to this group, and be readable.
     720        if ( ! bbp_is_topic_pending( $topic_id )
     721                || ! in_array( $forum_id, $forum_ids, true )
     722                || ! current_user_can( 'read_forum', $forum_id )
     723                || ! current_user_can( 'read_topic', $topic_id )
     724        ) {
     725                $topic_id = 0;
     726        }
     727
     728        return (int) apply_filters( 'bbp_get_group_pending_topic_id', $topic_id );
    669729}
    670730
  • trunk/src/includes/extend/buddypress/groups.php

    r7851 r7878  
    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
    … …  
    14171420                                        add_filter( 'bbp_get_topic_stick_link', array( $this, 'hide_super_sticky_admin_link' ), 10, 2 );
    14181421
     1422                                        $topic_args = array(
     1423                                                'name'           => bp_action_variable( $offset + 1 ),
     1424                                                'posts_per_page' => 1,
     1425                                                'show_stickies'  => false
     1426                                        );
     1427
     1428                                        // Pending topics use an explicit ID within their group route.
     1429                                        if ( bbp_is_group_pending_topic() ) {
     1430                                                $topic_id                  = bbp_get_group_pending_topic_id();
     1431                                                $topic_args['post_parent'] = $forum_id;
     1432                                                $topic_args['post_status'] = bbp_get_pending_status_id();
     1433
     1434                                                if ( ! empty( $topic_id ) ) {
     1435                                                        $topic_args['p'] = $topic_id;
     1436                                                } else {
     1437                                                        $topic_args['post__in'] = array( 0 );
     1438                                                }
     1439
     1440                                                unset( $topic_args['name'] );
     1441                                        }
     1442
    14191443                                        // Get the topic
    1420                                         bbp_has_topics(
    1421                                                 array(
    1422                                                         'name'           => bp_action_variable( $offset + 1 ),
    1423                                                         'posts_per_page' => 1,
    1424                                                         'show_stickies'  => false
    1425                                                 )
    1426                                         );
     1444                                        bbp_has_topics( $topic_args );
    14271445
    14281446                                        // If no topic, 404
    … …  
    16331651         */
    16341652        public function new_topic_redirect_to( $redirect_url = '', $redirect_to = '', $topic_id = 0 ) {
    1635                 if ( bp_is_group() ) {
     1653                if ( bp_is_group() && ! bbp_is_topic_pending( $topic_id ) ) {
    16361654                        $topic        = bbp_get_topic( $topic_id );
    16371655                        $topic_hash   = '#post-' . $topic_id;
    … …  
    16491667        public function new_reply_redirect_to( $redirect_url = '', $redirect_to = '', $reply_id = 0 ) {
    16501668
    1651                 if ( bp_is_group() ) {
     1669                if ( bp_is_group() && ! bbp_is_topic_pending( bbp_get_reply_topic_id( $reply_id ) ) ) {
    16521670                        $topic_id       = bbp_get_reply_topic_id( $reply_id );
    16531671                        $topic          = bbp_get_topic( $topic_id );
    … …  
    18411859
    18421860        /**
     1861         * Get a forum route URL for a group.
     1862         *
     1863         * @since 2.6.20 bbPress (r7878)
     1864         *
     1865         * @param int   $group_id        Group ID.
     1866         * @param array $action_variables Group forum route parts.
     1867         * @return string
     1868         */
     1869        private function group_forum_url( $group_id = 0, $action_variables = array() ) {
     1870
     1871                // BuddyPress 12.0 and newer support URLs built from route parts.
     1872                if ( function_exists( 'bp_get_group_url' ) ) {
     1873                        return bp_get_group_url(
     1874                                $group_id,
     1875                                array(
     1876                                        'single_item_action'           => $this->slug,
     1877                                        'single_item_action_variables' => $action_variables,
     1878                                )
     1879                        );
     1880                }
     1881
     1882                // BuddyPress before 12.0 uses path-based group URLs.
     1883                $retval = trailingslashit( $this->group_url( $group_id ) ) . trailingslashit( $this->slug );
     1884
     1885                foreach ( $action_variables as $action_variable ) {
     1886                        $retval .= trailingslashit( $action_variable );
     1887                }
     1888
     1889                return user_trailingslashit( $retval );
     1890        }
     1891
     1892        /**
    18431893         * Get the management URL for a group.
    18441894         *
    … …  
    18761926         */
    18771927        private function maybe_map_permalink_to_group( $post_id = 0, $url = false ) {
     1928                $pending_topic_id = 0;
    18781929
    18791930                switch ( get_post_type( $post_id ) ) {
    … …  
    18881939                        case bbp_get_topic_post_type() :
    18891940                                $forum_id = bbp_get_topic_forum_id( $post_id );
    1890                                 $url_end  = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id );
     1941
     1942                                // Pending topics use an explicit ID instead of an empty post name.
     1943                                if ( bbp_is_topic_pending( $post_id ) ) {
     1944                                        $pending_topic_id = $post_id;
     1945                                        $url_end          = trailingslashit( $this->topic_slug ) . bbp_get_pending_status_id();
     1946
     1947                                // Published topics use their post name.
     1948                                } else {
     1949                                        $url_end = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id );
     1950                                }
    18911951                                break;
    18921952
    … …  
    19141974                $group    = groups_get_group( array( 'group_id' => $group_id ) );
    19151975
    1916                 // Admin
    1917                 $group_permalink = bp_is_group_admin_screen( $this->slug )
    1918                         ? $this->group_manage_url( $group )
    1919                         : $this->group_url( $group );
    1920 
    1921                 return trailingslashit( trailingslashit( trailingslashit( $group_permalink ) . $this->slug ) . $url_end );
     1976                // Build the group route.
     1977                if ( ! empty( $pending_topic_id ) ) {
     1978                        $retval = $this->group_forum_url(
     1979                                $group,
     1980                                array( $this->topic_slug, bbp_get_pending_status_id() )
     1981                        );
     1982                } else {
     1983                        $group_permalink = bp_is_group_admin_screen( $this->slug )
     1984                                ? $this->group_manage_url( $group )
     1985                                : $this->group_url( $group );
     1986
     1987                        $retval = trailingslashit( trailingslashit( trailingslashit( $group_permalink ) . $this->slug ) . $url_end );
     1988                }
     1989
     1990                // Add the pending topic ID without overloading its post name.
     1991                if ( ! empty( $pending_topic_id ) ) {
     1992                        $retval = add_query_arg( 'bbp_topic_id', $pending_topic_id, $retval );
     1993                        $retval = bbp_add_view_all( $retval, true );
     1994                }
     1995
     1996                return $retval;
    19221997        }
    19231998
    … …  
    19592034        public function map_reply_permalink_to_group( $url, $reply_id ) {
    19602035                return $this->maybe_map_permalink_to_group( bbp_get_reply_topic_id( $reply_id ), $url );
     2036        }
     2037
     2038        /**
     2039         * Map a pending topic edit URL to its group forum.
     2040         *
     2041         * @since 2.6.20 bbPress (r7878)
     2042         *
     2043         * @param string $url      Topic edit URL.
     2044         * @param int    $topic_id Topic ID.
     2045         * @return string Topic edit URL.
     2046         */
     2047        public function map_topic_edit_url_to_group( $url, $topic_id ) {
     2048
     2049                // Keep existing edit URLs for published and non-group topics.
     2050                if ( ! bbp_is_topic_pending( $topic_id ) || ! bbp_is_forum_group_forum( bbp_get_topic_forum_id( $topic_id ) ) ) {
     2051                        return $url;
     2052                }
     2053
     2054                $group_ids = bbp_get_forum_group_ids( bbp_get_topic_forum_id( $topic_id ) );
     2055
     2056                // Use the first group associated with this forum.
     2057                $group = groups_get_group( array( 'group_id' => $group_ids[0] ) );
     2058                $url   = $this->group_forum_url(
     2059                        $group,
     2060                        array( $this->topic_slug, bbp_get_pending_status_id(), bbp_get_edit_slug() )
     2061                );
     2062                $url   = add_query_arg( 'bbp_topic_id', $topic_id, $url );
     2063
     2064                return bbp_add_view_all( $url, true );
    19612065        }
    19622066
    … …  
    20572161                }
    20582162
    2059                 $args['base'] = trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
     2163                $args['base'] = bbp_is_topic_pending()
     2164                        ? add_query_arg( 'paged', '%#%', $new )
     2165                        : trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
    20602166
    20612167                return $args;
    … …  
    21432249                } elseif ( bbp_is_single_topic() ) {
    21442250                        $topic_id  = get_the_ID();
    2145                         $slug      = get_post_field( 'post_name', $topic_id );
     2251                        $slug      = bbp_is_topic_pending( $topic_id )
     2252                                ? bbp_get_pending_status_id()
     2253                                : get_post_field( 'post_name', $topic_id );
    21462254                        $forum_id  = bbp_get_topic_forum_id( $topic_id );
    21472255                        $group_ids = bbp_get_forum_group_ids( $forum_id );
    … …  
    21582266
    21592267                // Use the first group ID
    2160                 $group_id    = $group_ids[0];
    2161                 $group       = groups_get_group( array( 'group_id' => $group_id ) );
    2162                 $group_link  = trailingslashit( $this->group_url( $group ) );
    2163                 $redirect_to = trailingslashit( $group_link . $this->slug );
     2268                $group_id = $group_ids[0];
     2269                $group    = groups_get_group( array( 'group_id' => $group_id ) );
     2270
     2271                if ( bbp_is_single_topic() && bbp_is_topic_pending( $topic_id ) ) {
     2272                        $redirect_to = $this->group_forum_url(
     2273                                $group,
     2274                                array( $this->topic_slug, bbp_get_pending_status_id() )
     2275                        );
     2276                } else {
     2277                        $group_link  = trailingslashit( $this->group_url( $group ) );
     2278                        $redirect_to = trailingslashit( $group_link . $this->slug );
     2279                }
    21642280
    21652281                // Add topic slug to URL
    2166                 if ( bbp_is_single_topic() ) {
     2282                if ( bbp_is_single_topic() && ! bbp_is_topic_pending( $topic_id ) ) {
    21672283                        $redirect_to  = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug );
     2284                }
     2285
     2286                if ( bbp_is_single_topic() && bbp_is_topic_pending( $topic_id ) ) {
     2287                        $redirect_to = add_query_arg( 'bbp_topic_id', $topic_id, $redirect_to );
     2288                        $redirect_to = bbp_add_view_all( $redirect_to, true );
    21682289                }
    21692290
  • trunk/tests/phpunit/testcases/extend/buddypress/groups.php

    r7861 r7878  
    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;
    … …  
    813816                $this->group_extension = new BBP_Forums_Group_Extension();
    814817                add_filter( 'bbp_map_meta_caps', array( $this->group_extension, 'map_group_forum_meta_caps' ), 99, 4 );
     818        }
     819
     820        /**
     821         * @covers ::bbp_is_group_pending_topic
     822         * @covers ::bbp_get_group_pending_topic_id
     823         */
     824        public function test_pending_group_topic_route_respects_topic_and_group_permissions() {
     825                $creator_id    = $this->factory->user->create();
     826                $author_id     = $this->factory->user->create();
     827                $member_id     = $this->factory->user->create();
     828                $group_mod_id  = $this->factory->user->create();
     829                $moderator_id  = $this->factory->user->create();
     830                $keymaster_id  = $this->factory->user->create();
     831                $group_id       = $this->bp_factory->group->create( array( 'creator_id' => $creator_id ) );
     832                $other_group_id = $this->bp_factory->group->create();
     833                $forum_id       = $this->factory->forum->create();
     834                $other_forum_id = $this->factory->forum->create();
     835                $topic_id       = $this->factory->topic->create(
     836                        array(
     837                                'post_author' => $author_id,
     838                                'post_parent' => $forum_id,
     839                                'post_status' => bbp_get_pending_status_id(),
     840                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     841                        )
     842                );
     843                $published_topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) );
     844                $other_topic_id     = $this->factory->topic->create(
     845                        array(
     846                                'post_parent' => $other_forum_id,
     847                                'post_status' => bbp_get_pending_status_id(),
     848                                'topic_meta'  => array( 'forum_id' => $other_forum_id ),
     849                        )
     850                );
     851
     852                foreach ( array( $creator_id, $author_id, $member_id, $group_mod_id ) as $user_id ) {
     853                        bbp_set_user_role( $user_id, bbp_get_participant_role() );
     854                }
     855                bbp_set_user_role( $moderator_id, bbp_get_moderator_role() );
     856                bbp_set_user_role( $keymaster_id, bbp_get_keymaster_role() );
     857
     858                groups_join_group( $group_id, $author_id );
     859                groups_join_group( $group_id, $member_id );
     860                groups_join_group( $group_id, $group_mod_id );
     861                groups_promote_member( $group_mod_id, $group_id, 'mod', $creator_id );
     862                $this->attach_forum_to_group( $forum_id, $group_id );
     863                $this->attach_forum_to_group( $other_forum_id, $other_group_id );
     864                $this->set_group_context( $group_id, $author_id );
     865
     866                buddypress()->is_single_item   = true;
     867                buddypress()->action_variables = array( $this->group_extension->topic_slug, bbp_get_pending_status_id() );
     868                $_GET['bbp_topic_id']          = $topic_id;
     869
     870                $this->assertTrue( bbp_is_group_pending_topic() );
     871                $this->assertSame( $topic_id, bbp_get_group_pending_topic_id() );
     872
     873                foreach ( array( $creator_id, $group_mod_id, $moderator_id, $keymaster_id ) as $user_id ) {
     874                        $this->set_current_user( $user_id );
     875                        unset( bbpress()->current_user->is_group_admin, bbpress()->current_user->is_group_mod, bbpress()->current_user->is_group_banned );
     876                        $this->assertSame( $topic_id, bbp_get_group_pending_topic_id() );
     877                }
     878
     879                foreach ( array( $member_id, 0 ) as $user_id ) {
     880                        $this->set_current_user( $user_id );
     881                        unset( bbpress()->current_user->is_group_admin, bbpress()->current_user->is_group_mod, bbpress()->current_user->is_group_banned );
     882                        $this->assertSame( 0, bbp_get_group_pending_topic_id() );
     883                }
     884
     885                $this->set_current_user( $keymaster_id );
     886                $_GET['bbp_topic_id'] = $other_topic_id;
     887                $this->assertSame( 0, bbp_get_group_pending_topic_id() );
     888
     889                $_GET['bbp_topic_id'] = $published_topic_id;
     890                $this->assertSame( 0, bbp_get_group_pending_topic_id() );
     891        }
     892
     893        /**
     894         * @covers ::BBP_Forums_Group_Extension::map_topic_permalink_to_group
     895         * @covers ::BBP_Forums_Group_Extension::map_topic_edit_url_to_group
     896         */
     897        public function test_pending_topic_uses_id_route_until_published() {
     898                $author_id = $this->factory->user->create();
     899                $group_id  = $this->bp_factory->group->create( array( 'creator_id' => $author_id ) );
     900                $forum_id  = $this->factory->forum->create();
     901                $topic_id  = $this->factory->topic->create(
     902                        array(
     903                                'post_author' => $author_id,
     904                                'post_parent' => $forum_id,
     905                                'post_status' => bbp_get_pending_status_id(),
     906                                'post_title'  => 'Pending group route',
     907                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     908                        )
     909                );
     910
     911                bbp_set_user_role( $author_id, bbp_get_participant_role() );
     912                $this->attach_forum_to_group( $forum_id, $group_id );
     913                $this->set_group_context( $group_id, $author_id );
     914
     915                $topic_url = wp_parse_url( html_entity_decode( bbp_get_topic_permalink( $topic_id ) ) );
     916                $edit_url  = wp_parse_url( html_entity_decode( bbp_get_topic_edit_url( $topic_id ) ) );
     917                parse_str( $topic_url['query'], $topic_query );
     918                parse_str( $edit_url['query'], $edit_query );
     919
     920                $topic_route = isset( $topic_query['bp_group_action_variables'] )
     921                        ? '/' . $topic_query['bp_group_action'] . '/' . implode( '/', $topic_query['bp_group_action_variables'] ) . '/'
     922                        : $topic_url['path'];
     923                $edit_route  = isset( $edit_query['bp_group_action_variables'] )
     924                        ? '/' . $edit_query['bp_group_action'] . '/' . implode( '/', $edit_query['bp_group_action_variables'] ) . '/'
     925                        : $edit_url['path'];
     926
     927                $this->assertStringEndsWith( '/forum/topic/pending/', $topic_route );
     928                $this->assertStringEndsWith( '/forum/topic/pending/edit/', $edit_route );
     929                $this->assertSame( (string) $topic_id, $topic_query['bbp_topic_id'] );
     930                $this->assertSame( 'all', $topic_query['view'] );
     931                $this->assertSame( (string) $topic_id, $edit_query['bbp_topic_id'] );
     932                $this->assertSame( 'all', $edit_query['view'] );
     933
     934                $filter_topic_id = function() use ( $topic_id ) {
     935                        return $topic_id;
     936                };
     937                add_filter( 'bbp_get_topic_id', $filter_topic_id );
     938                $pagination = $this->group_extension->replies_pagination( array() );
     939                remove_filter( 'bbp_get_topic_id', $filter_topic_id );
     940                $pagination_base = html_entity_decode( $pagination['base'] );
     941                $pagination_url  = wp_parse_url( $pagination_base );
     942                parse_str( $pagination_url['query'], $pagination_query );
     943
     944                $this->assertStringContainsString( 'paged=%#%', $pagination_base );
     945                $this->assertSame( (string) $topic_id, $pagination_query['bbp_topic_id'] );
     946                $this->assertSame( 'all', $pagination_query['view'] );
     947
     948                bbp_approve_topic( $topic_id );
     949                $published_url = wp_parse_url( html_entity_decode( bbp_get_topic_permalink( $topic_id ) ) );
     950                parse_str( $published_url['query'], $published_query );
     951                $published_route = isset( $published_query['bp_group'] ) ? $published_query['bp_group'] : $published_url['path'];
     952
     953                $this->assertStringEndsWith( '/forum/topic/pending-group-route/', $published_route );
     954                $this->assertArrayNotHasKey( 'bbp_topic_id', $published_query );
    815955        }
    816956
Note: See TracChangeset for help on using the changeset viewer.