PageRenderTime 93ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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