Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/20/2023 10:35:25 AM (4 years ago)
Author:
imath
Message:

Implement a parse_query() method for the Groups component

Props r-a-y, johnjamesjacoby, boonebgorges

Closes ​https://github.com/buddypress/buddypress/pull/88
See #4954

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r13451 r13456  
    232232
    233233        /**
    234          * Set up additional globals for the component.
    235          *
    236          * @since 10.0.0
    237          */
    238         public function setup_additional_globals() {
     234         * Sets up the current (displayed) group it it exists.
     235         *
     236         * @since 12.0.0
     237         *
     238         * @param string $group_slug The current action which is possibly a group slug.
     239         * @return BP_Groups_Group|Object|integer A group's object or 0 if no groups were found.
     240         */
     241        public function setup_current_group( $group_slug = '' ) {
     242                if ( ! bp_is_groups_component() || ! $group_slug ) {
     243                        return 0;
     244                }
     245
     246                // Get the BuddyPress main instance.
    239247                $bp = buddypress();
    240248
    241                 /* Single Group Globals **********************************************/
    242 
    243                 // Are we viewing a single group?
    244                 if ( bp_is_groups_component()
    245                         && ( ( $group_id = BP_Groups_Group::group_exists( bp_current_action() ) )
    246                                 || ( $group_id = BP_Groups_Group::get_id_by_previous_slug( bp_current_action() ) ) )
    247                         ) {
    248                         $bp->is_single_item  = true;
    249 
     249                // Try to find a group ID matching the requested slug.
     250                $group_id = BP_Groups_Group::group_exists( $group_slug );
     251                if ( ! $group_id ) {
     252                        $group_id = BP_Groups_Group::get_id_by_previous_slug( $group_slug );
     253                }
     254
     255                // The Group was not found.
     256                if ( ! $group_id ) {
     257                        return 0;
     258                }
     259
     260                // Set BP single item's global.
     261                $bp->is_single_item = true;
     262
     263                /**
     264                 * Filters the current PHP Class being used.
     265                 *
     266                 * @since 1.5.0
     267                 *
     268                 * @param string $value Name of the class being used.
     269                 */
     270                $current_group_class = apply_filters( 'bp_groups_current_group_class', 'BP_Groups_Group' );
     271
     272                if ( $current_group_class == 'BP_Groups_Group' ) {
     273                        $current_group = groups_get_group( $group_id );
     274
     275                } else {
    250276                        /**
    251                          * Filters the current PHP Class being used.
     277                         * Filters the current group object being instantiated from previous filter.
    252278                         *
    253279                         * @since 1.5.0
    254280                         *
    255                          * @param string $value Name of the class being used.
     281                         * @param object $value Newly instantiated object for the group.
    256282                         */
    257                         $current_group_class = apply_filters( 'bp_groups_current_group_class', 'BP_Groups_Group' );
    258 
    259                         if ( $current_group_class == 'BP_Groups_Group' ) {
    260                                 $this->current_group = groups_get_group( $group_id );
    261 
    262                         } else {
    263 
    264                                 /**
    265                                  * Filters the current group object being instantiated from previous filter.
    266                                  *
    267                                  * @since 1.5.0
    268                                  *
    269                                  * @param object $value Newly instantiated object for the group.
    270                                  */
    271                                 $this->current_group = apply_filters( 'bp_groups_current_group_object', new $current_group_class( $group_id ) );
    272                         }
    273 
    274                         // Make sure the Group ID is an integer.
    275                         $this->current_group->id = (int) $this->current_group->id;
    276 
    277                         // When in a single group, the first action is bumped down one because of the
    278                         // group name, so we need to adjust this and set the group name to current_item.
    279                         $bp->current_item   = bp_current_action();
    280                         $bp->current_action = bp_action_variable( 0 );
    281                         array_shift( $bp->action_variables );
    282 
    283                         // Using "item" not "group" for generic support in other components.
    284                         if ( bp_current_user_can( 'bp_moderate' ) ) {
    285                                 bp_update_is_item_admin( true, 'groups' );
    286                         } else {
    287                                 bp_update_is_item_admin( groups_is_user_admin( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
    288                         }
    289 
    290                         // If the user is not an admin, check if they are a moderator.
    291                         if ( ! bp_is_item_admin() ) {
    292                                 bp_update_is_item_mod( groups_is_user_mod( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
    293                         }
    294 
    295                         // Check once if the current group has a custom front template.
    296                         $this->current_group->front_template = bp_groups_get_front_template( $this->current_group );
    297 
    298                         /**
    299                          * Fires once the `current_group` global is fully set.
    300                          *
    301                          * @since 10.0.0
    302                          *
    303                          * @param BP_Groups_Group|object $current_group The current group object.
    304                          */
    305                         do_action_ref_array( 'bp_groups_set_current_group', array( $this->current_group ) );
    306 
    307                         // Initialize the nav for the groups component.
    308                         $this->nav = new BP_Core_Nav( $this->current_group->id, $this->id );
    309 
    310                 // Set current_group to 0 to prevent debug errors.
     283                        $current_group = apply_filters( 'bp_groups_current_group_object', new $current_group_class( $group_id ) );
     284                }
     285
     286                if ( ! isset( $current_group->id ) || ! $current_group->id ) {
     287                        return 0;
     288                }
     289
     290                // Make sure the Group ID is an integer.
     291                $current_group->id = (int) $current_group->id;
     292
     293                /**
     294                 * When in a single group, the first action is bumped down one because of the
     295                 * group name, so we need to adjust this and set the group name to current_item.
     296                 */
     297                $bp->current_item   = bp_current_action();
     298                $bp->current_action = bp_action_variable( 0 );
     299                array_shift( $bp->action_variables );
     300
     301                // Using "item" not "group" for generic support in other components.
     302                if ( bp_current_user_can( 'bp_moderate' ) ) {
     303                        bp_update_is_item_admin( true, 'groups' );
    311304                } else {
    312                         $this->current_group = 0;
    313                 }
     305                        bp_update_is_item_admin( groups_is_user_admin( bp_loggedin_user_id(), $current_group->id ), 'groups' );
     306                }
     307
     308                // If the user is not an admin, check if they are a moderator.
     309                if ( ! bp_is_item_admin() ) {
     310                        bp_update_is_item_mod( groups_is_user_mod( bp_loggedin_user_id(), $current_group->id ), 'groups' );
     311                }
     312
     313                // Check once if the current group has a custom front template.
     314                $current_group->front_template = bp_groups_get_front_template( $current_group );
     315
     316                /**
     317                 * Fires once the `current_group` global is fully set.
     318                 *
     319                 * @since 10.0.0
     320                 *
     321                 * @param BP_Groups_Group|object $current_group The current group object.
     322                 */
     323                do_action_ref_array( 'bp_groups_set_current_group', array( $current_group ) );
     324
     325                // Initialize the nav for the groups component.
     326                $this->nav = new BP_Core_Nav( $current_group->id, $this->id );
     327
     328                // Finally return the current group.
     329                return $current_group;
     330        }
     331
     332        /**
     333         * Set up additional globals for the component.
     334         *
     335         * @since 10.0.0
     336         */
     337        public function setup_additional_globals() {
     338                $bp = buddypress();
     339
     340                // Are we viewing a single group?
     341                $this->current_group = $this->setup_current_group( bp_current_action() );
    314342
    315343                // Set up variables specific to the group creation process.
    … …  
    10091037
    10101038        /**
     1039         * Parse the WP_Query and eventually display the component's directory or single item.
     1040         *
     1041         * @since 12.0.0
     1042         *
     1043         * @param WP_Query $query Required. See BP_Component::parse_query() for
     1044         *                        description.
     1045         */
     1046        public function parse_query( $query ) {
     1047                if ( bp_is_directory_homepage( $this->id ) ) {
     1048                        $query->set( $this->rewrite_ids['directory'], 1 );
     1049                }
     1050
     1051                if ( 1 === (int) $query->get( $this->rewrite_ids['directory'] ) ) {
     1052                        $bp                    = buddypress();
     1053                        $group_type            = false;
     1054                        $bp->current_component = 'groups';
     1055                        $group_slug            = $query->get( $this->rewrite_ids['single_item'] );
     1056                        $group_type_slug       = $query->get( $this->rewrite_ids['directory_type'] );
     1057                        $is_group_create       = 1 === (int) $query->get( $this->rewrite_ids['create_single_item'] );
     1058
     1059                        if ( $group_slug ) {
     1060                                $this->current_group = $this->setup_current_group( $group_slug );
     1061
     1062                                if ( ! $this->current_group ) {
     1063                                        $bp->current_component = false;
     1064                                        bp_do_404();
     1065                                        return;
     1066                                }
     1067
     1068                                // Set the current item using the group slug.
     1069                                $bp->current_item = $group_slug;
     1070
     1071                                $current_action = $query->get( $this->rewrite_ids['single_item_action'] );
     1072                                if ( $current_action ) {
     1073                                        $context = 'bp_group_read_';
     1074
     1075                                        // Get the rewrite ID corresponfing to the custom slug.
     1076                                        $current_action_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $current_action, $context );
     1077
     1078                                        if ( $current_action_rewrite_id ) {
     1079                                                $current_action = str_replace( $context, '', $current_action_rewrite_id );
     1080
     1081                                                // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
     1082                                                $current_action = str_replace( '_', '-', $current_action );
     1083                                        }
     1084
     1085                                        // Set the BuddyPress global.
     1086                                        $bp->current_action = $current_action;
     1087                                }
     1088
     1089                                $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] );
     1090                                if ( $action_variables ) {
     1091                                        if ( ! is_array( $action_variables ) ) {
     1092                                                $action_variables = explode( '/', ltrim( $action_variables, '/' ) );
     1093                                        }
     1094
     1095                                        // In the Manage context, we need to translate custom slugs to BP Expected variables.
     1096                                        if ( 'admin' === $bp->current_action ) {
     1097                                                $context = 'bp_group_manage_';
     1098
     1099                                                // Get the rewrite ID corresponfing to the custom slug.
     1100                                                $first_action_variable_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $action_variables[0], $context );
     1101
     1102                                                if ( $first_action_variable_rewrite_id ) {
     1103                                                        $first_action_variable = str_replace( $context, '', $first_action_variable_rewrite_id );
     1104
     1105                                                        // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
     1106                                                        $action_variables[0] = str_replace( '_', '-', $first_action_variable );
     1107                                                }
     1108                                        }
     1109
     1110                                        // Set the BuddyPress global.
     1111                                        $bp->action_variables = $action_variables;
     1112                                }
     1113                        } elseif ( $group_type_slug ) {
     1114                                $group_type = bp_groups_get_group_types(
     1115                                        array(
     1116                                                'has_directory'  => true,
     1117                                                'directory_slug' => $group_type_slug,
     1118                                        )
     1119                                );
     1120
     1121                                if ( $group_type ) {
     1122                                        $group_type                   = reset( $group_type );
     1123                                        $this->current_directory_type = $group_type;
     1124                                        $bp->current_action           = bp_get_groups_group_type_base();
     1125                                        $bp->action_variables         = array( $group_type_slug );
     1126                                } else {
     1127                                        $bp->current_component = false;
     1128                                        bp_do_404();
     1129                                        return;
     1130                                }
     1131                        } elseif ( $is_group_create ) {
     1132                                $bp->current_action = 'create';
     1133
     1134                                if ( bp_user_can_create_groups() && isset( $_COOKIE['bp_new_group_id'] ) ) {
     1135                                        $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id'];
     1136                                }
     1137
     1138                                $create_variables = $query->get( $this->rewrite_ids['create_single_item_variables'] );
     1139                                if ( $create_variables ) {
     1140                                        $context          = 'bp_group_create_';
     1141                                        $action_variables = array();
     1142
     1143                                        if ( ! is_array( $create_variables ) ) {
     1144                                                $action_variables = explode( '/', ltrim( $create_variables, '/' ) );
     1145                                        } else {
     1146                                                $action_variables = $create_variables;
     1147                                        }
     1148
     1149                                        // The slug of the step is the second action variable.
     1150                                        if ( isset( $action_variables[1] ) && $action_variables[1] ) {
     1151                                                // Get the rewrite ID corresponfing to the custom slug.
     1152                                                $second_action_variable_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $action_variables[1], $context );
     1153
     1154                                                // Reset the action variable with BP Default create step slug.
     1155                                                if ( $second_action_variable_rewrite_id ) {
     1156                                                        $second_action_variable = str_replace( $context, '', $second_action_variable_rewrite_id );
     1157
     1158                                                        // Make sure the action is stored as a slug: underscores need to be replaced by dashes.
     1159                                                        $action_variables[1] = str_replace( '_', '-', $second_action_variable );
     1160                                                }
     1161                                        }
     1162
     1163                                        $bp->action_variables = $action_variables;
     1164                                }
     1165                        }
     1166
     1167                        /**
     1168                         * Set the BuddyPress queried object.
     1169                         */
     1170                        if ( isset( $bp->pages->groups->id ) ) {
     1171                                $query->queried_object    = get_post( $bp->pages->groups->id );
     1172                                $query->queried_object_id = $query->queried_object->ID;
     1173
     1174                                if ( $this->current_group ) {
     1175                                        $query->queried_object->single_item_name = $this->current_group->name;
     1176                                } elseif ( $group_type ) {
     1177                                        $query->queried_object->directory_type_name = $group_type;
     1178                                }
     1179                        }
     1180                }
     1181
     1182                parent::parse_query( $query );
     1183        }
     1184
     1185        /**
    10111186         * Init the BP REST API.
    10121187         *
Note: See TracChangeset for help on using the changeset viewer.