PageRenderTime 86ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/class-wp-theme.php

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