Make WordPress Core


Ignore:
Timestamp:
02/28/2012 01:49:49 AM (13 years ago)
Author:
nacin
Message:

Move the template loading functions from wp-includes/theme.php to wp-includes/template.php. This includes get_query_template(), locate_template(), and friends. see #20103.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/theme.php

    r20001 r20002  
    44 *
    55 * @package WordPress
    6  * @subpackage Template
     6 * @subpackage Theme
    77 */
    88
     
    726726
    727727/**
    728  * Retrieve path to a template
    729  *
    730  * Used to quickly retrieve the path of a template without including the file
    731  * extension. It will also check the parent theme, if the file exists, with
    732  * the use of {@link locate_template()}. Allows for more generic template location
    733  * without the use of the other get_*_template() functions.
    734  *
    735  * @since 1.5.0
    736  *
    737  * @param string $type Filename without extension.
    738  * @param array $templates An optional list of template candidates
    739  * @return string Full path to file.
    740  */
    741 function get_query_template( $type, $templates = array() ) {
    742     $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    743 
    744     if ( empty( $templates ) )
    745         $templates = array("{$type}.php");
    746 
    747     return apply_filters( "{$type}_template", locate_template( $templates ) );
    748 }
    749 
    750 /**
    751  * Retrieve path of index template in current or parent template.
    752  *
    753  * @since 3.0.0
    754  *
    755  * @return string
    756  */
    757 function get_index_template() {
    758     return get_query_template('index');
    759 }
    760 
    761 /**
    762  * Retrieve path of 404 template in current or parent template.
    763  *
    764  * @since 1.5.0
    765  *
    766  * @return string
    767  */
    768 function get_404_template() {
    769     return get_query_template('404');
    770 }
    771 
    772 /**
    773  * Retrieve path of archive template in current or parent template.
    774  *
    775  * @since 1.5.0
    776  *
    777  * @return string
    778  */
    779 function get_archive_template() {
    780     $post_type = get_query_var( 'post_type' );
    781 
    782     $templates = array();
    783 
    784     if ( $post_type )
    785         $templates[] = "archive-{$post_type}.php";
    786     $templates[] = 'archive.php';
    787 
    788     return get_query_template( 'archive', $templates );
    789 }
    790 
    791 /**
    792  * Retrieve path of author template in current or parent template.
    793  *
    794  * @since 1.5.0
    795  *
    796  * @return string
    797  */
    798 function get_author_template() {
    799     $author = get_queried_object();
    800 
    801     $templates = array();
    802 
    803     $templates[] = "author-{$author->user_nicename}.php";
    804     $templates[] = "author-{$author->ID}.php";
    805     $templates[] = 'author.php';
    806 
    807     return get_query_template( 'author', $templates );
    808 }
    809 
    810 /**
    811  * Retrieve path of category template in current or parent template.
    812  *
    813  * Works by first retrieving the current slug for example 'category-default.php' and then
    814  * trying category ID, for example 'category-1.php' and will finally fallback to category.php
    815  * template, if those files don't exist.
    816  *
    817  * @since 1.5.0
    818  * @uses apply_filters() Calls 'category_template' on file path of category template.
    819  *
    820  * @return string
    821  */
    822 function get_category_template() {
    823     $category = get_queried_object();
    824 
    825     $templates = array();
    826 
    827     $templates[] = "category-{$category->slug}.php";
    828     $templates[] = "category-{$category->term_id}.php";
    829     $templates[] = 'category.php';
    830 
    831     return get_query_template( 'category', $templates );
    832 }
    833 
    834 /**
    835  * Retrieve path of tag template in current or parent template.
    836  *
    837  * Works by first retrieving the current tag name, for example 'tag-wordpress.php' and then
    838  * trying tag ID, for example 'tag-1.php' and will finally fallback to tag.php
    839  * template, if those files don't exist.
    840  *
    841  * @since 2.3.0
    842  * @uses apply_filters() Calls 'tag_template' on file path of tag template.
    843  *
    844  * @return string
    845  */
    846 function get_tag_template() {
    847     $tag = get_queried_object();
    848 
    849     $templates = array();
    850 
    851     $templates[] = "tag-{$tag->slug}.php";
    852     $templates[] = "tag-{$tag->term_id}.php";
    853     $templates[] = 'tag.php';
    854 
    855     return get_query_template( 'tag', $templates );
    856 }
    857 
    858 /**
    859  * Retrieve path of taxonomy template in current or parent template.
    860  *
    861  * Retrieves the taxonomy and term, if term is available. The template is
    862  * prepended with 'taxonomy-' and followed by both the taxonomy string and
    863  * the taxonomy string followed by a dash and then followed by the term.
    864  *
    865  * The taxonomy and term template is checked and used first, if it exists.
    866  * Second, just the taxonomy template is checked, and then finally, taxonomy.php
    867  * template is used. If none of the files exist, then it will fall back on to
    868  * index.php.
    869  *
    870  * @since 2.5.0
    871  * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
    872  *
    873  * @return string
    874  */
    875 function get_taxonomy_template() {
    876     $term = get_queried_object();
    877     $taxonomy = $term->taxonomy;
    878 
    879     $templates = array();
    880 
    881     $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
    882     $templates[] = "taxonomy-$taxonomy.php";
    883     $templates[] = 'taxonomy.php';
    884 
    885     return get_query_template( 'taxonomy', $templates );
    886 }
    887 
    888 /**
    889  * Retrieve path of date template in current or parent template.
    890  *
    891  * @since 1.5.0
    892  *
    893  * @return string
    894  */
    895 function get_date_template() {
    896     return get_query_template('date');
    897 }
    898 
    899 /**
    900  * Retrieve path of home template in current or parent template.
    901  *
    902  * This is the template used for the page containing the blog posts
    903  *
    904  * Attempts to locate 'home.php' first before falling back to 'index.php'.
    905  *
    906  * @since 1.5.0
    907  * @uses apply_filters() Calls 'home_template' on file path of home template.
    908  *
    909  * @return string
    910  */
    911 function get_home_template() {
    912     $templates = array( 'home.php', 'index.php' );
    913 
    914     return get_query_template( 'home', $templates );
    915 }
    916 
    917 /**
    918  * Retrieve path of front-page template in current or parent template.
    919  *
    920  * Looks for 'front-page.php'.
    921  *
    922  * @since 3.0.0
    923  * @uses apply_filters() Calls 'front_page_template' on file path of template.
    924  *
    925  * @return string
    926  */
    927 function get_front_page_template() {
    928     $templates = array('front-page.php');
    929 
    930     return get_query_template( 'front_page', $templates );
    931 }
    932 
    933 /**
    934  * Retrieve path of page template in current or parent template.
    935  *
    936  * Will first look for the specifically assigned page template
    937  * The will search for 'page-{slug}.php' followed by 'page-id.php'
    938  * and finally 'page.php'
    939  *
    940  * @since 1.5.0
    941  *
    942  * @return string
    943  */
    944 function get_page_template() {
    945     $id = get_queried_object_id();
    946     $template = get_post_meta($id, '_wp_page_template', true);
    947     $pagename = get_query_var('pagename');
    948 
    949     if ( !$pagename && $id > 0 ) {
    950         // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
    951         $post = get_queried_object();
    952         $pagename = $post->post_name;
    953     }
    954 
    955     if ( 'default' == $template )
    956         $template = '';
    957 
    958     $templates = array();
    959     if ( !empty($template) && !validate_file($template) )
    960         $templates[] = $template;
    961     if ( $pagename )
    962         $templates[] = "page-$pagename.php";
    963     if ( $id )
    964         $templates[] = "page-$id.php";
    965     $templates[] = 'page.php';
    966 
    967     return get_query_template( 'page', $templates );
    968 }
    969 
    970 /**
    971  * Retrieve path of paged template in current or parent template.
    972  *
    973  * @since 1.5.0
    974  *
    975  * @return string
    976  */
    977 function get_paged_template() {
    978     return get_query_template('paged');
    979 }
    980 
    981 /**
    982  * Retrieve path of search template in current or parent template.
    983  *
    984  * @since 1.5.0
    985  *
    986  * @return string
    987  */
    988 function get_search_template() {
    989     return get_query_template('search');
    990 }
    991 
    992 /**
    993  * Retrieve path of single template in current or parent template.
    994  *
    995  * @since 1.5.0
    996  *
    997  * @return string
    998  */
    999 function get_single_template() {
    1000     $object = get_queried_object();
    1001 
    1002     $templates = array();
    1003 
    1004     $templates[] = "single-{$object->post_type}.php";
    1005     $templates[] = "single.php";
    1006 
    1007     return get_query_template( 'single', $templates );
    1008 }
    1009 
    1010 /**
    1011  * Retrieve path of attachment template in current or parent template.
    1012  *
    1013  * The attachment path first checks if the first part of the mime type exists.
    1014  * The second check is for the second part of the mime type. The last check is
    1015  * for both types separated by an underscore. If neither are found then the file
    1016  * 'attachment.php' is checked and returned.
    1017  *
    1018  * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
    1019  * finally 'text_plain.php'.
    1020  *
    1021  * @since 2.0.0
    1022  *
    1023  * @return string
    1024  */
    1025 function get_attachment_template() {
    1026     global $posts;
    1027     $type = explode('/', $posts[0]->post_mime_type);
    1028     if ( $template = get_query_template($type[0]) )
    1029         return $template;
    1030     elseif ( $template = get_query_template($type[1]) )
    1031         return $template;
    1032     elseif ( $template = get_query_template("$type[0]_$type[1]") )
    1033         return $template;
    1034     else
    1035         return get_query_template('attachment');
    1036 }
    1037 
    1038 /**
    1039  * Retrieve path of comment popup template in current or parent template.
    1040  *
    1041  * Checks for comment popup template in current template, if it exists or in the
    1042  * parent template.
    1043  *
    1044  * @since 1.5.0
    1045  * @uses apply_filters() Calls 'comments_popup_template' filter on path.
    1046  *
    1047  * @return string
    1048  */
    1049 function get_comments_popup_template() {
    1050     $template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
    1051 
    1052     // Backward compat code will be removed in a future release
    1053     if ('' == $template)
    1054         $template = ABSPATH . WPINC . '/theme-compat/comments-popup.php';
    1055 
    1056     return $template;
    1057 }
    1058 
    1059 /**
    1060  * Retrieve the name of the highest priority template file that exists.
    1061  *
    1062  * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
    1063  * inherit from a parent theme can just overload one file.
    1064  *
    1065  * @since 2.7.0
    1066  *
    1067  * @param string|array $template_names Template file(s) to search for, in order.
    1068  * @param bool $load If true the template file will be loaded if it is found.
    1069  * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
    1070  * @return string The template filename if one is located.
    1071  */
    1072 function locate_template($template_names, $load = false, $require_once = true ) {
    1073     $located = '';
    1074     foreach ( (array) $template_names as $template_name ) {
    1075         if ( !$template_name )
    1076             continue;
    1077         if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
    1078             $located = STYLESHEETPATH . '/' . $template_name;
    1079             break;
    1080         } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
    1081             $located = TEMPLATEPATH . '/' . $template_name;
    1082             break;
    1083         }
    1084     }
    1085 
    1086     if ( $load && '' != $located )
    1087         load_template( $located, $require_once );
    1088 
    1089     return $located;
    1090 }
    1091 
    1092 /**
    1093  * Require the template file with WordPress environment.
    1094  *
    1095  * The globals are set up for the template file to ensure that the WordPress
    1096  * environment is available from within the function. The query variables are
    1097  * also available.
    1098  *
    1099  * @since 1.5.0
    1100  *
    1101  * @param string $_template_file Path to template file.
    1102  * @param bool $require_once Whether to require_once or require. Default true.
    1103  */
    1104 function load_template( $_template_file, $require_once = true ) {
    1105     global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
    1106 
    1107     if ( is_array( $wp_query->query_vars ) )
    1108         extract( $wp_query->query_vars, EXTR_SKIP );
    1109 
    1110     if ( $require_once )
    1111         require_once( $_template_file );
    1112     else
    1113         require( $_template_file );
    1114 }
    1115 
    1116 /**
    1117728 * Display localized stylesheet link element.
    1118729 *
Note: See TracChangeset for help on using the changeset viewer.