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',
48 * Absolute path to the theme root, usually wp-content/themes
56 * Header data from the theme's style.css file.
61 private $headers = array();
64 * Header data from the theme's style.css file after being sanitized.
69 private $headers_sanitized;
72 * Header name from the theme's style.css after being translated.
74 * Cached due to sorting functions running over the translated name.
76 private $name_translated;
79 * Errors encountered when initializing the theme.
87 * The directory name of the theme's files, inside the theme root.
89 * In the case of a child theme, this is directory name of the the child theme.
90 * Otherwise, 'stylesheet' is the same as 'template'.
98 * The directory name of the theme's files, inside the theme root.
100 * In the case of a child theme, this is the directory name of the parent theme.
101 * Otherwise, 'template' is the same as 'stylesheet'.
109 * A reference to the parent theme, in the case of a child theme.
117 * URL to the theme root, usually an absolute URL to wp-content/themes
122 private $theme_root_uri;
125 * Flag for whether the theme's textdomain is loaded.
130 private $textdomain_loaded;
133 * Stores an md5 hash of the theme root, to function as the cache key.
141 * Flag for whether the themes cache bucket should be persistently cached.
143 * Default is false. Can be set with the wp_cache_themes_persistently filter.
148 private static $persistently_cache;
151 * Expiration time for the themes cache bucket.
153 * By default the bucket is not cached, so this value is useless.
158 private static $cache_expiration = 1800;
161 * Constructor for WP_Theme.
163 * @param string $theme_dir Directory of the theme within the theme_root.
164 * @param string $theme_root Theme root.
165 * @param WP_Error|null $_child If this theme is a parent theme, the child may be passed for validation purposes.
167 public function __construct( $theme_dir, $theme_root, $_child = null ) {
168 global $wp_theme_directories;
170 // Initialize caching on first run.
171 if ( ! isset( self::$persistently_cache ) ) {
172 self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' );
173 if ( self::$persistently_cache ) {
174 wp_cache_add_global_groups( 'themes' );
175 if ( is_int( self::$persistently_cache ) )
176 self::$cache_expiration = self::$persistently_cache;
178 wp_cache_add_non_persistent_groups( 'themes' );
182 $this->theme_root = $theme_root;
183 $this->stylesheet = $theme_dir;
185 // Correct a situation where the theme is 'some-directory/some-theme' but 'some-directory' was passed in as part of the theme root instead.
186 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) && in_array( dirname( $theme_root ), (array) $wp_theme_directories ) ) {
187 $this->stylesheet = basename( $this->theme_root ) . '/' . $this->stylesheet;
188 $this->theme_root = dirname( $theme_root );
191 $this->cache_hash = md5( $this->theme_root . '/' . $this->stylesheet );
192 $theme_file = $this->stylesheet . '/style.css';
194 $cache = $this->cache_get( 'theme' );
196 if ( is_array( $cache ) ) {
197 foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
198 if ( isset( $cache[ $key ] ) )
199 $this->$key = $cache[ $key ];
203 if ( isset( $cache['theme_root_template'] ) )
204 $theme_root_template = $cache['theme_root_template'];
205 } elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) {
206 $this->headers['Name'] = $this->stylesheet;
207 if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) )
208 $this->errors = new WP_Error( 'theme_not_found', __( 'The theme directory does not exist.' ) );
210 $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
211 $this->template = $this->stylesheet;
212 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
213 if ( ! file_exists( $this->theme_root ) ) // Don't cache this one.
214 $this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn’t exist. Please check your installation.' ) );
216 } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) {
217 $this->headers['Name'] = $this->stylesheet;
218 $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
219 $this->template = $this->stylesheet;
220 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
223 $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' );
224 // Default themes always trump their pretenders.
225 // Properly identify default themes that are inside a directory within wp-content/themes.
226 if ( $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes ) ) {
227 if ( basename( $this->stylesheet ) != $default_theme_slug )
228 $this->headers['Name'] .= '/' . $this->stylesheet;
232 // (If template is set from cache [and there are no errors], we know it's good.)
233 if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) {
234 $this->template = $this->stylesheet;
235 if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
236 $this->errors = new WP_Error( 'theme_no_index', __( 'Template is missing.' ) );
237 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
242 // If we got our data from cache, we can assume that 'template' is pointing to the right place.
243 if ( ! is_array( $cache ) && $this->template != $this->stylesheet && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) ) {
244 // If we're in a directory of themes inside /themes, look for the parent nearby.
245 // wp-content/themes/directory-of-themes/*
246 $parent_dir = dirname( $this->stylesheet );
247 if ( '.' != $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) {
248 $this->template = $parent_dir . '/' . $this->template;
249 } elseif ( ( $directories = search_theme_directories() ) && isset( $directories[ $this->template ] ) ) {
250 // Look for the template in the search_theme_directories() results, in case it is in another theme root.
251 // We don't look into directories of themes, just the theme root.
252 $theme_root_template = $directories[ $this->template ]['theme_root'];
254 // Parent theme is missing.
255 $this->errors = new WP_Error( 'theme_no_parent', sprintf( __( 'The parent theme is missing. Please install the "%s" parent theme.' ), $this->template ) );
256 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
257 $this->parent = new WP_Theme( $this->template, $this->theme_root, $this );
262 // Set the parent, if we're a child theme.
263 if ( $this->template != $this->stylesheet ) {
264 // If we are a parent, then there is a problem. Only two generations allowed! Cancel things out.
265 if ( is_a( $_child, 'WP_Theme' ) && $_child->template == $this->stylesheet ) {
266 $_child->parent = null;
267 $_child->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $_child->template ) );
268 $_child->cache_add( 'theme', array( 'headers' => $_child->headers, 'errors' => $_child->errors, 'stylesheet' => $_child->stylesheet, 'template' => $_child->template ) );
269 // The two themes actually reference each other with the Template header.
270 if ( $_child->stylesheet == $this->template ) {
271 $this->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $this->template ) );
272 $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
276 // Set the parent. Pass the current instance so we can do the crazy checks above and assess errors.
277 $this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this );
280 // We're good. If we didn't retrieve from cache, set it.
281 if ( ! is_array( $cache ) ) {
282 $cache = array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template );
283 // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above.
284 if ( isset( $theme_root_template ) )
285 $cache['theme_root_template'] = $theme_root_template;
286 $this->cache_add( 'theme', $cache );
291 * When converting the object to a string, the theme name is returned.
293 * @return string Theme name, ready for display (translated)
295 public function __toString() {
296 return (string) $this->display('Name');
300 * __isset() magic method for properties formerly returned by current_theme_info()
302 public function __isset( $offset ) {
303 static $properties = array(
304 'name', 'title', 'version', 'parent_theme', 'template_dir', 'stylesheet_dir', 'template', 'stylesheet',
305 'screenshot', 'description', 'author', 'tags', 'theme_root', 'theme_root_uri',
308 return in_array( $offset, $properties );
312 * __get() magic method for properties formerly returned by current_theme_info()
314 public function __get( $offset ) {
318 return $this->get('Name');
320 return $this->get('Version');
321 case 'parent_theme' :
322 return $this->parent() ? $this->parent()->get('Name') : '';
323 case 'template_dir' :
324 return $this->get_template_directory();
325 case 'stylesheet_dir' :
326 return $this->get_stylesheet_directory();
328 return $this->get_template();
330 return $this->get_stylesheet();
332 return $this->get_screenshot( 'relative' );
333 // 'author' and 'description' did not previously return translated data.
335 return $this->display('Description');
337 return $this->display('Author');
339 return $this->get( 'Tags' );
341 return $this->get_theme_root();
342 case 'theme_root_uri' :
343 return $this->get_theme_root_uri();
344 // For cases where the array was converted to an object.
346 return $this->offsetGet( $offset );
351 * Method to implement ArrayAccess for keys formerly returned by get_themes()
353 public function offsetSet( $offset, $value ) {}
356 * Method to implement ArrayAccess for keys formerly returned by get_themes()
358 public function offsetUnset( $offset ) {}
361 * Method to implement ArrayAccess for keys formerly returned by get_themes()
363 public function offsetExists( $offset ) {
364 static $keys = array(
365 'Name', 'Version', 'Status', 'Title', 'Author', 'Author Name', 'Author URI', 'Description',
366 'Template', 'Stylesheet', 'Template Files', 'Stylesheet Files', 'Template Dir', 'Stylesheet Dir',
367 'Screenshot', 'Tags', 'Theme Root', 'Theme Root URI', 'Parent Theme',
370 return in_array( $offset, $keys );
374 * Method to implement ArrayAccess for keys formerly returned by get_themes().
376 * Author, Author Name, Author URI, and Description did not previously return
377 * translated data. We are doing so now as it is safe to do. However, as
378 * Name and Title could have been used as the key for get_themes(), both remain
379 * untranslated for back compatibility. This means that ['Name'] is not ideal,
380 * and care should be taken to use $theme->display('Name') to get a properly
383 public function offsetGet( $offset ) {
387 // See note above about using translated data. get() is not ideal.
388 // It is only for backwards compatibility. Use display().
389 return $this->get('Name');
391 return $this->display( 'Author');
393 return $this->display( 'Author', false);
395 return $this->display('AuthorURI');
397 return $this->display( 'Description');
400 return $this->get( $offset );
402 return $this->get_template();
404 return $this->get_stylesheet();
405 case 'Template Files' :
406 return $this->get_files( 'php', 1, true );
407 case 'Stylesheet Files' :
408 return $this->get_files( 'css', 0, false );
409 case 'Template Dir' :
410 return $this->get_template_directory();
411 case 'Stylesheet Dir' :
412 return $this->get_stylesheet_directory();
414 return $this->get_screenshot( 'relative' );
416 return $this->get('Tags');
418 return $this->get_theme_root();
419 case 'Theme Root URI' :
420 return $this->get_theme_root_uri();
421 case 'Parent Theme' :
422 return $this->parent() ? $this->parent()->get('Name') : '';
429 * Returns errors property.
434 * @return WP_Error|bool WP_Error if there are errors, or false.
436 public function errors() {
437 return is_wp_error( $this->errors ) ? $this->errors : false;
441 * Whether the theme exists.
443 * A theme with errors exists. A theme with the error of 'theme_not_found',
444 * meaning that the theme's directory was not found, does not exist.
449 * @return bool Whether the theme exists.
451 public function exists() {
452 return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes() ) );
456 * Returns reference to the parent theme.
461 * @return WP_Theme|bool Parent theme, or false if the current theme is not a child theme.
463 public function parent() {
464 return isset( $this->parent ) ? $this->parent : false;
468 * Adds theme data to cache.
470 * Cache entries keyed by the theme and the type of data.
475 * @param string $key Type of data to store (theme, screenshot, headers, page_templates)
476 * @param string $data Data to store
477 * @return bool Return value from wp_cache_add()
479 private function cache_add( $key, $data ) {
480 return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration );
484 * Gets theme data from cache.
486 * Cache entries are keyed by the theme and the type of data.
491 * @param string $key Type of data to retrieve (theme, screenshot, headers, page_templates)
492 * @return mixed Retrieved data
494 private function cache_get( $key ) {
495 return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' );
499 * Clears the cache for the theme.
504 public function cache_delete() {
505 foreach ( array( 'theme', 'screenshot', 'headers', 'page_templates' ) as $key )
506 wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' );
507 $this->template = $this->textdomain_loaded = $this->theme_root_uri = $this->parent = $this->errors = $this->headers_sanitized = $this->name_translated = null;
508 $this->headers = array();
509 $this->__construct( $this->stylesheet, $this->theme_root );
513 * Get a raw, unformatted theme header.
515 * The header is sanitized, but is not translated, and is not marked up for display.
516 * To get a theme header for display, use the display() method.
518 * Use the get_template() method, not the 'Template' header, for finding the template.
519 * The 'Template' header is only good for what was written in the style.css, while
520 * get_template() takes into account where WordPress actually located the theme and
521 * whether it is actually valid.
526 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
527 * @return string String on success, false on failure.
529 public function get( $header ) {
530 if ( ! isset( $this->headers[ $header ] ) )
533 if ( ! isset( $this->headers_sanitized ) ) {
534 $this->headers_sanitized = $this->cache_get( 'headers' );
535 if ( ! is_array( $this->headers_sanitized ) )
536 $this->headers_sanitized = array();
539 if ( isset( $this->headers_sanitized[ $header ] ) )
540 return $this->headers_sanitized[ $header ];
542 // If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets.
543 if ( self::$persistently_cache ) {
544 foreach ( array_keys( $this->headers ) as $_header )
545 $this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] );
546 $this->cache_add( 'headers', $this->headers_sanitized );
548 $this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] );
551 return $this->headers_sanitized[ $header ];
555 * Gets a theme header, formatted and translated for display.
560 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
561 * @param bool $markup Optional. Whether to mark up the header. Defaults to true.
562 * @param bool $translate Optional. Whether to translate the header. Defaults to true.
563 * @return string Processed header, false on failure.
565 public function display( $header, $markup = true, $translate = true ) {
566 $value = $this->get( $header );
568 if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) )
572 $value = $this->translate_header( $header, $value );
575 $value = $this->markup_header( $header, $value, $translate );
581 * Sanitize a theme header.
583 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
584 * @param string $value Value to sanitize.
586 private function sanitize_header( $header, $value ) {
593 // Fall through otherwise.
595 static $header_tags = array(
596 'abbr' => array( 'title' => true ),
597 'acronym' => array( 'title' => true ),
602 $value = wp_kses( $value, $header_tags );
605 // There shouldn't be anchor tags in Author, but some themes like to be challenging.
607 static $header_tags_with_a = array(
608 'a' => array( 'href' => true, 'title' => true ),
609 'abbr' => array( 'title' => true ),
610 'acronym' => array( 'title' => true ),
615 $value = wp_kses( $value, $header_tags_with_a );
619 $value = esc_url_raw( $value );
622 $value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
630 * Mark up a theme header.
635 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
636 * @param string $value Value to mark up.
637 * @param string $translate Whether the header has been translated.
638 * @return string Value, marked up.
640 private function markup_header( $header, $value, $translate ) {
643 if ( empty( $value ) )
644 $value = $this->get_stylesheet();
647 $value = wptexturize( $value );
650 if ( $this->get('AuthorURI') ) {
652 if ( ! isset( $attr ) )
653 $attr = esc_attr__( 'Visit author homepage' );
654 $value = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $this->display( 'AuthorURI', true, $translate ), $attr, $value );
655 } elseif ( ! $value ) {
656 $value = __( 'Anonymous' );
660 static $comma = null;
661 if ( ! isset( $comma ) ) {
662 /* translators: used between list items, there is a space after the comma */
665 $value = implode( $comma, $value );
669 $value = esc_url( $value );
677 * Translate a theme header.
682 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
683 * @param string $value Value to translate.
684 * @return string Translated value.
686 private function translate_header( $header, $value ) {
689 // Cached for sorting reasons.
690 if ( isset( $this->name_translated ) )
691 return $this->name_translated;
692 $this->name_translated = translate( $value, $this->get('TextDomain' ) );
693 return $this->name_translated;
695 if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) )
699 if ( ! isset( $tags_list ) ) {
700 $tags_list = array();
701 $feature_list = get_theme_feature_list( false ); // No API
702 foreach ( $feature_list as $tags )
706 foreach ( $value as &$tag ) {
707 if ( isset( $tags_list[ $tag ] ) )
708 $tag = $tags_list[ $tag ];
714 $value = translate( $value, $this->get('TextDomain') );
720 * The directory name of the theme's "stylesheet" files, inside the theme root.
722 * In the case of a child theme, this is directory name of the the child theme.
723 * Otherwise, get_stylesheet() is the same as get_template().
728 * @return string Stylesheet
730 public function get_stylesheet() {
731 return $this->stylesheet;
735 * The directory name of the theme's "template" files, inside the theme root.
737 * In the case of a child theme, this is the directory name of the parent theme.
738 * Otherwise, the get_template() is the same as get_stylesheet().
743 * @return string Template
745 public function get_template() {
746 return $this->template;
750 * Returns the absolute path to the directory of a theme's "stylesheet" files.
752 * In the case of a child theme, this is the absolute path to the directory
753 * of the child theme's files.
758 * @return string Absolute path of the stylesheet directory.
760 public function get_stylesheet_directory() {
761 if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes() ) )
764 return $this->theme_root . '/' . $this->stylesheet;
768 * Returns the absolute path to the directory of a theme's "template" files.
770 * In the case of a child theme, this is the absolute path to the directory
771 * of the parent theme's files.
776 * @return string Absolute path of the template directory.
778 public function get_template_directory() {
779 if ( $this->parent() )
780 $theme_root = $this->parent()->theme_root;
782 $theme_root = $this->theme_root;
784 return $theme_root . '/' . $this->template;
788 * Returns the URL to the directory of a theme's "stylesheet" files.
790 * In the case of a child theme, this is the URL to the directory of the
791 * child theme's files.
796 * @return string URL to the stylesheet directory.
798 public function get_stylesheet_directory_uri() {
799 return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) );
803 * Returns the URL to the directory of a theme's "template" files.
805 * In the case of a child theme, this is the URL to the directory of the
806 * parent theme's files.
811 * @return string URL to the template directory.
813 public function get_template_directory_uri() {
814 if ( $this->parent() )
815 $theme_root_uri = $this->parent()->get_theme_root_uri();
817 $theme_root_uri = $this->get_theme_root_uri();
819 return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) );
823 * The absolute path to the directory of the theme root.
825 * This is typically the absolute path to wp-content/themes.
830 * @return string Theme root.
832 public function get_theme_root() {
833 return $this->theme_root;
837 * Returns the URL to the directory of the theme root.
839 * This is typically the absolute URL to wp-content/themes. This forms the basis
840 * for all other URLs returned by WP_Theme, so we pass it to the public function
841 * get_theme_root_uri() and allow it to run the theme_root_uri filter.
843 * @uses get_theme_root_uri()
848 * @return string Theme root URI.
850 public function get_theme_root_uri() {
851 if ( ! isset( $this->theme_root_uri ) )
852 $this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root );
853 return $this->theme_root_uri;
857 * Returns the main screenshot file for the theme.
859 * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed.
861 * Screenshots for a theme must be in the stylesheet directory. (In the case of child
862 * themes, parent theme screenshots are not inherited.)
867 * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI.
868 * @return mixed Screenshot file. False if the theme does not have a screenshot.
870 public function get_screenshot( $uri = 'uri' ) {
871 $screenshot = $this->cache_get( 'screenshot' );
873 if ( 'relative' == $uri )
875 return $this->get_stylesheet_directory_uri() . '/' . $screenshot;
876 } elseif ( 0 === $screenshot ) {
880 foreach ( array( 'png', 'gif', 'jpg', 'jpeg' ) as $ext ) {
881 if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
882 $this->cache_add( 'screenshot', 'screenshot.' . $ext );
883 if ( 'relative' == $uri )
884 return 'screenshot.' . $ext;
885 return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext;
889 $this->cache_add( 'screenshot', 0 );
894 * Return files in the theme's directory.
899 * @param mixed $type Optional. Array of extensions to return. Defaults to all files (null).
900 * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.
901 * @param bool $search_parent Optional. Whether to return parent files. Defaults to false.
902 * @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values
903 * being absolute paths.
905 public function get_files( $type = null, $depth = 0, $search_parent = false ) {
906 $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
908 if ( $search_parent && $this->parent() )
909 $files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
915 * Returns the theme's page templates.
920 * @return array Array of page templates, keyed by filename, with the value of the translated header name.
922 public function get_page_templates() {
923 // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
924 if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
927 $page_templates = $this->cache_get( 'page_templates' );
929 if ( ! is_array( $page_templates ) ) {
930 $page_templates = array();
932 $files = (array) $this->get_files( 'php', 1 );
934 foreach ( $files as $file => $full_path ) {
935 if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) )
937 $page_templates[ $file ] = _cleanup_header_comment( $header[1] );
940 $this->cache_add( 'page_templates', $page_templates );
943 if ( $this->load_textdomain() ) {
944 foreach ( $page_templates as &$page_template ) {
945 $page_template = $this->translate_header( 'Template Name', $page_template );
949 if ( $this->parent() )
950 $page_templates += $this->parent()->get_page_templates();
952 return $page_templates;
956 * Scans a directory for files of a certain extension.
961 * @param string $path Absolute path to search.
962 * @param mixed Array of extensions to find, string of a single extension, or null for all extensions.
963 * @param int $depth How deep to search for files. Optional, defaults to a flat scan (0 depth). -1 depth is infinite.
964 * @param string $relative_path The basename of the absolute path. Used to control the returned path
965 * for the found files, particularly when this function recurses to lower depths.
967 private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
968 if ( ! is_dir( $path ) )
972 $extensions = (array) $extensions;
973 $_extensions = implode( '|', $extensions );
976 $relative_path = trailingslashit( $relative_path );
977 if ( '/' == $relative_path )
980 $results = scandir( $path );
983 foreach ( $results as $result ) {
984 if ( '.' == $result[0] )
986 if ( is_dir( $path . '/' . $result ) ) {
987 if ( ! $depth || 'CVS' == $result )
989 $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
990 $files = array_merge_recursive( $files, $found );
991 } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
992 $files[ $relative_path . $result ] = $path . '/' . $result;
1000 * Loads the theme's textdomain.
1002 * Translation files are not inherited from the parent theme. Todo: if this fails for the
1003 * child theme, it should probably try to load the parent theme's translations.
1008 * @return True if the textdomain was successfully loaded or has already been loaded. False if
1009 * no textdomain was specified in the file headers, or if the domain could not be loaded.
1011 public function load_textdomain() {
1012 if ( isset( $this->textdomain_loaded ) )
1013 return $this->textdomain_loaded;
1015 $textdomain = $this->get('TextDomain');
1016 if ( ! $textdomain ) {
1017 $this->textdomain_loaded = false;
1021 if ( is_textdomain_loaded( $textdomain ) ) {
1022 $this->textdomain_loaded = true;
1026 $path = $this->get_stylesheet_directory();
1027 if ( $domainpath = $this->get('DomainPath') )
1028 $path .= $domainpath;
1030 $path .= '/languages';
1032 $this->textdomain_loaded = load_theme_textdomain( $textdomain, $path );
1033 return $this->textdomain_loaded;
1037 * Whether the theme is allowed (multisite only).
1042 * @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site'
1043 * settings, or 'both'. Defaults to 'both'.
1044 * @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current blog.
1045 * @return bool Whether the theme is allowed for the network. Returns true in single-site.
1047 public function is_allowed( $check = 'both', $blog_id = null ) {
1048 if ( ! is_multisite() )
1051 if ( 'both' == $check || 'network' == $check ) {
1052 $allowed = self::get_allowed_on_network();
1053 if ( ! empty( $allowed[ $this->get_stylesheet() ] ) )
1057 if ( 'both' == $check || 'site' == $check ) {
1058 $allowed = self::get_allowed_on_site( $blog_id );
1059 if ( ! empty( $allowed[ $this->get_stylesheet() ] ) )
1067 * Returns array of stylesheet names of themes allowed on the site or network.
1072 * @param int $blog_id Optional. Defaults to current blog.
1073 * @return array Array of stylesheet names.
1075 public static function get_allowed( $blog_id = null ) {
1076 $network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
1077 return $network + self::get_allowed_on_site( $blog_id );
1081 * Returns array of stylesheet names of themes allowed on the network.
1086 * @return array Array of stylesheet names.
1088 public static function get_allowed_on_network() {
1089 static $allowed_themes;
1090 if ( ! isset( $allowed_themes ) )
1091 $allowed_themes = (array) get_site_option( 'allowedthemes' );
1092 return $allowed_themes;
1096 * Returns array of stylesheet names of themes allowed on the site.
1101 * @param int $blog_id Optional. Defaults to current blog.
1102 * @return array Array of stylesheet names.
1104 public static function get_allowed_on_site( $blog_id = null ) {
1105 static $allowed_themes = array();
1107 if ( ! $blog_id || ! is_multisite() )
1108 $blog_id = get_current_blog_id();
1110 if ( isset( $allowed_themes[ $blog_id ] ) )
1111 return $allowed_themes[ $blog_id ];
1113 $current = $blog_id == get_current_blog_id();
1116 $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1118 switch_to_blog( $blog_id );
1119 $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1120 restore_current_blog();
1123 // This is all super old MU back compat joy.
1124 // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
1125 if ( false === $allowed_themes[ $blog_id ] ) {
1127 $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1129 switch_to_blog( $blog_id );
1130 $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1131 restore_current_blog();
1134 if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
1135 $allowed_themes[ $blog_id ] = array();
1137 $converted = array();
1138 $themes = wp_get_themes();
1139 foreach ( $themes as $stylesheet => $theme_data ) {
1140 if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get('Name') ] ) )
1141 $converted[ $stylesheet ] = true;
1143 $allowed_themes[ $blog_id ] = $converted;
1145 // Set the option so we never have to go through this pain again.
1146 if ( is_admin() && $allowed_themes[ $blog_id ] ) {
1148 update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1149 delete_option( 'allowed_themes' );
1151 switch_to_blog( $blog_id );
1152 update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1153 delete_option( 'allowed_themes' );
1154 restore_current_blog();
1159 return (array) $allowed_themes[ $blog_id ];
1163 * Sort themes by name.
1168 public static function sort_by_name( &$themes ) {
1169 if ( 0 === strpos( get_locale(), 'en_' ) ) {
1170 uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
1172 uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) );
1177 * Callback function for usort() to naturally sort themes by name.
1179 * Accesses the Name header directly from the class for maximum speed.
1180 * Would choke on HTML but we don't care enough to slow it down with strip_tags().
1185 private static function _name_sort( $a, $b ) {
1186 return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] );
1190 * Name sort (with translation).
1195 private static function _name_sort_i18n( $a, $b ) {
1196 // Don't mark up; Do translate.
1197 return strnatcasecmp( $a->display( 'Name', false, true ), $b->display( 'Name', false, true ) );