[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/ -> bbpress.php (source)

   1  <?php
   2  
   3  /**
   4   * The bbPress Plugin
   5   *
   6   * bbPress is forum software with a twist from the creators of WordPress.
   7   *
   8   * $Id: bbpress.php 7232 2022-02-17 18:49:15Z johnjamesjacoby $
   9   *
  10   * @package bbPress
  11   * @subpackage Main
  12   */
  13  
  14  /**
  15   * Plugin Name:       bbPress
  16   * Plugin URI:        https://bbpress.org
  17   * Description:       bbPress is forum software with a twist from the creators of WordPress.
  18   * Author:            The bbPress Contributors
  19   * Author URI:        https://bbpress.org
  20   * License:           GNU General Public License v2 or later
  21   * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
  22   * Text Domain:       bbpress
  23   * Domain Path:       /languages/
  24   * Requires PHP:      5.6.20
  25   * Requires at least: 5.0
  26   * Version:           2.7.0-alpha-2
  27   */
  28  
  29  // Exit if accessed directly
  30  defined( 'ABSPATH' ) || exit;
  31  
  32  if ( ! class_exists( 'bbPress' ) ) :
  33  /**
  34   * Main bbPress Class
  35   *
  36   * "Word hard. Stay bumble."
  37   *
  38   * @since 2.0.0 bbPress (r2464)
  39   */
  40  final class bbPress {
  41  
  42      /** Magic *****************************************************************/
  43  
  44      /**
  45       * bbPress uses many variables, several of which can be filtered to
  46       * customize the way it operates. Most of these variables are stored in a
  47       * private array that gets updated with the help of PHP magic methods.
  48       *
  49       * This is a precautionary measure, to avoid potential errors produced by
  50       * unanticipated direct manipulation of run-time data.
  51       *
  52       * @see bbPress::setup_globals()
  53       * @var array
  54       */
  55      private $data;
  56  
  57      /** Not Magic *************************************************************/
  58  
  59      /**
  60       * @var mixed False when not logged in; WP_User object when logged in
  61       */
  62      public $current_user = false;
  63  
  64      /**
  65       * @var stdClass Add-ons append to this (Akismet, BuddyPress, etc...)
  66       */
  67      public $extend;
  68  
  69      /**
  70       * @var array Topic views
  71       */
  72      public $views = array();
  73  
  74      /**
  75       * @var array Overloads get_option()
  76       */
  77      public $options = array();
  78  
  79      /**
  80       * @var array Storage of options not in the database
  81       */
  82      public $not_options = array();
  83  
  84      /**
  85       * @var array Overloads get_user_meta()
  86       */
  87      public $user_options = array();
  88  
  89      /**
  90       * @var array Dynamically initialized user roles
  91       */
  92      public $roles = array();
  93  
  94      /** Singleton *************************************************************/
  95  
  96      /**
  97       * Main bbPress Instance
  98       *
  99       * bbPress is fun
 100       * Please load it only one time
 101       * For this, we thank you
 102       *
 103       * Insures that only one instance of bbPress exists in memory at any one
 104       * time. Also prevents needing to define globals all over the place.
 105       *
 106       * @since 2.1.0 bbPress (r3757)
 107       *
 108       * @staticvar object $instance
 109       * @see bbpress()
 110       * @return bbPress The one true bbPress
 111       */
 112  	public static function instance() {
 113  
 114          // Store the instance locally to avoid private static replication
 115          static $instance = null;
 116  
 117          // Only run these methods if they haven't been ran previously
 118          if ( null === $instance ) {
 119              $instance = new bbPress();
 120              $instance->setup_environment();
 121              $instance->includes();
 122              $instance->setup_variables();
 123              $instance->setup_actions();
 124          }
 125  
 126          // Always return the instance
 127          return $instance;
 128      }
 129  
 130      /** Magic Methods *********************************************************/
 131  
 132      /**
 133       * A dummy constructor to prevent bbPress from being loaded more than once.
 134       *
 135       * @since 2.0.0 bbPress (r2464)
 136       *
 137       * @see bbPress::instance()
 138       * @see bbpress();
 139       */
 140  	private function __construct() { /* Do nothing here */ }
 141  
 142      /**
 143       * A dummy magic method to prevent bbPress from being cloned
 144       *
 145       * @since 2.0.0 bbPress (r2464)
 146       */
 147  	public function __clone() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bbpress' ), '2.1' ); }
 148  
 149      /**
 150       * A dummy magic method to prevent bbPress from being unserialized
 151       *
 152       * @since 2.0.0 bbPress (r2464)
 153       */
 154  	public function __wakeup() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'bbpress' ), '2.1' ); }
 155  
 156      /**
 157       * Magic method for checking the existence of a certain custom field
 158       *
 159       * @since 2.1.0 bbPress (r3951)
 160       */
 161  	public function __isset( $key ) { return isset( $this->data[ $key ] ); }
 162  
 163      /**
 164       * Magic method for getting bbPress variables
 165       *
 166       * @since 2.1.0 bbPress (r3951)
 167       */
 168  	public function __get( $key ) { return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null; }
 169  
 170      /**
 171       * Magic method for setting bbPress variables
 172       *
 173       * @since 2.1.0 bbPress (r3951)
 174       */
 175  	public function __set( $key, $value ) { $this->data[ $key ] = $value; }
 176  
 177      /**
 178       * Magic method for unsetting bbPress variables
 179       *
 180       * @since 2.3.0 bbPress (r4628)
 181       */
 182  	public function __unset( $key ) {
 183          if ( isset( $this->data[ $key ] ) ) {
 184              unset( $this->data[ $key ] );
 185          }
 186      }
 187  
 188      /**
 189       * Magic method to prevent notices and errors from invalid method calls
 190       *
 191       * @since 2.2.0 bbPress (r4252)
 192       */
 193  	public function __call( $name = '', $args = array() ) { unset( $name, $args ); return null; }
 194  
 195      /** Private Methods *******************************************************/
 196  
 197      /**
 198       * Setup the environment variables to allow the rest of bbPress to function
 199       * more easily.
 200       *
 201       * @since 2.0.0 bbPress (r2626)
 202       *
 203       * @access private
 204       */
 205  	private function setup_environment() {
 206  
 207          /** Versions **********************************************************/
 208  
 209          $this->version    = '2.7.0-alpha-2';
 210          $this->db_version = '263';
 211  
 212          /** Paths *************************************************************/
 213  
 214          // File & base
 215          $this->file         = __FILE__;
 216          $this->basename     = apply_filters( 'bbp_plugin_basename', str_replace( array( 'build/', 'src/' ), '', plugin_basename( $this->file ) ) );
 217          $this->basepath     = apply_filters( 'bbp_plugin_basepath', trailingslashit( dirname( $this->basename ) ) );
 218  
 219          // Path and URL
 220          $this->plugin_dir   = apply_filters( 'bbp_plugin_dir_path', plugin_dir_path( $this->file ) );
 221          $this->plugin_url   = apply_filters( 'bbp_plugin_dir_url',  plugin_dir_url ( $this->file ) );
 222  
 223          // Includes
 224          $this->includes_dir = apply_filters( 'bbp_includes_dir', trailingslashit( $this->plugin_dir . 'includes'  ) );
 225          $this->includes_url = apply_filters( 'bbp_includes_url', trailingslashit( $this->plugin_url . 'includes'  ) );
 226  
 227          // Languages
 228          $this->lang_base    = apply_filters( 'bbp_lang_base',    trailingslashit( $this->basepath   . 'languages' ) );
 229          $this->lang_dir     = apply_filters( 'bbp_lang_dir',     trailingslashit( $this->plugin_dir . 'languages' ) );
 230  
 231          // Templates
 232          $this->themes_dir   = apply_filters( 'bbp_themes_dir',   trailingslashit( $this->plugin_dir . 'templates' ) );
 233          $this->themes_url   = apply_filters( 'bbp_themes_url',   trailingslashit( $this->plugin_url . 'templates' ) );
 234      }
 235  
 236      /**
 237       * Smart defaults to many bbPress specific class variables.
 238       *
 239       * @since 2.6.0 bbPress (r6330)
 240       */
 241  	private function setup_variables() {
 242  
 243          /** Identifiers *******************************************************/
 244  
 245          // Post type identifiers
 246          $this->forum_post_type   = apply_filters( 'bbp_forum_post_type',  'forum'     );
 247          $this->topic_post_type   = apply_filters( 'bbp_topic_post_type',  'topic'     );
 248          $this->topic_tag_tax_id  = apply_filters( 'bbp_topic_tag_tax_id', 'topic-tag' );
 249          $this->reply_post_type   = apply_filters( 'bbp_reply_post_type',  'reply'     );
 250  
 251          // Status identifiers
 252          $this->spam_status_id    = apply_filters( 'bbp_spam_post_status',    'spam'    );
 253          $this->closed_status_id  = apply_filters( 'bbp_closed_post_status',  'closed'  );
 254          $this->orphan_status_id  = apply_filters( 'bbp_orphan_post_status',  'orphan'  );
 255          $this->public_status_id  = apply_filters( 'bbp_public_post_status',  'publish' );
 256          $this->pending_status_id = apply_filters( 'bbp_pending_post_status', 'pending' );
 257          $this->private_status_id = apply_filters( 'bbp_private_post_status', 'private' );
 258          $this->hidden_status_id  = apply_filters( 'bbp_hidden_post_status',  'hidden'  );
 259          $this->trash_status_id   = apply_filters( 'bbp_trash_post_status',   'trash'   );
 260  
 261          // Other identifiers
 262          $this->user_id           = apply_filters( 'bbp_user_id',        'bbp_user'        );
 263          $this->tops_id           = apply_filters( 'bbp_tops_id',        'bbp_tops'        );
 264          $this->reps_id           = apply_filters( 'bbp_reps_id',        'bbp_reps'        );
 265          $this->favs_id           = apply_filters( 'bbp_favs_id',        'bbp_favs'        );
 266          $this->subs_id           = apply_filters( 'bbp_subs_id',        'bbp_subs'        );
 267          $this->view_id           = apply_filters( 'bbp_view_id',        'bbp_view'        );
 268          $this->edit_id           = apply_filters( 'bbp_edit_id',        'edit'            );
 269          $this->paged_id          = apply_filters( 'bbp_paged_id',       'paged'           );
 270          $this->search_id         = apply_filters( 'bbp_search_id',      'bbp_search'      );
 271          $this->engagements_id    = apply_filters( 'bbp_engagements_id', 'bbp_engagements' );
 272  
 273          /** Queries ***********************************************************/
 274  
 275          $this->current_view_id      = 0; // Current view id
 276          $this->current_forum_id     = 0; // Current forum id
 277          $this->current_topic_id     = 0; // Current topic id
 278          $this->current_reply_id     = 0; // Current reply id
 279          $this->current_topic_tag_id = 0; // Current topic tag id
 280          $this->current_user_id      = 0; // Current topic tag id
 281  
 282          $this->forum_query    = new WP_Query();       // Main forum query
 283          $this->topic_query    = new WP_Query();       // Main topic query
 284          $this->reply_query    = new WP_Query();       // Main reply query
 285          $this->search_query   = new WP_Query();       // Main search query
 286          $this->user_query     = new BBP_User_Query(); // Main user query
 287  
 288          /** Theme Compat ******************************************************/
 289  
 290          $this->theme_compat   = new stdClass(); // Base theme compatibility class
 291          $this->filters        = new stdClass(); // Used when adding/removing filters
 292  
 293          /** Users *************************************************************/
 294  
 295          $this->current_user   = new WP_User(); // Currently logged in user
 296          $this->displayed_user = new WP_User(); // Currently displayed user
 297  
 298          /** Misc **************************************************************/
 299  
 300          $this->domain         = 'bbpress';      // Unique identifier for retrieving translated strings
 301          $this->extend         = new stdClass(); // Plugins add data here
 302          $this->errors         = new WP_Error(); // Feedback
 303  
 304          /** Deprecated ********************************************************/
 305  
 306          $this->tab_index      = apply_filters( 'bbp_default_tab_index', 100 );
 307      }
 308  
 309      /**
 310       * Include required files
 311       *
 312       * @since 2.0.0 bbPress (r2626)
 313       *
 314       * @access private
 315       */
 316  	private function includes() {
 317  
 318          /** Core **************************************************************/
 319  
 320          require $this->includes_dir . 'core/abstraction.php';
 321          require $this->includes_dir . 'core/sub-actions.php';
 322          require $this->includes_dir . 'core/functions.php';
 323          require $this->includes_dir . 'core/cache.php';
 324          require $this->includes_dir . 'core/options.php';
 325          require $this->includes_dir . 'core/capabilities.php';
 326          require $this->includes_dir . 'core/update.php';
 327          require $this->includes_dir . 'core/template-functions.php';
 328          require $this->includes_dir . 'core/template-loader.php';
 329          require $this->includes_dir . 'core/theme-compat.php';
 330  
 331          /** Components ********************************************************/
 332  
 333          // Common
 334          require $this->includes_dir . 'common/ajax.php';
 335          require $this->includes_dir . 'common/classes.php';
 336          require $this->includes_dir . 'common/engagements.php';
 337          require $this->includes_dir . 'common/functions.php';
 338          require $this->includes_dir . 'common/formatting.php';
 339          require $this->includes_dir . 'common/locale.php';
 340          require $this->includes_dir . 'common/locks.php';
 341          require $this->includes_dir . 'common/template.php';
 342          require $this->includes_dir . 'common/widgets.php';
 343          require $this->includes_dir . 'common/shortcodes.php';
 344  
 345          // Forums
 346          require $this->includes_dir . 'forums/capabilities.php';
 347          require $this->includes_dir . 'forums/functions.php';
 348          require $this->includes_dir . 'forums/template.php';
 349  
 350          // Topics
 351          require $this->includes_dir . 'topics/capabilities.php';
 352          require $this->includes_dir . 'topics/functions.php';
 353          require $this->includes_dir . 'topics/template.php';
 354  
 355          // Replies
 356          require $this->includes_dir . 'replies/capabilities.php';
 357          require $this->includes_dir . 'replies/functions.php';
 358          require $this->includes_dir . 'replies/template.php';
 359  
 360          // Search
 361          require $this->includes_dir . 'search/functions.php';
 362          require $this->includes_dir . 'search/template.php';
 363  
 364          // Users
 365          require $this->includes_dir . 'users/capabilities.php';
 366          require $this->includes_dir . 'users/engagements.php';
 367          require $this->includes_dir . 'users/functions.php';
 368          require $this->includes_dir . 'users/template.php';
 369          require $this->includes_dir . 'users/options.php';
 370          require $this->includes_dir . 'users/signups.php';
 371  
 372          /** Hooks *************************************************************/
 373  
 374          require $this->includes_dir . 'core/extend.php';
 375          require $this->includes_dir . 'core/actions.php';
 376          require $this->includes_dir . 'core/filters.php';
 377  
 378          /** Admin *************************************************************/
 379  
 380          // Quick admin check and load if needed
 381          if ( is_admin() ) {
 382              require $this->includes_dir . 'admin/actions.php';
 383          }
 384      }
 385  
 386      /**
 387       * Setup the default hooks and actions
 388       *
 389       * @since 2.0.0 bbPress (r2644)
 390       *
 391       * @access private
 392       */
 393  	private function setup_actions() {
 394  
 395          // Add actions to plugin activation and deactivation hooks
 396          add_action( 'activate_'   . $this->basename, 'bbp_activation'   );
 397          add_action( 'deactivate_' . $this->basename, 'bbp_deactivation' );
 398  
 399          // If bbPress is being deactivated, do not add any actions
 400          if ( bbp_is_deactivation( $this->basename ) ) {
 401              return;
 402          }
 403  
 404          // Array of bbPress core actions
 405          $actions = array(
 406              'setup_theme',              // Setup the default theme compat
 407              'setup_current_user',       // Setup currently logged in user
 408              'setup_engagements',        // Setup user engagements strategy
 409              'roles_init',               // User roles init
 410              'register_meta',            // Register meta (forum|topic|reply|user)
 411              'register_post_types',      // Register post types (forum|topic|reply)
 412              'register_post_statuses',   // Register post statuses (closed|spam|orphan|hidden)
 413              'register_taxonomies',      // Register taxonomies (topic-tag)
 414              'register_shortcodes',      // Register shortcodes (bbp-login)
 415              'register_views',           // Register the views (no-replies)
 416              'register_theme_packages',  // Register bundled theme packages (bbp-theme-compat/bbp-themes)
 417              'load_textdomain',          // Load textdomain (bbpress)
 418              'add_rewrite_tags',         // Add rewrite tags (view|user|edit|search)
 419              'add_rewrite_rules',        // Generate rewrite rules (view|edit|paged|search)
 420              'add_permastructs'          // Add permalink structures (view|user|search)
 421          );
 422  
 423          // Add the actions
 424          foreach ( $actions as $class_action ) {
 425              add_action( 'bbp_' . $class_action, array( $this, $class_action ), 5 );
 426          }
 427  
 428          // All bbPress actions are setup (includes bbp-core-hooks.php)
 429          do_action_ref_array( 'bbp_after_setup_actions', array( &$this ) );
 430      }
 431  
 432      /** Public Methods ********************************************************/
 433  
 434      /**
 435       * Register bundled theme packages
 436       *
 437       * Note that since we currently have complete control over bbp-themes and
 438       * the bbp-theme-compat folders, it's fine to hardcode these here. If at a
 439       * later date we need to automate this, and API will need to be built.
 440       *
 441       * @since 2.1.0 bbPress (r3829)
 442       */
 443  	public function register_theme_packages() {
 444  
 445          // Register the basic theme stack. This is really dope.
 446          bbp_register_template_stack( 'get_stylesheet_directory', 6 );
 447          bbp_register_template_stack( 'get_template_directory',   8 );
 448  
 449          // Register the default theme compatibility package
 450          bbp_register_theme_package( array(
 451              'id'      => 'default',
 452              'name'    => 'bbPress Default',
 453              'version' => bbp_get_version(),
 454              'dir'     => trailingslashit( $this->themes_dir . 'default' ),
 455              'url'     => trailingslashit( $this->themes_url . 'default' )
 456          ) );
 457      }
 458  
 459      /**
 460       * Setup the default bbPress theme compatibility location.
 461       *
 462       * @since 2.1.0 bbPress (r3778)
 463       */
 464  	public function setup_theme() {
 465          bbp_setup_theme_compat( bbp_get_theme_package_id() );
 466      }
 467  
 468      /**
 469       * Load the translation file for current language. Checks the deprecated
 470       * languages folder inside the bbPress plugin first, and then the default
 471       * WordPress languages folder.
 472       *
 473       * Note that custom translation files inside the bbPress plugin folder
 474       * will be removed on bbPress updates. If you're creating custom
 475       * translation files, please use the global language folder.
 476       *
 477       * @since 2.0.0 bbPress (r2596)
 478       */
 479  	public function load_textdomain() {
 480  
 481          // Define the old directory
 482          $old_dir = WP_LANG_DIR . '/bbpress/';
 483  
 484          // Old location, deprecated in 2.6.0
 485          if ( is_dir( $old_dir ) ) {
 486  
 487              // Get locale & file-name
 488              $type   = is_admin() ? get_user_locale() : get_locale();
 489              $locale = apply_filters( 'plugin_locale', $type, $this->domain );
 490              $mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale );
 491  
 492              // Look in global /wp-content/languages/bbpress/ folder
 493              load_textdomain( $this->domain, $old_dir . $mofile );
 494          }
 495  
 496          // Look in global /wp-content/languages/plugins/
 497          load_plugin_textdomain( $this->domain, false, $this->lang_base );
 498      }
 499  
 500      /**
 501       * Setup the post types for forums, topics and replies
 502       *
 503       * @since 2.0.0 bbPress (r2597)
 504       */
 505  	public static function register_post_types() {
 506  
 507          /** Forums ************************************************************/
 508  
 509          // Register Forum content type
 510          register_post_type(
 511              bbp_get_forum_post_type(),
 512              apply_filters( 'bbp_register_forum_post_type', array(
 513                  'labels'              => bbp_get_forum_post_type_labels(),
 514                  'rewrite'             => bbp_get_forum_post_type_rewrite(),
 515                  'supports'            => bbp_get_forum_post_type_supports(),
 516                  'description'         => esc_html__( 'bbPress Forums', 'bbpress' ),
 517                  'capabilities'        => bbp_get_forum_caps(),
 518                  'capability_type'     => array( 'forum', 'forums' ),
 519                  'menu_position'       => 555555,
 520                  'has_archive'         => bbp_get_root_slug(),
 521                  'exclude_from_search' => true,
 522                  'show_in_nav_menus'   => true,
 523                  'public'              => true,
 524                  'show_ui'             => current_user_can( 'bbp_forums_admin' ),
 525                  'can_export'          => true,
 526                  'hierarchical'        => true,
 527                  'query_var'           => true,
 528                  'menu_icon'           => '',
 529                  'source'              => 'bbpress',
 530              ) )
 531          );
 532  
 533          /** Topics ************************************************************/
 534  
 535          // Register Topic content type
 536          register_post_type(
 537              bbp_get_topic_post_type(),
 538              apply_filters( 'bbp_register_topic_post_type', array(
 539                  'labels'              => bbp_get_topic_post_type_labels(),
 540                  'rewrite'             => bbp_get_topic_post_type_rewrite(),
 541                  'supports'            => bbp_get_topic_post_type_supports(),
 542                  'description'         => esc_html__( 'bbPress Topics', 'bbpress' ),
 543                  'capabilities'        => bbp_get_topic_caps(),
 544                  'capability_type'     => array( 'topic', 'topics' ),
 545                  'menu_position'       => 555555,
 546                  'has_archive'         => ( 'forums' === bbp_show_on_root() ) ? bbp_get_topic_archive_slug() : false,
 547                  'exclude_from_search' => true,
 548                  'show_in_nav_menus'   => false,
 549                  'public'              => true,
 550                  'show_ui'             => current_user_can( 'bbp_topics_admin' ),
 551                  'can_export'          => true,
 552                  'hierarchical'        => false,
 553                  'query_var'           => true,
 554                  'menu_icon'           => '',
 555                  'source'              => 'bbpress',
 556              ) )
 557          );
 558  
 559          /** Replies ***********************************************************/
 560  
 561          // Register reply content type
 562          register_post_type(
 563              bbp_get_reply_post_type(),
 564              apply_filters( 'bbp_register_reply_post_type', array(
 565                  'labels'              => bbp_get_reply_post_type_labels(),
 566                  'rewrite'             => bbp_get_reply_post_type_rewrite(),
 567                  'supports'            => bbp_get_reply_post_type_supports(),
 568                  'description'         => esc_html__( 'bbPress Replies', 'bbpress' ),
 569                  'capabilities'        => bbp_get_reply_caps(),
 570                  'capability_type'     => array( 'reply', 'replies' ),
 571                  'menu_position'       => 555555,
 572                  'exclude_from_search' => true,
 573                  'has_archive'         => false,
 574                  'show_in_nav_menus'   => false,
 575                  'public'              => true,
 576                  'show_ui'             => current_user_can( 'bbp_replies_admin' ),
 577                  'can_export'          => true,
 578                  'hierarchical'        => false,
 579                  'query_var'           => true,
 580                  'menu_icon'           => '',
 581                  'source'              => 'bbpress',
 582              ) )
 583          );
 584      }
 585  
 586      /**
 587       * Register the post statuses used by bbPress
 588       *
 589       * We do some manipulation of the 'trash' status so trashed topics and
 590       * replies can be viewed from within the theme.
 591       *
 592       * @since 2.0.0 bbPress (r2727)
 593       */
 594  	public static function register_post_statuses() {
 595  
 596          // Closed
 597          register_post_status(
 598              bbp_get_closed_status_id(),
 599              apply_filters( 'bbp_register_closed_post_status', array(
 600                  'label'                     => _x( 'Closed', 'post', 'bbpress' ),
 601                  'label_count'               => _nx_noop( 'Closed <span class="count">(%s)</span>', 'Closed <span class="count">(%s)</span>', 'post', 'bbpress' ),
 602                  'public'                    => true,
 603                  'show_in_admin_status_list' => true,
 604                  'show_in_admin_all_list'    => true,
 605                  'source'                    => 'bbpress'
 606              ) )
 607          );
 608  
 609          // Spam
 610          register_post_status(
 611              bbp_get_spam_status_id(),
 612              apply_filters( 'bbp_register_spam_post_status', array(
 613                  'label'                     => _x( 'Spam', 'post', 'bbpress' ),
 614                  'label_count'               => _nx_noop( 'Spam <span class="count">(%s)</span>', 'Spam <span class="count">(%s)</span>', 'post', 'bbpress' ),
 615                  'protected'                 => true,
 616                  'exclude_from_search'       => true,
 617                  'show_in_admin_status_list' => true,
 618                  'show_in_admin_all_list'    => false,
 619                  'source'                    => 'bbpress'
 620              ) )
 621          );
 622  
 623          // Orphan
 624          register_post_status(
 625              bbp_get_orphan_status_id(),
 626              apply_filters( 'bbp_register_orphan_post_status', array(
 627                  'label'                     => _x( 'Orphan', 'post', 'bbpress' ),
 628                  'label_count'               => _nx_noop( 'Orphan <span class="count">(%s)</span>', 'Orphans <span class="count">(%s)</span>', 'post', 'bbpress' ),
 629                  'protected'                 => true,
 630                  'exclude_from_search'       => true,
 631                  'show_in_admin_status_list' => true,
 632                  'show_in_admin_all_list'    => false,
 633                  'source'                    => 'bbpress'
 634              ) )
 635          );
 636  
 637          // Hidden
 638          register_post_status(
 639              bbp_get_hidden_status_id(),
 640              apply_filters( 'bbp_register_hidden_post_status', array(
 641                  'label'                     => _x( 'Hidden', 'post', 'bbpress' ),
 642                  'label_count'               => _nx_noop( 'Hidden <span class="count">(%s)</span>', 'Hidden <span class="count">(%s)</span>', 'post', 'bbpress' ),
 643                  'private'                   => true,
 644                  'exclude_from_search'       => true,
 645                  'show_in_admin_status_list' => true,
 646                  'show_in_admin_all_list'    => true,
 647                  'source'                    => 'bbpress'
 648              ) )
 649          );
 650  
 651          /**
 652           * Trash fix
 653           *
 654           * We need to remove the internal arg and change that to
 655           * protected so that the users with 'view_trash' cap can view
 656           * single trashed topics/replies in the front-end as wp_query
 657           * doesn't allow any hack for the trashed topics to be viewed.
 658           */
 659          global $wp_post_statuses;
 660  
 661          if ( ! empty( $wp_post_statuses['trash'] ) ) {
 662  
 663              // User can view trash so set internal to false
 664              if ( current_user_can( 'view_trash' ) ) {
 665                  $wp_post_statuses['trash']->internal  = false;
 666                  $wp_post_statuses['trash']->protected = true;
 667  
 668              // User cannot view trash so set internal to true
 669              } else {
 670                  $wp_post_statuses['trash']->internal = true;
 671              }
 672          }
 673      }
 674  
 675      /**
 676       * Register the topic tag and forum moderator taxonomies
 677       *
 678       * @since 2.0.0 bbPress (r2464) Added bbp_get_topic_tag_tax_id() taxonomy
 679       */
 680  	public static function register_taxonomies() {
 681  
 682          // Register the topic-tag taxonomy.
 683          register_taxonomy(
 684              bbp_get_topic_tag_tax_id(),
 685              bbp_get_topic_post_type(),
 686              apply_filters( 'bbp_register_topic_taxonomy', array(
 687                  'labels'                => bbp_get_topic_tag_tax_labels(),
 688                  'rewrite'               => bbp_get_topic_tag_tax_rewrite(),
 689                  'capabilities'          => bbp_get_topic_tag_caps(),
 690                  'update_count_callback' => 'bbp_update_topic_tag_count',
 691                  'query_var'             => true,
 692                  'show_tagcloud'         => true,
 693                  'hierarchical'          => false,
 694                  'show_in_nav_menus'     => false,
 695                  'public'                => true,
 696                  'show_ui'               => bbp_allow_topic_tags() && current_user_can( 'bbp_topic_tags_admin' ),
 697                  'source'                => 'bbpress'
 698              )
 699          ) );
 700      }
 701  
 702      /**
 703       * Register the bbPress views
 704       *
 705       * @since 2.0.0 bbPress (r2789)
 706       */
 707  	public static function register_views() {
 708  
 709          // Popular topics
 710          bbp_register_view(
 711              'popular',
 712              esc_html__( 'Most popular topics', 'bbpress' ),
 713              apply_filters( 'bbp_register_view_popular', array(
 714                  'meta_key'      => '_bbp_reply_count',
 715                  'meta_type'     => 'NUMERIC',
 716                  'max_num_pages' => 1,
 717                  'orderby'       => 'meta_value_num',
 718                  'show_stickies' => false
 719              )
 720          ) );
 721  
 722          // Topics with no replies
 723          bbp_register_view(
 724              'no-replies',
 725              esc_html__( 'Topics with no replies', 'bbpress' ),
 726              apply_filters( 'bbp_register_view_no_replies', array(
 727                  'meta_key'      => '_bbp_reply_count',
 728                  'meta_type'     => 'NUMERIC',
 729                  'meta_value'    => 1,
 730                  'meta_compare'  => '<',
 731                  'orderby'       => '',
 732                  'show_stickies' => false
 733              )
 734          ) );
 735      }
 736  
 737      /**
 738       * Register the bbPress shortcodes
 739       *
 740       * @since 2.0.0 bbPress (r3031)
 741       */
 742  	public function register_shortcodes() {
 743          $this->shortcodes = new BBP_Shortcodes();
 744      }
 745  
 746      /**
 747       * Register bbPress meta-data
 748       *
 749       * Counts added in 2.6.0 to avoid negative values
 750       *
 751       * @since 2.6.0 bbPress (r6300)
 752       */
 753  	public function register_meta() {
 754  
 755          // Define "count" meta-type array
 756          $count = array(
 757  
 758              // Counts are always integers
 759              'type'              => 'integer',
 760  
 761              // Generic count description
 762              'description'       => esc_html__( 'bbPress Item Count', 'bbpress' ),
 763  
 764              // Counts are single values
 765              'single'            => true,
 766  
 767              // Counts should be made available in REST
 768              'show_in_rest'      => true,
 769  
 770              // Never allow counts to go negative
 771              'sanitize_callback' => 'bbp_number_not_negative',
 772  
 773              // All users may update count meta data
 774              'auth_callback'     => '__return_true'
 775          );
 776  
 777          /** Post **************************************************************/
 778  
 779          // Counts
 780          register_meta( 'post', '_bbp_topic_count',           $count );
 781          register_meta( 'post', '_bbp_reply_count',           $count );
 782          register_meta( 'post', '_bbp_total_topic_count',     $count );
 783          register_meta( 'post', '_bbp_total_reply_count',     $count );
 784          register_meta( 'post', '_bbp_voice_count',           $count );
 785          register_meta( 'post', '_bbp_anonymous_reply_count', $count );
 786          register_meta( 'post', '_bbp_topic_count_hidden',    $count );
 787          register_meta( 'post', '_bbp_reply_count_hidden',    $count );
 788          register_meta( 'post', '_bbp_forum_subforum_count',  $count );
 789  
 790          /* User ***************************************************************/
 791  
 792          // Counts
 793          register_meta( 'user', '_bbp_topic_count', $count );
 794          register_meta( 'user', '_bbp_reply_count', $count );
 795  
 796          // Activity
 797          register_meta( 'user', '_bbp_last_posted', array(
 798              'type'              => 'integer',
 799              'description'       => esc_html__( 'bbPress User Activity', 'bbpress' ),
 800              'single'            => true,
 801              'show_in_rest'      => true,
 802              'sanitize_callback' => 'bbp_number_not_negative',
 803              'auth_callback'     => '__return_true'
 804          ) );
 805      }
 806  
 807      /**
 808       * Setup the currently logged-in user
 809       *
 810       * @since 2.0.0 bbPress (r2697)
 811       */
 812  	public function setup_current_user() {
 813          $this->current_user = wp_get_current_user();
 814      }
 815  
 816      /**
 817       * Setup the user engagements strategy
 818       *
 819       * @since 2.6.0 bbPress (r6875)
 820       */
 821  	public function setup_engagements() {
 822  
 823          // Setup the class name
 824          $strategy   = ucwords( bbp_engagements_strategy() );
 825          $class_name = "BBP_User_Engagements_{$strategy}";
 826  
 827          // Setup the engagements interface
 828          $this->engagements = new $class_name();
 829      }
 830  
 831      /**
 832       * Initialize forum-specific roles
 833       *
 834       * @since 2.6.0
 835       */
 836  	public function roles_init() {
 837  
 838          // Get role IDs
 839          $keymaster   = bbp_get_keymaster_role();
 840          $moderator   = bbp_get_moderator_role();
 841          $participant = bbp_get_participant_role();
 842          $spectator   = bbp_get_spectator_role();
 843          $blocked     = bbp_get_blocked_role();
 844  
 845          // Build the roles into one useful array
 846          $this->roles[ $keymaster   ] = new WP_Role( 'Keymaster',   bbp_get_caps_for_role( $keymaster   ) );
 847          $this->roles[ $moderator   ] = new WP_Role( 'Moderator',   bbp_get_caps_for_role( $moderator   ) );
 848          $this->roles[ $participant ] = new WP_Role( 'Participant', bbp_get_caps_for_role( $participant ) );
 849          $this->roles[ $spectator   ] = new WP_Role( 'Spectator',   bbp_get_caps_for_role( $spectator   ) );
 850          $this->roles[ $blocked     ] = new WP_Role( 'Blocked',     bbp_get_caps_for_role( $blocked     ) );
 851      }
 852  
 853      /** Custom Rewrite Rules **************************************************/
 854  
 855      /**
 856       * Add the bbPress-specific rewrite tags
 857       *
 858       * @since 2.0.0 bbPress (r2753)
 859       */
 860  	public static function add_rewrite_tags() {
 861          add_rewrite_tag( '%' . bbp_get_view_rewrite_id()               . '%', '([^/]+)'   ); // View Page tag
 862          add_rewrite_tag( '%' . bbp_get_edit_rewrite_id()               . '%', '([1]{1,})' ); // Edit Page tag
 863          add_rewrite_tag( '%' . bbp_get_search_rewrite_id()             . '%', '([^/]+)'   ); // Search Results tag
 864          add_rewrite_tag( '%' . bbp_get_user_rewrite_id()               . '%', '([^/]+)'   ); // User Profile tag
 865          add_rewrite_tag( '%' . bbp_get_user_favorites_rewrite_id()     . '%', '([1]{1,})' ); // User Favorites tag
 866          add_rewrite_tag( '%' . bbp_get_user_subscriptions_rewrite_id() . '%', '([1]{1,})' ); // User Subscriptions tag
 867          add_rewrite_tag( '%' . bbp_get_user_engagements_rewrite_id()   . '%', '([1]{1,})' ); // User Engagements tag
 868          add_rewrite_tag( '%' . bbp_get_user_topics_rewrite_id()        . '%', '([1]{1,})' ); // User Topics Tag
 869          add_rewrite_tag( '%' . bbp_get_user_replies_rewrite_id()       . '%', '([1]{1,})' ); // User Replies Tag
 870      }
 871  
 872      /**
 873       * Add bbPress-specific rewrite rules for uri's that are not
 874       * setup for us by way of custom post types or taxonomies. This includes:
 875       * - Front-end editing
 876       * - Topic views
 877       * - User profiles
 878       *
 879       * @since 2.0.0 bbPress (r2688)
 880       *
 881       * @todo Extract into an API
 882       */
 883  	public static function add_rewrite_rules() {
 884  
 885          /** Setup *************************************************************/
 886  
 887          // Add rules to top or bottom?
 888          $priority           = 'top';
 889  
 890          // Single Slugs
 891          $forum_slug         = bbp_get_forum_slug();
 892          $topic_slug         = bbp_get_topic_slug();
 893          $reply_slug         = bbp_get_reply_slug();
 894          $ttag_slug          = bbp_get_topic_tag_tax_slug();
 895  
 896          // Archive Slugs
 897          $user_slug          = bbp_get_user_slug();
 898          $view_slug          = bbp_get_view_slug();
 899          $search_slug        = bbp_get_search_slug();
 900          $topic_archive_slug = bbp_get_topic_archive_slug();
 901          $reply_archive_slug = bbp_get_reply_archive_slug();
 902  
 903          // Tertiary Slugs
 904          $feed_slug          = 'feed';
 905          $edit_slug          = bbp_get_edit_slug();
 906          $paged_slug         = bbp_get_paged_slug();
 907          $user_favs_slug     = bbp_get_user_favorites_slug();
 908          $user_subs_slug     = bbp_get_user_subscriptions_slug();
 909          $user_engs_slug     = bbp_get_user_engagements_slug();
 910  
 911          // Unique rewrite ID's
 912          $feed_id            = 'feed';
 913          $edit_id            = bbp_get_edit_rewrite_id();
 914          $view_id            = bbp_get_view_rewrite_id();
 915          $paged_id           = bbp_get_paged_rewrite_id();
 916          $search_id          = bbp_get_search_rewrite_id();
 917          $user_id            = bbp_get_user_rewrite_id();
 918          $user_favs_id       = bbp_get_user_favorites_rewrite_id();
 919          $user_subs_id       = bbp_get_user_subscriptions_rewrite_id();
 920          $user_tops_id       = bbp_get_user_topics_rewrite_id();
 921          $user_reps_id       = bbp_get_user_replies_rewrite_id();
 922          $user_engs_id       = bbp_get_user_engagements_rewrite_id();
 923  
 924          // Rewrite rule matches used repeatedly below
 925          $root_rule    = '/([^/]+)/?$';
 926          $feed_rule    = '/([^/]+)/' . $feed_slug  . '/?$';
 927          $edit_rule    = '/([^/]+)/' . $edit_slug  . '/?$';
 928          $paged_rule   = '/([^/]+)/' . $paged_slug . '/?([0-9]{1,})/?$';
 929  
 930          // Search rules (without slug check)
 931          $search_root_rule  = '/?$';
 932          $search_paged_rule = '/' . $paged_slug . '/?([0-9]{1,})/?$';
 933  
 934          /** Add ***************************************************************/
 935  
 936          // User profile rules
 937          $tops_rule       = '/([^/]+)/' . $topic_archive_slug . '/?$';
 938          $reps_rule       = '/([^/]+)/' . $reply_archive_slug . '/?$';
 939          $favs_rule       = '/([^/]+)/' . $user_favs_slug     . '/?$';
 940          $subs_rule       = '/([^/]+)/' . $user_subs_slug     . '/?$';
 941          $engs_rule       = '/([^/]+)/' . $user_engs_slug     . '/?$';
 942          $tops_paged_rule = '/([^/]+)/' . $topic_archive_slug . '/' . $paged_slug . '/?([0-9]{1,})/?$';
 943          $reps_paged_rule = '/([^/]+)/' . $reply_archive_slug . '/' . $paged_slug . '/?([0-9]{1,})/?$';
 944          $favs_paged_rule = '/([^/]+)/' . $user_favs_slug     . '/' . $paged_slug . '/?([0-9]{1,})/?$';
 945          $subs_paged_rule = '/([^/]+)/' . $user_subs_slug     . '/' . $paged_slug . '/?([0-9]{1,})/?$';
 946          $engs_paged_rule = '/([^/]+)/' . $user_engs_slug     . '/' . $paged_slug . '/?([0-9]{1,})/?$';
 947  
 948          // Edit Forum|Topic|Reply|Topic-tag
 949          add_rewrite_rule( $forum_slug . $edit_rule, 'index.php?' . bbp_get_forum_post_type()  . '=$matches[1]&' . $edit_id . '=1', $priority );
 950          add_rewrite_rule( $topic_slug . $edit_rule, 'index.php?' . bbp_get_topic_post_type()  . '=$matches[1]&' . $edit_id . '=1', $priority );
 951          add_rewrite_rule( $reply_slug . $edit_rule, 'index.php?' . bbp_get_reply_post_type()  . '=$matches[1]&' . $edit_id . '=1', $priority );
 952          add_rewrite_rule( $ttag_slug  . $edit_rule, 'index.php?' . bbp_get_topic_tag_tax_id() . '=$matches[1]&' . $edit_id . '=1', $priority );
 953  
 954          // User Pagination|Edit|View
 955          add_rewrite_rule( $user_slug . $tops_paged_rule, 'index.php?' . $user_id  . '=$matches[1]&' . $user_tops_id . '=1&' . $paged_id . '=$matches[2]', $priority );
 956          add_rewrite_rule( $user_slug . $reps_paged_rule, 'index.php?' . $user_id  . '=$matches[1]&' . $user_reps_id . '=1&' . $paged_id . '=$matches[2]', $priority );
 957          add_rewrite_rule( $user_slug . $favs_paged_rule, 'index.php?' . $user_id  . '=$matches[1]&' . $user_favs_id . '=1&' . $paged_id . '=$matches[2]', $priority );
 958          add_rewrite_rule( $user_slug . $subs_paged_rule, 'index.php?' . $user_id  . '=$matches[1]&' . $user_subs_id . '=1&' . $paged_id . '=$matches[2]', $priority );
 959          add_rewrite_rule( $user_slug . $engs_paged_rule, 'index.php?' . $user_id  . '=$matches[1]&' . $user_engs_id . '=1&' . $paged_id . '=$matches[2]', $priority );
 960          add_rewrite_rule( $user_slug . $tops_rule,       'index.php?' . $user_id  . '=$matches[1]&' . $user_tops_id . '=1',                               $priority );
 961          add_rewrite_rule( $user_slug . $reps_rule,       'index.php?' . $user_id  . '=$matches[1]&' . $user_reps_id . '=1',                               $priority );
 962          add_rewrite_rule( $user_slug . $favs_rule,       'index.php?' . $user_id  . '=$matches[1]&' . $user_favs_id . '=1',                               $priority );
 963          add_rewrite_rule( $user_slug . $subs_rule,       'index.php?' . $user_id  . '=$matches[1]&' . $user_subs_id . '=1',                               $priority );
 964          add_rewrite_rule( $user_slug . $engs_rule,       'index.php?' . $user_id  . '=$matches[1]&' . $user_engs_id . '=1',                               $priority );
 965          add_rewrite_rule( $user_slug . $edit_rule,       'index.php?' . $user_id  . '=$matches[1]&' . $edit_id      . '=1',                               $priority );
 966          add_rewrite_rule( $user_slug . $root_rule,       'index.php?' . $user_id  . '=$matches[1]',                                                       $priority );
 967  
 968          // Topic-View Pagination|Feed|View
 969          add_rewrite_rule( $view_slug . $paged_rule, 'index.php?' . $view_id . '=$matches[1]&' . $paged_id . '=$matches[2]', $priority );
 970          add_rewrite_rule( $view_slug . $feed_rule,  'index.php?' . $view_id . '=$matches[1]&' . $feed_id  . '=$matches[2]', $priority );
 971          add_rewrite_rule( $view_slug . $root_rule,  'index.php?' . $view_id . '=$matches[1]',                               $priority );
 972  
 973          // Search All
 974          add_rewrite_rule( $search_slug . $search_paged_rule, 'index.php?' . $paged_id .'=$matches[1]', $priority );
 975          add_rewrite_rule( $search_slug . $search_root_rule,  'index.php?' . $search_id,                $priority );
 976      }
 977  
 978      /**
 979       * Add permalink structures for new archive-style destinations.
 980       *
 981       * - Users
 982       * - Topic Views
 983       * - Search
 984       *
 985       * @since 2.4.0 bbPress (r4930)
 986       */
 987  	public static function add_permastructs() {
 988  
 989          // Get unique ID's
 990          $user_id     = bbp_get_user_rewrite_id();
 991          $view_id     = bbp_get_view_rewrite_id();
 992          $search_id   = bbp_get_search_rewrite_id();
 993  
 994          // Get root slugs
 995          $user_slug   = bbp_get_user_slug();
 996          $view_slug   = bbp_get_view_slug();
 997          $search_slug = bbp_get_search_slug();
 998  
 999          // User Permastruct
1000          add_permastruct( $user_id, $user_slug . '/%' . $user_id . '%', array(
1001              'with_front'  => false,
1002              'ep_mask'     => EP_NONE,
1003              'paged'       => false,
1004              'feed'        => false,
1005              'forcomments' => false,
1006              'walk_dirs'   => true,
1007              'endpoints'   => false,
1008          ) );
1009  
1010          // Topic View Permastruct
1011          add_permastruct( $view_id, $view_slug . '/%' . $view_id . '%', array(
1012              'with_front'  => false,
1013              'ep_mask'     => EP_NONE,
1014              'paged'       => false,
1015              'feed'        => false,
1016              'forcomments' => false,
1017              'walk_dirs'   => true,
1018              'endpoints'   => false,
1019          ) );
1020  
1021          // Search Permastruct
1022          add_permastruct( $search_id, $search_slug . '/%' . $search_id . '%', array(
1023              'with_front'  => false,
1024              'ep_mask'     => EP_NONE,
1025              'paged'       => true,
1026              'feed'        => false,
1027              'forcomments' => false,
1028              'walk_dirs'   => true,
1029              'endpoints'   => false,
1030          ) );
1031      }
1032  }
1033  
1034  /**
1035   * The main function responsible for returning the one true bbPress Instance
1036   * to functions everywhere.
1037   *
1038   * Use this function like you would a global variable, except without needing
1039   * to declare the global.
1040   *
1041   * Example: <?php $bbp = bbpress(); ?>
1042   *
1043   * @since 2.0.0 bbPress (r2464)
1044   *
1045   * @return bbPress The one true bbPress Instance
1046   */
1047  function bbpress() {
1048      return bbPress::instance();
1049  }
1050  
1051  /**
1052   * Hook bbPress early onto the 'plugins_loaded' action.
1053   *
1054   * This gives all other plugins the chance to load before bbPress, to get their
1055   * actions, filters, and overrides setup without bbPress being in the way.
1056   */
1057  if ( defined( 'BBPRESS_LATE_LOAD' ) ) {
1058      add_action( 'plugins_loaded', 'bbpress', (int) BBPRESS_LATE_LOAD );
1059  
1060  // "And now here's something we hope you'll really like!"
1061  } else {
1062      bbpress();
1063  }
1064  
1065  endif; // class_exists check


Generated: Sat Jul 2 01:00:48 2022 Cross-referenced by PHPXref 0.7.1