PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/template.php

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 411 lines | 154 code | 54 blank | 203 comment | 29 complexity | 0a26611f5651b17ee41082d22dc4e4c4 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Template loading functions.
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Retrieve path to a template
  10. *
  11. * Used to quickly retrieve the path of a template without including the file
  12. * extension. It will also check the parent theme, if the file exists, with
  13. * the use of {@link locate_template()}. Allows for more generic template location
  14. * without the use of the other get_*_template() functions.
  15. *
  16. * @since 1.5.0
  17. *
  18. * @param string $type Filename without extension.
  19. * @param array $templates An optional list of template candidates
  20. * @return string Full path to file.
  21. */
  22. function get_query_template( $type, $templates = array() ) {
  23. $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
  24. if ( empty( $templates ) )
  25. $templates = array("{$type}.php");
  26. return apply_filters( "{$type}_template", locate_template( $templates ) );
  27. }
  28. /**
  29. * Retrieve path of index template in current or parent template.
  30. *
  31. * @since 3.0.0
  32. *
  33. * @return string
  34. */
  35. function get_index_template() {
  36. return get_query_template('index');
  37. }
  38. /**
  39. * Retrieve path of 404 template in current or parent template.
  40. *
  41. * @since 1.5.0
  42. *
  43. * @return string
  44. */
  45. function get_404_template() {
  46. return get_query_template('404');
  47. }
  48. /**
  49. * Retrieve path of archive template in current or parent template.
  50. *
  51. * @since 1.5.0
  52. *
  53. * @return string
  54. */
  55. function get_archive_template() {
  56. $post_types = array_filter( (array) get_query_var( 'post_type' ) );
  57. $templates = array();
  58. if ( count( $post_types ) == 1 ) {
  59. $post_type = reset( $post_types );
  60. $templates[] = "archive-{$post_type}.php";
  61. }
  62. $templates[] = 'archive.php';
  63. return get_query_template( 'archive', $templates );
  64. }
  65. /**
  66. * Retrieve path of author template in current or parent template.
  67. *
  68. * @since 1.5.0
  69. *
  70. * @return string
  71. */
  72. function get_author_template() {
  73. $author = get_queried_object();
  74. $templates = array();
  75. if ( $author ) {
  76. $templates[] = "author-{$author->user_nicename}.php";
  77. $templates[] = "author-{$author->ID}.php";
  78. }
  79. $templates[] = 'author.php';
  80. return get_query_template( 'author', $templates );
  81. }
  82. /**
  83. * Retrieve path of category template in current or parent template.
  84. *
  85. * Works by first retrieving the current slug for example 'category-default.php' and then
  86. * trying category ID, for example 'category-1.php' and will finally fallback to category.php
  87. * template, if those files don't exist.
  88. *
  89. * @since 1.5.0
  90. * @uses apply_filters() Calls 'category_template' on file path of category template.
  91. *
  92. * @return string
  93. */
  94. function get_category_template() {
  95. $category = get_queried_object();
  96. $templates = array();
  97. if ( $category ) {
  98. $templates[] = "category-{$category->slug}.php";
  99. $templates[] = "category-{$category->term_id}.php";
  100. }
  101. $templates[] = 'category.php';
  102. return get_query_template( 'category', $templates );
  103. }
  104. /**
  105. * Retrieve path of tag template in current or parent template.
  106. *
  107. * Works by first retrieving the current tag name, for example 'tag-wordpress.php' and then
  108. * trying tag ID, for example 'tag-1.php' and will finally fallback to tag.php
  109. * template, if those files don't exist.
  110. *
  111. * @since 2.3.0
  112. * @uses apply_filters() Calls 'tag_template' on file path of tag template.
  113. *
  114. * @return string
  115. */
  116. function get_tag_template() {
  117. $tag = get_queried_object();
  118. $templates = array();
  119. if ( $tag ) {
  120. $templates[] = "tag-{$tag->slug}.php";
  121. $templates[] = "tag-{$tag->term_id}.php";
  122. }
  123. $templates[] = 'tag.php';
  124. return get_query_template( 'tag', $templates );
  125. }
  126. /**
  127. * Retrieve path of taxonomy template in current or parent template.
  128. *
  129. * Retrieves the taxonomy and term, if term is available. The template is
  130. * prepended with 'taxonomy-' and followed by both the taxonomy string and
  131. * the taxonomy string followed by a dash and then followed by the term.
  132. *
  133. * The taxonomy and term template is checked and used first, if it exists.
  134. * Second, just the taxonomy template is checked, and then finally, taxonomy.php
  135. * template is used. If none of the files exist, then it will fall back on to
  136. * index.php.
  137. *
  138. * @since 2.5.0
  139. * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
  140. *
  141. * @return string
  142. */
  143. function get_taxonomy_template() {
  144. $term = get_queried_object();
  145. $templates = array();
  146. if ( $term ) {
  147. $taxonomy = $term->taxonomy;
  148. $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
  149. $templates[] = "taxonomy-$taxonomy.php";
  150. }
  151. $templates[] = 'taxonomy.php';
  152. return get_query_template( 'taxonomy', $templates );
  153. }
  154. /**
  155. * Retrieve path of date template in current or parent template.
  156. *
  157. * @since 1.5.0
  158. *
  159. * @return string
  160. */
  161. function get_date_template() {
  162. return get_query_template('date');
  163. }
  164. /**
  165. * Retrieve path of home template in current or parent template.
  166. *
  167. * This is the template used for the page containing the blog posts
  168. *
  169. * Attempts to locate 'home.php' first before falling back to 'index.php'.
  170. *
  171. * @since 1.5.0
  172. * @uses apply_filters() Calls 'home_template' on file path of home template.
  173. *
  174. * @return string
  175. */
  176. function get_home_template() {
  177. $templates = array( 'home.php', 'index.php' );
  178. return get_query_template( 'home', $templates );
  179. }
  180. /**
  181. * Retrieve path of front-page template in current or parent template.
  182. *
  183. * Looks for 'front-page.php'.
  184. *
  185. * @since 3.0.0
  186. * @uses apply_filters() Calls 'front_page_template' on file path of template.
  187. *
  188. * @return string
  189. */
  190. function get_front_page_template() {
  191. $templates = array('front-page.php');
  192. return get_query_template( 'front_page', $templates );
  193. }
  194. /**
  195. * Retrieve path of page template in current or parent template.
  196. *
  197. * Will first look for the specifically assigned page template
  198. * The will search for 'page-{slug}.php' followed by 'page-id.php'
  199. * and finally 'page.php'
  200. *
  201. * @since 1.5.0
  202. *
  203. * @return string
  204. */
  205. function get_page_template() {
  206. $id = get_queried_object_id();
  207. $template = get_page_template_slug();
  208. $pagename = get_query_var('pagename');
  209. if ( ! $pagename && $id ) {
  210. // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
  211. $post = get_queried_object();
  212. $pagename = $post->post_name;
  213. }
  214. $templates = array();
  215. if ( $template && 0 === validate_file( $template ) )
  216. $templates[] = $template;
  217. if ( $pagename )
  218. $templates[] = "page-$pagename.php";
  219. if ( $id )
  220. $templates[] = "page-$id.php";
  221. $templates[] = 'page.php';
  222. return get_query_template( 'page', $templates );
  223. }
  224. /**
  225. * Retrieve path of paged template in current or parent template.
  226. *
  227. * @since 1.5.0
  228. *
  229. * @return string
  230. */
  231. function get_paged_template() {
  232. return get_query_template('paged');
  233. }
  234. /**
  235. * Retrieve path of search template in current or parent template.
  236. *
  237. * @since 1.5.0
  238. *
  239. * @return string
  240. */
  241. function get_search_template() {
  242. return get_query_template('search');
  243. }
  244. /**
  245. * Retrieve path of single template in current or parent template.
  246. *
  247. * @since 1.5.0
  248. *
  249. * @return string
  250. */
  251. function get_single_template() {
  252. $object = get_queried_object();
  253. $templates = array();
  254. if ( $object )
  255. $templates[] = "single-{$object->post_type}.php";
  256. $templates[] = "single.php";
  257. return get_query_template( 'single', $templates );
  258. }
  259. /**
  260. * Retrieve path of attachment template in current or parent template.
  261. *
  262. * The attachment path first checks if the first part of the mime type exists.
  263. * The second check is for the second part of the mime type. The last check is
  264. * for both types separated by an underscore. If neither are found then the file
  265. * 'attachment.php' is checked and returned.
  266. *
  267. * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
  268. * finally 'text_plain.php'.
  269. *
  270. * @since 2.0.0
  271. *
  272. * @return string
  273. */
  274. function get_attachment_template() {
  275. global $posts;
  276. if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
  277. $type = explode( '/', $posts[0]->post_mime_type );
  278. if ( ! empty( $type ) ) {
  279. if ( $template = get_query_template( $type[0] ) )
  280. return $template;
  281. elseif ( $template = get_query_template( $type[1] ) )
  282. return $template;
  283. elseif ( $template = get_query_template( "$type[0]_$type[1]" ) )
  284. return $template;
  285. }
  286. }
  287. return get_query_template( 'attachment' );
  288. }
  289. /**
  290. * Retrieve path of comment popup template in current or parent template.
  291. *
  292. * Checks for comment popup template in current template, if it exists or in the
  293. * parent template.
  294. *
  295. * @since 1.5.0
  296. * @uses apply_filters() Calls 'comments_popup_template' filter on path.
  297. *
  298. * @return string
  299. */
  300. function get_comments_popup_template() {
  301. $template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
  302. // Backward compat code will be removed in a future release
  303. if ('' == $template)
  304. $template = ABSPATH . WPINC . '/theme-compat/comments-popup.php';
  305. return $template;
  306. }
  307. /**
  308. * Retrieve the name of the highest priority template file that exists.
  309. *
  310. * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
  311. * inherit from a parent theme can just overload one file.
  312. *
  313. * @since 2.7.0
  314. *
  315. * @param string|array $template_names Template file(s) to search for, in order.
  316. * @param bool $load If true the template file will be loaded if it is found.
  317. * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
  318. * @return string The template filename if one is located.
  319. */
  320. function locate_template($template_names, $load = false, $require_once = true ) {
  321. $located = '';
  322. foreach ( (array) $template_names as $template_name ) {
  323. if ( !$template_name )
  324. continue;
  325. if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
  326. $located = STYLESHEETPATH . '/' . $template_name;
  327. break;
  328. } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
  329. $located = TEMPLATEPATH . '/' . $template_name;
  330. break;
  331. }
  332. }
  333. if ( $load && '' != $located )
  334. load_template( $located, $require_once );
  335. return $located;
  336. }
  337. /**
  338. * Require the template file with WordPress environment.
  339. *
  340. * The globals are set up for the template file to ensure that the WordPress
  341. * environment is available from within the function. The query variables are
  342. * also available.
  343. *
  344. * @since 1.5.0
  345. *
  346. * @param string $_template_file Path to template file.
  347. * @param bool $require_once Whether to require_once or require. Default true.
  348. */
  349. function load_template( $_template_file, $require_once = true ) {
  350. global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
  351. if ( is_array( $wp_query->query_vars ) )
  352. extract( $wp_query->query_vars, EXTR_SKIP );
  353. if ( $require_once )
  354. require_once( $_template_file );
  355. else
  356. require( $_template_file );
  357. }