PageRenderTime 37ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/bbpress/includes/core/template-functions.php

https://bitbucket.org/adatux_/uakami
PHP | 468 lines | 183 code | 85 blank | 200 comment | 34 complexity | fb4958f848afe9f54edd7160f75223af MD5 | raw file
  1. <?php
  2. /**
  3. * bbPress Template Functions
  4. *
  5. * This file contains functions necessary to mirror the WordPress core template
  6. * loading process. Many of those functions are not filterable, and even then
  7. * would not be robust enough to predict where bbPress templates might exist.
  8. *
  9. * @package bbPress
  10. * @subpackage TemplateFunctions
  11. */
  12. // Exit if accessed directly
  13. if ( !defined( 'ABSPATH' ) ) exit;
  14. /**
  15. * Adds bbPress theme support to any active WordPress theme
  16. *
  17. * @since bbPress (r3032)
  18. *
  19. * @param string $slug
  20. * @param string $name Optional. Default null
  21. * @uses bbp_locate_template()
  22. * @uses load_template()
  23. * @uses get_template_part()
  24. */
  25. function bbp_get_template_part( $slug, $name = null ) {
  26. // Execute code for this part
  27. do_action( 'get_template_part_' . $slug, $slug, $name );
  28. // Setup possible parts
  29. $templates = array();
  30. if ( isset( $name ) )
  31. $templates[] = $slug . '-' . $name . '.php';
  32. $templates[] = $slug . '.php';
  33. // Allow template parst to be filtered
  34. $templates = apply_filters( 'bbp_get_template_part', $templates, $slug, $name );
  35. // Return the part that is found
  36. return bbp_locate_template( $templates, true, false );
  37. }
  38. /**
  39. * Retrieve the name of the highest priority template file that exists.
  40. *
  41. * Searches in the child theme before parent theme so that themes which
  42. * inherit from a parent theme can just overload one file. If the template is
  43. * not found in either of those, it looks in the theme-compat folder last.
  44. *
  45. * @since bbPres (r3618)
  46. *
  47. * @param string|array $template_names Template file(s) to search for, in order.
  48. * @param bool $load If true the template file will be loaded if it is found.
  49. * @param bool $require_once Whether to require_once or require. Default true.
  50. * Has no effect if $load is false.
  51. * @return string The template filename if one is located.
  52. */
  53. function bbp_locate_template( $template_names, $load = false, $require_once = true ) {
  54. // No file found yet
  55. $located = false;
  56. $template_locations = bbp_get_template_stack();
  57. // Try to find a template file
  58. foreach ( (array) $template_names as $template_name ) {
  59. // Continue if template is empty
  60. if ( empty( $template_name ) )
  61. continue;
  62. // Trim off any slashes from the template name
  63. $template_name = ltrim( $template_name, '/' );
  64. // Loop through template stack
  65. foreach ( (array) $template_locations as $template_location ) {
  66. // Continue if $template_location is empty
  67. if ( empty( $template_location ) )
  68. continue;
  69. // Check child theme first
  70. if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
  71. $located = trailingslashit( $template_location ) . $template_name;
  72. break 2;
  73. }
  74. }
  75. }
  76. // Maybe load the template if one was located
  77. if ( ( true == $load ) && !empty( $located ) )
  78. load_template( $located, $require_once );
  79. return $located;
  80. }
  81. /**
  82. * This is really cool. This function registers a new template stack location,
  83. * using WordPress's built in filters API.
  84. *
  85. * This allows for templates to live in places beyond just the parent/child
  86. * relationship, to allow for custom template locations. Used in conjunction
  87. * with bbp_locate_template(), this allows for easy template overrides.
  88. *
  89. * @since bbPress (r4323)
  90. *
  91. * @param string $location Callback function that returns the
  92. * @param int $priority
  93. */
  94. function bbp_register_template_stack( $location_callback = '', $priority = 10 ) {
  95. // Bail if no location, or function does not exist
  96. if ( empty( $location_callback ) || ! function_exists( $location_callback ) )
  97. return false;
  98. // Add location callback to template stack
  99. add_filter( 'bbp_template_stack', $location_callback, (int) $priority );
  100. }
  101. /**
  102. * Call the functions added to the 'bbp_template_stack' filter hook, and return
  103. * an array of the template locations.
  104. *
  105. * @see bbp_register_template_stack()
  106. *
  107. * @since bbPress (r4323)
  108. *
  109. * @global array $wp_filter Stores all of the filters
  110. * @global array $merged_filters Merges the filter hooks using this function.
  111. * @global array $wp_current_filter stores the list of current filters with the current one last
  112. *
  113. * @return array The filtered value after all hooked functions are applied to it.
  114. */
  115. function bbp_get_template_stack() {
  116. global $wp_filter, $merged_filters, $wp_current_filter;
  117. // Setup some default variables
  118. $tag = 'bbp_template_stack';
  119. $args = $stack = array();
  120. // Add 'bbp_template_stack' to the current filter array
  121. $wp_current_filter[] = $tag;
  122. // Sort
  123. if ( ! isset( $merged_filters[ $tag ] ) ) {
  124. ksort( $wp_filter[$tag] );
  125. $merged_filters[ $tag ] = true;
  126. }
  127. // Ensure we're always at the beginning of the filter array
  128. reset( $wp_filter[ $tag ] );
  129. // Loop through 'bbp_template_stack' filters, and call callback functions
  130. do {
  131. foreach( (array) current( $wp_filter[$tag] ) as $the_ ) {
  132. if ( ! is_null( $the_['function'] ) ) {
  133. $args[1] = $stack;
  134. $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
  135. }
  136. }
  137. } while ( next( $wp_filter[$tag] ) !== false );
  138. // Remove 'bbp_template_stack' from the current filter array
  139. array_pop( $wp_current_filter );
  140. // Remove empties and duplicates
  141. $stack = array_unique( array_filter( $stack ) );
  142. return (array) apply_filters( 'bbp_get_template_stack', $stack ) ;
  143. }
  144. /**
  145. * Retrieve path to a template
  146. *
  147. * Used to quickly retrieve the path of a template without including the file
  148. * extension. It will also check the parent theme and theme-compat theme with
  149. * the use of {@link bbp_locate_template()}. Allows for more generic template
  150. * locations without the use of the other get_*_template() functions.
  151. *
  152. * @since bbPress (r3629)
  153. *
  154. * @param string $type Filename without extension.
  155. * @param array $templates An optional list of template candidates
  156. * @uses bbp_set_theme_compat_templates()
  157. * @uses bbp_locate_template()
  158. * @uses bbp_set_theme_compat_template()
  159. * @return string Full path to file.
  160. */
  161. function bbp_get_query_template( $type, $templates = array() ) {
  162. $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
  163. if ( empty( $templates ) )
  164. $templates = array( "{$type}.php" );
  165. // Filter possible templates, try to match one, and set any bbPress theme
  166. // compat properties so they can be cross-checked later.
  167. $templates = apply_filters( "bbp_get_{$type}_template", $templates );
  168. $templates = bbp_set_theme_compat_templates( $templates );
  169. $template = bbp_locate_template( $templates );
  170. $template = bbp_set_theme_compat_template( $template );
  171. return apply_filters( "bbp_{$type}_template", $template );
  172. }
  173. /**
  174. * Get the possible subdirectories to check for templates in
  175. *
  176. * @since bbPress (r3738)
  177. * @param array $templates Templates we are looking for
  178. * @return array Possible subfolders to look in
  179. */
  180. function bbp_get_template_locations( $templates = array() ) {
  181. $locations = array(
  182. 'bbpress',
  183. 'forums',
  184. ''
  185. );
  186. return apply_filters( 'bbp_get_template_locations', $locations, $templates );
  187. }
  188. /**
  189. * Add template locations to template files being searched for
  190. *
  191. * @since bbPress (r3738)
  192. *
  193. * @param array $templates
  194. * @return array()
  195. */
  196. function bbp_add_template_locations( $templates = array() ) {
  197. $retval = array();
  198. // Get alternate locations
  199. $locations = bbp_get_template_locations( $templates );
  200. // Loop through locations and templates and combine
  201. foreach ( (array) $locations as $location )
  202. foreach ( (array) $templates as $template )
  203. $retval[] = ltrim( trailingslashit( $location ) . $template, '/' );
  204. return apply_filters( 'bbp_add_template_locations', array_unique( $retval ), $templates );
  205. }
  206. /**
  207. * Add checks for bbPress conditions to parse_query action
  208. *
  209. * If it's a user page, WP_Query::bbp_is_single_user is set to true.
  210. * If it's a user edit page, WP_Query::bbp_is_single_user_edit is set to true
  211. * and the the 'wp-admin/includes/user.php' file is included.
  212. * In addition, on user/user edit pages, WP_Query::home is set to false & query
  213. * vars 'bbp_user_id' with the displayed user id and 'author_name' with the
  214. * displayed user's nicename are added.
  215. *
  216. * If it's a forum edit, WP_Query::bbp_is_forum_edit is set to true
  217. * If it's a topic edit, WP_Query::bbp_is_topic_edit is set to true
  218. * If it's a reply edit, WP_Query::bbp_is_reply_edit is set to true.
  219. *
  220. * If it's a view page, WP_Query::bbp_is_view is set to true
  221. *
  222. * @since bbPress (r2688)
  223. *
  224. * @param WP_Query $posts_query
  225. *
  226. * @uses get_query_var() To get {@link WP_Query} query var
  227. * @uses is_email() To check if the string is an email
  228. * @uses get_user_by() To try to get the user by email and nicename
  229. * @uses get_userdata() to get the user data
  230. * @uses current_user_can() To check if the current user can edit the user
  231. * @uses is_user_member_of_blog() To check if user profile page exists
  232. * @uses WP_Query::set_404() To set a 404 status
  233. * @uses apply_filters() Calls 'enable_edit_any_user_configuration' with true
  234. * @uses bbp_get_view_query_args() To get the view query args
  235. * @uses bbp_get_forum_post_type() To get the forum post type
  236. * @uses bbp_get_topic_post_type() To get the topic post type
  237. * @uses bbp_get_reply_post_type() To get the reply post type
  238. * @uses remove_action() To remove the auto save post revision action
  239. */
  240. function bbp_parse_query( $posts_query ) {
  241. // Bail if $posts_query is not the main loop
  242. if ( ! $posts_query->is_main_query() )
  243. return;
  244. // Bail if filters are suppressed on this query
  245. if ( true == $posts_query->get( 'suppress_filters' ) )
  246. return;
  247. // Bail if in admin
  248. if ( is_admin() )
  249. return;
  250. // Get query variables
  251. $bbp_view = $posts_query->get( bbp_get_view_rewrite_id() );
  252. $bbp_user = $posts_query->get( bbp_get_user_rewrite_id() );
  253. $is_edit = $posts_query->get( bbp_get_edit_rewrite_id() );
  254. // It is a user page - We'll also check if it is user edit
  255. if ( !empty( $bbp_user ) ) {
  256. // Not a user_id so try email and slug
  257. if ( !is_numeric( $bbp_user ) ) {
  258. // Email was passed
  259. if ( is_email( $bbp_user ) ) {
  260. $bbp_user = get_user_by( 'email', $bbp_user );
  261. // Try nicename
  262. } else {
  263. $bbp_user = get_user_by( 'slug', $bbp_user );
  264. }
  265. // If we were successful, set to ID
  266. if ( is_object( $bbp_user ) ) {
  267. $bbp_user = $bbp_user->ID;
  268. }
  269. }
  270. // Cast as int, just in case
  271. $bbp_user = (int) $bbp_user;
  272. // 404 and bail if user does not have a profile
  273. if ( ! bbp_user_has_profile( $bbp_user ) ) {
  274. $posts_query->set_404();
  275. return;
  276. }
  277. /** User Exists *******************************************************/
  278. $is_favs = $posts_query->get( bbp_get_user_favorites_rewrite_id() );
  279. $is_subs = $posts_query->get( bbp_get_user_subscriptions_rewrite_id() );
  280. $is_topics = $posts_query->get( bbp_get_user_topics_rewrite_id() );
  281. $is_replies = $posts_query->get( bbp_get_user_replies_rewrite_id() );
  282. // View or edit?
  283. if ( !empty( $is_edit ) ) {
  284. // We are editing a profile
  285. $posts_query->bbp_is_single_user_edit = true;
  286. // Load the core WordPress contact methods
  287. if ( !function_exists( '_wp_get_user_contactmethods' ) ) {
  288. include_once( ABSPATH . 'wp-includes/registration.php' );
  289. }
  290. // Load the edit_user functions
  291. if ( !function_exists( 'edit_user' ) ) {
  292. require_once( ABSPATH . 'wp-admin/includes/user.php' );
  293. }
  294. // Load the grant/revoke super admin functions
  295. if ( is_multisite() && !function_exists( 'revoke_super_admin' ) ) {
  296. require_once( ABSPATH . 'wp-admin/includes/ms.php' );
  297. }
  298. // Editing a user
  299. $posts_query->bbp_is_edit = true;
  300. // User favorites
  301. } elseif ( ! empty( $is_favs ) ) {
  302. $posts_query->bbp_is_single_user_favs = true;
  303. // User subscriptions
  304. } elseif ( ! empty( $is_subs ) ) {
  305. $posts_query->bbp_is_single_user_subs = true;
  306. // User topics
  307. } elseif ( ! empty( $is_topics ) ) {
  308. $posts_query->bbp_is_single_user_topics = true;
  309. // User topics
  310. } elseif ( ! empty( $is_replies ) ) {
  311. $posts_query->bbp_is_single_user_replies = true;
  312. // User profile
  313. } else {
  314. $posts_query->bbp_is_single_user_profile = true;
  315. }
  316. // Looking at a single user
  317. $posts_query->bbp_is_single_user = true;
  318. // Make sure 404 is not set
  319. $posts_query->is_404 = false;
  320. // Correct is_home variable
  321. $posts_query->is_home = false;
  322. // Get the user data
  323. $user = get_userdata( $bbp_user );
  324. // User is looking at their own profile
  325. if ( get_current_user_id() == $user->ID ) {
  326. $posts_query->bbp_is_single_user_home = true;
  327. }
  328. // Set bbp_user_id for future reference
  329. $posts_query->set( 'bbp_user_id', $user->ID );
  330. // Set author_name as current user's nicename to get correct posts
  331. $posts_query->set( 'author_name', $user->user_nicename );
  332. // Set the displayed user global to this user
  333. bbpress()->displayed_user = $user;
  334. // View Page
  335. } elseif ( !empty( $bbp_view ) ) {
  336. // Check if the view exists by checking if there are query args are set
  337. $view_args = bbp_get_view_query_args( $bbp_view );
  338. // Bail if view args is false (view isn't registered)
  339. if ( false === $view_args ) {
  340. $posts_query->set_404();
  341. return;
  342. }
  343. // Correct is_home variable
  344. $posts_query->is_home = false;
  345. // We are in a custom topic view
  346. $posts_query->bbp_is_view = true;
  347. // Forum/Topic/Reply Edit Page
  348. } elseif ( !empty( $is_edit ) ) {
  349. // Get the post type from the main query loop
  350. $post_type = $posts_query->get( 'post_type' );
  351. // Check which post_type we are editing, if any
  352. if ( !empty( $post_type ) ) {
  353. switch( $post_type ) {
  354. // We are editing a forum
  355. case bbp_get_forum_post_type() :
  356. $posts_query->bbp_is_forum_edit = true;
  357. $posts_query->bbp_is_edit = true;
  358. break;
  359. // We are editing a topic
  360. case bbp_get_topic_post_type() :
  361. $posts_query->bbp_is_topic_edit = true;
  362. $posts_query->bbp_is_edit = true;
  363. break;
  364. // We are editing a reply
  365. case bbp_get_reply_post_type() :
  366. $posts_query->bbp_is_reply_edit = true;
  367. $posts_query->bbp_is_edit = true;
  368. break;
  369. }
  370. // We are editing a topic tag
  371. } elseif ( bbp_is_topic_tag() ) {
  372. $posts_query->bbp_is_topic_tag_edit = true;
  373. $posts_query->bbp_is_edit = true;
  374. }
  375. // We save post revisions on our own
  376. remove_action( 'pre_post_update', 'wp_save_post_revision' );
  377. // Topic tag page
  378. } elseif ( bbp_is_topic_tag() ) {
  379. $posts_query->set( 'bbp_topic_tag', get_query_var( 'term' ) );
  380. $posts_query->set( 'post_type', bbp_get_topic_post_type() );
  381. $posts_query->set( 'posts_per_page', bbp_get_topics_per_page() );
  382. }
  383. }