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