Skip to:
Content

BuddyPress.org

Changeset 13448


Ignore:
Timestamp:
04/07/2023 07:47:34 AM (3 years ago)
Author:
imath
Message:

Do 404 when trying to reach a page about an unregistered group type

Improves the way we handle this case making sure we really end up to a
404 if the corresponding group type is not registered or do not support
directory filtering. Update corresponding PHP unit tests to really test
this case instead of a case about a member type.

Closes https://github.com/buddypress/buddypress/pull/82
Fixes #8866

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-template.php

    r13443 r13448  
    27802780 */
    27812781function bp_is_groups_directory() {
    2782         if ( bp_is_groups_component() && ! bp_is_group() && ( ! bp_current_action() || ( bp_action_variable() && bp_is_current_action( bp_get_groups_group_type_base() ) ) ) ) {
    2783                 return true;
    2784         }
    2785 
    2786         return false;
     2782        $return = false;
     2783
     2784        if ( bp_is_groups_component() && ! bp_is_group() ) {
     2785                $return = ! bp_current_action() || ! empty( buddypress()->groups->current_directory_type );
     2786        }
     2787
     2788        return $return;
    27872789}
    27882790
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r13441 r13448  
    322322                }
    323323
    324                 // Set group type if available.
    325                 if ( bp_is_groups_directory() && bp_is_current_action( bp_get_groups_group_type_base() ) && bp_action_variable() ) {
    326                         $matched_types = bp_groups_get_group_types( array(
    327                                 'has_directory'  => true,
    328                                 'directory_slug' => bp_action_variable(),
    329                         ) );
    330 
    331                         // Set 404 if we do not have a valid group type.
    332                         if ( empty( $matched_types ) ) {
    333                                 bp_do_404();
    334                                 return;
    335                         }
    336 
    337                         // Set our directory type marker.
    338                         $this->current_directory_type = reset( $matched_types );
    339                 }
    340 
    341324                // Set up variables specific to the group creation process.
    342325                if ( bp_is_groups_component() && bp_is_current_action( 'create' ) && bp_user_can_create_groups() && isset( $_COOKIE['bp_new_group_id'] ) ) {
    343326                        $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id'];
    344327                }
     328
     329                // The base slug to filter the groups directory according to a group type.
     330                $group_type_base = bp_get_groups_group_type_base();
    345331
    346332                /**
     
    351337                 * @param array $value Array of illegal group names/slugs.
    352338                 */
    353                 $this->forbidden_names = apply_filters( 'groups_forbidden_names', array(
    354                         'my-groups',
    355                         'create',
    356                         'invites',
    357                         'send-invites',
    358                         'forum',
    359                         'delete',
    360                         'add',
    361                         'admin',
    362                         'request-membership',
    363                         'members',
    364                         'settings',
    365                         'avatar',
    366                         $this->slug,
    367                         $this->root_slug,
    368                 ) );
     339                $this->forbidden_names = apply_filters(
     340                        'groups_forbidden_names',
     341                        array(
     342                                'my-groups',
     343                                'create',
     344                                'invites',
     345                                'send-invites',
     346                                'forum',
     347                                'delete',
     348                                'add',
     349                                'admin',
     350                                'request-membership',
     351                                'members',
     352                                'settings',
     353                                'avatar',
     354                                $this->slug,
     355                                $this->root_slug,
     356                                $group_type_base,
     357                        )
     358                );
    369359
    370360                // If the user was attempting to access a group, but no group by that name was found, 404.
    371                 if ( bp_is_groups_component() && empty( $this->current_group ) && empty( $this->current_directory_type ) && bp_current_action() && ! in_array( bp_current_action(), $this->forbidden_names ) ) {
    372                         bp_do_404();
    373                         return;
     361                if ( bp_is_groups_component() && empty( $this->current_group ) && bp_current_action() ) {
     362
     363                        // Set group type if available.
     364                        if ( bp_is_current_action( bp_get_groups_group_type_base() ) && bp_action_variable() ) {
     365                                $matched_type  = '';
     366                                $matched_types = bp_groups_get_group_types(
     367                                        array(
     368                                                'has_directory'  => true,
     369                                                'directory_slug' => bp_action_variable(),
     370                                        )
     371                                );
     372
     373                                // Set our directory type marker.
     374                                if ( ! empty( $matched_types ) ) {
     375                                        $this->current_directory_type = reset( $matched_types );
     376                                }
     377                        }
     378
     379                        if ( ! $this->current_directory_type && ! in_array( bp_current_action(), $this->forbidden_names, true ) ) {
     380                                bp_do_404();
     381                                return;
     382                        }
    374383                }
    375384
  • trunk/src/bp-groups/classes/class-bp-groups-theme-compat.php

    r12647 r13448  
    3737
    3838                // Bail if not looking at a group.
    39                 if ( ! bp_is_groups_component() )
     39                if ( ! bp_is_groups_component() ) {
    4040                        return;
     41                }
    4142
    4243                // Group Directory.
  • trunk/tests/phpunit/testcases/routing/groups.php

    r13433 r13448  
    7575        public function test_group_directory_should_404_for_group_types_that_have_no_directory() {
    7676                $this->set_permalink_structure( '/%postname%/' );
    77                 bp_register_member_type( 'foo', array( 'has_directory' => false ) );
    78                 $this->go_to( bp_get_members_directory_permalink() . 'type/foo/' );
    79                 $this->assertTrue( is_404() );
     77                bp_groups_register_group_type( 'taz', array( 'has_directory' => false ) );
     78                $this->go_to( bp_get_groups_directory_permalink() . 'type/taz/' );
     79                $this->assertEmpty( bp_get_current_group_directory_type() );
    8080        }
    8181
     
    8585        public function test_group_directory_should_404_for_invalid_group_types() {
    8686                $this->set_permalink_structure( '/%postname%/' );
    87                 $this->go_to( bp_get_members_directory_permalink() . 'type/foo/' );
    88                 $this->assertTrue( is_404() );
     87                $this->go_to( bp_get_groups_directory_permalink() . 'type/zat/' );
     88                $this->assertEmpty( bp_get_current_group_directory_type() );
    8989        }
    9090
Note: See TracChangeset for help on using the changeset viewer.