PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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