Make WordPress Core

Ticket #13691: get_template_part.5.diff

File get_template_part.5.diff, 6.3 KB (added by wjm, 14 years ago)

This removes the need to add an action

  • general-template.php

     
    1919 * @since 1.5.0
    2020 * @uses do_action() Calls 'get_header' action.
    2121 *
    22  * @param string $name The name of the specialised header.
     22 * @param mixed $names The names of the specialised header templates (it can be a string, or an array of names to search for in priority order.).
    2323 */
    24 function get_header( $name = null ) {
    25         do_action( 'get_header', $name );
     24function get_header( $names = array() ) {
     25        do_action( 'get_header', $names );
    2626
    27         $templates = array();
    28         if ( isset($name) )
    29                 $templates[] = "header-{$name}.php";
    30 
    31         $templates[] = "header.php";
    32 
    3327        // Backward compat code will be removed in a future release
    34         if ('' == locate_template($templates, true))
     28        if ('' == get_template_part( 'header', $names, true ) )
    3529                load_template( WPINC . '/theme-compat/header.php');
    3630}
    3731
     
    4842 * @since 1.5.0
    4943 * @uses do_action() Calls 'get_footer' action.
    5044 *
    51  * @param string $name The name of the specialised footer.
     45 * @param mixed $names The names of the specialised footer templates (it can be a string, or an array of names to search for in priority order.).
    5246 */
    53 function get_footer( $name = null ) {
    54         do_action( 'get_footer', $name );
     47function get_footer( $names = null ) {
     48        do_action( 'get_footer', $names );
    5549
    56         $templates = array();
    57         if ( isset($name) )
    58                 $templates[] = "footer-{$name}.php";
    59 
    60         $templates[] = "footer.php";
    61 
    6250        // Backward compat code will be removed in a future release
    63         if ('' == locate_template($templates, true))
     51        if ('' == get_template_part( 'footer', $names, true ) )
    6452                load_template( WPINC . '/theme-compat/footer.php');
    6553}
    6654
     
    7765 * @since 1.5.0
    7866 * @uses do_action() Calls 'get_sidebar' action.
    7967 *
    80  * @param string $name The name of the specialised sidebar.
     68 * @param mixed $names The names of the specialised sidebar templates (it can be a string, or an array of names to search for in priority order.).
    8169 */
    82 function get_sidebar( $name = null ) {
    83         do_action( 'get_sidebar', $name );
     70function get_sidebar( $names = array() ) {
     71        do_action( 'get_sidebar', $names );
    8472
    85         $templates = array();
    86         if ( isset($name) )
    87                 $templates[] = "sidebar-{$name}.php";
    88 
    89         $templates[] = "sidebar.php";
    90 
    9173        // Backward compat code will be removed in a future release
    92         if ('' == locate_template($templates, true))
     74        if ('' == get_template_part( 'sidebar', $names ) )
    9375                load_template( WPINC . '/theme-compat/sidebar.php');
    9476}
    9577
     
    10385 * specialised part will be included. If the theme contains no {slug}.php file
    10486 * then no template will be included.
    10587 *
    106  * The template is included using require, not require_once, so you may include the
    107  * same template part multiple times.
    108  *
    10988 * For the parameter, if the file is called "{slug}-special.php" then specify
    11089 * "special".
    11190 *
    11291 * @uses locate_template()
    11392 * @since 3.0.0
    114  * @uses do_action() Calls 'get_template_part{$slug}' action.
     93 * @uses do_action() Calls 'get_template_part_{$slug}' action.
    11594 *
    11695 * @param string $slug The slug name for the generic template.
    117  * @param string $name The name of the specialised template.
     96 * @param mixed $names The names of the specialised templates (it can be a string, or an array of names to search for in priority order.).
     97 * @param bool $require_once Where to require the template file once. Default is false.
     98 * @return string The template filename if one is located.
    11899 */
    119 function get_template_part( $slug, $name = null ) {
    120         do_action( "get_template_part_{$slug}", $slug, $name );
    121 
     100function get_template_part( $slug, $names = array(), $require_once = false ) {
     101        do_action( "get_template_part_{$slug}", $slug, $names );
     102       
    122103        $templates = array();
    123         if ( isset($name) )
     104        foreach ( (array)$names as $name ) {
    124105                $templates[] = "{$slug}-{$name}.php";
    125 
     106        }
    126107        $templates[] = "{$slug}.php";
    127108
    128         locate_template($templates, true, false);
     109        return locate_template($templates, true, $require_once);
    129110}
    130111
    131112/**
  • theme.php

     
    10441044function locate_template($template_names, $load = false, $require_once = true ) {
    10451045        if ( !is_array($template_names) )
    10461046                return '';
    1047 
     1047       
    10481048        $located = '';
    10491049        foreach ( $template_names as $template_name ) {
    10501050                if ( !$template_name )
    10511051                        continue;
    1052                 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
     1052                if ( template_exists( STYLESHEETPATH, $template_name ) ) {
    10531053                        $located = STYLESHEETPATH . '/' . $template_name;
    10541054                        break;
    1055                 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
     1055                } else if ( TEMPLATEPATH != STYLESHEETPATH && template_exists( TEMPLATEPATH, $template_name ) ) {
    10561056                        $located = TEMPLATEPATH . '/' . $template_name;
    10571057                        break;
    10581058                }
     
    10651065}
    10661066
    10671067/**
     1068 * Stores template files in the cache in global $wp_templates;
     1069 *
     1070 * @param string $dir Directory location to scan.
     1071 * @return bool
     1072 */
     1073function wp_cache_templates( $dir ) {
     1074        global $wp_templates;
     1075        if ( $handle = opendir( $dir ) ) {
     1076                $wp_templates[$dir] = 'dir';
     1077                while ( false !== ($file = readdir( $handle )) ) {
     1078                        if ( is_file( $dir.'/'.$file ) && $file != '.' && $file != '..' )
     1079                                $wp_templates[$dir.'/'.$file] = true;
     1080                }
     1081                closedir( $handle );
     1082                return true;
     1083        }
     1084        return false;
     1085}
     1086
     1087/**
     1088 * Checks in the cache if a template file exists locally.
     1089 *
     1090 * @param string $dir Directory location (template path).
     1091 * @param string $file Relative template file location.
     1092 * @return bool
     1093 */
     1094function template_exists( $dir, $file ) {
     1095        if ( '' == $file || '' == $dir )
     1096                return false;
     1097       
     1098        global $wp_templates;
     1099        $path = $dir.'/'.$file;
     1100       
     1101        if ( empty( $wp_templates ) ) {
     1102                wp_cache_templates( STYLESHEETPATH );
     1103                if ( TEMPLATEPATH != STYLESHEETPATH )
     1104                        wp_cache_templates( TEMPLATEPATH );
     1105        }
     1106       
     1107        if ( isset( $wp_templates[$path] ) )
     1108                return true;
     1109
     1110        //if its a file in a subdirectory, cache that dir
     1111        if ( false !== strpos( $file, '/' ) || false !== strpos( $file, '\\' ) ) {
     1112                $dir = dirname( $path );
     1113                if ( !isset( $wp_templates[$dir] ) )
     1114                        wp_cache_templates( $dir );
     1115        }
     1116        return isset( $wp_templates[$path] );
     1117}
     1118
     1119/**
    10681120 * Require the template file with WordPress environment.
    10691121 *
    10701122 * The globals are set up for the template file to ensure that the WordPress