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

/wp-includes/template.php

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