[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Sat Jun 6 08:20:01 2020 Cross-referenced by PHPXref