PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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