Make WordPress Core

Changeset 8624


Ignore:
Timestamp:
08/12/2008 08:18:05 PM (16 years ago)
Author:
westi
Message:

Refactor template location code to reduce duplication.
Also make it easier for theme authors to pull in seperate files into templates while making theme overrideable. See #7492.

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/general-template.php

    r8600 r8624  
    55function get_header() {
    66    do_action( 'get_header' );
    7     if ( file_exists( STYLESHEETPATH . '/header.php') )
    8         load_template( STYLESHEETPATH . '/header.php');
    9     elseif ( file_exists( TEMPLATEPATH . '/header.php') )
    10         load_template( TEMPLATEPATH . '/header.php');
    11     else
     7    if ('' == locate_template(array('header.php'), true))
    128        load_template( get_theme_root() . '/default/header.php');
    139}
     
    1612function get_footer() {
    1713    do_action( 'get_footer' );
    18     if ( file_exists( STYLESHEETPATH . '/footer.php') )
    19         load_template( STYLESHEETPATH . '/footer.php');
    20     elseif ( file_exists( TEMPLATEPATH . '/footer.php') )
    21         load_template( TEMPLATEPATH . '/footer.php');
    22     else
     14    if ('' == locate_template(array('footer.php'), true))
    2315        load_template( get_theme_root() . '/default/footer.php');
    2416}
     
    2719function get_sidebar( $name = null ) {
    2820    do_action( 'get_sidebar' );
    29     if ( isset($name) && file_exists( STYLESHEETPATH . "/sidebar-{$name}.php") )
    30         load_template( STYLESHEETPATH . "/sidebar-{$name}.php");
    31     elseif ( isset($name) && file_exists( TEMPLATEPATH . "/sidebar-{$name}.php") )
    32         load_template( TEMPLATEPATH . "/sidebar-{$name}.php");
    33     elseif ( file_exists( STYLESHEETPATH . '/sidebar.php') )
    34         load_template( STYLESHEETPATH . '/sidebar.php');
    35     elseif ( file_exists( TEMPLATEPATH . '/sidebar.php') )
    36         load_template( TEMPLATEPATH . '/sidebar.php');
    37     else
     21   
     22    $templates = array();
     23    if ( isset($name) )
     24        $templates[] = "sidebar-{$name}.php";
     25   
     26    $templates[] = "sidebar.php";
     27   
     28    if ('' == locate_template($templates, true))
    3829        load_template( get_theme_root() . '/default/sidebar.php');
    3930}
  • trunk/wp-includes/theme.php

    r8600 r8624  
    11<?php
    2 /*
     2/**
     3 * @package WordPress
     4 * @subpackage Themes
     5 * @since 0.0
    36 * Theme/template/stylesheet functions.
    47 */
    5 
    68function get_stylesheet() {
    79    return apply_filters('stylesheet', get_option('stylesheet'));
     
    347349
    348350function get_query_template($type) {
    349     $template = '';
    350351    $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    351     if ( file_exists(STYLESHEETPATH . "/{$type}.php") )
    352         $template = STYLESHEETPATH . "/{$type}.php";
    353     elseif ( file_exists(TEMPLATEPATH . "/{$type}.php") )
    354         $template = TEMPLATEPATH . "/{$type}.php";
    355 
    356     return apply_filters("{$type}_template", $template);
     352    return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
    357353}
    358354
     
    370366
    371367function get_category_template() {
    372     $template = '';
    373     if ( file_exists(STYLESHEETPATH . "/category-" . absint( get_query_var('cat') ) . '.php') )
    374         $template = STYLESHEETPATH . "/category-" . absint( get_query_var('cat') ) . '.php';
    375     elseif ( file_exists(TEMPLATEPATH . "/category-" . absint( get_query_var('cat') ) . '.php') )
    376         $template = TEMPLATEPATH . "/category-" . absint( get_query_var('cat') ) . '.php';
    377     elseif ( file_exists(STYLESHEETPATH . "/category.php") )
    378         $template = STYLESHEETPATH . "/category.php";
    379     elseif ( file_exists(TEMPLATEPATH . "/category.php") )
    380         $template = TEMPLATEPATH . "/category.php";
    381 
     368    $template =locate_template(array("category-" . absint( get_query_var('cat') ) . '.php',"category.php"));
    382369    return apply_filters('category_template', $template);
    383370}
    384371
    385372function get_tag_template() {
    386     $template = '';
    387     if ( file_exists(STYLESHEETPATH . "/tag-" . get_query_var('tag') . '.php') )
    388         $template = STYLESHEETPATH . "/tag-" . get_query_var('tag') . '.php';
    389     elseif ( file_exists(TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php') )
    390         $template = TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php';
    391     elseif ( file_exists(STYLESHEETPATH . "/tag.php") )
    392         $template = STYLESHEETPATH . "/tag.php";
    393     elseif ( file_exists(TEMPLATEPATH . "/tag.php") )
    394         $template = TEMPLATEPATH . "/tag.php";
    395 
     373    $template = locate_template(array("tag-" . absint( get_query_var('tag') ) . '.php',"tag.php"));
    396374    return apply_filters('tag_template', $template);
    397375}
    398376
    399377function get_taxonomy_template() {
    400     $template = '';
    401378    $taxonomy = get_query_var('taxonomy');
    402379    $term = get_query_var('term');
    403     if ( $taxonomy && $term && file_exists(STYLESHEETPATH . "/taxonomy-$taxonomy-$term.php") )
    404         $template = STYLESHEETPATH . "/taxonomy-$taxonomy-$term.php";
    405     elseif ( $taxonomy && $term && file_exists(TEMPLATEPATH . "/taxonomy-$taxonomy-$term.php") )
    406         $template = TEMPLATEPATH . "/taxonomy-$taxonomy-$term.php";
    407     elseif ( $taxonomy && file_exists(STYLESHEETPATH . "/taxonomy-$taxonomy.php") )
    408         $template = STYLESHEETPATH . "/taxonomy-$taxonomy.php";
    409     elseif ( $taxonomy && file_exists(TEMPLATEPATH . "/taxonomy-$taxonomy.php") )
    410         $template = TEMPLATEPATH . "/taxonomy-$taxonomy.php";
    411     elseif ( file_exists(STYLESHEETPATH . "/taxonomy.php") )
    412         $template = STYLESHEETPATH . "/taxonomy.php";
    413     elseif ( file_exists(TEMPLATEPATH . "/taxonomy.php") )
    414         $template = TEMPLATEPATH . "/taxonomy.php";
    415 
     380
     381    $templates = array();
     382    if ( $taxonomy && $term )
     383        $templates[] = "taxonomy-$taxonomy-$term.php";
     384    if ( $taxonomy )
     385        $templates[] = "taxonomy-$taxonomy.php";
     386   
     387    $templates[] = "taxonomy.php";
     388
     389    $template = locate_template($templates);
    416390    return apply_filters('taxonomy_template', $template);
    417391}
     
    422396
    423397function get_home_template() {
    424     $template = '';
    425 
    426     if ( file_exists(STYLESHEETPATH . "/home.php") )
    427         $template = STYLESHEETPATH . "/home.php";
    428     elseif ( file_exists(TEMPLATEPATH . "/home.php") )
    429         $template = TEMPLATEPATH . "/home.php";
    430     elseif ( file_exists(STYLESHEETPATH . "/index.php") )
    431         $template = STYLESHEETPATH . "/index.php";
    432     elseif ( file_exists(TEMPLATEPATH . "/index.php") )
    433         $template = TEMPLATEPATH . "/index.php";
    434 
     398    $template = locate_template(array('home.php','index.php'));
    435399    return apply_filters('home_template', $template);
    436400}
     
    445409        $template = '';
    446410
    447     if ( !empty($template) && !validate_file($template) && file_exists(STYLESHEETPATH . "/$template") )
    448         $template = STYLESHEETPATH . "/$template";
    449     elseif ( !empty($template) && !validate_file($template) && file_exists(TEMPLATEPATH . "/$template") )
    450         $template = TEMPLATEPATH . "/$template";
    451     elseif ( file_exists(STYLESHEETPATH . "/page.php") )
    452         $template = STYLESHEETPATH . "/page.php";
    453     elseif ( file_exists(TEMPLATEPATH . "/page.php") )
    454         $template = TEMPLATEPATH . "/page.php";
    455     else
    456         $template = '';
    457 
    458     return apply_filters('page_template', $template);
     411    $templates = array();
     412    if ( !empty($template) && !validate_file($template) )
     413        $templates[] = $template;
     414   
     415    $templates[] = "page.php";
     416
     417    return apply_filters('page_template', locate_template($templates));
    459418}
    460419
     
    485444
    486445function get_comments_popup_template() {
    487     if ( file_exists( STYLESHEETPATH . '/comments-popup.php') )
    488         $template = STYLESHEETPATH . '/comments-popup.php';
    489     elseif ( file_exists( TEMPLATEPATH . '/comments-popup.php') )
    490         $template = TEMPLATEPATH . '/comments-popup.php';
    491     else
     446    $template = locate_template(array("comments-popup.php"));
     447    if ('' == $template)
    492448        $template = get_theme_root() . '/default/comments-popup.php';
    493449
    494450    return apply_filters('comments_popup_template', $template);
     451}
     452
     453/**
     454 * Returns the name of the highest priority template file that exists
     455 *
     456 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
     457 * inherit from a parent theme can just overload one file.
     458 * @since 2.7
     459 *
     460 * @param array $template_names Array of template files to search for in priority order
     461 * @param bool $load If true the template file will be loaded if it is found.
     462 * @return string The template filename if one is located.
     463 */
     464function locate_template($template_names, $load = false) {
     465    if (!is_array($template_names))
     466        return '';
     467   
     468    $located = '';
     469    foreach($template_names as $template_name) {
     470        if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
     471            $located = STYLESHEETPATH . '/' . $template_name;
     472            break;
     473        } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
     474            $located = TEMPLATEPATH . '/' . $template_name;
     475            break;
     476        }
     477    }
     478   
     479    if ($load && '' != $located)
     480        load_template($located);
     481   
     482    return $located;
    495483}
    496484
Note: See TracChangeset for help on using the changeset viewer.