oup', 'type' => 'thumb', 'avatar_dir' => 'group-avatars', 'alt' => __( 'Group Profile Photo', 'buddypress' ) ) ); if ( empty( $bp->bp_options_avatar ) ) { $bp->bp_options_avatar = '' . esc_attr__( 'No Group Profile Photo', 'buddypress' ) . ''; } } } parent::setup_title(); } /** * Setup cache groups * * @since 2.2.0 */ public function setup_cache_groups() { // Global groups. wp_cache_add_global_groups( array( 'bp_groups', 'bp_group_admins', 'bp_group_invite_count', 'group_meta', 'bp_groups_memberships', 'bp_groups_memberships_for_user', 'bp_group_mods', 'bp_groups_invitations_as_memberships', 'bp_groups_group_type', ) ); parent::setup_cache_groups(); } /** * Set up taxonomies. * * @since 2.6.0 * @since 7.0.0 The Group Type taxonomy is registered using the `bp_groups_register_group_type_taxonomy()` function. */ public function register_taxonomies() { // Just let BP Component fire 'bp_groups_register_taxonomies'. return parent::register_taxonomies(); } /** * Adds the Groups directory type & Group create rewrite tags. * * @since 12.0.0 * * @param array $rewrite_tags Optional. See BP_Component::add_rewrite_tags() for * description. */ public function add_rewrite_tags( $rewrite_tags = array() ) { $rewrite_tags = array( 'directory_type' => '([^/]+)', 'create_single_item' => '([1]{1,})', 'create_single_item_variables' => '(.+?)', ); parent::add_rewrite_tags( $rewrite_tags ); } /** * Adds the Groups directory type & Group create rewrite rules. * * @since 12.0.0 * * @param array $rewrite_rules Optional. See BP_Component::add_rewrite_rules() for * description. */ public function add_rewrite_rules( $rewrite_rules = array() ) { $create_slug = bp_rewrites_get_slug( 'groups', 'group_create', 'create' ); $rewrite_rules = array( 'directory_type' => array( 'regex' => $this->root_slug . '/' . bp_get_groups_group_type_base() . '/([^/]+)/?$', 'order' => 50, 'query' => 'index.php?' . $this->rewrite_ids['directory'] . '=1&' . $this->rewrite_ids['directory_type'] . '=$matches[1]', ), 'create_single_item' => array( 'regex' => $this->root_slug . '/' . $create_slug . '/?$', 'order' => 40, 'query' => 'index.php?' . $this->rewrite_ids['directory'] . '=1&' . $this->rewrite_ids['create_single_item'] . '=1', ), 'create_single_item_variables' => array( 'regex' => $this->root_slug . '/' . $create_slug . '/(.+?)/?$', 'order' =>30, 'query' => 'index.php?' . $this->rewrite_ids['directory'] . '=1&' . $this->rewrite_ids['create_single_item'] . '=1&' . $this->rewrite_ids['create_single_item_variables'] . '=$matches[1]', ), ); parent::add_rewrite_rules( $rewrite_rules ); } /** * Parse the WP_Query and eventually display the component's directory or single item. * * @since 12.0.0 * * @param WP_Query $query Required. See BP_Component::parse_query() for * description. */ public function parse_query( $query ) { /* * If BP Rewrites are not in use, no need to parse BP URI globals another time. * Legacy Parser should have already set these. */ if ( 'rewrites' !== bp_core_get_query_parser() ) { return parent::parse_query( $query ); } if ( bp_is_site_home() && bp_is_directory_homepage( $this->id ) ) { $query->set( $this->rewrite_ids['directory'], 1 ); } if ( 1 === (int) $query->get( $this->rewrite_ids['directory'] ) ) { $bp = buddypress(); $group_type = false; $bp->current_component = 'groups'; $group_slug = $query->get( $this->rewrite_ids['single_item'] ); $group_type_slug = $query->get( $this->rewrite_ids['directory_type'] ); $is_group_create = 1 === (int) $query->get( $this->rewrite_ids['create_single_item'] ); if ( $group_slug ) { $this->current_group = $this->setup_current_group( $group_slug ); if ( ! $this->current_group ) { $bp->current_component = false; bp_do_404(); return; } // Set the current item using the group slug. $bp->current_item = $group_slug; $current_action = $query->get( $this->rewrite_ids['single_item_action'] ); if ( $current_action ) { $context = 'bp_group_read_'; // Get the rewrite ID corresponfing to the custom slug. $current_action_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $current_action, $context ); if ( $current_action_rewrite_id ) { $current_action = str_replace( $context, '', $current_action_rewrite_id ); // Make sure the action is stored as a slug: underscores need to be replaced by dashes. $current_action = str_replace( '_', '-', $current_action ); } // Set the BuddyPress global. $bp->current_action = $current_action; } $action_variables = $query->get( $this->rewrite_ids['single_item_action_variables'] ); if ( $action_variables ) { if ( ! is_array( $action_variables ) ) { $action_variables = explode( '/', ltrim( $action_variables, '/' ) ); } // In the Manage context, we need to translate custom slugs to BP Expected variables. if ( 'admin' === $bp->current_action ) { $context = 'bp_group_manage_'; // Get the rewrite ID corresponfing to the custom slug. $first_action_variable_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $action_variables[0], $context ); if ( $first_action_variable_rewrite_id ) { $first_action_variable = str_replace( $context, '', $first_action_variable_rewrite_id ); // Make sure the action is stored as a slug: underscores need to be replaced by dashes. $action_variables[0] = str_replace( '_', '-', $first_action_variable ); } } // Set the BuddyPress global. $bp->action_variables = $action_variables; } } elseif ( $group_type_slug ) { $group_type = bp_groups_get_group_types( array( 'has_directory' => true, 'directory_slug' => $group_type_slug, ) ); if ( $group_type ) { $group_type = reset( $group_type ); $this->current_directory_type = $group_type; $bp->current_action = bp_get_groups_group_type_base(); $bp->action_variables = array( $group_type_slug ); } else { $bp->current_component = false; $this->current_directory_type = ''; bp_do_404(); return; } } elseif ( $is_group_create ) { $bp->current_action = 'create'; if ( bp_user_can_create_groups() && isset( $_COOKIE['bp_new_group_id'] ) ) { $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id']; } $create_variables = $query->get( $this->rewrite_ids['create_single_item_variables'] ); if ( $create_variables ) { $context = 'bp_group_create_'; $action_variables = array(); if ( ! is_array( $create_variables ) ) { $action_variables = explode( '/', ltrim( $create_variables, '/' ) ); } else { $action_variables = $create_variables; } // The slug of the step is the second action variable. if ( isset( $action_variables[1] ) && $action_variables[1] ) { // Get the rewrite ID corresponfing to the custom slug. $second_action_variable_rewrite_id = bp_rewrites_get_custom_slug_rewrite_id( 'groups', $action_variables[1], $context ); // Reset the action variable with BP Default create step slug. if ( $second_action_variable_rewrite_id ) { $second_action_variable = str_replace( $context, '', $second_action_variable_rewrite_id ); // Make sure the action is stored as a slug: underscores need to be replaced by dashes. $action_variables[1] = str_replace( '_', '-', $second_action_variable ); } } $bp->action_variables = $action_variables; } } /** * Set the BuddyPress queried object. */ if ( isset( $bp->pages->groups->id ) ) { $query->queried_object = get_post( $bp->pages->groups->id ); $query->queried_object_id = $query->queried_object->ID; if ( $this->current_group ) { $query->queried_object->single_item_name = $this->current_group->name; } elseif ( $group_type ) { $query->queried_object->directory_type_name = $group_type; } } } parent::parse_query( $query ); } /** * Check the parsed query is consistent with Group’s registered screens. * * @since 12.0.0 */ public function check_parsed_query() { if ( bp_is_group() ) { $slug = bp_current_action(); $context = 'read'; if ( 'admin' === $slug ) { $slug = bp_action_variable( 0 ); $context = 'manage'; } $registered_group_screens = bp_get_group_screens( $context ); if ( ! isset( $registered_group_screens[ $slug ] ) ) { bp_do_404(); return; } } } /** * Init the BP REST API. * * @since 5.0.0 * @since 6.0.0 Adds the Group Cover REST endpoint. * * @param array $controllers Optional. See BP_Component::rest_api_init() for * description. */ public function rest_api_init( $controllers = array() ) { $controllers = array( 'BP_REST_Groups_Endpoint', 'BP_REST_Group_Membership_Endpoint', 'BP_REST_Group_Invites_Endpoint', 'BP_REST_Group_Membership_Request_Endpoint', 'BP_REST_Attachments_Group_Avatar_Endpoint', ); // Support to Group Cover. if ( bp_is_active( 'groups', 'cover_image' ) ) { $controllers[] = 'BP_REST_Attachments_Group_Cover_Endpoint'; } parent::rest_api_init( $controllers ); } /** * Register the BP Groups Blocks. * * @since 6.0.0 * @since 12.0.0 Use the WP Blocks API v2. * * @param array $blocks Optional. See BP_Component::blocks_init() for * description. */ public function blocks_init( $blocks = array() ) { parent::blocks_init( array( 'bp/group' => array( 'metadata' => trailingslashit( buddypress()->plugin_dir ) . 'bp-groups/blocks/group', 'render_callback' => 'bp_groups_render_group_block', ), 'bp/groups' => array( 'metadata' => trailingslashit( buddypress()->plugin_dir ) . 'bp-groups/blocks/groups', 'render_callback' => 'bp_groups_render_groups_block', ), 'bp/dynamic-groups' => array( 'metadata' => trailingslashit( buddypress()->plugin_dir ) . 'bp-groups/blocks/dynamic-groups', 'render_callback' => 'bp_groups_render_dynamic_groups_block', ), ) ); } }