[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-theme.php (source)

   1  <?php
   2  /**
   3   * WP_Theme Class
   4   *
   5   * @package WordPress
   6   * @subpackage Theme
   7   * @since 3.4.0
   8   */
   9  final class WP_Theme implements ArrayAccess {
  10  
  11      /**
  12       * Whether the theme has been marked as updateable.
  13       *
  14       * @since 4.4.0
  15       * @var bool
  16       *
  17       * @see WP_MS_Themes_List_Table
  18       */
  19      public $update = false;
  20  
  21      /**
  22       * Headers for style.css files.
  23       *
  24       * @since 3.4.0
  25       * @since 5.4.0 Added `Requires at least` and `Requires PHP` headers.
  26       * @var array
  27       */
  28      private static $file_headers = array(
  29          'Name'        => 'Theme Name',
  30          'ThemeURI'    => 'Theme URI',
  31          'Description' => 'Description',
  32          'Author'      => 'Author',
  33          'AuthorURI'   => 'Author URI',
  34          'Version'     => 'Version',
  35          'Template'    => 'Template',
  36          'Status'      => 'Status',
  37          'Tags'        => 'Tags',
  38          'TextDomain'  => 'Text Domain',
  39          'DomainPath'  => 'Domain Path',
  40          'RequiresWP'  => 'Requires at least',
  41          'RequiresPHP' => 'Requires PHP',
  42      );
  43  
  44      /**
  45       * Default themes.
  46       *
  47       * @var array
  48       */
  49      private static $default_themes = array(
  50          'classic'         => 'WordPress Classic',
  51          'default'         => 'WordPress Default',
  52          'twentyten'       => 'Twenty Ten',
  53          'twentyeleven'    => 'Twenty Eleven',
  54          'twentytwelve'    => 'Twenty Twelve',
  55          'twentythirteen'  => 'Twenty Thirteen',
  56          'twentyfourteen'  => 'Twenty Fourteen',
  57          'twentyfifteen'   => 'Twenty Fifteen',
  58          'twentysixteen'   => 'Twenty Sixteen',
  59          'twentyseventeen' => 'Twenty Seventeen',
  60          'twentynineteen'  => 'Twenty Nineteen',
  61          'twentytwenty'    => 'Twenty Twenty',
  62      );
  63  
  64      /**
  65       * Renamed theme tags.
  66       *
  67       * @var array
  68       */
  69      private static $tag_map = array(
  70          'fixed-width'    => 'fixed-layout',
  71          'flexible-width' => 'fluid-layout',
  72      );
  73  
  74      /**
  75       * Absolute path to the theme root, usually wp-content/themes
  76       *
  77       * @var string
  78       */
  79      private $theme_root;
  80  
  81      /**
  82       * Header data from the theme's style.css file.
  83       *
  84       * @var array
  85       */
  86      private $headers = array();
  87  
  88      /**
  89       * Header data from the theme's style.css file after being sanitized.
  90       *
  91       * @var array
  92       */
  93      private $headers_sanitized;
  94  
  95      /**
  96       * Header name from the theme's style.css after being translated.
  97       *
  98       * Cached due to sorting functions running over the translated name.
  99       *
 100       * @var string
 101       */
 102      private $name_translated;
 103  
 104      /**
 105       * Errors encountered when initializing the theme.
 106       *
 107       * @var WP_Error
 108       */
 109      private $errors;
 110  
 111      /**
 112       * The directory name of the theme's files, inside the theme root.
 113       *
 114       * In the case of a child theme, this is directory name of the child theme.
 115       * Otherwise, 'stylesheet' is the same as 'template'.
 116       *
 117       * @var string
 118       */
 119      private $stylesheet;
 120  
 121      /**
 122       * The directory name of the theme's files, inside the theme root.
 123       *
 124       * In the case of a child theme, this is the directory name of the parent theme.
 125       * Otherwise, 'template' is the same as 'stylesheet'.
 126       *
 127       * @var string
 128       */
 129      private $template;
 130  
 131      /**
 132       * A reference to the parent theme, in the case of a child theme.
 133       *
 134       * @var WP_Theme
 135       */
 136      private $parent;
 137  
 138      /**
 139       * URL to the theme root, usually an absolute URL to wp-content/themes
 140       *
 141       * @var string
 142       */
 143      private $theme_root_uri;
 144  
 145      /**
 146       * Flag for whether the theme's textdomain is loaded.
 147       *
 148       * @var bool
 149       */
 150      private $textdomain_loaded;
 151  
 152      /**
 153       * Stores an md5 hash of the theme root, to function as the cache key.
 154       *
 155       * @var string
 156       */
 157      private $cache_hash;
 158  
 159      /**
 160       * Flag for whether the themes cache bucket should be persistently cached.
 161       *
 162       * Default is false. Can be set with the {@see 'wp_cache_themes_persistently'} filter.
 163       *
 164       * @var bool
 165       */
 166      private static $persistently_cache;
 167  
 168      /**
 169       * Expiration time for the themes cache bucket.
 170       *
 171       * By default the bucket is not cached, so this value is useless.
 172       *
 173       * @var bool
 174       */
 175      private static $cache_expiration = 1800;
 176  
 177      /**
 178       * Constructor for WP_Theme.
 179       *
 180       * @since 3.4.0
 181       *
 182       * @global array $wp_theme_directories
 183       *
 184       * @param string $theme_dir Directory of the theme within the theme_root.
 185       * @param string $theme_root Theme root.
 186       * @param WP_Theme|null $_child If this theme is a parent theme, the child may be passed for validation purposes.
 187       */
 188  	public function __construct( $theme_dir, $theme_root, $_child = null ) {
 189          global $wp_theme_directories;
 190  
 191          // Initialize caching on first run.
 192          if ( ! isset( self::$persistently_cache ) ) {
 193              /** This action is documented in wp-includes/theme.php */
 194              self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' );
 195              if ( self::$persistently_cache ) {
 196                  wp_cache_add_global_groups( 'themes' );
 197                  if ( is_int( self::$persistently_cache ) ) {
 198                      self::$cache_expiration = self::$persistently_cache;
 199                  }
 200              } else {
 201                  wp_cache_add_non_persistent_groups( 'themes' );
 202              }
 203          }
 204  
 205          $this->theme_root = $theme_root;
 206          $this->stylesheet = $theme_dir;
 207  
 208          // Correct a situation where the theme is 'some-directory/some-theme' but 'some-directory' was passed in as part of the theme root instead.
 209          if ( ! in_array( $theme_root, (array) $wp_theme_directories ) && in_array( dirname( $theme_root ), (array) $wp_theme_directories ) ) {
 210              $this->stylesheet = basename( $this->theme_root ) . '/' . $this->stylesheet;
 211              $this->theme_root = dirname( $theme_root );
 212          }
 213  
 214          $this->cache_hash = md5( $this->theme_root . '/' . $this->stylesheet );
 215          $theme_file       = $this->stylesheet . '/style.css';
 216  
 217          $cache = $this->cache_get( 'theme' );
 218  
 219          if ( is_array( $cache ) ) {
 220              foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
 221                  if ( isset( $cache[ $key ] ) ) {
 222                      $this->$key = $cache[ $key ];
 223                  }
 224              }
 225              if ( $this->errors ) {
 226                  return;
 227              }
 228              if ( isset( $cache['theme_root_template'] ) ) {
 229                  $theme_root_template = $cache['theme_root_template'];
 230              }
 231          } elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) {
 232              $this->headers['Name'] = $this->stylesheet;
 233              if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) ) {
 234                  $this->errors = new WP_Error(
 235                      'theme_not_found',
 236                      sprintf(
 237                          /* translators: %s: Theme directory name. */
 238                          __( 'The theme directory "%s" does not exist.' ),
 239                          esc_html( $this->stylesheet )
 240                      )
 241                  );
 242              } else {
 243                  $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
 244              }
 245              $this->template = $this->stylesheet;
 246              $this->cache_add(
 247                  'theme',
 248                  array(
 249                      'headers'    => $this->headers,
 250                      'errors'     => $this->errors,
 251                      'stylesheet' => $this->stylesheet,
 252                      'template'   => $this->template,
 253                  )
 254              );
 255              if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one.
 256                  $this->errors->add( 'theme_root_missing', __( 'Error: The themes directory is either empty or doesn&#8217;t exist. Please check your installation.' ) );
 257              }
 258              return;
 259          } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) {
 260              $this->headers['Name'] = $this->stylesheet;
 261              $this->errors          = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
 262              $this->template        = $this->stylesheet;
 263              $this->cache_add(
 264                  'theme',
 265                  array(
 266                      'headers'    => $this->headers,
 267                      'errors'     => $this->errors,
 268                      'stylesheet' => $this->stylesheet,
 269                      'template'   => $this->template,
 270                  )
 271              );
 272              return;
 273          } else {
 274              $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' );
 275              // Default themes always trump their pretenders.
 276              // Properly identify default themes that are inside a directory within wp-content/themes.
 277              $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes );
 278              if ( $default_theme_slug ) {
 279                  if ( basename( $this->stylesheet ) != $default_theme_slug ) {
 280                      $this->headers['Name'] .= '/' . $this->stylesheet;
 281                  }
 282              }
 283          }
 284  
 285          if ( ! $this->template && $this->stylesheet === $this->headers['Template'] ) {
 286              $this->errors = new WP_Error(
 287                  'theme_child_invalid',
 288                  sprintf(
 289                      /* translators: %s: Template. */
 290                      __( 'The theme defines itself as its parent theme. Please check the %s header.' ),
 291                      '<code>Template</code>'
 292                  )
 293              );
 294              $this->cache_add(
 295                  'theme',
 296                  array(
 297                      'headers'    => $this->headers,
 298                      'errors'     => $this->errors,
 299                      'stylesheet' => $this->stylesheet,
 300                  )
 301              );
 302  
 303              return;
 304          }
 305  
 306          // (If template is set from cache [and there are no errors], we know it's good.)
 307          if ( ! $this->template ) {
 308              $this->template = $this->headers['Template'];
 309          }
 310  
 311          if ( ! $this->template ) {
 312              $this->template = $this->stylesheet;
 313              if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
 314                  $error_message = sprintf(
 315                      /* translators: 1: index.php, 2: Documentation URL, 3: style.css */
 316                      __( 'Template is missing. Standalone themes need to have a %1$s template file. <a href="%2$s">Child themes</a> need to have a Template header in the %3$s stylesheet.' ),
 317                      '<code>index.php</code>',
 318                      __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
 319                      '<code>style.css</code>'
 320                  );
 321                  $this->errors = new WP_Error( 'theme_no_index', $error_message );
 322                  $this->cache_add(
 323                      'theme',
 324                      array(
 325                          'headers'    => $this->headers,
 326                          'errors'     => $this->errors,
 327                          'stylesheet' => $this->stylesheet,
 328                          'template'   => $this->template,
 329                      )
 330                  );
 331                  return;
 332              }
 333          }
 334  
 335          // If we got our data from cache, we can assume that 'template' is pointing to the right place.
 336          if ( ! is_array( $cache ) && $this->template != $this->stylesheet && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) ) {
 337              // If we're in a directory of themes inside /themes, look for the parent nearby.
 338              // wp-content/themes/directory-of-themes/*
 339              $parent_dir  = dirname( $this->stylesheet );
 340              $directories = search_theme_directories();
 341              if ( '.' != $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) {
 342                  $this->template = $parent_dir . '/' . $this->template;
 343              } elseif ( $directories && isset( $directories[ $this->template ] ) ) {
 344                  // Look for the template in the search_theme_directories() results, in case it is in another theme root.
 345                  // We don't look into directories of themes, just the theme root.
 346                  $theme_root_template = $directories[ $this->template ]['theme_root'];
 347              } else {
 348                  // Parent theme is missing.
 349                  $this->errors = new WP_Error(
 350                      'theme_no_parent',
 351                      sprintf(
 352                          /* translators: %s: Theme directory name. */
 353                          __( 'The parent theme is missing. Please install the "%s" parent theme.' ),
 354                          esc_html( $this->template )
 355                      )
 356                  );
 357                  $this->cache_add(
 358                      'theme',
 359                      array(
 360                          'headers'    => $this->headers,
 361                          'errors'     => $this->errors,
 362                          'stylesheet' => $this->stylesheet,
 363                          'template'   => $this->template,
 364                      )
 365                  );
 366                  $this->parent = new WP_Theme( $this->template, $this->theme_root, $this );
 367                  return;
 368              }
 369          }
 370  
 371          // Set the parent, if we're a child theme.
 372          if ( $this->template != $this->stylesheet ) {
 373              // If we are a parent, then there is a problem. Only two generations allowed! Cancel things out.
 374              if ( $_child instanceof WP_Theme && $_child->template == $this->stylesheet ) {
 375                  $_child->parent = null;
 376                  $_child->errors = new WP_Error(
 377                      'theme_parent_invalid',
 378                      sprintf(
 379                          /* translators: %s: Theme directory name. */
 380                          __( 'The "%s" theme is not a valid parent theme.' ),
 381                          esc_html( $_child->template )
 382                      )
 383                  );
 384                  $_child->cache_add(
 385                      'theme',
 386                      array(
 387                          'headers'    => $_child->headers,
 388                          'errors'     => $_child->errors,
 389                          'stylesheet' => $_child->stylesheet,
 390                          'template'   => $_child->template,
 391                      )
 392                  );
 393                  // The two themes actually reference each other with the Template header.
 394                  if ( $_child->stylesheet == $this->template ) {
 395                      $this->errors = new WP_Error(
 396                          'theme_parent_invalid',
 397                          sprintf(
 398                              /* translators: %s: Theme directory name. */
 399                              __( 'The "%s" theme is not a valid parent theme.' ),
 400                              esc_html( $this->template )
 401                          )
 402                      );
 403                      $this->cache_add(
 404                          'theme',
 405                          array(
 406                              'headers'    => $this->headers,
 407                              'errors'     => $this->errors,
 408                              'stylesheet' => $this->stylesheet,
 409                              'template'   => $this->template,
 410                          )
 411                      );
 412                  }
 413                  return;
 414              }
 415              // Set the parent. Pass the current instance so we can do the crazy checks above and assess errors.
 416              $this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this );
 417          }
 418  
 419          if ( wp_paused_themes()->get( $this->stylesheet ) && ( ! is_wp_error( $this->errors ) || ! isset( $this->errors->errors['theme_paused'] ) ) ) {
 420              $this->errors = new WP_Error( 'theme_paused', __( 'This theme failed to load properly and was paused within the admin backend.' ) );
 421          }
 422  
 423          // We're good. If we didn't retrieve from cache, set it.
 424          if ( ! is_array( $cache ) ) {
 425              $cache = array(
 426                  'headers'    => $this->headers,
 427                  'errors'     => $this->errors,
 428                  'stylesheet' => $this->stylesheet,
 429                  'template'   => $this->template,
 430              );
 431              // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above.
 432              if ( isset( $theme_root_template ) ) {
 433                  $cache['theme_root_template'] = $theme_root_template;
 434              }
 435              $this->cache_add( 'theme', $cache );
 436          }
 437      }
 438  
 439      /**
 440       * When converting the object to a string, the theme name is returned.
 441       *
 442       * @since 3.4.0
 443       *
 444       * @return string Theme name, ready for display (translated)
 445       */
 446  	public function __toString() {
 447          return (string) $this->display( 'Name' );
 448      }
 449  
 450      /**
 451       * __isset() magic method for properties formerly returned by current_theme_info()
 452       *
 453       * @staticvar array $properties
 454       *
 455       * @since 3.4.0
 456       *
 457       * @param string $offset Property to check if set.
 458       * @return bool Whether the given property is set.
 459       */
 460  	public function __isset( $offset ) {
 461          static $properties = array(
 462              'name',
 463              'title',
 464              'version',
 465              'parent_theme',
 466              'template_dir',
 467              'stylesheet_dir',
 468              'template',
 469              'stylesheet',
 470              'screenshot',
 471              'description',
 472              'author',
 473              'tags',
 474              'theme_root',
 475              'theme_root_uri',
 476          );
 477  
 478          return in_array( $offset, $properties );
 479      }
 480  
 481      /**
 482       * __get() magic method for properties formerly returned by current_theme_info()
 483       *
 484       * @since 3.4.0
 485       *
 486       * @param string $offset Property to get.
 487       * @return mixed Property value.
 488       */
 489  	public function __get( $offset ) {
 490          switch ( $offset ) {
 491              case 'name':
 492              case 'title':
 493                  return $this->get( 'Name' );
 494              case 'version':
 495                  return $this->get( 'Version' );
 496              case 'parent_theme':
 497                  return $this->parent() ? $this->parent()->get( 'Name' ) : '';
 498              case 'template_dir':
 499                  return $this->get_template_directory();
 500              case 'stylesheet_dir':
 501                  return $this->get_stylesheet_directory();
 502              case 'template':
 503                  return $this->get_template();
 504              case 'stylesheet':
 505                  return $this->get_stylesheet();
 506              case 'screenshot':
 507                  return $this->get_screenshot( 'relative' );
 508              // 'author' and 'description' did not previously return translated data.
 509              case 'description':
 510                  return $this->display( 'Description' );
 511              case 'author':
 512                  return $this->display( 'Author' );
 513              case 'tags':
 514                  return $this->get( 'Tags' );
 515              case 'theme_root':
 516                  return $this->get_theme_root();
 517              case 'theme_root_uri':
 518                  return $this->get_theme_root_uri();
 519              // For cases where the array was converted to an object.
 520              default:
 521                  return $this->offsetGet( $offset );
 522          }
 523      }
 524  
 525      /**
 526       * Method to implement ArrayAccess for keys formerly returned by get_themes()
 527       *
 528       * @since 3.4.0
 529       *
 530       * @param mixed $offset
 531       * @param mixed $value
 532       */
 533  	public function offsetSet( $offset, $value ) {}
 534  
 535      /**
 536       * Method to implement ArrayAccess for keys formerly returned by get_themes()
 537       *
 538       * @since 3.4.0
 539       *
 540       * @param mixed $offset
 541       */
 542  	public function offsetUnset( $offset ) {}
 543  
 544      /**
 545       * Method to implement ArrayAccess for keys formerly returned by get_themes()
 546       *
 547       * @staticvar array $keys
 548       *
 549       * @since 3.4.0
 550       *
 551       * @param mixed $offset
 552       * @return bool
 553       */
 554  	public function offsetExists( $offset ) {
 555          static $keys = array(
 556              'Name',
 557              'Version',
 558              'Status',
 559              'Title',
 560              'Author',
 561              'Author Name',
 562              'Author URI',
 563              'Description',
 564              'Template',
 565              'Stylesheet',
 566              'Template Files',
 567              'Stylesheet Files',
 568              'Template Dir',
 569              'Stylesheet Dir',
 570              'Screenshot',
 571              'Tags',
 572              'Theme Root',
 573              'Theme Root URI',
 574              'Parent Theme',
 575          );
 576  
 577          return in_array( $offset, $keys );
 578      }
 579  
 580      /**
 581       * Method to implement ArrayAccess for keys formerly returned by get_themes().
 582       *
 583       * Author, Author Name, Author URI, and Description did not previously return
 584       * translated data. We are doing so now as it is safe to do. However, as
 585       * Name and Title could have been used as the key for get_themes(), both remain
 586       * untranslated for back compatibility. This means that ['Name'] is not ideal,
 587       * and care should be taken to use `$theme::display( 'Name' )` to get a properly
 588       * translated header.
 589       *
 590       * @since 3.4.0
 591       *
 592       * @param mixed $offset
 593       * @return mixed
 594       */
 595  	public function offsetGet( $offset ) {
 596          switch ( $offset ) {
 597              case 'Name':
 598              case 'Title':
 599                  /*
 600                   * See note above about using translated data. get() is not ideal.
 601                   * It is only for backward compatibility. Use display().
 602                   */
 603                  return $this->get( 'Name' );
 604              case 'Author':
 605                  return $this->display( 'Author' );
 606              case 'Author Name':
 607                  return $this->display( 'Author', false );
 608              case 'Author URI':
 609                  return $this->display( 'AuthorURI' );
 610              case 'Description':
 611                  return $this->display( 'Description' );
 612              case 'Version':
 613              case 'Status':
 614                  return $this->get( $offset );
 615              case 'Template':
 616                  return $this->get_template();
 617              case 'Stylesheet':
 618                  return $this->get_stylesheet();
 619              case 'Template Files':
 620                  return $this->get_files( 'php', 1, true );
 621              case 'Stylesheet Files':
 622                  return $this->get_files( 'css', 0, false );
 623              case 'Template Dir':
 624                  return $this->get_template_directory();
 625              case 'Stylesheet Dir':
 626                  return $this->get_stylesheet_directory();
 627              case 'Screenshot':
 628                  return $this->get_screenshot( 'relative' );
 629              case 'Tags':
 630                  return $this->get( 'Tags' );
 631              case 'Theme Root':
 632                  return $this->get_theme_root();
 633              case 'Theme Root URI':
 634                  return $this->get_theme_root_uri();
 635              case 'Parent Theme':
 636                  return $this->parent() ? $this->parent()->get( 'Name' ) : '';
 637              default:
 638                  return null;
 639          }
 640      }
 641  
 642      /**
 643       * Returns errors property.
 644       *
 645       * @since 3.4.0
 646       *
 647       * @return WP_Error|false WP_Error if there are errors, or false.
 648       */
 649  	public function errors() {
 650          return is_wp_error( $this->errors ) ? $this->errors : false;
 651      }
 652  
 653      /**
 654       * Whether the theme exists.
 655       *
 656       * A theme with errors exists. A theme with the error of 'theme_not_found',
 657       * meaning that the theme's directory was not found, does not exist.
 658       *
 659       * @since 3.4.0
 660       *
 661       * @return bool Whether the theme exists.
 662       */
 663  	public function exists() {
 664          return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes() ) );
 665      }
 666  
 667      /**
 668       * Returns reference to the parent theme.
 669       *
 670       * @since 3.4.0
 671       *
 672       * @return WP_Theme|false Parent theme, or false if the current theme is not a child theme.
 673       */
 674  	public function parent() {
 675          return isset( $this->parent ) ? $this->parent : false;
 676      }
 677  
 678      /**
 679       * Adds theme data to cache.
 680       *
 681       * Cache entries keyed by the theme and the type of data.
 682       *
 683       * @since 3.4.0
 684       *
 685       * @param string $key Type of data to store (theme, screenshot, headers, post_templates)
 686       * @param array|string $data Data to store
 687       * @return bool Return value from wp_cache_add()
 688       */
 689  	private function cache_add( $key, $data ) {
 690          return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration );
 691      }
 692  
 693      /**
 694       * Gets theme data from cache.
 695       *
 696       * Cache entries are keyed by the theme and the type of data.
 697       *
 698       * @since 3.4.0
 699       *
 700       * @param string $key Type of data to retrieve (theme, screenshot, headers, post_templates)
 701       * @return mixed Retrieved data
 702       */
 703  	private function cache_get( $key ) {
 704          return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' );
 705      }
 706  
 707      /**
 708       * Clears the cache for the theme.
 709       *
 710       * @since 3.4.0
 711       */
 712  	public function cache_delete() {
 713          foreach ( array( 'theme', 'screenshot', 'headers', 'post_templates' ) as $key ) {
 714              wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' );
 715          }
 716          $this->template          = null;
 717          $this->textdomain_loaded = null;
 718          $this->theme_root_uri    = null;
 719          $this->parent            = null;
 720          $this->errors            = null;
 721          $this->headers_sanitized = null;
 722          $this->name_translated   = null;
 723          $this->headers           = array();
 724          $this->__construct( $this->stylesheet, $this->theme_root );
 725      }
 726  
 727      /**
 728       * Get a raw, unformatted theme header.
 729       *
 730       * The header is sanitized, but is not translated, and is not marked up for display.
 731       * To get a theme header for display, use the display() method.
 732       *
 733       * Use the get_template() method, not the 'Template' header, for finding the template.
 734       * The 'Template' header is only good for what was written in the style.css, while
 735       * get_template() takes into account where WordPress actually located the theme and
 736       * whether it is actually valid.
 737       *
 738       * @since 3.4.0
 739       *
 740       * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 741       * @return string|array|false String or array (for Tags header) on success, false on failure.
 742       */
 743  	public function get( $header ) {
 744          if ( ! isset( $this->headers[ $header ] ) ) {
 745              return false;
 746          }
 747  
 748          if ( ! isset( $this->headers_sanitized ) ) {
 749              $this->headers_sanitized = $this->cache_get( 'headers' );
 750              if ( ! is_array( $this->headers_sanitized ) ) {
 751                  $this->headers_sanitized = array();
 752              }
 753          }
 754  
 755          if ( isset( $this->headers_sanitized[ $header ] ) ) {
 756              return $this->headers_sanitized[ $header ];
 757          }
 758  
 759          // If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets.
 760          if ( self::$persistently_cache ) {
 761              foreach ( array_keys( $this->headers ) as $_header ) {
 762                  $this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] );
 763              }
 764              $this->cache_add( 'headers', $this->headers_sanitized );
 765          } else {
 766              $this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] );
 767          }
 768  
 769          return $this->headers_sanitized[ $header ];
 770      }
 771  
 772      /**
 773       * Gets a theme header, formatted and translated for display.
 774       *
 775       * @since 3.4.0
 776       *
 777       * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 778       * @param bool $markup Optional. Whether to mark up the header. Defaults to true.
 779       * @param bool $translate Optional. Whether to translate the header. Defaults to true.
 780       * @return string|array|false Processed header. An array for Tags if `$markup` is false, string otherwise.
 781       *                            False on failure.
 782       */
 783  	public function display( $header, $markup = true, $translate = true ) {
 784          $value = $this->get( $header );
 785          if ( false === $value ) {
 786              return false;
 787          }
 788  
 789          if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) ) {
 790              $translate = false;
 791          }
 792  
 793          if ( $translate ) {
 794              $value = $this->translate_header( $header, $value );
 795          }
 796  
 797          if ( $markup ) {
 798              $value = $this->markup_header( $header, $value, $translate );
 799          }
 800  
 801          return $value;
 802      }
 803  
 804      /**
 805       * Sanitize a theme header.
 806       *
 807       * @since 3.4.0
 808       * @since 5.4.0 Added support for `Requires at least` and `Requires PHP` headers.
 809       *
 810       * @staticvar array $header_tags
 811       * @staticvar array $header_tags_with_a
 812       *
 813       * @param string $header Theme header. Accepts 'Name', 'Description', 'Author', 'Version',
 814       *                       'ThemeURI', 'AuthorURI', 'Status', 'Tags', 'RequiresWP', 'RequiresPHP'.
 815       * @param string $value  Value to sanitize.
 816       * @return string|array An array for Tags header, string otherwise.
 817       */
 818  	private function sanitize_header( $header, $value ) {
 819          switch ( $header ) {
 820              case 'Status':
 821                  if ( ! $value ) {
 822                      $value = 'publish';
 823                      break;
 824                  }
 825                  // Fall through otherwise.
 826              case 'Name':
 827                  static $header_tags = array(
 828                      'abbr'    => array( 'title' => true ),
 829                      'acronym' => array( 'title' => true ),
 830                      'code'    => true,
 831                      'em'      => true,
 832                      'strong'  => true,
 833                  );
 834  
 835                  $value = wp_kses( $value, $header_tags );
 836                  break;
 837              case 'Author':
 838                  // There shouldn't be anchor tags in Author, but some themes like to be challenging.
 839              case 'Description':
 840                  static $header_tags_with_a = array(
 841                      'a'       => array(
 842                          'href'  => true,
 843                          'title' => true,
 844                      ),
 845                      'abbr'    => array( 'title' => true ),
 846                      'acronym' => array( 'title' => true ),
 847                      'code'    => true,
 848                      'em'      => true,
 849                      'strong'  => true,
 850                  );
 851  
 852                  $value = wp_kses( $value, $header_tags_with_a );
 853                  break;
 854              case 'ThemeURI':
 855              case 'AuthorURI':
 856                  $value = esc_url_raw( $value );
 857                  break;
 858              case 'Tags':
 859                  $value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
 860                  break;
 861              case 'Version':
 862              case 'RequiresWP':
 863              case 'RequiresPHP':
 864                  $value = strip_tags( $value );
 865                  break;
 866          }
 867  
 868          return $value;
 869      }
 870  
 871      /**
 872       * Mark up a theme header.
 873       *
 874       * @since 3.4.0
 875       *
 876       * @staticvar string $comma
 877       *
 878       * @param string       $header    Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 879       * @param string|array $value     Value to mark up. An array for Tags header, string otherwise.
 880       * @param string       $translate Whether the header has been translated.
 881       * @return string Value, marked up.
 882       */
 883  	private function markup_header( $header, $value, $translate ) {
 884          switch ( $header ) {
 885              case 'Name':
 886                  if ( empty( $value ) ) {
 887                      $value = esc_html( $this->get_stylesheet() );
 888                  }
 889                  break;
 890              case 'Description':
 891                  $value = wptexturize( $value );
 892                  break;
 893              case 'Author':
 894                  if ( $this->get( 'AuthorURI' ) ) {
 895                      $value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value );
 896                  } elseif ( ! $value ) {
 897                      $value = __( 'Anonymous' );
 898                  }
 899                  break;
 900              case 'Tags':
 901                  static $comma = null;
 902                  if ( ! isset( $comma ) ) {
 903                      /* translators: Used between list items, there is a space after the comma. */
 904                      $comma = __( ', ' );
 905                  }
 906                  $value = implode( $comma, $value );
 907                  break;
 908              case 'ThemeURI':
 909              case 'AuthorURI':
 910                  $value = esc_url( $value );
 911                  break;
 912          }
 913  
 914          return $value;
 915      }
 916  
 917      /**
 918       * Translate a theme header.
 919       *
 920       * @since 3.4.0
 921       *
 922       * @staticvar array $tags_list
 923       *
 924       * @param string       $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 925       * @param string|array $value  Value to translate. An array for Tags header, string otherwise.
 926       * @return string|array Translated value. An array for Tags header, string otherwise.
 927       */
 928  	private function translate_header( $header, $value ) {
 929          switch ( $header ) {
 930              case 'Name':
 931                  // Cached for sorting reasons.
 932                  if ( isset( $this->name_translated ) ) {
 933                      return $this->name_translated;
 934                  }
 935                  // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
 936                  $this->name_translated = translate( $value, $this->get( 'TextDomain' ) );
 937                  return $this->name_translated;
 938              case 'Tags':
 939                  if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) ) {
 940                      return $value;
 941                  }
 942  
 943                  static $tags_list;
 944                  if ( ! isset( $tags_list ) ) {
 945                      $tags_list = array(
 946                          // As of 4.6, deprecated tags which are only used to provide translation for older themes.
 947                          'black'             => __( 'Black' ),
 948                          'blue'              => __( 'Blue' ),
 949                          'brown'             => __( 'Brown' ),
 950                          'gray'              => __( 'Gray' ),
 951                          'green'             => __( 'Green' ),
 952                          'orange'            => __( 'Orange' ),
 953                          'pink'              => __( 'Pink' ),
 954                          'purple'            => __( 'Purple' ),
 955                          'red'               => __( 'Red' ),
 956                          'silver'            => __( 'Silver' ),
 957                          'tan'               => __( 'Tan' ),
 958                          'white'             => __( 'White' ),
 959                          'yellow'            => __( 'Yellow' ),
 960                          'dark'              => __( 'Dark' ),
 961                          'light'             => __( 'Light' ),
 962                          'fixed-layout'      => __( 'Fixed Layout' ),
 963                          'fluid-layout'      => __( 'Fluid Layout' ),
 964                          'responsive-layout' => __( 'Responsive Layout' ),
 965                          'blavatar'          => __( 'Blavatar' ),
 966                          'photoblogging'     => __( 'Photoblogging' ),
 967                          'seasonal'          => __( 'Seasonal' ),
 968                      );
 969  
 970                      $feature_list = get_theme_feature_list( false ); // No API.
 971                      foreach ( $feature_list as $tags ) {
 972                          $tags_list += $tags;
 973                      }
 974                  }
 975  
 976                  foreach ( $value as &$tag ) {
 977                      if ( isset( $tags_list[ $tag ] ) ) {
 978                          $tag = $tags_list[ $tag ];
 979                      } elseif ( isset( self::$tag_map[ $tag ] ) ) {
 980                          $tag = $tags_list[ self::$tag_map[ $tag ] ];
 981                      }
 982                  }
 983  
 984                  return $value;
 985  
 986              default:
 987                  // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
 988                  $value = translate( $value, $this->get( 'TextDomain' ) );
 989          }
 990          return $value;
 991      }
 992  
 993      /**
 994       * The directory name of the theme's "stylesheet" files, inside the theme root.
 995       *
 996       * In the case of a child theme, this is directory name of the child theme.
 997       * Otherwise, get_stylesheet() is the same as get_template().
 998       *
 999       * @since 3.4.0
1000       *
1001       * @return string Stylesheet
1002       */
1003  	public function get_stylesheet() {
1004          return $this->stylesheet;
1005      }
1006  
1007      /**
1008       * The directory name of the theme's "template" files, inside the theme root.
1009       *
1010       * In the case of a child theme, this is the directory name of the parent theme.
1011       * Otherwise, the get_template() is the same as get_stylesheet().
1012       *
1013       * @since 3.4.0
1014       *
1015       * @return string Template
1016       */
1017  	public function get_template() {
1018          return $this->template;
1019      }
1020  
1021      /**
1022       * Returns the absolute path to the directory of a theme's "stylesheet" files.
1023       *
1024       * In the case of a child theme, this is the absolute path to the directory
1025       * of the child theme's files.
1026       *
1027       * @since 3.4.0
1028       *
1029       * @return string Absolute path of the stylesheet directory.
1030       */
1031  	public function get_stylesheet_directory() {
1032          if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes() ) ) {
1033              return '';
1034          }
1035  
1036          return $this->theme_root . '/' . $this->stylesheet;
1037      }
1038  
1039      /**
1040       * Returns the absolute path to the directory of a theme's "template" files.
1041       *
1042       * In the case of a child theme, this is the absolute path to the directory
1043       * of the parent theme's files.
1044       *
1045       * @since 3.4.0
1046       *
1047       * @return string Absolute path of the template directory.
1048       */
1049  	public function get_template_directory() {
1050          if ( $this->parent() ) {
1051              $theme_root = $this->parent()->theme_root;
1052          } else {
1053              $theme_root = $this->theme_root;
1054          }
1055  
1056          return $theme_root . '/' . $this->template;
1057      }
1058  
1059      /**
1060       * Returns the URL to the directory of a theme's "stylesheet" files.
1061       *
1062       * In the case of a child theme, this is the URL to the directory of the
1063       * child theme's files.
1064       *
1065       * @since 3.4.0
1066       *
1067       * @return string URL to the stylesheet directory.
1068       */
1069  	public function get_stylesheet_directory_uri() {
1070          return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) );
1071      }
1072  
1073      /**
1074       * Returns the URL to the directory of a theme's "template" files.
1075       *
1076       * In the case of a child theme, this is the URL to the directory of the
1077       * parent theme's files.
1078       *
1079       * @since 3.4.0
1080       *
1081       * @return string URL to the template directory.
1082       */
1083  	public function get_template_directory_uri() {
1084          if ( $this->parent() ) {
1085              $theme_root_uri = $this->parent()->get_theme_root_uri();
1086          } else {
1087              $theme_root_uri = $this->get_theme_root_uri();
1088          }
1089  
1090          return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) );
1091      }
1092  
1093      /**
1094       * The absolute path to the directory of the theme root.
1095       *
1096       * This is typically the absolute path to wp-content/themes.
1097       *
1098       * @since 3.4.0
1099       *
1100       * @return string Theme root.
1101       */
1102  	public function get_theme_root() {
1103          return $this->theme_root;
1104      }
1105  
1106      /**
1107       * Returns the URL to the directory of the theme root.
1108       *
1109       * This is typically the absolute URL to wp-content/themes. This forms the basis
1110       * for all other URLs returned by WP_Theme, so we pass it to the public function
1111       * get_theme_root_uri() and allow it to run the {@see 'theme_root_uri'} filter.
1112       *
1113       * @since 3.4.0
1114       *
1115       * @return string Theme root URI.
1116       */
1117  	public function get_theme_root_uri() {
1118          if ( ! isset( $this->theme_root_uri ) ) {
1119              $this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root );
1120          }
1121          return $this->theme_root_uri;
1122      }
1123  
1124      /**
1125       * Returns the main screenshot file for the theme.
1126       *
1127       * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed.
1128       *
1129       * Screenshots for a theme must be in the stylesheet directory. (In the case of child
1130       * themes, parent theme screenshots are not inherited.)
1131       *
1132       * @since 3.4.0
1133       *
1134       * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI.
1135       * @return string|false Screenshot file. False if the theme does not have a screenshot.
1136       */
1137  	public function get_screenshot( $uri = 'uri' ) {
1138          $screenshot = $this->cache_get( 'screenshot' );
1139          if ( $screenshot ) {
1140              if ( 'relative' == $uri ) {
1141                  return $screenshot;
1142              }
1143              return $this->get_stylesheet_directory_uri() . '/' . $screenshot;
1144          } elseif ( 0 === $screenshot ) {
1145              return false;
1146          }
1147  
1148          foreach ( array( 'png', 'gif', 'jpg', 'jpeg' ) as $ext ) {
1149              if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
1150                  $this->cache_add( 'screenshot', 'screenshot.' . $ext );
1151                  if ( 'relative' == $uri ) {
1152                      return 'screenshot.' . $ext;
1153                  }
1154                  return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext;
1155              }
1156          }
1157  
1158          $this->cache_add( 'screenshot', 0 );
1159          return false;
1160      }
1161  
1162      /**
1163       * Return files in the theme's directory.
1164       *
1165       * @since 3.4.0
1166       *
1167       * @param mixed $type Optional. Array of extensions to return. Defaults to all files (null).
1168       * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.
1169       * @param bool $search_parent Optional. Whether to return parent files. Defaults to false.
1170       * @return string[] Array of files, keyed by the path to the file relative to the theme's directory, with the values
1171       *                  being absolute paths.
1172       */
1173  	public function get_files( $type = null, $depth = 0, $search_parent = false ) {
1174          $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
1175  
1176          if ( $search_parent && $this->parent() ) {
1177              $files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
1178          }
1179  
1180          return $files;
1181      }
1182  
1183      /**
1184       * Returns the theme's post templates.
1185       *
1186       * @since 4.7.0
1187       *
1188       * @return string Array of page templates, keyed by filename and post type,
1189       *               with the value of the translated header name.
1190       */
1191  	public function get_post_templates() {
1192          // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
1193          if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {
1194              return array();
1195          }
1196  
1197          $post_templates = $this->cache_get( 'post_templates' );
1198  
1199          if ( ! is_array( $post_templates ) ) {
1200              $post_templates = array();
1201  
1202              $files = (array) $this->get_files( 'php', 1, true );
1203  
1204              foreach ( $files as $file => $full_path ) {
1205                  if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {
1206                      continue;
1207                  }
1208  
1209                  $types = array( 'page' );
1210                  if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {
1211                      $types = explode( ',', _cleanup_header_comment( $type[1] ) );
1212                  }
1213  
1214                  foreach ( $types as $type ) {
1215                      $type = sanitize_key( $type );
1216                      if ( ! isset( $post_templates[ $type ] ) ) {
1217                          $post_templates[ $type ] = array();
1218                      }
1219  
1220                      $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
1221                  }
1222              }
1223  
1224              $this->cache_add( 'post_templates', $post_templates );
1225          }
1226  
1227          if ( $this->load_textdomain() ) {
1228              foreach ( $post_templates as &$post_type ) {
1229                  foreach ( $post_type as &$post_template ) {
1230                      $post_template = $this->translate_header( 'Template Name', $post_template );
1231                  }
1232              }
1233          }
1234  
1235          return $post_templates;
1236      }
1237  
1238      /**
1239       * Returns the theme's post templates for a given post type.
1240       *
1241       * @since 3.4.0
1242       * @since 4.7.0 Added the `$post_type` parameter.
1243       *
1244       * @param WP_Post|null $post      Optional. The post being edited, provided for context.
1245       * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
1246       *                                If a post is provided, its post type is used.
1247       * @return string[] Array of template header names keyed by the template file name.
1248       */
1249  	public function get_page_templates( $post = null, $post_type = 'page' ) {
1250          if ( $post ) {
1251              $post_type = get_post_type( $post );
1252          }
1253  
1254          $post_templates = $this->get_post_templates();
1255          $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array();
1256  
1257          /**
1258           * Filters list of page templates for a theme.
1259           *
1260           * @since 4.9.6
1261           *
1262           * @param string[]     $post_templates Array of template header names keyed by the template file name.
1263           * @param WP_Theme     $this           The theme object.
1264           * @param WP_Post|null $post           The post being edited, provided for context, or null.
1265           * @param string       $post_type      Post type to get the templates for.
1266           */
1267          $post_templates = (array) apply_filters( 'theme_templates', $post_templates, $this, $post, $post_type );
1268  
1269          /**
1270           * Filters list of page templates for a theme.
1271           *
1272           * The dynamic portion of the hook name, `$post_type`, refers to the post type.
1273           *
1274           * @since 3.9.0
1275           * @since 4.4.0 Converted to allow complete control over the `$page_templates` array.
1276           * @since 4.7.0 Added the `$post_type` parameter.
1277           *
1278           * @param string[]     $post_templates Array of template header names keyed by the template file name.
1279           * @param WP_Theme     $this           The theme object.
1280           * @param WP_Post|null $post           The post being edited, provided for context, or null.
1281           * @param string       $post_type      Post type to get the templates for.
1282           */
1283          $post_templates = (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
1284  
1285          return $post_templates;
1286      }
1287  
1288      /**
1289       * Scans a directory for files of a certain extension.
1290       *
1291       * @since 3.4.0
1292       *
1293       * @param string            $path          Absolute path to search.
1294       * @param array|string|null $extensions    Optional. Array of extensions to find, string of a single extension,
1295       *                                         or null for all extensions. Default null.
1296       * @param int               $depth         Optional. How many levels deep to search for files. Accepts 0, 1+, or
1297       *                                         -1 (infinite depth). Default 0.
1298       * @param string            $relative_path Optional. The basename of the absolute path. Used to control the
1299       *                                         returned path for the found files, particularly when this function
1300       *                                         recurses to lower depths. Default empty.
1301       * @return string[]|false Array of files, keyed by the path to the file relative to the `$path` directory prepended
1302       *                        with `$relative_path`, with the values being absolute paths. False otherwise.
1303       */
1304  	private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
1305          if ( ! is_dir( $path ) ) {
1306              return false;
1307          }
1308  
1309          if ( $extensions ) {
1310              $extensions  = (array) $extensions;
1311              $_extensions = implode( '|', $extensions );
1312          }
1313  
1314          $relative_path = trailingslashit( $relative_path );
1315          if ( '/' == $relative_path ) {
1316              $relative_path = '';
1317          }
1318  
1319          $results = scandir( $path );
1320          $files   = array();
1321  
1322          /**
1323           * Filters the array of excluded directories and files while scanning theme folder.
1324           *
1325           * @since 4.7.4
1326           *
1327           * @param string[] $exclusions Array of excluded directories and files.
1328           */
1329          $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) );
1330  
1331          foreach ( $results as $result ) {
1332              if ( '.' == $result[0] || in_array( $result, $exclusions, true ) ) {
1333                  continue;
1334              }
1335              if ( is_dir( $path . '/' . $result ) ) {
1336                  if ( ! $depth ) {
1337                      continue;
1338                  }
1339                  $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1, $relative_path . $result );
1340                  $files = array_merge_recursive( $files, $found );
1341              } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
1342                  $files[ $relative_path . $result ] = $path . '/' . $result;
1343              }
1344          }
1345  
1346          return $files;
1347      }
1348  
1349      /**
1350       * Loads the theme's textdomain.
1351       *
1352       * Translation files are not inherited from the parent theme. TODO: If this fails for the
1353       * child theme, it should probably try to load the parent theme's translations.
1354       *
1355       * @since 3.4.0
1356       *
1357       * @return bool True if the textdomain was successfully loaded or has already been loaded.
1358       *  False if no textdomain was specified in the file headers, or if the domain could not be loaded.
1359       */
1360  	public function load_textdomain() {
1361          if ( isset( $this->textdomain_loaded ) ) {
1362              return $this->textdomain_loaded;
1363          }
1364  
1365          $textdomain = $this->get( 'TextDomain' );
1366          if ( ! $textdomain ) {
1367              $this->textdomain_loaded = false;
1368              return false;
1369          }
1370  
1371          if ( is_textdomain_loaded( $textdomain ) ) {
1372              $this->textdomain_loaded = true;
1373              return true;
1374          }
1375  
1376          $path       = $this->get_stylesheet_directory();
1377          $domainpath = $this->get( 'DomainPath' );
1378          if ( $domainpath ) {
1379              $path .= $domainpath;
1380          } else {
1381              $path .= '/languages';
1382          }
1383  
1384          $this->textdomain_loaded = load_theme_textdomain( $textdomain, $path );
1385          return $this->textdomain_loaded;
1386      }
1387  
1388      /**
1389       * Whether the theme is allowed (multisite only).
1390       *
1391       * @since 3.4.0
1392       *
1393       * @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site'
1394       *  settings, or 'both'. Defaults to 'both'.
1395       * @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current site.
1396       * @return bool Whether the theme is allowed for the network. Returns true in single-site.
1397       */
1398  	public function is_allowed( $check = 'both', $blog_id = null ) {
1399          if ( ! is_multisite() ) {
1400              return true;
1401          }
1402  
1403          if ( 'both' == $check || 'network' == $check ) {
1404              $allowed = self::get_allowed_on_network();
1405              if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) {
1406                  return true;
1407              }
1408          }
1409  
1410          if ( 'both' == $check || 'site' == $check ) {
1411              $allowed = self::get_allowed_on_site( $blog_id );
1412              if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) {
1413                  return true;
1414              }
1415          }
1416  
1417          return false;
1418      }
1419  
1420      /**
1421       * Determines the latest WordPress default theme that is installed.
1422       *
1423       * This hits the filesystem.
1424       *
1425       * @since 4.4.0
1426       *
1427       * @return WP_Theme|false Object, or false if no theme is installed, which would be bad.
1428       */
1429  	public static function get_core_default_theme() {
1430          foreach ( array_reverse( self::$default_themes ) as $slug => $name ) {
1431              $theme = wp_get_theme( $slug );
1432              if ( $theme->exists() ) {
1433                  return $theme;
1434              }
1435          }
1436          return false;
1437      }
1438  
1439      /**
1440       * Returns array of stylesheet names of themes allowed on the site or network.
1441       *
1442       * @since 3.4.0
1443       *
1444       * @param int $blog_id Optional. ID of the site. Defaults to the current site.
1445       * @return string[] Array of stylesheet names.
1446       */
1447  	public static function get_allowed( $blog_id = null ) {
1448          /**
1449           * Filters the array of themes allowed on the network.
1450           *
1451           * Site is provided as context so that a list of network allowed themes can
1452           * be filtered further.
1453           *
1454           * @since 4.5.0
1455           *
1456           * @param string[] $allowed_themes An array of theme stylesheet names.
1457           * @param int      $blog_id        ID of the site.
1458           */
1459          $network = (array) apply_filters( 'network_allowed_themes', self::get_allowed_on_network(), $blog_id );
1460          return $network + self::get_allowed_on_site( $blog_id );
1461      }
1462  
1463      /**
1464       * Returns array of stylesheet names of themes allowed on the network.
1465       *
1466       * @since 3.4.0
1467       *
1468       * @staticvar array $allowed_themes
1469       *
1470       * @return string[] Array of stylesheet names.
1471       */
1472  	public static function get_allowed_on_network() {
1473          static $allowed_themes;
1474          if ( ! isset( $allowed_themes ) ) {
1475              $allowed_themes = (array) get_site_option( 'allowedthemes' );
1476          }
1477  
1478          /**
1479           * Filters the array of themes allowed on the network.
1480           *
1481           * @since MU (3.0.0)
1482           *
1483           * @param string[] $allowed_themes An array of theme stylesheet names.
1484           */
1485          $allowed_themes = apply_filters( 'allowed_themes', $allowed_themes );
1486  
1487          return $allowed_themes;
1488      }
1489  
1490      /**
1491       * Returns array of stylesheet names of themes allowed on the site.
1492       *
1493       * @since 3.4.0
1494       *
1495       * @staticvar array $allowed_themes
1496       *
1497       * @param int $blog_id Optional. ID of the site. Defaults to the current site.
1498       * @return string[] Array of stylesheet names.
1499       */
1500  	public static function get_allowed_on_site( $blog_id = null ) {
1501          static $allowed_themes = array();
1502  
1503          if ( ! $blog_id || ! is_multisite() ) {
1504              $blog_id = get_current_blog_id();
1505          }
1506  
1507          if ( isset( $allowed_themes[ $blog_id ] ) ) {
1508              /**
1509               * Filters the array of themes allowed on the site.
1510               *
1511               * @since 4.5.0
1512               *
1513               * @param string[] $allowed_themes An array of theme stylesheet names.
1514               * @param int      $blog_id        ID of the site. Defaults to current site.
1515               */
1516              return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
1517          }
1518  
1519          $current = get_current_blog_id() == $blog_id;
1520  
1521          if ( $current ) {
1522              $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1523          } else {
1524              switch_to_blog( $blog_id );
1525              $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1526              restore_current_blog();
1527          }
1528  
1529          // This is all super old MU back compat joy.
1530          // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
1531          if ( false === $allowed_themes[ $blog_id ] ) {
1532              if ( $current ) {
1533                  $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1534              } else {
1535                  switch_to_blog( $blog_id );
1536                  $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1537                  restore_current_blog();
1538              }
1539  
1540              if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
1541                  $allowed_themes[ $blog_id ] = array();
1542              } else {
1543                  $converted = array();
1544                  $themes    = wp_get_themes();
1545                  foreach ( $themes as $stylesheet => $theme_data ) {
1546                      if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get( 'Name' ) ] ) ) {
1547                          $converted[ $stylesheet ] = true;
1548                      }
1549                  }
1550                  $allowed_themes[ $blog_id ] = $converted;
1551              }
1552              // Set the option so we never have to go through this pain again.
1553              if ( is_admin() && $allowed_themes[ $blog_id ] ) {
1554                  if ( $current ) {
1555                      update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1556                      delete_option( 'allowed_themes' );
1557                  } else {
1558                      switch_to_blog( $blog_id );
1559                      update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1560                      delete_option( 'allowed_themes' );
1561                      restore_current_blog();
1562                  }
1563              }
1564          }
1565  
1566          /** This filter is documented in wp-includes/class-wp-theme.php */
1567          return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
1568      }
1569  
1570      /**
1571       * Enables a theme for all sites on the current network.
1572       *
1573       * @since 4.6.0
1574       *
1575       * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
1576       */
1577  	public static function network_enable_theme( $stylesheets ) {
1578          if ( ! is_multisite() ) {
1579              return;
1580          }
1581  
1582          if ( ! is_array( $stylesheets ) ) {
1583              $stylesheets = array( $stylesheets );
1584          }
1585  
1586          $allowed_themes = get_site_option( 'allowedthemes' );
1587          foreach ( $stylesheets as $stylesheet ) {
1588              $allowed_themes[ $stylesheet ] = true;
1589          }
1590  
1591          update_site_option( 'allowedthemes', $allowed_themes );
1592      }
1593  
1594      /**
1595       * Disables a theme for all sites on the current network.
1596       *
1597       * @since 4.6.0
1598       *
1599       * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
1600       */
1601  	public static function network_disable_theme( $stylesheets ) {
1602          if ( ! is_multisite() ) {
1603              return;
1604          }
1605  
1606          if ( ! is_array( $stylesheets ) ) {
1607              $stylesheets = array( $stylesheets );
1608          }
1609  
1610          $allowed_themes = get_site_option( 'allowedthemes' );
1611          foreach ( $stylesheets as $stylesheet ) {
1612              if ( isset( $allowed_themes[ $stylesheet ] ) ) {
1613                  unset( $allowed_themes[ $stylesheet ] );
1614              }
1615          }
1616  
1617          update_site_option( 'allowedthemes', $allowed_themes );
1618      }
1619  
1620      /**
1621       * Sorts themes by name.
1622       *
1623       * @since 3.4.0
1624       *
1625       * @param WP_Theme[] $themes Array of theme objects to sort (passed by reference).
1626       */
1627  	public static function sort_by_name( &$themes ) {
1628          if ( 0 === strpos( get_user_locale(), 'en_' ) ) {
1629              uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
1630          } else {
1631              foreach ( $themes as $key => $theme ) {
1632                  $theme->translate_header( 'Name', $theme->headers['Name'] );
1633              }
1634              uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) );
1635          }
1636      }
1637  
1638      /**
1639       * Callback function for usort() to naturally sort themes by name.
1640       *
1641       * Accesses the Name header directly from the class for maximum speed.
1642       * Would choke on HTML but we don't care enough to slow it down with strip_tags().
1643       *
1644       * @since 3.4.0
1645       *
1646       * @param string $a First name.
1647       * @param string $b Second name.
1648       * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally.
1649       *             Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort().
1650       */
1651  	private static function _name_sort( $a, $b ) {
1652          return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] );
1653      }
1654  
1655      /**
1656       * Callback function for usort() to naturally sort themes by translated name.
1657       *
1658       * @since 3.4.0
1659       *
1660       * @param string $a First name.
1661       * @param string $b Second name.
1662       * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally.
1663       *             Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort().
1664       */
1665  	private static function _name_sort_i18n( $a, $b ) {
1666          return strnatcasecmp( $a->name_translated, $b->name_translated );
1667      }
1668  }


Generated: Mon Feb 17 01:00:03 2020 Cross-referenced by PHPXref 0.7.1