WP_Theme

WP_Theme Class.

Defined (1)

The class is defined in the following location(s).

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