Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.