You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1490 lines
46 KiB

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