| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WP_Theme Class 4 * 5 * @package WordPress 6 * @subpackage Theme 7 * @since 3.4.0 8 * 9 * @property-read string|false $name 10 * @property-read string|false $title 11 * @property-read string|false $version 12 * @property-read string|false $parent_theme 13 * @property-read string $template_dir 14 * @property-read string $stylesheet_dir 15 * @property-read string $template 16 * @property-read string $stylesheet 17 * @property-read string|false $screenshot 18 * @property-read string|false $description 19 * @property-read string|false $author 20 * @property-read string[]|false $tags 21 * @property-read string $theme_root 22 * @property-read string $theme_root_uri 23 * 24 * @phpstan-type Theme_Key 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme' 25 */ 26 #[AllowDynamicProperties] 27 final class WP_Theme implements ArrayAccess { 28 29 /** 30 * Whether the theme has been marked as updateable, or the theme's update data. 31 * 32 * This is false by default. WP_MS_Themes_List_Table sets it to true when an 33 * update is available for the theme, while get_theme_updates() sets it to the 34 * theme's entry in the `response` of the `update_themes` site transient. That 35 * entry always contains the `new_version`, and usually the `theme`, `url`, 36 * `package`, `requires`, and `requires_php` as well, where `requires` and 37 * `requires_php` are false when the theme does not declare them. Themes updated 38 * via the {@see 'update_themes_$hostname'} filter may also include other keys. 39 * 40 * @since 4.4.0 41 * @var bool|array<string, mixed> 42 * @phpstan-var bool|array{ 43 * new_version: string, 44 * theme?: string, 45 * url?: string, 46 * package?: string, 47 * requires?: string|false, 48 * requires_php?: string|false, 49 * ... 50 * } 51 * 52 * @see WP_MS_Themes_List_Table 53 * @see get_theme_updates() 54 */ 55 public $update = false; 56 57 /** 58 * Headers for style.css files. 59 * 60 * @since 3.4.0 61 * @since 5.4.0 Added `Requires at least` and `Requires PHP` headers. 62 * @since 6.1.0 Added `Update URI` header. 63 * @var string[] 64 */ 65 private static $file_headers = array( 66 'Name' => 'Theme Name', 67 'ThemeURI' => 'Theme URI', 68 'Description' => 'Description', 69 'Author' => 'Author', 70 'AuthorURI' => 'Author URI', 71 'Version' => 'Version', 72 'Template' => 'Template', 73 'Status' => 'Status', 74 'Tags' => 'Tags', 75 'TextDomain' => 'Text Domain', 76 'DomainPath' => 'Domain Path', 77 'RequiresWP' => 'Requires at least', 78 'RequiresPHP' => 'Requires PHP', 79 'UpdateURI' => 'Update URI', 80 ); 81 82 /** 83 * Default themes. 84 * 85 * @since 3.4.0 86 * @since 3.5.0 Added the Twenty Twelve theme. 87 * @since 3.6.0 Added the Twenty Thirteen theme. 88 * @since 3.8.0 Added the Twenty Fourteen theme. 89 * @since 4.1.0 Added the Twenty Fifteen theme. 90 * @since 4.4.0 Added the Twenty Sixteen theme. 91 * @since 4.7.0 Added the Twenty Seventeen theme. 92 * @since 5.0.0 Added the Twenty Nineteen theme. 93 * @since 5.3.0 Added the Twenty Twenty theme. 94 * @since 5.6.0 Added the Twenty Twenty-One theme. 95 * @since 5.9.0 Added the Twenty Twenty-Two theme. 96 * @since 6.1.0 Added the Twenty Twenty-Three theme. 97 * @since 6.4.0 Added the Twenty Twenty-Four theme. 98 * @since 6.7.0 Added the Twenty Twenty-Five theme. 99 * @since 7.2.0 Added the Ipsum theme. 100 * @var string[] 101 */ 102 private static $default_themes = array( 103 'classic' => 'WordPress Classic', 104 'default' => 'WordPress Default', 105 'twentyten' => 'Twenty Ten', 106 'twentyeleven' => 'Twenty Eleven', 107 'twentytwelve' => 'Twenty Twelve', 108 'twentythirteen' => 'Twenty Thirteen', 109 'twentyfourteen' => 'Twenty Fourteen', 110 'twentyfifteen' => 'Twenty Fifteen', 111 'twentysixteen' => 'Twenty Sixteen', 112 'twentyseventeen' => 'Twenty Seventeen', 113 'twentynineteen' => 'Twenty Nineteen', 114 'twentytwenty' => 'Twenty Twenty', 115 'twentytwentyone' => 'Twenty Twenty-One', 116 'twentytwentytwo' => 'Twenty Twenty-Two', 117 'twentytwentythree' => 'Twenty Twenty-Three', 118 'twentytwentyfour' => 'Twenty Twenty-Four', 119 'twentytwentyfive' => 'Twenty Twenty-Five', 120 'ipsum' => 'Ipsum', 121 ); 122 123 /** 124 * Renamed theme tags. 125 * 126 * @since 3.8.0 127 * @var string[] 128 */ 129 private static $tag_map = array( 130 'fixed-width' => 'fixed-layout', 131 'flexible-width' => 'fluid-layout', 132 ); 133 134 /** 135 * Absolute path to the theme root, usually wp-content/themes 136 * 137 * @since 3.4.0 138 * @var string 139 */ 140 private $theme_root; 141 142 /** 143 * Header data from the theme's style.css file. 144 * 145 * @since 3.4.0 146 * @var array 147 */ 148 private $headers = array(); 149 150 /** 151 * Header data from the theme's style.css file after being sanitized. 152 * 153 * @since 3.4.0 154 * @var ?array 155 */ 156 private $headers_sanitized; 157 158 /** 159 * Is this theme a block theme. 160 * 161 * @since 6.2.0 162 * @var ?bool 163 */ 164 private $block_theme; 165 166 /** 167 * Header name from the theme's style.css after being translated. 168 * 169 * Cached due to sorting functions running over the translated name. 170 * 171 * @since 3.4.0 172 * @var ?string 173 */ 174 private $name_translated; 175 176 /** 177 * Errors encountered when initializing the theme. 178 * 179 * @since 3.4.0 180 * @var ?WP_Error 181 */ 182 private $errors; 183 184 /** 185 * The directory name of the theme's files, inside the theme root. 186 * 187 * In the case of a child theme, this is directory name of the child theme. 188 * Otherwise, 'stylesheet' is the same as 'template'. 189 * 190 * @since 3.4.0 191 * @var string 192 */ 193 private $stylesheet; 194 195 /** 196 * The directory name of the theme's files, inside the theme root. 197 * 198 * In the case of a child theme, this is the directory name of the parent theme. 199 * Otherwise, 'template' is the same as 'stylesheet'. 200 * 201 * @since 3.4.0 202 * @var ?string 203 */ 204 private $template; 205 206 /** 207 * A reference to the parent theme, in the case of a child theme. 208 * 209 * @since 3.4.0 210 * @var ?WP_Theme 211 */ 212 private $parent; 213 214 /** 215 * URL to the theme root, usually an absolute URL to wp-content/themes 216 * 217 * @since 3.4.0 218 * @var ?string 219 */ 220 private $theme_root_uri; 221 222 /** 223 * Flag for whether the theme's textdomain is loaded. 224 * 225 * @since 3.4.0 226 * @var ?bool 227 */ 228 private $textdomain_loaded; 229 230 /** 231 * Stores an md5 hash of the theme root, to function as the cache key. 232 * 233 * @since 3.4.0 234 * @var string 235 */ 236 private $cache_hash; 237 238 /** 239 * Block template folders. 240 * 241 * @since 6.4.0 242 * @var ?string[] 243 */ 244 private $block_template_folders; 245 246 /** 247 * Default values for template folders. 248 * 249 * @since 6.4.0 250 * @var string[] 251 */ 252 private $default_template_folders = array( 253 'wp_template' => 'templates', 254 'wp_template_part' => 'parts', 255 ); 256 257 /** 258 * Flag for whether the themes cache bucket should be persistently cached. 259 * 260 * Default is false. Can be set with the {@see 'wp_cache_themes_persistently'} filter. 261 * 262 * @since 3.4.0 263 * @var bool 264 */ 265 private static $persistently_cache; 266 267 /** 268 * Expiration time for the themes cache bucket. 269 * 270 * By default the bucket is not cached, so this value is useless. 271 * 272 * @since 3.4.0 273 * @var int 274 */ 275 private static $cache_expiration = 1800; 276 277 /** 278 * Constructor for WP_Theme. 279 * 280 * @since 3.4.0 281 * 282 * @global string[] $wp_theme_directories 283 * 284 * @param string $theme_dir Directory of the theme within the theme_root. 285 * @param string $theme_root Theme root. 286 * @param WP_Theme|null $_child If this theme is a parent theme, the child may be passed for validation purposes. 287 */ 288 public function __construct( $theme_dir, $theme_root, $_child = null ) { 289 global $wp_theme_directories; 290 291 // Initialize caching on first run. 292 if ( ! isset( self::$persistently_cache ) ) { 293 /** This action is documented in wp-includes/theme.php */ 294 self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' ); 295 if ( self::$persistently_cache ) { 296 wp_cache_add_global_groups( 'themes' ); 297 if ( is_int( self::$persistently_cache ) ) { 298 self::$cache_expiration = self::$persistently_cache; 299 } 300 } else { 301 wp_cache_add_non_persistent_groups( 'themes' ); 302 } 303 } 304 305 // Handle a numeric theme directory as a string. 306 $theme_dir = (string) $theme_dir; 307 308 $this->theme_root = $theme_root; 309 $this->stylesheet = $theme_dir; 310 311 // Correct a situation where the theme is 'some-directory/some-theme' but 'some-directory' was passed in as part of the theme root instead. 312 if ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) 313 && in_array( dirname( $theme_root ), (array) $wp_theme_directories, true ) 314 ) { 315 $this->stylesheet = basename( $this->theme_root ) . '/' . $this->stylesheet; 316 $this->theme_root = dirname( $theme_root ); 317 } 318 319 $this->cache_hash = md5( $this->theme_root . '/' . $this->stylesheet ); 320 $theme_file = $this->stylesheet . '/style.css'; 321 322 $cache = $this->cache_get( 'theme' ); 323 324 if ( is_array( $cache ) ) { 325 foreach ( array( 'block_template_folders', 'block_theme', 'errors', 'headers', 'template' ) as $key ) { 326 if ( isset( $cache[ $key ] ) ) { 327 $this->$key = $cache[ $key ]; 328 } 329 } 330 if ( $this->errors ) { 331 return; 332 } 333 if ( isset( $cache['theme_root_template'] ) ) { 334 $theme_root_template = $cache['theme_root_template']; 335 } 336 } elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) { 337 $this->headers['Name'] = $this->stylesheet; 338 if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) ) { 339 $this->errors = new WP_Error( 340 'theme_not_found', 341 sprintf( 342 /* translators: %s: Theme directory name. */ 343 __( 'The theme directory "%s" does not exist.' ), 344 esc_html( $this->stylesheet ) 345 ) 346 ); 347 } else { 348 $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); 349 } 350 $this->template = $this->stylesheet; 351 $this->block_theme = false; 352 $this->block_template_folders = $this->default_template_folders; 353 $this->cache_add( 354 'theme', 355 array( 356 'block_template_folders' => $this->block_template_folders, 357 'block_theme' => $this->block_theme, 358 'headers' => $this->headers, 359 'errors' => $this->errors, 360 'stylesheet' => $this->stylesheet, 361 'template' => $this->template, 362 ) 363 ); 364 if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one. 365 $this->errors->add( 'theme_root_missing', __( '<strong>Error:</strong> The themes directory is either empty or does not exist. Please check your installation.' ) ); 366 } 367 return; 368 } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) { 369 $this->headers['Name'] = $this->stylesheet; 370 $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); 371 $this->template = $this->stylesheet; 372 $this->block_theme = false; 373 $this->block_template_folders = $this->default_template_folders; 374 $this->cache_add( 375 'theme', 376 array( 377 'block_template_folders' => $this->block_template_folders, 378 'block_theme' => $this->block_theme, 379 'headers' => $this->headers, 380 'errors' => $this->errors, 381 'stylesheet' => $this->stylesheet, 382 'template' => $this->template, 383 ) 384 ); 385 return; 386 } else { 387 $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); 388 /* 389 * Default themes always trump their pretenders. 390 * Properly identify default themes that are inside a directory within wp-content/themes. 391 */ 392 $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes, true ); 393 if ( $default_theme_slug ) { 394 if ( basename( $this->stylesheet ) !== $default_theme_slug ) { 395 $this->headers['Name'] .= '/' . $this->stylesheet; 396 } 397 } 398 } 399 400 if ( ! $this->template && $this->stylesheet === $this->headers['Template'] ) { 401 $this->errors = new WP_Error( 402 'theme_child_invalid', 403 sprintf( 404 /* translators: %s: Template. */ 405 __( 'The theme defines itself as its parent theme. Please check the %s header.' ), 406 '<code>Template</code>' 407 ) 408 ); 409 $this->template = $this->stylesheet; 410 $this->cache_add( 411 'theme', 412 array( 413 'block_template_folders' => $this->get_block_template_folders(), 414 'block_theme' => $this->is_block_theme(), 415 'headers' => $this->headers, 416 'errors' => $this->errors, 417 'stylesheet' => $this->stylesheet, 418 'template' => $this->template, 419 ) 420 ); 421 422 return; 423 } 424 425 // (If template is set from cache [and there are no errors], we know it's good.) 426 if ( ! $this->template ) { 427 $this->template = $this->headers['Template']; 428 } 429 430 if ( ! $this->template ) { 431 $this->template = $this->stylesheet; 432 $theme_path = $this->theme_root . '/' . $this->stylesheet; 433 434 if ( ! $this->is_block_theme() && ! file_exists( $theme_path . '/index.php' ) ) { 435 $error_message = sprintf( 436 /* translators: 1: templates/index.html, 2: index.php, 3: Documentation URL, 4: Template, 5: style.css */ 437 __( 'Template is missing. Standalone themes need to have a %1$s or %2$s template file. <a href="%3$s">Child themes</a> need to have a %4$s header in the %5$s stylesheet.' ), 438 '<code>templates/index.html</code>', 439 '<code>index.php</code>', 440 __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ), 441 '<code>Template</code>', 442 '<code>style.css</code>' 443 ); 444 $this->errors = new WP_Error( 'theme_no_index', $error_message ); 445 $this->cache_add( 446 'theme', 447 array( 448 'block_template_folders' => $this->get_block_template_folders(), 449 'block_theme' => $this->block_theme, 450 'headers' => $this->headers, 451 'errors' => $this->errors, 452 'stylesheet' => $this->stylesheet, 453 'template' => $this->template, 454 ) 455 ); 456 return; 457 } 458 } 459 460 // If we got our data from cache, we can assume that 'template' is pointing to the right place. 461 if ( ! is_array( $cache ) 462 && $this->template !== $this->stylesheet 463 && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) 464 ) { 465 /* 466 * If we're in a directory of themes inside /themes, look for the parent nearby. 467 * wp-content/themes/directory-of-themes/* 468 */ 469 $parent_dir = dirname( $this->stylesheet ); 470 $directories = search_theme_directories(); 471 472 if ( '.' !== $parent_dir 473 && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) 474 ) { 475 $this->template = $parent_dir . '/' . $this->template; 476 } elseif ( $directories && isset( $directories[ $this->template ] ) ) { 477 /* 478 * Look for the template in the search_theme_directories() results, in case it is in another theme root. 479 * We don't look into directories of themes, just the theme root. 480 */ 481 $theme_root_template = $directories[ $this->template ]['theme_root']; 482 } else { 483 // Parent theme is missing. 484 $this->errors = new WP_Error( 485 'theme_no_parent', 486 sprintf( 487 /* translators: %s: Theme directory name. */ 488 __( 'The parent theme is missing. Please install the "%s" parent theme.' ), 489 esc_html( $this->template ) 490 ) 491 ); 492 $this->cache_add( 493 'theme', 494 array( 495 'block_template_folders' => $this->get_block_template_folders(), 496 'block_theme' => $this->is_block_theme(), 497 'headers' => $this->headers, 498 'errors' => $this->errors, 499 'stylesheet' => $this->stylesheet, 500 'template' => $this->template, 501 ) 502 ); 503 $this->parent = new WP_Theme( $this->template, $this->theme_root, $this ); 504 return; 505 } 506 } 507 508 // Set the parent, if we're a child theme. 509 if ( $this->template !== $this->stylesheet ) { 510 // If we are a parent, then there is a problem. Only two generations allowed! Cancel things out. 511 if ( $_child instanceof WP_Theme && $_child->template === $this->stylesheet ) { 512 $_child->parent = null; 513 $_child->errors = new WP_Error( 514 'theme_parent_invalid', 515 sprintf( 516 /* translators: %s: Theme directory name. */ 517 __( 'The "%s" theme is not a valid parent theme.' ), 518 esc_html( $_child->template ) 519 ) 520 ); 521 $_child->cache_add( 522 'theme', 523 array( 524 'block_template_folders' => $_child->get_block_template_folders(), 525 'block_theme' => $_child->is_block_theme(), 526 'headers' => $_child->headers, 527 'errors' => $_child->errors, 528 'stylesheet' => $_child->stylesheet, 529 'template' => $_child->template, 530 ) 531 ); 532 // The two themes actually reference each other with the Template header. 533 if ( $_child->stylesheet === $this->template ) { 534 $this->errors = new WP_Error( 535 'theme_parent_invalid', 536 sprintf( 537 /* translators: %s: Theme directory name. */ 538 __( 'The "%s" theme is not a valid parent theme.' ), 539 esc_html( $this->template ) 540 ) 541 ); 542 $this->cache_add( 543 'theme', 544 array( 545 'block_template_folders' => $this->get_block_template_folders(), 546 'block_theme' => $this->is_block_theme(), 547 'headers' => $this->headers, 548 'errors' => $this->errors, 549 'stylesheet' => $this->stylesheet, 550 'template' => $this->template, 551 ) 552 ); 553 } 554 return; 555 } 556 // Set the parent. Pass the current instance so we can do the checks above and assess errors. 557 $this->parent = new WP_Theme( $this->template, $theme_root_template ?? $this->theme_root, $this ); 558 } 559 560 if ( wp_paused_themes()->get( $this->stylesheet ) && ( ! is_wp_error( $this->errors ) || ! isset( $this->errors->errors['theme_paused'] ) ) ) { 561 $this->errors = new WP_Error( 'theme_paused', __( 'This theme failed to load properly and was paused within the admin backend.' ) ); 562 } 563 564 // We're good. If we didn't retrieve from cache, set it. 565 if ( ! is_array( $cache ) ) { 566 $cache = array( 567 'block_theme' => $this->is_block_theme(), 568 'block_template_folders' => $this->get_block_template_folders(), 569 'headers' => $this->headers, 570 'errors' => $this->errors, 571 'stylesheet' => $this->stylesheet, 572 'template' => $this->template, 573 ); 574 // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above. 575 if ( isset( $theme_root_template ) ) { 576 $cache['theme_root_template'] = $theme_root_template; 577 } 578 $this->cache_add( 'theme', $cache ); 579 } 580 } 581 582 /** 583 * When converting the object to a string, the theme name is returned. 584 * 585 * @since 3.4.0 586 * 587 * @return string Theme name, ready for display (translated) 588 */ 589 public function __toString() { 590 return (string) $this->display( 'Name' ); 591 } 592 593 /** 594 * __isset() magic method for properties formerly returned by current_theme_info() 595 * 596 * @since 3.4.0 597 * 598 * @param string $offset Property to check if set. 599 * @return bool Whether the given property is set. 600 */ 601 public function __isset( $offset ) { 602 static $properties = array( 603 'name', 604 'title', 605 'version', 606 'parent_theme', 607 'template_dir', 608 'stylesheet_dir', 609 'template', 610 'stylesheet', 611 'screenshot', 612 'description', 613 'author', 614 'tags', 615 'theme_root', 616 'theme_root_uri', 617 ); 618 619 return in_array( $offset, $properties, true ); 620 } 621 622 /** 623 * __get() magic method for properties formerly returned by current_theme_info() 624 * 625 * @since 3.4.0 626 * 627 * @param string $offset Property to get. 628 * @return mixed Property value. 629 */ 630 public function __get( $offset ) { 631 switch ( $offset ) { 632 case 'name': 633 case 'title': 634 return $this->get( 'Name' ); 635 case 'version': 636 return $this->get( 'Version' ); 637 case 'parent_theme': 638 return $this->parent() ? $this->parent()->get( 'Name' ) : ''; 639 case 'template_dir': 640 return $this->get_template_directory(); 641 case 'stylesheet_dir': 642 return $this->get_stylesheet_directory(); 643 case 'template': 644 return $this->get_template(); 645 case 'stylesheet': 646 return $this->get_stylesheet(); 647 case 'screenshot': 648 return $this->get_screenshot( 'relative' ); 649 // 'author' and 'description' did not previously return translated data. 650 case 'description': 651 return $this->display( 'Description' ); 652 case 'author': 653 return $this->display( 'Author' ); 654 case 'tags': 655 return $this->get( 'Tags' ); 656 case 'theme_root': 657 return $this->get_theme_root(); 658 case 'theme_root_uri': 659 return $this->get_theme_root_uri(); 660 // For cases where the array was converted to an object. 661 default: 662 return $this->offsetGet( $offset ); 663 } 664 } 665 666 /** 667 * Method to implement ArrayAccess for keys formerly returned by get_themes() 668 * 669 * @since 3.4.0 670 * 671 * @param mixed $offset 672 * @param mixed $value 673 */ 674 #[ReturnTypeWillChange] 675 public function offsetSet( $offset, $value ) {} 676 677 /** 678 * Method to implement ArrayAccess for keys formerly returned by get_themes() 679 * 680 * @since 3.4.0 681 * 682 * @param mixed $offset 683 */ 684 #[ReturnTypeWillChange] 685 public function offsetUnset( $offset ) {} 686 687 /** 688 * Method to implement ArrayAccess for keys formerly returned by get_themes() 689 * 690 * @since 3.4.0 691 * 692 * @param mixed $offset 693 * @return bool 694 * 695 * @phpstan-return ( $offset is Theme_Key ? true : false ) 696 */ 697 #[ReturnTypeWillChange] 698 public function offsetExists( $offset ) { 699 static $keys = array( 700 'Name', 701 'Version', 702 'Status', 703 'Title', 704 'Author', 705 'Author Name', 706 'Author URI', 707 'Description', 708 'Template', 709 'Stylesheet', 710 'Template Files', 711 'Stylesheet Files', 712 'Template Dir', 713 'Stylesheet Dir', 714 'Screenshot', 715 'Tags', 716 'Theme Root', 717 'Theme Root URI', 718 'Parent Theme', 719 ); 720 721 return in_array( $offset, $keys, true ); 722 } 723 724 /** 725 * Method to implement ArrayAccess for keys formerly returned by get_themes(). 726 * 727 * Author, Author Name, Author URI, and Description did not previously return 728 * translated data. We are doing so now as it is safe to do. However, as 729 * Name and Title could have been used as the key for get_themes(), both remain 730 * untranslated for back compatibility. This means that ['Name'] is not ideal, 731 * and care should be taken to use `$theme::display( 'Name' )` to get a properly 732 * translated header. 733 * 734 * @since 3.4.0 735 * 736 * @param mixed $offset 737 * @return mixed 738 * 739 * @phpstan-return ( $offset is Theme_Key ? mixed : null ) 740 */ 741 #[ReturnTypeWillChange] 742 public function offsetGet( $offset ) { 743 switch ( $offset ) { 744 case 'Name': 745 case 'Title': 746 /* 747 * See note above about using translated data. get() is not ideal. 748 * It is only for backward compatibility. Use display(). 749 */ 750 return $this->get( 'Name' ); 751 case 'Author': 752 return $this->display( 'Author' ); 753 case 'Author Name': 754 return $this->display( 'Author', false ); 755 case 'Author URI': 756 return $this->display( 'AuthorURI' ); 757 case 'Description': 758 return $this->display( 'Description' ); 759 case 'Version': 760 case 'Status': 761 return $this->get( $offset ); 762 case 'Template': 763 return $this->get_template(); 764 case 'Stylesheet': 765 return $this->get_stylesheet(); 766 case 'Template Files': 767 return $this->get_files( 'php', 1, true ); 768 case 'Stylesheet Files': 769 return $this->get_files( 'css', 0, false ); 770 case 'Template Dir': 771 return $this->get_template_directory(); 772 case 'Stylesheet Dir': 773 return $this->get_stylesheet_directory(); 774 case 'Screenshot': 775 return $this->get_screenshot( 'relative' ); 776 case 'Tags': 777 return $this->get( 'Tags' ); 778 case 'Theme Root': 779 return $this->get_theme_root(); 780 case 'Theme Root URI': 781 return $this->get_theme_root_uri(); 782 case 'Parent Theme': 783 return $this->parent() ? $this->parent()->get( 'Name' ) : ''; 784 default: 785 return null; 786 } 787 } 788 789 /** 790 * Returns errors property. 791 * 792 * @since 3.4.0 793 * 794 * @return WP_Error|false WP_Error if there are errors, or false. 795 */ 796 public function errors() { 797 return is_wp_error( $this->errors ) ? $this->errors : false; 798 } 799 800 /** 801 * Determines whether the theme exists. 802 * 803 * A theme with errors exists. A theme with the error of 'theme_not_found', 804 * meaning that the theme's directory was not found, does not exist. 805 * 806 * @since 3.4.0 807 * 808 * @return bool Whether the theme exists. 809 */ 810 public function exists() { 811 return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes(), true ) ); 812 } 813 814 /** 815 * Returns reference to the parent theme. 816 * 817 * @since 3.4.0 818 * 819 * @return WP_Theme|false Parent theme, or false if the active theme is not a child theme. 820 */ 821 public function parent() { 822 return $this->parent ?? false; 823 } 824 825 /** 826 * Perform reinitialization tasks. 827 * 828 * Prevents a callback from being injected during unserialization of an object. 829 */ 830 public function __wakeup() { 831 if ( $this->parent && ! $this->parent instanceof self ) { 832 throw new UnexpectedValueException(); 833 } 834 if ( $this->headers && ! is_array( $this->headers ) ) { 835 throw new UnexpectedValueException(); 836 } 837 foreach ( $this->headers as $value ) { 838 if ( ! is_string( $value ) ) { 839 throw new UnexpectedValueException(); 840 } 841 } 842 $this->headers_sanitized = array(); 843 } 844 845 /** 846 * Adds theme data to cache. 847 * 848 * Cache entries keyed by the theme and the type of data. 849 * 850 * @since 3.4.0 851 * 852 * @param string $key Type of data to store (theme, screenshot, headers, post_templates) 853 * @param array|string $data Data to store 854 * @return bool Return value from wp_cache_add() 855 */ 856 private function cache_add( $key, $data ) { 857 return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration ); 858 } 859 860 /** 861 * Gets theme data from cache. 862 * 863 * Cache entries are keyed by the theme and the type of data. 864 * 865 * @since 3.4.0 866 * 867 * @param string $key Type of data to retrieve (theme, screenshot, headers, post_templates) 868 * @return mixed Retrieved data 869 */ 870 private function cache_get( $key ) { 871 return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' ); 872 } 873 874 /** 875 * Clears the cache for the theme. 876 * 877 * @since 3.4.0 878 */ 879 public function cache_delete() { 880 foreach ( array( 'theme', 'screenshot', 'headers', 'post_templates' ) as $key ) { 881 wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' ); 882 } 883 $this->template = null; 884 $this->textdomain_loaded = null; 885 $this->theme_root_uri = null; 886 $this->parent = null; 887 $this->errors = null; 888 $this->headers_sanitized = null; 889 $this->name_translated = null; 890 $this->block_theme = null; 891 $this->block_template_folders = null; 892 $this->headers = array(); 893 $this->__construct( $this->stylesheet, $this->theme_root ); 894 $this->delete_pattern_cache(); 895 } 896 897 /** 898 * Gets a raw, unformatted theme header. 899 * 900 * The header is sanitized, but is not translated, and is not marked up for display. 901 * To get a theme header for display, use the display() method. 902 * 903 * Use the get_template() method, not the 'Template' header, for finding the template. 904 * The 'Template' header is only good for what was written in the style.css, while 905 * get_template() takes into account where WordPress actually located the theme and 906 * whether it is actually valid. 907 * 908 * @since 3.4.0 909 * 910 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. 911 * @return string|array|false String or array (for Tags header) on success, false on failure. 912 * 913 * @phpstan-return ( 914 * $header is 'Tags' 915 * ? string[]|false 916 * : ( $header is 'Name'|'ThemeURI'|'Description'|'Author'|'AuthorURI'|'Version'|'Template'|'Status'|'TextDomain'|'DomainPath'|'RequiresWP'|'RequiresPHP'|'UpdateURI' 917 * ? string|false 918 * : false ) 919 * ) 920 */ 921 public function get( $header ) { 922 if ( ! isset( $this->headers[ $header ] ) ) { 923 return false; 924 } 925 926 if ( ! isset( $this->headers_sanitized ) ) { 927 $this->headers_sanitized = $this->cache_get( 'headers' ); 928 if ( ! is_array( $this->headers_sanitized ) ) { 929 $this->headers_sanitized = array(); 930 } 931 } 932 933 if ( isset( $this->headers_sanitized[ $header ] ) ) { 934 return $this->headers_sanitized[ $header ]; 935 } 936 937 // If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets. 938 if ( self::$persistently_cache ) { 939 foreach ( array_keys( $this->headers ) as $_header ) { 940 $this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] ); 941 } 942 $this->cache_add( 'headers', $this->headers_sanitized ); 943 } else { 944 $this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] ); 945 } 946 947 return $this->headers_sanitized[ $header ]; 948 } 949 950 /** 951 * Gets a theme header, formatted and translated for display. 952 * 953 * @since 3.4.0 954 * 955 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. 956 * @param bool $markup Optional. Whether to mark up the header. Defaults to true. 957 * @param bool $translate Optional. Whether to translate the header. Defaults to true. 958 * @return string|array|false Processed header. An array for Tags if `$markup` is false, string otherwise. 959 * False on failure. 960 * 961 * @phpstan-return ( 962 * $markup is false 963 * ? ( $header is 'Tags' 964 * ? string[]|false 965 * : string|false ) 966 * : string|false 967 * ) 968 */ 969 public function display( $header, $markup = true, $translate = true ) { 970 $value = $this->get( $header ); 971 if ( false === $value ) { 972 return false; 973 } 974 975 if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) ) { 976 $translate = false; 977 } 978 979 if ( $translate ) { 980 $value = $this->translate_header( $header, $value ); 981 } 982 983 if ( $markup ) { 984 $value = $this->markup_header( $header, $value, $translate ); 985 } 986 987 return $value; 988 } 989 990 /** 991 * Sanitizes a theme header. 992 * 993 * @since 3.4.0 994 * @since 5.4.0 Added support for `Requires at least` and `Requires PHP` headers. 995 * @since 6.1.0 Added support for `Update URI` header. 996 * 997 * @param string $header Theme header. Accepts 'Name', 'Description', 'Author', 'Version', 998 * 'ThemeURI', 'AuthorURI', 'Status', 'Tags', 'RequiresWP', 'RequiresPHP', 999 * 'UpdateURI'. 1000 * @param string $value Value to sanitize. 1001 * @return string|string[] An array for Tags header, string otherwise. 1002 * 1003 * @phpstan-return ( $header is 'Tags' ? string[] : string ) 1004 */ 1005 private function sanitize_header( $header, $value ) { 1006 switch ( $header ) { 1007 case 'Status': 1008 if ( ! $value ) { 1009 $value = 'publish'; 1010 break; 1011 } 1012 // Fall through otherwise. 1013 case 'Name': 1014 static $header_tags = array( 1015 'abbr' => array( 'title' => true ), 1016 'acronym' => array( 'title' => true ), 1017 'code' => true, 1018 'em' => true, 1019 'strong' => true, 1020 ); 1021 1022 $value = wp_kses( $value, $header_tags ); 1023 break; 1024 case 'Author': 1025 // There shouldn't be anchor tags in Author, but some themes like to be challenging. 1026 case 'Description': 1027 static $header_tags_with_a = array( 1028 'a' => array( 1029 'href' => true, 1030 'title' => true, 1031 ), 1032 'abbr' => array( 'title' => true ), 1033 'acronym' => array( 'title' => true ), 1034 'code' => true, 1035 'em' => true, 1036 'strong' => true, 1037 ); 1038 1039 $value = wp_kses( $value, $header_tags_with_a ); 1040 break; 1041 case 'ThemeURI': 1042 case 'AuthorURI': 1043 $value = sanitize_url( $value ); 1044 break; 1045 case 'Tags': 1046 $value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) ); 1047 break; 1048 case 'Version': 1049 case 'RequiresWP': 1050 case 'RequiresPHP': 1051 case 'UpdateURI': 1052 $value = strip_tags( $value ); 1053 break; 1054 } 1055 1056 return $value; 1057 } 1058 1059 /** 1060 * Marks up a theme header. 1061 * 1062 * @since 3.4.0 1063 * 1064 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. 1065 * @param string|array $value Value to mark up. An array for Tags header, string otherwise. 1066 * @param bool $translate Whether the header has been translated. 1067 * @return string Value, marked up. 1068 */ 1069 private function markup_header( $header, $value, $translate ) { 1070 switch ( $header ) { 1071 case 'Name': 1072 if ( empty( $value ) ) { 1073 $value = esc_html( $this->get_stylesheet() ); 1074 } 1075 break; 1076 case 'Description': 1077 $value = wptexturize( $value ); 1078 break; 1079 case 'Author': 1080 if ( $this->get( 'AuthorURI' ) ) { 1081 $value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value ); 1082 } elseif ( ! $value ) { 1083 $value = __( 'Anonymous' ); 1084 } 1085 break; 1086 case 'Tags': 1087 static $comma = null; 1088 if ( ! isset( $comma ) ) { 1089 $comma = wp_get_list_item_separator(); 1090 } 1091 $value = implode( $comma, $value ); 1092 break; 1093 case 'ThemeURI': 1094 case 'AuthorURI': 1095 $value = esc_url( $value ); 1096 break; 1097 } 1098 1099 return $value; 1100 } 1101 1102 /** 1103 * Translates a theme header. 1104 * 1105 * @since 3.4.0 1106 * 1107 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. 1108 * @param string|string[] $value Value to translate. An array for Tags header, string otherwise. 1109 * @return string|string[] Translated value. An array for Tags header, string otherwise. 1110 * 1111 * @phpstan-return ( $value is string ? string : string[] ) 1112 */ 1113 private function translate_header( $header, $value ) { 1114 switch ( $header ) { 1115 case 'Name': 1116 // Cached for sorting reasons. 1117 if ( isset( $this->name_translated ) ) { 1118 return $this->name_translated; 1119 } 1120 1121 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain 1122 $this->name_translated = translate( $value, $this->get( 'TextDomain' ) ); 1123 1124 return $this->name_translated; 1125 case 'Tags': 1126 if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) ) { 1127 return $value; 1128 } 1129 1130 static $tags_list; 1131 if ( ! isset( $tags_list ) ) { 1132 $tags_list = array( 1133 // As of 4.6, deprecated tags which are only used to provide translation for older themes. 1134 'black' => __( 'Black' ), 1135 'blue' => __( 'Blue' ), 1136 'brown' => __( 'Brown' ), 1137 'gray' => __( 'Gray' ), 1138 'green' => __( 'Green' ), 1139 'orange' => __( 'Orange' ), 1140 'pink' => __( 'Pink' ), 1141 'purple' => __( 'Purple' ), 1142 'red' => __( 'Red' ), 1143 'silver' => __( 'Silver' ), 1144 'tan' => __( 'Tan' ), 1145 'white' => __( 'White' ), 1146 'yellow' => __( 'Yellow' ), 1147 'dark' => _x( 'Dark', 'color scheme' ), 1148 'light' => _x( 'Light', 'color scheme' ), 1149 'fixed-layout' => __( 'Fixed Layout' ), 1150 'fluid-layout' => __( 'Fluid Layout' ), 1151 'responsive-layout' => __( 'Responsive Layout' ), 1152 'blavatar' => __( 'Blavatar' ), 1153 'photoblogging' => __( 'Photoblogging' ), 1154 'seasonal' => __( 'Seasonal' ), 1155 ); 1156 1157 $feature_list = get_theme_feature_list( false ); // No API. 1158 1159 foreach ( $feature_list as $tags ) { 1160 $tags_list += $tags; 1161 } 1162 } 1163 1164 foreach ( $value as &$tag ) { 1165 if ( isset( $tags_list[ $tag ] ) ) { 1166 $tag = $tags_list[ $tag ]; 1167 } elseif ( isset( self::$tag_map[ $tag ] ) ) { 1168 $tag = $tags_list[ self::$tag_map[ $tag ] ]; 1169 } 1170 } 1171 1172 return $value; 1173 1174 default: 1175 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain 1176 $value = translate( $value, $this->get( 'TextDomain' ) ); 1177 } 1178 return $value; 1179 } 1180 1181 /** 1182 * Returns the directory name of the theme's "stylesheet" files, inside the theme root. 1183 * 1184 * In the case of a child theme, this is directory name of the child theme. 1185 * Otherwise, get_stylesheet() is the same as get_template(). 1186 * 1187 * @since 3.4.0 1188 * 1189 * @return string Stylesheet 1190 */ 1191 public function get_stylesheet() { 1192 return $this->stylesheet; 1193 } 1194 1195 /** 1196 * Returns the directory name of the theme's "template" files, inside the theme root. 1197 * 1198 * In the case of a child theme, this is the directory name of the parent theme. 1199 * Otherwise, the get_template() is the same as get_stylesheet(). 1200 * 1201 * @since 3.4.0 1202 * 1203 * @return string Template 1204 */ 1205 public function get_template() { 1206 return $this->template; 1207 } 1208 1209 /** 1210 * Returns the absolute path to the directory of a theme's "stylesheet" files. 1211 * 1212 * In the case of a child theme, this is the absolute path to the directory 1213 * of the child theme's files. 1214 * 1215 * @since 3.4.0 1216 * 1217 * @return string Absolute path of the stylesheet directory. 1218 */ 1219 public function get_stylesheet_directory() { 1220 if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes(), true ) ) { 1221 return ''; 1222 } 1223 1224 return $this->theme_root . '/' . $this->stylesheet; 1225 } 1226 1227 /** 1228 * Returns the absolute path to the directory of a theme's "template" files. 1229 * 1230 * In the case of a child theme, this is the absolute path to the directory 1231 * of the parent theme's files. 1232 * 1233 * @since 3.4.0 1234 * 1235 * @return string Absolute path of the template directory. 1236 */ 1237 public function get_template_directory() { 1238 if ( $this->parent() ) { 1239 $theme_root = $this->parent()->theme_root; 1240 } else { 1241 $theme_root = $this->theme_root; 1242 } 1243 1244 return $theme_root . '/' . $this->template; 1245 } 1246 1247 /** 1248 * Returns the URL to the directory of a theme's "stylesheet" files. 1249 * 1250 * In the case of a child theme, this is the URL to the directory of the 1251 * child theme's files. 1252 * 1253 * @since 3.4.0 1254 * 1255 * @return string URL to the stylesheet directory. 1256 */ 1257 public function get_stylesheet_directory_uri() { 1258 return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) ); 1259 } 1260 1261 /** 1262 * Returns the URL to the directory of a theme's "template" files. 1263 * 1264 * In the case of a child theme, this is the URL to the directory of the 1265 * parent theme's files. 1266 * 1267 * @since 3.4.0 1268 * 1269 * @return string URL to the template directory. 1270 */ 1271 public function get_template_directory_uri() { 1272 if ( $this->parent() ) { 1273 $theme_root_uri = $this->parent()->get_theme_root_uri(); 1274 } else { 1275 $theme_root_uri = $this->get_theme_root_uri(); 1276 } 1277 1278 return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) ); 1279 } 1280 1281 /** 1282 * Returns the absolute path to the directory of the theme root. 1283 * 1284 * This is typically the absolute path to wp-content/themes. 1285 * 1286 * @since 3.4.0 1287 * 1288 * @return string Theme root. 1289 */ 1290 public function get_theme_root() { 1291 return $this->theme_root; 1292 } 1293 1294 /** 1295 * Returns the URL to the directory of the theme root. 1296 * 1297 * This is typically the absolute URL to wp-content/themes. This forms the basis 1298 * for all other URLs returned by WP_Theme, so we pass it to the public function 1299 * get_theme_root_uri() and allow it to run the {@see 'theme_root_uri'} filter. 1300 * 1301 * @since 3.4.0 1302 * 1303 * @return string Theme root URI. 1304 */ 1305 public function get_theme_root_uri() { 1306 if ( ! isset( $this->theme_root_uri ) ) { 1307 $this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root ); 1308 } 1309 return $this->theme_root_uri; 1310 } 1311 1312 /** 1313 * Returns the main screenshot file for the theme. 1314 * 1315 * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed. 1316 * 1317 * Screenshots for a theme must be in the stylesheet directory. (In the case of child 1318 * themes, parent theme screenshots are not inherited.) 1319 * 1320 * @since 3.4.0 1321 * 1322 * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI. 1323 * @return string|false Screenshot file. False if the theme does not have a screenshot. 1324 */ 1325 public function get_screenshot( $uri = 'uri' ) { 1326 $screenshot = $this->cache_get( 'screenshot' ); 1327 if ( $screenshot ) { 1328 if ( 'relative' === $uri ) { 1329 return $screenshot; 1330 } 1331 return $this->get_stylesheet_directory_uri() . '/' . $screenshot; 1332 } elseif ( 0 === $screenshot ) { 1333 return false; 1334 } 1335 1336 foreach ( array( 'png', 'gif', 'jpg', 'jpeg', 'webp', 'avif' ) as $ext ) { 1337 if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) { 1338 $this->cache_add( 'screenshot', 'screenshot.' . $ext ); 1339 if ( 'relative' === $uri ) { 1340 return 'screenshot.' . $ext; 1341 } 1342 return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext; 1343 } 1344 } 1345 1346 $this->cache_add( 'screenshot', 0 ); 1347 return false; 1348 } 1349 1350 /** 1351 * Returns files in the theme's directory. 1352 * 1353 * @since 3.4.0 1354 * 1355 * @param string[]|string $type Optional. Array of extensions to find, string of a single extension, 1356 * or null for all extensions. Default null. 1357 * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). 1358 * -1 depth is infinite. 1359 * @param bool $search_parent Optional. Whether to return parent files. Default false. 1360 * @return string[] Array of files, keyed by the path to the file relative to the theme's directory, with the values 1361 * being absolute paths. 1362 */ 1363 public function get_files( $type = null, $depth = 0, $search_parent = false ) { 1364 $files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth ); 1365 1366 if ( $search_parent && $this->parent() ) { 1367 $files += (array) self::scandir( $this->get_template_directory(), $type, $depth ); 1368 } 1369 1370 return array_filter( $files ); 1371 } 1372 1373 /** 1374 * Returns the theme's post templates. 1375 * 1376 * @since 4.7.0 1377 * @since 5.8.0 Include block templates. 1378 * 1379 * @return array[] Array of page template arrays, keyed by post type and filename, 1380 * with the value of the translated header name. 1381 */ 1382 public function get_post_templates() { 1383 // If you screw up your active theme and we invalidate your parent, most things still work. Let it slide. 1384 if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) { 1385 return array(); 1386 } 1387 1388 $post_templates = $this->cache_get( 'post_templates' ); 1389 1390 if ( ! is_array( $post_templates ) ) { 1391 $post_templates = array(); 1392 1393 $files = (array) $this->get_files( 'php', 1, true ); 1394 1395 foreach ( $files as $file => $full_path ) { 1396 $headers = get_file_data( 1397 $full_path, 1398 array( 1399 'TemplateName' => 'Template Name', 1400 'TemplatePostType' => 'Template Post Type', 1401 ), 1402 'theme' 1403 ); 1404 1405 if ( ! $headers['TemplateName'] ) { 1406 continue; 1407 } 1408 1409 $types = array( 'page' ); 1410 if ( $headers['TemplatePostType'] ) { 1411 $types = explode( ',', $headers['TemplatePostType'] ); 1412 } 1413 1414 foreach ( $types as $type ) { 1415 $type = sanitize_key( $type ); 1416 $post_templates[ $type ] ??= array(); 1417 1418 $post_templates[ $type ][ $file ] = $headers['TemplateName']; 1419 } 1420 } 1421 1422 $this->cache_add( 'post_templates', $post_templates ); 1423 } 1424 1425 if ( current_theme_supports( 'block-templates' ) ) { 1426 $block_templates = get_block_templates( array(), 'wp_template' ); 1427 foreach ( get_post_types( array( 'public' => true ) ) as $type ) { 1428 foreach ( $block_templates as $block_template ) { 1429 if ( ! $block_template->is_custom ) { 1430 continue; 1431 } 1432 1433 if ( isset( $block_template->post_types ) && ! in_array( $type, $block_template->post_types, true ) ) { 1434 continue; 1435 } 1436 1437 $post_templates[ $type ][ $block_template->slug ] = $block_template->title; 1438 } 1439 } 1440 } 1441 1442 if ( $this->load_textdomain() ) { 1443 foreach ( $post_templates as &$post_type ) { 1444 foreach ( $post_type as &$post_template ) { 1445 $post_template = $this->translate_header( 'Template Name', $post_template ); 1446 } 1447 } 1448 } 1449 1450 return $post_templates; 1451 } 1452 1453 /** 1454 * Returns the theme's post templates for a given post type. 1455 * 1456 * @since 3.4.0 1457 * @since 4.7.0 Added the `$post_type` parameter. 1458 * 1459 * @param WP_Post|null $post Optional. The post being edited, provided for context. 1460 * @param string $post_type Optional. Post type to get the templates for. Default 'page'. 1461 * If a post is provided, its post type is used. 1462 * @return string[] Array of template header names keyed by the template file name. 1463 */ 1464 public function get_page_templates( $post = null, $post_type = 'page' ) { 1465 if ( $post ) { 1466 $post_type = get_post_type( $post ); 1467 } 1468 1469 $post_templates = $this->get_post_templates(); 1470 $post_templates = $post_templates[ $post_type ] ?? array(); 1471 1472 /** 1473 * Filters list of page templates for a theme. 1474 * 1475 * @since 4.9.6 1476 * 1477 * @param string[] $post_templates Array of template header names keyed by the template file name. 1478 * @param WP_Theme $theme The theme object. 1479 * @param WP_Post|null $post The post being edited, provided for context, or null. 1480 * @param string $post_type Post type to get the templates for. 1481 */ 1482 $post_templates = (array) apply_filters( 'theme_templates', $post_templates, $this, $post, $post_type ); 1483 1484 /** 1485 * Filters list of page templates for a theme. 1486 * 1487 * The dynamic portion of the hook name, `$post_type`, refers to the post type. 1488 * 1489 * Possible hook names include: 1490 * 1491 * - `theme_post_templates` 1492 * - `theme_page_templates` 1493 * - `theme_attachment_templates` 1494 * 1495 * @since 3.9.0 1496 * @since 4.4.0 Converted to allow complete control over the `$page_templates` array. 1497 * @since 4.7.0 Added the `$post_type` parameter. 1498 * 1499 * @param string[] $post_templates Array of template header names keyed by the template file name. 1500 * @param WP_Theme $theme The theme object. 1501 * @param WP_Post|null $post The post being edited, provided for context, or null. 1502 * @param string $post_type Post type to get the templates for. 1503 */ 1504 $post_templates = (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type ); 1505 1506 return $post_templates; 1507 } 1508 1509 /** 1510 * Scans a directory for files of a certain extension. 1511 * 1512 * @since 3.4.0 1513 * 1514 * @param string $path Absolute path to search. 1515 * @param array|string|null $extensions Optional. Array of extensions to find, string of a single extension, 1516 * or null for all extensions. Default null. 1517 * @param int $depth Optional. How many levels deep to search for files. Accepts 0, 1+, or 1518 * -1 (infinite depth). Default 0. 1519 * @param string $relative_path Optional. The basename of the absolute path. Used to control the 1520 * returned path for the found files, particularly when this function 1521 * recurses to lower depths. Default empty. 1522 * @return string[]|false Array of files, keyed by the path to the file relative to the `$path` directory prepended 1523 * with `$relative_path`, with the values being absolute paths. False otherwise. 1524 */ 1525 private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) { 1526 if ( ! is_dir( $path ) ) { 1527 return false; 1528 } 1529 1530 if ( $extensions ) { 1531 $extensions = (array) $extensions; 1532 $_extensions = implode( '|', $extensions ); 1533 } 1534 1535 $relative_path = trailingslashit( $relative_path ); 1536 if ( '/' === $relative_path ) { 1537 $relative_path = ''; 1538 } 1539 1540 $results = scandir( $path ); 1541 $files = array(); 1542 1543 /** 1544 * Filters the array of excluded directories and files while scanning theme folder. 1545 * 1546 * @since 4.7.4 1547 * 1548 * @param string[] $exclusions Array of excluded directories and files. 1549 */ 1550 $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) ); 1551 1552 foreach ( $results as $result ) { 1553 if ( '.' === $result[0] || in_array( $result, $exclusions, true ) ) { 1554 continue; 1555 } 1556 if ( is_dir( $path . '/' . $result ) ) { 1557 if ( ! $depth ) { 1558 continue; 1559 } 1560 $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1, $relative_path . $result ); 1561 $files = array_merge_recursive( $files, $found ); 1562 } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) { 1563 $files[ $relative_path . $result ] = $path . '/' . $result; 1564 } 1565 } 1566 1567 return $files; 1568 } 1569 1570 /** 1571 * Loads the theme's textdomain. 1572 * 1573 * Translation files are not inherited from the parent theme. TODO: If this fails for the 1574 * child theme, it should probably try to load the parent theme's translations. 1575 * 1576 * @since 3.4.0 1577 * 1578 * @return bool True if the textdomain was successfully loaded or has already been loaded. 1579 * False if no textdomain was specified in the file headers, or if the domain could not be loaded. 1580 */ 1581 public function load_textdomain() { 1582 if ( isset( $this->textdomain_loaded ) ) { 1583 return $this->textdomain_loaded; 1584 } 1585 1586 $textdomain = $this->get( 'TextDomain' ); 1587 if ( ! $textdomain ) { 1588 $this->textdomain_loaded = false; 1589 return false; 1590 } 1591 1592 if ( is_textdomain_loaded( $textdomain ) ) { 1593 $this->textdomain_loaded = true; 1594 return true; 1595 } 1596 1597 $path = $this->get_stylesheet_directory(); 1598 $domainpath = $this->get( 'DomainPath' ); 1599 if ( $domainpath ) { 1600 $path .= $domainpath; 1601 } else { 1602 $path .= '/languages'; 1603 } 1604 1605 $this->textdomain_loaded = load_theme_textdomain( $textdomain, $path ); 1606 return $this->textdomain_loaded; 1607 } 1608 1609 /** 1610 * Determines whether the theme is allowed (multisite only). 1611 * 1612 * @since 3.4.0 1613 * 1614 * @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site' 1615 * settings, or 'both'. Defaults to 'both'. 1616 * @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current site. 1617 * @return bool Whether the theme is allowed for the network. Returns true in single-site. 1618 */ 1619 public function is_allowed( $check = 'both', $blog_id = null ) { 1620 if ( ! is_multisite() ) { 1621 return true; 1622 } 1623 1624 if ( 'both' === $check || 'network' === $check ) { 1625 $allowed = self::get_allowed_on_network(); 1626 if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) { 1627 return true; 1628 } 1629 } 1630 1631 if ( 'both' === $check || 'site' === $check ) { 1632 $allowed = self::get_allowed_on_site( $blog_id ); 1633 if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) { 1634 return true; 1635 } 1636 } 1637 1638 return false; 1639 } 1640 1641 /** 1642 * Returns whether this theme is a block-based theme or not. 1643 * 1644 * @since 5.9.0 1645 * 1646 * @return bool 1647 */ 1648 public function is_block_theme() { 1649 if ( isset( $this->block_theme ) ) { 1650 return $this->block_theme; 1651 } 1652 1653 $paths_to_index_block_template = array( 1654 $this->get_file_path( '/templates/index.html' ), 1655 $this->get_file_path( '/block-templates/index.html' ), 1656 ); 1657 1658 $this->block_theme = false; 1659 1660 foreach ( $paths_to_index_block_template as $path_to_index_block_template ) { 1661 if ( is_file( $path_to_index_block_template ) && is_readable( $path_to_index_block_template ) ) { 1662 $this->block_theme = true; 1663 break; 1664 } 1665 } 1666 1667 return $this->block_theme; 1668 } 1669 1670 /** 1671 * Retrieves the path of a file in the theme. 1672 * 1673 * Searches in the stylesheet directory before the template directory so themes 1674 * which inherit from a parent theme can just override one file. 1675 * 1676 * @since 5.9.0 1677 * 1678 * @param string $file Optional. File to search for in the stylesheet directory. 1679 * @return string The path of the file. 1680 */ 1681 public function get_file_path( $file = '' ) { 1682 $file = ltrim( $file, '/' ); 1683 1684 $stylesheet_directory = $this->get_stylesheet_directory(); 1685 $template_directory = $this->get_template_directory(); 1686 1687 if ( empty( $file ) ) { 1688 $path = $stylesheet_directory; 1689 } elseif ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/' . $file ) ) { 1690 $path = $stylesheet_directory . '/' . $file; 1691 } else { 1692 $path = $template_directory . '/' . $file; 1693 } 1694 1695 /** This filter is documented in wp-includes/link-template.php */ 1696 return apply_filters( 'theme_file_path', $path, $file ); 1697 } 1698 1699 /** 1700 * Determines the latest WordPress default theme that is installed. 1701 * 1702 * This hits the filesystem. 1703 * 1704 * @since 4.4.0 1705 * 1706 * @return WP_Theme|false Object, or false if no theme is installed, which would be bad. 1707 */ 1708 public static function get_core_default_theme() { 1709 foreach ( array_reverse( self::$default_themes ) as $slug => $name ) { 1710 $theme = wp_get_theme( $slug ); 1711 if ( $theme->exists() ) { 1712 return $theme; 1713 } 1714 } 1715 return false; 1716 } 1717 1718 /** 1719 * Returns array of stylesheet names of themes allowed on the site or network. 1720 * 1721 * @since 3.4.0 1722 * 1723 * @param int $blog_id Optional. ID of the site. Defaults to the current site. 1724 * @return string[] Array of stylesheet names. 1725 */ 1726 public static function get_allowed( $blog_id = null ) { 1727 /** 1728 * Filters the array of themes allowed on the network. 1729 * 1730 * Site is provided as context so that a list of network allowed themes can 1731 * be filtered further. 1732 * 1733 * @since 4.5.0 1734 * 1735 * @param string[] $allowed_themes An array of theme stylesheet names. 1736 * @param int $blog_id ID of the site. 1737 */ 1738 $network = (array) apply_filters( 'network_allowed_themes', self::get_allowed_on_network(), $blog_id ); 1739 return $network + self::get_allowed_on_site( $blog_id ); 1740 } 1741 1742 /** 1743 * Returns array of stylesheet names of themes allowed on the network. 1744 * 1745 * @since 3.4.0 1746 * 1747 * @return string[] Array of stylesheet names. 1748 */ 1749 public static function get_allowed_on_network() { 1750 static $allowed_themes; 1751 if ( ! isset( $allowed_themes ) ) { 1752 $allowed_themes = (array) get_site_option( 'allowedthemes' ); 1753 } 1754 1755 /** 1756 * Filters the array of themes allowed on the network. 1757 * 1758 * @since MU (3.0.0) 1759 * 1760 * @param string[] $allowed_themes An array of theme stylesheet names. 1761 */ 1762 $allowed_themes = apply_filters( 'allowed_themes', $allowed_themes ); 1763 1764 return $allowed_themes; 1765 } 1766 1767 /** 1768 * Returns array of stylesheet names of themes allowed on the site. 1769 * 1770 * @since 3.4.0 1771 * 1772 * @param int $blog_id Optional. ID of the site. Defaults to the current site. 1773 * @return string[] Array of stylesheet names. 1774 */ 1775 public static function get_allowed_on_site( $blog_id = null ) { 1776 static $allowed_themes = array(); 1777 1778 if ( ! $blog_id || ! is_multisite() ) { 1779 $blog_id = get_current_blog_id(); 1780 } 1781 1782 if ( isset( $allowed_themes[ $blog_id ] ) ) { 1783 /** 1784 * Filters the array of themes allowed on the site. 1785 * 1786 * @since 4.5.0 1787 * 1788 * @param string[] $allowed_themes An array of theme stylesheet names. 1789 * @param int $blog_id ID of the site. Defaults to current site. 1790 */ 1791 return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); 1792 } 1793 1794 $current = get_current_blog_id() === $blog_id; 1795 1796 if ( $current ) { 1797 $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' ); 1798 } else { 1799 switch_to_blog( $blog_id ); 1800 $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' ); 1801 restore_current_blog(); 1802 } 1803 1804 /* 1805 * This is all super old MU back compat joy. 1806 * 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name. 1807 */ 1808 if ( false === $allowed_themes[ $blog_id ] ) { 1809 if ( $current ) { 1810 $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' ); 1811 } else { 1812 switch_to_blog( $blog_id ); 1813 $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' ); 1814 restore_current_blog(); 1815 } 1816 1817 if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) { 1818 $allowed_themes[ $blog_id ] = array(); 1819 } else { 1820 $converted = array(); 1821 $themes = wp_get_themes(); 1822 foreach ( $themes as $stylesheet => $theme_data ) { 1823 if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get( 'Name' ) ] ) ) { 1824 $converted[ $stylesheet ] = true; 1825 } 1826 } 1827 $allowed_themes[ $blog_id ] = $converted; 1828 } 1829 // Set the option so we never have to go through this pain again. 1830 if ( is_admin() && $allowed_themes[ $blog_id ] ) { 1831 if ( $current ) { 1832 update_option( 'allowedthemes', $allowed_themes[ $blog_id ], false ); 1833 delete_option( 'allowed_themes' ); 1834 } else { 1835 switch_to_blog( $blog_id ); 1836 update_option( 'allowedthemes', $allowed_themes[ $blog_id ], false ); 1837 delete_option( 'allowed_themes' ); 1838 restore_current_blog(); 1839 } 1840 } 1841 } 1842 1843 /** This filter is documented in wp-includes/class-wp-theme.php */ 1844 return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); 1845 } 1846 1847 /** 1848 * Returns the folder names of the block template directories. 1849 * 1850 * @since 6.4.0 1851 * 1852 * @return string[] { 1853 * Folder names used by block themes. 1854 * 1855 * @type string $wp_template Theme-relative directory name for block templates. 1856 * @type string $wp_template_part Theme-relative directory name for block template parts. 1857 * } 1858 */ 1859 public function get_block_template_folders() { 1860 // Return set/cached value if available. 1861 if ( isset( $this->block_template_folders ) ) { 1862 return $this->block_template_folders; 1863 } 1864 1865 $this->block_template_folders = $this->default_template_folders; 1866 1867 $stylesheet_directory = $this->get_stylesheet_directory(); 1868 // If the theme uses deprecated block template folders. 1869 if ( file_exists( $stylesheet_directory . '/block-templates' ) || file_exists( $stylesheet_directory . '/block-template-parts' ) ) { 1870 $this->block_template_folders = array( 1871 'wp_template' => 'block-templates', 1872 'wp_template_part' => 'block-template-parts', 1873 ); 1874 } 1875 return $this->block_template_folders; 1876 } 1877 1878 /** 1879 * Gets block pattern data for a specified theme. 1880 * Each pattern is defined as a PHP file and defines 1881 * its metadata using plugin-style headers. The minimum required definition is: 1882 * 1883 * /** 1884 * * Title: My Pattern 1885 * * Slug: my-theme/my-pattern 1886 * * 1887 * 1888 * The output of the PHP source corresponds to the content of the pattern, e.g.: 1889 * 1890 * <main><p><?php echo "Hello"; ?></p></main> 1891 * 1892 * If applicable, this will collect from both parent and child theme. 1893 * 1894 * Other settable fields include: 1895 * 1896 * - Description 1897 * - Viewport Width 1898 * - Inserter (yes/no) 1899 * - Categories (comma-separated values) 1900 * - Keywords (comma-separated values) 1901 * - Block Types (comma-separated values) 1902 * - Post Types (comma-separated values) 1903 * - Template Types (comma-separated values) 1904 * 1905 * @since 6.4.0 1906 * 1907 * @return array Block pattern data. 1908 */ 1909 public function get_block_patterns() { 1910 $can_use_cached = ! wp_is_development_mode( 'theme' ); 1911 1912 $pattern_data = $this->get_pattern_cache(); 1913 if ( is_array( $pattern_data ) ) { 1914 if ( $can_use_cached ) { 1915 return $pattern_data; 1916 } 1917 // If in development mode, clear pattern cache. 1918 $this->delete_pattern_cache(); 1919 } 1920 1921 $dirpath = $this->get_stylesheet_directory() . '/patterns'; 1922 $pattern_data = array(); 1923 1924 if ( ! file_exists( $dirpath ) ) { 1925 if ( $can_use_cached ) { 1926 $this->set_pattern_cache( $pattern_data ); 1927 } 1928 return $pattern_data; 1929 } 1930 1931 $files = (array) self::scandir( $dirpath, 'php', -1 ); 1932 1933 /** 1934 * Filters list of block pattern files for a theme. 1935 * 1936 * @since 6.8.0 1937 * 1938 * @param array $files Array of theme files found within `patterns` directory. 1939 * @param string $dirpath Path of theme `patterns` directory being scanned. 1940 */ 1941 $files = apply_filters( 'theme_block_pattern_files', $files, $dirpath ); 1942 1943 $dirpath = trailingslashit( $dirpath ); 1944 1945 if ( ! $files ) { 1946 if ( $can_use_cached ) { 1947 $this->set_pattern_cache( $pattern_data ); 1948 } 1949 return $pattern_data; 1950 } 1951 1952 $default_headers = array( 1953 'title' => 'Title', 1954 'slug' => 'Slug', 1955 'description' => 'Description', 1956 'viewportWidth' => 'Viewport Width', 1957 'inserter' => 'Inserter', 1958 'categories' => 'Categories', 1959 'keywords' => 'Keywords', 1960 'blockTypes' => 'Block Types', 1961 'postTypes' => 'Post Types', 1962 'templateTypes' => 'Template Types', 1963 ); 1964 1965 $properties_to_parse = array( 1966 'categories', 1967 'keywords', 1968 'blockTypes', 1969 'postTypes', 1970 'templateTypes', 1971 ); 1972 1973 foreach ( $files as $file ) { 1974 $pattern = get_file_data( $file, $default_headers ); 1975 1976 if ( empty( $pattern['slug'] ) ) { 1977 _doing_it_wrong( 1978 __FUNCTION__, 1979 sprintf( 1980 /* translators: 1: file name. */ 1981 __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ), 1982 $file 1983 ), 1984 '6.0.0' 1985 ); 1986 continue; 1987 } 1988 1989 if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) { 1990 _doing_it_wrong( 1991 __FUNCTION__, 1992 sprintf( 1993 /* translators: 1: file name; 2: slug value found. */ 1994 __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ), 1995 $file, 1996 $pattern['slug'] 1997 ), 1998 '6.0.0' 1999 ); 2000 } 2001 2002 // Title is a required property. 2003 if ( ! $pattern['title'] ) { 2004 _doing_it_wrong( 2005 __FUNCTION__, 2006 sprintf( 2007 /* translators: 1: file name. */ 2008 __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ), 2009 $file 2010 ), 2011 '6.0.0' 2012 ); 2013 continue; 2014 } 2015 2016 // For properties of type array, parse data as comma-separated. 2017 foreach ( $properties_to_parse as $property ) { 2018 if ( ! empty( $pattern[ $property ] ) ) { 2019 $pattern[ $property ] = array_filter( wp_parse_list( (string) $pattern[ $property ] ) ); 2020 } else { 2021 unset( $pattern[ $property ] ); 2022 } 2023 } 2024 2025 // Parse properties of type int. 2026 $property = 'viewportWidth'; 2027 if ( ! empty( $pattern[ $property ] ) ) { 2028 $pattern[ $property ] = (int) $pattern[ $property ]; 2029 } else { 2030 unset( $pattern[ $property ] ); 2031 } 2032 2033 // Parse properties of type bool. 2034 $property = 'inserter'; 2035 if ( ! empty( $pattern[ $property ] ) ) { 2036 $pattern[ $property ] = in_array( 2037 strtolower( $pattern[ $property ] ), 2038 array( 'yes', 'true' ), 2039 true 2040 ); 2041 } else { 2042 unset( $pattern[ $property ] ); 2043 } 2044 2045 $key = str_replace( $dirpath, '', $file ); 2046 2047 $pattern_data[ $key ] = $pattern; 2048 } 2049 2050 if ( $can_use_cached ) { 2051 $this->set_pattern_cache( $pattern_data ); 2052 } 2053 2054 return $pattern_data; 2055 } 2056 2057 /** 2058 * Gets block pattern cache. 2059 * 2060 * @since 6.4.0 2061 * @since 6.6.0 Uses transients to cache regardless of site environment. 2062 * 2063 * @return array|false Returns an array of patterns if cache is found, otherwise false. 2064 */ 2065 private function get_pattern_cache() { 2066 if ( ! $this->exists() ) { 2067 return false; 2068 } 2069 2070 $pattern_data = get_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash ); 2071 2072 if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) { 2073 return $pattern_data['patterns']; 2074 } 2075 return false; 2076 } 2077 2078 /** 2079 * Sets block pattern cache. 2080 * 2081 * @since 6.4.0 2082 * @since 6.6.0 Uses transients to cache regardless of site environment. 2083 * 2084 * @param array $patterns Block patterns data to set in cache. 2085 */ 2086 private function set_pattern_cache( array $patterns ) { 2087 $pattern_data = array( 2088 'version' => $this->get( 'Version' ), 2089 'patterns' => $patterns, 2090 ); 2091 2092 /** 2093 * Filters the cache expiration time for theme files. 2094 * 2095 * @since 6.6.0 2096 * 2097 * @param int $cache_expiration Cache expiration time in seconds. 2098 * @param string $cache_type Type of cache being set. 2099 */ 2100 $cache_expiration = (int) apply_filters( 'wp_theme_files_cache_ttl', self::$cache_expiration, 'theme_block_patterns' ); 2101 2102 // We don't want to cache patterns infinitely. 2103 if ( $cache_expiration <= 0 ) { 2104 _doing_it_wrong( 2105 __METHOD__, 2106 sprintf( 2107 /* translators: %1$s: The filter name.*/ 2108 __( 'The %1$s filter must return an integer value greater than 0.' ), 2109 '<code>wp_theme_files_cache_ttl</code>' 2110 ), 2111 '6.6.0' 2112 ); 2113 2114 $cache_expiration = self::$cache_expiration; 2115 } 2116 2117 set_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash, $pattern_data, $cache_expiration ); 2118 } 2119 2120 /** 2121 * Clears block pattern cache. 2122 * 2123 * @since 6.4.0 2124 * @since 6.6.0 Uses transients to cache regardless of site environment. 2125 */ 2126 public function delete_pattern_cache() { 2127 delete_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash ); 2128 } 2129 2130 /** 2131 * Enables a theme for all sites on the current network. 2132 * 2133 * @since 4.6.0 2134 * 2135 * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names. 2136 */ 2137 public static function network_enable_theme( $stylesheets ) { 2138 if ( ! is_multisite() ) { 2139 return; 2140 } 2141 2142 if ( ! is_array( $stylesheets ) ) { 2143 $stylesheets = array( $stylesheets ); 2144 } 2145 2146 $allowed_themes = get_site_option( 'allowedthemes' ); 2147 foreach ( $stylesheets as $stylesheet ) { 2148 $allowed_themes[ $stylesheet ] = true; 2149 } 2150 2151 update_site_option( 'allowedthemes', $allowed_themes ); 2152 } 2153 2154 /** 2155 * Disables a theme for all sites on the current network. 2156 * 2157 * @since 4.6.0 2158 * 2159 * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names. 2160 */ 2161 public static function network_disable_theme( $stylesheets ) { 2162 if ( ! is_multisite() ) { 2163 return; 2164 } 2165 2166 if ( ! is_array( $stylesheets ) ) { 2167 $stylesheets = array( $stylesheets ); 2168 } 2169 2170 $allowed_themes = get_site_option( 'allowedthemes' ); 2171 foreach ( $stylesheets as $stylesheet ) { 2172 if ( isset( $allowed_themes[ $stylesheet ] ) ) { 2173 unset( $allowed_themes[ $stylesheet ] ); 2174 } 2175 } 2176 2177 update_site_option( 'allowedthemes', $allowed_themes ); 2178 } 2179 2180 /** 2181 * Sorts themes by name. 2182 * 2183 * @since 3.4.0 2184 * 2185 * @param WP_Theme[] $themes Array of theme objects to sort (passed by reference). 2186 */ 2187 public static function sort_by_name( &$themes ) { 2188 if ( str_starts_with( get_user_locale(), 'en_' ) ) { 2189 uasort( $themes, array( 'WP_Theme', '_name_sort' ) ); 2190 } else { 2191 foreach ( $themes as $key => $theme ) { 2192 $theme->translate_header( 'Name', $theme->headers['Name'] ); 2193 } 2194 uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) ); 2195 } 2196 } 2197 2198 /** 2199 * Callback function for usort() to naturally sort themes by name. 2200 * 2201 * Accesses the Name header directly from the class for maximum speed. 2202 * Would choke on HTML but we don't care enough to slow it down with strip_tags(). 2203 * 2204 * @since 3.4.0 2205 * 2206 * @param WP_Theme $a First theme. 2207 * @param WP_Theme $b Second theme. 2208 * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. 2209 * Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). 2210 */ 2211 private static function _name_sort( $a, $b ) { 2212 return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] ); 2213 } 2214 2215 /** 2216 * Callback function for usort() to naturally sort themes by translated name. 2217 * 2218 * @since 3.4.0 2219 * 2220 * @param WP_Theme $a First theme. 2221 * @param WP_Theme $b Second theme. 2222 * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. 2223 * Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). 2224 */ 2225 private static function _name_sort_i18n( $a, $b ) { 2226 return strnatcasecmp( $a->name_translated, $b->name_translated ); 2227 } 2228 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Nov 29 08:20:01 2026 | Cross-referenced by PHPXref |