| 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 notpreviously 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_title' => '', |
| 304 |
'post_author' => 0, |
| 305 |
'post_date' => 0, |
| 306 |
'post_content' => '', |
| 307 |
'post_type' => 'page', |
| 308 |
'post_status' => bbp_get_public_status_id(), |
| 309 |
'post_name' => '', |
| 310 |
'comment_status' => 'closed', |
| 311 |
'is_404' => false, |
| 312 |
'is_page' => false, |
| 313 |
'is_single' => false, |
| 314 |
'is_archive' => false, |
| 315 |
'is_tax' => false, |
| 316 |
); |
| 317 |
|
| 318 |
// Switch defaults if post is set |
| 319 |
if ( isset( $wp_query->post ) ) { |
| 320 |
$defaults = array( |
| 321 |
'ID' => get_the_ID(), |
| 322 |
'post_title' => get_the_title(), |
| 323 |
'post_author' => get_the_author_meta('ID'), |
| 324 |
'post_date' => get_the_date(), |
| 325 |
'post_content' => get_the_content(), |
| 326 |
'post_type' => get_post_type(), |
| 327 |
'post_status' => get_post_status(), |
| 328 |
'post_name' => !empty( $wp_query->post->post_name ) ? $wp_query->post->post_name : '', |
| 329 |
'comment_status' => comments_open(), |
| 330 |
'is_404' => false, |
| 331 |
'is_page' => false, |
| 332 |
'is_single' => false, |
| 333 |
'is_archive' => false, |
| 334 |
'is_tax' => false, |
| 335 |
); |
| 336 |
} |
| 337 |
$dummy = bbp_parse_args( $args, $defaults, 'theme_compat_reset_post' ); |
| 338 |
|
| 339 |
// Clear out the post related globals |
| 340 |
unset( $wp_query->posts ); |
| 341 |
unset( $wp_query->post ); |
| 342 |
unset( $post ); |
| 343 |
|
| 344 |
// Setup the dummy post object |
| 345 |
$wp_query->post = new stdClass; |
| 346 |
$wp_query->post->ID = $dummy['ID']; |
| 347 |
$wp_query->post->post_title = $dummy['post_title']; |
| 348 |
$wp_query->post->post_author = $dummy['post_author']; |
| 349 |
$wp_query->post->post_date = $dummy['post_date']; |
| 350 |
$wp_query->post->post_content = $dummy['post_content']; |
| 351 |
$wp_query->post->post_type = $dummy['post_type']; |
| 352 |
$wp_query->post->post_status = $dummy['post_status']; |
| 353 |
$wp_query->post->post_name = $dummy['post_name']; |
| 354 |
$wp_query->post->comment_status = $dummy['comment_status']; |
| 355 |
|
| 356 |
// Set the $post global |
| 357 |
$post = $wp_query->post; |
| 358 |
|
| 359 |
// Setup the dummy post loop |
| 360 |
$wp_query->posts[0] = $wp_query->post; |
| 361 |
|
| 362 |
// Prevent comments form from appearing |
| 363 |
$wp_query->post_count = 1; |
| 364 |
$wp_query->is_404 = $dummy['is_404']; |
| 365 |
$wp_query->is_page = $dummy['is_page']; |
| 366 |
$wp_query->is_single = $dummy['is_single']; |
| 367 |
$wp_query->is_archive = $dummy['is_archive']; |
| 368 |
$wp_query->is_tax = $dummy['is_tax']; |
| 369 |
|
| 370 |
// If we are resetting a post, we are in theme compat |
| 371 |
bbp_set_theme_compat_active(); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Reset main query vars and filter 'the_content' to output a bbPress |
| 376 |
* template part as needed. |
| 377 |
* |
| 378 |
* @since bbPress (r3032) |
| 379 |
* @param string $template |
| 380 |
* @uses bbp_is_single_user() To check if page is single user |
| 381 |
* @uses bbp_get_single_user_template() To get user template |
| 382 |
* @uses bbp_is_single_user_edit() To check if page is single user edit |
| 383 |
* @uses bbp_get_single_user_edit_template() To get user edit template |
| 384 |
* @uses bbp_is_single_view() To check if page is single view |
| 385 |
* @uses bbp_get_single_view_template() To get view template |
| 386 |
* @uses bbp_is_forum_edit() To check if page is forum edit |
| 387 |
* @uses bbp_get_forum_edit_template() To get forum edit template |
| 388 |
* @uses bbp_is_topic_merge() To check if page is topic merge |
| 389 |
* @uses bbp_get_topic_merge_template() To get topic merge template |
| 390 |
* @uses bbp_is_topic_split() To check if page is topic split |
| 391 |
* @uses bbp_get_topic_split_template() To get topic split template |
| 392 |
* @uses bbp_is_topic_edit() To check if page is topic edit |
| 393 |
* @uses bbp_get_topic_edit_template() To get topic edit template |
| 394 |
* @uses bbp_is_reply_edit() To check if page is reply edit |
| 395 |
* @uses bbp_get_reply_edit_template() To get reply edit template |
| 396 |
* @uses bbp_set_theme_compat_template() To set the global theme compat template |
| 397 |
*/ |
| 398 |
function bbp_template_include_theme_compat( $template = '' ) { |
| 399 |
|
| 400 |
// Bail if the template already matches a bbPress template. This includes |
| 401 |
// archive-* and single-* WordPress post_type matches (allowing |
| 402 |
// themes to use the expected format) as well as all bbPress-specific |
| 403 |
// template files for users, topics, forums, etc... |
| 404 |
if ( !empty( bbpress()->theme_compat->bbpress_template ) ) |
| 405 |
return $template; |
| 406 |
|
| 407 |
/** Users *************************************************************/ |
| 408 |
|
| 409 |
if ( bbp_is_single_user() || bbp_is_single_user_edit() ) { |
| 410 |
|
| 411 |
// Reset post |
| 412 |
bbp_theme_compat_reset_post( array( |
| 413 |
'post_title' => esc_attr( bbp_get_displayed_user_field( 'display_name' ) ), |
| 414 |
'comment_status' => 'closed' |
| 415 |
) ); |
| 416 |
|
| 417 |
/** Forums ************************************************************/ |
| 418 |
|
| 419 |
// Single forum edit |
| 420 |
} elseif ( bbp_is_forum_edit() ) { |
| 421 |
|
| 422 |
// Reset post |
| 423 |
bbp_theme_compat_reset_post( array( |
| 424 |
'ID' => bbp_get_forum_id(), |
| 425 |
'post_title' => bbp_get_forum_title(), |
| 426 |
'post_author' => bbp_get_forum_author_id(), |
| 427 |
'post_date' => 0, |
| 428 |
'post_content' => get_post_field( 'post_content', bbp_get_forum_id() ), |
| 429 |
'post_type' => bbp_get_forum_post_type(), |
| 430 |
'post_status' => bbp_get_forum_visibility(), |
| 431 |
'is_single' => true, |
| 432 |
'comment_status' => 'closed' |
| 433 |
) ); |
| 434 |
|
| 435 |
// Forum archive |
| 436 |
} elseif ( bbp_is_forum_archive() ) { |
| 437 |
|
| 438 |
// Reset post |
| 439 |
bbp_theme_compat_reset_post( array( |
| 440 |
'ID' => 0, |
| 441 |
'post_title' => bbp_get_forum_archive_title(), |
| 442 |
'post_author' => 0, |
| 443 |
'post_date' => 0, |
| 444 |
'post_content' => '', |
| 445 |
'post_type' => bbp_get_forum_post_type(), |
| 446 |
'post_status' => bbp_get_public_status_id(), |
| 447 |
'is_archive' => true, |
| 448 |
'comment_status' => 'closed' |
| 449 |
) ); |
| 450 |
|
| 451 |
/** Topics ************************************************************/ |
| 452 |
|
| 453 |
// Topic archive |
| 454 |
} elseif ( bbp_is_topic_archive() ) { |
| 455 |
|
| 456 |
// Reset post |
| 457 |
bbp_theme_compat_reset_post( array( |
| 458 |
'ID' => 0, |
| 459 |
'post_title' => bbp_get_topic_archive_title(), |
| 460 |
'post_author' => 0, |
| 461 |
'post_date' => 0, |
| 462 |
'post_content' => '', |
| 463 |
'post_type' => bbp_get_topic_post_type(), |
| 464 |
'post_status' => bbp_get_public_status_id(), |
| 465 |
'is_archive' => true, |
| 466 |
'comment_status' => 'closed' |
| 467 |
) ); |
| 468 |
|
| 469 |
// Single topic |
| 470 |
} elseif ( bbp_is_topic_edit() || bbp_is_topic_split() || bbp_is_topic_merge() ) { |
| 471 |
|
| 472 |
// Reset post |
| 473 |
bbp_theme_compat_reset_post( array( |
| 474 |
'ID' => bbp_get_topic_id(), |
| 475 |
'post_title' => bbp_get_topic_title(), |
| 476 |
'post_author' => bbp_get_topic_author_id(), |
| 477 |
'post_date' => 0, |
| 478 |
'post_content' => get_post_field( 'post_content', bbp_get_topic_id() ), |
| 479 |
'post_type' => bbp_get_topic_post_type(), |
| 480 |
'post_status' => bbp_get_topic_status(), |
| 481 |
'is_single' => true, |
| 482 |
'comment_status' => 'closed' |
| 483 |
) ); |
| 484 |
|
| 485 |
/** Replies ***********************************************************/ |
| 486 |
|
| 487 |
// Reply archive |
| 488 |
} elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) { |
| 489 |
|
| 490 |
// Reset post |
| 491 |
bbp_theme_compat_reset_post( array( |
| 492 |
'ID' => 0, |
| 493 |
'post_title' => __( 'Replies', 'bbpress' ), |
| 494 |
'post_author' => 0, |
| 495 |
'post_date' => 0, |
| 496 |
'post_content' => '', |
| 497 |
'post_type' => bbp_get_reply_post_type(), |
| 498 |
'post_status' => bbp_get_public_status_id(), |
| 499 |
'comment_status' => 'closed' |
| 500 |
) ); |
| 501 |
|
| 502 |
// Single reply |
| 503 |
} elseif ( bbp_is_reply_edit() ) { |
| 504 |
|
| 505 |
// Reset post |
| 506 |
bbp_theme_compat_reset_post( array( |
| 507 |
'ID' => bbp_get_reply_id(), |
| 508 |
'post_title' => bbp_get_reply_title(), |
| 509 |
'post_author' => bbp_get_reply_author_id(), |
| 510 |
'post_date' => 0, |
| 511 |
'post_content' => get_post_field( 'post_content', bbp_get_reply_id() ), |
| 512 |
'post_type' => bbp_get_reply_post_type(), |
| 513 |
'post_status' => bbp_get_reply_status(), |
| 514 |
'comment_status' => 'closed' |
| 515 |
) ); |
| 516 |
|
| 517 |
/** Views *************************************************************/ |
| 518 |
|
| 519 |
} elseif ( bbp_is_single_view() ) { |
| 520 |
|
| 521 |
// Reset post |
| 522 |
bbp_theme_compat_reset_post( array( |
| 523 |
'ID' => 0, |
| 524 |
'post_title' => bbp_get_view_title(), |
| 525 |
'post_author' => 0, |
| 526 |
'post_date' => 0, |
| 527 |
'post_content' => '', |
| 528 |
'post_type' => '', |
| 529 |
'post_status' => bbp_get_public_status_id(), |
| 530 |
'comment_status' => 'closed' |
| 531 |
) ); |
| 532 |
|
| 533 |
|
| 534 |
/** Topic Tags ********************************************************/ |
| 535 |
|
| 536 |
} elseif ( bbp_is_topic_tag_edit() ) { |
| 537 |
|
| 538 |
// Stash the current term in a new var |
| 539 |
set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) ); |
| 540 |
|
| 541 |
// Reset the post with our new title |
| 542 |
bbp_theme_compat_reset_post( array( |
| 543 |
'post_title' => sprintf( __( 'Topic Tag: %s', 'bbpress' ), '<span>' . bbp_get_topic_tag_name() . '</span>' ) |
| 544 |
) ); |
| 545 |
|
| 546 |
} elseif ( bbp_is_topic_tag() ) { |
| 547 |
|
| 548 |
// Stash the current term in a new var |
| 549 |
set_query_var( 'bbp_topic_tag', get_query_var( 'term' ) ); |
| 550 |
|
| 551 |
// Reset the post with our new title |
| 552 |
bbp_theme_compat_reset_post( array( |
| 553 |
'post_title' => sprintf( __( 'Topic Tag: %s', 'bbpress' ), '<span>' . bbp_get_topic_tag_name() . '</span>' ) |
| 554 |
) ); |
| 555 |
|
| 556 |
/** Single Forums/Topics/Replies **************************************/ |
| 557 |
|
| 558 |
} elseif ( bbp_is_custom_post_type() ) { |
| 559 |
bbp_set_theme_compat_active(); |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* If we are relying on bbPress's built in theme compatibility to load |
| 564 |
* the proper content, we need to intercept the_content, replace the |
| 565 |
* output, and display ours instead. |
| 566 |
* |
| 567 |
* To do this, we first remove all filters from 'the_content' and hook |
| 568 |
* our own function into it, which runs a series of checks to determine |
| 569 |
* the context, and then uses the built in shortcodes to output the |
| 570 |
* correct results from inside an output buffer. |
| 571 |
* |
| 572 |
* Uses bbp_get_theme_compat_templates() to provide fall-backs that |
| 573 |
* should be coded without superfluous mark-up and logic (prev/next |
| 574 |
* navigation, comments, date/time, etc...) |
| 575 |
* |
| 576 |
* Hook into the 'bbp_get_bbpress_template' to override the array of |
| 577 |
* possible templates, or 'bbp_bbpress_template' to override the result. |
| 578 |
*/ |
| 579 |
if ( bbp_is_theme_compat_active() ) { |
| 580 |
|
| 581 |
// Remove all filters from the_content |
| 582 |
bbp_remove_all_filters( 'the_content' ); |
| 583 |
|
| 584 |
// Add a filter on the_content late, which we will later remove |
| 585 |
add_filter( 'the_content', 'bbp_replace_the_content' ); |
| 586 |
|
| 587 |
// Find the appropriate template file |
| 588 |
$template = bbp_get_theme_compat_templates(); |
| 589 |
} |
| 590 |
|
| 591 |
return apply_filters( 'bbp_template_include_theme_compat', $template ); |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Replaces the_content() if the post_type being displayed is one that would |
| 596 |
* normally be handled by bbPress, but proper single page templates do not |
| 597 |
* exist in the currently active theme. |
| 598 |
* |
| 599 |
* @since bbPress (r3034) |
| 600 |
* @param string $content |
| 601 |
* @return type |
| 602 |
*/ |
| 603 |
function bbp_replace_the_content( $content = '' ) { |
| 604 |
$bbp = bbpress(); |
| 605 |
|
| 606 |
// Define local variable(s) |
| 607 |
$new_content = ''; |
| 608 |
|
| 609 |
// Bail if shortcodes are unset somehow |
| 610 |
if ( !is_a( $bbp->shortcodes, 'BBP_Shortcodes' ) ) |
| 611 |
return $content; |
| 612 |
|
| 613 |
// Use shortcode API to display forums/topics/replies because they are |
| 614 |
// already output buffered and ready to fit inside the_content |
| 615 |
|
| 616 |
/** Users *************************************************************/ |
| 617 |
|
| 618 |
// Profile View |
| 619 |
if ( bbp_is_single_user() ) { |
| 620 |
ob_start(); |
| 621 |
|
| 622 |
bbp_get_template_part( 'content', 'single-user' ); |
| 623 |
|
| 624 |
$new_content = ob_get_contents(); |
| 625 |
|
| 626 |
ob_end_clean(); |
| 627 |
|
| 628 |
// Profile Edit |
| 629 |
} elseif ( bbp_is_single_user_edit() ) { |
| 630 |
ob_start(); |
| 631 |
|
| 632 |
bbp_get_template_part( 'content', 'single-user-edit' ); |
| 633 |
|
| 634 |
$new_content = ob_get_contents(); |
| 635 |
|
| 636 |
ob_end_clean(); |
| 637 |
|
| 638 |
/** Forums ************************************************************/ |
| 639 |
|
| 640 |
// Reply Edit |
| 641 |
} elseif ( bbp_is_forum_edit() ) { |
| 642 |
$new_content = $bbp->shortcodes->display_forum_form(); |
| 643 |
|
| 644 |
// Forum archive |
| 645 |
} elseif ( bbp_is_forum_archive() ) { |
| 646 |
|
| 647 |
// Page exists where this archive should be |
| 648 |
$page = bbp_get_page_by_path( bbp_get_root_slug() ); |
| 649 |
if ( !empty( $page ) ) { |
| 650 |
|
| 651 |
// Restore previously unset filters |
| 652 |
bbp_restore_all_filters( 'the_content' ); |
| 653 |
|
| 654 |
// Remove 'bbp_replace_the_content' filter to prevent infinite loops |
| 655 |
remove_filter( 'the_content', 'bbp_replace_the_content' ); |
| 656 |
|
| 657 |
// Start output buffer |
| 658 |
ob_start(); |
| 659 |
|
| 660 |
// Grab the content of this page |
| 661 |
$new_content = apply_filters( 'the_content', $page->post_content ); |
| 662 |
|
| 663 |
// Clean up the buffer |
| 664 |
ob_end_clean(); |
| 665 |
|
| 666 |
// Add 'bbp_replace_the_content' filter back (@see $this::start()) |
| 667 |
add_filter( 'the_content', 'bbp_replace_the_content' ); |
| 668 |
|
| 669 |
// No page so show the archive |
| 670 |
} else { |
| 671 |
$new_content = $bbp->shortcodes->display_forum_index(); |
| 672 |
} |
| 673 |
|
| 674 |
/** Topics ************************************************************/ |
| 675 |
|
| 676 |
// Topic archive |
| 677 |
} elseif ( bbp_is_topic_archive() ) { |
| 678 |
|
| 679 |
// Page exists where this archive should be |
| 680 |
$page = bbp_get_page_by_path( bbp_get_topic_archive_slug() ); |
| 681 |
if ( !empty( $page ) ) { |
| 682 |
|
| 683 |
// Restore previously unset filters |
| 684 |
bbp_restore_all_filters( 'the_content' ); |
| 685 |
|
| 686 |
// Remove 'bbp_replace_the_content' filter to prevent infinite loops |
| 687 |
remove_filter( 'the_content', 'bbp_replace_the_content' ); |
| 688 |
|
| 689 |
// Start output buffer |
| 690 |
ob_start(); |
| 691 |
|
| 692 |
// Grab the content of this page |
| 693 |
$new_content = apply_filters( 'the_content', $page->post_content ); |
| 694 |
|
| 695 |
// Clean up the buffer |
| 696 |
ob_end_clean(); |
| 697 |
|
| 698 |
// Add 'bbp_replace_the_content' filter back (@see $this::start()) |
| 699 |
add_filter( 'the_content', 'bbp_replace_the_content' ); |
| 700 |
|
| 701 |
// No page so show the archive |
| 702 |
} else { |
| 703 |
$new_content = $bbp->shortcodes->display_topic_index(); |
| 704 |
} |
| 705 |
|
| 706 |
// Single topic |
| 707 |
} elseif ( bbp_is_topic_edit() ) { |
| 708 |
|
| 709 |
// Split |
| 710 |
if ( bbp_is_topic_split() ) { |
| 711 |
ob_start(); |
| 712 |
|
| 713 |
bbp_get_template_part( 'form', 'topic-split' ); |
| 714 |
|
| 715 |
$new_content = ob_get_contents(); |
| 716 |
|
| 717 |
ob_end_clean(); |
| 718 |
|
| 719 |
// Merge |
| 720 |
} elseif ( bbp_is_topic_merge() ) { |
| 721 |
ob_start(); |
| 722 |
|
| 723 |
bbp_get_template_part( 'form', 'topic-merge' ); |
| 724 |
|
| 725 |
$new_content = ob_get_contents(); |
| 726 |
|
| 727 |
ob_end_clean(); |
| 728 |
|
| 729 |
// Edit |
| 730 |
} else { |
| 731 |
$new_content = $bbp->shortcodes->display_topic_form(); |
| 732 |
} |
| 733 |
|
| 734 |
/** Replies ***********************************************************/ |
| 735 |
|
| 736 |
// Reply archive |
| 737 |
} elseif ( is_post_type_archive( bbp_get_reply_post_type() ) ) { |
| 738 |
//$new_content = $bbp->shortcodes->display_reply_index(); |
| 739 |
|
| 740 |
// Reply Edit |
| 741 |
} elseif ( bbp_is_reply_edit() ) { |
| 742 |
$new_content = $bbp->shortcodes->display_reply_form(); |
| 743 |
|
| 744 |
/** Views *************************************************************/ |
| 745 |
|
| 746 |
} elseif ( bbp_is_single_view() ) { |
| 747 |
$new_content = $bbp->shortcodes->display_view( array( 'id' => get_query_var( 'bbp_view' ) ) ); |
| 748 |
|
| 749 |
/** Topic Tags ********************************************************/ |
| 750 |
|
| 751 |
} elseif ( get_query_var( 'bbp_topic_tag' ) ) { |
| 752 |
|
| 753 |
// Edit topic tag |
| 754 |
if ( bbp_is_topic_tag_edit() ) { |
| 755 |
$new_content = $bbp->shortcodes->display_topic_tag_form(); |
| 756 |
|
| 757 |
// Show topics of tag |
| 758 |
} else { |
| 759 |
$new_content = $bbp->shortcodes->display_topics_of_tag( array( 'id' => bbp_get_topic_tag_id() ) ); |
| 760 |
} |
| 761 |
|
| 762 |
/** Forums/Topics/Replies *********************************************/ |
| 763 |
|
| 764 |
} else { |
| 765 |
|
| 766 |
// Check the post_type |
| 767 |
switch ( get_post_type() ) { |
| 768 |
|
| 769 |
// Single Forum |
| 770 |
case bbp_get_forum_post_type() : |
| 771 |
$new_content = $bbp->shortcodes->display_forum( array( 'id' => get_the_ID() ) ); |
| 772 |
break; |
| 773 |
|
| 774 |
// Single Topic |
| 775 |
case bbp_get_topic_post_type() : |
| 776 |
$new_content = $bbp->shortcodes->display_topic( array( 'id' => get_the_ID() ) ); |
| 777 |
break; |
| 778 |
|
| 779 |
// Single Reply |
| 780 |
case bbp_get_reply_post_type() : |
| 781 |
$new_content = $bbp->shortcodes->display_reply( array( 'id' => get_the_ID() ) ); |
| 782 |
break; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
// Juggle the content around and try to prevent unsightly comments |
| 787 |
if ( !empty( $new_content ) && ( $new_content != $content ) ) { |
| 788 |
|
| 789 |
// Set the content to be the new content |
| 790 |
$content = apply_filters( 'bbp_replace_the_content', $new_content, $content ); |
| 791 |
|
| 792 |
// Clean up after ourselves |
| 793 |
unset( $new_content ); |
| 794 |
|
| 795 |
/** |
| 796 |
* Supplemental hack to prevent stubborn comments_template() output. |
| 797 |
* |
| 798 |
* @see comments_template() For why we're doing this :) |
| 799 |
* |
| 800 |
* Note: If a theme uses custom code to output comments, it's |
| 801 |
* possible all of this dancing around is for not. |
| 802 |
* |
| 803 |
* Note: If you need to keep these globals around for any special |
| 804 |
* reason, we've provided a failsafe hook to bypass this you |
| 805 |
* can put in your plugin or theme below ---v |
| 806 |
* |
| 807 |
* apply_filters( 'bbp_spill_the_beans', '__return_true' ); |
| 808 |
*/ |
| 809 |
if ( !apply_filters( 'bbp_spill_the_beans', false ) ) { |
| 810 |
|
| 811 |
// Empty globals that aren't being used in this loop anymore |
| 812 |
$GLOBALS['withcomments'] = false; |
| 813 |
$GLOBALS['post'] = false; |
| 814 |
|
| 815 |
// Reset the post data when the next sidebar is fired |
| 816 |
add_action( 'get_sidebar', 'bbp_theme_compat_reset_post_data' ); |
| 817 |
add_action( 'get_footer', 'bbp_theme_compat_reset_post_data' ); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
// Return possibly hi-jacked content |
| 822 |
return $content; |
| 823 |
} |
| 824 |
|
| 825 |
/** |
| 826 |
* Resets the post data after the content has displayed |
| 827 |
* |
| 828 |
* @since bbPress (r3724) |
| 829 |
* @uses wp_reset_postdata() To reset the post data |
| 830 |
* @uses remove_action() To unhook itself so it does not fire more than once |
| 831 |
*/ |
| 832 |
function bbp_theme_compat_reset_post_data() { |
| 833 |
static $ran = false; |
| 834 |
|
| 835 |
// Bail if this already ran |
| 836 |
if ( true === $ran ) |
| 837 |
return; |
| 838 |
|
| 839 |
// Reset the post data to whatever our global post is |
| 840 |
wp_reset_postdata(); |
| 841 |
|
| 842 |
// Prevent this from firing again |
| 843 |
remove_action( 'get_sidebar', 'bbp_theme_compat_reset_post_data' ); |
| 844 |
remove_action( 'get_footer', 'bbp_theme_compat_reset_post_data' ); |
| 845 |
|
| 846 |
// Set this to true so it does not run again |
| 847 |
$ran = true; |
| 848 |
} |
| 849 |
|
| 850 |
/** Helpers *******************************************************************/ |
| 851 |
|
| 852 |
/** |
| 853 |
* Remove the canonical redirect to allow pretty pagination |
| 854 |
* |
| 855 |
* @since bbPress (r2628) |
| 856 |
* @param string $redirect_url Redirect url |
| 857 |
* @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks |
| 858 |
* @uses bbp_get_paged() To get the current page number |
| 859 |
* @uses bbp_is_single_topic() To check if it's a topic page |
| 860 |
* @uses bbp_is_single_forum() To check if it's a forum page |
| 861 |
* @return bool|string False if it's a topic/forum and their first page, |
| 862 |
* otherwise the redirect url |
| 863 |
*/ |
| 864 |
function bbp_redirect_canonical( $redirect_url ) { |
| 865 |
global $wp_rewrite; |
| 866 |
|
| 867 |
// Canonical is for the beautiful |
| 868 |
if ( $wp_rewrite->using_permalinks() ) { |
| 869 |
|
| 870 |
// If viewing beyond page 1 of several |
| 871 |
if ( 1 < bbp_get_paged() ) { |
| 872 |
|
| 873 |
// Only on single topics... |
| 874 |
if ( bbp_is_single_topic() ) { |
| 875 |
$redirect_url = false; |
| 876 |
|
| 877 |
// ...and single forums... |
| 878 |
} elseif ( bbp_is_single_forum() ) { |
| 879 |
$redirect_url = false; |
| 880 |
|
| 881 |
// ...and single replies... |
| 882 |
} elseif ( bbp_is_single_reply() ) { |
| 883 |
$redirect_url = false; |
| 884 |
|
| 885 |
// ...and any single anything else... |
| 886 |
// |
| 887 |
// @todo - Find a more accurate way to disable paged canonicals for |
| 888 |
// paged shortcode usage within other posts. |
| 889 |
} elseif ( is_page() || is_singular() ) { |
| 890 |
$redirect_url = false; |
| 891 |
} |
| 892 |
|
| 893 |
// If editing a topic |
| 894 |
} elseif ( bbp_is_topic_edit() ) { |
| 895 |
$redirect_url = false; |
| 896 |
|
| 897 |
// If editing a reply |
| 898 |
} elseif ( bbp_is_reply_edit() ) { |
| 899 |
$redirect_url = false; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
return $redirect_url; |
| 904 |
} |
| 905 |
|
| 906 |
/** Filters *******************************************************************/ |
| 907 |
|
| 908 |
/** |
| 909 |
* Removes all filters from a WordPress filter, and stashes them in the $bbp |
| 910 |
* global in the event they need to be restored later. |
| 911 |
* |
| 912 |
* @since bbPress (r3251) |
| 913 |
* @global WP_filter $wp_filter |
| 914 |
* @global array $merged_filters |
| 915 |
* @param string $tag |
| 916 |
* @param int $priority |
| 917 |
* @return bool |
| 918 |
*/ |
| 919 |
function bbp_remove_all_filters( $tag, $priority = false ) { |
| 920 |
global $wp_filter, $merged_filters; |
| 921 |
|
| 922 |
$bbp = bbpress(); |
| 923 |
|
| 924 |
// Filters exist |
| 925 |
if ( isset( $wp_filter[$tag] ) ) { |
| 926 |
|
| 927 |
// Filters exist in this priority |
| 928 |
if ( !empty( $priority ) && isset( $wp_filter[$tag][$priority] ) ) { |
| 929 |
|
| 930 |
// Store filters in a backup |
| 931 |
$bbp->filters->wp_filter[$tag][$priority] = $wp_filter[$tag][$priority]; |
| 932 |
|
| 933 |
// Unset the filters |
| 934 |
unset( $wp_filter[$tag][$priority] ); |
| 935 |
|
| 936 |
// Priority is empty |
| 937 |
} else { |
| 938 |
|
| 939 |
// Store filters in a backup |
| 940 |
$bbp->filters->wp_filter[$tag] = $wp_filter[$tag]; |
| 941 |
|
| 942 |
// Unset the filters |
| 943 |
unset( $wp_filter[$tag] ); |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
// Check merged filters |
| 948 |
if ( isset( $merged_filters[$tag] ) ) { |
| 949 |
|
| 950 |
// Store filters in a backup |
| 951 |
$bbp->filters->merged_filters[$tag] = $merged_filters[$tag]; |
| 952 |
|
| 953 |
// Unset the filters |
| 954 |
unset( $merged_filters[$tag] ); |
| 955 |
} |
| 956 |
|
| 957 |
return true; |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
* Restores filters from the $bbp global that were removed using |
| 962 |
* bbp_remove_all_filters() |
| 963 |
* |
| 964 |
* @since bbPress (r3251) |
| 965 |
* @global WP_filter $wp_filter |
| 966 |
* @global array $merged_filters |
| 967 |
* @param string $tag |
| 968 |
* @param int $priority |
| 969 |
* @return bool |
| 970 |
*/ |
| 971 |
function bbp_restore_all_filters( $tag, $priority = false ) { |
| 972 |
global $wp_filter, $merged_filters; |
| 973 |
|
| 974 |
$bbp = bbpress(); |
| 975 |
|
| 976 |
// Filters exist |
| 977 |
if ( isset( $bbp->filters->wp_filter[$tag] ) ) { |
| 978 |
|
| 979 |
// Filters exist in this priority |
| 980 |
if ( !empty( $priority ) && isset( $bbp->filters->wp_filter[$tag][$priority] ) ) { |
| 981 |
|
| 982 |
// Store filters in a backup |
| 983 |
$wp_filter[$tag][$priority] = $bbp->filters->wp_filter[$tag][$priority]; |
| 984 |
|
| 985 |
// Unset the filters |
| 986 |
unset( $bbp->filters->wp_filter[$tag][$priority] ); |
| 987 |
|
| 988 |
// Priority is empty |
| 989 |
} else { |
| 990 |
|
| 991 |
// Store filters in a backup |
| 992 |
$wp_filter[$tag] = $bbp->filters->wp_filter[$tag]; |
| 993 |
|
| 994 |
// Unset the filters |
| 995 |
unset( $bbp->filters->wp_filter[$tag] ); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
// Check merged filters |
| 1000 |
if ( isset( $bbp->filters->merged_filters[$tag] ) ) { |
| 1001 |
|
| 1002 |
// Store filters in a backup |
| 1003 |
$merged_filters[$tag] = $bbp->filters->merged_filters[$tag]; |
| 1004 |
|
| 1005 |
// Unset the filters |
| 1006 |
unset( $bbp->filters->merged_filters[$tag] ); |
| 1007 |
} |
| 1008 |
|
| 1009 |
return true; |
| 1010 |
} |
| 1011 |
|
| 1012 |
/** |
| 1013 |
* Force comments_status to 'closed' for bbPress post types |
| 1014 |
* |
| 1015 |
* @since bbPress (r3589) |
| 1016 |
* @param bool $open True if open, false if closed |
| 1017 |
* @param int $post_id ID of the post to check |
| 1018 |
* @return bool True if open, false if closed |
| 1019 |
*/ |
| 1020 |
function bbp_force_comment_status( $open, $post_id = 0 ) { |
| 1021 |
|
| 1022 |
// Get the post type of the post ID |
| 1023 |
$post_type = get_post_type( $post_id ); |
| 1024 |
|
| 1025 |
// Default return value is what is passed in $open |
| 1026 |
$retval = $open; |
| 1027 |
|
| 1028 |
// Only force for bbPress post types |
| 1029 |
switch ( $post_type ) { |
| 1030 |
case bbp_get_forum_post_type() : |
| 1031 |
case bbp_get_topic_post_type() : |
| 1032 |
case bbp_get_reply_post_type() : |
| 1033 |
$retval = false; |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
|
| 1037 |
// Allow override of the override |
| 1038 |
return apply_filters( 'bbp_force_comment_status', $retval, $open, $post_id, $post_type ); |
| 1039 |
} |
| 1040 |
|