PageRenderTime 61ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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