Ticket #13691: get_template_part.4.diff
File get_template_part.4.diff, 6.0 KB (added by , 14 years ago) |
---|
-
general-template.php
19 19 * @since 1.5.0 20 20 * @uses do_action() Calls 'get_header' action. 21 21 * 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.). 23 23 */ 24 function get_header( $name = null) {25 do_action( 'get_header', $name );24 function get_header( $names = array() ) { 25 do_action( 'get_header', $names ); 26 26 27 $templates = array();28 if ( isset($name) )29 $templates[] = "header-{$name}.php";30 31 $templates[] = "header.php";32 33 27 // Backward compat code will be removed in a future release 34 if ('' == locate_template($templates, true))28 if ('' == get_template_part( 'header', $names, true ) ) 35 29 load_template( WPINC . '/theme-compat/header.php'); 36 30 } 37 31 … … 48 42 * @since 1.5.0 49 43 * @uses do_action() Calls 'get_footer' action. 50 44 * 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.). 52 46 */ 53 function get_footer( $name = null ) {54 do_action( 'get_footer', $name );47 function get_footer( $names = null ) { 48 do_action( 'get_footer', $names ); 55 49 56 $templates = array();57 if ( isset($name) )58 $templates[] = "footer-{$name}.php";59 60 $templates[] = "footer.php";61 62 50 // Backward compat code will be removed in a future release 63 if ('' == locate_template($templates, true))51 if ('' == get_template_part( 'footer', $names, true ) ) 64 52 load_template( WPINC . '/theme-compat/footer.php'); 65 53 } 66 54 … … 77 65 * @since 1.5.0 78 66 * @uses do_action() Calls 'get_sidebar' action. 79 67 * 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.). 81 69 */ 82 function get_sidebar( $name = null) {83 do_action( 'get_sidebar', $name );70 function get_sidebar( $names = array() ) { 71 do_action( 'get_sidebar', $names ); 84 72 85 $templates = array();86 if ( isset($name) )87 $templates[] = "sidebar-{$name}.php";88 89 $templates[] = "sidebar.php";90 91 73 // Backward compat code will be removed in a future release 92 if ('' == locate_template($templates, true))74 if ('' == get_template_part( 'sidebar', $names ) ) 93 75 load_template( WPINC . '/theme-compat/sidebar.php'); 94 76 } 95 77 … … 111 93 * 112 94 * @uses locate_template() 113 95 * @since 3.0.0 114 * @uses do_action() Calls 'get_template_part {$slug}' action.96 * @uses do_action() Calls 'get_template_part_{$slug}' action. 115 97 * 116 98 * @param string $slug The slug name for the generic template. 117 * @param string $name The name of the specialised template.99 * @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.). 118 100 */ 119 function get_template_part( $slug, $name = null) {120 do_action( "get_template_part_{$slug}", $slug, $name );121 101 function get_template_part( $slug, $names = array(), $require_once = false ) { 102 do_action( "get_template_part_{$slug}", $slug, $names ); 103 122 104 $templates = array(); 123 if ( isset($name) )105 foreach ( (array)$names as $name ) { 124 106 $templates[] = "{$slug}-{$name}.php"; 125 107 } 126 108 $templates[] = "{$slug}.php"; 127 109 128 locate_template($templates, true, false);110 return locate_template($templates, true, $require_once); 129 111 } 130 112 131 113 /** -
theme.php
1044 1044 function locate_template($template_names, $load = false, $require_once = true ) { 1045 1045 if ( !is_array($template_names) ) 1046 1046 return ''; 1047 1047 1048 1048 $located = ''; 1049 1049 foreach ( $template_names as $template_name ) { 1050 1050 if ( !$template_name ) 1051 1051 continue; 1052 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {1052 if ( template_exists( STYLESHEETPATH, $template_name ) ) { 1053 1053 $located = STYLESHEETPATH . '/' . $template_name; 1054 1054 break; 1055 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {1055 } else if ( TEMPLATEPATH != STYLESHEETPATH && template_exists( TEMPLATEPATH, $template_name ) ) { 1056 1056 $located = TEMPLATEPATH . '/' . $template_name; 1057 1057 break; 1058 1058 } … … 1064 1064 return $located; 1065 1065 } 1066 1066 1067 // Cache the parent and child theme's template paths 1068 add_action( 'init', 'wp_cache_template_paths', 1 ); 1069 function wp_cache_template_paths() { 1070 foreach ( array( STYLESHEETPATH, TEMPLATEPATH ) as $dir ) 1071 wp_cache_templates( $dir ); 1072 global $wp_templates; 1073 } 1074 1067 1075 /** 1076 * Stores template files in the cache in global $wp_templates; 1077 * 1078 * @param string $dir Directory location to scan. 1079 * @return bool 1080 */ 1081 function wp_cache_templates( $dir ) { 1082 global $wp_templates; 1083 if ( $handle = opendir( $dir ) ) { 1084 $wp_templates[$dir] = 'dir'; 1085 while ( false !== ($file = readdir( $handle )) ) { 1086 if ( is_file( $dir.'/'.$file ) && $file != '.' && $file != '..' ) 1087 $wp_templates[$dir.'/'.$file] = true; 1088 } 1089 closedir( $handle ); 1090 return true; 1091 } 1092 return false; 1093 } 1094 1095 /** 1096 * Checks in the cache if a template file exists locally. 1097 * 1098 * @param string $dir Directory location (template path). 1099 * @param string $file Relative template file location. 1100 * @return bool 1101 */ 1102 function template_exists( $dir, $file ) { 1103 if ( '' == $file || '' == $dir ) 1104 return false; 1105 1106 global $wp_templates; 1107 $path = $dir.'/'.$file; 1108 1109 if ( isset( $wp_templates[$path] ) ) 1110 return true; 1111 1112 //if its a file in a subdirectory, scan that dir 1113 if ( false !== strpos( $file, '/' ) || false !== strpos( $file, '\\' ) ) { 1114 $dir = dirname( $path ); 1115 if ( !isset( $wp_templates[$dir] ) ) 1116 wp_cache_templates( $dir ); 1117 } 1118 return isset( $wp_templates[$path] ); 1119 } 1120 1121 /** 1068 1122 * Require the template file with WordPress environment. 1069 1123 * 1070 1124 * The globals are set up for the template file to ensure that the WordPress