9 final class WP_Theme implements ArrayAccess {
12 * Headers for style.css files.
18 private static $file_headers = array(
19 'Name' => 'Theme Name',
20 'ThemeURI' => 'Theme URI',
21 'Description' => 'Description',
23 'AuthorURI' => 'Author URI',
24 'Version' => 'Version',
25 'Template' => 'Template',
28 'TextDomain' => 'Text Domain',
29 'DomainPath' => 'Domain Path',
39 private static $default_themes = array(
40 'classic' => 'WordPress Classic',
41 'default' => 'WordPress Default',
42 'twentyten' => 'Twenty Ten',
43 'twentyeleven' => 'Twenty Eleven',
44 'twentytwelve' => 'Twenty Twelve',
45 'twentythirteen' => 'Twenty Thirteen',
46 'twentyfourteen' => 'Twenty Fourteen',
47 'twentyfifteen' => 'Twenty Fifteen',
53 private static $tag_map = array(
54 'fixed-width' => 'fixed-layout',
55 'flexible-width' => 'fluid-layout',
59 * Absolute path to the theme root, usually wp-content/themes
67 * Header data from the theme's style.css file.
72 private $headers = array();
75 * Header data from the theme's style.css file after being sanitized.
80 private $headers_sanitized;
83 * Header name from the theme's style.css after being translated.
85 * Cached due to sorting functions running over the translated name.
87 private $name_translated;
90 * Errors encountered when initializing the theme.
98 * The directory name of the theme's files, inside the theme root.
100 * In the case of a child theme, this is directory name of the child theme.
101 * Otherwise, 'stylesheet' is the same as 'template'.
109 * The directory name of the theme's files, inside the theme root.
111 * In the case of a child theme, this is the directory name of the parent theme.
112 * Otherwise, 'template' is the same as 'stylesheet'.
120 * A reference to the parent theme, in the case of a child theme.
128 * URL to the theme root, usually an absolute URL to wp-content/themes
133 private $theme_root_uri;
136 * Flag for whether the theme's textdomain is loaded.
141 private $textdomain_loaded;
144 * Stores an md5 hash of the theme root, to function as the cache key.
152 * Flag for whether the themes cache bucket should be persistently cached.
154 * Default is false. Can be set with the wp_cache_themes_persistently filter.
159 private static $persistently_cache;
162 * Expiration time for the themes cache bucket.
164 * By default the bucket is not cached, so this value is useless.
169 private static $cache_expiration = 1800;
172 * Constructor for WP_Theme.
174 * @param string $theme_dir Directory of the theme within the theme_root.
175 * @param string $theme_root Theme root.
176 * @param WP_Error|null $_child If this theme is a parent theme, the child may be passed for validation purposes.
178 public function __construct( $theme_dir, $theme_root, $_child = null ) {
179 global $wp_theme_directories;
181 // Initialize caching on first run.
182 if ( ! isset( self::$persistently_cache ) ) {
183 /** This action is documented in wp-includes/theme.php */
184 self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' );
185 if ( self::$persistently_cache ) {
186 wp_cache_add_global_groups( 'themes' );
187 if ( is_int( self::$persistently_cache ) )
188 self::$cache_expiration = self::$persistently_cache;
190 wp_cache_add_non_persistent_groups( 'themes' );
194 $this->theme_root = $theme_root;
195 $this->stylesheet = $theme_dir;
197 // Correct a situation where the theme is 'some-directory/some-theme' but 'some-directory' was passed in as part of the theme root instead.
198 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) && in_array( dirname( $theme_root ), (array) $wp_theme_directories ) ) {
199 $this->stylesheet = basename( $this->theme_root ) . '/' . $this->stylesheet;
200 $this->theme_root = dirname( $theme_root );
203 $this->cache_hash = md5( $this->theme_root . '/' . $this->stylesheet );
204 $theme_file = $this->stylesheet . '/style.css';
206 $cache = $this->cache_get( 'theme' );
208 if ( is_array( $cache ) ) {
209 foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
210 if ( isset( $cache[ $key ] ) )
211 $this->$key = $cache[ $key ];
215 if ( isset( $cache['theme_root_template'] ) )
216 $theme_root_template = $cache['theme_root_template'];
217 } elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) {
218 $this->headers['Name'] = $this->stylesheet;
219 if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) )
220 $this->errors = new WP_Error( 'theme_not_found', sprintf( __( 'The theme directory "%s" does not exist.' ), $this->stylesheet ) );
222 $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
223 $this->template = $this->stylesheet;
224 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
225 if ( ! file_exists( $this->theme_root ) ) // Don't cache this one.
226 $this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn’t exist. Please check your installation.' ) );
228 } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) {
229 $this->headers['Name'] = $this->stylesheet;
230 $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
231 $this->template = $this->stylesheet;
232 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
235 $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' );
236 // Default themes always trump their pretenders.
237 // Properly identify default themes that are inside a directory within wp-content/themes.
238 if ( $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes ) ) {
239 if ( basename( $this->stylesheet ) != $default_theme_slug )
240 $this->headers['Name'] .= '/' . $this->stylesheet;
244 // (If template is set from cache [and there are no errors], we know it's good.)
245 if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) {
246 $this->template = $this->stylesheet;
247 if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
248 $this->errors = new WP_Error( 'theme_no_index', __( 'Template is missing.' ) );
249 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
254 // If we got our data from cache, we can assume that 'template' is pointing to the right place.
255 if ( ! is_array( $cache ) && $this->template != $this->stylesheet && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) ) {
256 // If we're in a directory of themes inside /themes, look for the parent nearby.
257 // wp-content/themes/directory-of-themes/*
258 $parent_dir = dirname( $this->stylesheet );
259 if ( '.' != $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) {
260 $this->template = $parent_dir . '/' . $this->template;
261 } elseif ( ( $directories = search_theme_directories() ) && isset( $directories[ $this->template ] ) ) {
262 // Look for the template in the search_theme_directories() results, in case it is in another theme root.
263 // We don't look into directories of themes, just the theme root.
264 $theme_root_template = $directories[ $this->template ]['theme_root'];
266 // Parent theme is missing.
267 $this->errors = new WP_Error( 'theme_no_parent', sprintf( __( 'The parent theme is missing. Please install the "%s" parent theme.' ), $this->template ) );
268 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
269 $this->parent = new WP_Theme( $this->template, $this->theme_root, $this );
274 // Set the parent, if we're a child theme.
275 if ( $this->template != $this->stylesheet ) {
276 // If we are a parent, then there is a problem. Only two generations allowed! Cancel things out.
277 if ( is_a( $_child, 'WP_Theme' ) && $_child->template == $this->stylesheet ) {
278 $_child->parent = null;
279 $_child->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $_child->template ) );
280 $_child->cache_add( 'theme', array( 'headers' => $_child->headers, 'errors' => $_child->errors, 'stylesheet' => $_child->stylesheet, 'template' => $_child->template ) );
281 // The two themes actually reference each other with the Template header.
282 if ( $_child->stylesheet == $this->template ) {
283 $this->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $this->template ) );
284 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
288 // Set the parent. Pass the current instance so we can do the crazy checks above and assess errors.
289 $this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this );
292 // We're good. If we didn't retrieve from cache, set it.
293 if ( ! is_array( $cache ) ) {
294 $cache = array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template );
295 // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above.
296 if ( isset( $theme_root_template ) )
297 $cache['theme_root_template'] = $theme_root_template;
298 $this->cache_add( 'theme', $cache );
303 * When converting the object to a string, the theme name is returned.
305 * @return string Theme name, ready for display (translated)
307 public function __toString() {
308 return (string) $this->display('Name');
312 * __isset() magic method for properties formerly returned by current_theme_info()
314 public function __isset( $offset ) {
315 static $properties = array(
316 'name', 'title', 'version', 'parent_theme', 'template_dir', 'stylesheet_dir', 'template', 'stylesheet',
317 'screenshot', 'description', 'author', 'tags', 'theme_root', 'theme_root_uri',
320 return in_array( $offset, $properties );
324 * __get() magic method for properties formerly returned by current_theme_info()
326 public function __get( $offset ) {
330 return $this->get('Name');
332 return $this->get('Version');
333 case 'parent_theme' :
334 return $this->parent() ? $this->parent()->get('Name') : '';
335 case 'template_dir' :
336 return $this->get_template_directory();
337 case 'stylesheet_dir' :
338 return $this->get_stylesheet_directory();
340 return $this->get_template();
342 return $this->get_stylesheet();
344 return $this->get_screenshot( 'relative' );
345 // 'author' and 'description' did not previously return translated data.
347 return $this->display('Description');
349 return $this->display('Author');
351 return $this->get( 'Tags' );
353 return $this->get_theme_root();
354 case 'theme_root_uri' :
355 return $this->get_theme_root_uri();
356 // For cases where the array was converted to an object.
358 return $this->offsetGet( $offset );
363 * Method to implement ArrayAccess for keys formerly returned by get_themes()
365 public function offsetSet( $offset, $value ) {}
368 * Method to implement ArrayAccess for keys formerly returned by get_themes()
370 public function offsetUnset( $offset ) {}
373 * Method to implement ArrayAccess for keys formerly returned by get_themes()
375 public function offsetExists( $offset ) {
376 static $keys = array(
377 'Name', 'Version', 'Status', 'Title', 'Author', 'Author Name', 'Author URI', 'Description',
378 'Template', 'Stylesheet', 'Template Files', 'Stylesheet Files', 'Template Dir', 'Stylesheet Dir',
379 'Screenshot', 'Tags', 'Theme Root', 'Theme Root URI', 'Parent Theme',
382 return in_array( $offset, $keys );
386 * Method to implement ArrayAccess for keys formerly returned by get_themes().
388 * Author, Author Name, Author URI, and Description did not previously return
389 * translated data. We are doing so now as it is safe to do. However, as
390 * Name and Title could have been used as the key for get_themes(), both remain
391 * untranslated for back compatibility. This means that ['Name'] is not ideal,
392 * and care should be taken to use $theme->display('Name') to get a properly
395 public function offsetGet( $offset ) {
399 // See note above about using translated data. get() is not ideal.
400 // It is only for backwards compatibility. Use display().
401 return $this->get('Name');
403 return $this->display( 'Author');
405 return $this->display( 'Author', false);
407 return $this->display('AuthorURI');
409 return $this->display( 'Description');
412 return $this->get( $offset );
414 return $this->get_template();
416 return $this->get_stylesheet();
417 case 'Template Files' :
418 return $this->get_files( 'php', 1, true );
419 case 'Stylesheet Files' :
420 return $this->get_files( 'css', 0, false );
421 case 'Template Dir' :
422 return $this->get_template_directory();
423 case 'Stylesheet Dir' :
424 return $this->get_stylesheet_directory();
426 return $this->get_screenshot( 'relative' );
428 return $this->get('Tags');
430 return $this->get_theme_root();
431 case 'Theme Root URI' :
432 return $this->get_theme_root_uri();
433 case 'Parent Theme' :
434 return $this->parent() ? $this->parent()->get('Name') : '';
441 * Returns errors property.
446 * @return WP_Error|bool WP_Error if there are errors, or false.
448 public function errors() {
449 return is_wp_error( $this->errors ) ? $this->errors : false;
453 * Whether the theme exists.
455 * A theme with errors exists. A theme with the error of 'theme_not_found',
456 * meaning that the theme's directory was not found, does not exist.
461 * @return bool Whether the theme exists.
463 public function exists() {
464 return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes() ) );
468 * Returns reference to the parent theme.
473 * @return WP_Theme|bool Parent theme, or false if the current theme is not a child theme.
475 public function parent() {
476 return isset( $this->parent ) ? $this->parent : false;
480 * Adds theme data to cache.
482 * Cache entries keyed by the theme and the type of data.
487 * @param string $key Type of data to store (theme, screenshot, headers, page_templates)
488 * @param string $data Data to store
489 * @return bool Return value from wp_cache_add()
491 private function cache_add( $key, $data ) {
492 return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration );
496 * Gets theme data from cache.
498 * Cache entries are keyed by the theme and the type of data.
503 * @param string $key Type of data to retrieve (theme, screenshot, headers, page_templates)
504 * @return mixed Retrieved data
506 private function cache_get( $key ) {
507 return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' );
511 * Clears the cache for the theme.
516 public function cache_delete() {
517 foreach ( array( 'theme', 'screenshot', 'headers', 'page_templates' ) as $key )
518 wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' );
519 $this->template = $this->textdomain_loaded = $this->theme_root_uri = $this->parent = $this->errors = $this->headers_sanitized = $this->name_translated = null;
520 $this->headers = array();
521 $this->__construct( $this->stylesheet, $this->theme_root );
525 * Get a raw, unformatted theme header.
527 * The header is sanitized, but is not translated, and is not marked up for display.
528 * To get a theme header for display, use the display() method.
530 * Use the get_template() method, not the 'Template' header, for finding the template.
531 * The 'Template' header is only good for what was written in the style.css, while
532 * get_template() takes into account where WordPress actually located the theme and
533 * whether it is actually valid.
538 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
539 * @return string|bool String on success, false on failure.
541 public function get( $header ) {
542 if ( ! isset( $this->headers[ $header ] ) )
545 if ( ! isset( $this->headers_sanitized ) ) {
546 $this->headers_sanitized = $this->cache_get( 'headers' );
547 if ( ! is_array( $this->headers_sanitized ) )
548 $this->headers_sanitized = array();
551 if ( isset( $this->headers_sanitized[ $header ] ) )
552 return $this->headers_sanitized[ $header ];
554 // If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets.
555 if ( self::$persistently_cache ) {
556 foreach ( array_keys( $this->headers ) as $_header )
557 $this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] );
558 $this->cache_add( 'headers', $this->headers_sanitized );
560 $this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] );
563 return $this->headers_sanitized[ $header ];
567 * Gets a theme header, formatted and translated for display.
572 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
573 * @param bool $markup Optional. Whether to mark up the header. Defaults to true.
574 * @param bool $translate Optional. Whether to translate the header. Defaults to true.
575 * @return string|bool Processed header, false on failure.
577 public function display( $header, $markup = true, $translate = true ) {
578 $value = $this->get( $header );
579 if ( false === $value ) {
583 if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) )
587 $value = $this->translate_header( $header, $value );
590 $value = $this->markup_header( $header, $value, $translate );
596 * Sanitize a theme header.
598 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
599 * @param string $value Value to sanitize.
601 private function sanitize_header( $header, $value ) {
608 // Fall through otherwise.
610 static $header_tags = array(
611 'abbr' => array( 'title' => true ),
612 'acronym' => array( 'title' => true ),
617 $value = wp_kses( $value, $header_tags );
620 // There shouldn't be anchor tags in Author, but some themes like to be challenging.
622 static $header_tags_with_a = array(
623 'a' => array( 'href' => true, 'title' => true ),
624 'abbr' => array( 'title' => true ),
625 'acronym' => array( 'title' => true ),
630 $value = wp_kses( $value, $header_tags_with_a );
634 $value = esc_url_raw( $value );
637 $value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
645 * Mark up a theme header.
650 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
651 * @param string $value Value to mark up.
652 * @param string $translate Whether the header has been translated.
653 * @return string Value, marked up.
655 private function markup_header( $header, $value, $translate ) {
658 if ( empty( $value ) )
659 $value = $this->get_stylesheet();
662 $value = wptexturize( $value );
665 if ( $this->get('AuthorURI') ) {
666 $value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value );
667 } elseif ( ! $value ) {
668 $value = __( 'Anonymous' );
672 static $comma = null;
673 if ( ! isset( $comma ) ) {
674 /* translators: used between list items, there is a space after the comma */
677 $value = implode( $comma, $value );
681 $value = esc_url( $value );
689 * Translate a theme header.
694 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
695 * @param string $value Value to translate.
696 * @return string Translated value.
698 private function translate_header( $header, $value ) {
701 // Cached for sorting reasons.
702 if ( isset( $this->name_translated ) )
703 return $this->name_translated;
704 $this->name_translated = translate( $value, $this->get('TextDomain' ) );
705 return $this->name_translated;
707 if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) )
711 if ( ! isset( $tags_list ) ) {
712 $tags_list = array();
713 $feature_list = get_theme_feature_list( false ); // No API
714 foreach ( $feature_list as $tags )
718 foreach ( $value as &$tag ) {
719 if ( isset( $tags_list[ $tag ] ) ) {
720 $tag = $tags_list[ $tag ];
721 } elseif ( isset( self::$tag_map[ $tag ] ) ) {
722 $tag = $tags_list[ self::$tag_map[ $tag ] ];
729 $value = translate( $value, $this->get('TextDomain') );
735 * The directory name of the theme's "stylesheet" files, inside the theme root.
737 * In the case of a child theme, this is directory name of the child theme.
738 * Otherwise, get_stylesheet() is the same as get_template().
743 * @return string Stylesheet
745 public function get_stylesheet() {
746 return $this->stylesheet;
750 * The directory name of the theme's "template" files, inside the theme root.
752 * In the case of a child theme, this is the directory name of the parent theme.
753 * Otherwise, the get_template() is the same as get_stylesheet().
758 * @return string Template
760 public function get_template() {
761 return $this->template;
765 * Returns the absolute path to the directory of a theme's "stylesheet" files.
767 * In the case of a child theme, this is the absolute path to the directory
768 * of the child theme's files.
773 * @return string Absolute path of the stylesheet directory.
775 public function get_stylesheet_directory() {
776 if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes() ) )
779 return $this->theme_root . '/' . $this->stylesheet;
783 * Returns the absolute path to the directory of a theme's "template" files.
785 * In the case of a child theme, this is the absolute path to the directory
786 * of the parent theme's files.
791 * @return string Absolute path of the template directory.
793 public function get_template_directory() {
794 if ( $this->parent() )
795 $theme_root = $this->parent()->theme_root;
797 $theme_root = $this->theme_root;
799 return $theme_root . '/' . $this->template;
803 * Returns the URL to the directory of a theme's "stylesheet" files.
805 * In the case of a child theme, this is the URL to the directory of the
806 * child theme's files.
811 * @return string URL to the stylesheet directory.
813 public function get_stylesheet_directory_uri() {
814 return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) );
818 * Returns the URL to the directory of a theme's "template" files.
820 * In the case of a child theme, this is the URL to the directory of the
821 * parent theme's files.
826 * @return string URL to the template directory.
828 public function get_template_directory_uri() {
829 if ( $this->parent() )
830 $theme_root_uri = $this->parent()->get_theme_root_uri();
832 $theme_root_uri = $this->get_theme_root_uri();
834 return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) );
838 * The absolute path to the directory of the theme root.
840 * This is typically the absolute path to wp-content/themes.
845 * @return string Theme root.
847 public function get_theme_root() {
848 return $this->theme_root;
852 * Returns the URL to the directory of the theme root.
854 * This is typically the absolute URL to wp-content/themes. This forms the basis
855 * for all other URLs returned by WP_Theme, so we pass it to the public function
856 * get_theme_root_uri() and allow it to run the theme_root_uri filter.
861 * @return string Theme root URI.
863 public function get_theme_root_uri() {
864 if ( ! isset( $this->theme_root_uri ) )
865 $this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root );
866 return $this->theme_root_uri;
870 * Returns the main screenshot file for the theme.
872 * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed.
874 * Screenshots for a theme must be in the stylesheet directory. (In the case of child
875 * themes, parent theme screenshots are not inherited.)
880 * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI.
881 * @return mixed Screenshot file. False if the theme does not have a screenshot.
883 public function get_screenshot( $uri = 'uri' ) {
884 $screenshot = $this->cache_get( 'screenshot' );
886 if ( 'relative' == $uri )
888 return $this->get_stylesheet_directory_uri() . '/' . $screenshot;
889 } elseif ( 0 === $screenshot ) {
893 foreach ( array( 'png', 'gif', 'jpg', 'jpeg' ) as $ext ) {
894 if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
895 $this->cache_add( 'screenshot', 'screenshot.' . $ext );
896 if ( 'relative' == $uri )
897 return 'screenshot.' . $ext;
898 return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext;
902 $this->cache_add( 'screenshot', 0 );
907 * Return files in the theme's directory.
912 * @param mixed $type Optional. Array of extensions to return. Defaults to all files (null).
913 * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.
914 * @param bool $search_parent Optional. Whether to return parent files. Defaults to false.
915 * @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values
916 * being absolute paths.
918 public function get_files( $type = null, $depth = 0, $search_parent = false ) {
919 $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
921 if ( $search_parent && $this->parent() )
922 $files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
928 * Returns the theme's page templates.
933 * @param WP_Post|null $post Optional. The post being edited, provided for context.
934 * @return array Array of page templates, keyed by filename, with the value of the translated header name.
936 public function get_page_templates( $post = null ) {
937 // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
938 if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
941 $page_templates = $this->cache_get( 'page_templates' );
943 if ( ! is_array( $page_templates ) ) {
944 $page_templates = array();
946 $files = (array) $this->get_files( 'php', 1 );
948 foreach ( $files as $file => $full_path ) {
949 if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) )
951 $page_templates[ $file ] = _cleanup_header_comment( $header[1] );
954 $this->cache_add( 'page_templates', $page_templates );
957 if ( $this->load_textdomain() ) {
958 foreach ( $page_templates as &$page_template ) {
959 $page_template = $this->translate_header( 'Template Name', $page_template );
963 if ( $this->parent() )
964 $page_templates += $this->parent()->get_page_templates( $post );
967 * Filter list of page templates for a theme.
969 * This filter does not currently allow for page templates to be added.
973 * @param array $page_templates Array of page templates. Keys are filenames,
974 * values are translated names.
975 * @param WP_Theme $this The theme object.
976 * @param WP_Post|null $post The post being edited, provided for context, or null.
978 $return = apply_filters( 'theme_page_templates', $page_templates, $this, $post );
980 return array_intersect_assoc( $return, $page_templates );
984 * Scans a directory for files of a certain extension.
989 * @param string $path Absolute path to search.
990 * @param mixed Array of extensions to find, string of a single extension, or null for all extensions.
991 * @param int $depth How deep to search for files. Optional, defaults to a flat scan (0 depth). -1 depth is infinite.
992 * @param string $relative_path The basename of the absolute path. Used to control the returned path
993 * for the found files, particularly when this function recurses to lower depths.
995 private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
996 if ( ! is_dir( $path ) )
1000 $extensions = (array) $extensions;
1001 $_extensions = implode( '|', $extensions );
1004 $relative_path = trailingslashit( $relative_path );
1005 if ( '/' == $relative_path )
1006 $relative_path = '';
1008 $results = scandir( $path );
1011 foreach ( $results as $result ) {
1012 if ( '.' == $result[0] )
1014 if ( is_dir( $path . '/' . $result ) ) {
1015 if ( ! $depth || 'CVS' == $result )
1017 $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
1018 $files = array_merge_recursive( $files, $found );
1019 } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
1020 $files[ $relative_path . $result ] = $path . '/' . $result;
1028 * Loads the theme's textdomain.
1030 * Translation files are not inherited from the parent theme. Todo: if this fails for the
1031 * child theme, it should probably try to load the parent theme's translations.
1036 * @return bool If the textdomain was successfully loaded or has already been loaded. False if
1037 * no textdomain was specified in the file headers, or if the domain could not be loaded.
1039 public function load_textdomain() {
1040 if ( isset( $this->textdomain_loaded ) )
1041 return $this->textdomain_loaded;
1043 $textdomain = $this->get('TextDomain');
1044 if ( ! $textdomain ) {
1045 $this->textdomain_loaded = false;
1049 if ( is_textdomain_loaded( $textdomain ) ) {
1050 $this->textdomain_loaded = true;
1054 $path = $this->get_stylesheet_directory();
1055 if ( $domainpath = $this->get('DomainPath') )
1056 $path .= $domainpath;
1058 $path .= '/languages';
1060 $this->textdomain_loaded = load_theme_textdomain( $textdomain, $path );
1061 return $this->textdomain_loaded;
1065 * Whether the theme is allowed (multisite only).
1070 * @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site'
1071 * settings, or 'both'. Defaults to 'both'.
1072 * @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current blog.
1073 * @return bool Whether the theme is allowed for the network. Returns true in single-site.
1075 public function is_allowed( $check = 'both', $blog_id = null ) {
1076 if ( ! is_multisite() )
1079 if ( 'both' == $check || 'network' == $check ) {
1080 $allowed = self::get_allowed_on_network();
1081 if ( ! empty( $allowed[ $this->get_stylesheet() ] ) )
1085 if ( 'both' == $check || 'site' == $check ) {
1086 $allowed = self::get_allowed_on_site( $blog_id );
1087 if ( ! empty( $allowed[ $this->get_stylesheet() ] ) )
1095 * Returns array of stylesheet names of themes allowed on the site or network.
1100 * @param int $blog_id Optional. Defaults to current blog.
1101 * @return array Array of stylesheet names.
1103 public static function get_allowed( $blog_id = null ) {
1105 * Filter the array of themes allowed on the site or network.
1109 * @param array $allowed_themes An array of theme stylesheet names.
1111 $network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
1112 return $network + self::get_allowed_on_site( $blog_id );
1116 * Returns array of stylesheet names of themes allowed on the network.
1121 * @return array Array of stylesheet names.
1123 public static function get_allowed_on_network() {
1124 static $allowed_themes;
1125 if ( ! isset( $allowed_themes ) )
1126 $allowed_themes = (array) get_site_option( 'allowedthemes' );
1127 return $allowed_themes;
1131 * Returns array of stylesheet names of themes allowed on the site.
1136 * @param int $blog_id Optional. Defaults to current blog.
1137 * @return array Array of stylesheet names.
1139 public static function get_allowed_on_site( $blog_id = null ) {
1140 static $allowed_themes = array();
1142 if ( ! $blog_id || ! is_multisite() )
1143 $blog_id = get_current_blog_id();
1145 if ( isset( $allowed_themes[ $blog_id ] ) )
1146 return $allowed_themes[ $blog_id ];
1148 $current = $blog_id == get_current_blog_id();
1151 $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1153 switch_to_blog( $blog_id );
1154 $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1155 restore_current_blog();
1158 // This is all super old MU back compat joy.
1159 // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
1160 if ( false === $allowed_themes[ $blog_id ] ) {
1162 $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1164 switch_to_blog( $blog_id );
1165 $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1166 restore_current_blog();
1169 if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
1170 $allowed_themes[ $blog_id ] = array();
1172 $converted = array();
1173 $themes = wp_get_themes();
1174 foreach ( $themes as $stylesheet => $theme_data ) {
1175 if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get('Name') ] ) )
1176 $converted[ $stylesheet ] = true;
1178 $allowed_themes[ $blog_id ] = $converted;
1180 // Set the option so we never have to go through this pain again.
1181 if ( is_admin() && $allowed_themes[ $blog_id ] ) {
1183 update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1184 delete_option( 'allowed_themes' );
1186 switch_to_blog( $blog_id );
1187 update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1188 delete_option( 'allowed_themes' );
1189 restore_current_blog();
1194 return (array) $allowed_themes[ $blog_id ];
1198 * Sort themes by name.
1203 public static function sort_by_name( &$themes ) {
1204 if ( 0 === strpos( get_locale(), 'en_' ) ) {
1205 uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
1207 uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) );
1212 * Callback function for usort() to naturally sort themes by name.
1214 * Accesses the Name header directly from the class for maximum speed.
1215 * Would choke on HTML but we don't care enough to slow it down with strip_tags().
1220 private static function _name_sort( $a, $b ) {
1221 return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] );
1225 * Name sort (with translation).
1230 private static function _name_sort_i18n( $a, $b ) {
1231 // Don't mark up; Do translate.
1232 return strnatcasecmp( $a->display( 'Name', false, true ), $b->display( 'Name', false, true ) );