[ 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      #[ReturnTypeWillChange]
 560  	public function offsetSet( $offset, $value ) {}
 561  
 562      /**
 563       * Method to implement ArrayAccess for keys formerly returned by get_themes()
 564       *
 565       * @since 3.4.0
 566       *
 567       * @param mixed $offset
 568       */
 569      #[ReturnTypeWillChange]
 570  	public function offsetUnset( $offset ) {}
 571  
 572      /**
 573       * Method to implement ArrayAccess for keys formerly returned by get_themes()
 574       *
 575       * @since 3.4.0
 576       *
 577       * @param mixed $offset
 578       * @return bool
 579       */
 580      #[ReturnTypeWillChange]
 581  	public function offsetExists( $offset ) {
 582          static $keys = array(
 583              'Name',
 584              'Version',
 585              'Status',
 586              'Title',
 587              'Author',
 588              'Author Name',
 589              'Author URI',
 590              'Description',
 591              'Template',
 592              'Stylesheet',
 593              'Template Files',
 594              'Stylesheet Files',
 595              'Template Dir',
 596              'Stylesheet Dir',
 597              'Screenshot',
 598              'Tags',
 599              'Theme Root',
 600              'Theme Root URI',
 601              'Parent Theme',
 602          );
 603  
 604          return in_array( $offset, $keys, true );
 605      }
 606  
 607      /**
 608       * Method to implement ArrayAccess for keys formerly returned by get_themes().
 609       *
 610       * Author, Author Name, Author URI, and Description did not previously return
 611       * translated data. We are doing so now as it is safe to do. However, as
 612       * Name and Title could have been used as the key for get_themes(), both remain
 613       * untranslated for back compatibility. This means that ['Name'] is not ideal,
 614       * and care should be taken to use `$theme::display( 'Name' )` to get a properly
 615       * translated header.
 616       *
 617       * @since 3.4.0
 618       *
 619       * @param mixed $offset
 620       * @return mixed
 621       */
 622      #[ReturnTypeWillChange]
 623  	public function offsetGet( $offset ) {
 624          switch ( $offset ) {
 625              case 'Name':
 626              case 'Title':
 627                  /*
 628                   * See note above about using translated data. get() is not ideal.
 629                   * It is only for backward compatibility. Use display().
 630                   */
 631                  return $this->get( 'Name' );
 632              case 'Author':
 633                  return $this->display( 'Author' );
 634              case 'Author Name':
 635                  return $this->display( 'Author', false );
 636              case 'Author URI':
 637                  return $this->display( 'AuthorURI' );
 638              case 'Description':
 639                  return $this->display( 'Description' );
 640              case 'Version':
 641              case 'Status':
 642                  return $this->get( $offset );
 643              case 'Template':
 644                  return $this->get_template();
 645              case 'Stylesheet':
 646                  return $this->get_stylesheet();
 647              case 'Template Files':
 648                  return $this->get_files( 'php', 1, true );
 649              case 'Stylesheet Files':
 650                  return $this->get_files( 'css', 0, false );
 651              case 'Template Dir':
 652                  return $this->get_template_directory();
 653              case 'Stylesheet Dir':
 654                  return $this->get_stylesheet_directory();
 655              case 'Screenshot':
 656                  return $this->get_screenshot( 'relative' );
 657              case 'Tags':
 658                  return $this->get( 'Tags' );
 659              case 'Theme Root':
 660                  return $this->get_theme_root();
 661              case 'Theme Root URI':
 662                  return $this->get_theme_root_uri();
 663              case 'Parent Theme':
 664                  return $this->parent() ? $this->parent()->get( 'Name' ) : '';
 665              default:
 666                  return null;
 667          }
 668      }
 669  
 670      /**
 671       * Returns errors property.
 672       *
 673       * @since 3.4.0
 674       *
 675       * @return WP_Error|false WP_Error if there are errors, or false.
 676       */
 677  	public function errors() {
 678          return is_wp_error( $this->errors ) ? $this->errors : false;
 679      }
 680  
 681      /**
 682       * Whether the theme exists.
 683       *
 684       * A theme with errors exists. A theme with the error of 'theme_not_found',
 685       * meaning that the theme's directory was not found, does not exist.
 686       *
 687       * @since 3.4.0
 688       *
 689       * @return bool Whether the theme exists.
 690       */
 691  	public function exists() {
 692          return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes(), true ) );
 693      }
 694  
 695      /**
 696       * Returns reference to the parent theme.
 697       *
 698       * @since 3.4.0
 699       *
 700       * @return WP_Theme|false Parent theme, or false if the current theme is not a child theme.
 701       */
 702  	public function parent() {
 703          return isset( $this->parent ) ? $this->parent : false;
 704      }
 705  
 706      /**
 707       * Adds theme data to cache.
 708       *
 709       * Cache entries keyed by the theme and the type of data.
 710       *
 711       * @since 3.4.0
 712       *
 713       * @param string       $key  Type of data to store (theme, screenshot, headers, post_templates)
 714       * @param array|string $data Data to store
 715       * @return bool Return value from wp_cache_add()
 716       */
 717  	private function cache_add( $key, $data ) {
 718          return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration );
 719      }
 720  
 721      /**
 722       * Gets theme data from cache.
 723       *
 724       * Cache entries are keyed by the theme and the type of data.
 725       *
 726       * @since 3.4.0
 727       *
 728       * @param string $key Type of data to retrieve (theme, screenshot, headers, post_templates)
 729       * @return mixed Retrieved data
 730       */
 731  	private function cache_get( $key ) {
 732          return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' );
 733      }
 734  
 735      /**
 736       * Clears the cache for the theme.
 737       *
 738       * @since 3.4.0
 739       */
 740  	public function cache_delete() {
 741          foreach ( array( 'theme', 'screenshot', 'headers', 'post_templates' ) as $key ) {
 742              wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' );
 743          }
 744          $this->template          = null;
 745          $this->textdomain_loaded = null;
 746          $this->theme_root_uri    = null;
 747          $this->parent            = null;
 748          $this->errors            = null;
 749          $this->headers_sanitized = null;
 750          $this->name_translated   = null;
 751          $this->headers           = array();
 752          $this->__construct( $this->stylesheet, $this->theme_root );
 753      }
 754  
 755      /**
 756       * Get a raw, unformatted theme header.
 757       *
 758       * The header is sanitized, but is not translated, and is not marked up for display.
 759       * To get a theme header for display, use the display() method.
 760       *
 761       * Use the get_template() method, not the 'Template' header, for finding the template.
 762       * The 'Template' header is only good for what was written in the style.css, while
 763       * get_template() takes into account where WordPress actually located the theme and
 764       * whether it is actually valid.
 765       *
 766       * @since 3.4.0
 767       *
 768       * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 769       * @return string|array|false String or array (for Tags header) on success, false on failure.
 770       */
 771  	public function get( $header ) {
 772          if ( ! isset( $this->headers[ $header ] ) ) {
 773              return false;
 774          }
 775  
 776          if ( ! isset( $this->headers_sanitized ) ) {
 777              $this->headers_sanitized = $this->cache_get( 'headers' );
 778              if ( ! is_array( $this->headers_sanitized ) ) {
 779                  $this->headers_sanitized = array();
 780              }
 781          }
 782  
 783          if ( isset( $this->headers_sanitized[ $header ] ) ) {
 784              return $this->headers_sanitized[ $header ];
 785          }
 786  
 787          // If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets.
 788          if ( self::$persistently_cache ) {
 789              foreach ( array_keys( $this->headers ) as $_header ) {
 790                  $this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] );
 791              }
 792              $this->cache_add( 'headers', $this->headers_sanitized );
 793          } else {
 794              $this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] );
 795          }
 796  
 797          return $this->headers_sanitized[ $header ];
 798      }
 799  
 800      /**
 801       * Gets a theme header, formatted and translated for display.
 802       *
 803       * @since 3.4.0
 804       *
 805       * @param string $header    Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 806       * @param bool   $markup    Optional. Whether to mark up the header. Defaults to true.
 807       * @param bool   $translate Optional. Whether to translate the header. Defaults to true.
 808       * @return string|array|false Processed header. An array for Tags if `$markup` is false, string otherwise.
 809       *                            False on failure.
 810       */
 811  	public function display( $header, $markup = true, $translate = true ) {
 812          $value = $this->get( $header );
 813          if ( false === $value ) {
 814              return false;
 815          }
 816  
 817          if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) ) {
 818              $translate = false;
 819          }
 820  
 821          if ( $translate ) {
 822              $value = $this->translate_header( $header, $value );
 823          }
 824  
 825          if ( $markup ) {
 826              $value = $this->markup_header( $header, $value, $translate );
 827          }
 828  
 829          return $value;
 830      }
 831  
 832      /**
 833       * Sanitize a theme header.
 834       *
 835       * @since 3.4.0
 836       * @since 5.4.0 Added support for `Requires at least` and `Requires PHP` headers.
 837       *
 838       * @param string $header Theme header. Accepts 'Name', 'Description', 'Author', 'Version',
 839       *                       'ThemeURI', 'AuthorURI', 'Status', 'Tags', 'RequiresWP', 'RequiresPHP'.
 840       * @param string $value  Value to sanitize.
 841       * @return string|array An array for Tags header, string otherwise.
 842       */
 843  	private function sanitize_header( $header, $value ) {
 844          switch ( $header ) {
 845              case 'Status':
 846                  if ( ! $value ) {
 847                      $value = 'publish';
 848                      break;
 849                  }
 850                  // Fall through otherwise.
 851              case 'Name':
 852                  static $header_tags = array(
 853                      'abbr'    => array( 'title' => true ),
 854                      'acronym' => array( 'title' => true ),
 855                      'code'    => true,
 856                      'em'      => true,
 857                      'strong'  => true,
 858                  );
 859  
 860                  $value = wp_kses( $value, $header_tags );
 861                  break;
 862              case 'Author':
 863                  // There shouldn't be anchor tags in Author, but some themes like to be challenging.
 864              case 'Description':
 865                  static $header_tags_with_a = array(
 866                      'a'       => array(
 867                          'href'  => true,
 868                          'title' => true,
 869                      ),
 870                      'abbr'    => array( 'title' => true ),
 871                      'acronym' => array( 'title' => true ),
 872                      'code'    => true,
 873                      'em'      => true,
 874                      'strong'  => true,
 875                  );
 876  
 877                  $value = wp_kses( $value, $header_tags_with_a );
 878                  break;
 879              case 'ThemeURI':
 880              case 'AuthorURI':
 881                  $value = esc_url_raw( $value );
 882                  break;
 883              case 'Tags':
 884                  $value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
 885                  break;
 886              case 'Version':
 887              case 'RequiresWP':
 888              case 'RequiresPHP':
 889                  $value = strip_tags( $value );
 890                  break;
 891          }
 892  
 893          return $value;
 894      }
 895  
 896      /**
 897       * Mark up a theme header.
 898       *
 899       * @since 3.4.0
 900       *
 901       * @param string       $header    Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 902       * @param string|array $value     Value to mark up. An array for Tags header, string otherwise.
 903       * @param string       $translate Whether the header has been translated.
 904       * @return string Value, marked up.
 905       */
 906  	private function markup_header( $header, $value, $translate ) {
 907          switch ( $header ) {
 908              case 'Name':
 909                  if ( empty( $value ) ) {
 910                      $value = esc_html( $this->get_stylesheet() );
 911                  }
 912                  break;
 913              case 'Description':
 914                  $value = wptexturize( $value );
 915                  break;
 916              case 'Author':
 917                  if ( $this->get( 'AuthorURI' ) ) {
 918                      $value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value );
 919                  } elseif ( ! $value ) {
 920                      $value = __( 'Anonymous' );
 921                  }
 922                  break;
 923              case 'Tags':
 924                  static $comma = null;
 925                  if ( ! isset( $comma ) ) {
 926                      /* translators: Used between list items, there is a space after the comma. */
 927                      $comma = __( ', ' );
 928                  }
 929                  $value = implode( $comma, $value );
 930                  break;
 931              case 'ThemeURI':
 932              case 'AuthorURI':
 933                  $value = esc_url( $value );
 934                  break;
 935          }
 936  
 937          return $value;
 938      }
 939  
 940      /**
 941       * Translate a theme header.
 942       *
 943       * @since 3.4.0
 944       *
 945       * @param string       $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
 946       * @param string|array $value  Value to translate. An array for Tags header, string otherwise.
 947       * @return string|array Translated value. An array for Tags header, string otherwise.
 948       */
 949  	private function translate_header( $header, $value ) {
 950          switch ( $header ) {
 951              case 'Name':
 952                  // Cached for sorting reasons.
 953                  if ( isset( $this->name_translated ) ) {
 954                      return $this->name_translated;
 955                  }
 956  
 957                  // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
 958                  $this->name_translated = translate( $value, $this->get( 'TextDomain' ) );
 959  
 960                  return $this->name_translated;
 961              case 'Tags':
 962                  if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) ) {
 963                      return $value;
 964                  }
 965  
 966                  static $tags_list;
 967                  if ( ! isset( $tags_list ) ) {
 968                      $tags_list = array(
 969                          // As of 4.6, deprecated tags which are only used to provide translation for older themes.
 970                          'black'             => __( 'Black' ),
 971                          'blue'              => __( 'Blue' ),
 972                          'brown'             => __( 'Brown' ),
 973                          'gray'              => __( 'Gray' ),
 974                          'green'             => __( 'Green' ),
 975                          'orange'            => __( 'Orange' ),
 976                          'pink'              => __( 'Pink' ),
 977                          'purple'            => __( 'Purple' ),
 978                          'red'               => __( 'Red' ),
 979                          'silver'            => __( 'Silver' ),
 980                          'tan'               => __( 'Tan' ),
 981                          'white'             => __( 'White' ),
 982                          'yellow'            => __( 'Yellow' ),
 983                          'dark'              => __( 'Dark' ),
 984                          'light'             => __( 'Light' ),
 985                          'fixed-layout'      => __( 'Fixed Layout' ),
 986                          'fluid-layout'      => __( 'Fluid Layout' ),
 987                          'responsive-layout' => __( 'Responsive Layout' ),
 988                          'blavatar'          => __( 'Blavatar' ),
 989                          'photoblogging'     => __( 'Photoblogging' ),
 990                          'seasonal'          => __( 'Seasonal' ),
 991                      );
 992  
 993                      $feature_list = get_theme_feature_list( false ); // No API.
 994  
 995                      foreach ( $feature_list as $tags ) {
 996                          $tags_list += $tags;
 997                      }
 998                  }
 999  
1000                  foreach ( $value as &$tag ) {
1001                      if ( isset( $tags_list[ $tag ] ) ) {
1002                          $tag = $tags_list[ $tag ];
1003                      } elseif ( isset( self::$tag_map[ $tag ] ) ) {
1004                          $tag = $tags_list[ self::$tag_map[ $tag ] ];
1005                      }
1006                  }
1007  
1008                  return $value;
1009  
1010              default:
1011                  // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
1012                  $value = translate( $value, $this->get( 'TextDomain' ) );
1013          }
1014          return $value;
1015      }
1016  
1017      /**
1018       * The directory name of the theme's "stylesheet" files, inside the theme root.
1019       *
1020       * In the case of a child theme, this is directory name of the child theme.
1021       * Otherwise, get_stylesheet() is the same as get_template().
1022       *
1023       * @since 3.4.0
1024       *
1025       * @return string Stylesheet
1026       */
1027  	public function get_stylesheet() {
1028          return $this->stylesheet;
1029      }
1030  
1031      /**
1032       * The directory name of the theme's "template" files, inside the theme root.
1033       *
1034       * In the case of a child theme, this is the directory name of the parent theme.
1035       * Otherwise, the get_template() is the same as get_stylesheet().
1036       *
1037       * @since 3.4.0
1038       *
1039       * @return string Template
1040       */
1041  	public function get_template() {
1042          return $this->template;
1043      }
1044  
1045      /**
1046       * Returns the absolute path to the directory of a theme's "stylesheet" files.
1047       *
1048       * In the case of a child theme, this is the absolute path to the directory
1049       * of the child theme's files.
1050       *
1051       * @since 3.4.0
1052       *
1053       * @return string Absolute path of the stylesheet directory.
1054       */
1055  	public function get_stylesheet_directory() {
1056          if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes(), true ) ) {
1057              return '';
1058          }
1059  
1060          return $this->theme_root . '/' . $this->stylesheet;
1061      }
1062  
1063      /**
1064       * Returns the absolute path to the directory of a theme's "template" files.
1065       *
1066       * In the case of a child theme, this is the absolute path to the directory
1067       * of the parent theme's files.
1068       *
1069       * @since 3.4.0
1070       *
1071       * @return string Absolute path of the template directory.
1072       */
1073  	public function get_template_directory() {
1074          if ( $this->parent() ) {
1075              $theme_root = $this->parent()->theme_root;
1076          } else {
1077              $theme_root = $this->theme_root;
1078          }
1079  
1080          return $theme_root . '/' . $this->template;
1081      }
1082  
1083      /**
1084       * Returns the URL to the directory of a theme's "stylesheet" files.
1085       *
1086       * In the case of a child theme, this is the URL to the directory of the
1087       * child theme's files.
1088       *
1089       * @since 3.4.0
1090       *
1091       * @return string URL to the stylesheet directory.
1092       */
1093  	public function get_stylesheet_directory_uri() {
1094          return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) );
1095      }
1096  
1097      /**
1098       * Returns the URL to the directory of a theme's "template" files.
1099       *
1100       * In the case of a child theme, this is the URL to the directory of the
1101       * parent theme's files.
1102       *
1103       * @since 3.4.0
1104       *
1105       * @return string URL to the template directory.
1106       */
1107  	public function get_template_directory_uri() {
1108          if ( $this->parent() ) {
1109              $theme_root_uri = $this->parent()->get_theme_root_uri();
1110          } else {
1111              $theme_root_uri = $this->get_theme_root_uri();
1112          }
1113  
1114          return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) );
1115      }
1116  
1117      /**
1118       * The absolute path to the directory of the theme root.
1119       *
1120       * This is typically the absolute path to wp-content/themes.
1121       *
1122       * @since 3.4.0
1123       *
1124       * @return string Theme root.
1125       */
1126  	public function get_theme_root() {
1127          return $this->theme_root;
1128      }
1129  
1130      /**
1131       * Returns the URL to the directory of the theme root.
1132       *
1133       * This is typically the absolute URL to wp-content/themes. This forms the basis
1134       * for all other URLs returned by WP_Theme, so we pass it to the public function
1135       * get_theme_root_uri() and allow it to run the {@see 'theme_root_uri'} filter.
1136       *
1137       * @since 3.4.0
1138       *
1139       * @return string Theme root URI.
1140       */
1141  	public function get_theme_root_uri() {
1142          if ( ! isset( $this->theme_root_uri ) ) {
1143              $this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root );
1144          }
1145          return $this->theme_root_uri;
1146      }
1147  
1148      /**
1149       * Returns the main screenshot file for the theme.
1150       *
1151       * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed.
1152       *
1153       * Screenshots for a theme must be in the stylesheet directory. (In the case of child
1154       * themes, parent theme screenshots are not inherited.)
1155       *
1156       * @since 3.4.0
1157       *
1158       * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI.
1159       * @return string|false Screenshot file. False if the theme does not have a screenshot.
1160       */
1161  	public function get_screenshot( $uri = 'uri' ) {
1162          $screenshot = $this->cache_get( 'screenshot' );
1163          if ( $screenshot ) {
1164              if ( 'relative' === $uri ) {
1165                  return $screenshot;
1166              }
1167              return $this->get_stylesheet_directory_uri() . '/' . $screenshot;
1168          } elseif ( 0 === $screenshot ) {
1169              return false;
1170          }
1171  
1172          foreach ( array( 'png', 'gif', 'jpg', 'jpeg', 'webp' ) as $ext ) {
1173              if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
1174                  $this->cache_add( 'screenshot', 'screenshot.' . $ext );
1175                  if ( 'relative' === $uri ) {
1176                      return 'screenshot.' . $ext;
1177                  }
1178                  return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext;
1179              }
1180          }
1181  
1182          $this->cache_add( 'screenshot', 0 );
1183          return false;
1184      }
1185  
1186      /**
1187       * Return files in the theme's directory.
1188       *
1189       * @since 3.4.0
1190       *
1191       * @param string[]|string $type          Optional. Array of extensions to find, string of a single extension,
1192       *                                       or null for all extensions. Default null.
1193       * @param int             $depth         Optional. How deep to search for files. Defaults to a flat scan (0 depth).
1194       *                                       -1 depth is infinite.
1195       * @param bool            $search_parent Optional. Whether to return parent files. Default false.
1196       * @return string[] Array of files, keyed by the path to the file relative to the theme's directory, with the values
1197       *                  being absolute paths.
1198       */
1199  	public function get_files( $type = null, $depth = 0, $search_parent = false ) {
1200          $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
1201  
1202          if ( $search_parent && $this->parent() ) {
1203              $files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
1204          }
1205  
1206          return $files;
1207      }
1208  
1209      /**
1210       * Returns the theme's post templates.
1211       *
1212       * @since 4.7.0
1213       * @since 5.8.0 Include block templates.
1214       *
1215       * @return string[] Array of page templates, keyed by filename and post type,
1216       *                  with the value of the translated header name.
1217       */
1218  	public function get_post_templates() {
1219          // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
1220          if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {
1221              return array();
1222          }
1223  
1224          $post_templates = $this->cache_get( 'post_templates' );
1225  
1226          if ( ! is_array( $post_templates ) ) {
1227              $post_templates = array();
1228  
1229              $files = (array) $this->get_files( 'php', 1, true );
1230  
1231              foreach ( $files as $file => $full_path ) {
1232                  if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {
1233                      continue;
1234                  }
1235  
1236                  $types = array( 'page' );
1237                  if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {
1238                      $types = explode( ',', _cleanup_header_comment( $type[1] ) );
1239                  }
1240  
1241                  foreach ( $types as $type ) {
1242                      $type = sanitize_key( $type );
1243                      if ( ! isset( $post_templates[ $type ] ) ) {
1244                          $post_templates[ $type ] = array();
1245                      }
1246  
1247                      $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
1248                  }
1249              }
1250  
1251              if ( current_theme_supports( 'block-templates' ) ) {
1252                  $block_templates = get_block_templates( array(), 'wp_template' );
1253                  foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
1254                      foreach ( $block_templates as $block_template ) {
1255                          $post_templates[ $type ][ $block_template->slug ] = $block_template->title;
1256                      }
1257                  }
1258              }
1259  
1260              $this->cache_add( 'post_templates', $post_templates );
1261          }
1262  
1263          if ( $this->load_textdomain() ) {
1264              foreach ( $post_templates as &$post_type ) {
1265                  foreach ( $post_type as &$post_template ) {
1266                      $post_template = $this->translate_header( 'Template Name', $post_template );
1267                  }
1268              }
1269          }
1270  
1271          return $post_templates;
1272      }
1273  
1274      /**
1275       * Returns the theme's post templates for a given post type.
1276       *
1277       * @since 3.4.0
1278       * @since 4.7.0 Added the `$post_type` parameter.
1279       *
1280       * @param WP_Post|null $post      Optional. The post being edited, provided for context.
1281       * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
1282       *                                If a post is provided, its post type is used.
1283       * @return string[] Array of template header names keyed by the template file name.
1284       */
1285  	public function get_page_templates( $post = null, $post_type = 'page' ) {
1286          if ( $post ) {
1287              $post_type = get_post_type( $post );
1288          }
1289  
1290          $post_templates = $this->get_post_templates();
1291          $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array();
1292  
1293          /**
1294           * Filters list of page templates for a theme.
1295           *
1296           * @since 4.9.6
1297           *
1298           * @param string[]     $post_templates Array of template header names keyed by the template file name.
1299           * @param WP_Theme     $theme          The theme object.
1300           * @param WP_Post|null $post           The post being edited, provided for context, or null.
1301           * @param string       $post_type      Post type to get the templates for.
1302           */
1303          $post_templates = (array) apply_filters( 'theme_templates', $post_templates, $this, $post, $post_type );
1304  
1305          /**
1306           * Filters list of page templates for a theme.
1307           *
1308           * The dynamic portion of the hook name, `$post_type`, refers to the post type.
1309           *
1310           * Possible hook names include:
1311           *
1312           *  - `theme_post_templates`
1313           *  - `theme_page_templates`
1314           *  - `theme_attachment_templates`
1315           *
1316           * @since 3.9.0
1317           * @since 4.4.0 Converted to allow complete control over the `$page_templates` array.
1318           * @since 4.7.0 Added the `$post_type` parameter.
1319           *
1320           * @param string[]     $post_templates Array of template header names keyed by the template file name.
1321           * @param WP_Theme     $theme          The theme object.
1322           * @param WP_Post|null $post           The post being edited, provided for context, or null.
1323           * @param string       $post_type      Post type to get the templates for.
1324           */
1325          $post_templates = (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
1326  
1327          return $post_templates;
1328      }
1329  
1330      /**
1331       * Scans a directory for files of a certain extension.
1332       *
1333       * @since 3.4.0
1334       *
1335       * @param string            $path          Absolute path to search.
1336       * @param array|string|null $extensions    Optional. Array of extensions to find, string of a single extension,
1337       *                                         or null for all extensions. Default null.
1338       * @param int               $depth         Optional. How many levels deep to search for files. Accepts 0, 1+, or
1339       *                                         -1 (infinite depth). Default 0.
1340       * @param string            $relative_path Optional. The basename of the absolute path. Used to control the
1341       *                                         returned path for the found files, particularly when this function
1342       *                                         recurses to lower depths. Default empty.
1343       * @return string[]|false Array of files, keyed by the path to the file relative to the `$path` directory prepended
1344       *                        with `$relative_path`, with the values being absolute paths. False otherwise.
1345       */
1346  	private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
1347          if ( ! is_dir( $path ) ) {
1348              return false;
1349          }
1350  
1351          if ( $extensions ) {
1352              $extensions  = (array) $extensions;
1353              $_extensions = implode( '|', $extensions );
1354          }
1355  
1356          $relative_path = trailingslashit( $relative_path );
1357          if ( '/' === $relative_path ) {
1358              $relative_path = '';
1359          }
1360  
1361          $results = scandir( $path );
1362          $files   = array();
1363  
1364          /**
1365           * Filters the array of excluded directories and files while scanning theme folder.
1366           *
1367           * @since 4.7.4
1368           *
1369           * @param string[] $exclusions Array of excluded directories and files.
1370           */
1371          $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) );
1372  
1373          foreach ( $results as $result ) {
1374              if ( '.' === $result[0] || in_array( $result, $exclusions, true ) ) {
1375                  continue;
1376              }
1377              if ( is_dir( $path . '/' . $result ) ) {
1378                  if ( ! $depth ) {
1379                      continue;
1380                  }
1381                  $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1, $relative_path . $result );
1382                  $files = array_merge_recursive( $files, $found );
1383              } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
1384                  $files[ $relative_path . $result ] = $path . '/' . $result;
1385              }
1386          }
1387  
1388          return $files;
1389      }
1390  
1391      /**
1392       * Loads the theme's textdomain.
1393       *
1394       * Translation files are not inherited from the parent theme. TODO: If this fails for the
1395       * child theme, it should probably try to load the parent theme's translations.
1396       *
1397       * @since 3.4.0
1398       *
1399       * @return bool True if the textdomain was successfully loaded or has already been loaded.
1400       *  False if no textdomain was specified in the file headers, or if the domain could not be loaded.
1401       */
1402  	public function load_textdomain() {
1403          if ( isset( $this->textdomain_loaded ) ) {
1404              return $this->textdomain_loaded;
1405          }
1406  
1407          $textdomain = $this->get( 'TextDomain' );
1408          if ( ! $textdomain ) {
1409              $this->textdomain_loaded = false;
1410              return false;
1411          }
1412  
1413          if ( is_textdomain_loaded( $textdomain ) ) {
1414              $this->textdomain_loaded = true;
1415              return true;
1416          }
1417  
1418          $path       = $this->get_stylesheet_directory();
1419          $domainpath = $this->get( 'DomainPath' );
1420          if ( $domainpath ) {
1421              $path .= $domainpath;
1422          } else {
1423              $path .= '/languages';
1424          }
1425  
1426          $this->textdomain_loaded = load_theme_textdomain( $textdomain, $path );
1427          return $this->textdomain_loaded;
1428      }
1429  
1430      /**
1431       * Whether the theme is allowed (multisite only).
1432       *
1433       * @since 3.4.0
1434       *
1435       * @param string $check   Optional. Whether to check only the 'network'-wide settings, the 'site'
1436       *                        settings, or 'both'. Defaults to 'both'.
1437       * @param int    $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current site.
1438       * @return bool Whether the theme is allowed for the network. Returns true in single-site.
1439       */
1440  	public function is_allowed( $check = 'both', $blog_id = null ) {
1441          if ( ! is_multisite() ) {
1442              return true;
1443          }
1444  
1445          if ( 'both' === $check || 'network' === $check ) {
1446              $allowed = self::get_allowed_on_network();
1447              if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) {
1448                  return true;
1449              }
1450          }
1451  
1452          if ( 'both' === $check || 'site' === $check ) {
1453              $allowed = self::get_allowed_on_site( $blog_id );
1454              if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) {
1455                  return true;
1456              }
1457          }
1458  
1459          return false;
1460      }
1461  
1462      /**
1463       * Determines the latest WordPress default theme that is installed.
1464       *
1465       * This hits the filesystem.
1466       *
1467       * @since 4.4.0
1468       *
1469       * @return WP_Theme|false Object, or false if no theme is installed, which would be bad.
1470       */
1471  	public static function get_core_default_theme() {
1472          foreach ( array_reverse( self::$default_themes ) as $slug => $name ) {
1473              $theme = wp_get_theme( $slug );
1474              if ( $theme->exists() ) {
1475                  return $theme;
1476              }
1477          }
1478          return false;
1479      }
1480  
1481      /**
1482       * Returns array of stylesheet names of themes allowed on the site or network.
1483       *
1484       * @since 3.4.0
1485       *
1486       * @param int $blog_id Optional. ID of the site. Defaults to the current site.
1487       * @return string[] Array of stylesheet names.
1488       */
1489  	public static function get_allowed( $blog_id = null ) {
1490          /**
1491           * Filters the array of themes allowed on the network.
1492           *
1493           * Site is provided as context so that a list of network allowed themes can
1494           * be filtered further.
1495           *
1496           * @since 4.5.0
1497           *
1498           * @param string[] $allowed_themes An array of theme stylesheet names.
1499           * @param int      $blog_id        ID of the site.
1500           */
1501          $network = (array) apply_filters( 'network_allowed_themes', self::get_allowed_on_network(), $blog_id );
1502          return $network + self::get_allowed_on_site( $blog_id );
1503      }
1504  
1505      /**
1506       * Returns array of stylesheet names of themes allowed on the network.
1507       *
1508       * @since 3.4.0
1509       *
1510       * @return string[] Array of stylesheet names.
1511       */
1512  	public static function get_allowed_on_network() {
1513          static $allowed_themes;
1514          if ( ! isset( $allowed_themes ) ) {
1515              $allowed_themes = (array) get_site_option( 'allowedthemes' );
1516          }
1517  
1518          /**
1519           * Filters the array of themes allowed on the network.
1520           *
1521           * @since MU (3.0.0)
1522           *
1523           * @param string[] $allowed_themes An array of theme stylesheet names.
1524           */
1525          $allowed_themes = apply_filters( 'allowed_themes', $allowed_themes );
1526  
1527          return $allowed_themes;
1528      }
1529  
1530      /**
1531       * Returns array of stylesheet names of themes allowed on the site.
1532       *
1533       * @since 3.4.0
1534       *
1535       * @param int $blog_id Optional. ID of the site. Defaults to the current site.
1536       * @return string[] Array of stylesheet names.
1537       */
1538  	public static function get_allowed_on_site( $blog_id = null ) {
1539          static $allowed_themes = array();
1540  
1541          if ( ! $blog_id || ! is_multisite() ) {
1542              $blog_id = get_current_blog_id();
1543          }
1544  
1545          if ( isset( $allowed_themes[ $blog_id ] ) ) {
1546              /**
1547               * Filters the array of themes allowed on the site.
1548               *
1549               * @since 4.5.0
1550               *
1551               * @param string[] $allowed_themes An array of theme stylesheet names.
1552               * @param int      $blog_id        ID of the site. Defaults to current site.
1553               */
1554              return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
1555          }
1556  
1557          $current = get_current_blog_id() == $blog_id;
1558  
1559          if ( $current ) {
1560              $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1561          } else {
1562              switch_to_blog( $blog_id );
1563              $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
1564              restore_current_blog();
1565          }
1566  
1567          // This is all super old MU back compat joy.
1568          // 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
1569          if ( false === $allowed_themes[ $blog_id ] ) {
1570              if ( $current ) {
1571                  $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1572              } else {
1573                  switch_to_blog( $blog_id );
1574                  $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
1575                  restore_current_blog();
1576              }
1577  
1578              if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
1579                  $allowed_themes[ $blog_id ] = array();
1580              } else {
1581                  $converted = array();
1582                  $themes    = wp_get_themes();
1583                  foreach ( $themes as $stylesheet => $theme_data ) {
1584                      if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get( 'Name' ) ] ) ) {
1585                          $converted[ $stylesheet ] = true;
1586                      }
1587                  }
1588                  $allowed_themes[ $blog_id ] = $converted;
1589              }
1590              // Set the option so we never have to go through this pain again.
1591              if ( is_admin() && $allowed_themes[ $blog_id ] ) {
1592                  if ( $current ) {
1593                      update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1594                      delete_option( 'allowed_themes' );
1595                  } else {
1596                      switch_to_blog( $blog_id );
1597                      update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
1598                      delete_option( 'allowed_themes' );
1599                      restore_current_blog();
1600                  }
1601              }
1602          }
1603  
1604          /** This filter is documented in wp-includes/class-wp-theme.php */
1605          return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
1606      }
1607  
1608      /**
1609       * Enables a theme for all sites on the current network.
1610       *
1611       * @since 4.6.0
1612       *
1613       * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
1614       */
1615  	public static function network_enable_theme( $stylesheets ) {
1616          if ( ! is_multisite() ) {
1617              return;
1618          }
1619  
1620          if ( ! is_array( $stylesheets ) ) {
1621              $stylesheets = array( $stylesheets );
1622          }
1623  
1624          $allowed_themes = get_site_option( 'allowedthemes' );
1625          foreach ( $stylesheets as $stylesheet ) {
1626              $allowed_themes[ $stylesheet ] = true;
1627          }
1628  
1629          update_site_option( 'allowedthemes', $allowed_themes );
1630      }
1631  
1632      /**
1633       * Disables a theme for all sites on the current network.
1634       *
1635       * @since 4.6.0
1636       *
1637       * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names.
1638       */
1639  	public static function network_disable_theme( $stylesheets ) {
1640          if ( ! is_multisite() ) {
1641              return;
1642          }
1643  
1644          if ( ! is_array( $stylesheets ) ) {
1645              $stylesheets = array( $stylesheets );
1646          }
1647  
1648          $allowed_themes = get_site_option( 'allowedthemes' );
1649          foreach ( $stylesheets as $stylesheet ) {
1650              if ( isset( $allowed_themes[ $stylesheet ] ) ) {
1651                  unset( $allowed_themes[ $stylesheet ] );
1652              }
1653          }
1654  
1655          update_site_option( 'allowedthemes', $allowed_themes );
1656      }
1657  
1658      /**
1659       * Sorts themes by name.
1660       *
1661       * @since 3.4.0
1662       *
1663       * @param WP_Theme[] $themes Array of theme objects to sort (passed by reference).
1664       */
1665  	public static function sort_by_name( &$themes ) {
1666          if ( 0 === strpos( get_user_locale(), 'en_' ) ) {
1667              uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
1668          } else {
1669              foreach ( $themes as $key => $theme ) {
1670                  $theme->translate_header( 'Name', $theme->headers['Name'] );
1671              }
1672              uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) );
1673          }
1674      }
1675  
1676      /**
1677       * Callback function for usort() to naturally sort themes by name.
1678       *
1679       * Accesses the Name header directly from the class for maximum speed.
1680       * Would choke on HTML but we don't care enough to slow it down with strip_tags().
1681       *
1682       * @since 3.4.0
1683       *
1684       * @param WP_Theme $a First theme.
1685       * @param WP_Theme $b Second theme.
1686       * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally.
1687       *             Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort().
1688       */
1689  	private static function _name_sort( $a, $b ) {
1690          return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] );
1691      }
1692  
1693      /**
1694       * Callback function for usort() to naturally sort themes by translated name.
1695       *
1696       * @since 3.4.0
1697       *
1698       * @param WP_Theme $a First theme.
1699       * @param WP_Theme $b Second theme.
1700       * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally.
1701       *             Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort().
1702       */
1703  	private static function _name_sort_i18n( $a, $b ) {
1704          return strnatcasecmp( $a->name_translated, $b->name_translated );
1705      }
1706  }


Generated: Tue Oct 19 01:00:04 2021 Cross-referenced by PHPXref 0.7.1