-
+ FB6D4F7601223DC083B39A0865987827E5D806DEA9C8FFC330C2382FAEBD3E2C870C9E8236FD3B7750821231AF289A4ADB3B005AB722B845541EE17645193284
mp-wp/wp-includes/theme.php
(0 . 0)(1 . 1113)
153935 <?php
153936 /**
153937 * Theme, template, and stylesheet functions.
153938 *
153939 * @package WordPress
153940 * @subpackage Template
153941 */
153942
153943 /**
153944 * Retrieve name of the current stylesheet.
153945 *
153946 * The theme name that the administrator has currently set the front end theme
153947 * as.
153948 *
153949 * For all extensive purposes, the template name and the stylesheet name are
153950 * going to be the same for most cases.
153951 *
153952 * @since 1.5.0
153953 * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
153954 *
153955 * @return string Stylesheet name.
153956 */
153957 function get_stylesheet() {
153958 return apply_filters('stylesheet', get_option('stylesheet'));
153959 }
153960
153961 /**
153962 * Retrieve stylesheet directory path for current theme.
153963 *
153964 * @since 1.5.0
153965 * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
153966 *
153967 * @return string Path to current theme directory.
153968 */
153969 function get_stylesheet_directory() {
153970 $stylesheet = get_stylesheet();
153971 $stylesheet_dir = get_theme_root() . "/$stylesheet";
153972 return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
153973 }
153974
153975 /**
153976 * Retrieve stylesheet directory URI.
153977 *
153978 * @since 1.5.0
153979 *
153980 * @return string
153981 */
153982 function get_stylesheet_directory_uri() {
153983 $stylesheet = get_stylesheet();
153984 $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
153985 return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
153986 }
153987
153988 /**
153989 * Retrieve URI of current theme stylesheet.
153990 *
153991 * The stylesheet file name is 'style.css' which is appended to {@link
153992 * get_stylesheet_directory_uri() stylesheet directory URI} path.
153993 *
153994 * @since 1.5.0
153995 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
153996 *
153997 * @return string
153998 */
153999 function get_stylesheet_uri() {
154000 $stylesheet_dir_uri = get_stylesheet_directory_uri();
154001 $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
154002 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
154003 }
154004
154005 /**
154006 * Retrieve localized stylesheet URI.
154007 *
154008 * The stylesheet directory for the localized stylesheet files are located, by
154009 * default, in the base theme directory. The name of the locale file will be the
154010 * locale followed by '.css'. If that does not exist, then the text direction
154011 * stylesheet will be checked for existence, for example 'ltr.css'.
154012 *
154013 * The theme may change the location of the stylesheet directory by either using
154014 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
154015 * If you want to change the location of the stylesheet files for the entire
154016 * WordPress workflow, then change the former. If you just have the locale in a
154017 * separate folder, then change the latter.
154018 *
154019 * @since 2.1.0
154020 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
154021 *
154022 * @return string
154023 */
154024 function get_locale_stylesheet_uri() {
154025 global $wp_locale;
154026 $stylesheet_dir_uri = get_stylesheet_directory_uri();
154027 $dir = get_stylesheet_directory();
154028 $locale = get_locale();
154029 if ( file_exists("$dir/$locale.css") )
154030 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
154031 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
154032 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
154033 else
154034 $stylesheet_uri = '';
154035 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
154036 }
154037
154038 /**
154039 * Retrieve name of the current theme.
154040 *
154041 * @since 1.5.0
154042 * @uses apply_filters() Calls 'template' filter on template option.
154043 *
154044 * @return string Template name.
154045 */
154046 function get_template() {
154047 return apply_filters('template', get_option('template'));
154048 }
154049
154050 /**
154051 * Retrieve current theme directory.
154052 *
154053 * @since 1.5.0
154054 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
154055 *
154056 * @return string Template directory path.
154057 */
154058 function get_template_directory() {
154059 $template = get_template();
154060 $template_dir = get_theme_root() . "/$template";
154061 return apply_filters('template_directory', $template_dir, $template);
154062 }
154063
154064 /**
154065 * Retrieve theme directory URI.
154066 *
154067 * @since 1.5.0
154068 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
154069 *
154070 * @return string Template directory URI.
154071 */
154072 function get_template_directory_uri() {
154073 $template = get_template();
154074 $template_dir_uri = get_theme_root_uri() . "/$template";
154075 return apply_filters('template_directory_uri', $template_dir_uri, $template);
154076 }
154077
154078 /**
154079 * Retrieve theme data from parsed theme file.
154080 *
154081 * The description will have the tags filtered with the following HTML elements
154082 * whitelisted. The <b>'a'</b> element with the <em>href</em> and <em>title</em>
154083 * attributes. The <b>abbr</b> element with the <em>title</em> attribute. The
154084 * <b>acronym<b> element with the <em>title</em> attribute allowed. The
154085 * <b>code</b>, <b>em</b>, and <b>strong</b> elements also allowed.
154086 *
154087 * The style.css file must contain theme name, theme URI, and description. The
154088 * data can also contain author URI, author, template (parent template),
154089 * version, status, and finally tags. Some of these are not used by WordPress
154090 * administration panels, but are used by theme directory web sites which list
154091 * the theme.
154092 *
154093 * @since 1.5.0
154094 *
154095 * @param string $theme_file Theme file path.
154096 * @return array Theme data.
154097 */
154098 function get_theme_data( $theme_file ) {
154099 $themes_allowed_tags = array(
154100 'a' => array(
154101 'href' => array(),'title' => array()
154102 ),
154103 'abbr' => array(
154104 'title' => array()
154105 ),
154106 'acronym' => array(
154107 'title' => array()
154108 ),
154109 'code' => array(),
154110 'em' => array(),
154111 'strong' => array()
154112 );
154113
154114 $theme_data = implode( '', file( $theme_file ) );
154115 $theme_data = str_replace ( '\r', '\n', $theme_data );
154116 preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name );
154117 preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri );
154118 preg_match( '|Description:(.*)$|mi', $theme_data, $description );
154119
154120 if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
154121 $author_uri = clean_url( trim( $author_uri[1]) );
154122 else
154123 $author_uri = '';
154124
154125 if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
154126 $template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
154127 else
154128 $template = '';
154129
154130 if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
154131 $version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
154132 else
154133 $version = '';
154134
154135 if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
154136 $status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
154137 else
154138 $status = 'publish';
154139
154140 if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
154141 $tags = array_map( 'trim', explode( ',', wp_kses( trim( $tags[1] ), array() ) ) );
154142 else
154143 $tags = array();
154144
154145 $name = $theme = wp_kses( trim( $theme_name[1] ), $themes_allowed_tags );
154146 $theme_uri = clean_url( trim( $theme_uri[1] ) );
154147 $description = wptexturize( wp_kses( trim( $description[1] ), $themes_allowed_tags ) );
154148
154149 if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
154150 if ( empty( $author_uri ) ) {
154151 $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
154152 } else {
154153 $author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( trim( $author_name[1] ), $themes_allowed_tags ) );
154154 }
154155 } else {
154156 $author = __('Anonymous');
154157 }
154158
154159 return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
154160 }
154161
154162 /**
154163 * Retrieve list of themes with theme data in theme directory.
154164 *
154165 * The theme is broken, if it doesn't have a parent theme and is missing either
154166 * style.css and, or index.php. If the theme has a parent theme then it is
154167 * broken, if it is missing style.css; index.php is optional. The broken theme
154168 * list is saved in the {@link $wp_broken_themes} global, which is displayed on
154169 * the theme list in the administration panels.
154170 *
154171 * @since 1.5.0
154172 * @global array $wp_broken_themes Stores the broken themes.
154173 * @global array $wp_themes Stores the working themes.
154174 *
154175 * @return array Theme list with theme data.
154176 */
154177 function get_themes() {
154178 global $wp_themes, $wp_broken_themes;
154179
154180 if ( isset($wp_themes) )
154181 return $wp_themes;
154182
154183 $themes = array();
154184 $wp_broken_themes = array();
154185 $theme_loc = $theme_root = get_theme_root();
154186 if ( '/' != WP_CONTENT_DIR ) // don't want to replace all forward slashes, see Trac #4541
154187 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
154188
154189 // Files in wp-content/themes directory and one subdir down
154190 $themes_dir = @ opendir($theme_root);
154191 if ( !$themes_dir )
154192 return false;
154193
154194 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
154195 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
154196 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
154197 continue;
154198 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
154199 $found_stylesheet = false;
154200 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
154201 if ( $theme_file == 'style.css' ) {
154202 $theme_files[] = $theme_dir . '/' . $theme_file;
154203 $found_stylesheet = true;
154204 break;
154205 }
154206 }
154207 @closedir($stylish_dir);
154208 if ( !$found_stylesheet ) { // look for themes in that dir
154209 $subdir = "$theme_root/$theme_dir";
154210 $subdir_name = $theme_dir;
154211 $theme_subdir = @ opendir( $subdir );
154212 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
154213 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
154214 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
154215 continue;
154216 $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
154217 $found_stylesheet = false;
154218 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
154219 if ( $theme_file == 'style.css' ) {
154220 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
154221 $found_stylesheet = true;
154222 break;
154223 }
154224 }
154225 @closedir($stylish_dir);
154226 }
154227 }
154228 @closedir($theme_subdir);
154229 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
154230 }
154231 }
154232 }
154233 if ( is_dir( $theme_dir ) )
154234 @closedir( $theme_dir );
154235
154236 if ( !$themes_dir || !$theme_files )
154237 return $themes;
154238
154239 sort($theme_files);
154240
154241 foreach ( (array) $theme_files as $theme_file ) {
154242 if ( !is_readable("$theme_root/$theme_file") ) {
154243 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
154244 continue;
154245 }
154246
154247 $theme_data = get_theme_data("$theme_root/$theme_file");
154248
154249 $name = $theme_data['Name'];
154250 $title = $theme_data['Title'];
154251 $description = wptexturize($theme_data['Description']);
154252 $version = $theme_data['Version'];
154253 $author = $theme_data['Author'];
154254 $template = $theme_data['Template'];
154255 $stylesheet = dirname($theme_file);
154256
154257 $screenshot = false;
154258 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
154259 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
154260 $screenshot = "screenshot.$ext";
154261 break;
154262 }
154263 }
154264
154265 if ( empty($name) ) {
154266 $name = dirname($theme_file);
154267 $title = $name;
154268 }
154269
154270 if ( empty($template) ) {
154271 if ( file_exists(dirname("$theme_root/$theme_file/index.php")) )
154272 $template = dirname($theme_file);
154273 else
154274 continue;
154275 }
154276
154277 $template = trim($template);
154278
154279 if ( !file_exists("$theme_root/$template/index.php") ) {
154280 $parent_dir = dirname(dirname($theme_file));
154281 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
154282 $template = "$parent_dir/$template";
154283 } else {
154284 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
154285 continue;
154286 }
154287 }
154288
154289 $stylesheet_files = array();
154290 $template_files = array();
154291
154292 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
154293 if ( $stylesheet_dir ) {
154294 while ( ($file = $stylesheet_dir->read()) !== false ) {
154295 if ( !preg_match('|^\.+$|', $file) ) {
154296 if ( preg_match('|\.css$|', $file) )
154297 $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
154298 elseif ( preg_match('|\.php$|', $file) )
154299 $template_files[] = "$theme_loc/$stylesheet/$file";
154300 }
154301 }
154302 }
154303
154304 $template_dir = @ dir("$theme_root/$template");
154305 if ( $template_dir ) {
154306 while(($file = $template_dir->read()) !== false) {
154307 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
154308 $template_files[] = "$theme_loc/$template/$file";
154309 }
154310 }
154311
154312 $template_dir = dirname($template_files[0]);
154313 $stylesheet_dir = dirname($stylesheet_files[0]);
154314
154315 if ( empty($template_dir) )
154316 $template_dir = '/';
154317 if ( empty($stylesheet_dir) )
154318 $stylesheet_dir = '/';
154319
154320 // Check for theme name collision. This occurs if a theme is copied to
154321 // a new theme directory and the theme header is not updated. Whichever
154322 // theme is first keeps the name. Subsequent themes get a suffix applied.
154323 // The Default and Classic themes always trump their pretenders.
154324 if ( isset($themes[$name]) ) {
154325 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
154326 ('default' == $stylesheet || 'classic' == $stylesheet) ) {
154327 // If another theme has claimed to be one of our default themes, move
154328 // them aside.
154329 $suffix = $themes[$name]['Stylesheet'];
154330 $new_name = "$name/$suffix";
154331 $themes[$new_name] = $themes[$name];
154332 $themes[$new_name]['Name'] = $new_name;
154333 } else {
154334 $name = "$name/$stylesheet";
154335 }
154336 }
154337
154338 $themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot, 'Tags' => $theme_data['Tags']);
154339 }
154340
154341 // Resolve theme dependencies.
154342 $theme_names = array_keys($themes);
154343
154344 foreach ( (array) $theme_names as $theme_name ) {
154345 $themes[$theme_name]['Parent Theme'] = '';
154346 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
154347 foreach ( (array) $theme_names as $parent_theme_name ) {
154348 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
154349 $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
154350 break;
154351 }
154352 }
154353 }
154354 }
154355
154356 $wp_themes = $themes;
154357
154358 return $themes;
154359 }
154360
154361 /**
154362 * Retrieve theme data.
154363 *
154364 * @since 1.5.0
154365 *
154366 * @param string $theme Theme name.
154367 * @return array|null Null, if theme name does not exist. Theme data, if exists.
154368 */
154369 function get_theme($theme) {
154370 $themes = get_themes();
154371
154372 if ( array_key_exists($theme, $themes) )
154373 return $themes[$theme];
154374
154375 return null;
154376 }
154377
154378 /**
154379 * Retrieve current theme display name.
154380 *
154381 * If the 'current_theme' option has already been set, then it will be returned
154382 * instead. If it is not set, then each theme will be iterated over until both
154383 * the current stylesheet and current template name.
154384 *
154385 * @since 1.5.0
154386 *
154387 * @return string
154388 */
154389 function get_current_theme() {
154390 if ( $theme = get_option('current_theme') )
154391 return $theme;
154392
154393 $themes = get_themes();
154394 $theme_names = array_keys($themes);
154395 $current_template = get_option('template');
154396 $current_stylesheet = get_option('stylesheet');
154397 $current_theme = 'WordPress Default';
154398
154399 if ( $themes ) {
154400 foreach ( (array) $theme_names as $theme_name ) {
154401 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
154402 $themes[$theme_name]['Template'] == $current_template ) {
154403 $current_theme = $themes[$theme_name]['Name'];
154404 break;
154405 }
154406 }
154407 }
154408
154409 update_option('current_theme', $current_theme);
154410
154411 return $current_theme;
154412 }
154413
154414 /**
154415 * Retrieve path to themes directory.
154416 *
154417 * Does not have trailing slash.
154418 *
154419 * @since 1.5.0
154420 * @uses apply_filters() Calls 'theme_root' filter on path.
154421 *
154422 * @return string Theme path.
154423 */
154424 function get_theme_root() {
154425 return apply_filters('theme_root', WP_CONTENT_DIR . "/themes");
154426 }
154427
154428 /**
154429 * Retrieve URI for themes directory.
154430 *
154431 * Does not have trailing slash.
154432 *
154433 * @since 1.5.0
154434 *
154435 * @return string Themes URI.
154436 */
154437 function get_theme_root_uri() {
154438 return apply_filters('theme_root_uri', content_url('themes'), get_option('siteurl'));
154439 }
154440
154441 /**
154442 * Retrieve path to file without the use of extension.
154443 *
154444 * Used to quickly retrieve the path of file without including the file
154445 * extension. It will also check the parent template, if the file exists, with
154446 * the use of {@link locate_template()}. Allows for more generic file location
154447 * without the use of the other get_*_template() functions.
154448 *
154449 * Can be used with include() or require() to retrieve path.
154450 * <code>
154451 * if( '' != get_query_template( '404' ) )
154452 * include( get_query_template( '404' ) );
154453 * </code>
154454 * or the same can be accomplished with
154455 * <code>
154456 * if( '' != get_404_template() )
154457 * include( get_404_template() );
154458 * </code>
154459 *
154460 * @since 1.5.0
154461 *
154462 * @param string $type Filename without extension.
154463 * @return string Full path to file.
154464 */
154465 function get_query_template($type) {
154466 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
154467 return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
154468 }
154469
154470 /**
154471 * Retrieve path of 404 template in current or parent template.
154472 *
154473 * @since 1.5.0
154474 *
154475 * @return string
154476 */
154477 function get_404_template() {
154478 return get_query_template('404');
154479 }
154480
154481 /**
154482 * Retrieve path of archive template in current or parent template.
154483 *
154484 * @since 1.5.0
154485 *
154486 * @return string
154487 */
154488 function get_archive_template() {
154489 return get_query_template('archive');
154490 }
154491
154492 /**
154493 * Retrieve path of author template in current or parent template.
154494 *
154495 * @since 1.5.0
154496 *
154497 * @return string
154498 */
154499 function get_author_template() {
154500 return get_query_template('author');
154501 }
154502
154503 /**
154504 * Retrieve path of category template in current or parent template.
154505 *
154506 * Works by retrieving the current category ID, for example 'category-1.php' and
154507 * will fallback to category.php template, if the ID category file doesn't
154508 * exist.
154509 *
154510 * @since 1.5.0
154511 * @uses apply_filters() Calls 'category_template' on file path of category template.
154512 *
154513 * @return string
154514 */
154515 function get_category_template() {
154516 $template = locate_template(array("category-" . absint( get_query_var('cat') ) . '.php', 'category.php'));
154517 return apply_filters('category_template', $template);
154518 }
154519
154520 /**
154521 * Retrieve path of tag template in current or parent template.
154522 *
154523 * Works by retrieving the current tag name, for example 'tag-wordpress.php' and will
154524 * fallback to tag.php template, if the name tag file doesn't exist.
154525 *
154526 * @since 2.3.0
154527 * @uses apply_filters() Calls 'tag_template' on file path of tag template.
154528 *
154529 * @return string
154530 */
154531 function get_tag_template() {
154532 $template = locate_template(array("tag-" . get_query_var('tag') . '.php', 'tag.php'));
154533 return apply_filters('tag_template', $template);
154534 }
154535
154536 /**
154537 * Retrieve path of taxonomy template in current or parent template.
154538 *
154539 * Retrieves the taxonomy and term, if term is available. The template is
154540 * prepended with 'taxonomy-' and followed by both the taxonomy string and
154541 * the taxonomy string followed by a dash and then followed by the term.
154542 *
154543 * The taxonomy and term template is checked and used first, if it exists.
154544 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
154545 * template is used. If none of the files exist, then it will fall back on to
154546 * index.php.
154547 *
154548 * @since unknown (2.6.0 most likely)
154549 * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
154550 *
154551 * @return string
154552 */
154553 function get_taxonomy_template() {
154554 $taxonomy = get_query_var('taxonomy');
154555 $term = get_query_var('term');
154556
154557 $templates = array();
154558 if ( $taxonomy && $term )
154559 $templates[] = "taxonomy-$taxonomy-$term.php";
154560 if ( $taxonomy )
154561 $templates[] = "taxonomy-$taxonomy.php";
154562
154563 $templates[] = "taxonomy.php";
154564
154565 $template = locate_template($templates);
154566 return apply_filters('taxonomy_template', $template);
154567 }
154568
154569 /**
154570 * Retrieve path of date template in current or parent template.
154571 *
154572 * @since 1.5.0
154573 *
154574 * @return string
154575 */
154576 function get_date_template() {
154577 return get_query_template('date');
154578 }
154579
154580 /**
154581 * Retrieve path of home template in current or parent template.
154582 *
154583 * Attempts to locate 'home.php' first before falling back to 'index.php'.
154584 *
154585 * @since 1.5.0
154586 * @uses apply_filters() Calls 'home_template' on file path of home template.
154587 *
154588 * @return string
154589 */
154590 function get_home_template() {
154591 $template = locate_template(array('home.php', 'index.php'));
154592 return apply_filters('home_template', $template);
154593 }
154594
154595 /**
154596 * Retrieve path of page template in current or parent template.
154597 *
154598 * First attempt is to look for the file in the '_wp_page_template' page meta
154599 * data. The second attempt, if the first has a file and is not empty, is to
154600 * look for 'page.php'.
154601 *
154602 * @since 1.5.0
154603 *
154604 * @return string
154605 */
154606 function get_page_template() {
154607 global $wp_query;
154608
154609 $id = (int) $wp_query->post->ID;
154610 $template = get_post_meta($id, '_wp_page_template', true);
154611
154612 if ( 'default' == $template )
154613 $template = '';
154614
154615 $templates = array();
154616 if ( !empty($template) && !validate_file($template) )
154617 $templates[] = $template;
154618
154619 $templates[] = "page.php";
154620
154621 return apply_filters('page_template', locate_template($templates));
154622 }
154623
154624 /**
154625 * Retrieve path of paged template in current or parent template.
154626 *
154627 * @since 1.5.0
154628 *
154629 * @return string
154630 */
154631 function get_paged_template() {
154632 return get_query_template('paged');
154633 }
154634
154635 /**
154636 * Retrieve path of search template in current or parent template.
154637 *
154638 * @since 1.5.0
154639 *
154640 * @return string
154641 */
154642 function get_search_template() {
154643 return get_query_template('search');
154644 }
154645
154646 /**
154647 * Retrieve path of single template in current or parent template.
154648 *
154649 * @since 1.5.0
154650 *
154651 * @return string
154652 */
154653 function get_single_template() {
154654 return get_query_template('single');
154655 }
154656
154657 /**
154658 * Retrieve path of attachment template in current or parent template.
154659 *
154660 * The attachment path first checks if the first part of the mime type exists.
154661 * The second check is for the second part of the mime type. The last check is
154662 * for both types separated by an underscore. If neither are found then the file
154663 * 'attachment.php' is checked and returned.
154664 *
154665 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
154666 * finally 'text_plain.php'.
154667 *
154668 * @since 2.0.0
154669 *
154670 * @return string
154671 */
154672 function get_attachment_template() {
154673 global $posts;
154674 $type = explode('/', $posts[0]->post_mime_type);
154675 if ( $template = get_query_template($type[0]) )
154676 return $template;
154677 elseif ( $template = get_query_template($type[1]) )
154678 return $template;
154679 elseif ( $template = get_query_template("$type[0]_$type[1]") )
154680 return $template;
154681 else
154682 return get_query_template('attachment');
154683 }
154684
154685 /**
154686 * Retrieve path of comment popup template in current or parent template.
154687 *
154688 * Checks for comment popup template in current template, if it exists or in the
154689 * parent template. If it doesn't exist, then it retrieves the comment-popup.php
154690 * file from the default theme. The default theme must then exist for it to
154691 * work.
154692 *
154693 * @since 1.5.0
154694 * @uses apply_filters() Calls 'comments_popup_template' filter on path.
154695 *
154696 * @return string
154697 */
154698 function get_comments_popup_template() {
154699 $template = locate_template(array("comments-popup.php"));
154700 if ('' == $template)
154701 $template = get_theme_root() . '/default/comments-popup.php';
154702
154703 return apply_filters('comments_popup_template', $template);
154704 }
154705
154706 /**
154707 * Retrieve the name of the highest priority template file that exists.
154708 *
154709 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
154710 * inherit from a parent theme can just overload one file.
154711 *
154712 * @since 2.7.0
154713 *
154714 * @param array $template_names Array of template files to search for in priority order.
154715 * @param bool $load If true the template file will be loaded if it is found.
154716 * @return string The template filename if one is located.
154717 */
154718 function locate_template($template_names, $load = false) {
154719 if (!is_array($template_names))
154720 return '';
154721
154722 $located = '';
154723 foreach($template_names as $template_name) {
154724 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
154725 $located = STYLESHEETPATH . '/' . $template_name;
154726 break;
154727 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
154728 $located = TEMPLATEPATH . '/' . $template_name;
154729 break;
154730 }
154731 }
154732
154733 if ($load && '' != $located)
154734 load_template($located);
154735
154736 return $located;
154737 }
154738
154739 /**
154740 * Require once the template file with WordPress environment.
154741 *
154742 * The globals are set up for the template file to ensure that the WordPress
154743 * environment is available from within the function. The query variables are
154744 * also available.
154745 *
154746 * @since 1.5.0
154747 *
154748 * @param string $_template_file Path to template file.
154749 */
154750 function load_template($_template_file) {
154751 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
154752
154753 if ( is_array($wp_query->query_vars) )
154754 extract($wp_query->query_vars, EXTR_SKIP);
154755
154756 require_once($_template_file);
154757 }
154758
154759 /**
154760 * Display localized stylesheet link element.
154761 *
154762 * @since 2.1.0
154763 */
154764 function locale_stylesheet() {
154765 $stylesheet = get_locale_stylesheet_uri();
154766 if ( empty($stylesheet) )
154767 return;
154768 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
154769 }
154770
154771 /**
154772 * Start preview theme output buffer.
154773 *
154774 * Will only preform task if the user has permissions and template and preview
154775 * query variables exist.
154776 *
154777 * @since 2.5.0
154778 */
154779 function preview_theme() {
154780 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
154781 return;
154782
154783 if ( !current_user_can( 'switch_themes' ) )
154784 return;
154785
154786 $_GET['template'] = preg_replace('|[^a-z0-9_.-]|i', '', $_GET['template']);
154787
154788 if ( validate_file($_GET['template']) )
154789 return;
154790
154791 add_filter('template', create_function('', "return '{$_GET['template']}';") );
154792
154793 if ( isset($_GET['stylesheet']) ) {
154794 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_.-]|i', '', $_GET['stylesheet']);
154795 if ( validate_file($_GET['stylesheet']) )
154796 return;
154797 add_filter('stylesheet', create_function('', "return '{$_GET['stylesheet']}';") );
154798 }
154799
154800 ob_start( 'preview_theme_ob_filter' );
154801 }
154802 add_action('setup_theme', 'preview_theme');
154803
154804 /**
154805 * Callback function for ob_start() to capture all links in the theme.
154806 *
154807 * @since unknown
154808 * @access private
154809 *
154810 * @param string $content
154811 * @return string
154812 */
154813 function preview_theme_ob_filter( $content ) {
154814 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
154815 }
154816
154817 /**
154818 * Manipulates preview theme links in order to control and maintain location.
154819 *
154820 * Callback function for preg_replace_callback() to accept and filter matches.
154821 *
154822 * @since unknown
154823 * @access private
154824 *
154825 * @param array $matches
154826 * @return string
154827 */
154828 function preview_theme_ob_filter_callback( $matches ) {
154829 if ( strpos($matches[4], 'onclick') !== false )
154830 $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if its escaped by \ to prevent breaking mid-attribute.
154831 if (
154832 ( false !== strpos($matches[3], '/wp-admin/') )
154833 ||
154834 ( false !== strpos($matches[3], '://') && 0 !== strpos($matches[3], get_option('home')) )
154835 ||
154836 ( false !== strpos($matches[3], '/feed/') )
154837 ||
154838 ( false !== strpos($matches[3], '/trackback/') )
154839 )
154840 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
154841
154842 $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
154843 if ( 0 === strpos($link, 'preview=1') )
154844 $link = "?$link";
154845 return $matches[1] . attribute_escape( $link ) . $matches[4];
154846 }
154847
154848 /**
154849 * Switches current theme to new template and stylesheet names.
154850 *
154851 * @since unknown
154852 * @uses do_action() Calls 'switch_theme' action on updated theme display name.
154853 *
154854 * @param string $template Template name
154855 * @param string $stylesheet Stylesheet name.
154856 */
154857 function switch_theme($template, $stylesheet) {
154858 update_option('template', $template);
154859 update_option('stylesheet', $stylesheet);
154860 delete_option('current_theme');
154861 $theme = get_current_theme();
154862 do_action('switch_theme', $theme);
154863 }
154864
154865 /**
154866 * Checks that current theme files 'index.php' and 'style.css' exists.
154867 *
154868 * Does not check the 'default' theme. The 'default' theme should always exist
154869 * or should have another theme renamed to that template name and directory
154870 * path. Will switch theme to default if current theme does not validate.
154871 * You can use the 'validate_current_theme' filter to return FALSE to
154872 * disable this functionality.
154873 *
154874 * @since 1.5.0
154875 *
154876 * @return bool
154877 */
154878 function validate_current_theme() {
154879 // Don't validate during an install/upgrade.
154880 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
154881 return true;
154882
154883 if ( get_template() != 'default' && !file_exists(get_template_directory() . '/index.php') ) {
154884 switch_theme('default', 'default');
154885 return false;
154886 }
154887
154888 if ( get_stylesheet() != 'default' && !file_exists(get_template_directory() . '/style.css') ) {
154889 switch_theme('default', 'default');
154890 return false;
154891 }
154892
154893 return true;
154894 }
154895
154896 /**
154897 * Retrieve theme modification value for the current theme.
154898 *
154899 * If the modification name does not exist, then the $default will be passed
154900 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
154901 * string the template directory URI and the second string the stylesheet
154902 * directory URI.
154903 *
154904 * @since 2.1.0
154905 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
154906 *
154907 * @param string $name Theme modification name.
154908 * @param bool|string $default
154909 * @return string
154910 */
154911 function get_theme_mod($name, $default = false) {
154912 $theme = get_current_theme();
154913
154914 $mods = get_option("mods_$theme");
154915
154916 if ( isset($mods[$name]) )
154917 return apply_filters( "theme_mod_$name", $mods[$name] );
154918
154919 return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()) );
154920 }
154921
154922 /**
154923 * Update theme modification value for the current theme.
154924 *
154925 * @since 2.1.0
154926 *
154927 * @param string $name Theme modification name.
154928 * @param string $value theme modification value.
154929 */
154930 function set_theme_mod($name, $value) {
154931 $theme = get_current_theme();
154932
154933 $mods = get_option("mods_$theme");
154934
154935 $mods[$name] = $value;
154936
154937 update_option("mods_$theme", $mods);
154938 wp_cache_delete("mods_$theme", 'options');
154939 }
154940
154941 /**
154942 * Remove theme modification name from current theme list.
154943 *
154944 * If removing the name also removes all elements, then the entire option will
154945 * be removed.
154946 *
154947 * @since 2.1.0
154948 *
154949 * @param string $name Theme modification name.
154950 * @return null
154951 */
154952 function remove_theme_mod( $name ) {
154953 $theme = get_current_theme();
154954
154955 $mods = get_option("mods_$theme");
154956
154957 if ( !isset($mods[$name]) )
154958 return;
154959
154960 unset($mods[$name]);
154961
154962 if ( empty($mods) )
154963 return remove_theme_mods();
154964
154965 update_option("mods_$theme", $mods);
154966 wp_cache_delete("mods_$theme", 'options');
154967 }
154968
154969 /**
154970 * Remove theme modifications option for current theme.
154971 *
154972 * @since 2.1.0
154973 */
154974 function remove_theme_mods() {
154975 $theme = get_current_theme();
154976
154977 delete_option("mods_$theme");
154978 }
154979
154980 /**
154981 * Retrieve text color for custom header.
154982 *
154983 * @since 2.1.0
154984 * @uses HEADER_TEXTCOLOR
154985 *
154986 * @return string
154987 */
154988 function get_header_textcolor() {
154989 return get_theme_mod('header_textcolor', HEADER_TEXTCOLOR);
154990 }
154991
154992 /**
154993 * Display text color for custom header.
154994 *
154995 * @since 2.1.0
154996 */
154997 function header_textcolor() {
154998 echo get_header_textcolor();
154999 }
155000
155001 /**
155002 * Retrieve header image for custom header.
155003 *
155004 * @since 2.1.0
155005 * @uses HEADER_IMAGE
155006 *
155007 * @return string
155008 */
155009 function get_header_image() {
155010 return get_theme_mod('header_image', HEADER_IMAGE);
155011 }
155012
155013 /**
155014 * Display header image path.
155015 *
155016 * @since 2.1.0
155017 */
155018 function header_image() {
155019 echo get_header_image();
155020 }
155021
155022 /**
155023 * Add callbacks for image header display.
155024 *
155025 * The parameter $header_callback callback will be required to display the
155026 * content for the 'wp_head' action. The parameter $admin_header_callback
155027 * callback will be added to Custom_Image_Header class and that will be added
155028 * to the 'admin_menu' action.
155029 *
155030 * @since 2.1.0
155031 * @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
155032 *
155033 * @param callback $header_callback Call on 'wp_head' action.
155034 * @param callback $admin_header_callback Call on administration panels.
155035 */
155036 function add_custom_image_header($header_callback, $admin_header_callback) {
155037 if ( ! empty($header_callback) )
155038 add_action('wp_head', $header_callback);
155039
155040 if ( ! is_admin() )
155041 return;
155042 require_once(ABSPATH . 'wp-admin/custom-header.php');
155043 $GLOBALS['custom_image_header'] = new Custom_Image_Header($admin_header_callback);
155044 add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));
155045 }
155046
155047 ?>