PageRenderTime 73ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/theme.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 2014 lines | 979 code | 250 blank | 785 comment | 249 complexity | 1d5813ae36183214a6cdf74f5080ae91 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full 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 intents and 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 = str_replace( '%2F', '/', rawurlencode( 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 = str_replace( '%2F', '/', rawurlencode( 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. }
  344. if ( ! is_array( $wp_theme_directories ) ) {
  345. $wp_theme_directories = array();
  346. }
  347. $untrailed = untrailingslashit( $directory );
  348. if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories ) ) {
  349. $wp_theme_directories[] = $untrailed;
  350. }
  351. return true;
  352. }
  353. /**
  354. * Search all registered theme directories for complete and valid themes.
  355. *
  356. * @since 2.9.0
  357. *
  358. * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
  359. * @return array Valid themes found
  360. */
  361. function search_theme_directories( $force = false ) {
  362. global $wp_theme_directories;
  363. if ( empty( $wp_theme_directories ) )
  364. return false;
  365. static $found_themes;
  366. if ( ! $force && isset( $found_themes ) )
  367. return $found_themes;
  368. $found_themes = array();
  369. $wp_theme_directories = (array) $wp_theme_directories;
  370. // Set up maybe-relative, maybe-absolute array of theme directories.
  371. // We always want to return absolute, but we need to cache relative
  372. // to use in get_theme_root().
  373. foreach ( $wp_theme_directories as $theme_root ) {
  374. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
  375. $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
  376. else
  377. $relative_theme_roots[ $theme_root ] = $theme_root;
  378. }
  379. /**
  380. * Filter whether to get the cache of the registered theme directories.
  381. *
  382. * @since 3.4.0
  383. *
  384. * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false.
  385. * @param string $cache_directory Directory to be searched for the cache.
  386. */
  387. if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
  388. $cached_roots = get_site_transient( 'theme_roots' );
  389. if ( is_array( $cached_roots ) ) {
  390. foreach ( $cached_roots as $theme_dir => $theme_root ) {
  391. // A cached theme root is no longer around, so skip it.
  392. if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
  393. continue;
  394. $found_themes[ $theme_dir ] = array(
  395. 'theme_file' => $theme_dir . '/style.css',
  396. 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
  397. );
  398. }
  399. return $found_themes;
  400. }
  401. if ( ! is_int( $cache_expiration ) )
  402. $cache_expiration = 1800; // half hour
  403. } else {
  404. $cache_expiration = 1800; // half hour
  405. }
  406. /* Loop the registered theme directories and extract all themes */
  407. foreach ( $wp_theme_directories as $theme_root ) {
  408. // Start with directories in the root of the current theme directory.
  409. $dirs = @ scandir( $theme_root );
  410. if ( ! $dirs ) {
  411. trigger_error( "$theme_root is not readable", E_USER_NOTICE );
  412. continue;
  413. }
  414. foreach ( $dirs as $dir ) {
  415. if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
  416. continue;
  417. if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
  418. // wp-content/themes/a-single-theme
  419. // wp-content/themes is $theme_root, a-single-theme is $dir
  420. $found_themes[ $dir ] = array(
  421. 'theme_file' => $dir . '/style.css',
  422. 'theme_root' => $theme_root,
  423. );
  424. } else {
  425. $found_theme = false;
  426. // wp-content/themes/a-folder-of-themes/*
  427. // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
  428. $sub_dirs = @ scandir( $theme_root . '/' . $dir );
  429. if ( ! $sub_dirs ) {
  430. trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
  431. continue;
  432. }
  433. foreach ( $sub_dirs as $sub_dir ) {
  434. if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
  435. continue;
  436. if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
  437. continue;
  438. $found_themes[ $dir . '/' . $sub_dir ] = array(
  439. 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
  440. 'theme_root' => $theme_root,
  441. );
  442. $found_theme = true;
  443. }
  444. // Never mind the above, it's just a theme missing a style.css.
  445. // Return it; WP_Theme will catch the error.
  446. if ( ! $found_theme )
  447. $found_themes[ $dir ] = array(
  448. 'theme_file' => $dir . '/style.css',
  449. 'theme_root' => $theme_root,
  450. );
  451. }
  452. }
  453. }
  454. asort( $found_themes );
  455. $theme_roots = array();
  456. $relative_theme_roots = array_flip( $relative_theme_roots );
  457. foreach ( $found_themes as $theme_dir => $theme_data ) {
  458. $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
  459. }
  460. if ( $theme_roots != get_site_transient( 'theme_roots' ) )
  461. set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
  462. return $found_themes;
  463. }
  464. /**
  465. * Retrieve path to themes directory.
  466. *
  467. * Does not have trailing slash.
  468. *
  469. * @since 1.5.0
  470. *
  471. * @param string $stylesheet_or_template The stylesheet or template name of the theme
  472. * @return string Theme path.
  473. */
  474. function get_theme_root( $stylesheet_or_template = false ) {
  475. global $wp_theme_directories;
  476. if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
  477. // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
  478. // This gives relative theme roots the benefit of the doubt when things go haywire.
  479. if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
  480. $theme_root = WP_CONTENT_DIR . $theme_root;
  481. } else {
  482. $theme_root = WP_CONTENT_DIR . '/themes';
  483. }
  484. /**
  485. * Filter the absolute path to the themes directory.
  486. *
  487. * @since 1.5.0
  488. *
  489. * @param string $theme_root Absolute path to themes directory.
  490. */
  491. return apply_filters( 'theme_root', $theme_root );
  492. }
  493. /**
  494. * Retrieve URI for themes directory.
  495. *
  496. * Does not have trailing slash.
  497. *
  498. * @since 1.5.0
  499. *
  500. * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
  501. * Default is to leverage the main theme root.
  502. * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
  503. * the need for a get_raw_theme_root() call.
  504. * @return string Themes URI.
  505. */
  506. function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
  507. global $wp_theme_directories;
  508. if ( $stylesheet_or_template && ! $theme_root )
  509. $theme_root = get_raw_theme_root( $stylesheet_or_template );
  510. if ( $stylesheet_or_template && $theme_root ) {
  511. if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
  512. // Absolute path. Make an educated guess. YMMV -- but note the filter below.
  513. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
  514. $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
  515. elseif ( 0 === strpos( $theme_root, ABSPATH ) )
  516. $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
  517. elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
  518. $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
  519. else
  520. $theme_root_uri = $theme_root;
  521. } else {
  522. $theme_root_uri = content_url( $theme_root );
  523. }
  524. } else {
  525. $theme_root_uri = content_url( 'themes' );
  526. }
  527. /**
  528. * Filter the URI for themes directory.
  529. *
  530. * @since 1.5.0
  531. *
  532. * @param string $theme_root_uri The URI for themes directory.
  533. * @param string $siteurl WordPress web address which is set in General Options.
  534. * @param string $stylesheet_or_template Stylesheet or template name of the theme.
  535. */
  536. return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
  537. }
  538. /**
  539. * Get the raw theme root relative to the content directory with no filters applied.
  540. *
  541. * @since 3.1.0
  542. *
  543. * @param string $stylesheet_or_template The stylesheet or template name of the theme
  544. * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used.
  545. * @return string Theme root
  546. */
  547. function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
  548. global $wp_theme_directories;
  549. if ( count($wp_theme_directories) <= 1 )
  550. return '/themes';
  551. $theme_root = false;
  552. // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
  553. if ( ! $skip_cache ) {
  554. if ( get_option('stylesheet') == $stylesheet_or_template )
  555. $theme_root = get_option('stylesheet_root');
  556. elseif ( get_option('template') == $stylesheet_or_template )
  557. $theme_root = get_option('template_root');
  558. }
  559. if ( empty($theme_root) ) {
  560. $theme_roots = get_theme_roots();
  561. if ( !empty($theme_roots[$stylesheet_or_template]) )
  562. $theme_root = $theme_roots[$stylesheet_or_template];
  563. }
  564. return $theme_root;
  565. }
  566. /**
  567. * Display localized stylesheet link element.
  568. *
  569. * @since 2.1.0
  570. */
  571. function locale_stylesheet() {
  572. $stylesheet = get_locale_stylesheet_uri();
  573. if ( empty($stylesheet) )
  574. return;
  575. echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
  576. }
  577. /**
  578. * Start preview theme output buffer.
  579. *
  580. * Will only perform task if the user has permissions and template and preview
  581. * query variables exist.
  582. *
  583. * @since 2.6.0
  584. */
  585. function preview_theme() {
  586. if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
  587. return;
  588. if ( !current_user_can( 'switch_themes' ) )
  589. return;
  590. // Admin Thickbox requests
  591. if ( isset( $_GET['preview_iframe'] ) )
  592. show_admin_bar( false );
  593. $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
  594. if ( validate_file($_GET['template']) )
  595. return;
  596. add_filter( 'template', '_preview_theme_template_filter' );
  597. if ( isset($_GET['stylesheet']) ) {
  598. $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
  599. if ( validate_file($_GET['stylesheet']) )
  600. return;
  601. add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
  602. }
  603. // Prevent theme mods to current theme being used on theme being previewed
  604. add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' );
  605. ob_start( 'preview_theme_ob_filter' );
  606. }
  607. add_action('setup_theme', 'preview_theme');
  608. /**
  609. * Private function to modify the current template when previewing a theme
  610. *
  611. * @since 2.9.0
  612. * @access private
  613. *
  614. * @return string
  615. */
  616. function _preview_theme_template_filter() {
  617. return isset($_GET['template']) ? $_GET['template'] : '';
  618. }
  619. /**
  620. * Private function to modify the current stylesheet when previewing a theme
  621. *
  622. * @since 2.9.0
  623. * @access private
  624. *
  625. * @return string
  626. */
  627. function _preview_theme_stylesheet_filter() {
  628. return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
  629. }
  630. /**
  631. * Callback function for ob_start() to capture all links in the theme.
  632. *
  633. * @since 2.6.0
  634. * @access private
  635. *
  636. * @param string $content
  637. * @return string
  638. */
  639. function preview_theme_ob_filter( $content ) {
  640. return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
  641. }
  642. /**
  643. * Manipulates preview theme links in order to control and maintain location.
  644. *
  645. * Callback function for preg_replace_callback() to accept and filter matches.
  646. *
  647. * @since 2.6.0
  648. * @access private
  649. *
  650. * @param array $matches
  651. * @return string
  652. */
  653. function preview_theme_ob_filter_callback( $matches ) {
  654. if ( strpos($matches[4], 'onclick') !== false )
  655. $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.
  656. if (
  657. ( false !== strpos($matches[3], '/wp-admin/') )
  658. ||
  659. ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
  660. ||
  661. ( false !== strpos($matches[3], '/feed/') )
  662. ||
  663. ( false !== strpos($matches[3], '/trackback/') )
  664. )
  665. return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
  666. $stylesheet = isset( $_GET['stylesheet'] ) ? $_GET['stylesheet'] : '';
  667. $template = isset( $_GET['template'] ) ? $_GET['template'] : '';
  668. $link = add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => 1 ), $matches[3] );
  669. if ( 0 === strpos($link, 'preview=1') )
  670. $link = "?$link";
  671. return $matches[1] . esc_attr( $link ) . $matches[4];
  672. }
  673. /**
  674. * Switches the theme.
  675. *
  676. * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
  677. * of two arguments: $template then $stylesheet. This is for backwards compatibility.
  678. *
  679. * @since 2.5.0
  680. *
  681. * @param string $stylesheet Stylesheet name
  682. */
  683. function switch_theme( $stylesheet ) {
  684. global $wp_theme_directories, $wp_customize, $sidebars_widgets;
  685. $_sidebars_widgets = null;
  686. if ( 'wp_ajax_customize_save' === current_action() ) {
  687. $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
  688. } elseif ( is_array( $sidebars_widgets ) ) {
  689. $_sidebars_widgets = $sidebars_widgets;
  690. }
  691. if ( is_array( $_sidebars_widgets ) ) {
  692. set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
  693. }
  694. $old_theme = wp_get_theme();
  695. $new_theme = wp_get_theme( $stylesheet );
  696. if ( func_num_args() > 1 ) {
  697. $template = $stylesheet;
  698. $stylesheet = func_get_arg( 1 );
  699. } else {
  700. $template = $new_theme->get_template();
  701. }
  702. update_option( 'template', $template );
  703. update_option( 'stylesheet', $stylesheet );
  704. if ( count( $wp_theme_directories ) > 1 ) {
  705. update_option( 'template_root', get_raw_theme_root( $template, true ) );
  706. update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
  707. } else {
  708. delete_option( 'template_root' );
  709. delete_option( 'stylesheet_root' );
  710. }
  711. $new_name = $new_theme->get('Name');
  712. update_option( 'current_theme', $new_name );
  713. // Migrate from the old mods_{name} option to theme_mods_{slug}.
  714. if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
  715. $default_theme_mods = (array) get_option( 'mods_' . $new_name );
  716. add_option( "theme_mods_$stylesheet", $default_theme_mods );
  717. } else {
  718. /*
  719. * Since retrieve_widgets() is called when initializing the customizer theme,
  720. * we need to to remove the theme mods to avoid overwriting changes made via
  721. * the widget customizer when accessing wp-admin/widgets.php.
  722. */
  723. if ( 'wp_ajax_customize_save' === current_action() ) {
  724. remove_theme_mod( 'sidebars_widgets' );
  725. }
  726. }
  727. update_option( 'theme_switched', $old_theme->get_stylesheet() );
  728. /**
  729. * Fires after the theme is switched.
  730. *
  731. * @since 1.5.0
  732. *
  733. * @param string $new_name Name of the new theme.
  734. * @param WP_Theme $new_theme WP_Theme instance of the new theme.
  735. */
  736. do_action( 'switch_theme', $new_name, $new_theme );
  737. }
  738. /**
  739. * Checks that current theme files 'index.php' and 'style.css' exists.
  740. *
  741. * Does not check the default theme, which is the fallback and should always exist.
  742. * Will switch theme to the fallback theme if current theme does not validate.
  743. * You can use the 'validate_current_theme' filter to return false to
  744. * disable this functionality.
  745. *
  746. * @since 1.5.0
  747. * @see WP_DEFAULT_THEME
  748. *
  749. * @return bool
  750. */
  751. function validate_current_theme() {
  752. /**
  753. * Filter whether to validate the current theme.
  754. *
  755. * @since 2.7.0
  756. *
  757. * @param bool true Validation flag to check the current theme.
  758. */
  759. if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) )
  760. return true;
  761. if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
  762. switch_theme( WP_DEFAULT_THEME );
  763. return false;
  764. }
  765. if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
  766. switch_theme( WP_DEFAULT_THEME );
  767. return false;
  768. }
  769. if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
  770. switch_theme( WP_DEFAULT_THEME );
  771. return false;
  772. }
  773. return true;
  774. }
  775. /**
  776. * Retrieve all theme modifications.
  777. *
  778. * @since 3.1.0
  779. *
  780. * @return array Theme modifications.
  781. */
  782. function get_theme_mods() {
  783. $theme_slug = get_option( 'stylesheet' );
  784. if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
  785. $theme_name = get_option( 'current_theme' );
  786. if ( false === $theme_name )
  787. $theme_name = wp_get_theme()->get('Name');
  788. $mods = get_option( "mods_$theme_name" ); // Deprecated location.
  789. if ( is_admin() && false !== $mods ) {
  790. update_option( "theme_mods_$theme_slug", $mods );
  791. delete_option( "mods_$theme_name" );
  792. }
  793. }
  794. return $mods;
  795. }
  796. /**
  797. * Retrieve theme modification value for the current theme.
  798. *
  799. * If the modification name does not exist, then the $default will be passed
  800. * through {@link http://php.net/sprintf sprintf()} PHP function with the first
  801. * string the template directory URI and the second string the stylesheet
  802. * directory URI.
  803. *
  804. * @since 2.1.0
  805. *
  806. * @param string $name Theme modification name.
  807. * @param bool|string $default
  808. * @return string
  809. */
  810. function get_theme_mod( $name, $default = false ) {
  811. $mods = get_theme_mods();
  812. if ( isset( $mods[$name] ) ) {
  813. /**
  814. * Filter the theme modification, or 'theme_mod', value.
  815. *
  816. * The dynamic portion of the hook name, $name, refers to
  817. * the key name of the modification array. For example,
  818. * 'header_textcolor', 'header_image', and so on depending
  819. * on the theme options.
  820. *
  821. * @since 2.2.0
  822. *
  823. * @param string $current_mod The value of the current theme modification.
  824. */
  825. return apply_filters( "theme_mod_{$name}", $mods[$name] );
  826. }
  827. if ( is_string( $default ) )
  828. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  829. /** This filter is documented in wp-includes/theme.php */
  830. return apply_filters( "theme_mod_{$name}", $default );
  831. }
  832. /**
  833. * Update theme modification value for the current theme.
  834. *
  835. * @since 2.1.0
  836. *
  837. * @param string $name Theme modification name.
  838. * @param string $value theme modification value.
  839. */
  840. function set_theme_mod( $name, $value ) {
  841. $mods = get_theme_mods();
  842. $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
  843. /**
  844. * Filter the theme mod value on save.
  845. *
  846. * The dynamic portion of the hook name, $name, refers to the key name of
  847. * the modification array. For example, 'header_textcolor', 'header_image',
  848. * and so on depending on the theme options.
  849. *
  850. * @since 3.9.0
  851. *
  852. * @param string $value The new value of the theme mod.
  853. * @param string $old_value The current value of the theme mod.
  854. */
  855. $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value );
  856. $theme = get_option( 'stylesheet' );
  857. update_option( "theme_mods_$theme", $mods );
  858. }
  859. /**
  860. * Remove theme modification name from current theme list.
  861. *
  862. * If removing the name also removes all elements, then the entire option will
  863. * be removed.
  864. *
  865. * @since 2.1.0
  866. *
  867. * @param string $name Theme modification name.
  868. * @return null
  869. */
  870. function remove_theme_mod( $name ) {
  871. $mods = get_theme_mods();
  872. if ( ! isset( $mods[ $name ] ) )
  873. return;
  874. unset( $mods[ $name ] );
  875. if ( empty( $mods ) )
  876. return remove_theme_mods();
  877. $theme = get_option( 'stylesheet' );
  878. update_option( "theme_mods_$theme", $mods );
  879. }
  880. /**
  881. * Remove theme modifications option for current theme.
  882. *
  883. * @since 2.1.0
  884. */
  885. function remove_theme_mods() {
  886. delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
  887. // Old style.
  888. $theme_name = get_option( 'current_theme' );
  889. if ( false === $theme_name )
  890. $theme_name = wp_get_theme()->get('Name');
  891. delete_option( 'mods_' . $theme_name );
  892. }
  893. /**
  894. * Retrieve text color for custom header.
  895. *
  896. * @since 2.1.0
  897. *
  898. * @return string
  899. */
  900. function get_header_textcolor() {
  901. return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
  902. }
  903. /**
  904. * Display text color for custom header.
  905. *
  906. * @since 2.1.0
  907. */
  908. function header_textcolor() {
  909. echo get_header_textcolor();
  910. }
  911. /**
  912. * Whether to display the header text.
  913. *
  914. * @since 3.4.0
  915. *
  916. * @return bool
  917. */
  918. function display_header_text() {
  919. if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
  920. return false;
  921. $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
  922. return 'blank' != $text_color;
  923. }
  924. /**
  925. * Retrieve header image for custom header.
  926. *
  927. * @since 2.1.0
  928. *
  929. * @return string
  930. */
  931. function get_header_image() {
  932. $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  933. if ( 'remove-header' == $url )
  934. return false;
  935. if ( is_random_header_image() )
  936. $url = get_random_header_image();
  937. return esc_url_raw( set_url_scheme( $url ) );
  938. }
  939. /**
  940. * Get random header image data from registered images in theme.
  941. *
  942. * @since 3.4.0
  943. *
  944. * @access private
  945. *
  946. * @return string Path to header image
  947. */
  948. function _get_random_header_data() {
  949. static $_wp_random_header;
  950. if ( empty( $_wp_random_header ) ) {
  951. global $_wp_default_headers;
  952. $header_image_mod = get_theme_mod( 'header_image', '' );
  953. $headers = array();
  954. if ( 'random-uploaded-image' == $header_image_mod )
  955. $headers = get_uploaded_header_images();
  956. elseif ( ! empty( $_wp_default_headers ) ) {
  957. if ( 'random-default-image' == $header_image_mod ) {
  958. $headers = $_wp_default_headers;
  959. } else {
  960. if ( current_theme_supports( 'custom-header', 'random-default' ) )
  961. $headers = $_wp_default_headers;
  962. }
  963. }
  964. if ( empty( $headers ) )
  965. return new stdClass;
  966. $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
  967. $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
  968. $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
  969. }
  970. return $_wp_random_header;
  971. }
  972. /**
  973. * Get random header image url from registered images in theme.
  974. *
  975. * @since 3.2.0
  976. *
  977. * @return string Path to header image
  978. */
  979. function get_random_header_image() {
  980. $random_image = _get_random_header_data();
  981. if ( empty( $random_image->url ) )
  982. return '';
  983. return $random_image->url;
  984. }
  985. /**
  986. * Check if random header image is in use.
  987. *
  988. * Always true if user expressly chooses the option in Appearance > Header.
  989. * Also true if theme has multiple header images registered, no specific header image
  990. * is chosen, and theme turns on random headers with add_theme_support().
  991. *
  992. * @since 3.2.0
  993. *
  994. * @param string $type The random pool to use. any|default|uploaded
  995. * @return boolean
  996. */
  997. function is_random_header_image( $type = 'any' ) {
  998. $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
  999. if ( 'any' == $type ) {
  1000. if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
  1001. return true;
  1002. } else {
  1003. if ( "random-$type-image" == $header_image_mod )
  1004. return true;
  1005. elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
  1006. return true;
  1007. }
  1008. return false;
  1009. }
  1010. /**
  1011. * Display header image URL.
  1012. *
  1013. * @since 2.1.0
  1014. */
  1015. function header_image() {
  1016. echo esc_url( get_header_image() );
  1017. }
  1018. /**
  1019. * Get the header images uploaded for the current theme.
  1020. *
  1021. * @since 3.2.0
  1022. *
  1023. * @return array
  1024. */
  1025. function get_uploaded_header_images() {
  1026. $header_images = array();
  1027. // @todo caching
  1028. $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
  1029. if ( empty( $headers ) )
  1030. return array();
  1031. foreach ( (array) $headers as $header ) {
  1032. $url = esc_url_raw( wp_get_attachment_url( $header->ID ) );
  1033. $header_data = wp_get_attachment_metadata( $header->ID );
  1034. $header_index = basename($url);
  1035. $header_images[$header_index] = array();
  1036. $header_images[$header_index]['attachment_id'] = $header->ID;
  1037. $header_images[$header_index]['url'] = $url;
  1038. $header_images[$header_index]['thumbnail_url'] = $url;
  1039. if ( isset( $header_data['width'] ) )
  1040. $header_images[$header_index]['width'] = $header_data['width'];
  1041. if ( isset( $header_data['height'] ) )
  1042. $header_images[$header_index]['height'] = $header_data['height'];
  1043. }
  1044. return $header_images;
  1045. }
  1046. /**
  1047. * Get the header image data.
  1048. *
  1049. * @since 3.4.0
  1050. *
  1051. * @return object
  1052. */
  1053. function get_custom_header() {
  1054. global $_wp_default_headers;
  1055. if ( is_random_header_image() ) {
  1056. $data = _get_random_header_data();
  1057. } else {
  1058. $data = get_theme_mod( 'header_image_data' );
  1059. if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
  1060. $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
  1061. $data = array();
  1062. $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
  1063. if ( ! empty( $_wp_default_headers ) ) {
  1064. foreach ( (array) $_wp_default_headers as $default_header ) {
  1065. $url = vsprintf( $default_header['url'], $directory_args );
  1066. if ( $data['url'] == $url ) {
  1067. $data = $default_header;
  1068. $data['url'] = $url;
  1069. $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
  1070. break;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. }
  1076. $default = array(
  1077. 'url' => '',
  1078. 'thumbnail_url' => '',
  1079. 'width' => get_theme_support( 'custom-header', 'width' ),
  1080. 'height' => get_theme_support( 'custom-header', 'height' ),
  1081. );
  1082. return (object) wp_parse_args( $data, $default );
  1083. }
  1084. /**
  1085. * Register a selection of default headers to be displayed by the custom header admin UI.
  1086. *
  1087. * @since 3.0.0
  1088. *
  1089. * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
  1090. */
  1091. function register_default_headers( $headers ) {
  1092. global $_wp_default_headers;
  1093. $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
  1094. }
  1095. /**
  1096. * Unregister default headers.
  1097. *
  1098. * This function must be called after register_default_headers() has already added the
  1099. * header you want to remove.
  1100. *
  1101. * @see register_default_headers()
  1102. * @since 3.0.0
  1103. *
  1104. * @param string|array $header The header string id (key of array) to remove, or an array thereof.
  1105. * @return bool|void A single header returns true on success, false on failure.
  1106. * There is currently no return value for multiple headers.
  1107. */
  1108. function unregister_default_headers( $header ) {
  1109. global $_wp_default_headers;
  1110. if ( is_array( $header ) ) {
  1111. array_map( 'unregister_default_headers', $header );
  1112. } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
  1113. unset( $_wp_default_headers[ $header ] );
  1114. return true;
  1115. } else {
  1116. return false;
  1117. }
  1118. }
  1119. /**
  1120. * Retrieve background image for custom background.
  1121. *
  1122. * @since 3.0.0
  1123. *
  1124. * @return string
  1125. */
  1126. function get_background_image() {
  1127. return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
  1128. }
  1129. /**
  1130. * Display background image path.
  1131. *
  1132. * @since 3.0.0
  1133. */
  1134. function background_image() {
  1135. echo get_background_image();
  1136. }
  1137. /**
  1138. * Retrieve value for custom background color.
  1139. *
  1140. * @since 3.0.0
  1141. *
  1142. * @return string
  1143. */
  1144. function get_background_color() {
  1145. return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
  1146. }
  1147. /**
  1148. * Display background color value.
  1149. *
  1150. * @since 3.0.0
  1151. */
  1152. function background_color() {
  1153. echo get_background_color();
  1154. }
  1155. /**
  1156. * Default custom background callback.
  1157. *
  1158. * @since 3.0.0
  1159. * @access protected
  1160. */
  1161. function _custom_background_cb() {
  1162. // $background is the saved custom image, or the default image.
  1163. $background = set_url_scheme( get_background_image() );
  1164. // $color is the saved custom color.
  1165. // A default has to be specified in style.css. It will not be printed here.
  1166. $color = get_background_color();
  1167. if ( $color === get_theme_support( 'custom-background', 'default-color' ) ) {
  1168. $color = false;
  1169. }
  1170. if ( ! $background && ! $color )
  1171. return;
  1172. $style = $color ? "background-color: #$color;" : '';
  1173. if ( $background ) {
  1174. $image = " background-image: url('$background');";
  1175. $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
  1176. if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
  1177. $repeat = 'repeat';
  1178. $repeat = " background-repeat: $repeat;";
  1179. $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
  1180. if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
  1181. $position = 'left';
  1182. $position = " background-position: top $position;";
  1183. $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
  1184. if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
  1185. $attachment = 'scroll';
  1186. $attachment = " background-attachment: $attachment;";
  1187. $style .= $image . $repeat . $position . $attachment;
  1188. }
  1189. ?>
  1190. <style type="text/css" id="custom-background-css">
  1191. body.custom-background { <?php echo trim( $style ); ?> }
  1192. </style>
  1193. <?php
  1194. }
  1195. /**
  1196. * Add callback for custom TinyMCE editor stylesheets.
  1197. *
  1198. * The parameter $stylesheet is the name of the stylesheet, relative to
  1199. * the theme root. It also accepts an array of stylesheets.
  1200. * It is optional and defaults to 'editor-style.css'.
  1201. *
  1202. * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
  1203. * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
  1204. * If an array of stylesheets is passed to add_editor_style(),
  1205. * RTL is only added for the first stylesheet.
  1206. *
  1207. * Since version 3.4 the TinyMCE body has .rtl CSS class.
  1208. * It is a better option to use that class and add any RTL styles to the main stylesheet.
  1209. *
  1210. * @since 3.0.0
  1211. *
  1212. * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
  1213. * Defaults to 'editor-style.css'
  1214. */
  1215. function add_editor_style( $stylesheet = 'editor-style.css' ) {
  1216. add_theme_support( 'editor-style' );
  1217. if ( ! is_admin() )
  1218. return;
  1219. global $editor_styles;
  1220. $editor_styles = (array) $editor_styles;
  1221. $stylesheet = (array) $stylesheet;
  1222. if ( is_rtl() ) {
  1223. $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
  1224. $stylesheet[] = $rtl_stylesheet;
  1225. }
  1226. $editor_styles = array_merge( $editor_styles, $stylesheet );
  1227. }
  1228. /**
  1229. * Removes all visual editor stylesheets.
  1230. *
  1231. * @since 3.1.0
  1232. *
  1233. * @return bool True on success, false if there were no stylesheets to remove.
  1234. */
  1235. function remove_editor_styles() {
  1236. if ( ! current_theme_supports( 'editor-style' ) )
  1237. return false;
  1238. _remove_theme_support( 'editor-style' );
  1239. if ( is_admin() )
  1240. $GLOBALS['editor_styles'] = array();
  1241. return true;
  1242. }
  1243. /**
  1244. * Retrieve any registered editor stylesheets
  1245. *
  1246. * @since 4.0.0
  1247. *
  1248. * @global $editor_styles Registered editor stylesheets
  1249. *
  1250. * @return array If registered, a list of editor stylesheet URLs.
  1251. */
  1252. function get_editor_stylesheets() {
  1253. $stylesheets = array();
  1254. // load editor_style.css if the current theme supports it
  1255. if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
  1256. $editor_styles = $GLOBALS['editor_styles'];
  1257. $editor_styles = array_unique( array_filter( $editor_styles ) );
  1258. $style_uri = get_stylesheet_directory_uri();
  1259. $style_dir = get_stylesheet_directory();
  1260. // Support externally referenced styles (like, say, fonts).
  1261. foreach ( $editor_styles as $key => $file ) {
  1262. if ( preg_match( '~^(https?:)?//~', $file ) ) {
  1263. $stylesheets[] = esc_url_raw( $file );
  1264. unset( $editor_styles[ $key ] );
  1265. }
  1266. }
  1267. // Look in a parent theme first, that way child theme CSS overrides.
  1268. if ( is_child_theme() ) {
  1269. $template_uri = get_template_directory_uri();
  1270. $template_dir = get_template_directory();
  1271. foreach ( $editor_styles as $key => $file ) {
  1272. if ( $file && file_exists( "$template_dir/$file" ) ) {
  1273. $stylesheets[] = "$template_uri/$file";
  1274. }
  1275. }
  1276. }
  1277. foreach ( $editor_styles as $file ) {
  1278. if ( $file && file_exists( "$style_dir/$file" ) ) {
  1279. $stylesheets[] = "$style_uri/$file";
  1280. }
  1281. }
  1282. }
  1283. return $stylesheets;
  1284. }
  1285. /**
  1286. * Allows a theme to register its support of a certain feature
  1287. *
  1288. * Must be called in the theme's functions.php file to work.
  1289. * If attached to a hook, it must be after_setup_theme.
  1290. * The init hook may be too late for some features.
  1291. *
  1292. * @since 2.9.0
  1293. *
  1294. * @param string $feature The feature being added.
  1295. * @return void|bool False on failure, void otherwise.
  1296. */
  1297. function add_theme_support( $feature ) {
  1298. global $_wp_theme_features;
  1299. if ( func_num_args() == 1 )
  1300. $args = true;
  1301. else
  1302. $args = array_slice( func_get_args(), 1 );
  1303. switch ( $feature ) {
  1304. case 'post-formats' :
  1305. if ( is_array( $args[0] ) )
  1306. $args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) );
  1307. break;
  1308. case 'html5' :
  1309. // You can't just pass 'html5', you need to pass an array of types.
  1310. if ( empty( $args[0] ) ) {
  1311. // Build an array of types for back-compat.
  1312. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
  1313. } elseif ( ! is_array( $args[0] ) ) {
  1314. _doing_it_wrong( "add_theme_support( 'html5' )", 'You need to pass an array of types.', '3.6.1' );
  1315. return false;
  1316. }
  1317. // Calling 'html5' again merges, rather than overwrites.
  1318. if ( isset( $_wp_theme_features['html5'] ) )
  1319. $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
  1320. break;
  1321. case 'custom-header-uploads' :
  1322. return add_theme_support( 'custom-header', array( 'uploads' => true ) );
  1323. case 'custom-header' :
  1324. if ( ! is_array( $args ) )
  1325. $args = array( 0 => array() );
  1326. $defaults = array(
  1327. 'default-image' => '',
  1328. 'random-default' => false,
  1329. 'width' => 0,
  1330. 'height' => 0,
  1331. 'flex-height' => false,
  1332. 'flex-width' => false,
  1333. 'default-text-color' => '',
  1334. 'header-text' => true,
  1335. 'uploads' => true,
  1336. 'wp-head-callback' => '',
  1337. 'admin-head-callback' => '',
  1338. 'admin-preview-callback' => '',
  1339. );
  1340. $jit = isset( $args[0]['__jit'] );
  1341. unset( $args[0]['__jit'] );
  1342. // Merge in data from previous add_theme_support() calls.
  1343. // The first value registered wins. (A child theme is set up first.)
  1344. if ( isset( $_wp_theme_features['custom-header'] ) )
  1345. $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
  1346. // Load in the defaults at the end, as we need to insure first one wins.
  1347. // This will cause all constants to be defined, as each arg will then be set to the default.
  1348. if ( $jit )
  1349. $args[0] = wp_parse_args( $args[0], $defaults );
  1350. // If a constant was defined, use that value. Otherwise, define the constant to ensure
  1351. // the constant is always accurate (and is not defined later, overriding our value).
  1352. // As stated above, the first value wins.
  1353. // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
  1354. // Constants are lame. Don't reference them. This is just for backwards compatibility.
  1355. if ( defined( 'NO_HEADER_TEXT' ) )
  1356. $args[0]['header-text'] = ! NO_HEADER_TEXT;
  1357. elseif ( isset( $args[0]['header-text'] ) )
  1358. define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
  1359. if ( defined( 'HEADER_IMAGE_WIDTH' ) )
  1360. $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
  1361. elseif ( isset( $args[0]['width'] ) )
  1362. define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
  1363. if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
  1364. $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
  1365. elseif ( isset( $args[0]['height'] ) )
  1366. define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
  1367. if ( defined( 'HEADER_TEXTCOLOR' ) )
  1368. $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
  1369. elseif ( isset( $args[0]['default-text-color'] ) )
  1370. define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
  1371. if ( defined( 'HEADER_IMAGE' ) )
  1372. $args[0]['default-image'] = HEADER_IMAGE;
  1373. elseif ( isset( $args[0]['default-image'] ) )
  1374. define( 'HEADER_IMAGE', $args[0]['default-image'] );
  1375. if ( $jit && ! empty( $args[0]['default-image'] ) )
  1376. $args[0]['random-default'] = false;
  1377. // If headers are supported, and we still don't have a defined width or height,
  1378. // we have implicit flex sizes.
  1379. if ( $jit ) {
  1380. if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
  1381. $args[0]['flex-width'] = true;
  1382. if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
  1383. $args[0]['flex-height'] = true;
  1384. }
  1385. break;
  1386. case 'custom-background' :
  1387. if ( ! is_array( $args ) )
  1388. $args = array( 0 => array() );
  1389. $defaults =

Large files files are truncated, but you can click here to view the full file