[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-component.php (source)

   1  <?php
   2  /**
   3   * Component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  if ( !class_exists( 'BP_Component' ) ) :
  14  
  15  /**
  16   * BuddyPress Component Class.
  17   *
  18   * The BuddyPress component class is responsible for simplifying the creation
  19   * of components that share similar behaviors and routines. It is used
  20   * internally by BuddyPress to create the bundled components, but can be
  21   * extended to create other really neat things.
  22   *
  23   * @since 1.5.0
  24   */
  25  class BP_Component {
  26  
  27      /** Variables *************************************************************/
  28  
  29      /**
  30       * Translatable name for the component.
  31       *
  32       * @internal
  33       * @var string $name
  34       */
  35      public $name = '';
  36  
  37      /**
  38       * Unique ID for the component.
  39       *
  40       * @since 1.5.0
  41       * @var string $id
  42       */
  43      public $id = '';
  44  
  45      /**
  46       * Unique slug for the component, for use in query strings and URLs.
  47       *
  48       * @since 1.5.0
  49       * @var string $slug
  50       */
  51      public $slug = '';
  52  
  53      /**
  54       * Does the component need a top-level directory?
  55       *
  56       * @since 1.5.0
  57       * @var bool $has_directory
  58       */
  59      public $has_directory = false;
  60  
  61      /**
  62       * The path to the component's files.
  63       *
  64       * @since 1.5.0
  65       * @var string $path
  66       */
  67      public $path = '';
  68  
  69      /**
  70       * The WP_Query loop for this component.
  71       *
  72       * @since 1.5.0
  73       * @var WP_Query $query
  74       */
  75      public $query = false;
  76  
  77      /**
  78       * The current ID of the queried object.
  79       *
  80       * @since 1.5.0
  81       * @var string $current_id
  82       */
  83      public $current_id = '';
  84  
  85      /**
  86       * Callback for formatting notifications.
  87       *
  88       * @since 1.5.0
  89       * @var callable $notification_callback
  90       */
  91      public $notification_callback = '';
  92  
  93      /**
  94       * WordPress Toolbar links.
  95       *
  96       * @since 1.5.0
  97       * @var array $admin_menu
  98       */
  99      public $admin_menu = '';
 100  
 101      /**
 102       * Placeholder text for component directory search box.
 103       *
 104       * @since 1.6.0
 105       * @var string $search_string
 106       */
 107      public $search_string = '';
 108  
 109      /**
 110       * Root slug for the component.
 111       *
 112       * @since 1.6.0
 113       * @var string $root_slug
 114       */
 115      public $root_slug = '';
 116  
 117      /**
 118       * Metadata tables for the component (if applicable).
 119       *
 120       * @since 2.0.0
 121       *
 122       * @var array
 123       */
 124      public $meta_tables = array();
 125  
 126      /**
 127       * Global tables for the component (if applicable).
 128       *
 129       * @since 2.0.0
 130       *
 131       * @var array
 132       */
 133      public $global_tables = array();
 134  
 135      /**
 136       * Query argument for component search URLs.
 137       *
 138       * @since 2.4.0
 139       * @var string
 140       */
 141      public $search_query_arg = 's';
 142  
 143      /**
 144       * An array of globalized data for BP Blocks.
 145       *
 146       * @since 9.0.0
 147       *
 148       * @var array
 149       */
 150      public $block_globals = array();
 151  
 152      /** Methods ***************************************************************/
 153  
 154      /**
 155       * Component loader.
 156       *
 157       * @since 1.5.0
 158       * @since 1.9.0 Added $params as a parameter.
 159       * @since 2.3.0 Added $params['features'] as a configurable value.
 160       * @since 2.4.0 Added $params['search_query_arg'] as a configurable value.
 161       *
 162       * @param string $id   Unique ID. Letters, numbers, and underscores only.
 163       * @param string $name Unique name. This should be a translatable name, eg.
 164       *                     __( 'Groups', 'buddypress' ).
 165       * @param string $path The file path for the component's files. Used by {@link BP_Component::includes()}.
 166       * @param array  $params {
 167       *     Additional parameters used by the component.
 168       *     @type int    $adminbar_myaccount_order Set the position for our menu under the WP Toolbar's "My Account menu".
 169       *     @type array  $features                 An array of feature names. This is used to load additional files from your
 170       *                                            component directory and for feature active checks. eg. array( 'awesome' )
 171       *                                            would look for a file called "bp-{$this->id}-awesome.php" and you could use
 172       *                                            bp_is_active( $this->id, 'awesome' ) to determine if the feature is active.
 173       *     @type string $search_query_arg         String to be used as the query argument in component search URLs.
 174       * }
 175       */
 176  	public function start( $id = '', $name = '', $path = '', $params = array() ) {
 177  
 178          // Internal identifier of component.
 179          $this->id   = $id;
 180  
 181          // Internal component name.
 182          $this->name = $name;
 183  
 184          // Path for includes.
 185          $this->path = $path;
 186  
 187          // Miscellaneous component parameters that need to be set early on.
 188          if ( ! empty( $params ) ) {
 189              // Sets the position for our menu under the WP Toolbar's "My Account" menu.
 190              if ( ! empty( $params['adminbar_myaccount_order'] ) ) {
 191                  $this->adminbar_myaccount_order = (int) $params['adminbar_myaccount_order'];
 192              }
 193  
 194              // Register features.
 195              if ( ! empty( $params['features'] ) ) {
 196                  $this->features = array_map( 'sanitize_title', (array) $params['features'] );
 197              }
 198  
 199              if ( ! empty( $params['search_query_arg'] ) ) {
 200                  $this->search_query_arg = sanitize_title( $params['search_query_arg'] );
 201              }
 202  
 203          // Set defaults if not passed.
 204          } else {
 205              // New component menus are added before the settings menu if not set.
 206              $this->adminbar_myaccount_order = 90;
 207          }
 208  
 209          // Move on to the next step.
 210          $this->setup_actions();
 211      }
 212  
 213      /**
 214       * Set up component global variables.
 215       *
 216       * @since 1.5.0
 217       * @since 9.0.0 Adds the `$block_globals` argument to the `$args` parameter.
 218       *
 219       * @param array $args {
 220       *     All values are optional.
 221       *     @type string   $slug                  The component slug. Used to construct certain URLs, such as 'friends' in
 222       *                                           http://example.com/members/joe/friends/. Default: the value of $this->id.
 223       *     @type string   $root_slug             The component root slug. Note that this value is generally unused if the
 224       *                                           component has a root directory (the slug will be overridden by the
 225       *                                           post_name of the directory page). Default: the slug of the directory page
 226       *                                           if one is found, otherwise an empty string.
 227       *     @type bool     $has_directory         Set to true if the component requires an associated WordPress page.
 228       *     @type callable $notification_callback Optional. The callable function that formats the component's notifications.
 229       *     @type string   $search_term           Optional. The placeholder text in the component directory search box. Eg,
 230       *                                           'Search Groups...'.
 231       *     @type array    $global_tables         Optional. An array of database table names.
 232       *     @type array    $meta_tables           Optional. An array of metadata table names.
 233       *     @type array    $block_globals         Optional. An array of globalized data for BP Blocks.
 234       * }
 235       */
 236  	public function setup_globals( $args = array() ) {
 237          $r = bp_parse_args(
 238              $args,
 239              array(
 240                  'slug'                  => $this->id,
 241                  'root_slug'             => '',
 242                  'has_directory'         => false,
 243                  'directory_title'       => '',
 244                  'notification_callback' => '',
 245                  'search_string'         => '',
 246                  'global_tables'         => '',
 247                  'meta_tables'           => '',
 248                  'block_globals'         => array(),
 249              )
 250          );
 251  
 252          /** Slugs ************************************************************
 253           */
 254  
 255          // For all Components except Core.
 256          if ( 'core' !== $this->id ) {
 257              /**
 258               * If a WP directory page exists for the component, it should
 259               * be the default value of 'root_slug'.
 260               */
 261              if ( isset( buddypress()->pages->{$this->id}->slug ) ) {
 262                  $r['root_slug'] = buddypress()->pages->{$this->id}->slug;
 263              }
 264  
 265              /**
 266               * Filters the slug to be used for the permalink URI chunk after root.
 267               *
 268               * @since 1.5.0
 269               *
 270               * @param string $value Slug to use in permalink URI chunk.
 271               */
 272              $this->slug = apply_filters( 'bp_' . $this->id . '_slug', $r['slug'] );
 273  
 274              /**
 275               * Filters the slug used for root directory.
 276               *
 277               * @since 1.5.0
 278               *
 279               * @param string $value Root directory slug.
 280               */
 281              $this->root_slug = apply_filters( 'bp_' . $this->id . '_root_slug', $r['root_slug'] );
 282  
 283              /**
 284               * Filters the component's top-level directory if available.
 285               *
 286               * @since 1.5.0
 287               *
 288               * @param bool $value Whether or not there is a top-level directory.
 289               */
 290              $this->has_directory = apply_filters( 'bp_' . $this->id . '_has_directory', $r['has_directory'] );
 291  
 292              /**
 293               * Filters the component's directory title.
 294               *
 295               * @since 2.0.0
 296               *
 297               * @param string $value Title to use for the directory.
 298               */
 299              $this->directory_title = apply_filters( 'bp_' . $this->id . '_directory_title', $r['directory_title'] );
 300  
 301              /**
 302               * Filters the placeholder text for search inputs for component.
 303               *
 304               * @since 1.5.0
 305               *
 306               * @param string $value Name to use in search input placeholders.
 307               */
 308              $this->search_string = apply_filters( 'bp_' . $this->id . '_search_string', $r['search_string'] );
 309  
 310              /**
 311               * Filters the callable function that formats the component's notifications.
 312               *
 313               * @since 1.5.0
 314               *
 315               * @param string $value Function callback.
 316               */
 317              $this->notification_callback = apply_filters( 'bp_' . $this->id . '_notification_callback', $r['notification_callback'] );
 318  
 319              // Set the global table names, if applicable.
 320              if ( ! empty( $r['global_tables'] ) ) {
 321                  $this->register_global_tables( $r['global_tables'] );
 322              }
 323  
 324              // Set the metadata table, if applicable.
 325              if ( ! empty( $r['meta_tables'] ) ) {
 326                  $this->register_meta_tables( $r['meta_tables'] );
 327              }
 328  
 329              // Register this component in the loaded components array.
 330              buddypress()->loaded_components[ $this->slug ] = $this->id;
 331          }
 332  
 333          /**
 334           * Filters the $blocks global value.
 335           *
 336           * @since 9.0.0
 337           *
 338           * @param array $blocks a list of global properties for blocks keyed
 339           *                      by their corresponding block name.
 340           */
 341          $block_globals = apply_filters( 'bp_' . $this->id . '_block_globals', $r['block_globals'] );
 342          if ( is_array( $block_globals ) && array_filter( $block_globals ) ) {
 343              foreach ( $block_globals as $block_name => $block_props ) {
 344                  $this->block_globals[ $block_name ] = new stdClass();
 345  
 346                  // Initialize an `items` property for Widget Block occurrences.
 347                  $this->block_globals[ $block_name ]->items = array();
 348  
 349                  // Set the global properties for the Block.
 350                  $this->block_globals[ $block_name ]->props = (array) $block_props;
 351              }
 352          }
 353  
 354          /**
 355           * Fires at the end of the setup_globals method inside BP_Component.
 356           *
 357           * This is a dynamic hook that is based on the component string ID.
 358           *
 359           * @since 1.5.0
 360           */
 361          do_action( 'bp_' . $this->id . '_setup_globals' );
 362      }
 363  
 364      /**
 365       * Include required files.
 366       *
 367       * Please note that, by default, this method is fired on the bp_include
 368       * hook, with priority 8. This is necessary so that core components are
 369       * loaded in time to be available to third-party plugins. However, this
 370       * load order means that third-party plugins whose main files are
 371       * loaded at bp_include with priority 10 (as recommended), will not be
 372       * loaded in time for their includes() method to fire automatically.
 373       *
 374       * For this reason, it is recommended that your plugin has its own
 375       * method or function for requiring necessary files. If you must use
 376       * this method, you will have to call it manually in your constructor
 377       * class, ie
 378       *   $this->includes();
 379       *
 380       * Note that when you pass an array value like 'actions' to includes,
 381       * it looks for the following three files (assuming your component is
 382       * called 'my_component'):
 383       *   - ./actions
 384       *   - ./bp-my_component/actions
 385       *   - ./bp-my_component/bp-my_component-actions.php
 386       *
 387       * @since 1.5.0
 388       *
 389       *
 390       * @param array $includes An array of file names, or file name chunks,
 391       *                        to be parsed and then included.
 392       */
 393  	public function includes( $includes = array() ) {
 394  
 395          // Bail if no files to include.
 396          if ( ! empty( $includes ) ) {
 397              $slashed_path = trailingslashit( $this->path );
 398  
 399              // Loop through files to be included.
 400              foreach ( (array) $includes as $file ) {
 401  
 402                  $paths = array(
 403  
 404                      // Passed with no extension.
 405                      'bp-' . $this->id . '/bp-' . $this->id . '-' . $file  . '.php',
 406                      'bp-' . $this->id . '-' . $file . '.php',
 407                      'bp-' . $this->id . '/' . $file . '.php',
 408  
 409                      // Passed with extension.
 410                      $file,
 411                      'bp-' . $this->id . '-' . $file,
 412                      'bp-' . $this->id . '/' . $file,
 413                  );
 414  
 415                  foreach ( $paths as $path ) {
 416                      if ( @is_file( $slashed_path . $path ) ) {
 417                          require( $slashed_path . $path );
 418                          break;
 419                      }
 420                  }
 421              }
 422          }
 423  
 424          /**
 425           * Fires at the end of the includes method inside BP_Component.
 426           *
 427           * This is a dynamic hook that is based on the component string ID.
 428           *
 429           * @since 1.5.0
 430           */
 431          do_action( 'bp_' . $this->id . '_includes' );
 432      }
 433  
 434      /**
 435       * Late includes method.
 436       *
 437       * Components should include files here only on specific pages using
 438       * conditionals such as {@link bp_is_current_component()}. Intentionally left
 439       * empty.
 440       *
 441       * @since 3.0.0
 442       */
 443  	public function late_includes() {}
 444  
 445      /**
 446       * Set up the actions.
 447       *
 448       * @since 1.5.0
 449       *
 450       */
 451  	public function setup_actions() {
 452  
 453          // Setup globals.
 454          add_action( 'bp_setup_globals',          array( $this, 'setup_globals'          ), 10 );
 455  
 456          // Set up canonical stack.
 457          add_action( 'bp_setup_canonical_stack',  array( $this, 'setup_canonical_stack'  ), 10 );
 458  
 459          // Include required files. Called early to ensure that BP core
 460          // components are loaded before plugins that hook their loader functions
 461          // to bp_include with the default priority of 10. This is for backwards
 462          // compatibility; henceforth, plugins should register themselves by
 463          // extending this base class.
 464          add_action( 'bp_include',                array( $this, 'includes'               ), 8 );
 465  
 466          // Load files conditionally, based on certain pages.
 467          add_action( 'bp_late_include',           array( $this, 'late_includes'          ) );
 468  
 469          // Setup navigation.
 470          add_action( 'bp_setup_nav',              array( $this, 'setup_nav'              ), 10 );
 471  
 472          // Setup WP Toolbar menus.
 473          add_action( 'bp_setup_admin_bar',        array( $this, 'setup_admin_bar'        ), $this->adminbar_myaccount_order );
 474  
 475          // Setup component title.
 476          add_action( 'bp_setup_title',            array( $this, 'setup_title'            ), 10 );
 477  
 478          // Setup cache groups.
 479          add_action( 'bp_setup_cache_groups',     array( $this, 'setup_cache_groups'     ), 10 );
 480  
 481          // Register post types.
 482          add_action( 'bp_register_post_types',    array( $this, 'register_post_types'    ), 10 );
 483  
 484          // Register taxonomies.
 485          add_action( 'bp_register_taxonomies',    array( $this, 'register_taxonomies'    ), 10 );
 486  
 487          // Add the rewrite tags.
 488          add_action( 'bp_add_rewrite_tags',       array( $this, 'add_rewrite_tags'       ), 10 );
 489  
 490          // Add the rewrite rules.
 491          add_action( 'bp_add_rewrite_rules',      array( $this, 'add_rewrite_rules'      ), 10 );
 492  
 493          // Add the permalink structure.
 494          add_action( 'bp_add_permastructs',       array( $this, 'add_permastructs'       ), 10 );
 495  
 496          // Allow components to parse the main query.
 497          add_action( 'bp_parse_query',            array( $this, 'parse_query'            ), 10 );
 498  
 499          // Generate rewrite rules.
 500          add_action( 'bp_generate_rewrite_rules', array( $this, 'generate_rewrite_rules' ), 10 );
 501  
 502          // Register BP REST Endpoints.
 503          if ( bp_rest_in_buddypress() && bp_rest_api_is_available() ) {
 504              add_action( 'bp_rest_api_init', array( $this, 'rest_api_init' ), 10 );
 505          }
 506  
 507          // Register BP Blocks.
 508          if ( bp_support_blocks() ) {
 509              add_action( 'bp_blocks_init', array( $this, 'blocks_init' ), 10 );
 510          }
 511  
 512          /**
 513           * Fires at the end of the setup_actions method inside BP_Component.
 514           *
 515           * This is a dynamic hook that is based on the component string ID.
 516           *
 517           * @since 1.5.0
 518           */
 519          do_action( 'bp_' . $this->id . '_setup_actions' );
 520      }
 521  
 522      /**
 523       * Set up the canonical URL stack for this component.
 524       *
 525       * @since 2.1.0
 526       */
 527  	public function setup_canonical_stack() {}
 528  
 529      /**
 530       * Set up component navigation.
 531       *
 532       * @since 1.5.0
 533       *
 534       * @see bp_core_new_nav_item() For a description of the $main_nav
 535       *      parameter formatting.
 536       * @see bp_core_new_subnav_item() For a description of how each item
 537       *      in the $sub_nav parameter array should be formatted.
 538       *
 539       * @param array $main_nav Optional. Passed directly to bp_core_new_nav_item().
 540       *                        See that function for a description.
 541       * @param array $sub_nav  Optional. Multidimensional array, each item in
 542       *                        which is passed to bp_core_new_subnav_item(). See that
 543       *                        function for a description.
 544       */
 545  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 546  
 547          // No sub nav items without a main nav item.
 548          if ( !empty( $main_nav ) ) {
 549              // Always set the component ID.
 550              $main_nav['component_id'] = $this->id;
 551  
 552              bp_core_new_nav_item( $main_nav, 'members' );
 553  
 554              // Sub nav items are not required.
 555              if ( !empty( $sub_nav ) ) {
 556                  foreach( (array) $sub_nav as $nav ) {
 557                      bp_core_new_subnav_item( $nav, 'members' );
 558                  }
 559              }
 560          }
 561  
 562          /**
 563           * Fires at the end of the setup_nav method inside BP_Component.
 564           *
 565           * This is a dynamic hook that is based on the component string ID.
 566           *
 567           * @since 1.5.0
 568           */
 569          do_action( 'bp_' . $this->id . '_setup_nav' );
 570      }
 571  
 572      /**
 573       * Set up the component entries in the WordPress Admin Bar.
 574       *
 575       * @since 1.5.0
 576       *
 577       * @see WP_Admin_Bar::add_menu() for a description of the syntax
 578       *      required by each item in the $wp_admin_nav parameter array.
 579       * @global object $wp_admin_bar
 580       *
 581       * @param array $wp_admin_nav An array of nav item arguments. Each item in this parameter
 582       *                            array is passed to {@link WP_Admin_Bar::add_menu()}.
 583       *                            See that method for a description of the required syntax for
 584       *                            each item.
 585       */
 586  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 587  
 588          // Bail if this is an ajax request.
 589          if ( defined( 'DOING_AJAX' ) ) {
 590              return;
 591          }
 592  
 593          // Do not proceed if BP_USE_WP_ADMIN_BAR constant is not set or is false.
 594          if ( ! bp_use_wp_admin_bar() ) {
 595              return;
 596          }
 597  
 598          /**
 599           * Filters the admin navigation passed into setup_admin_bar.
 600           *
 601           * This is a dynamic hook that is based on the component string ID.
 602           *
 603           * @since 1.9.0
 604           *
 605           * @param array $wp_admin_nav Array of navigation items to add.
 606           */
 607          $wp_admin_nav = apply_filters( 'bp_' . $this->id . '_admin_nav', $wp_admin_nav );
 608  
 609          // Do we have Toolbar menus to add?
 610          if ( !empty( $wp_admin_nav ) ) {
 611              // Fill in position if one wasn't passed for backpat.
 612              $pos = 0;
 613              $not_set_pos = 1;
 614              foreach( $wp_admin_nav as $key => $nav ) {
 615                  if ( ! isset( $nav['position'] ) ) {
 616                      $wp_admin_nav[$key]['position'] = $pos + $not_set_pos;
 617  
 618                      if ( 9 !== $not_set_pos ) {
 619                          ++$not_set_pos;
 620                      }
 621                  } else {
 622                      $pos = $nav['position'];
 623  
 624                      // Reset not set pos to 1.
 625                      if ( $pos % 10 === 0 ) {
 626                          $not_set_pos = 1;
 627                      }
 628                  }
 629              }
 630  
 631              // Sort admin nav by position.
 632              $wp_admin_nav = bp_sort_by_key( $wp_admin_nav, 'position', 'num' );
 633  
 634              // Set this objects menus.
 635              $this->admin_menu = $wp_admin_nav;
 636  
 637              // Define the WordPress global.
 638              global $wp_admin_bar;
 639  
 640              // Add each admin menu.
 641              foreach( $this->admin_menu as $admin_menu ) {
 642                  $wp_admin_bar->add_node( $admin_menu );
 643              }
 644          }
 645  
 646          /**
 647           * Fires at the end of the setup_admin_bar method inside BP_Component.
 648           *
 649           * This is a dynamic hook that is based on the component string ID.
 650           *
 651           * @since 1.5.0
 652           */
 653          do_action( 'bp_' . $this->id . '_setup_admin_bar' );
 654      }
 655  
 656      /**
 657       * Set up the component title.
 658       *
 659       * @since 1.5.0
 660       *
 661       */
 662  	public function setup_title() {
 663  
 664          /**
 665           * Fires in the setup_title method inside BP_Component.
 666           *
 667           * This is a dynamic hook that is based on the component string ID.
 668           *
 669           * @since 1.5.0
 670           */
 671          do_action(  'bp_' . $this->id . '_setup_title' );
 672      }
 673  
 674      /**
 675       * Setup component-specific cache groups.
 676       *
 677       * @since 2.2.0
 678       *
 679       */
 680  	public function setup_cache_groups() {
 681  
 682          /**
 683           * Fires in the setup_cache_groups method inside BP_Component.
 684           *
 685           * This is a dynamic hook that is based on the component string ID.
 686           *
 687           * @since 2.2.0
 688           */
 689          do_action( 'bp_' . $this->id . '_setup_cache_groups' );
 690      }
 691  
 692      /**
 693       * Register global tables for the component, so that it may use WordPress's database API.
 694       *
 695       * @since 2.0.0
 696       *
 697       * @param array $tables Table names to register.
 698       */
 699  	public function register_global_tables( $tables = array() ) {
 700  
 701          /**
 702           * Filters the global tables for the component, so that it may use WordPress' database API.
 703           *
 704           * This is a dynamic hook that is based on the component string ID.
 705           * It allows for component-specific filtering of table names. To filter
 706           * *all* tables, use the 'bp_core_get_table_prefix' filter instead.
 707           *
 708           * @since 1.6.0
 709           */
 710          $tables = apply_filters( 'bp_' . $this->id . '_global_tables', $tables );
 711  
 712          // Add to the BuddyPress global object.
 713          if ( !empty( $tables ) && is_array( $tables ) ) {
 714              foreach ( $tables as $global_name => $table_name ) {
 715                  $this->{$global_name} = $table_name;
 716              }
 717  
 718              // Keep a record of the metadata tables in the component.
 719              $this->global_tables = $tables;
 720          }
 721  
 722          /**
 723           * Fires at the end of the register_global_tables method inside BP_Component.
 724           *
 725           * This is a dynamic hook that is based on the component string ID.
 726           *
 727           * @since 2.0.0
 728           */
 729          do_action( 'bp_' . $this->id . '_register_global_tables' );
 730      }
 731  
 732      /**
 733       * Register component metadata tables.
 734       *
 735       * Metadata tables are registered in the $wpdb global, for
 736       * compatibility with the WordPress metadata API.
 737       *
 738       * @since 2.0.0
 739       *
 740       * @param array $tables Table names to register.
 741       */
 742  	public function register_meta_tables( $tables = array() ) {
 743          global $wpdb;
 744  
 745          /**
 746           * Filters the global meta_tables for the component.
 747           *
 748           * This is a dynamic hook that is based on the component string ID.
 749           * It allows for component-specific filtering of table names. To filter
 750           * *all* tables, use the 'bp_core_get_table_prefix' filter instead.
 751           *
 752           * @since 2.0.0
 753           */
 754          $tables = apply_filters( 'bp_' . $this->id . '_meta_tables', $tables );
 755  
 756          /**
 757           * Add the name of each metadata table to WPDB to allow BuddyPress
 758           * components to play nicely with the WordPress metadata API.
 759           */
 760          if ( !empty( $tables ) && is_array( $tables ) ) {
 761              foreach( $tables as $meta_prefix => $table_name ) {
 762                  $wpdb->{$meta_prefix . 'meta'} = $table_name;
 763              }
 764  
 765              // Keep a record of the metadata tables in the component.
 766              $this->meta_tables = $tables;
 767          }
 768  
 769          /**
 770           * Fires at the end of the register_meta_tables method inside BP_Component.
 771           *
 772           * This is a dynamic hook that is based on the component string ID.
 773           *
 774           * @since 2.0.0
 775           */
 776          do_action( 'bp_' . $this->id . '_register_meta_tables' );
 777      }
 778  
 779      /**
 780       * Set up the component post types.
 781       *
 782       * @since 1.5.0
 783       *
 784       */
 785  	public function register_post_types() {
 786  
 787          /**
 788           * Fires in the register_post_types method inside BP_Component.
 789           *
 790           * This is a dynamic hook that is based on the component string ID.
 791           *
 792           * @since 1.5.0
 793           */
 794          do_action( 'bp_' . $this->id . '_register_post_types' );
 795      }
 796  
 797      /**
 798       * Register component-specific taxonomies.
 799       *
 800       * @since 1.5.0
 801       *
 802       */
 803  	public function register_taxonomies() {
 804  
 805          /**
 806           * Fires in the register_taxonomies method inside BP_Component.
 807           *
 808           * This is a dynamic hook that is based on the component string ID.
 809           *
 810           * @since 1.5.0
 811           */
 812          do_action( 'bp_' . $this->id . '_register_taxonomies' );
 813      }
 814  
 815      /**
 816       * Add any additional rewrite tags.
 817       *
 818       * @since 1.5.0
 819       *
 820       */
 821  	public function add_rewrite_tags() {
 822  
 823          /**
 824           * Fires in the add_rewrite_tags method inside BP_Component.
 825           *
 826           * This is a dynamic hook that is based on the component string ID.
 827           *
 828           * @since 1.5.0
 829           */
 830          do_action( 'bp_' . $this->id . '_add_rewrite_tags' );
 831      }
 832  
 833      /**
 834       * Add any additional rewrite rules.
 835       *
 836       * @since 1.9.0
 837       *
 838       */
 839  	public function add_rewrite_rules() {
 840  
 841          /**
 842           * Fires in the add_rewrite_rules method inside BP_Component.
 843           *
 844           * This is a dynamic hook that is based on the component string ID.
 845           *
 846           * @since 1.9.0
 847           */
 848          do_action( 'bp_' . $this->id . '_add_rewrite_rules' );
 849      }
 850  
 851      /**
 852       * Add any permalink structures.
 853       *
 854       * @since 1.9.0
 855       *
 856       */
 857  	public function add_permastructs() {
 858  
 859          /**
 860           * Fires in the add_permastructs method inside BP_Component.
 861           *
 862           * This is a dynamic hook that is based on the component string ID.
 863           *
 864           * @since 1.9.0
 865           */
 866          do_action( 'bp_' . $this->id . '_add_permastructs' );
 867      }
 868  
 869      /**
 870       * Allow components to parse the main query.
 871       *
 872       * @since 1.9.0
 873       *
 874       *
 875       * @param object $query The main WP_Query.
 876       */
 877  	public function parse_query( $query ) {
 878  
 879          /**
 880           * Fires in the parse_query method inside BP_Component.
 881           *
 882           * This is a dynamic hook that is based on the component string ID.
 883           *
 884           * @since 1.9.0
 885           *
 886           * @param object $query Main WP_Query object. Passed by reference.
 887           */
 888          do_action_ref_array( 'bp_' . $this->id . '_parse_query', array( &$query ) );
 889      }
 890  
 891      /**
 892       * Generate any additional rewrite rules.
 893       *
 894       * @since 1.5.0
 895       *
 896       */
 897  	public function generate_rewrite_rules() {
 898  
 899          /**
 900           * Fires in the generate_rewrite_rules method inside BP_Component.
 901           *
 902           * This is a dynamic hook that is based on the component string ID.
 903           *
 904           * @since 1.5.0
 905           */
 906          do_action( 'bp_' . $this->id . '_generate_rewrite_rules' );
 907      }
 908  
 909      /**
 910       * Init the BP REST API.
 911       *
 912       * @since 5.0.0
 913       *
 914       * @param array $controllers The list of BP REST controllers to load.
 915       */
 916  	public function rest_api_init( $controllers = array() ) {
 917          if ( is_array( $controllers ) && $controllers ) {
 918              // Built-in controllers.
 919              $_controllers = $controllers;
 920  
 921              /**
 922               * Use this filter to disable all or some REST API controllers
 923               * for the component.
 924               *
 925               * This is a dynamic hook that is based on the component string ID.
 926               *
 927               * @since 5.0.0
 928               *
 929               * @param array $controllers The list of BP REST API controllers to load.
 930               */
 931              $controllers = (array) apply_filters( 'bp_' . $this->id . '_rest_api_controllers', $controllers );
 932  
 933              foreach( $controllers as $controller ) {
 934                  if ( ! in_array( $controller, $_controllers, true ) ) {
 935                      continue;
 936                  }
 937  
 938                  $component_controller = new $controller;
 939                  $component_controller->register_routes();
 940              }
 941          }
 942  
 943          /**
 944           * Fires in the rest_api_init method inside BP_Component.
 945           *
 946           * This is a dynamic hook that is based on the component string ID.
 947           *
 948           * @since 5.0.0
 949           */
 950          do_action( 'bp_' . $this->id . '_rest_api_init' );
 951      }
 952  
 953      /**
 954       * Register the BP Blocks.
 955       *
 956       * @since 6.0.0
 957       *
 958       * @see `BP_Block->construct()` for a full description of a BP Block arguments.
 959       *
 960       * @param array $blocks The list of BP Blocks to register.
 961       */
 962  	public function blocks_init( $blocks = array() ) {
 963          /**
 964           * Filter here to add new BP Blocks, disable some or all BP Blocks for a component.
 965           *
 966           * This is a dynamic hook that is based on the component string ID.
 967           *
 968           * @since 6.0.0
 969           *
 970           * @param array $blocks The list of BP Blocks for the component.
 971           */
 972          $blocks = (array) apply_filters( 'bp_' . $this->id . '_register_blocks', $blocks );
 973          $blocks = array_filter( $blocks );
 974  
 975          if ( $blocks ) {
 976              foreach ( $blocks as $block ) {
 977                  bp_register_block( $block );
 978              }
 979          }
 980  
 981          /**
 982           * Fires in the blocks_init method inside BP_Component.
 983           *
 984           * This is a dynamic hook that is based on the component string ID.
 985           *
 986           * @since 6.0.0
 987           */
 988          do_action( 'bp_' . $this->id . '_blocks_init' );
 989      }
 990  }
 991  endif; // BP_Component.


Generated: Fri Sep 17 01:01:38 2021 Cross-referenced by PHPXref 0.7.1