Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Theme often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_Theme, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | * @access public |
||
| 16 | * @var bool |
||
| 17 | * |
||
| 18 | * @see WP_MS_Themes_List_Table |
||
| 19 | */ |
||
| 20 | public $update = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Headers for style.css files. |
||
| 24 | * |
||
| 25 | * @static |
||
| 26 | * @access private |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $file_headers = array( |
||
| 30 | 'Name' => 'Theme Name', |
||
| 31 | 'ThemeURI' => 'Theme URI', |
||
| 32 | 'Description' => 'Description', |
||
| 33 | 'Author' => 'Author', |
||
| 34 | 'AuthorURI' => 'Author URI', |
||
| 35 | 'Version' => 'Version', |
||
| 36 | 'Template' => 'Template', |
||
| 37 | 'Status' => 'Status', |
||
| 38 | 'Tags' => 'Tags', |
||
| 39 | 'TextDomain' => 'Text Domain', |
||
| 40 | 'DomainPath' => 'Domain Path', |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Default themes. |
||
| 45 | * |
||
| 46 | * @static |
||
| 47 | * @access private |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $default_themes = array( |
||
| 51 | 'classic' => 'WordPress Classic', |
||
| 52 | 'default' => 'WordPress Default', |
||
| 53 | 'twentyten' => 'Twenty Ten', |
||
| 54 | 'twentyeleven' => 'Twenty Eleven', |
||
| 55 | 'twentytwelve' => 'Twenty Twelve', |
||
| 56 | 'twentythirteen' => 'Twenty Thirteen', |
||
| 57 | 'twentyfourteen' => 'Twenty Fourteen', |
||
| 58 | 'twentyfifteen' => 'Twenty Fifteen', |
||
| 59 | 'twentysixteen' => 'Twenty Sixteen', |
||
| 60 | 'twentyseventeen' => 'Twenty Seventeen', |
||
| 61 | ); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Renamed theme tags. |
||
| 65 | * |
||
| 66 | * @static |
||
| 67 | * @access private |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private static $tag_map = array( |
||
| 71 | 'fixed-width' => 'fixed-layout', |
||
| 72 | 'flexible-width' => 'fluid-layout', |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Absolute path to the theme root, usually wp-content/themes |
||
| 77 | * |
||
| 78 | * @access private |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $theme_root; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Header data from the theme's style.css file. |
||
| 85 | * |
||
| 86 | * @access private |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $headers = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Header data from the theme's style.css file after being sanitized. |
||
| 93 | * |
||
| 94 | * @access private |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private $headers_sanitized; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Header name from the theme's style.css after being translated. |
||
| 101 | * |
||
| 102 | * Cached due to sorting functions running over the translated name. |
||
| 103 | * |
||
| 104 | * @access private |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $name_translated; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Errors encountered when initializing the theme. |
||
| 111 | * |
||
| 112 | * @access private |
||
| 113 | * @var WP_Error |
||
| 114 | */ |
||
| 115 | private $errors; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The directory name of the theme's files, inside the theme root. |
||
| 119 | * |
||
| 120 | * In the case of a child theme, this is directory name of the child theme. |
||
| 121 | * Otherwise, 'stylesheet' is the same as 'template'. |
||
| 122 | * |
||
| 123 | * @access private |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private $stylesheet; |
||
| 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 the directory name of the parent theme. |
||
| 132 | * Otherwise, 'template' is the same as 'stylesheet'. |
||
| 133 | * |
||
| 134 | * @access private |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | private $template; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * A reference to the parent theme, in the case of a child theme. |
||
| 141 | * |
||
| 142 | * @access private |
||
| 143 | * @var WP_Theme |
||
| 144 | */ |
||
| 145 | private $parent; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * URL to the theme root, usually an absolute URL to wp-content/themes |
||
| 149 | * |
||
| 150 | * @access private |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | private $theme_root_uri; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Flag for whether the theme's textdomain is loaded. |
||
| 157 | * |
||
| 158 | * @access private |
||
| 159 | * @var bool |
||
| 160 | */ |
||
| 161 | private $textdomain_loaded; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Stores an md5 hash of the theme root, to function as the cache key. |
||
| 165 | * |
||
| 166 | * @access private |
||
| 167 | * @var string |
||
| 168 | */ |
||
| 169 | private $cache_hash; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Flag for whether the themes cache bucket should be persistently cached. |
||
| 173 | * |
||
| 174 | * Default is false. Can be set with the {@see 'wp_cache_themes_persistently'} filter. |
||
| 175 | * |
||
| 176 | * @static |
||
| 177 | * @access private |
||
| 178 | * @var bool |
||
| 179 | */ |
||
| 180 | private static $persistently_cache; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Expiration time for the themes cache bucket. |
||
| 184 | * |
||
| 185 | * By default the bucket is not cached, so this value is useless. |
||
| 186 | * |
||
| 187 | * @static |
||
| 188 | * @access private |
||
| 189 | * @var bool |
||
| 190 | */ |
||
| 191 | private static $cache_expiration = 1800; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Constructor for WP_Theme. |
||
| 195 | * |
||
| 196 | * @since 3.4.0 |
||
| 197 | * |
||
| 198 | * @global array $wp_theme_directories |
||
| 199 | * |
||
| 200 | * @param string $theme_dir Directory of the theme within the theme_root. |
||
| 201 | * @param string $theme_root Theme root. |
||
| 202 | * @param WP_Error|void $_child If this theme is a parent theme, the child may be passed for validation purposes. |
||
| 203 | */ |
||
| 204 | public function __construct( $theme_dir, $theme_root, $_child = null ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * When converting the object to a string, the theme name is returned. |
||
| 337 | * |
||
| 338 | * @since 3.4.0 |
||
| 339 | * |
||
| 340 | * @return string Theme name, ready for display (translated) |
||
| 341 | */ |
||
| 342 | public function __toString() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * __isset() magic method for properties formerly returned by current_theme_info() |
||
| 348 | * |
||
| 349 | * @staticvar array $properties |
||
| 350 | * |
||
| 351 | * @since 3.4.0 |
||
| 352 | * |
||
| 353 | * @param string $offset Property to check if set. |
||
| 354 | * @return bool Whether the given property is set. |
||
| 355 | */ |
||
| 356 | public function __isset( $offset ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * __get() magic method for properties formerly returned by current_theme_info() |
||
| 367 | * |
||
| 368 | * @since 3.4.0 |
||
| 369 | * |
||
| 370 | * @param string $offset Property to get. |
||
| 371 | * @return mixed Property value. |
||
| 372 | */ |
||
| 373 | public function __get( $offset ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Method to implement ArrayAccess for keys formerly returned by get_themes() |
||
| 411 | * |
||
| 412 | * @since 3.4.0 |
||
| 413 | * |
||
| 414 | * @param mixed $offset |
||
| 415 | * @param mixed $value |
||
| 416 | */ |
||
| 417 | public function offsetSet( $offset, $value ) {} |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Method to implement ArrayAccess for keys formerly returned by get_themes() |
||
| 421 | * |
||
| 422 | * @since 3.4.0 |
||
| 423 | * |
||
| 424 | * @param mixed $offset |
||
| 425 | */ |
||
| 426 | public function offsetUnset( $offset ) {} |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Method to implement ArrayAccess for keys formerly returned by get_themes() |
||
| 430 | * |
||
| 431 | * @staticvar array $keys |
||
| 432 | * |
||
| 433 | * @since 3.4.0 |
||
| 434 | * |
||
| 435 | * @param mixed $offset |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function offsetExists( $offset ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Method to implement ArrayAccess for keys formerly returned by get_themes(). |
||
| 450 | * |
||
| 451 | * Author, Author Name, Author URI, and Description did not previously return |
||
| 452 | * translated data. We are doing so now as it is safe to do. However, as |
||
| 453 | * Name and Title could have been used as the key for get_themes(), both remain |
||
| 454 | * untranslated for back compatibility. This means that ['Name'] is not ideal, |
||
| 455 | * and care should be taken to use `$theme::display( 'Name' )` to get a properly |
||
| 456 | * translated header. |
||
| 457 | * |
||
| 458 | * @since 3.4.0 |
||
| 459 | * |
||
| 460 | * @param mixed $offset |
||
| 461 | * @return mixed |
||
| 462 | */ |
||
| 463 | public function offsetGet( $offset ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns errors property. |
||
| 512 | * |
||
| 513 | * @since 3.4.0 |
||
| 514 | * @access public |
||
| 515 | * |
||
| 516 | * @return WP_Error|false WP_Error if there are errors, or false. |
||
| 517 | */ |
||
| 518 | public function errors() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Whether the theme exists. |
||
| 524 | * |
||
| 525 | * A theme with errors exists. A theme with the error of 'theme_not_found', |
||
| 526 | * meaning that the theme's directory was not found, does not exist. |
||
| 527 | * |
||
| 528 | * @since 3.4.0 |
||
| 529 | * @access public |
||
| 530 | * |
||
| 531 | * @return bool Whether the theme exists. |
||
| 532 | */ |
||
| 533 | public function exists() { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns reference to the parent theme. |
||
| 539 | * |
||
| 540 | * @since 3.4.0 |
||
| 541 | * @access public |
||
| 542 | * |
||
| 543 | * @return WP_Theme|false Parent theme, or false if the current theme is not a child theme. |
||
| 544 | */ |
||
| 545 | public function parent() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Adds theme data to cache. |
||
| 551 | * |
||
| 552 | * Cache entries keyed by the theme and the type of data. |
||
| 553 | * |
||
| 554 | * @since 3.4.0 |
||
| 555 | * @access private |
||
| 556 | * |
||
| 557 | * @param string $key Type of data to store (theme, screenshot, headers, post_templates) |
||
| 558 | * @param string $data Data to store |
||
| 559 | * @return bool Return value from wp_cache_add() |
||
| 560 | */ |
||
| 561 | private function cache_add( $key, $data ) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Gets theme data from cache. |
||
| 567 | * |
||
| 568 | * Cache entries are keyed by the theme and the type of data. |
||
| 569 | * |
||
| 570 | * @since 3.4.0 |
||
| 571 | * @access private |
||
| 572 | * |
||
| 573 | * @param string $key Type of data to retrieve (theme, screenshot, headers, post_templates) |
||
| 574 | * @return mixed Retrieved data |
||
| 575 | */ |
||
| 576 | private function cache_get( $key ) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Clears the cache for the theme. |
||
| 582 | * |
||
| 583 | * @since 3.4.0 |
||
| 584 | * @access public |
||
| 585 | */ |
||
| 586 | public function cache_delete() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Get a raw, unformatted theme header. |
||
| 596 | * |
||
| 597 | * The header is sanitized, but is not translated, and is not marked up for display. |
||
| 598 | * To get a theme header for display, use the display() method. |
||
| 599 | * |
||
| 600 | * Use the get_template() method, not the 'Template' header, for finding the template. |
||
| 601 | * The 'Template' header is only good for what was written in the style.css, while |
||
| 602 | * get_template() takes into account where WordPress actually located the theme and |
||
| 603 | * whether it is actually valid. |
||
| 604 | * |
||
| 605 | * @since 3.4.0 |
||
| 606 | * @access public |
||
| 607 | * |
||
| 608 | * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
||
| 609 | * @return string|false String on success, false on failure. |
||
| 610 | */ |
||
| 611 | public function get( $header ) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Gets a theme header, formatted and translated for display. |
||
| 638 | * |
||
| 639 | * @since 3.4.0 |
||
| 640 | * @access public |
||
| 641 | * |
||
| 642 | * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
||
| 643 | * @param bool $markup Optional. Whether to mark up the header. Defaults to true. |
||
| 644 | * @param bool $translate Optional. Whether to translate the header. Defaults to true. |
||
| 645 | * @return string|false Processed header, false on failure. |
||
| 646 | */ |
||
| 647 | public function display( $header, $markup = true, $translate = true ) { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Sanitize a theme header. |
||
| 667 | * |
||
| 668 | * @since 3.4.0 |
||
| 669 | * @access private |
||
| 670 | * |
||
| 671 | * @staticvar array $header_tags |
||
| 672 | * @staticvar array $header_tags_with_a |
||
| 673 | * |
||
| 674 | * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
||
| 675 | * @param string $value Value to sanitize. |
||
| 676 | * @return mixed |
||
| 677 | */ |
||
| 678 | private function sanitize_header( $header, $value ) { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Mark up a theme header. |
||
| 726 | * |
||
| 727 | * @since 3.4.0 |
||
| 728 | * @access private |
||
| 729 | * |
||
| 730 | * @staticvar string $comma |
||
| 731 | * |
||
| 732 | * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
||
| 733 | * @param string $value Value to mark up. |
||
| 734 | * @param string $translate Whether the header has been translated. |
||
| 735 | * @return string Value, marked up. |
||
| 736 | */ |
||
| 737 | private function markup_header( $header, $value, $translate ) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Translate a theme header. |
||
| 773 | * |
||
| 774 | * @since 3.4.0 |
||
| 775 | * @access private |
||
| 776 | * |
||
| 777 | * @staticvar array $tags_list |
||
| 778 | * |
||
| 779 | * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
||
| 780 | * @param string $value Value to translate. |
||
| 781 | * @return string Translated value. |
||
| 782 | */ |
||
| 783 | private function translate_header( $header, $value ) { |
||
| 831 | |||
| 832 | /** |
||
| 833 | * The directory name of the theme's "stylesheet" files, inside the theme root. |
||
| 834 | * |
||
| 835 | * In the case of a child theme, this is directory name of the child theme. |
||
| 836 | * Otherwise, get_stylesheet() is the same as get_template(). |
||
| 837 | * |
||
| 838 | * @since 3.4.0 |
||
| 839 | * @access public |
||
| 840 | * |
||
| 841 | * @return string Stylesheet |
||
| 842 | */ |
||
| 843 | public function get_stylesheet() { |
||
| 846 | |||
| 847 | /** |
||
| 848 | * The directory name of the theme's "template" files, inside the theme root. |
||
| 849 | * |
||
| 850 | * In the case of a child theme, this is the directory name of the parent theme. |
||
| 851 | * Otherwise, the get_template() is the same as get_stylesheet(). |
||
| 852 | * |
||
| 853 | * @since 3.4.0 |
||
| 854 | * @access public |
||
| 855 | * |
||
| 856 | * @return string Template |
||
| 857 | */ |
||
| 858 | public function get_template() { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Returns the absolute path to the directory of a theme's "stylesheet" files. |
||
| 864 | * |
||
| 865 | * In the case of a child theme, this is the absolute path to the directory |
||
| 866 | * of the child theme's files. |
||
| 867 | * |
||
| 868 | * @since 3.4.0 |
||
| 869 | * @access public |
||
| 870 | * |
||
| 871 | * @return string Absolute path of the stylesheet directory. |
||
| 872 | */ |
||
| 873 | public function get_stylesheet_directory() { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Returns the absolute path to the directory of a theme's "template" files. |
||
| 882 | * |
||
| 883 | * In the case of a child theme, this is the absolute path to the directory |
||
| 884 | * of the parent theme's files. |
||
| 885 | * |
||
| 886 | * @since 3.4.0 |
||
| 887 | * @access public |
||
| 888 | * |
||
| 889 | * @return string Absolute path of the template directory. |
||
| 890 | */ |
||
| 891 | public function get_template_directory() { |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Returns the URL to the directory of a theme's "stylesheet" files. |
||
| 902 | * |
||
| 903 | * In the case of a child theme, this is the URL to the directory of the |
||
| 904 | * child theme's files. |
||
| 905 | * |
||
| 906 | * @since 3.4.0 |
||
| 907 | * @access public |
||
| 908 | * |
||
| 909 | * @return string URL to the stylesheet directory. |
||
| 910 | */ |
||
| 911 | public function get_stylesheet_directory_uri() { |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Returns the URL to the directory of a theme's "template" files. |
||
| 917 | * |
||
| 918 | * In the case of a child theme, this is the URL to the directory of the |
||
| 919 | * parent theme's files. |
||
| 920 | * |
||
| 921 | * @since 3.4.0 |
||
| 922 | * @access public |
||
| 923 | * |
||
| 924 | * @return string URL to the template directory. |
||
| 925 | */ |
||
| 926 | public function get_template_directory_uri() { |
||
| 934 | |||
| 935 | /** |
||
| 936 | * The absolute path to the directory of the theme root. |
||
| 937 | * |
||
| 938 | * This is typically the absolute path to wp-content/themes. |
||
| 939 | * |
||
| 940 | * @since 3.4.0 |
||
| 941 | * @access public |
||
| 942 | * |
||
| 943 | * @return string Theme root. |
||
| 944 | */ |
||
| 945 | public function get_theme_root() { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Returns the URL to the directory of the theme root. |
||
| 951 | * |
||
| 952 | * This is typically the absolute URL to wp-content/themes. This forms the basis |
||
| 953 | * for all other URLs returned by WP_Theme, so we pass it to the public function |
||
| 954 | * get_theme_root_uri() and allow it to run the {@see 'theme_root_uri'} filter. |
||
| 955 | * |
||
| 956 | * @since 3.4.0 |
||
| 957 | * @access public |
||
| 958 | * |
||
| 959 | * @return string Theme root URI. |
||
| 960 | */ |
||
| 961 | public function get_theme_root_uri() { |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Returns the main screenshot file for the theme. |
||
| 969 | * |
||
| 970 | * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed. |
||
| 971 | * |
||
| 972 | * Screenshots for a theme must be in the stylesheet directory. (In the case of child |
||
| 973 | * themes, parent theme screenshots are not inherited.) |
||
| 974 | * |
||
| 975 | * @since 3.4.0 |
||
| 976 | * @access public |
||
| 977 | * |
||
| 978 | * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI. |
||
| 979 | * @return string|false Screenshot file. False if the theme does not have a screenshot. |
||
| 980 | */ |
||
| 981 | public function get_screenshot( $uri = 'uri' ) { |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Return files in the theme's directory. |
||
| 1006 | * |
||
| 1007 | * @since 3.4.0 |
||
| 1008 | * @access public |
||
| 1009 | * |
||
| 1010 | * @param mixed $type Optional. Array of extensions to return. Defaults to all files (null). |
||
| 1011 | * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite. |
||
| 1012 | * @param bool $search_parent Optional. Whether to return parent files. Defaults to false. |
||
| 1013 | * @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values |
||
| 1014 | * being absolute paths. |
||
| 1015 | */ |
||
| 1016 | public function get_files( $type = null, $depth = 0, $search_parent = false ) { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Returns the theme's post templates. |
||
| 1027 | * |
||
| 1028 | * @since 4.7.0 |
||
| 1029 | * @access public |
||
| 1030 | * |
||
| 1031 | * @return array Array of page templates, keyed by filename and post type, |
||
| 1032 | * with the value of the translated header name. |
||
| 1033 | */ |
||
| 1034 | public function get_post_templates() { |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Returns the theme's post templates for a given post type. |
||
| 1083 | * |
||
| 1084 | * @since 3.4.0 |
||
| 1085 | * @since 4.7.0 Added the `$post_type` parameter. |
||
| 1086 | * @access public |
||
| 1087 | * |
||
| 1088 | * @param WP_Post|null $post Optional. The post being edited, provided for context. |
||
| 1089 | * @param string $post_type Optional. Post type to get the templates for. Default 'page'. |
||
| 1090 | * If a post is provided, its post type is used. |
||
| 1091 | * @return array Array of page templates, keyed by filename, with the value of the translated header name. |
||
| 1092 | */ |
||
| 1093 | public function get_page_templates( $post = null, $post_type = 'page' ) { |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Scans a directory for files of a certain extension. |
||
| 1125 | * |
||
| 1126 | * @since 3.4.0 |
||
| 1127 | * |
||
| 1128 | * @static |
||
| 1129 | * @access private |
||
| 1130 | * |
||
| 1131 | * @param string $path Absolute path to search. |
||
| 1132 | * @param array|string|null $extensions Optional. Array of extensions to find, string of a single extension, |
||
| 1133 | * or null for all extensions. Default null. |
||
| 1134 | * @param int $depth Optional. How many levels deep to search for files. Accepts 0, 1+, or |
||
| 1135 | * -1 (infinite depth). Default 0. |
||
| 1136 | * @param string $relative_path Optional. The basename of the absolute path. Used to control the |
||
| 1137 | * returned path for the found files, particularly when this function |
||
| 1138 | * recurses to lower depths. Default empty. |
||
| 1139 | * @return array|false Array of files, keyed by the path to the file relative to the `$path` directory prepended |
||
| 1140 | * with `$relative_path`, with the values being absolute paths. False otherwise. |
||
| 1141 | */ |
||
| 1142 | private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) { |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Loads the theme's textdomain. |
||
| 1186 | * |
||
| 1187 | * Translation files are not inherited from the parent theme. Todo: if this fails for the |
||
| 1188 | * child theme, it should probably try to load the parent theme's translations. |
||
| 1189 | * |
||
| 1190 | * @since 3.4.0 |
||
| 1191 | * @access public |
||
| 1192 | * |
||
| 1193 | * @return bool True if the textdomain was successfully loaded or has already been loaded. |
||
| 1194 | * False if no textdomain was specified in the file headers, or if the domain could not be loaded. |
||
| 1195 | */ |
||
| 1196 | public function load_textdomain() { |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Whether the theme is allowed (multisite only). |
||
| 1223 | * |
||
| 1224 | * @since 3.4.0 |
||
| 1225 | * @access public |
||
| 1226 | * |
||
| 1227 | * @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site' |
||
| 1228 | * settings, or 'both'. Defaults to 'both'. |
||
| 1229 | * @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current site. |
||
| 1230 | * @return bool Whether the theme is allowed for the network. Returns true in single-site. |
||
| 1231 | */ |
||
| 1232 | public function is_allowed( $check = 'both', $blog_id = null ) { |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Determines the latest WordPress default theme that is installed. |
||
| 1253 | * |
||
| 1254 | * This hits the filesystem. |
||
| 1255 | * |
||
| 1256 | * @since 4.4.0 |
||
| 1257 | * |
||
| 1258 | * @return WP_Theme|false Object, or false if no theme is installed, which would be bad. |
||
| 1259 | */ |
||
| 1260 | public static function get_core_default_theme() { |
||
| 1261 | foreach ( array_reverse( self::$default_themes ) as $slug => $name ) { |
||
| 1262 | $theme = wp_get_theme( $slug ); |
||
| 1263 | if ( $theme->exists() ) { |
||
| 1264 | return $theme; |
||
| 1265 | } |
||
| 1266 | } |
||
| 1267 | return false; |
||
| 1268 | } |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Returns array of stylesheet names of themes allowed on the site or network. |
||
| 1272 | * |
||
| 1273 | * @since 3.4.0 |
||
| 1274 | * |
||
| 1275 | * @static |
||
| 1276 | * @access public |
||
| 1277 | * |
||
| 1278 | * @param int $blog_id Optional. ID of the site. Defaults to the current site. |
||
| 1279 | * @return array Array of stylesheet names. |
||
| 1280 | */ |
||
| 1281 | public static function get_allowed( $blog_id = null ) { |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Returns array of stylesheet names of themes allowed on the network. |
||
| 1299 | * |
||
| 1300 | * @since 3.4.0 |
||
| 1301 | * |
||
| 1302 | * @static |
||
| 1303 | * @access public |
||
| 1304 | * |
||
| 1305 | * @staticvar array $allowed_themes |
||
| 1306 | * |
||
| 1307 | * @return array Array of stylesheet names. |
||
| 1308 | */ |
||
| 1309 | public static function get_allowed_on_network() { |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Returns array of stylesheet names of themes allowed on the site. |
||
| 1329 | * |
||
| 1330 | * @since 3.4.0 |
||
| 1331 | * |
||
| 1332 | * @static |
||
| 1333 | * @access public |
||
| 1334 | * |
||
| 1335 | * @staticvar array $allowed_themes |
||
| 1336 | * |
||
| 1337 | * @param int $blog_id Optional. ID of the site. Defaults to the current site. |
||
| 1338 | * @return array Array of stylesheet names. |
||
| 1339 | */ |
||
| 1340 | public static function get_allowed_on_site( $blog_id = null ) { |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Enables a theme for all sites on the current network. |
||
| 1410 | * |
||
| 1411 | * @since 4.6.0 |
||
| 1412 | * @access public |
||
| 1413 | * @static |
||
| 1414 | * |
||
| 1415 | * @param string|array $stylesheets Stylesheet name or array of stylesheet names. |
||
| 1416 | */ |
||
| 1417 | public static function network_enable_theme( $stylesheets ) { |
||
| 1418 | if ( ! is_multisite() ) { |
||
| 1419 | return; |
||
| 1420 | } |
||
| 1421 | |||
| 1422 | if ( ! is_array( $stylesheets ) ) { |
||
| 1423 | $stylesheets = array( $stylesheets ); |
||
| 1424 | } |
||
| 1425 | |||
| 1426 | $allowed_themes = get_site_option( 'allowedthemes' ); |
||
| 1427 | foreach ( $stylesheets as $stylesheet ) { |
||
| 1428 | $allowed_themes[ $stylesheet ] = true; |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | update_site_option( 'allowedthemes', $allowed_themes ); |
||
| 1432 | } |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * Disables a theme for all sites on the current network. |
||
| 1436 | * |
||
| 1437 | * @since 4.6.0 |
||
| 1438 | * @access public |
||
| 1439 | * @static |
||
| 1440 | * |
||
| 1441 | * @param string|array $stylesheets Stylesheet name or array of stylesheet names. |
||
| 1442 | */ |
||
| 1443 | public static function network_disable_theme( $stylesheets ) { |
||
| 1444 | if ( ! is_multisite() ) { |
||
| 1445 | return; |
||
| 1446 | } |
||
| 1447 | |||
| 1448 | if ( ! is_array( $stylesheets ) ) { |
||
| 1449 | $stylesheets = array( $stylesheets ); |
||
| 1450 | } |
||
| 1451 | |||
| 1452 | $allowed_themes = get_site_option( 'allowedthemes' ); |
||
| 1453 | foreach ( $stylesheets as $stylesheet ) { |
||
| 1454 | if ( isset( $allowed_themes[ $stylesheet ] ) ) { |
||
| 1455 | unset( $allowed_themes[ $stylesheet ] ); |
||
| 1456 | } |
||
| 1457 | } |
||
| 1458 | |||
| 1459 | update_site_option( 'allowedthemes', $allowed_themes ); |
||
| 1460 | } |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Sorts themes by name. |
||
| 1464 | * |
||
| 1465 | * @since 3.4.0 |
||
| 1466 | * |
||
| 1467 | * @static |
||
| 1468 | * @access public |
||
| 1469 | * |
||
| 1470 | * @param array $themes Array of themes to sort, passed by reference. |
||
| 1471 | */ |
||
| 1472 | public static function sort_by_name( &$themes ) { |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Callback function for usort() to naturally sort themes by name. |
||
| 1482 | * |
||
| 1483 | * Accesses the Name header directly from the class for maximum speed. |
||
| 1484 | * Would choke on HTML but we don't care enough to slow it down with strip_tags(). |
||
| 1485 | * |
||
| 1486 | * @since 3.4.0 |
||
| 1487 | * |
||
| 1488 | * @static |
||
| 1489 | * @access private |
||
| 1490 | * |
||
| 1491 | * @param string $a First name. |
||
| 1492 | * @param string $b Second name. |
||
| 1493 | * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. |
||
| 1494 | * Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). |
||
| 1495 | */ |
||
| 1496 | private static function _name_sort( $a, $b ) { |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Name sort (with translation). |
||
| 1502 | * |
||
| 1503 | * @since 3.4.0 |
||
| 1504 | * |
||
| 1505 | * @static |
||
| 1506 | * @access private |
||
| 1507 | * |
||
| 1508 | * @param string $a First name. |
||
| 1509 | * @param string $b Second name. |
||
| 1510 | * @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. |
||
| 1511 | * Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). |
||
| 1512 | */ |
||
| 1513 | private static function _name_sort_i18n( $a, $b ) { |
||
| 1517 | } |
||
| 1518 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.