/includes/core/theme-compat.php

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