Skip to:
Content

BuddyPress.org


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.