/includes/core/template-loader.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Template Loader 
  5. * 
  6. * @package bbPress 
  7. * @subpackage TemplateLoader 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** 
  14. * Possibly intercept the template being loaded 
  15. * 
  16. * Listens to the 'template_include' filter and waits for any bbPress specific 
  17. * template condition to be met. If one is met and the template file exists,  
  18. * it will be used; otherwise  
  19. * 
  20. * Note that the _edit() checks are ahead of their counterparts, to prevent them 
  21. * from being stomped on accident. 
  22. * 
  23. * @since bbPress (r3032) 
  24. * 
  25. * @param string $template 
  26. * 
  27. * @uses bbp_is_single_user() To check if page is single user 
  28. * @uses bbp_get_single_user_template() To get user template 
  29. * @uses bbp_is_single_user_edit() To check if page is single user edit 
  30. * @uses bbp_get_single_user_edit_template() To get user edit template 
  31. * @uses bbp_is_single_view() To check if page is single view 
  32. * @uses bbp_get_single_view_template() To get view template 
  33. * @uses bbp_is_search() To check if page is search 
  34. * @uses bbp_get_search_template() To get search template 
  35. * @uses bbp_is_forum_edit() To check if page is forum edit 
  36. * @uses bbp_get_forum_edit_template() To get forum edit template 
  37. * @uses bbp_is_topic_merge() To check if page is topic merge 
  38. * @uses bbp_get_topic_merge_template() To get topic merge template 
  39. * @uses bbp_is_topic_split() To check if page is topic split 
  40. * @uses bbp_get_topic_split_template() To get topic split template 
  41. * @uses bbp_is_topic_edit() To check if page is topic edit 
  42. * @uses bbp_get_topic_edit_template() To get topic edit template 
  43. * @uses bbp_is_reply_move() To check if page is reply move 
  44. * @uses bbp_get_reply_move_template() To get reply move template 
  45. * @uses bbp_is_reply_edit() To check if page is reply edit 
  46. * @uses bbp_get_reply_edit_template() To get reply edit template 
  47. * @uses bbp_set_theme_compat_template() To set the global theme compat template 
  48. * 
  49. * @return string The path to the template file that is being used 
  50. */ 
  51. function bbp_template_include_theme_supports( $template = '' ) { 
  52.  
  53. // Editing a user 
  54. if ( bbp_is_single_user_edit() && ( $new_template = bbp_get_single_user_edit_template() ) ) : 
  55.  
  56. // User favorites 
  57. elseif ( bbp_is_favorites() && ( $new_template = bbp_get_favorites_template() ) ) : 
  58.  
  59. // User favorites 
  60. elseif ( bbp_is_subscriptions() && ( $new_template = bbp_get_subscriptions_template() ) ) : 
  61.  
  62. // Viewing a user 
  63. elseif ( bbp_is_single_user() && ( $new_template = bbp_get_single_user_template() ) ) : 
  64.  
  65. // Single View 
  66. elseif ( bbp_is_single_view() && ( $new_template = bbp_get_single_view_template() ) ) : 
  67.  
  68. // Search 
  69. elseif ( bbp_is_search() && ( $new_template = bbp_get_search_template() ) ) : 
  70.  
  71. // Forum edit 
  72. elseif ( bbp_is_forum_edit() && ( $new_template = bbp_get_forum_edit_template() ) ) : 
  73.  
  74. // Single Forum 
  75. elseif ( bbp_is_single_forum() && ( $new_template = bbp_get_single_forum_template() ) ) : 
  76.  
  77. // Forum Archive 
  78. elseif ( bbp_is_forum_archive() && ( $new_template = bbp_get_forum_archive_template() ) ) : 
  79.  
  80. // Topic merge 
  81. elseif ( bbp_is_topic_merge() && ( $new_template = bbp_get_topic_merge_template() ) ) : 
  82.  
  83. // Topic split 
  84. elseif ( bbp_is_topic_split() && ( $new_template = bbp_get_topic_split_template() ) ) : 
  85.  
  86. // Topic edit 
  87. elseif ( bbp_is_topic_edit() && ( $new_template = bbp_get_topic_edit_template() ) ) : 
  88.  
  89. // Single Topic 
  90. elseif ( bbp_is_single_topic() && ( $new_template = bbp_get_single_topic_template() ) ) : 
  91.  
  92. // Topic Archive 
  93. elseif ( bbp_is_topic_archive() && ( $new_template = bbp_get_topic_archive_template() ) ) : 
  94.  
  95. // Reply move 
  96. elseif ( bbp_is_reply_move() && ( $new_template = bbp_get_reply_move_template() ) ) : 
  97.  
  98. // Editing a reply 
  99. elseif ( bbp_is_reply_edit() && ( $new_template = bbp_get_reply_edit_template() ) ) : 
  100.  
  101. // Single Reply 
  102. elseif ( bbp_is_single_reply() && ( $new_template = bbp_get_single_reply_template() ) ) : 
  103.  
  104. // Editing a topic tag 
  105. elseif ( bbp_is_topic_tag_edit() && ( $new_template = bbp_get_topic_tag_edit_template() ) ) : 
  106.  
  107. // Viewing a topic tag 
  108. elseif ( bbp_is_topic_tag() && ( $new_template = bbp_get_topic_tag_template() ) ) : 
  109. endif; 
  110.  
  111. // A bbPress template file was located, so override the WordPress template 
  112. // and use it to switch off bbPress's theme compatibility. 
  113. if ( !empty( $new_template ) ) { 
  114. $template = bbp_set_template_included( $new_template ); 
  115.  
  116. return apply_filters( 'bbp_template_include_theme_supports', $template ); 
  117.  
  118. /** 
  119. * Set the included template 
  120. * 
  121. * @since bbPress (r4975) 
  122. * @param mixed $template Default false 
  123. * @return mixed False if empty. Template name if template included 
  124. */ 
  125. function bbp_set_template_included( $template = false ) { 
  126. bbpress()->theme_compat->bbpress_template = $template; 
  127.  
  128. return bbpress()->theme_compat->bbpress_template; 
  129.  
  130. /** 
  131. * Is a bbPress template being included? 
  132. * 
  133. * @since bbPress (r4975) 
  134. * @return bool True if yes, false if no 
  135. */ 
  136. function bbp_is_template_included() { 
  137. return ! empty( bbpress()->theme_compat->bbpress_template ); 
  138.  
  139. /** Custom Functions **********************************************************/ 
  140.  
  141. /** 
  142. * Attempt to load a custom bbPress functions file, similar to each themes 
  143. * functions.php file. 
  144. * 
  145. * @since bbPress (r3732) 
  146. * 
  147. * @global string $pagenow 
  148. * @uses bbp_locate_template() 
  149. */ 
  150. function bbp_load_theme_functions() { 
  151. global $pagenow; 
  152.  
  153. // If bbPress is being deactivated, do not load any more files 
  154. if ( bbp_is_deactivation() ) 
  155. return; 
  156.  
  157. if ( ! defined( 'WP_INSTALLING' ) || ( !empty( $pagenow ) && ( 'wp-activate.php' !== $pagenow ) ) ) { 
  158. bbp_locate_template( 'bbpress-functions.php', true ); 
  159.  
  160. /** Individual Templates ******************************************************/ 
  161.  
  162. /** 
  163. * Get the user profile template 
  164. * 
  165. * @since bbPress (r3311) 
  166. * 
  167. * @uses bbp_get_displayed_user_id() 
  168. * @uses bbp_get_query_template() 
  169. * @return string Path to template file 
  170. */ 
  171. function bbp_get_single_user_template() { 
  172. $nicename = bbp_get_displayed_user_field( 'user_nicename' ); 
  173. $user_id = bbp_get_displayed_user_id(); 
  174. $templates = array( 
  175. 'single-user-' . $nicename . '.php', // Single User nicename 
  176. 'single-user-' . $user_id . '.php', // Single User ID 
  177. 'single-user.php', // Single User 
  178. 'user.php', // User 
  179. ); 
  180. return bbp_get_query_template( 'profile', $templates ); 
  181.  
  182. /** 
  183. * Get the user profile edit template 
  184. * 
  185. * @since bbPress (r3311) 
  186. * 
  187. * @uses bbp_get_displayed_user_id() 
  188. * @uses bbp_get_query_template() 
  189. * @return string Path to template file 
  190. */ 
  191. function bbp_get_single_user_edit_template() { 
  192. $nicename = bbp_get_displayed_user_field( 'user_nicename' ); 
  193. $user_id = bbp_get_displayed_user_id(); 
  194. $templates = array( 
  195. 'single-user-edit-' . $nicename . '.php', // Single User Edit nicename 
  196. 'single-user-edit-' . $user_id . '.php', // Single User Edit ID 
  197. 'single-user-edit.php', // Single User Edit 
  198. 'user-edit.php', // User Edit 
  199. 'user.php', // User 
  200. ); 
  201. return bbp_get_query_template( 'profile_edit', $templates ); 
  202.  
  203. /** 
  204. * Get the user favorites template 
  205. * 
  206. * @since bbPress (r4225) 
  207. * 
  208. * @uses bbp_get_displayed_user_id() 
  209. * @uses bbp_get_query_template() 
  210. * @return string Path to template file 
  211. */ 
  212. function bbp_get_favorites_template() { 
  213. $nicename = bbp_get_displayed_user_field( 'user_nicename' ); 
  214. $user_id = bbp_get_displayed_user_id(); 
  215. $templates = array( 
  216. 'single-user-favorites-' . $nicename . '.php', // Single User Favs nicename 
  217. 'single-user-favorites-' . $user_id . '.php', // Single User Favs ID 
  218. 'favorites-' . $nicename . '.php', // Favorites nicename 
  219. 'favorites-' . $user_id . '.php', // Favorites ID 
  220. 'favorites.php', // Favorites 
  221. 'user.php', // User 
  222. ); 
  223. return bbp_get_query_template( 'favorites', $templates ); 
  224.  
  225. /** 
  226. * Get the user subscriptions template 
  227. * 
  228. * @since bbPress (r4225) 
  229. * 
  230. * @uses bbp_get_displayed_user_id() 
  231. * @uses bbp_get_query_template() 
  232. * @return string Path to template file 
  233. */ 
  234. function bbp_get_subscriptions_template() { 
  235. $nicename = bbp_get_displayed_user_field( 'user_nicename' ); 
  236. $user_id = bbp_get_displayed_user_id(); 
  237. $templates = array( 
  238. 'single-user-subscriptions-' . $nicename . '.php', // Single User Subs nicename 
  239. 'single-user-subscriptions-' . $user_id . '.php', // Single User Subs ID 
  240. 'subscriptions-' . $nicename . '.php', // Subscriptions nicename 
  241. 'subscriptions-' . $user_id . '.php', // Subscriptions ID 
  242. 'subscriptions.php', // Subscriptions 
  243. 'user.php', // User 
  244. ); 
  245. return bbp_get_query_template( 'subscriptions', $templates ); 
  246.  
  247. /** 
  248. * Get the view template 
  249. * 
  250. * @since bbPress (r3311) 
  251. * 
  252. * @uses bbp_get_view_id() 
  253. * @uses bbp_get_query_template() 
  254. * @return string Path to template file 
  255. */ 
  256. function bbp_get_single_view_template() { 
  257. $view_id = bbp_get_view_id(); 
  258. $templates = array( 
  259. 'single-view-' . $view_id . '.php', // Single View ID 
  260. 'view-' . $view_id . '.php', // View ID 
  261. 'single-view.php', // Single View 
  262. 'view.php', // View 
  263. ); 
  264. return bbp_get_query_template( 'single_view', $templates ); 
  265.  
  266. /** 
  267. * Get the search template 
  268. * 
  269. * @since bbPress (r4579) 
  270. * 
  271. * @uses bbp_get_query_template() 
  272. * @return string Path to template file 
  273. */ 
  274. function bbp_get_search_template() { 
  275. $templates = array( 
  276. 'page-forum-search.php', // Single Search 
  277. 'forum-search.php', // Search 
  278. ); 
  279. return bbp_get_query_template( 'single_search', $templates ); 
  280.  
  281. /** 
  282. * Get the single forum template 
  283. * 
  284. * @since bbPress (r3922) 
  285. * 
  286. * @uses bbp_get_forum_post_type() 
  287. * @uses bbp_get_query_template() 
  288. * @return string Path to template file 
  289. */ 
  290. function bbp_get_single_forum_template() { 
  291. $templates = array( 
  292. 'single-' . bbp_get_forum_post_type() . '.php' // Single Forum 
  293. ); 
  294. return bbp_get_query_template( 'single_forum', $templates ); 
  295.  
  296. /** 
  297. * Get the forum archive template 
  298. * 
  299. * @since bbPress (r3922) 
  300. * 
  301. * @uses bbp_get_forum_post_type() 
  302. * @uses bbp_get_query_template() 
  303. * @return string Path to template file 
  304. */ 
  305. function bbp_get_forum_archive_template() { 
  306. $templates = array( 
  307. 'archive-' . bbp_get_forum_post_type() . '.php' // Forum Archive 
  308. ); 
  309. return bbp_get_query_template( 'forum_archive', $templates ); 
  310.  
  311. /** 
  312. * Get the forum edit template 
  313. * 
  314. * @since bbPress (r3566) 
  315. * 
  316. * @uses bbp_get_topic_post_type() 
  317. * @uses bbp_get_query_template() 
  318. * @return string Path to template file 
  319. */ 
  320. function bbp_get_forum_edit_template() { 
  321. $templates = array( 
  322. 'single-' . bbp_get_forum_post_type() . '-edit.php' // Single Forum Edit 
  323. ); 
  324. return bbp_get_query_template( 'forum_edit', $templates ); 
  325.  
  326. /** 
  327. * Get the single topic template 
  328. * 
  329. * @since bbPress (r3922) 
  330. * 
  331. * @uses bbp_get_topic_post_type() 
  332. * @uses bbp_get_query_template() 
  333. * @return string Path to template file 
  334. */ 
  335. function bbp_get_single_topic_template() { 
  336. $templates = array( 
  337. 'single-' . bbp_get_topic_post_type() . '.php' 
  338. ); 
  339. return bbp_get_query_template( 'single_topic', $templates ); 
  340.  
  341. /** 
  342. * Get the topic archive template 
  343. * 
  344. * @since bbPress (r3922) 
  345. * 
  346. * @uses bbp_get_topic_post_type() 
  347. * @uses bbp_get_query_template() 
  348. * @return string Path to template file 
  349. */ 
  350. function bbp_get_topic_archive_template() { 
  351. $templates = array( 
  352. 'archive-' . bbp_get_topic_post_type() . '.php' // Topic Archive 
  353. ); 
  354. return bbp_get_query_template( 'topic_archive', $templates ); 
  355.  
  356. /** 
  357. * Get the topic edit template 
  358. * 
  359. * @since bbPress (r3311) 
  360. * 
  361. * @uses bbp_get_topic_post_type() 
  362. * @uses bbp_get_query_template() 
  363. * @return string Path to template file 
  364. */ 
  365. function bbp_get_topic_edit_template() { 
  366. $templates = array( 
  367. 'single-' . bbp_get_topic_post_type() . '-edit.php' // Single Topic Edit 
  368. ); 
  369. return bbp_get_query_template( 'topic_edit', $templates ); 
  370.  
  371. /** 
  372. * Get the topic split template 
  373. * 
  374. * @since bbPress (r3311) 
  375. * 
  376. * @uses bbp_get_topic_post_type() 
  377. * @uses bbp_get_query_template() 
  378. * @return string Path to template file 
  379. */ 
  380. function bbp_get_topic_split_template() { 
  381. $templates = array( 
  382. 'single-' . bbp_get_topic_post_type() . '-split.php', // Topic Split 
  383. ); 
  384. return bbp_get_query_template( 'topic_split', $templates ); 
  385.  
  386. /** 
  387. * Get the topic merge template 
  388. * 
  389. * @since bbPress (r3311) 
  390. * 
  391. * @uses bbp_get_topic_post_type() 
  392. * @uses bbp_get_query_template() 
  393. * @return string Path to template file 
  394. */ 
  395. function bbp_get_topic_merge_template() { 
  396. $templates = array( 
  397. 'single-' . bbp_get_topic_post_type() . '-merge.php', // Topic Merge 
  398. ); 
  399. return bbp_get_query_template( 'topic_merge', $templates ); 
  400.  
  401. /** 
  402. * Get the single reply template 
  403. * 
  404. * @since bbPress (r3922) 
  405. * 
  406. * @uses bbp_get_reply_post_type() 
  407. * @uses bbp_get_query_template() 
  408. * @return string Path to template file 
  409. */ 
  410. function bbp_get_single_reply_template() { 
  411. $templates = array( 
  412. 'single-' . bbp_get_reply_post_type() . '.php' 
  413. ); 
  414. return bbp_get_query_template( 'single_reply', $templates ); 
  415.  
  416. /** 
  417. * Get the reply edit template 
  418. * 
  419. * @since bbPress (r3311) 
  420. * 
  421. * @uses bbp_get_reply_post_type() 
  422. * @uses bbp_get_query_template() 
  423. * @return string Path to template file 
  424. */ 
  425. function bbp_get_reply_edit_template() { 
  426. $templates = array( 
  427. 'single-' . bbp_get_reply_post_type() . '-edit.php' // Single Reply Edit 
  428. ); 
  429. return bbp_get_query_template( 'reply_edit', $templates ); 
  430.  
  431. /** 
  432. * Get the reply move template 
  433. * 
  434. * @since bbPress (r4521) 
  435. * 
  436. * @uses bbp_get_reply_post_type() 
  437. * @uses bbp_get_query_template() 
  438. * @return string Path to template file 
  439. */ 
  440. function bbp_get_reply_move_template() { 
  441. $templates = array( 
  442. 'single-' . bbp_get_reply_post_type() . '-move.php', // Reply move 
  443. ); 
  444. return bbp_get_query_template( 'reply_move', $templates ); 
  445.  
  446. /** 
  447. * Get the topic template 
  448. * 
  449. * @since bbPress (r3311) 
  450. * 
  451. * @uses bbp_get_topic_tag_tax_id() 
  452. * @uses bbp_get_query_template() 
  453. * @return string Path to template file 
  454. */ 
  455. function bbp_get_topic_tag_template() { 
  456. $tt_slug = bbp_get_topic_tag_slug(); 
  457. $tt_id = bbp_get_topic_tag_tax_id(); 
  458. $templates = array( 
  459. 'taxonomy-' . $tt_slug . '.php', // Single Topic Tag slug 
  460. 'taxonomy-' . $tt_id . '.php', // Single Topic Tag ID 
  461. ); 
  462. return bbp_get_query_template( 'topic_tag', $templates ); 
  463.  
  464. /** 
  465. * Get the topic edit template 
  466. * 
  467. * @since bbPress (r3311) 
  468. * 
  469. * @uses bbp_get_topic_tag_tax_id() 
  470. * @uses bbp_get_query_template() 
  471. * @return string Path to template file 
  472. */ 
  473. function bbp_get_topic_tag_edit_template() { 
  474. $tt_slug = bbp_get_topic_tag_slug(); 
  475. $tt_id = bbp_get_topic_tag_tax_id(); 
  476. $templates = array( 
  477. 'taxonomy-' . $tt_slug . '-edit.php', // Single Topic Tag Edit slug 
  478. 'taxonomy-' . $tt_id . '-edit.php' // Single Topic Tag Edit ID 
  479. ); 
  480. return bbp_get_query_template( 'topic_tag_edit', $templates ); 
  481.  
  482. /** 
  483. * Get the templates to use as the endpoint for bbPress template parts 
  484. * 
  485. * @since bbPress (r3311) 
  486. * 
  487. * @uses apply_filters() 
  488. * @uses bbp_set_theme_compat_templates() 
  489. * @uses bbp_get_query_template() 
  490. * @return string Path to template file 
  491. */ 
  492. function bbp_get_theme_compat_templates() { 
  493. $templates = array( 
  494. 'plugin-bbpress.php',  
  495. 'bbpress.php',  
  496. 'forums.php',  
  497. 'forum.php',  
  498. 'generic.php',  
  499. 'page.php',  
  500. 'single.php',  
  501. 'index.php' 
  502. ); 
  503. return bbp_get_query_template( 'bbpress', $templates ); 
.