PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/public/frontend/wp-includes/template.php

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