PageRenderTime 72ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/theme.php

https://gitlab.com/Blueprint-Marketing/WordPress-1
PHP | 1635 lines | 797 code | 202 blank | 636 comment | 201 complexity | 02da51e1a15916646943461a3d14f134 MD5 | raw file
  1. <?php
  2. /**
  3. * Theme, template, and stylesheet functions.
  4. *
  5. * @package WordPress
  6. * @subpackage Theme
  7. */
  8. /**
  9. * Returns an array of WP_Theme objects based on the arguments.
  10. *
  11. * Despite advances over get_themes(), this function is quite expensive, and grows
  12. * linearly with additional themes. Stick to wp_get_theme() if possible.
  13. *
  14. * @since 3.4.0
  15. *
  16. * @param array $args The search arguments. Optional.
  17. * - errors mixed True to return themes with errors, false to return themes without errors, null
  18. * to return all themes. Defaults to false.
  19. * - allowed mixed (Multisite) True to return only allowed themes for a site. False to return only
  20. * disallowed themes for a site. 'site' to return only site-allowed themes. 'network'
  21. * to return only network-allowed themes. Null to return all themes. Defaults to null.
  22. * - blog_id int (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0,
  23. * synonymous for the current blog.
  24. * @return Array of WP_Theme objects.
  25. */
  26. function wp_get_themes( $args = array() ) {
  27. global $wp_theme_directories;
  28. $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
  29. $args = wp_parse_args( $args, $defaults );
  30. $theme_directories = search_theme_directories();
  31. if ( count( $wp_theme_directories ) > 1 ) {
  32. // Make sure the current theme wins out, in case search_theme_directories() picks the wrong
  33. // one in the case of a conflict. (Normally, last registered theme root wins.)
  34. $current_theme = get_stylesheet();
  35. if ( isset( $theme_directories[ $current_theme ] ) ) {
  36. $root_of_current_theme = get_raw_theme_root( $current_theme );
  37. if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) )
  38. $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
  39. $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
  40. }
  41. }
  42. if ( empty( $theme_directories ) )
  43. return array();
  44. if ( is_multisite() && null !== $args['allowed'] ) {
  45. $allowed = $args['allowed'];
  46. if ( 'network' === $allowed )
  47. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
  48. elseif ( 'site' === $allowed )
  49. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
  50. elseif ( $allowed )
  51. $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
  52. else
  53. $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
  54. }
  55. $themes = array();
  56. static $_themes = array();
  57. foreach ( $theme_directories as $theme => $theme_root ) {
  58. if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) )
  59. $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
  60. else
  61. $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
  62. }
  63. if ( null !== $args['errors'] ) {
  64. foreach ( $themes as $theme => $wp_theme ) {
  65. if ( $wp_theme->errors() != $args['errors'] )
  66. unset( $themes[ $theme ] );
  67. }
  68. }
  69. return $themes;
  70. }
  71. /**
  72. * Gets a WP_Theme object for a theme.
  73. *
  74. * @since 3.4.0
  75. *
  76. * @param string $stylesheet Directory name for the theme. Optional. Defaults to current theme.
  77. * @param string $theme_root Absolute path of the theme root to look in. Optional. If not specified, get_raw_theme_root()
  78. * is used to calculate the theme root for the $stylesheet provided (or current theme).
  79. * @return WP_Theme Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.
  80. */
  81. function wp_get_theme( $stylesheet = null, $theme_root = null ) {
  82. global $wp_theme_directories;
  83. if ( empty( $stylesheet ) )
  84. $stylesheet = get_stylesheet();
  85. if ( empty( $theme_root ) ) {
  86. $theme_root = get_raw_theme_root( $stylesheet );
  87. if ( false === $theme_root )
  88. $theme_root = WP_CONTENT_DIR . '/themes';
  89. elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
  90. $theme_root = WP_CONTENT_DIR . $theme_root;
  91. }
  92. return new WP_Theme( $stylesheet, $theme_root );
  93. }
  94. /**
  95. * Clears the cache held by get_theme_roots() and WP_Theme.
  96. *
  97. * @since 3.5.0
  98. * @param bool $clear_update_cache Whether to clear the Theme updates cache
  99. */
  100. function wp_clean_themes_cache( $clear_update_cache = true ) {
  101. if ( $clear_update_cache )
  102. delete_site_transient( 'update_themes' );
  103. search_theme_directories( true );
  104. foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme )
  105. $theme->cache_delete();
  106. }
  107. /**
  108. * Whether a child theme is in use.
  109. *
  110. * @since 3.0.0
  111. *
  112. * @return bool true if a child theme is in use, false otherwise.
  113. **/
  114. function is_child_theme() {
  115. return ( TEMPLATEPATH !== STYLESHEETPATH );
  116. }
  117. /**
  118. * Retrieve name of the current stylesheet.
  119. *
  120. * The theme name that the administrator has currently set the front end theme
  121. * as.
  122. *
  123. * For all extensive purposes, the template name and the stylesheet name are
  124. * going to be the same for most cases.
  125. *
  126. * @since 1.5.0
  127. *
  128. * @return string Stylesheet name.
  129. */
  130. function get_stylesheet() {
  131. /**
  132. * Filter the name of current stylesheet.
  133. *
  134. * @since 1.5.0
  135. *
  136. * @param string $stylesheet Name of the current stylesheet.
  137. */
  138. return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
  139. }
  140. /**
  141. * Retrieve stylesheet directory path for current theme.
  142. *
  143. * @since 1.5.0
  144. *
  145. * @return string Path to current theme directory.
  146. */
  147. function get_stylesheet_directory() {
  148. $stylesheet = get_stylesheet();
  149. $theme_root = get_theme_root( $stylesheet );
  150. $stylesheet_dir = "$theme_root/$stylesheet";
  151. /**
  152. * Filter the stylesheet directory path for current theme.
  153. *
  154. * @since 1.5.0
  155. *
  156. * @param string $stylesheet_dir Absolute path to the current them.
  157. * @param string $stylesheet Directory name of the current theme.
  158. * @param string $theme_root Absolute path to themes directory.
  159. */
  160. return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
  161. }
  162. /**
  163. * Retrieve stylesheet directory URI.
  164. *
  165. * @since 1.5.0
  166. *
  167. * @return string
  168. */
  169. function get_stylesheet_directory_uri() {
  170. $stylesheet = get_stylesheet();
  171. $theme_root_uri = get_theme_root_uri( $stylesheet );
  172. $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
  173. /**
  174. * Filter the stylesheet directory URI.
  175. *
  176. * @since 1.5.0
  177. *
  178. * @param string $stylesheet_dir_uri Stylesheet directory URI.
  179. * @param string $stylesheet Name of the activated theme's directory.
  180. * @param string $theme_root_uri Themes root URI.
  181. */
  182. return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
  183. }
  184. /**
  185. * Retrieve URI of current theme stylesheet.
  186. *
  187. * The stylesheet file name is 'style.css' which is appended to {@link
  188. * get_stylesheet_directory_uri() stylesheet directory URI} path.
  189. *
  190. * @since 1.5.0
  191. *
  192. * @return string
  193. */
  194. function get_stylesheet_uri() {
  195. $stylesheet_dir_uri = get_stylesheet_directory_uri();
  196. $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
  197. /**
  198. * Filter the URI of the current theme stylesheet.
  199. *
  200. * @since 1.5.0
  201. *
  202. * @param string $stylesheet_uri Stylesheet URI for the current theme/child theme.
  203. * @param string $stylesheet_dir_uri Stylesheet directory URI for the current theme/child theme.
  204. */
  205. return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
  206. }
  207. /**
  208. * Retrieve localized stylesheet URI.
  209. *
  210. * The stylesheet directory for the localized stylesheet files are located, by
  211. * default, in the base theme directory. The name of the locale file will be the
  212. * locale followed by '.css'. If that does not exist, then the text direction
  213. * stylesheet will be checked for existence, for example 'ltr.css'.
  214. *
  215. * The theme may change the location of the stylesheet directory by either using
  216. * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
  217. * If you want to change the location of the stylesheet files for the entire
  218. * WordPress workflow, then change the former. If you just have the locale in a
  219. * separate folder, then change the latter.
  220. *
  221. * @since 2.1.0
  222. *
  223. * @return string
  224. */
  225. function get_locale_stylesheet_uri() {
  226. global $wp_locale;
  227. $stylesheet_dir_uri = get_stylesheet_directory_uri();
  228. $dir = get_stylesheet_directory();
  229. $locale = get_locale();
  230. if ( file_exists("$dir/$locale.css") )
  231. $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
  232. elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
  233. $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
  234. else
  235. $stylesheet_uri = '';
  236. /**
  237. * Filter the localized stylesheet URI.
  238. *
  239. * @since 2.1.0
  240. *
  241. * @param string $stylesheet_uri Localized stylesheet URI.
  242. * @param string $stylesheet_dir_uri Stylesheet directory URI.
  243. */
  244. return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
  245. }
  246. /**
  247. * Retrieve name of the current theme.
  248. *
  249. * @since 1.5.0
  250. * @uses apply_filters() Calls 'template' filter on template option.
  251. *
  252. * @return string Template name.
  253. */
  254. function get_template() {
  255. /**
  256. * Filter the name of the current theme.
  257. *
  258. * @since 1.5.0
  259. *
  260. * @param string $template Current theme's directory name.
  261. */
  262. return apply_filters( 'template', get_option( 'template' ) );
  263. }
  264. /**
  265. * Retrieve current theme directory.
  266. *
  267. * @since 1.5.0
  268. *
  269. * @return string Template directory path.
  270. */
  271. function get_template_directory() {
  272. $template = get_template();
  273. $theme_root = get_theme_root( $template );
  274. $template_dir = "$theme_root/$template";
  275. /**
  276. * Filter the current theme directory path.
  277. *
  278. * @since 1.5.0
  279. *
  280. * @param string $template_dir The URI of the current theme directory.
  281. * @param string $template Directory name of the current theme.
  282. * @param string $theme_root Absolute path to the themes directory.
  283. */
  284. return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
  285. }
  286. /**
  287. * Retrieve theme directory URI.
  288. *
  289. * @since 1.5.0
  290. *
  291. * @return string Template directory URI.
  292. */
  293. function get_template_directory_uri() {
  294. $template = get_template();
  295. $theme_root_uri = get_theme_root_uri( $template );
  296. $template_dir_uri = "$theme_root_uri/$template";
  297. /**
  298. * Filter the current theme directory URI.
  299. *
  300. * @since 1.5.0
  301. *
  302. * @param string $template_dir_uri The URI of the current theme directory.
  303. * @param string $template Directory name of the current theme.
  304. * @param string $theme_root_uri The themes root URI.
  305. */
  306. return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
  307. }
  308. /**
  309. * Retrieve theme roots.
  310. *
  311. * @since 2.9.0
  312. *
  313. * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
  314. */
  315. function get_theme_roots() {
  316. global $wp_theme_directories;
  317. if ( count($wp_theme_directories) <= 1 )
  318. return '/themes';
  319. $theme_roots = get_site_transient( 'theme_roots' );
  320. if ( false === $theme_roots ) {
  321. search_theme_directories( true ); // Regenerate the transient.
  322. $theme_roots = get_site_transient( 'theme_roots' );
  323. }
  324. return $theme_roots;
  325. }
  326. /**
  327. * Register a directory that contains themes.
  328. *
  329. * @since 2.9.0
  330. *
  331. * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
  332. * @return bool
  333. */
  334. function register_theme_directory( $directory ) {
  335. global $wp_theme_directories;
  336. if ( ! file_exists( $directory ) ) {
  337. // Try prepending as the theme directory could be relative to the content directory
  338. $directory = WP_CONTENT_DIR . '/' . $directory;
  339. // If this directory does not exist, return and do not register
  340. if ( ! file_exists( $directory ) )
  341. return false;
  342. }
  343. $wp_theme_directories[] = $directory;
  344. return true;
  345. }
  346. /**
  347. * Search all registered theme directories for complete and valid themes.
  348. *
  349. * @since 2.9.0
  350. *
  351. * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
  352. * @return array Valid themes found
  353. */
  354. function search_theme_directories( $force = false ) {
  355. global $wp_theme_directories;
  356. if ( empty( $wp_theme_directories ) )
  357. return false;
  358. static $found_themes;
  359. if ( ! $force && isset( $found_themes ) )
  360. return $found_themes;
  361. $found_themes = array();
  362. $wp_theme_directories = (array) $wp_theme_directories;
  363. // Set up maybe-relative, maybe-absolute array of theme directories.
  364. // We always want to return absolute, but we need to cache relative
  365. // to use in get_theme_root().
  366. foreach ( $wp_theme_directories as $theme_root ) {
  367. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
  368. $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
  369. else
  370. $relative_theme_roots[ $theme_root ] = $theme_root;
  371. }
  372. /**
  373. * Filter whether to get the cache of the registered theme directories.
  374. *
  375. * @since 3.4.0
  376. *
  377. * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false.
  378. * @param string $cache_directory Directory to be searched for the cache.
  379. */
  380. if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
  381. $cached_roots = get_site_transient( 'theme_roots' );
  382. if ( is_array( $cached_roots ) ) {
  383. foreach ( $cached_roots as $theme_dir => $theme_root ) {
  384. // A cached theme root is no longer around, so skip it.
  385. if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
  386. continue;
  387. $found_themes[ $theme_dir ] = array(
  388. 'theme_file' => $theme_dir . '/style.css',
  389. 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
  390. );
  391. }
  392. return $found_themes;
  393. }
  394. if ( ! is_int( $cache_expiration ) )
  395. $cache_expiration = 1800; // half hour
  396. } else {
  397. $cache_expiration = 1800; // half hour
  398. }
  399. /* Loop the registered theme directories and extract all themes */
  400. foreach ( $wp_theme_directories as $theme_root ) {
  401. // Start with directories in the root of the current theme directory.
  402. $dirs = @ scandir( $theme_root );
  403. if ( ! $dirs ) {
  404. trigger_error( "$theme_root is not readable", E_USER_NOTICE );
  405. continue;
  406. }
  407. foreach ( $dirs as $dir ) {
  408. if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
  409. continue;
  410. if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
  411. // wp-content/themes/a-single-theme
  412. // wp-content/themes is $theme_root, a-single-theme is $dir
  413. $found_themes[ $dir ] = array(
  414. 'theme_file' => $dir . '/style.css',
  415. 'theme_root' => $theme_root,
  416. );
  417. } else {
  418. $found_theme = false;
  419. // wp-content/themes/a-folder-of-themes/*
  420. // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
  421. $sub_dirs = @ scandir( $theme_root . '/' . $dir );
  422. if ( ! $sub_dirs ) {
  423. trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
  424. continue;
  425. }
  426. foreach ( $sub_dirs as $sub_dir ) {
  427. if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
  428. continue;
  429. if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
  430. continue;
  431. $found_themes[ $dir . '/' . $sub_dir ] = array(
  432. 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
  433. 'theme_root' => $theme_root,
  434. );
  435. $found_theme = true;
  436. }
  437. // Never mind the above, it's just a theme missing a style.css.
  438. // Return it; WP_Theme will catch the error.
  439. if ( ! $found_theme )
  440. $found_themes[ $dir ] = array(
  441. 'theme_file' => $dir . '/style.css',
  442. 'theme_root' => $theme_root,
  443. );
  444. }
  445. }
  446. }
  447. asort( $found_themes );
  448. $theme_roots = array();
  449. $relative_theme_roots = array_flip( $relative_theme_roots );
  450. foreach ( $found_themes as $theme_dir => $theme_data ) {
  451. $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
  452. }
  453. if ( $theme_roots != get_site_transient( 'theme_roots' ) )
  454. set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
  455. return $found_themes;
  456. }
  457. /**
  458. * Retrieve path to themes directory.
  459. *
  460. * Does not have trailing slash.
  461. *
  462. * @since 1.5.0
  463. *
  464. * @param string $stylesheet_or_template The stylesheet or template name of the theme
  465. * @return string Theme path.
  466. */
  467. function get_theme_root( $stylesheet_or_template = false ) {
  468. global $wp_theme_directories;
  469. if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
  470. // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
  471. // This gives relative theme roots the benefit of the doubt when things go haywire.
  472. if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
  473. $theme_root = WP_CONTENT_DIR . $theme_root;
  474. } else {
  475. $theme_root = WP_CONTENT_DIR . '/themes';
  476. }
  477. /**
  478. * Filter the absolute path to the themes directory.
  479. *
  480. * @since 1.5.0
  481. *
  482. * @param string $theme_root Absolute path to themes directory.
  483. */
  484. return apply_filters( 'theme_root', $theme_root );
  485. }
  486. /**
  487. * Retrieve URI for themes directory.
  488. *
  489. * Does not have trailing slash.
  490. *
  491. * @since 1.5.0
  492. *
  493. * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
  494. * Default is to leverage the main theme root.
  495. * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
  496. * the need for a get_raw_theme_root() call.
  497. * @return string Themes URI.
  498. */
  499. function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
  500. global $wp_theme_directories;
  501. if ( $stylesheet_or_template && ! $theme_root )
  502. $theme_root = get_raw_theme_root( $stylesheet_or_template );
  503. if ( $stylesheet_or_template && $theme_root ) {
  504. if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
  505. // Absolute path. Make an educated guess. YMMV -- but note the filter below.
  506. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
  507. $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
  508. elseif ( 0 === strpos( $theme_root, ABSPATH ) )
  509. $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
  510. elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
  511. $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
  512. else
  513. $theme_root_uri = $theme_root;
  514. } else {
  515. $theme_root_uri = content_url( $theme_root );
  516. }
  517. } else {
  518. $theme_root_uri = content_url( 'themes' );
  519. }
  520. /**
  521. * Filter the URI for themes directory.
  522. *
  523. * @since 1.5.0
  524. *
  525. * @param string $theme_root_uri The URI for themes directory.
  526. * @param string $siteurl WordPress web address which is set in General Options.
  527. * @param string $stylesheet_or_template Stylesheet or template name of the theme.
  528. */
  529. return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
  530. }
  531. /**
  532. * Get the raw theme root relative to the content directory with no filters applied.
  533. *
  534. * @since 3.1.0
  535. *
  536. * @param string $stylesheet_or_template The stylesheet or template name of the theme
  537. * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used.
  538. * @return string Theme root
  539. */
  540. function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
  541. global $wp_theme_directories;
  542. if ( count($wp_theme_directories) <= 1 )
  543. return '/themes';
  544. $theme_root = false;
  545. // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
  546. if ( ! $skip_cache ) {
  547. if ( get_option('stylesheet') == $stylesheet_or_template )
  548. $theme_root = get_option('stylesheet_root');
  549. elseif ( get_option('template') == $stylesheet_or_template )
  550. $theme_root = get_option('template_root');
  551. }
  552. if ( empty($theme_root) ) {
  553. $theme_roots = get_theme_roots();
  554. if ( !empty($theme_roots[$stylesheet_or_template]) )
  555. $theme_root = $theme_roots[$stylesheet_or_template];
  556. }
  557. return $theme_root;
  558. }
  559. /**
  560. * Display localized stylesheet link element.
  561. *
  562. * @since 2.1.0
  563. */
  564. function locale_stylesheet() {
  565. $stylesheet = get_locale_stylesheet_uri();
  566. if ( empty($stylesheet) )
  567. return;
  568. echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
  569. }
  570. /**
  571. * Start preview theme output buffer.
  572. *
  573. * Will only perform task if the user has permissions and template and preview
  574. * query variables exist.
  575. *
  576. * @since 2.6.0
  577. */
  578. function preview_theme() {
  579. if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
  580. return;
  581. if ( !current_user_can( 'switch_themes' ) )
  582. return;
  583. // Admin Thickbox requests
  584. if ( isset( $_GET['preview_iframe'] ) )
  585. show_admin_bar( false );
  586. $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
  587. if ( validate_file($_GET['template']) )
  588. return;
  589. add_filter( 'template', '_preview_theme_template_filter' );
  590. if ( isset($_GET['stylesheet']) ) {
  591. $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
  592. if ( validate_file($_GET['stylesheet']) )
  593. return;
  594. add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
  595. }
  596. // Prevent theme mods to current theme being used on theme being previewed
  597. add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' );
  598. ob_start( 'preview_theme_ob_filter' );
  599. }
  600. add_action('setup_theme', 'preview_theme');
  601. /**
  602. * Private function to modify the current template when previewing a theme
  603. *
  604. * @since 2.9.0
  605. * @access private
  606. *
  607. * @return string
  608. */
  609. function _preview_theme_template_filter() {
  610. return isset($_GET['template']) ? $_GET['template'] : '';
  611. }
  612. /**
  613. * Private function to modify the current stylesheet when previewing a theme
  614. *
  615. * @since 2.9.0
  616. * @access private
  617. *
  618. * @return string
  619. */
  620. function _preview_theme_stylesheet_filter() {
  621. return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
  622. }
  623. /**
  624. * Callback function for ob_start() to capture all links in the theme.
  625. *
  626. * @since 2.6.0
  627. * @access private
  628. *
  629. * @param string $content
  630. * @return string
  631. */
  632. function preview_theme_ob_filter( $content ) {
  633. return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
  634. }
  635. /**
  636. * Manipulates preview theme links in order to control and maintain location.
  637. *
  638. * Callback function for preg_replace_callback() to accept and filter matches.
  639. *
  640. * @since 2.6.0
  641. * @access private
  642. *
  643. * @param array $matches
  644. * @return string
  645. */
  646. function preview_theme_ob_filter_callback( $matches ) {
  647. if ( strpos($matches[4], 'onclick') !== false )
  648. $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if it's escaped by \ to prevent breaking mid-attribute.
  649. if (
  650. ( false !== strpos($matches[3], '/wp-admin/') )
  651. ||
  652. ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
  653. ||
  654. ( false !== strpos($matches[3], '/feed/') )
  655. ||
  656. ( false !== strpos($matches[3], '/trackback/') )
  657. )
  658. return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
  659. $stylesheet = isset( $_GET['stylesheet'] ) ? $_GET['stylesheet'] : '';
  660. $template = isset( $_GET['template'] ) ? $_GET['template'] : '';
  661. $link = add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => 1 ), $matches[3] );
  662. if ( 0 === strpos($link, 'preview=1') )
  663. $link = "?$link";
  664. return $matches[1] . esc_attr( $link ) . $matches[4];
  665. }
  666. /**
  667. * Switches the theme.
  668. *
  669. * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
  670. * of two arguments: $template then $stylesheet. This is for backwards compatibility.
  671. *
  672. * @since 2.5.0
  673. *
  674. * @param string $stylesheet Stylesheet name
  675. */
  676. function switch_theme( $stylesheet ) {
  677. global $wp_theme_directories, $sidebars_widgets;
  678. if ( is_array( $sidebars_widgets ) )
  679. set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
  680. $old_theme = wp_get_theme();
  681. $new_theme = wp_get_theme( $stylesheet );
  682. if ( func_num_args() > 1 ) {
  683. $template = $stylesheet;
  684. $stylesheet = func_get_arg( 1 );
  685. } else {
  686. $template = $new_theme->get_template();
  687. }
  688. update_option( 'template', $template );
  689. update_option( 'stylesheet', $stylesheet );
  690. if ( count( $wp_theme_directories ) > 1 ) {
  691. update_option( 'template_root', get_raw_theme_root( $template, true ) );
  692. update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
  693. } else {
  694. delete_option( 'template_root' );
  695. delete_option( 'stylesheet_root' );
  696. }
  697. $new_name = $new_theme->get('Name');
  698. update_option( 'current_theme', $new_name );
  699. if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
  700. $default_theme_mods = (array) get_option( 'mods_' . $new_name );
  701. add_option( "theme_mods_$stylesheet", $default_theme_mods );
  702. }
  703. update_option( 'theme_switched', $old_theme->get_stylesheet() );
  704. /**
  705. * Fires after the theme is switched.
  706. *
  707. * @since 1.5.0
  708. *
  709. * @param string $new_name Name of the new theme.
  710. * @param WP_Theme $new_theme WP_Theme instance of the new theme.
  711. */
  712. do_action( 'switch_theme', $new_name, $new_theme );
  713. }
  714. /**
  715. * Checks that current theme files 'index.php' and 'style.css' exists.
  716. *
  717. * Does not check the default theme, which is the fallback and should always exist.
  718. * Will switch theme to the fallback theme if current theme does not validate.
  719. * You can use the 'validate_current_theme' filter to return false to
  720. * disable this functionality.
  721. *
  722. * @since 1.5.0
  723. * @see WP_DEFAULT_THEME
  724. *
  725. * @return bool
  726. */
  727. function validate_current_theme() {
  728. /**
  729. * Filter whether to validate the current theme.
  730. *
  731. * @since 2.7.0
  732. *
  733. * @param bool true Validation flag to check the current theme.
  734. */
  735. if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) )
  736. return true;
  737. if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
  738. switch_theme( WP_DEFAULT_THEME );
  739. return false;
  740. }
  741. if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
  742. switch_theme( WP_DEFAULT_THEME );
  743. return false;
  744. }
  745. if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
  746. switch_theme( WP_DEFAULT_THEME );
  747. return false;
  748. }
  749. return true;
  750. }
  751. /**
  752. * Retrieve all theme modifications.
  753. *
  754. * @since 3.1.0
  755. *
  756. * @return array Theme modifications.
  757. */
  758. function get_theme_mods() {
  759. $theme_slug = get_option( 'stylesheet' );
  760. if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
  761. $theme_name = get_option( 'current_theme' );
  762. if ( false === $theme_name )
  763. $theme_name = wp_get_theme()->get('Name');
  764. $mods = get_option( "mods_$theme_name" ); // Deprecated location.
  765. if ( is_admin() && false !== $mods ) {
  766. update_option( "theme_mods_$theme_slug", $mods );
  767. delete_option( "mods_$theme_name" );
  768. }
  769. }
  770. return $mods;
  771. }
  772. /**
  773. * Retrieve theme modification value for the current theme.
  774. *
  775. * If the modification name does not exist, then the $default will be passed
  776. * through {@link http://php.net/sprintf sprintf()} PHP function with the first
  777. * string the template directory URI and the second string the stylesheet
  778. * directory URI.
  779. *
  780. * @since 2.1.0
  781. *
  782. * @param string $name Theme modification name.
  783. * @param bool|string $default
  784. * @return string
  785. */
  786. function get_theme_mod( $name, $default = false ) {
  787. $mods = get_theme_mods();
  788. if ( isset( $mods[$name] ) ) {
  789. /**
  790. * Filter the theme modification, or 'theme_mod', value.
  791. *
  792. * The dynamic portion of the hook name, $name, refers to
  793. * the key name of the modification array. For example,
  794. * 'header_textcolor', 'header_image', and so on depending
  795. * on the theme options.
  796. *
  797. * @since 2.2.0
  798. *
  799. * @param string $current_mod The value of the current theme modification.
  800. */
  801. return apply_filters( "theme_mod_{$name}", $mods[$name] );
  802. }
  803. if ( is_string( $default ) )
  804. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  805. /** This filter is documented in wp-includes/theme.php */
  806. return apply_filters( "theme_mod_{$name}", $default );
  807. }
  808. /**
  809. * Update theme modification value for the current theme.
  810. *
  811. * @since 2.1.0
  812. *
  813. * @param string $name Theme modification name.
  814. * @param string $value theme modification value.
  815. */
  816. function set_theme_mod( $name, $value ) {
  817. $mods = get_theme_mods();
  818. $mods[ $name ] = $value;
  819. $theme = get_option( 'stylesheet' );
  820. update_option( "theme_mods_$theme", $mods );
  821. }
  822. /**
  823. * Remove theme modification name from current theme list.
  824. *
  825. * If removing the name also removes all elements, then the entire option will
  826. * be removed.
  827. *
  828. * @since 2.1.0
  829. *
  830. * @param string $name Theme modification name.
  831. * @return null
  832. */
  833. function remove_theme_mod( $name ) {
  834. $mods = get_theme_mods();
  835. if ( ! isset( $mods[ $name ] ) )
  836. return;
  837. unset( $mods[ $name ] );
  838. if ( empty( $mods ) )
  839. return remove_theme_mods();
  840. $theme = get_option( 'stylesheet' );
  841. update_option( "theme_mods_$theme", $mods );
  842. }
  843. /**
  844. * Remove theme modifications option for current theme.
  845. *
  846. * @since 2.1.0
  847. */
  848. function remove_theme_mods() {
  849. delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
  850. // Old style.
  851. $theme_name = get_option( 'current_theme' );
  852. if ( false === $theme_name )
  853. $theme_name = wp_get_theme()->get('Name');
  854. delete_option( 'mods_' . $theme_name );
  855. }
  856. /**
  857. * Retrieve text color for custom header.
  858. *
  859. * @since 2.1.0
  860. *
  861. * @return string
  862. */
  863. function get_header_textcolor() {
  864. return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
  865. }
  866. /**
  867. * Display text color for custom header.
  868. *
  869. * @since 2.1.0
  870. */
  871. function header_textcolor() {
  872. echo get_header_textcolor();
  873. }
  874. /**
  875. * Whether to display the header text.
  876. *
  877. * @since 3.4.0
  878. *
  879. * @return bool
  880. */
  881. function display_header_text() {
  882. if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
  883. return false;
  884. $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
  885. return 'blank' != $text_color;
  886. }
  887. /**
  888. * Retrieve header image for custom header.
  889. *
  890. * @since 2.1.0
  891. *
  892. * @return string
  893. */
  894. function get_header_image() {
  895. $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  896. if ( 'remove-header' == $url )
  897. return false;
  898. if ( is_random_header_image() )
  899. $url = get_random_header_image();
  900. return esc_url_raw( set_url_scheme( $url ) );
  901. }
  902. /**
  903. * Get random header image data from registered images in theme.
  904. *
  905. * @since 3.4.0
  906. *
  907. * @access private
  908. *
  909. * @return string Path to header image
  910. */
  911. function _get_random_header_data() {
  912. static $_wp_random_header;
  913. if ( empty( $_wp_random_header ) ) {
  914. global $_wp_default_headers;
  915. $header_image_mod = get_theme_mod( 'header_image', '' );
  916. $headers = array();
  917. if ( 'random-uploaded-image' == $header_image_mod )
  918. $headers = get_uploaded_header_images();
  919. elseif ( ! empty( $_wp_default_headers ) ) {
  920. if ( 'random-default-image' == $header_image_mod ) {
  921. $headers = $_wp_default_headers;
  922. } else {
  923. if ( current_theme_supports( 'custom-header', 'random-default' ) )
  924. $headers = $_wp_default_headers;
  925. }
  926. }
  927. if ( empty( $headers ) )
  928. return new stdClass;
  929. $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
  930. $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
  931. $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
  932. }
  933. return $_wp_random_header;
  934. }
  935. /**
  936. * Get random header image url from registered images in theme.
  937. *
  938. * @since 3.2.0
  939. *
  940. * @return string Path to header image
  941. */
  942. function get_random_header_image() {
  943. $random_image = _get_random_header_data();
  944. if ( empty( $random_image->url ) )
  945. return '';
  946. return $random_image->url;
  947. }
  948. /**
  949. * Check if random header image is in use.
  950. *
  951. * Always true if user expressly chooses the option in Appearance > Header.
  952. * Also true if theme has multiple header images registered, no specific header image
  953. * is chosen, and theme turns on random headers with add_theme_support().
  954. *
  955. * @since 3.2.0
  956. *
  957. * @param string $type The random pool to use. any|default|uploaded
  958. * @return boolean
  959. */
  960. function is_random_header_image( $type = 'any' ) {
  961. $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  962. if ( 'any' == $type ) {
  963. if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
  964. return true;
  965. } else {
  966. if ( "random-$type-image" == $header_image_mod )
  967. return true;
  968. elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
  969. return true;
  970. }
  971. return false;
  972. }
  973. /**
  974. * Display header image URL.
  975. *
  976. * @since 2.1.0
  977. */
  978. function header_image() {
  979. echo esc_url( get_header_image() );
  980. }
  981. /**
  982. * Get the header images uploaded for the current theme.
  983. *
  984. * @since 3.2.0
  985. *
  986. * @return array
  987. */
  988. function get_uploaded_header_images() {
  989. $header_images = array();
  990. // @todo caching
  991. $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
  992. if ( empty( $headers ) )
  993. return array();
  994. foreach ( (array) $headers as $header ) {
  995. $url = esc_url_raw( $header->guid );
  996. $header_data = wp_get_attachment_metadata( $header->ID );
  997. $header_index = basename($url);
  998. $header_images[$header_index] = array();
  999. $header_images[$header_index]['attachment_id'] = $header->ID;
  1000. $header_images[$header_index]['url'] = $url;
  1001. $header_images[$header_index]['thumbnail_url'] = $url;
  1002. if ( isset( $header_data['width'] ) )
  1003. $header_images[$header_index]['width'] = $header_data['width'];
  1004. if ( isset( $header_data['height'] ) )
  1005. $header_images[$header_index]['height'] = $header_data['height'];
  1006. }
  1007. return $header_images;
  1008. }
  1009. /**
  1010. * Get the header image data.
  1011. *
  1012. * @since 3.4.0
  1013. *
  1014. * @return object
  1015. */
  1016. function get_custom_header() {
  1017. global $_wp_default_headers;
  1018. if ( is_random_header_image() ) {
  1019. $data = _get_random_header_data();
  1020. } else {
  1021. $data = get_theme_mod( 'header_image_data' );
  1022. if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
  1023. $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
  1024. $data = array();
  1025. $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
  1026. if ( ! empty( $_wp_default_headers ) ) {
  1027. foreach ( (array) $_wp_default_headers as $default_header ) {
  1028. $url = vsprintf( $default_header['url'], $directory_args );
  1029. if ( $data['url'] == $url ) {
  1030. $data = $default_header;
  1031. $data['url'] = $url;
  1032. $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
  1033. break;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. $default = array(
  1040. 'url' => '',
  1041. 'thumbnail_url' => '',
  1042. 'width' => get_theme_support( 'custom-header', 'width' ),
  1043. 'height' => get_theme_support( 'custom-header', 'height' ),
  1044. );
  1045. return (object) wp_parse_args( $data, $default );
  1046. }
  1047. /**
  1048. * Register a selection of default headers to be displayed by the custom header admin UI.
  1049. *
  1050. * @since 3.0.0
  1051. *
  1052. * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
  1053. */
  1054. function register_default_headers( $headers ) {
  1055. global $_wp_default_headers;
  1056. $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
  1057. }
  1058. /**
  1059. * Unregister default headers.
  1060. *
  1061. * This function must be called after register_default_headers() has already added the
  1062. * header you want to remove.
  1063. *
  1064. * @see register_default_headers()
  1065. * @since 3.0.0
  1066. *
  1067. * @param string|array $header The header string id (key of array) to remove, or an array thereof.
  1068. * @return True on success, false on failure.
  1069. */
  1070. function unregister_default_headers( $header ) {
  1071. global $_wp_default_headers;
  1072. if ( is_array( $header ) ) {
  1073. array_map( 'unregister_default_headers', $header );
  1074. } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
  1075. unset( $_wp_default_headers[ $header ] );
  1076. return true;
  1077. } else {
  1078. return false;
  1079. }
  1080. }
  1081. /**
  1082. * Retrieve background image for custom background.
  1083. *
  1084. * @since 3.0.0
  1085. *
  1086. * @return string
  1087. */
  1088. function get_background_image() {
  1089. return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
  1090. }
  1091. /**
  1092. * Display background image path.
  1093. *
  1094. * @since 3.0.0
  1095. */
  1096. function background_image() {
  1097. echo get_background_image();
  1098. }
  1099. /**
  1100. * Retrieve value for custom background color.
  1101. *
  1102. * @since 3.0.0
  1103. *
  1104. * @return string
  1105. */
  1106. function get_background_color() {
  1107. return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
  1108. }
  1109. /**
  1110. * Display background color value.
  1111. *
  1112. * @since 3.0.0
  1113. */
  1114. function background_color() {
  1115. echo get_background_color();
  1116. }
  1117. /**
  1118. * Default custom background callback.
  1119. *
  1120. * @since 3.0.0
  1121. * @access protected
  1122. */
  1123. function _custom_background_cb() {
  1124. // $background is the saved custom image, or the default image.
  1125. $background = set_url_scheme( get_background_image() );
  1126. // $color is the saved custom color.
  1127. // A default has to be specified in style.css. It will not be printed here.
  1128. $color = get_theme_mod( 'background_color' );
  1129. if ( ! $background && ! $color )
  1130. return;
  1131. $style = $color ? "background-color: #$color;" : '';
  1132. if ( $background ) {
  1133. $image = " background-image: url('$background');";
  1134. $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
  1135. if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
  1136. $repeat = 'repeat';
  1137. $repeat = " background-repeat: $repeat;";
  1138. $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
  1139. if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
  1140. $position = 'left';
  1141. $position = " background-position: top $position;";
  1142. $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
  1143. if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
  1144. $attachment = 'scroll';
  1145. $attachment = " background-attachment: $attachment;";
  1146. $style .= $image . $repeat . $position . $attachment;
  1147. }
  1148. ?>
  1149. <style type="text/css" id="custom-background-css">
  1150. body.custom-background { <?php echo trim( $style ); ?> }
  1151. </style>
  1152. <?php
  1153. }
  1154. /**
  1155. * Add callback for custom TinyMCE editor stylesheets.
  1156. *
  1157. * The parameter $stylesheet is the name of the stylesheet, relative to
  1158. * the theme root. It also accepts an array of stylesheets.
  1159. * It is optional and defaults to 'editor-style.css'.
  1160. *
  1161. * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
  1162. * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
  1163. * If an array of stylesheets is passed to add_editor_style(),
  1164. * RTL is only added for the first stylesheet.
  1165. *
  1166. * Since version 3.4 the TinyMCE body has .rtl CSS class.
  1167. * It is a better option to use that class and add any RTL styles to the main stylesheet.
  1168. *
  1169. * @since 3.0.0
  1170. *
  1171. * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
  1172. * Defaults to 'editor-style.css'
  1173. */
  1174. function add_editor_style( $stylesheet = 'editor-style.css' ) {
  1175. add_theme_support( 'editor-style' );
  1176. if ( ! is_admin() )
  1177. return;
  1178. global $editor_styles;
  1179. $editor_styles = (array) $editor_styles;
  1180. $stylesheet = (array) $stylesheet;
  1181. if ( is_rtl() ) {
  1182. $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
  1183. $stylesheet[] = $rtl_stylesheet;
  1184. }
  1185. $editor_styles = array_merge( $editor_styles, $stylesheet );
  1186. }
  1187. /**
  1188. * Removes all visual editor stylesheets.
  1189. *
  1190. * @since 3.1.0
  1191. *
  1192. * @return bool True on success, false if there were no stylesheets to remove.
  1193. */
  1194. function remove_editor_styles() {
  1195. if ( ! current_theme_supports( 'editor-style' ) )
  1196. return false;
  1197. _remove_theme_support( 'editor-style' );
  1198. if ( is_admin() )
  1199. $GLOBALS['editor_styles'] = array();
  1200. return true;
  1201. }
  1202. /**
  1203. * Allows a theme to register its support of a certain feature
  1204. *
  1205. * Must be called in the theme's functions.php file to work.
  1206. * If attached to a hook, it must be after_setup_theme.
  1207. * The init hook may be too late for some features.
  1208. *
  1209. * @since 2.9.0
  1210. * @param string $feature the feature being added
  1211. */
  1212. function add_theme_support( $feature ) {
  1213. global $_wp_theme_features;
  1214. if ( func_num_args() == 1 )
  1215. $args = true;
  1216. else
  1217. $args = array_slice( func_get_args(), 1 );
  1218. switch ( $feature ) {
  1219. case 'post-formats' :
  1220. if ( is_array( $args[0] ) )
  1221. $args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) );
  1222. break;
  1223. case 'html5' :
  1224. // You can't just pass 'html5', you need to pass an array of types.
  1225. if ( empty( $args[0] ) ) {
  1226. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
  1227. } elseif ( ! is_array( $args[0] ) ) {
  1228. _doing_it_wrong( "add_theme_support( 'html5' )", 'You need to pass an array of types.', '3.6.1' );
  1229. return false;
  1230. }
  1231. // Calling 'html5' again merges, rather than overwrites.
  1232. if ( isset( $_wp_theme_features['html5'] ) )
  1233. $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
  1234. break;
  1235. case 'custom-header-uploads' :
  1236. return add_theme_support( 'custom-header', array( 'uploads' => true ) );
  1237. break;
  1238. case 'custom-header' :
  1239. if ( ! is_array( $args ) )
  1240. $args = array( 0 => array() );
  1241. $defaults = array(
  1242. 'default-image' => '',
  1243. 'random-default' => false,
  1244. 'width' => 0,
  1245. 'height' => 0,
  1246. 'flex-height' => false,
  1247. 'flex-width' => false,
  1248. 'default-text-color' => '',
  1249. 'header-text' => true,
  1250. 'uploads' => true,
  1251. 'wp-head-callback' => '',
  1252. 'admin-head-callback' => '',
  1253. 'admin-preview-callback' => '',
  1254. );
  1255. $jit = isset( $args[0]['__jit'] );
  1256. unset( $args[0]['__jit'] );
  1257. // Merge in data from previous add_theme_support() calls.
  1258. // The first value registered wins. (A child theme is set up first.)
  1259. if ( isset( $_wp_theme_features['custom-header'] ) )
  1260. $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
  1261. // Load in the defaults at the end, as we need to insure first one wins.
  1262. // This will cause all constants to be defined, as each arg will then be set to the default.
  1263. if ( $jit )
  1264. $args[0] = wp_parse_args( $args[0], $defaults );
  1265. // If a constant was defined, use that value. Otherwise, define the constant to ensure
  1266. // the constant is always accurate (and is not defined later, overriding our value).
  1267. // As stated above, the first value wins.
  1268. // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
  1269. // Constants are lame. Don't reference them. This is just for backwards compatibility.
  1270. if ( defined( 'NO_HEADER_TEXT' ) )
  1271. $args[0]['header-text'] = ! NO_HEADER_TEXT;
  1272. elseif ( isset( $args[0]['header-text'] ) )
  1273. define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
  1274. if ( defined( 'HEADER_IMAGE_WIDTH' ) )
  1275. $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
  1276. elseif ( isset( $args[0]['width'] ) )
  1277. define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
  1278. if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
  1279. $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
  1280. elseif ( isset( $args[0]['height'] ) )
  1281. define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
  1282. if ( defined( 'HEADER_TEXTCOLOR' ) )
  1283. $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
  1284. elseif ( isset( $args[0]['default-text-color'] ) )
  1285. define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
  1286. if ( defined( 'HEADER_IMAGE' ) )
  1287. $args[0]['default-image'] = HEADER_IMAGE;
  1288. elseif ( isset( $args[0]['default-image'] ) )
  1289. define( 'HEADER_IMAGE', $args[0]['default-image'] );
  1290. if ( $jit && ! empty( $args[0]['default-image'] ) )
  1291. $args[0]['random-default'] = false;
  1292. // If headers are supported, and we still don't have a defined width or height,
  1293. // we have implicit flex sizes.
  1294. if ( $jit ) {
  1295. if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
  1296. $args[0]['flex-width'] = true;
  1297. if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
  1298. $args[0]['flex-height'] = true;
  1299. }
  1300. break;
  1301. case 'custom-background' :
  1302. if ( ! is_array( $args ) )
  1303. $args = array( 0 => array() );
  1304. $defaults = array(
  1305. 'default-image' => '',
  1306. 'default-repeat' => 'repeat',
  1307. 'default-position-x' => 'left',
  1308. 'default-attachment' => 'scroll',
  1309. 'default-color' => '',
  1310. 'wp-head-callback' => '_custom_background_cb',
  1311. 'admin-head-callback' => '',
  1312. 'admin-preview-callback' => '',
  1313. );
  1314. $jit = isset( $args[0]['__jit'] );
  1315. unset( $args[0]['__jit'] );
  1316. // Merge in data from previous add_theme_support() calls. The first value registered wins.
  1317. if ( isset( $_wp_theme_features['custom-background'] ) )
  1318. $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
  1319. if ( $jit )
  1320. $args[0] = wp_parse_args( $args[0], $defaults );
  1321. if ( defined( 'BACKGROUND_COLOR' ) )
  1322. $args[0]['default-color'] = BACKGROUND_COLOR;
  1323. elseif ( isset( $args[0]['default-color'] ) || $jit )
  1324. define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
  1325. if ( defined( 'BACKGROUND_IMAGE' ) )
  1326. $args[0]['default-image'] = BACKGROUND_IMAGE;
  1327. elseif ( isset( $args[0]['default-image'] ) || $jit )
  1328. define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
  1329. break;
  1330. }
  1331. $_wp_theme_features[ $feature ] = $args;
  1332. }
  1333. /**
  1334. * Registers the internal custom header and background routines.
  1335. *
  1336. * @since 3.4.0
  1337. * @access private
  1338. */
  1339. function _custom_header_background_just_in_time() {
  1340. global $custom_image_header, $custom_background;
  1341. if ( current_theme_supports( 'custom-header' ) ) {
  1342. // In case any constants were defined after an add_custom_image_header() call, re-run.
  1343. add_theme_support( 'custom-header', array( '__jit' => true ) );
  1344. $args = get_theme_support( 'custom-header' );
  1345. if ( $args[0]['wp-head-callback'] )
  1346. add_action( 'wp_head', $args[0]['wp-head-callback'] );
  1347. if ( is_admin() ) {
  1348. require_once( ABSPATH . 'wp-admin/custom-header.php' );
  1349. $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
  1350. }
  1351. }
  1352. if ( current_theme_supports( 'custom-background' ) ) {
  1353. // In case any constants were defined after an add_custom_background() call, re-run.
  1354. add_theme_support( 'custom-background', array( '__jit' => true ) );
  1355. $args = get_theme_support( 'custom-background' );
  1356. add_action( 'wp_head', $args[0]['wp-head-callback'] );
  1357. if ( is_admin() ) {
  1358. require_once( ABSPATH . 'wp-admin/custom-background.php' );
  1359. $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
  1360. }
  1361. }
  1362. }
  1363. add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
  1364. /**
  1365. * Gets the theme support arguments passed when registering that support
  1366. *
  1367. * @since 3.1
  1368. * @param string $feature the feature to check
  1369. * @return array The array of extra arguments
  1370. */
  1371. function get_theme_support( $feature ) {
  1372. global $_wp_theme_features;
  1373. if ( ! isset( $_wp_theme_features[ $feature ] ) )
  1374. return false;
  1375. if ( func_num_args() <= 1 )
  1376. return $_wp_theme_features[ $feature ];
  1377. $args = array_slice( func_get_args(), 1 );
  1378. switch ( $feature ) {
  1379. case 'custom-header' :
  1380. case 'custom-background' :
  1381. if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
  1382. return $_wp_theme_features[ $feature ][0][ $args[0] ];
  1383. return false;
  1384. break;
  1385. default :
  1386. return $_wp_theme_features[ $feature ];
  1387. break;
  1388. }
  1389. }
  1390. /**
  1391. * Allows a theme to de-register its support of a certain feature
  1392. *
  1393. * Should be called in the theme's functions.php file. Generally would
  1394. * be used for child themes to override support from the parent theme.
  1395. *
  1396. * @since 3.0.0
  1397. * @see add_theme_support()
  1398. * @param string $feature the feature being added
  1399. * @return bool Whether feature was removed.
  1400. */
  1401. function remove_theme_support( $feature ) {
  1402. // Blacklist: for internal registrations not used directly by themes.
  1403. if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
  1404. return false;
  1405. return _remove_theme_support( $feature );
  1406. }
  1407. /**
  1408. * Do not use. Removes theme support internally, ignorant of the blacklist.
  1409. *
  1410. * @access private
  1411. * @since 3.1.0
  1412. */
  1413. function _remove_theme_support( $feature ) {
  1414. global $_wp_theme_features;
  1415. switch ( $feature ) {
  1416. case 'custom-header-uploads' :
  1417. if ( ! isset( $_wp_theme_features['custom-header'] ) )
  1418. return false;
  1419. add_theme_support( 'custom-header', array( 'uploads' => false ) );
  1420. return; // Do not continue - custom-header-uploads no longer exists.
  1421. }
  1422. if ( ! isset( $_wp_theme_features[ $feature ] ) )
  1423. return false;
  1424. switch ( $feature ) {
  1425. case 'custom-header' :
  1426. if ( ! did_action( 'wp_loaded' ) )
  1427. break;
  1428. $support = get_theme_support( 'custom-header' );
  1429. if ( $support[0]['wp-head-callback'] )
  1430. remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  1431. remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
  1432. unset( $GLOBALS['custom_image_he