PageRenderTime 123ms CodeModel.GetById 28ms RepoModel.GetById 4ms app.codeStats 0ms

/wp-content/plugins/bbpress/includes/core/theme-compat.php

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