]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/theme.php
WordPress 4.3.1-scripts
[autoinstalls/wordpress.git] / wp-includes / theme.php
1 <?php
2 /**
3  * Theme, template, and stylesheet functions.
4  *
5  * @package WordPress
6  * @subpackage Theme
7  */
8
9 /**
10  * Returns an array of WP_Theme objects based on the arguments.
11  *
12  * Despite advances over get_themes(), this function is quite expensive, and grows
13  * linearly with additional themes. Stick to wp_get_theme() if possible.
14  *
15  * @since 3.4.0
16  *
17  * @global array $wp_theme_directories
18  * @staticvar array $_themes
19  *
20  * @param array $args The search arguments. Optional.
21  * - errors      mixed  True to return themes with errors, false to return themes without errors, null
22  *                      to return all themes. Defaults to false.
23  * - allowed     mixed  (Multisite) True to return only allowed themes for a site. False to return only
24  *                      disallowed themes for a site. 'site' to return only site-allowed themes. 'network'
25  *                      to return only network-allowed themes. Null to return all themes. Defaults to null.
26  * - blog_id     int    (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0,
27  *                      synonymous for the current blog.
28  * @return array Array of WP_Theme objects.
29  */
30 function wp_get_themes( $args = array() ) {
31         global $wp_theme_directories;
32
33         $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
34         $args = wp_parse_args( $args, $defaults );
35
36         $theme_directories = search_theme_directories();
37
38         if ( count( $wp_theme_directories ) > 1 ) {
39                 // Make sure the current theme wins out, in case search_theme_directories() picks the wrong
40                 // one in the case of a conflict. (Normally, last registered theme root wins.)
41                 $current_theme = get_stylesheet();
42                 if ( isset( $theme_directories[ $current_theme ] ) ) {
43                         $root_of_current_theme = get_raw_theme_root( $current_theme );
44                         if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) )
45                                 $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
46                         $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
47                 }
48         }
49
50         if ( empty( $theme_directories ) )
51                 return array();
52
53         if ( is_multisite() && null !== $args['allowed'] ) {
54                 $allowed = $args['allowed'];
55                 if ( 'network' === $allowed )
56                         $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
57                 elseif ( 'site' === $allowed )
58                         $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
59                 elseif ( $allowed )
60                         $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
61                 else
62                         $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
63         }
64
65         $themes = array();
66         static $_themes = array();
67
68         foreach ( $theme_directories as $theme => $theme_root ) {
69                 if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) )
70                         $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
71                 else
72                         $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
73         }
74
75         if ( null !== $args['errors'] ) {
76                 foreach ( $themes as $theme => $wp_theme ) {
77                         if ( $wp_theme->errors() != $args['errors'] )
78                                 unset( $themes[ $theme ] );
79                 }
80         }
81
82         return $themes;
83 }
84
85 /**
86  * Gets a WP_Theme object for a theme.
87  *
88  * @since 3.4.0
89  *
90  * @global array $wp_theme_directories
91  *
92  * @param string $stylesheet Directory name for the theme. Optional. Defaults to current theme.
93  * @param string $theme_root Absolute path of the theme root to look in. Optional. If not specified, get_raw_theme_root()
94  *                               is used to calculate the theme root for the $stylesheet provided (or current theme).
95  * @return WP_Theme Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.
96  */
97 function wp_get_theme( $stylesheet = null, $theme_root = null ) {
98         global $wp_theme_directories;
99
100         if ( empty( $stylesheet ) )
101                 $stylesheet = get_stylesheet();
102
103         if ( empty( $theme_root ) ) {
104                 $theme_root = get_raw_theme_root( $stylesheet );
105                 if ( false === $theme_root )
106                         $theme_root = WP_CONTENT_DIR . '/themes';
107                 elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
108                         $theme_root = WP_CONTENT_DIR . $theme_root;
109         }
110
111         return new WP_Theme( $stylesheet, $theme_root );
112 }
113
114 /**
115  * Clears the cache held by get_theme_roots() and WP_Theme.
116  *
117  * @since 3.5.0
118  * @param bool $clear_update_cache Whether to clear the Theme updates cache
119  */
120 function wp_clean_themes_cache( $clear_update_cache = true ) {
121         if ( $clear_update_cache )
122                 delete_site_transient( 'update_themes' );
123         search_theme_directories( true );
124         foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme )
125                 $theme->cache_delete();
126 }
127
128 /**
129  * Whether a child theme is in use.
130  *
131  * @since 3.0.0
132  *
133  * @return bool true if a child theme is in use, false otherwise.
134  **/
135 function is_child_theme() {
136         return ( TEMPLATEPATH !== STYLESHEETPATH );
137 }
138
139 /**
140  * Retrieve name of the current stylesheet.
141  *
142  * The theme name that the administrator has currently set the front end theme
143  * as.
144  *
145  * For all intents and purposes, the template name and the stylesheet name are
146  * going to be the same for most cases.
147  *
148  * @since 1.5.0
149  *
150  * @return string Stylesheet name.
151  */
152 function get_stylesheet() {
153         /**
154          * Filter the name of current stylesheet.
155          *
156          * @since 1.5.0
157          *
158          * @param string $stylesheet Name of the current stylesheet.
159          */
160         return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
161 }
162
163 /**
164  * Retrieve stylesheet directory path for current theme.
165  *
166  * @since 1.5.0
167  *
168  * @return string Path to current theme directory.
169  */
170 function get_stylesheet_directory() {
171         $stylesheet = get_stylesheet();
172         $theme_root = get_theme_root( $stylesheet );
173         $stylesheet_dir = "$theme_root/$stylesheet";
174
175         /**
176          * Filter the stylesheet directory path for current theme.
177          *
178          * @since 1.5.0
179          *
180          * @param string $stylesheet_dir Absolute path to the current them.
181          * @param string $stylesheet     Directory name of the current theme.
182          * @param string $theme_root     Absolute path to themes directory.
183          */
184         return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
185 }
186
187 /**
188  * Retrieve stylesheet directory URI.
189  *
190  * @since 1.5.0
191  *
192  * @return string
193  */
194 function get_stylesheet_directory_uri() {
195         $stylesheet = str_replace( '%2F', '/', rawurlencode( get_stylesheet() ) );
196         $theme_root_uri = get_theme_root_uri( $stylesheet );
197         $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
198
199         /**
200          * Filter the stylesheet directory URI.
201          *
202          * @since 1.5.0
203          *
204          * @param string $stylesheet_dir_uri Stylesheet directory URI.
205          * @param string $stylesheet         Name of the activated theme's directory.
206          * @param string $theme_root_uri     Themes root URI.
207          */
208         return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
209 }
210
211 /**
212  * Retrieve URI of current theme stylesheet.
213  *
214  * The stylesheet file name is 'style.css' which is appended to {@link
215  * get_stylesheet_directory_uri() stylesheet directory URI} path.
216  *
217  * @since 1.5.0
218  *
219  * @return string
220  */
221 function get_stylesheet_uri() {
222         $stylesheet_dir_uri = get_stylesheet_directory_uri();
223         $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
224         /**
225          * Filter the URI of the current theme stylesheet.
226          *
227          * @since 1.5.0
228          *
229          * @param string $stylesheet_uri     Stylesheet URI for the current theme/child theme.
230          * @param string $stylesheet_dir_uri Stylesheet directory URI for the current theme/child theme.
231          */
232         return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
233 }
234
235 /**
236  * Retrieve localized stylesheet URI.
237  *
238  * The stylesheet directory for the localized stylesheet files are located, by
239  * default, in the base theme directory. The name of the locale file will be the
240  * locale followed by '.css'. If that does not exist, then the text direction
241  * stylesheet will be checked for existence, for example 'ltr.css'.
242  *
243  * The theme may change the location of the stylesheet directory by either using
244  * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
245  * If you want to change the location of the stylesheet files for the entire
246  * WordPress workflow, then change the former. If you just have the locale in a
247  * separate folder, then change the latter.
248  *
249  * @since 2.1.0
250  *
251  * @global WP_Locale $wp_locale
252  *
253  * @return string
254  */
255 function get_locale_stylesheet_uri() {
256         global $wp_locale;
257         $stylesheet_dir_uri = get_stylesheet_directory_uri();
258         $dir = get_stylesheet_directory();
259         $locale = get_locale();
260         if ( file_exists("$dir/$locale.css") )
261                 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
262         elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
263                 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
264         else
265                 $stylesheet_uri = '';
266         /**
267          * Filter the localized stylesheet URI.
268          *
269          * @since 2.1.0
270          *
271          * @param string $stylesheet_uri     Localized stylesheet URI.
272          * @param string $stylesheet_dir_uri Stylesheet directory URI.
273          */
274         return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
275 }
276
277 /**
278  * Retrieve name of the current theme.
279  *
280  * @since 1.5.0
281  *
282  * @return string Template name.
283  */
284 function get_template() {
285         /**
286          * Filter the name of the current theme.
287          *
288          * @since 1.5.0
289          *
290          * @param string $template Current theme's directory name.
291          */
292         return apply_filters( 'template', get_option( 'template' ) );
293 }
294
295 /**
296  * Retrieve current theme directory.
297  *
298  * @since 1.5.0
299  *
300  * @return string Template directory path.
301  */
302 function get_template_directory() {
303         $template = get_template();
304         $theme_root = get_theme_root( $template );
305         $template_dir = "$theme_root/$template";
306
307         /**
308          * Filter the current theme directory path.
309          *
310          * @since 1.5.0
311          *
312          * @param string $template_dir The URI of the current theme directory.
313          * @param string $template     Directory name of the current theme.
314          * @param string $theme_root   Absolute path to the themes directory.
315          */
316         return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
317 }
318
319 /**
320  * Retrieve theme directory URI.
321  *
322  * @since 1.5.0
323  *
324  * @return string Template directory URI.
325  */
326 function get_template_directory_uri() {
327         $template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
328         $theme_root_uri = get_theme_root_uri( $template );
329         $template_dir_uri = "$theme_root_uri/$template";
330
331         /**
332          * Filter the current theme directory URI.
333          *
334          * @since 1.5.0
335          *
336          * @param string $template_dir_uri The URI of the current theme directory.
337          * @param string $template         Directory name of the current theme.
338          * @param string $theme_root_uri   The themes root URI.
339          */
340         return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
341 }
342
343 /**
344  * Retrieve theme roots.
345  *
346  * @since 2.9.0
347  *
348  * @global array $wp_theme_directories
349  *
350  * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
351  */
352 function get_theme_roots() {
353         global $wp_theme_directories;
354
355         if ( count($wp_theme_directories) <= 1 )
356                 return '/themes';
357
358         $theme_roots = get_site_transient( 'theme_roots' );
359         if ( false === $theme_roots ) {
360                 search_theme_directories( true ); // Regenerate the transient.
361                 $theme_roots = get_site_transient( 'theme_roots' );
362         }
363         return $theme_roots;
364 }
365
366 /**
367  * Register a directory that contains themes.
368  *
369  * @since 2.9.0
370  *
371  * @global array $wp_theme_directories
372  *
373  * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
374  * @return bool
375  */
376 function register_theme_directory( $directory ) {
377         global $wp_theme_directories;
378
379         if ( ! file_exists( $directory ) ) {
380                 // Try prepending as the theme directory could be relative to the content directory
381                 $directory = WP_CONTENT_DIR . '/' . $directory;
382                 // If this directory does not exist, return and do not register
383                 if ( ! file_exists( $directory ) ) {
384                         return false;
385                 }
386         }
387
388         if ( ! is_array( $wp_theme_directories ) ) {
389                 $wp_theme_directories = array();
390         }
391
392         $untrailed = untrailingslashit( $directory );
393         if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories ) ) {
394                 $wp_theme_directories[] = $untrailed;
395         }
396
397         return true;
398 }
399
400 /**
401  * Search all registered theme directories for complete and valid themes.
402  *
403  * @since 2.9.0
404  *
405  * @global array $wp_theme_directories
406  * @staticvar array $found_themes
407  *
408  * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
409  * @return array|false Valid themes found
410  */
411 function search_theme_directories( $force = false ) {
412         global $wp_theme_directories;
413         static $found_themes = null;
414
415         if ( empty( $wp_theme_directories ) )
416                 return false;
417
418         if ( ! $force && isset( $found_themes ) )
419                 return $found_themes;
420
421         $found_themes = array();
422
423         $wp_theme_directories = (array) $wp_theme_directories;
424         $relative_theme_roots = array();
425
426         // Set up maybe-relative, maybe-absolute array of theme directories.
427         // We always want to return absolute, but we need to cache relative
428         // to use in get_theme_root().
429         foreach ( $wp_theme_directories as $theme_root ) {
430                 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
431                         $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
432                 else
433                         $relative_theme_roots[ $theme_root ] = $theme_root;
434         }
435
436         /**
437          * Filter whether to get the cache of the registered theme directories.
438          *
439          * @since 3.4.0
440          *
441          * @param bool   $cache_expiration Whether to get the cache of the theme directories. Default false.
442          * @param string $cache_directory  Directory to be searched for the cache.
443          */
444         if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
445                 $cached_roots = get_site_transient( 'theme_roots' );
446                 if ( is_array( $cached_roots ) ) {
447                         foreach ( $cached_roots as $theme_dir => $theme_root ) {
448                                 // A cached theme root is no longer around, so skip it.
449                                 if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
450                                         continue;
451                                 $found_themes[ $theme_dir ] = array(
452                                         'theme_file' => $theme_dir . '/style.css',
453                                         'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
454                                 );
455                         }
456                         return $found_themes;
457                 }
458                 if ( ! is_int( $cache_expiration ) )
459                         $cache_expiration = 1800; // half hour
460         } else {
461                 $cache_expiration = 1800; // half hour
462         }
463
464         /* Loop the registered theme directories and extract all themes */
465         foreach ( $wp_theme_directories as $theme_root ) {
466
467                 // Start with directories in the root of the current theme directory.
468                 $dirs = @ scandir( $theme_root );
469                 if ( ! $dirs ) {
470                         trigger_error( "$theme_root is not readable", E_USER_NOTICE );
471                         continue;
472                 }
473                 foreach ( $dirs as $dir ) {
474                         if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
475                                 continue;
476                         if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
477                                 // wp-content/themes/a-single-theme
478                                 // wp-content/themes is $theme_root, a-single-theme is $dir
479                                 $found_themes[ $dir ] = array(
480                                         'theme_file' => $dir . '/style.css',
481                                         'theme_root' => $theme_root,
482                                 );
483                         } else {
484                                 $found_theme = false;
485                                 // wp-content/themes/a-folder-of-themes/*
486                                 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
487                                 $sub_dirs = @ scandir( $theme_root . '/' . $dir );
488                                 if ( ! $sub_dirs ) {
489                                         trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
490                                         continue;
491                                 }
492                                 foreach ( $sub_dirs as $sub_dir ) {
493                                         if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
494                                                 continue;
495                                         if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
496                                                 continue;
497                                         $found_themes[ $dir . '/' . $sub_dir ] = array(
498                                                 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
499                                                 'theme_root' => $theme_root,
500                                         );
501                                         $found_theme = true;
502                                 }
503                                 // Never mind the above, it's just a theme missing a style.css.
504                                 // Return it; WP_Theme will catch the error.
505                                 if ( ! $found_theme )
506                                         $found_themes[ $dir ] = array(
507                                                 'theme_file' => $dir . '/style.css',
508                                                 'theme_root' => $theme_root,
509                                         );
510                         }
511                 }
512         }
513
514         asort( $found_themes );
515
516         $theme_roots = array();
517         $relative_theme_roots = array_flip( $relative_theme_roots );
518
519         foreach ( $found_themes as $theme_dir => $theme_data ) {
520                 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
521         }
522
523         if ( $theme_roots != get_site_transient( 'theme_roots' ) )
524                 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
525
526         return $found_themes;
527 }
528
529 /**
530  * Retrieve path to themes directory.
531  *
532  * Does not have trailing slash.
533  *
534  * @since 1.5.0
535  *
536  * @global array $wp_theme_directories
537  *
538  * @param string $stylesheet_or_template The stylesheet or template name of the theme
539  * @return string Theme path.
540  */
541 function get_theme_root( $stylesheet_or_template = false ) {
542         global $wp_theme_directories;
543
544         if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
545                 // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
546                 // This gives relative theme roots the benefit of the doubt when things go haywire.
547                 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
548                         $theme_root = WP_CONTENT_DIR . $theme_root;
549         } else {
550                 $theme_root = WP_CONTENT_DIR . '/themes';
551         }
552
553         /**
554          * Filter the absolute path to the themes directory.
555          *
556          * @since 1.5.0
557          *
558          * @param string $theme_root Absolute path to themes directory.
559          */
560         return apply_filters( 'theme_root', $theme_root );
561 }
562
563 /**
564  * Retrieve URI for themes directory.
565  *
566  * Does not have trailing slash.
567  *
568  * @since 1.5.0
569  *
570  * @global array $wp_theme_directories
571  *
572  * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
573  *                                           Default is to leverage the main theme root.
574  * @param string $theme_root             Optional. The theme root for which calculations will be based, preventing
575  *                                           the need for a get_raw_theme_root() call.
576  * @return string Themes URI.
577  */
578 function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
579         global $wp_theme_directories;
580
581         if ( $stylesheet_or_template && ! $theme_root )
582                 $theme_root = get_raw_theme_root( $stylesheet_or_template );
583
584         if ( $stylesheet_or_template && $theme_root ) {
585                 if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
586                         // Absolute path. Make an educated guess. YMMV -- but note the filter below.
587                         if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
588                                 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
589                         elseif ( 0 === strpos( $theme_root, ABSPATH ) )
590                                 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
591                         elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
592                                 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
593                         else
594                                 $theme_root_uri = $theme_root;
595                 } else {
596                         $theme_root_uri = content_url( $theme_root );
597                 }
598         } else {
599                 $theme_root_uri = content_url( 'themes' );
600         }
601
602         /**
603          * Filter the URI for themes directory.
604          *
605          * @since 1.5.0
606          *
607          * @param string $theme_root_uri         The URI for themes directory.
608          * @param string $siteurl                WordPress web address which is set in General Options.
609          * @param string $stylesheet_or_template Stylesheet or template name of the theme.
610          */
611         return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
612 }
613
614 /**
615  * Get the raw theme root relative to the content directory with no filters applied.
616  *
617  * @since 3.1.0
618  *
619  * @global array $wp_theme_directories
620  *
621  * @param string $stylesheet_or_template The stylesheet or template name of the theme
622  * @param bool   $skip_cache             Optional. Whether to skip the cache.
623  *                                       Defaults to false, meaning the cache is used.
624  * @return string Theme root
625  */
626 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
627         global $wp_theme_directories;
628
629         if ( count($wp_theme_directories) <= 1 )
630                 return '/themes';
631
632         $theme_root = false;
633
634         // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
635         if ( ! $skip_cache ) {
636                 if ( get_option('stylesheet') == $stylesheet_or_template )
637                         $theme_root = get_option('stylesheet_root');
638                 elseif ( get_option('template') == $stylesheet_or_template )
639                         $theme_root = get_option('template_root');
640         }
641
642         if ( empty($theme_root) ) {
643                 $theme_roots = get_theme_roots();
644                 if ( !empty($theme_roots[$stylesheet_or_template]) )
645                         $theme_root = $theme_roots[$stylesheet_or_template];
646         }
647
648         return $theme_root;
649 }
650
651 /**
652  * Display localized stylesheet link element.
653  *
654  * @since 2.1.0
655  */
656 function locale_stylesheet() {
657         $stylesheet = get_locale_stylesheet_uri();
658         if ( empty($stylesheet) )
659                 return;
660         echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
661 }
662
663 /**
664  * Switches the theme.
665  *
666  * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
667  * of two arguments: $template then $stylesheet. This is for backwards compatibility.
668  *
669  * @since 2.5.0
670  *
671  * @global array                $wp_theme_directories
672  * @global WP_Customize_Manager $wp_customize
673  * @global array                $sidebars_widgets
674  *
675  * @param string $stylesheet Stylesheet name
676  */
677 function switch_theme( $stylesheet ) {
678         global $wp_theme_directories, $wp_customize, $sidebars_widgets;
679
680         $_sidebars_widgets = null;
681         if ( 'wp_ajax_customize_save' === current_action() ) {
682                 $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
683         } elseif ( is_array( $sidebars_widgets ) ) {
684                 $_sidebars_widgets = $sidebars_widgets;
685         }
686
687         if ( is_array( $_sidebars_widgets ) ) {
688                 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
689         }
690
691         $old_theme  = wp_get_theme();
692         $new_theme = wp_get_theme( $stylesheet );
693
694         if ( func_num_args() > 1 ) {
695                 $template = $stylesheet;
696                 $stylesheet = func_get_arg( 1 );
697         } else {
698                 $template = $new_theme->get_template();
699         }
700
701         update_option( 'template', $template );
702         update_option( 'stylesheet', $stylesheet );
703
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
712         $new_name  = $new_theme->get('Name');
713
714         update_option( 'current_theme', $new_name );
715
716         // Migrate from the old mods_{name} option to theme_mods_{slug}.
717         if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
718                 $default_theme_mods = (array) get_option( 'mods_' . $new_name );
719                 add_option( "theme_mods_$stylesheet", $default_theme_mods );
720         } else {
721                 /*
722                  * Since retrieve_widgets() is called when initializing a theme in the Customizer,
723                  * we need to to remove the theme mods to avoid overwriting changes made via
724                  * the Customizer when accessing wp-admin/widgets.php.
725                  */
726                 if ( 'wp_ajax_customize_save' === current_action() ) {
727                         remove_theme_mod( 'sidebars_widgets' );
728                 }
729         }
730
731         update_option( 'theme_switched', $old_theme->get_stylesheet() );
732         /**
733          * Fires after the theme is switched.
734          *
735          * @since 1.5.0
736          *
737          * @param string   $new_name  Name of the new theme.
738          * @param WP_Theme $new_theme WP_Theme instance of the new theme.
739          */
740         do_action( 'switch_theme', $new_name, $new_theme );
741 }
742
743 /**
744  * Checks that current theme files 'index.php' and 'style.css' exists.
745  *
746  * Does not check the default theme, which is the fallback and should always exist.
747  * Will switch theme to the fallback theme if current theme does not validate.
748  * You can use the 'validate_current_theme' filter to return false to
749  * disable this functionality.
750  *
751  * @since 1.5.0
752  * @see WP_DEFAULT_THEME
753  *
754  * @return bool
755  */
756 function validate_current_theme() {
757         /**
758          * Filter whether to validate the current theme.
759          *
760          * @since 2.7.0
761          *
762          * @param bool true Validation flag to check the current theme.
763          */
764         if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) )
765                 return true;
766
767         if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
768                 switch_theme( WP_DEFAULT_THEME );
769                 return false;
770         }
771
772         if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
773                 switch_theme( WP_DEFAULT_THEME );
774                 return false;
775         }
776
777         if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
778                 switch_theme( WP_DEFAULT_THEME );
779                 return false;
780         }
781
782         return true;
783 }
784
785 /**
786  * Retrieve all theme modifications.
787  *
788  * @since 3.1.0
789  *
790  * @return array|void Theme modifications.
791  */
792 function get_theme_mods() {
793         $theme_slug = get_option( 'stylesheet' );
794         $mods = get_option( "theme_mods_$theme_slug" );
795         if ( false === $mods ) {
796                 $theme_name = get_option( 'current_theme' );
797                 if ( false === $theme_name )
798                         $theme_name = wp_get_theme()->get('Name');
799                 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
800                 if ( is_admin() && false !== $mods ) {
801                         update_option( "theme_mods_$theme_slug", $mods );
802                         delete_option( "mods_$theme_name" );
803                 }
804         }
805         return $mods;
806 }
807
808 /**
809  * Retrieve theme modification value for the current theme.
810  *
811  * If the modification name does not exist, then the $default will be passed
812  * through {@link http://php.net/sprintf sprintf()} PHP function with the first
813  * string the template directory URI and the second string the stylesheet
814  * directory URI.
815  *
816  * @since 2.1.0
817  *
818  * @param string      $name    Theme modification name.
819  * @param bool|string $default
820  * @return string
821  */
822 function get_theme_mod( $name, $default = false ) {
823         $mods = get_theme_mods();
824
825         if ( isset( $mods[$name] ) ) {
826                 /**
827                  * Filter the theme modification, or 'theme_mod', value.
828                  *
829                  * The dynamic portion of the hook name, `$name`, refers to
830                  * the key name of the modification array. For example,
831                  * 'header_textcolor', 'header_image', and so on depending
832                  * on the theme options.
833                  *
834                  * @since 2.2.0
835                  *
836                  * @param string $current_mod The value of the current theme modification.
837                  */
838                 return apply_filters( "theme_mod_{$name}", $mods[$name] );
839         }
840
841         if ( is_string( $default ) )
842                 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
843
844         /** This filter is documented in wp-includes/theme.php */
845         return apply_filters( "theme_mod_{$name}", $default );
846 }
847
848 /**
849  * Update theme modification value for the current theme.
850  *
851  * @since 2.1.0
852  *
853  * @param string $name  Theme modification name.
854  * @param mixed  $value Theme modification value.
855  */
856 function set_theme_mod( $name, $value ) {
857         $mods = get_theme_mods();
858         $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
859
860         /**
861          * Filter the theme mod value on save.
862          *
863          * The dynamic portion of the hook name, `$name`, refers to the key name of
864          * the modification array. For example, 'header_textcolor', 'header_image',
865          * and so on depending on the theme options.
866          *
867          * @since 3.9.0
868          *
869          * @param string $value     The new value of the theme mod.
870          * @param string $old_value The current value of the theme mod.
871          */
872         $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value );
873
874         $theme = get_option( 'stylesheet' );
875         update_option( "theme_mods_$theme", $mods );
876 }
877
878 /**
879  * Remove theme modification name from current theme list.
880  *
881  * If removing the name also removes all elements, then the entire option will
882  * be removed.
883  *
884  * @since 2.1.0
885  *
886  * @param string $name Theme modification name.
887  */
888 function remove_theme_mod( $name ) {
889         $mods = get_theme_mods();
890
891         if ( ! isset( $mods[ $name ] ) )
892                 return;
893
894         unset( $mods[ $name ] );
895
896         if ( empty( $mods ) ) {
897                 remove_theme_mods();
898                 return;
899         }
900         $theme = get_option( 'stylesheet' );
901         update_option( "theme_mods_$theme", $mods );
902 }
903
904 /**
905  * Remove theme modifications option for current theme.
906  *
907  * @since 2.1.0
908  */
909 function remove_theme_mods() {
910         delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
911
912         // Old style.
913         $theme_name = get_option( 'current_theme' );
914         if ( false === $theme_name )
915                 $theme_name = wp_get_theme()->get('Name');
916         delete_option( 'mods_' . $theme_name );
917 }
918
919 /**
920  * Retrieve text color for custom header.
921  *
922  * @since 2.1.0
923  *
924  * @return string
925  */
926 function get_header_textcolor() {
927         return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
928 }
929
930 /**
931  * Display text color for custom header.
932  *
933  * @since 2.1.0
934  */
935 function header_textcolor() {
936         echo get_header_textcolor();
937 }
938
939 /**
940  * Whether to display the header text.
941  *
942  * @since 3.4.0
943  *
944  * @return bool
945  */
946 function display_header_text() {
947         if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
948                 return false;
949
950         $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
951         return 'blank' !== $text_color;
952 }
953
954 /**
955  * Check whether a header image is set or not.
956  *
957  * @since 4.2.0
958  *
959  * @see get_header_image()
960  *
961  * @return bool Whether a header image is set or not.
962  */
963 function has_header_image() {
964         return (bool) get_header_image();
965 }
966
967 /**
968  * Retrieve header image for custom header.
969  *
970  * @since 2.1.0
971  *
972  * @return string|false
973  */
974 function get_header_image() {
975         $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
976
977         if ( 'remove-header' == $url )
978                 return false;
979
980         if ( is_random_header_image() )
981                 $url = get_random_header_image();
982
983         return esc_url_raw( set_url_scheme( $url ) );
984 }
985
986 /**
987  * Get random header image data from registered images in theme.
988  *
989  * @since 3.4.0
990  *
991  * @access private
992  *
993  * @global array  $_wp_default_headers
994  * @staticvar object $_wp_random_header
995  *
996  * @return object
997  */
998 function _get_random_header_data() {
999         static $_wp_random_header = null;
1000
1001         if ( empty( $_wp_random_header ) ) {
1002                 global $_wp_default_headers;
1003                 $header_image_mod = get_theme_mod( 'header_image', '' );
1004                 $headers = array();
1005
1006                 if ( 'random-uploaded-image' == $header_image_mod )
1007                         $headers = get_uploaded_header_images();
1008                 elseif ( ! empty( $_wp_default_headers ) ) {
1009                         if ( 'random-default-image' == $header_image_mod ) {
1010                                 $headers = $_wp_default_headers;
1011                         } else {
1012                                 if ( current_theme_supports( 'custom-header', 'random-default' ) )
1013                                         $headers = $_wp_default_headers;
1014                         }
1015                 }
1016
1017                 if ( empty( $headers ) )
1018                         return new stdClass;
1019
1020                 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
1021
1022                 $_wp_random_header->url =  sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1023                 $_wp_random_header->thumbnail_url =  sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1024         }
1025         return $_wp_random_header;
1026 }
1027
1028 /**
1029  * Get random header image url from registered images in theme.
1030  *
1031  * @since 3.2.0
1032  *
1033  * @return string Path to header image
1034  */
1035 function get_random_header_image() {
1036         $random_image = _get_random_header_data();
1037         if ( empty( $random_image->url ) )
1038                 return '';
1039         return $random_image->url;
1040 }
1041
1042 /**
1043  * Check if random header image is in use.
1044  *
1045  * Always true if user expressly chooses the option in Appearance > Header.
1046  * Also true if theme has multiple header images registered, no specific header image
1047  * is chosen, and theme turns on random headers with add_theme_support().
1048  *
1049  * @since 3.2.0
1050  *
1051  * @param string $type The random pool to use. any|default|uploaded
1052  * @return bool
1053  */
1054 function is_random_header_image( $type = 'any' ) {
1055         $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
1056
1057         if ( 'any' == $type ) {
1058                 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
1059                         return true;
1060         } else {
1061                 if ( "random-$type-image" == $header_image_mod )
1062                         return true;
1063                 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
1064                         return true;
1065         }
1066
1067         return false;
1068 }
1069
1070 /**
1071  * Display header image URL.
1072  *
1073  * @since 2.1.0
1074  */
1075 function header_image() {
1076         $image = get_header_image();
1077         if ( $image ) {
1078                 echo esc_url( $image );
1079         }
1080 }
1081
1082 /**
1083  * Get the header images uploaded for the current theme.
1084  *
1085  * @since 3.2.0
1086  *
1087  * @return array
1088  */
1089 function get_uploaded_header_images() {
1090         $header_images = array();
1091
1092         // @todo caching
1093         $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1094
1095         if ( empty( $headers ) )
1096                 return array();
1097
1098         foreach ( (array) $headers as $header ) {
1099                 $url = esc_url_raw( wp_get_attachment_url( $header->ID ) );
1100                 $header_data = wp_get_attachment_metadata( $header->ID );
1101                 $header_index = basename($url);
1102
1103                 $header_images[$header_index] = array();
1104                 $header_images[$header_index]['attachment_id'] = $header->ID;
1105                 $header_images[$header_index]['url'] =  $url;
1106                 $header_images[$header_index]['thumbnail_url'] = $url;
1107                 $header_images[$header_index]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
1108
1109                 if ( isset( $header_data['width'] ) )
1110                         $header_images[$header_index]['width'] = $header_data['width'];
1111                 if ( isset( $header_data['height'] ) )
1112                         $header_images[$header_index]['height'] = $header_data['height'];
1113         }
1114
1115         return $header_images;
1116 }
1117
1118 /**
1119  * Get the header image data.
1120  *
1121  * @since 3.4.0
1122  *
1123  * @global array $_wp_default_headers
1124  *
1125  * @return object
1126  */
1127 function get_custom_header() {
1128         global $_wp_default_headers;
1129
1130         if ( is_random_header_image() ) {
1131                 $data = _get_random_header_data();
1132         } else {
1133                 $data = get_theme_mod( 'header_image_data' );
1134                 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
1135                         $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
1136                         $data = array();
1137                         $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
1138                         if ( ! empty( $_wp_default_headers ) ) {
1139                                 foreach ( (array) $_wp_default_headers as $default_header ) {
1140                                         $url = vsprintf( $default_header['url'], $directory_args );
1141                                         if ( $data['url'] == $url ) {
1142                                                 $data = $default_header;
1143                                                 $data['url'] = $url;
1144                                                 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
1145                                                 break;
1146                                         }
1147                                 }
1148                         }
1149                 }
1150         }
1151
1152         $default = array(
1153                 'url'           => '',
1154                 'thumbnail_url' => '',
1155                 'width'         => get_theme_support( 'custom-header', 'width' ),
1156                 'height'        => get_theme_support( 'custom-header', 'height' ),
1157         );
1158         return (object) wp_parse_args( $data, $default );
1159 }
1160
1161 /**
1162  * Register a selection of default headers to be displayed by the custom header admin UI.
1163  *
1164  * @since 3.0.0
1165  *
1166  * @global array $_wp_default_headers
1167  *
1168  * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1169  */
1170 function register_default_headers( $headers ) {
1171         global $_wp_default_headers;
1172
1173         $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1174 }
1175
1176 /**
1177  * Unregister default headers.
1178  *
1179  * This function must be called after register_default_headers() has already added the
1180  * header you want to remove.
1181  *
1182  * @see register_default_headers()
1183  * @since 3.0.0
1184  *
1185  * @global array $_wp_default_headers
1186  *
1187  * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1188  * @return bool|void A single header returns true on success, false on failure.
1189  *                   There is currently no return value for multiple headers.
1190  */
1191 function unregister_default_headers( $header ) {
1192         global $_wp_default_headers;
1193         if ( is_array( $header ) ) {
1194                 array_map( 'unregister_default_headers', $header );
1195         } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1196                 unset( $_wp_default_headers[ $header ] );
1197                 return true;
1198         } else {
1199                 return false;
1200         }
1201 }
1202
1203 /**
1204  * Retrieve background image for custom background.
1205  *
1206  * @since 3.0.0
1207  *
1208  * @return string
1209  */
1210 function get_background_image() {
1211         return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1212 }
1213
1214 /**
1215  * Display background image path.
1216  *
1217  * @since 3.0.0
1218  */
1219 function background_image() {
1220         echo get_background_image();
1221 }
1222
1223 /**
1224  * Retrieve value for custom background color.
1225  *
1226  * @since 3.0.0
1227  *
1228  * @return string
1229  */
1230 function get_background_color() {
1231         return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1232 }
1233
1234 /**
1235  * Display background color value.
1236  *
1237  * @since 3.0.0
1238  */
1239 function background_color() {
1240         echo get_background_color();
1241 }
1242
1243 /**
1244  * Default custom background callback.
1245  *
1246  * @since 3.0.0
1247  * @access protected
1248  */
1249 function _custom_background_cb() {
1250         // $background is the saved custom image, or the default image.
1251         $background = set_url_scheme( get_background_image() );
1252
1253         // $color is the saved custom color.
1254         // A default has to be specified in style.css. It will not be printed here.
1255         $color = get_background_color();
1256
1257         if ( $color === get_theme_support( 'custom-background', 'default-color' ) ) {
1258                 $color = false;
1259         }
1260
1261         if ( ! $background && ! $color )
1262                 return;
1263
1264         $style = $color ? "background-color: #$color;" : '';
1265
1266         if ( $background ) {
1267                 $image = " background-image: url('$background');";
1268
1269                 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
1270                 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1271                         $repeat = 'repeat';
1272                 $repeat = " background-repeat: $repeat;";
1273
1274                 $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
1275                 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1276                         $position = 'left';
1277                 $position = " background-position: top $position;";
1278
1279                 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
1280                 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1281                         $attachment = 'scroll';
1282                 $attachment = " background-attachment: $attachment;";
1283
1284                 $style .= $image . $repeat . $position . $attachment;
1285         }
1286 ?>
1287 <style type="text/css" id="custom-background-css">
1288 body.custom-background { <?php echo trim( $style ); ?> }
1289 </style>
1290 <?php
1291 }
1292
1293 /**
1294  * Add callback for custom TinyMCE editor stylesheets.
1295  *
1296  * The parameter $stylesheet is the name of the stylesheet, relative to
1297  * the theme root. It also accepts an array of stylesheets.
1298  * It is optional and defaults to 'editor-style.css'.
1299  *
1300  * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1301  * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1302  * If an array of stylesheets is passed to add_editor_style(),
1303  * RTL is only added for the first stylesheet.
1304  *
1305  * Since version 3.4 the TinyMCE body has .rtl CSS class.
1306  * It is a better option to use that class and add any RTL styles to the main stylesheet.
1307  *
1308  * @since 3.0.0
1309  *
1310  * @global array $editor_styles
1311  *
1312  * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1313  *                                     Defaults to 'editor-style.css'
1314  */
1315 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1316         add_theme_support( 'editor-style' );
1317
1318         if ( ! is_admin() )
1319                 return;
1320
1321         global $editor_styles;
1322         $editor_styles = (array) $editor_styles;
1323         $stylesheet    = (array) $stylesheet;
1324         if ( is_rtl() ) {
1325                 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1326                 $stylesheet[] = $rtl_stylesheet;
1327         }
1328
1329         $editor_styles = array_merge( $editor_styles, $stylesheet );
1330 }
1331
1332 /**
1333  * Removes all visual editor stylesheets.
1334  *
1335  * @since 3.1.0
1336  *
1337  * @global array $editor_styles
1338  *
1339  * @return bool True on success, false if there were no stylesheets to remove.
1340  */
1341 function remove_editor_styles() {
1342         if ( ! current_theme_supports( 'editor-style' ) )
1343                 return false;
1344         _remove_theme_support( 'editor-style' );
1345         if ( is_admin() )
1346                 $GLOBALS['editor_styles'] = array();
1347         return true;
1348 }
1349
1350 /**
1351  * Retrieve any registered editor stylesheets
1352  *
1353  * @since 4.0.0
1354  *
1355  * @global array $editor_styles Registered editor stylesheets
1356  *
1357  * @return array If registered, a list of editor stylesheet URLs.
1358  */
1359 function get_editor_stylesheets() {
1360         $stylesheets = array();
1361         // load editor_style.css if the current theme supports it
1362         if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
1363                 $editor_styles = $GLOBALS['editor_styles'];
1364
1365                 $editor_styles = array_unique( array_filter( $editor_styles ) );
1366                 $style_uri = get_stylesheet_directory_uri();
1367                 $style_dir = get_stylesheet_directory();
1368
1369                 // Support externally referenced styles (like, say, fonts).
1370                 foreach ( $editor_styles as $key => $file ) {
1371                         if ( preg_match( '~^(https?:)?//~', $file ) ) {
1372                                 $stylesheets[] = esc_url_raw( $file );
1373                                 unset( $editor_styles[ $key ] );
1374                         }
1375                 }
1376
1377                 // Look in a parent theme first, that way child theme CSS overrides.
1378                 if ( is_child_theme() ) {
1379                         $template_uri = get_template_directory_uri();
1380                         $template_dir = get_template_directory();
1381
1382                         foreach ( $editor_styles as $key => $file ) {
1383                                 if ( $file && file_exists( "$template_dir/$file" ) ) {
1384                                         $stylesheets[] = "$template_uri/$file";
1385                                 }
1386                         }
1387                 }
1388
1389                 foreach ( $editor_styles as $file ) {
1390                         if ( $file && file_exists( "$style_dir/$file" ) ) {
1391                                 $stylesheets[] = "$style_uri/$file";
1392                         }
1393                 }
1394         }
1395
1396         /**
1397          * Filter the array of stylesheets applied to the editor.
1398          *
1399          * @since 4.3.0
1400          *
1401          * @param array $stylesheets Array of stylesheets to be applied to the editor.
1402          */
1403         return apply_filters( 'editor_stylesheets', $stylesheets );
1404 }
1405
1406 /**
1407  * Allows a theme to register its support of a certain feature
1408  *
1409  * Must be called in the theme's functions.php file to work.
1410  * If attached to a hook, it must be after_setup_theme.
1411  * The init hook may be too late for some features.
1412  *
1413  * @since 2.9.0
1414  *
1415  * @global array $_wp_theme_features
1416  *
1417  * @param string $feature The feature being added.
1418  * @return void|bool False on failure, void otherwise.
1419  */
1420 function add_theme_support( $feature ) {
1421         global $_wp_theme_features;
1422
1423         if ( func_num_args() == 1 )
1424                 $args = true;
1425         else
1426                 $args = array_slice( func_get_args(), 1 );
1427
1428         switch ( $feature ) {
1429                 case 'post-formats' :
1430                         if ( is_array( $args[0] ) ) {
1431                                 $post_formats = get_post_format_slugs();
1432                                 unset( $post_formats['standard'] );
1433
1434                                 $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
1435                         }
1436                         break;
1437
1438                 case 'html5' :
1439                         // You can't just pass 'html5', you need to pass an array of types.
1440                         if ( empty( $args[0] ) ) {
1441                                 // Build an array of types for back-compat.
1442                                 $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
1443                         } elseif ( ! is_array( $args[0] ) ) {
1444                                 _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );
1445                                 return false;
1446                         }
1447
1448                         // Calling 'html5' again merges, rather than overwrites.
1449                         if ( isset( $_wp_theme_features['html5'] ) )
1450                                 $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
1451                         break;
1452
1453                 case 'custom-header-uploads' :
1454                         return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1455
1456                 case 'custom-header' :
1457                         if ( ! is_array( $args ) )
1458                                 $args = array( 0 => array() );
1459
1460                         $defaults = array(
1461                                 'default-image' => '',
1462                                 'random-default' => false,
1463                                 'width' => 0,
1464                                 'height' => 0,
1465                                 'flex-height' => false,
1466                                 'flex-width' => false,
1467                                 'default-text-color' => '',
1468                                 'header-text' => true,
1469                                 'uploads' => true,
1470                                 'wp-head-callback' => '',
1471                                 'admin-head-callback' => '',
1472                                 'admin-preview-callback' => '',
1473                         );
1474
1475                         $jit = isset( $args[0]['__jit'] );
1476                         unset( $args[0]['__jit'] );
1477
1478                         // Merge in data from previous add_theme_support() calls.
1479                         // The first value registered wins. (A child theme is set up first.)
1480                         if ( isset( $_wp_theme_features['custom-header'] ) )
1481                                 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1482
1483                         // Load in the defaults at the end, as we need to insure first one wins.
1484                         // This will cause all constants to be defined, as each arg will then be set to the default.
1485                         if ( $jit )
1486                                 $args[0] = wp_parse_args( $args[0], $defaults );
1487
1488                         // If a constant was defined, use that value. Otherwise, define the constant to ensure
1489                         // the constant is always accurate (and is not defined later,  overriding our value).
1490                         // As stated above, the first value wins.
1491                         // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1492                         // Constants are lame. Don't reference them. This is just for backwards compatibility.
1493
1494                         if ( defined( 'NO_HEADER_TEXT' ) )
1495                                 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1496                         elseif ( isset( $args[0]['header-text'] ) )
1497                                 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1498
1499                         if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1500                                 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1501                         elseif ( isset( $args[0]['width'] ) )
1502                                 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1503
1504                         if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1505                                 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1506                         elseif ( isset( $args[0]['height'] ) )
1507                                 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1508
1509                         if ( defined( 'HEADER_TEXTCOLOR' ) )
1510                                 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1511                         elseif ( isset( $args[0]['default-text-color'] ) )
1512                                 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1513
1514                         if ( defined( 'HEADER_IMAGE' ) )
1515                                 $args[0]['default-image'] = HEADER_IMAGE;
1516                         elseif ( isset( $args[0]['default-image'] ) )
1517                                 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1518
1519                         if ( $jit && ! empty( $args[0]['default-image'] ) )
1520                                 $args[0]['random-default'] = false;
1521
1522                         // If headers are supported, and we still don't have a defined width or height,
1523                         // we have implicit flex sizes.
1524                         if ( $jit ) {
1525                                 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1526                                         $args[0]['flex-width'] = true;
1527                                 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1528                                         $args[0]['flex-height'] = true;
1529                         }
1530
1531                         break;
1532
1533                 case 'custom-background' :
1534                         if ( ! is_array( $args ) )
1535                                 $args = array( 0 => array() );
1536
1537                         $defaults = array(
1538                                 'default-image'          => '',
1539                                 'default-repeat'         => 'repeat',
1540                                 'default-position-x'     => 'left',
1541                                 'default-attachment'     => 'scroll',
1542                                 'default-color'          => '',
1543                                 'wp-head-callback'       => '_custom_background_cb',
1544                                 'admin-head-callback'    => '',
1545                                 'admin-preview-callback' => '',
1546                         );
1547
1548                         $jit = isset( $args[0]['__jit'] );
1549                         unset( $args[0]['__jit'] );
1550
1551                         // Merge in data from previous add_theme_support() calls. The first value registered wins.
1552                         if ( isset( $_wp_theme_features['custom-background'] ) )
1553                                 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1554
1555                         if ( $jit )
1556                                 $args[0] = wp_parse_args( $args[0], $defaults );
1557
1558                         if ( defined( 'BACKGROUND_COLOR' ) )
1559                                 $args[0]['default-color'] = BACKGROUND_COLOR;
1560                         elseif ( isset( $args[0]['default-color'] ) || $jit )
1561                                 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1562
1563                         if ( defined( 'BACKGROUND_IMAGE' ) )
1564                                 $args[0]['default-image'] = BACKGROUND_IMAGE;
1565                         elseif ( isset( $args[0]['default-image'] ) || $jit )
1566                                 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1567
1568                         break;
1569
1570                 // Ensure that 'title-tag' is accessible in the admin.
1571                 case 'title-tag' :
1572                         // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
1573                         if ( did_action( 'wp_loaded' ) ) {
1574                                 /* translators: 1: Theme support 2: hook name */
1575                                 _doing_it_wrong( "add_theme_support( 'title-tag' )", sprintf( __( 'Theme support for %1$s should be registered before the %2$s hook.' ),
1576                                         '<code>title-tag</code>', '<code>wp_loaded</code>' ), '4.1' );
1577
1578                                 return false;
1579                         }
1580         }
1581
1582         $_wp_theme_features[ $feature ] = $args;
1583 }
1584
1585 /**
1586  * Registers the internal custom header and background routines.
1587  *
1588  * @since 3.4.0
1589  * @access private
1590  *
1591  * @global Custom_Image_Header $custom_image_header
1592  * @global Custom_Background   $custom_background
1593  */
1594 function _custom_header_background_just_in_time() {
1595         global $custom_image_header, $custom_background;
1596
1597         if ( current_theme_supports( 'custom-header' ) ) {
1598                 // In case any constants were defined after an add_custom_image_header() call, re-run.
1599                 add_theme_support( 'custom-header', array( '__jit' => true ) );
1600
1601                 $args = get_theme_support( 'custom-header' );
1602                 if ( $args[0]['wp-head-callback'] )
1603                         add_action( 'wp_head', $args[0]['wp-head-callback'] );
1604
1605                 if ( is_admin() ) {
1606                         require_once( ABSPATH . 'wp-admin/custom-header.php' );
1607                         $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1608                 }
1609         }
1610
1611         if ( current_theme_supports( 'custom-background' ) ) {
1612                 // In case any constants were defined after an add_custom_background() call, re-run.
1613                 add_theme_support( 'custom-background', array( '__jit' => true ) );
1614
1615                 $args = get_theme_support( 'custom-background' );
1616                 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1617
1618                 if ( is_admin() ) {
1619                         require_once( ABSPATH . 'wp-admin/custom-background.php' );
1620                         $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1621                 }
1622         }
1623 }
1624
1625 /**
1626  * Gets the theme support arguments passed when registering that support
1627  *
1628  * @since 3.1.0
1629  *
1630  * @global array $_wp_theme_features
1631  *
1632  * @param string $feature the feature to check
1633  * @return mixed The array of extra arguments or the value for the registered feature.
1634  */
1635 function get_theme_support( $feature ) {
1636         global $_wp_theme_features;
1637         if ( ! isset( $_wp_theme_features[ $feature ] ) )
1638                 return false;
1639
1640         if ( func_num_args() <= 1 )
1641                 return $_wp_theme_features[ $feature ];
1642
1643         $args = array_slice( func_get_args(), 1 );
1644         switch ( $feature ) {
1645                 case 'custom-header' :
1646                 case 'custom-background' :
1647                         if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1648                                 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1649                         return false;
1650
1651                 default :
1652                         return $_wp_theme_features[ $feature ];
1653         }
1654 }
1655
1656 /**
1657  * Allows a theme to de-register its support of a certain feature
1658  *
1659  * Should be called in the theme's functions.php file. Generally would
1660  * be used for child themes to override support from the parent theme.
1661  *
1662  * @since 3.0.0
1663  * @see add_theme_support()
1664  * @param string $feature the feature being added
1665  * @return bool|void Whether feature was removed.
1666  */
1667 function remove_theme_support( $feature ) {
1668         // Blacklist: for internal registrations not used directly by themes.
1669         if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1670                 return false;
1671
1672         return _remove_theme_support( $feature );
1673 }
1674
1675 /**
1676  * Do not use. Removes theme support internally, ignorant of the blacklist.
1677  *
1678  * @access private
1679  * @since 3.1.0
1680  *
1681  * @global array               $_wp_theme_features
1682  * @global Custom_Image_Header $custom_image_header
1683  * @global Custom_Background   $custom_background
1684  *
1685  * @param string $feature
1686  */
1687 function _remove_theme_support( $feature ) {
1688         global $_wp_theme_features;
1689
1690         switch ( $feature ) {
1691                 case 'custom-header-uploads' :
1692                         if ( ! isset( $_wp_theme_features['custom-header'] ) )
1693                                 return false;
1694                         add_theme_support( 'custom-header', array( 'uploads' => false ) );
1695                         return; // Do not continue - custom-header-uploads no longer exists.
1696         }
1697
1698         if ( ! isset( $_wp_theme_features[ $feature ] ) )
1699                 return false;
1700
1701         switch ( $feature ) {
1702                 case 'custom-header' :
1703                         if ( ! did_action( 'wp_loaded' ) )
1704                                 break;
1705                         $support = get_theme_support( 'custom-header' );
1706                         if ( $support[0]['wp-head-callback'] )
1707                                 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1708                         remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1709                         unset( $GLOBALS['custom_image_header'] );
1710                         break;
1711
1712                 case 'custom-background' :
1713                         if ( ! did_action( 'wp_loaded' ) )
1714                                 break;
1715                         $support = get_theme_support( 'custom-background' );
1716                         remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1717                         remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1718                         unset( $GLOBALS['custom_background'] );
1719                         break;
1720         }
1721
1722         unset( $_wp_theme_features[ $feature ] );
1723         return true;
1724 }
1725
1726 /**
1727  * Checks a theme's support for a given feature
1728  *
1729  * @since 2.9.0
1730  *
1731  * @global array $_wp_theme_features
1732  *
1733  * @param string $feature the feature being checked
1734  * @return bool
1735  */
1736 function current_theme_supports( $feature ) {
1737         global $_wp_theme_features;
1738
1739         if ( 'custom-header-uploads' == $feature )
1740                 return current_theme_supports( 'custom-header', 'uploads' );
1741
1742         if ( !isset( $_wp_theme_features[$feature] ) )
1743                 return false;
1744
1745         if ( 'title-tag' == $feature ) {
1746                 // Don't confirm support unless called internally.
1747                 $trace = debug_backtrace();
1748                 if ( ! in_array( $trace[1]['function'], array( '_wp_render_title_tag', 'wp_title' ) ) ) {
1749                         return false;
1750                 }
1751         }
1752
1753         // If no args passed then no extra checks need be performed
1754         if ( func_num_args() <= 1 )
1755                 return true;
1756
1757         $args = array_slice( func_get_args(), 1 );
1758
1759         switch ( $feature ) {
1760                 case 'post-thumbnails':
1761                         // post-thumbnails can be registered for only certain content/post types by passing
1762                         // an array of types to add_theme_support(). If no array was passed, then
1763                         // any type is accepted
1764                         if ( true === $_wp_theme_features[$feature] )  // Registered for all types
1765                                 return true;
1766                         $content_type = $args[0];
1767                         return in_array( $content_type, $_wp_theme_features[$feature][0] );
1768
1769                 case 'html5':
1770                 case 'post-formats':
1771                         // specific post formats can be registered by passing an array of types to
1772                         // add_theme_support()
1773
1774                         // Specific areas of HTML5 support *must* be passed via an array to add_theme_support()
1775
1776                         $type = $args[0];
1777                         return in_array( $type, $_wp_theme_features[$feature][0] );
1778
1779                 case 'custom-header':
1780                 case 'custom-background' :
1781                         // specific custom header and background capabilities can be registered by passing
1782                         // an array to add_theme_support()
1783                         $header_support = $args[0];
1784                         return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1785         }
1786
1787         /**
1788          * Filter whether the current theme supports a specific feature.
1789          *
1790          * The dynamic portion of the hook name, `$feature`, refers to the specific theme
1791          * feature. Possible values include 'post-formats', 'post-thumbnails', 'custom-background',
1792          * 'custom-header', 'menus', 'automatic-feed-links', and 'html5'.
1793          *
1794          * @since 3.4.0
1795          *
1796          * @param bool   true     Whether the current theme supports the given feature. Default true.
1797          * @param array  $args    Array of arguments for the feature.
1798          * @param string $feature The theme feature.
1799          */
1800         return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[$feature] );
1801 }
1802
1803 /**
1804  * Checks a theme's support for a given feature before loading the functions which implement it.
1805  *
1806  * @since 2.9.0
1807  *
1808  * @param string $feature The feature being checked.
1809  * @param string $include Path to the file.
1810  * @return bool True if the current theme supports the supplied feature, false otherwise.
1811  */
1812 function require_if_theme_supports( $feature, $include ) {
1813         if ( current_theme_supports( $feature ) ) {
1814                 require ( $include );
1815                 return true;
1816         }
1817         return false;
1818 }
1819
1820 /**
1821  * Checks an attachment being deleted to see if it's a header or background image.
1822  *
1823  * If true it removes the theme modification which would be pointing at the deleted
1824  * attachment.
1825  *
1826  * @access private
1827  * @since 3.0.0
1828  * @since 4.3.0 Also removes `header_image_data`.
1829  *
1830  * @param int $id The attachment id.
1831  */
1832 function _delete_attachment_theme_mod( $id ) {
1833         $attachment_image = wp_get_attachment_url( $id );
1834         $header_image     = get_header_image();
1835         $background_image = get_background_image();
1836
1837         if ( $header_image && $header_image == $attachment_image ) {
1838                 remove_theme_mod( 'header_image' );
1839                 remove_theme_mod( 'header_image_data' );
1840         }
1841
1842         if ( $background_image && $background_image == $attachment_image ) {
1843                 remove_theme_mod( 'background_image' );
1844         }
1845 }
1846
1847 /**
1848  * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1849  *
1850  * @since 3.3.0
1851  */
1852 function check_theme_switched() {
1853         if ( $stylesheet = get_option( 'theme_switched' ) ) {
1854                 $old_theme = wp_get_theme( $stylesheet );
1855
1856                 // Prevent retrieve_widgets() from running since Customizer already called it up front
1857                 if ( get_option( 'theme_switched_via_customizer' ) ) {
1858                         remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
1859                         update_option( 'theme_switched_via_customizer', false );
1860                 }
1861
1862                 if ( $old_theme->exists() ) {
1863                         /**
1864                          * Fires on the first WP load after a theme switch if the old theme still exists.
1865                          *
1866                          * This action fires multiple times and the parameters differs
1867                          * according to the context, if the old theme exists or not.
1868                          * If the old theme is missing, the parameter will be the slug
1869                          * of the old theme.
1870                          *
1871                          * @since 3.3.0
1872                          *
1873                          * @param string   $old_name  Old theme name.
1874                          * @param WP_Theme $old_theme WP_Theme instance of the old theme.
1875                          */
1876                         do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
1877                 } else {
1878                         /** This action is documented in wp-includes/theme.php */
1879                         do_action( 'after_switch_theme', $stylesheet );
1880                 }
1881
1882                 update_option( 'theme_switched', false );
1883         }
1884 }
1885
1886 /**
1887  * Includes and instantiates the WP_Customize_Manager class.
1888  *
1889  * Fires when ?wp_customize=on or on wp-admin/customize.php.
1890  *
1891  * @since 3.4.0
1892  *
1893  * @global WP_Customize_Manager $wp_customize
1894  */
1895 function _wp_customize_include() {
1896         if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
1897                 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
1898         ) )
1899                 return;
1900
1901         require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
1902         $GLOBALS['wp_customize'] = new WP_Customize_Manager();
1903 }
1904
1905 /**
1906  * Adds settings for the customize-loader script.
1907  *
1908  * @since 3.4.0
1909  */
1910 function _wp_customize_loader_settings() {
1911         $admin_origin = parse_url( admin_url() );
1912         $home_origin  = parse_url( home_url() );
1913         $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1914
1915         $browser = array(
1916                 'mobile' => wp_is_mobile(),
1917                 'ios'    => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
1918         );
1919
1920         $settings = array(
1921                 'url'           => esc_url( admin_url( 'customize.php' ) ),
1922                 'isCrossDomain' => $cross_domain,
1923                 'browser'       => $browser,
1924                 'l10n'          => array(
1925                         'saveAlert'       => __( 'The changes you made will be lost if you navigate away from this page.' ),
1926                         'mainIframeTitle' => __( 'Customizer' ),
1927                 ),
1928         );
1929
1930         $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings ) . ';';
1931
1932         $wp_scripts = wp_scripts();
1933         $data = $wp_scripts->get_data( 'customize-loader', 'data' );
1934         if ( $data )
1935                 $script = "$data\n$script";
1936
1937         $wp_scripts->add_data( 'customize-loader', 'data', $script );
1938 }
1939
1940 /**
1941  * Returns a URL to load the Customizer.
1942  *
1943  * @since 3.4.0
1944  *
1945  * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
1946  *                               The theme's stylesheet will be urlencoded if necessary.
1947  * @return string
1948  */
1949 function wp_customize_url( $stylesheet = null ) {
1950         $url = admin_url( 'customize.php' );
1951         if ( $stylesheet )
1952                 $url .= '?theme=' . urlencode( $stylesheet );
1953         return esc_url( $url );
1954 }
1955
1956 /**
1957  * Prints a script to check whether or not the Customizer is supported,
1958  * and apply either the no-customize-support or customize-support class
1959  * to the body.
1960  *
1961  * This function MUST be called inside the body tag.
1962  *
1963  * Ideally, call this function immediately after the body tag is opened.
1964  * This prevents a flash of unstyled content.
1965  *
1966  * It is also recommended that you add the "no-customize-support" class
1967  * to the body tag by default.
1968  *
1969  * @since 3.4.0
1970  */
1971 function wp_customize_support_script() {
1972         $admin_origin = parse_url( admin_url() );
1973         $home_origin  = parse_url( home_url() );
1974         $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1975
1976         ?>
1977         <script type="text/javascript">
1978                 (function() {
1979                         var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
1980
1981 <?php           if ( $cross_domain ): ?>
1982                         request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
1983 <?php           else: ?>
1984                         request = true;
1985 <?php           endif; ?>
1986
1987                         b[c] = b[c].replace( rcs, ' ' );
1988                         b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
1989                 }());
1990         </script>
1991         <?php
1992 }
1993
1994 /**
1995  * Whether the site is being previewed in the Customizer.
1996  *
1997  * @since 4.0.0
1998  *
1999  * @global WP_Customize_Manager $wp_customize Customizer instance.
2000  *
2001  * @return bool True if the site is being previewed in the Customizer, false otherwise.
2002  */
2003 function is_customize_preview() {
2004         global $wp_customize;
2005
2006         return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
2007 }