PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

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