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


Generated: Thu Jul 29 01:00:05 2021 Cross-referenced by PHPXref 0.7.1