tadata table to WPDB to allow BuddyPress * components to play nicely with the WordPress metadata API. */ if ( !empty( $tables ) && is_array( $tables ) ) { foreach( $tables as $meta_prefix => $table_name ) { $wpdb->{$meta_prefix . 'meta'} = $table_name; } // Keep a record of the metadata tables in the component. $this->meta_tables = $tables; } /** * Fires at the end of the register_meta_tables method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 2.0.0 */ do_action( 'bp_' . $this->id . '_register_meta_tables' ); } /** * Set up the component post types. * * @since 1.5.0 * */ public function register_post_types() { /** * Fires in the register_post_types method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.5.0 */ do_action( 'bp_' . $this->id . '_register_post_types' ); } /** * Set up the component post statuses. * * @since 12.0.0 */ public function register_post_statuses() { /** * Fires in the `register_post_statuses` method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 12.0.0 */ do_action( 'bp_' . $this->id . '_register_post_statuses' ); } /** * Register component-specific taxonomies. * * @since 1.5.0 * */ public function register_taxonomies() { /** * Fires in the register_taxonomies method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.5.0 */ do_action( 'bp_' . $this->id . '_register_taxonomies' ); } /** * Add Component's additional rewrite tags. * * @since 1.5.0 * @since 12.0.0 Adds the `$rewrite_tags` parameter. * * @param array $rewrite_tags Array of arguments list used to add WordPress rewrite tags. * Each argument key needs to match one of `$this->rewrite_ids` keys. */ public function add_rewrite_tags( $rewrite_tags = array() ) { if ( array_filter( $this->rewrite_ids ) ) { $chunks = bp_rewrites_get_default_url_chunks(); foreach ( $this->rewrite_ids as $rewrite_id_key => $rewrite_id_value ) { $rewrite_tag = '%' . $rewrite_id_value . '%'; $rewrite_regex = ''; if ( isset( $rewrite_tags[ $rewrite_id_key ] ) ) { $rewrite_regex = $rewrite_tags[ $rewrite_id_key ]; } elseif ( isset( $chunks[ $rewrite_id_key ]['regex'] ) ) { $rewrite_regex = $chunks[ $rewrite_id_key ]['regex']; } if ( ! $rewrite_regex ) { continue; } add_rewrite_tag( $rewrite_tag, $rewrite_regex ); } } /** * Fires in the add_rewrite_tags method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.5.0 */ do_action( 'bp_' . $this->id . '_add_rewrite_tags' ); } /** * Add Component's additional rewrite rules. * * @since 1.9.0 * @since 12.0.0 Adds the `$rewrite_rules` parameter. * * @param array $rewrite_rules { * Array of associative arrays of arguments list used to add WordPress rewrite rules. * Each associative array needs to include the following keys. * * @type string $regex Regular expression to match request against. Required. * @type string $query The corresponding query vars for this rewrite rule. Required. * @type int $order The insertion order for the rewrite rule. Required. * @type string $priority The Priority of the new rule. Accepts 'top' or 'bottom'. Optional. * Default 'top'. * } */ public function add_rewrite_rules( $rewrite_rules = array() ) { if ( array_filter( $this->rewrite_ids ) ) { $priority = 'top'; $chunks = array_merge( bp_rewrites_get_default_url_chunks(), $rewrite_rules ); $rules = bp_sort_by_key( $chunks, 'order', 'num', true ); $reversed_rules = array_reverse( $rules, true ); $regex = ''; $query = ''; $match = 1; // Build rewrite rules for the component. foreach ( $reversed_rules as $rule_key => $rule_information ) { if ( ! isset( $this->rewrite_ids[ $rule_key ] ) ) { unset( $rules[ $rule_key ] ); continue; } // The query is already set, use it. if ( isset( $rule_information['query'] ) ) { $rules[ $rule_key ]['regex'] = $rule_information['regex']; $rules[ $rule_key ]['query'] = $rule_information['query']; } elseif ( 'directory' === $rule_key ) { $regex = $this->root_slug; $query = 'index.php?' . $this->rewrite_ids['directory'] . '=1'; $rules[ $rule_key ]['regex'] = $regex . '/?$'; $rules[ $rule_key ]['query'] = $query; } else { $regex = trailingslashit( $regex ) . $rule_information['regex']; $query .= '&' . $this->rewrite_ids[ $rule_key ] . '=$matches['. $match .']'; $match += 1; $rules[ $rule_key ]['regex'] = $regex . '/?$'; $rules[ $rule_key ]['query'] = $query; } } // Then register the rewrite rules. if ( $rules ) { foreach ( $rules as $rewrite_rule ) { if ( ! isset( $rewrite_rule['regex'] ) || ! isset( $rewrite_rule['query'] ) ) { continue; } if ( ! isset( $rewrite_rule['priority'] ) || ! $rewrite_rule['priority'] ) { $rewrite_rule['priority'] = $priority; } add_rewrite_rule( $rewrite_rule['regex'], $rewrite_rule['query'], $rewrite_rule['priority'] ); } } } /** * Fires in the add_rewrite_rules method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.9.0 */ do_action( 'bp_' . $this->id . '_add_rewrite_rules' ); } /** * Add Component's permalink structures. * * @since 1.9.0 * @since 12.0.0 Adds the `$permastructs` parameter. * * @param array $permastructs { * Array of associative arrays of arguments list used to register WordPress additional permalink structures. * Each array enty is keyed with the permalink structure. * Each associative array needs to include the following keys. * * @type string $permastruct The permalink structure. Required. * @type array $args The permalink structure arguments. Optional. * } */ public function add_permastructs( $permastructs = array() ) { // Always include the directory permastruct when the component has a directory. if ( isset( $this->rewrite_ids['directory'] ) ) { $directory_permastruct = array( $this->rewrite_ids['directory'] => array( 'permastruct' => $this->directory_permastruct, 'args' => array(), ), ); $permastructs = array_merge( $directory_permastruct, (array) $permastructs ); } if ( $permastructs ) { foreach ( $permastructs as $name => $params ) { if ( ! $name || ! isset( $params['permastruct'] ) || ! $params['permastruct'] ) { continue; } if ( ! $params['args'] ) { $params['args'] = array(); } $args = wp_parse_args( $params['args'], array( 'with_front' => false, 'ep_mask' => EP_NONE, 'paged' => true, 'feed' => false, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => false, ) ); // Add the permastruct. add_permastruct( $name, $params['permastruct'], $args ); } } /** * Fires in the add_permastructs method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.9.0 */ do_action( 'bp_' . $this->id . '_add_permastructs' ); } /** * Allow components to parse the main query. * * @since 1.9.0 * * @param object $query The main WP_Query. */ public function parse_query( $query ) { if ( is_buddypress() && 'rewrites' === bp_core_get_query_parser() ) { add_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10, 2 ); } /** * Fires in the parse_query method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.9.0 * * @param object $query Main WP_Query object. Passed by reference. */ do_action_ref_array( 'bp_' . $this->id . '_parse_query', array( &$query ) ); } /** * Make sure to avoid querying for regular posts when displaying a BuddyPress page. * * @since 12.0.0 * * @param null $posts A null value to use the regular WP Query. * @param WP_Query $query The WP Query object. * @return null|array Null if not displaying a BuddyPress page. * An array containing the BuddyPress directory page otherwise. */ public function pre_query( $posts = null, $query = null ) { remove_filter( 'posts_pre_query', array( $this, 'pre_query' ), 10 ); $queried_object = $query->get_queried_object(); if ( $queried_object instanceof WP_Post && 'buddypress' === get_post_type( $queried_object ) ) { $component = bp_core_get_component_from_directory_page_id( $queried_object->ID ); if ( bp_current_user_can( 'bp_view', array( 'bp_component' => $component ) ) ) { // Only include the queried directory post into returned posts. $posts = array( $queried_object ); // Reset some query flags. $query->is_home = false; $query->is_front_page = false; $query->is_page = false; $query->is_archive = false; $query->is_tax = false; if ( ! is_embed() ) { $query->is_single = true; } } else { /** * Use this filter to send the user to the site login screen when the user does * not have the `bp_view` capability for the current screen or situation. * The default behavior is for the user to be shown the content in the * `assets/utils/restricted-access-message.php` file. * * Only users that are not logged in will be sent to the login screen, * else we can cause a redirect loop if the `bp_view` capability is not met * for a logged-in user. * * @since 12.0.0 * * @param false Whether the user should be redirected to the site login screen. */ $do_redirect_to_login_screen = apply_filters( 'bp_view_no_access_redirect_to_login_screen', false ); If ( true === $do_redirect_to_login_screen && ! is_user_logged_in() ) { bp_core_no_access(); } // The current user may not access the directory page. $bp = buddypress(); $bp->current_component = 'core'; // Unset other BuddyPress URI globals. foreach ( array( 'current_item', 'current_action', 'action_variables', 'displayed_user' ) as $global ) { if ( 'action_variables' === $global ) { $bp->{$global} = array(); } elseif ( 'displayed_user' === $global ) { $bp->{$global} = new \stdClass(); } else { $bp->{$global} = ''; } } // Reset the post. $post = (object) array( 'ID' => 0, 'post_type' => 'buddypress', 'post_name' => 'restricted', 'post_title' => __( 'Members-only area', 'buddypress' ), 'post_content' => bp_buffer_template_part( 'assets/utils/restricted-access-message', null, false ), 'comment_status' => 'closed', 'comment_count' => 0, ); // Reset the queried object. $query->queried_object = get_post( $post ); $query->queried_object_id = $query->queried_object->ID; // Reset the posts. $posts = array( $query->queried_object ); // Reset some WP Query properties. $query->found_posts = 1; $query->max_num_pages = 1; $query->posts = $posts; $query->post = $post; $query->post_count = 1; $query->is_home = false; $query->is_front_page = false; $query->is_page = true; $query->is_archive = false; $query->is_tax = false; // Make sure no comments are displayed for this page. add_filter( 'comments_pre_query', 'bp_comments_pre_query', 10, 2 ); } return $posts; } } /** * Generate any additional rewrite rules. * * @since 1.5.0 * */ public function generate_rewrite_rules() { /** * Fires in the generate_rewrite_rules method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 1.5.0 */ do_action( 'bp_' . $this->id . '_generate_rewrite_rules' ); } /** * Init the BP REST API. * * @since 5.0.0 * * @param array $controllers The list of BP REST controllers to load. */ public function rest_api_init( $controllers = array() ) { if ( is_array( $controllers ) && $controllers ) { // Built-in controllers. $_controllers = $controllers; /** * Use this filter to disable all or some REST API controllers * for the component. * * This is a dynamic hook that is based on the component string ID. * * @since 5.0.0 * * @param array $controllers The list of BP REST API controllers to load. */ $controllers = (array) apply_filters( 'bp_' . $this->id . '_rest_api_controllers', $controllers ); foreach( $controllers as $controller ) { if ( ! in_array( $controller, $_controllers, true ) ) { continue; } $component_controller = new $controller; $component_controller->register_routes(); } } /** * Fires in the rest_api_init method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 5.0.0 */ do_action( 'bp_' . $this->id . '_rest_api_init' ); } /** * Register the BP Blocks. * * @since 6.0.0 * * @see `BP_Block->construct()` for a full description of a BP Block arguments. * * @param array $blocks The list of BP Blocks to register. */ public function blocks_init( $blocks = array() ) { /** * Filter here to add new BP Blocks, disable some or all BP Blocks for a component. * * This is a dynamic hook that is based on the component string ID. * * @since 6.0.0 * * @param array $blocks The list of BP Blocks for the component. */ $blocks = (array) apply_filters( 'bp_' . $this->id . '_register_blocks', $blocks ); $blocks = array_filter( $blocks ); if ( $blocks ) { foreach ( $blocks as $block ) { bp_register_block( $block ); } } /** * Fires in the blocks_init method inside BP_Component. * * This is a dynamic hook that is based on the component string ID. * * @since 6.0.0 */ do_action( 'bp_' . $this->id . '_blocks_init' ); } /** * Add component's directory states. * * @since 10.0.0 * @deprecated 12.0.0 * * @param string[] $states An array of post display states. * @param WP_Post $post The current post object. * @return array The component's directory states. */ public function admin_directory_states( $states = array(), $post = null ) { _deprecated_function( __METHOD__, '12.0.0' ); return $states; } } endif; // BP_Component.