[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/core/ -> theme-compat.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Theme Compatibility
   5   *
   6   * @package bbPress
   7   * @subpackage Core
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /** Theme Compat **************************************************************/
  14  
  15  /**
  16   * What follows is an attempt at intercepting the natural page load process
  17   * to replace the_content() with the appropriate bbPress content.
  18   *
  19   * To do this, bbPress does several direct manipulations of global variables
  20   * and forces them to do what they are not supposed to be doing.
  21   *
  22   * Don't try anything you're about to witness here, at home. Ever.
  23   */
  24  
  25  /** Base Class ****************************************************************/
  26  
  27  /**
  28   * Theme Compatibility base class
  29   *
  30   * This is only intended to be extended, and is included here as a basic guide
  31   * for future Template Packs to use. @link bbp_setup_theme_compat()
  32   *
  33   * @since 2.0.0 bbPress (r3506)
  34   */
  35  class BBP_Theme_Compat {
  36  
  37      /**
  38       * Should be like:
  39       *
  40       * array(
  41       *     'id'      => ID of the theme (should be unique)
  42       *     'name'    => Name of the theme (should match style.css)
  43       *     'version' => Theme version for cache busting scripts and styling
  44       *     'dir'     => Path to theme
  45       *     'url'     => URL to theme
  46       * );
  47       * @var array
  48       */
  49      private $_data = array();
  50  
  51      /**
  52       * Pass the $properties to the object on creation.
  53       *
  54       * @since 2.1.0 bbPress (r3926)
  55       *
  56       * @param array $properties
  57       */
  58  	public function __construct( Array $properties = array() ) {
  59          $this->_data = $properties;
  60      }
  61  
  62      /**
  63       * Set a theme's property.
  64       *
  65       * @since 2.1.0 bbPress (r3926)
  66       *
  67       * @param string $property
  68       * @param mixed $value
  69       * @return mixed
  70       */
  71  	public function __set( $property, $value ) {
  72          return $this->_data[ $property ] = $value;
  73      }
  74  
  75      /**
  76       * Get a theme's property.
  77       *
  78       * @since 2.1.0 bbPress (r3926)
  79       *
  80       * @param string $property
  81       * @param mixed $value
  82       * @return mixed
  83       */
  84  	public function __get( $property ) {
  85          return array_key_exists( $property, $this->_data )
  86              ? $this->_data[ $property ]
  87              : '';
  88      }
  89  
  90      /**
  91       * Return the template directory.
  92       *
  93       * @since 2.6.0 bbPress (r6548)
  94       *
  95       * @return string
  96       */
  97  	public function get_dir() {
  98          return $this->dir;
  99      }
 100  }
 101  
 102  /** Functions *****************************************************************/
 103  
 104  /**
 105   * Setup the active template pack and register it's directory in the stack.
 106   *
 107   * @since 2.0.0 bbPress (r3311)
 108   *
 109   * @param BBP_Theme_Compat $theme
 110   */
 111  function bbp_setup_theme_compat( $theme = 'default' ) {
 112      $bbp = bbpress();
 113  
 114      // Bail if something already has this under control
 115      if ( ! empty( $bbp->theme_compat->theme ) ) {
 116          return;
 117      }
 118  
 119      // Fallback for empty theme
 120      if ( empty( $theme ) ) {
 121          $theme = 'default';
 122      }
 123  
 124      // If the theme is registered, use it and add it to the stack
 125      if ( isset( $bbp->theme_compat->packages[ $theme ] ) ) {
 126          $bbp->theme_compat->theme = $bbp->theme_compat->packages[ $theme ];
 127  
 128          // Setup the template stack for the active template pack
 129          bbp_register_template_stack( array( $bbp->theme_compat->theme, 'get_dir' ) );
 130      }
 131  }
 132  
 133  /**
 134   * Get the current template pack package.
 135   *
 136   * @since 2.6.0 bbPress (r6548)
 137   *
 138   * @return BBP_Theme_Compat
 139   */
 140  function bbp_get_current_template_pack() {
 141      $bbp = bbpress();
 142  
 143      // Theme was not setup, so fallback to an empty object
 144      if ( empty( $bbp->theme_compat->theme ) ) {
 145          $bbp->theme_compat->theme = new BBP_Theme_Compat();
 146      }
 147  
 148      // Filter & return
 149      return apply_filters( 'bbp_get_current_template_pack', $bbp->theme_compat->theme );
 150  }
 151  
 152  /**
 153   * Gets the id of the bbPress compatible theme used, in the event the
 154   * currently active WordPress theme does not explicitly support bbPress.
 155   * This can be filtered or set manually. Tricky theme authors can override the
 156   * default and include their own bbPress compatibility layers for their themes.
 157   *
 158   * @since 2.0.0 bbPress (r3506)
 159   *
 160   * @return string
 161   */
 162  function bbp_get_theme_compat_id() {
 163  
 164      // Filter & return
 165      return apply_filters( 'bbp_get_theme_compat_id', bbp_get_current_template_pack()->id );
 166  }
 167  
 168  /**
 169   * Gets the name of the bbPress compatible theme used, in the event the
 170   * currently active WordPress theme does not explicitly support bbPress.
 171   * This can be filtered or set manually. Tricky theme authors can override the
 172   * default and include their own bbPress compatibility layers for their themes.
 173   *
 174   * @since 2.0.0 bbPress (r3506)
 175   *
 176   * @return string
 177   */
 178  function bbp_get_theme_compat_name() {
 179  
 180      // Filter & return
 181      return apply_filters( 'bbp_get_theme_compat_name', bbp_get_current_template_pack()->name );
 182  }
 183  
 184  /**
 185   * Gets the version of the bbPress compatible theme used, in the event the
 186   * currently active WordPress theme does not explicitly support bbPress.
 187   * This can be filtered or set manually. Tricky theme authors can override the
 188   * default and include their own bbPress compatibility layers for their themes.
 189   *
 190   * @since 2.0.0 bbPress (r3506)
 191   *
 192   * @return string
 193   */
 194  function bbp_get_theme_compat_version() {
 195  
 196      // Filter & return
 197      return apply_filters( 'bbp_get_theme_compat_version', bbp_get_current_template_pack()->version );
 198  }
 199  
 200  /**
 201   * Gets the bbPress compatible theme used in the event the currently active
 202   * WordPress theme does not explicitly support bbPress. This can be filtered,
 203   * or set manually. Tricky theme authors can override the default and include
 204   * their own bbPress compatibility layers for their themes.
 205   *
 206   * @since 2.0.0 bbPress (r3032)
 207   *
 208   * @return string
 209   */
 210  function bbp_get_theme_compat_dir() {
 211  
 212      // Filter & return
 213      return apply_filters( 'bbp_get_theme_compat_dir', bbp_get_current_template_pack()->dir );
 214  }
 215  
 216  /**
 217   * Gets the bbPress compatible theme used in the event the currently active
 218   * WordPress theme does not explicitly support bbPress. This can be filtered,
 219   * or set manually. Tricky theme authors can override the default and include
 220   * their own bbPress compatibility layers for their themes.
 221   *
 222   * @since 2.0.0 bbPress (r3032)
 223   *
 224   * @return string
 225   */
 226  function bbp_get_theme_compat_url() {
 227  
 228      // Filter & return
 229      return apply_filters( 'bbp_get_theme_compat_url', bbp_get_current_template_pack()->url );
 230  }
 231  
 232  /**
 233   * Gets true/false if page is currently inside theme compatibility
 234   *
 235   * @since 2.0.0 bbPress (r3265)
 236   *
 237   * @return bool
 238   */
 239  function bbp_is_theme_compat_active() {
 240      $bbp = bbpress();
 241  
 242      if ( empty( $bbp->theme_compat->active ) ) {
 243          return false;
 244      }
 245  
 246      return $bbp->theme_compat->active;
 247  }
 248  
 249  /**
 250   * Sets true/false if page is currently inside theme compatibility
 251   *
 252   * @since 2.0.0 bbPress (r3265)
 253   *
 254   * @param bool $set
 255   * @return bool
 256   */
 257  function bbp_set_theme_compat_active( $set = true ) {
 258      bbpress()->theme_compat->active = $set;
 259  
 260      return (bool) bbpress()->theme_compat->active;
 261  }
 262  
 263  /**
 264   * Set the theme compat templates global
 265   *
 266   * Stash possible template files for the current query. Useful if plugins want
 267   * to override them, or see what files are being scanned for inclusion.
 268   *
 269   * @since 2.0.0 bbPress (r3311)
 270   */
 271  function bbp_set_theme_compat_templates( $templates = array() ) {
 272      bbpress()->theme_compat->templates = $templates;
 273  
 274      return bbpress()->theme_compat->templates;
 275  }
 276  
 277  /**
 278   * Set the theme compat template global
 279   *
 280   * Stash the template file for the current query. Useful if plugins want
 281   * to override it, or see what file is being included.
 282   *
 283   * @since 2.0.0 bbPress (r3311)
 284   */
 285  function bbp_set_theme_compat_template( $template = '' ) {
 286      bbpress()->theme_compat->template = $template;
 287  
 288      return bbpress()->theme_compat->template;
 289  }
 290  
 291  /**
 292   * Set the theme compat original_template global
 293   *
 294   * Stash the original template file for the current query. Useful for checking
 295   * if bbPress was able to find a more appropriate template.
 296   *
 297   * @since 2.1.0 bbPress (r3926)
 298   */
 299  function bbp_set_theme_compat_original_template( $template = '' ) {
 300      bbpress()->theme_compat->original_template = $template;
 301  
 302      return bbpress()->theme_compat->original_template;
 303  }
 304  
 305  /**
 306   * Is a template the original_template global
 307   *
 308   * Stash the original template file for the current query. Useful for checking
 309   * if bbPress was able to find a more appropriate template.
 310   *
 311   * @since 2.1.0 bbPress (r3926)
 312   */
 313  function bbp_is_theme_compat_original_template( $template = '' ) {
 314      $bbp = bbpress();
 315  
 316      // Bail if no original template
 317      if ( empty( $bbp->theme_compat->original_template ) ) {
 318          return false;
 319      }
 320  
 321      return (bool) ( $bbp->theme_compat->original_template === $template );
 322  }
 323  
 324  /**
 325   * Register a new bbPress theme package to the active theme packages array
 326   *
 327   * @since 2.1.0 bbPress (r3829)
 328   *
 329   * @param array $theme
 330   */
 331  function bbp_register_theme_package( $theme = array(), $override = true ) {
 332  
 333      // Create new BBP_Theme_Compat object from the $theme array
 334      if ( is_array( $theme ) ) {
 335          $theme = new BBP_Theme_Compat( $theme );
 336      }
 337  
 338      // Bail if $theme isn't a proper object
 339      if ( ! is_a( $theme, 'BBP_Theme_Compat' ) ) {
 340          return;
 341      }
 342  
 343      // Load up bbPress
 344      $bbp = bbpress();
 345  
 346      // Only override if the flag is set and not previously registered
 347      if ( empty( $bbp->theme_compat->packages[ $theme->id ] ) || ( true === $override ) ) {
 348          $bbp->theme_compat->packages[ $theme->id ] = $theme;
 349      }
 350  }
 351  
 352  /**
 353   * This fun little function fills up some WordPress globals with dummy data to
 354   * stop your average page template from complaining about it missing.
 355   *
 356   * @since 2.0.0 bbPress (r3108)
 357   *
 358   * @global WP_Query $wp_query
 359   * @global object $post
 360   * @param array $args
 361   */
 362  function bbp_theme_compat_reset_post( $args = array() ) {
 363      global $wp_query, $post;
 364  
 365      // Switch defaults if post is set
 366      if ( isset( $wp_query->post ) ) {
 367          $dummy = bbp_parse_args( $args, array(
 368              'ID'                    => $wp_query->post->ID,
 369              'post_status'           => $wp_query->post->post_status,
 370              'post_author'           => $wp_query->post->post_author,
 371              'post_parent'           => $wp_query->post->post_parent,
 372              'post_type'             => $wp_query->post->post_type,
 373              'post_date'             => $wp_query->post->post_date,
 374              'post_date_gmt'         => $wp_query->post->post_date_gmt,
 375              'post_modified'         => $wp_query->post->post_modified,
 376              'post_modified_gmt'     => $wp_query->post->post_modified_gmt,
 377              'post_content'          => $wp_query->post->post_content,
 378              'post_title'            => $wp_query->post->post_title,
 379              'post_excerpt'          => $wp_query->post->post_excerpt,
 380              'post_content_filtered' => $wp_query->post->post_content_filtered,
 381              'post_mime_type'        => $wp_query->post->post_mime_type,
 382              'post_password'         => $wp_query->post->post_password,
 383              'post_name'             => $wp_query->post->post_name,
 384              'guid'                  => $wp_query->post->guid,
 385              'menu_order'            => $wp_query->post->menu_order,
 386              'pinged'                => $wp_query->post->pinged,
 387              'to_ping'               => $wp_query->post->to_ping,
 388              'ping_status'           => $wp_query->post->ping_status,
 389              'comment_status'        => $wp_query->post->comment_status,
 390              'comment_count'         => $wp_query->post->comment_count,
 391              'filter'                => $wp_query->post->filter,
 392  
 393              'is_404'                => false,
 394              'is_page'               => false,
 395              'is_single'             => false,
 396              'is_archive'            => false,
 397              'is_tax'                => false
 398          ), 'theme_compat_reset_post' );
 399      } else {
 400          $dummy = bbp_parse_args( $args, array(
 401              'ID'                    => -9999,
 402              'post_status'           => bbp_get_public_status_id(),
 403              'post_author'           => 0,
 404              'post_parent'           => 0,
 405              'post_type'             => 'page',
 406              'post_date'             => 0,
 407              'post_date_gmt'         => 0,
 408              'post_modified'         => 0,
 409              'post_modified_gmt'     => 0,
 410              'post_content'          => '',
 411              'post_title'            => '',
 412              'post_excerpt'          => '',
 413              'post_content_filtered' => '',
 414              'post_mime_type'        => '',
 415              'post_password'         => '',
 416              'post_name'             => '',
 417              'guid'                  => '',
 418              'menu_order'            => 0,
 419              'pinged'                => '',
 420              'to_ping'               => '',
 421              'ping_status'           => '',
 422              'comment_status'        => 'closed',
 423              'comment_count'         => 0,
 424              'filter'                => 'raw',
 425  
 426              'is_404'                => false,
 427              'is_page'               => false,
 428              'is_single'             => false,
 429              'is_archive'            => false,
 430              'is_tax'                => false
 431          ), 'theme_compat_reset_post' );
 432      }
 433  
 434      // Bail if dummy post is empty
 435      if ( empty( $dummy ) ) {
 436          return;
 437      }
 438  
 439      // Set the $post global
 440      $post = new WP_Post( (object) $dummy );
 441  
 442      // Copy the new post global into the main $wp_query
 443      $wp_query->post       = $post;
 444      $wp_query->posts      = array( $post );
 445  
 446      // Prevent comments form from appearing
 447      $wp_query->post_count = 1;
 448      $wp_query->is_404     = $dummy['is_404'];
 449      $wp_query->is_page    = $dummy['is_page'];
 450      $wp_query->is_single  = $dummy['is_single'];
 451      $wp_query->is_archive = $dummy['is_archive'];
 452      $wp_query->is_tax     = $dummy['is_tax'];
 453  
 454      // Reset is_singular based on page/single args
 455      // https://bbpress.trac.wordpress.org/ticket/2545
 456      $wp_query->is_singular = $wp_query->is_single;
 457  
 458      // Clean up the dummy post
 459      unset( $dummy );
 460  
 461      // If we are resetting a post, we are in theme compat
 462      bbp_set_theme_compat_active( true );
 463  }
 464  
 465  /**
 466   * Reset main query vars and filter 'the_content' to output a bbPress
 467   * template part as needed.
 468   *
 469   * @since 2.0.0 bbPress (r3032)
 470   *
 471   * @param string $template
 472   */
 473  function bbp_template_include_theme_compat( $template = '' ) {
 474  
 475      /**
 476       * Bail if a root template was already found. This prevents unintended
 477       * recursive filtering of 'the_content'.
 478       *
 479       * @link https://bbpress.trac.wordpress.org/ticket/2429
 480       */
 481      if ( bbp_is_template_included() ) {
 482          return $template;
 483      }
 484  
 485      /**
 486       * If BuddyPress is activated at a network level, the action order is
 487       * reversed, which causes the template integration to fail. If we're looking
 488       * at a BuddyPress page here, bail to prevent the extra processing.
 489       *
 490       * This is a bit more brute-force than is probably necessary, but gets the
 491       * job done while we work towards something more elegant.
 492       */
 493      if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
 494          return $template;
 495      }
 496  
 497      // Define local variable(s)
 498      $bbp_shortcodes = bbpress()->shortcodes;
 499  
 500      // Bail if shortcodes are unset somehow
 501      if ( ! is_a( $bbp_shortcodes, 'BBP_Shortcodes' ) ) {
 502          return $template;
 503      }
 504  
 505      /** Users *************************************************************/
 506  
 507      if ( bbp_is_single_user_edit() || bbp_is_single_user() ) {
 508  
 509          // Reset post
 510          bbp_theme_compat_reset_post( array(
 511              'ID'             => 0,
 512              'post_author'    => 0,
 513              'post_date'      => 0,
 514              'post_content'   => bbp_buffer_template_part( 'content', 'single-user', false ),
 515              'post_type'      => '',
 516              'post_title'     => bbp_get_displayed_user_field( 'display_name' ),
 517              'post_status'    => bbp_get_public_status_id(),
 518              'is_archive'     => false,
 519              'comment_status' => 'closed'
 520          ) );
 521  
 522      /** Forums ************************************************************/
 523  
 524      // Forum archive
 525      } elseif ( bbp_is_forum_archive() ) {
 526  
 527          // Page exists where this archive should be
 528          $page = bbp_get_page_by_path( bbp_get_root_slug() );
 529  
 530          // Should we replace the content...
 531          if ( empty( $page->post_content ) ) {
 532  
 533              // Use the topics archive
 534              if ( 'topics' === bbp_show_on_root() ) {
 535                  $new_content = $bbp_shortcodes->display_topic_index();
 536  
 537              // No page so show the archive
 538              } else {
 539                  $new_content = $bbp_shortcodes->display_forum_index();
 540              }
 541  
 542          // ...or use the existing page content?
 543          } else {
 544              $new_content = apply_filters( 'the_content', $page->post_content );
 545          }
 546  
 547          // Should we replace the title...
 548          if ( empty( $page->post_title ) ) {
 549  
 550              // Use the topics archive
 551              if ( 'topics' === bbp_show_on_root() ) {
 552                  $new_title = bbp_get_topic_archive_title();
 553  
 554              // No page so show the archive
 555              } else {
 556                  $new_title = bbp_get_forum_archive_title();
 557              }
 558  
 559          // ...or use the existing page title?
 560          } else {
 561              $new_title = apply_filters( 'the_title',   $page->post_title   );
 562          }
 563  
 564          // Reset post
 565          bbp_theme_compat_reset_post( array(
 566              'ID'             => ! empty( $page->ID ) ? $page->ID : 0,
 567              'post_title'     => $new_title,
 568              'post_author'    => 0,
 569              'post_date'      => 0,
 570              'post_content'   => $new_content,
 571              'post_type'      => bbp_get_forum_post_type(),
 572              'post_status'    => bbp_get_public_status_id(),
 573              'is_archive'     => true,
 574              'comment_status' => 'closed'
 575          ) );
 576  
 577      // Single Forum
 578      } elseif ( bbp_is_forum_edit() ) {
 579  
 580          // Reset post
 581          bbp_theme_compat_reset_post( array(
 582              'ID'             => bbp_get_forum_id(),
 583              'post_title'     => bbp_get_forum_title(),
 584              'post_author'    => bbp_get_forum_author_id(),
 585              'post_date'      => 0,
 586              'post_content'   => $bbp_shortcodes->display_forum_form(),
 587              'post_type'      => bbp_get_forum_post_type(),
 588              'post_status'    => bbp_get_forum_visibility(),
 589              'is_single'      => true,
 590              'comment_status' => 'closed'
 591          ) );
 592  
 593          // Lock the forum from other edits
 594          bbp_set_post_lock( bbp_get_forum_id() );
 595  
 596      } elseif ( bbp_is_single_forum() ) {
 597  
 598          // Reset post
 599          bbp_theme_compat_reset_post( array(
 600              'ID'             => bbp_get_forum_id(),
 601              'post_title'     => bbp_get_forum_title(),
 602              'post_author'    => bbp_get_forum_author_id(),
 603              'post_date'      => 0,
 604              'post_content'   => $bbp_shortcodes->display_forum( array( 'id' => bbp_get_forum_id() ) ),
 605              'post_type'      => bbp_get_forum_post_type(),
 606              'post_status'    => bbp_get_forum_visibility(),
 607              'is_single'      => true,
 608              'comment_status' => 'closed'
 609          ) );
 610  
 611      /** Topics ************************************************************/
 612  
 613      // Topic archive
 614      } elseif ( bbp_is_topic_archive() ) {
 615  
 616          // Page exists where this archive should be
 617          $page = bbp_get_page_by_path( bbp_get_topic_archive_slug() );
 618  
 619          // Should we replace the content...
 620          if ( empty( $page->post_content ) ) {
 621              $new_content = $bbp_shortcodes->display_topic_index();
 622  
 623          // ...or use the existing page content?
 624          } else {
 625              $new_content = apply_filters( 'the_content', $page->post_content );
 626          }
 627  
 628          // Should we replace the title...
 629          if ( empty( $page->post_title ) ) {
 630              $new_title = bbp_get_topic_archive_title();
 631  
 632          // ...or use the existing page title?
 633          } else {
 634              $new_title = apply_filters( 'the_title',   $page->post_title   );
 635          }
 636  
 637          // Reset post
 638          bbp_theme_compat_reset_post( array(
 639              'ID'             => ! empty( $page->ID ) ? $page->ID : 0,
 640              'post_title'     => bbp_get_topic_archive_title(),
 641              'post_author'    => 0,
 642              'post_date'      => 0,
 643              'post_content'   => $new_content,
 644              'post_type'      => bbp_get_topic_post_type(),
 645              'post_status'    => bbp_get_public_status_id(),
 646              'is_archive'     => true,
 647              'comment_status' => 'closed'
 648          ) );
 649  
 650      // Single Topic
 651      } elseif ( bbp_is_topic_edit() || bbp_is_single_topic() ) {
 652  
 653          // Split
 654          if ( bbp_is_topic_split() ) {
 655              $new_content = bbp_buffer_template_part( 'form', 'topic-split', false );
 656  
 657          // Merge
 658          } elseif ( bbp_is_topic_merge() ) {
 659              $new_content = bbp_buffer_template_part( 'form', 'topic-merge', false );
 660  
 661          // Edit
 662          } elseif ( bbp_is_topic_edit() ) {
 663              $new_content = $bbp_shortcodes->display_topic_form();
 664  
 665              // Lock the topic from other edits
 666              bbp_set_post_lock( bbp_get_topic_id() );
 667  
 668          // Single
 669          } else {
 670              $new_content = $bbp_shortcodes->display_topic( array( 'id' => bbp_get_topic_id() ) );
 671          }
 672  
 673          // Reset post
 674          bbp_theme_compat_reset_post( array(
 675              'ID'             => bbp_get_topic_id(),
 676              'post_title'     => bbp_get_topic_title(),
 677              'post_author'    => bbp_get_topic_author_id(),
 678              'post_date'      => 0,
 679              'post_content'   => $new_content,
 680              'post_type'      => bbp_get_topic_post_type(),
 681              'post_status'    => bbp_get_topic_status(),
 682              'is_single'      => true,
 683              'comment_status' => 'closed'
 684          ) );
 685  
 686      /** Replies ***********************************************************/
 687  
 688      // Reply archive
 689      } elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) {
 690  
 691          // Reset post
 692          bbp_theme_compat_reset_post( array(
 693              'ID'             => 0,
 694              'post_title'     => esc_html__( 'Replies', 'bbpress' ),
 695              'post_author'    => 0,
 696              'post_date'      => 0,
 697              'post_content'   => $bbp_shortcodes->display_reply_index(),
 698              'post_type'      => bbp_get_reply_post_type(),
 699              'post_status'    => bbp_get_public_status_id(),
 700              'is_archive'     => true,
 701              'comment_status' => 'closed'
 702          ) );
 703  
 704      // Single Reply
 705      } elseif ( bbp_is_reply_edit() || bbp_is_single_reply() ) {
 706  
 707          // Move
 708          if ( bbp_is_reply_move() ) {
 709              $new_content = bbp_buffer_template_part( 'form', 'reply-move', false );
 710  
 711          // Edit
 712          } elseif ( bbp_is_reply_edit() ) {
 713              $new_content = $bbp_shortcodes->display_reply_form();
 714  
 715              // Lock the reply from other edits
 716              bbp_set_post_lock( bbp_get_reply_id() );
 717  
 718          // Single
 719          } else {
 720              $new_content = $bbp_shortcodes->display_reply( array( 'id' => get_the_ID() ) );
 721          }
 722  
 723          // Reset post
 724          bbp_theme_compat_reset_post( array(
 725              'ID'             => bbp_get_reply_id(),
 726              'post_title'     => bbp_get_reply_title(),
 727              'post_author'    => bbp_get_reply_author_id(),
 728              'post_date'      => 0,
 729              'post_content'   => $new_content,
 730              'post_type'      => bbp_get_reply_post_type(),
 731              'post_status'    => bbp_get_reply_status(),
 732              'is_single'      => true,
 733              'comment_status' => 'closed'
 734          ) );
 735  
 736      /** Views *************************************************************/
 737  
 738      } elseif ( bbp_is_single_view() ) {
 739  
 740          // Reset post
 741          bbp_theme_compat_reset_post( array(
 742              'ID'             => 0,
 743              'post_title'     => bbp_get_view_title(),
 744              'post_author'    => 0,
 745              'post_date'      => 0,
 746              'post_content'   => $bbp_shortcodes->display_view( array( 'id' => get_query_var( bbp_get_view_rewrite_id() ) ) ),
 747              'post_type'      => '',
 748              'post_status'    => bbp_get_public_status_id(),
 749              'is_archive'     => true,
 750              'comment_status' => 'closed'
 751          ) );
 752  
 753      /** Search ************************************************************/
 754  
 755      } elseif ( bbp_is_search() ) {
 756  
 757          // Reset post
 758          bbp_theme_compat_reset_post( array(
 759              'ID'             => 0,
 760              'post_title'     => bbp_get_search_title(),
 761              'post_author'    => 0,
 762              'post_date'      => 0,
 763              'post_content'   => $bbp_shortcodes->display_search( array( 'search' => get_query_var( bbp_get_search_rewrite_id() ) ) ),
 764              'post_type'      => '',
 765              'post_status'    => bbp_get_public_status_id(),
 766              'is_archive'     => true,
 767              'comment_status' => 'closed'
 768          ) );
 769  
 770      /** Topic Tags ********************************************************/
 771  
 772      // Topic Tag Edit
 773      } elseif ( bbp_is_topic_tag_edit() || bbp_is_topic_tag() ) {
 774  
 775          // Stash the current term in a new var
 776          set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) );
 777  
 778          // Show topics of tag
 779          if ( bbp_is_topic_tag() ) {
 780              $new_content = $bbp_shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) );
 781  
 782          // Edit topic tag
 783          } elseif ( bbp_is_topic_tag_edit() ) {
 784              $new_content = $bbp_shortcodes->display_topic_tag_form();
 785          }
 786  
 787          // Reset the post with our new title
 788          bbp_theme_compat_reset_post( array(
 789              'ID'             => 0,
 790              'post_author'    => 0,
 791              'post_date'      => 0,
 792              'post_content'   => $new_content,
 793              'post_type'      => '',
 794              'post_title'     => sprintf( esc_html__( 'Topic Tag: %s', 'bbpress' ), bbp_get_topic_tag_name() ),
 795              'post_status'    => bbp_get_public_status_id(),
 796              'is_tax'         => true,
 797              'is_archive'     => true,
 798              'comment_status' => 'closed'
 799          ) );
 800      }
 801  
 802      /**
 803       * Bail if the template already matches a bbPress template. This includes
 804       * archive-* and single-* WordPress post_type matches (allowing
 805       * themes to use the expected format) as well as all bbPress-specific
 806       * template files for users, topics, forums, etc...
 807       *
 808       * We do this after the above checks to prevent incorrect 404 body classes
 809       * and header statuses, as well as to set the post global as needed.
 810       *
 811       * @see https://bbpress.trac.wordpress.org/ticket/1478/
 812       */
 813      if ( bbp_is_template_included() ) {
 814          return $template;
 815  
 816      /**
 817       * If we are relying on the built-in theme compatibility API to load
 818       * the proper content, we need to intercept the_content, replace the
 819       * output, and display ours instead.
 820       *
 821       * To do this, we first remove all filters from 'the_content' and hook
 822       * our own function into it, which runs a series of checks to determine
 823       * the context, and then uses the built in shortcodes to output the
 824       * correct results from inside an output buffer.
 825       *
 826       * Uses bbp_get_theme_compat_templates() to provide fall-backs that
 827       * should be coded without superfluous mark-up and logic (prev/next
 828       * navigation, comments, date/time, etc...)
 829       *
 830       * Hook into the 'bbp_get_bbpress_template' to override the array of
 831       * possible templates, or 'bbp_bbpress_template' to override the result.
 832       */
 833      } elseif ( bbp_is_theme_compat_active() ) {
 834          bbp_remove_all_filters( 'the_content' );
 835  
 836          $template = bbp_get_theme_compat_templates();
 837      }
 838  
 839      // Filter & return
 840      return apply_filters( 'bbp_template_include_theme_compat', $template );
 841  }
 842  
 843  /** Helpers *******************************************************************/
 844  
 845  /**
 846   * Remove the canonical redirect to allow pretty pagination
 847   *
 848   * @since 2.0.0 bbPress (r2628)
 849   *
 850   * @param string $redirect_url Redirect url
 851   *
 852   * @return bool|string False if it's a topic/forum and their first page,
 853   *                      otherwise the redirect url
 854   */
 855  function bbp_redirect_canonical( $redirect_url ) {
 856  
 857      // Canonical is for the beautiful
 858      if ( bbp_use_pretty_urls() ) {
 859  
 860          // If viewing beyond page 1 of several
 861          if ( 1 < bbp_get_paged() ) {
 862  
 863              // Only on single topics...
 864              if ( bbp_is_single_topic() ) {
 865                  $redirect_url = false;
 866  
 867              // ...and single forums...
 868              } elseif ( bbp_is_single_forum() ) {
 869                  $redirect_url = false;
 870  
 871              // ...and single replies...
 872              } elseif ( bbp_is_single_reply() ) {
 873                  $redirect_url = false;
 874  
 875              // ...and any single anything else...
 876              //
 877              // @todo - Find a more accurate way to disable paged canonicals for
 878              //          paged shortcode usage within other posts.
 879              } elseif ( is_page() || is_singular() ) {
 880                  $redirect_url = false;
 881              }
 882  
 883          // If editing a topic
 884          } elseif ( bbp_is_topic_edit() ) {
 885              $redirect_url = false;
 886  
 887          // If editing a reply
 888          } elseif ( bbp_is_reply_edit() ) {
 889              $redirect_url = false;
 890          }
 891      }
 892  
 893      return $redirect_url;
 894  }
 895  
 896  /** Filters *******************************************************************/
 897  
 898  /**
 899   * Removes all filters from a WordPress filter, and stashes them in the $bbp
 900   * global in the event they need to be restored later.
 901   *
 902   * @since 2.0.0 bbPress (r3251)
 903   *
 904   * @global WP_filter $wp_filter
 905   * @global array $merged_filters
 906   * @param string $tag
 907   * @param int $priority
 908   * @return bool
 909   */
 910  function bbp_remove_all_filters( $tag, $priority = false ) {
 911      global $wp_filter, $merged_filters;
 912  
 913      $bbp = bbpress();
 914  
 915      // Filters exist
 916      if ( isset( $wp_filter[ $tag ] ) ) {
 917  
 918          // Filters exist in this priority
 919          if ( ! empty( $priority ) && isset( $wp_filter[ $tag ][ $priority ] ) ) {
 920  
 921              // Store filters in a backup
 922              $bbp->filters->wp_filter[ $tag ][ $priority ] = $wp_filter[ $tag ][ $priority ];
 923  
 924              // Unset the filters
 925              unset( $wp_filter[ $tag ][ $priority ] );
 926  
 927          // Priority is empty
 928          } else {
 929  
 930              // Store filters in a backup
 931              $bbp->filters->wp_filter[ $tag ] = $wp_filter[ $tag ];
 932  
 933              // Unset the filters
 934              unset( $wp_filter[ $tag ] );
 935          }
 936      }
 937  
 938      // Check merged filters
 939      if ( isset( $merged_filters[ $tag ] ) ) {
 940  
 941          // Store filters in a backup
 942          $bbp->filters->merged_filters[ $tag ] = $merged_filters[ $tag ];
 943  
 944          // Unset the filters
 945          unset( $merged_filters[ $tag ] );
 946      }
 947  
 948      return true;
 949  }
 950  
 951  /**
 952   * Restores filters from the $bbp global that were removed using
 953   * bbp_remove_all_filters()
 954   *
 955   * @since 2.0.0 bbPress (r3251)
 956   *
 957   * @global WP_filter $wp_filter
 958   * @global array $merged_filters
 959   * @param string $tag
 960   * @param int $priority
 961   * @return bool
 962   */
 963  function bbp_restore_all_filters( $tag, $priority = false ) {
 964      global $wp_filter, $merged_filters;
 965  
 966      $bbp = bbpress();
 967  
 968      // Filters exist
 969      if ( isset( $bbp->filters->wp_filter[ $tag ] ) ) {
 970  
 971          // Filters exist in this priority
 972          if ( ! empty( $priority ) && isset( $bbp->filters->wp_filter[ $tag ][ $priority  ] ) ) {
 973  
 974              // Store filters in a backup
 975              $wp_filter[ $tag ][ $priority ] = $bbp->filters->wp_filter[ $tag ][ $priority ];
 976  
 977              // Unset the filters
 978              unset( $bbp->filters->wp_filter[ $tag ][ $priority ] );
 979  
 980          // Priority is empty
 981          } else {
 982  
 983              // Store filters in a backup
 984              $wp_filter[ $tag ] = $bbp->filters->wp_filter[ $tag ];
 985  
 986              // Unset the filters
 987              unset( $bbp->filters->wp_filter[ $tag ] );
 988          }
 989      }
 990  
 991      // Check merged filters
 992      if ( isset( $bbp->filters->merged_filters[ $tag ] ) ) {
 993  
 994          // Store filters in a backup
 995          $merged_filters[ $tag ] = $bbp->filters->merged_filters[ $tag ];
 996  
 997          // Unset the filters
 998          unset( $bbp->filters->merged_filters[ $tag ] );
 999      }
1000  
1001      return true;
1002  }
1003  
1004  /**
1005   * Force comments_status to 'closed' for bbPress post types
1006   *
1007   * @since 2.1.0 bbPress (r3589)
1008   *
1009   * @param bool $open True if open, false if closed
1010   * @param int $post_id ID of the post to check
1011   * @return bool True if open, false if closed
1012   */
1013  function bbp_force_comment_status( $open = false, $post_id = 0 ) {
1014  
1015      // Default return value is what is passed in $open
1016      $retval = (bool) $open;
1017  
1018      // Get the post type of the post ID
1019      $post_type = get_post_type( $post_id );
1020  
1021      // Only force for bbPress post types
1022      if ( in_array( $post_type, bbp_get_post_types(), true ) ) {
1023          $retval = false;
1024      }
1025  
1026      // Filter & return
1027      return (bool) apply_filters( 'bbp_force_comment_status', $retval, $open, $post_id, $post_type );
1028  }


Generated: Thu Nov 21 01:01:27 2019 Cross-referenced by PHPXref 0.7.1