]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/theme.php
WordPress 4.4
[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         $nav_menu_locations = get_theme_mod( 'nav_menu_locations' );
692
693         if ( func_num_args() > 1 ) {
694                 $stylesheet = func_get_arg( 1 );
695         }
696
697         $old_theme = wp_get_theme();
698         $new_theme = wp_get_theme( $stylesheet );
699         $template  = $new_theme->get_template();
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                 if ( ! empty( $nav_menu_locations ) && empty( $default_theme_mods['nav_menu_locations'] ) ) {
720                         $default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
721                 }
722                 add_option( "theme_mods_$stylesheet", $default_theme_mods );
723         } else {
724                 /*
725                  * Since retrieve_widgets() is called when initializing a theme in the Customizer,
726                  * we need to to remove the theme mods to avoid overwriting changes made via
727                  * the Customizer when accessing wp-admin/widgets.php.
728                  */
729                 if ( 'wp_ajax_customize_save' === current_action() ) {
730                         remove_theme_mod( 'sidebars_widgets' );
731                 }
732
733                 if ( ! empty( $nav_menu_locations ) ) {
734                         $nav_mods = get_theme_mod( 'nav_menu_locations' );
735                         if ( empty( $nav_mods ) ) {
736                                 set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
737                         }
738                 }
739         }
740
741         update_option( 'theme_switched', $old_theme->get_stylesheet() );
742         /**
743          * Fires after the theme is switched.
744          *
745          * @since 1.5.0
746          *
747          * @param string   $new_name  Name of the new theme.
748          * @param WP_Theme $new_theme WP_Theme instance of the new theme.
749          */
750         do_action( 'switch_theme', $new_name, $new_theme );
751 }
752
753 /**
754  * Checks that current theme files 'index.php' and 'style.css' exists.
755  *
756  * Does not initially check the default theme, which is the fallback and should always exist.
757  * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
758  * Will switch theme to the fallback theme if current theme does not validate.
759  *
760  * You can use the 'validate_current_theme' filter to return false to
761  * disable this functionality.
762  *
763  * @since 1.5.0
764  * @see WP_DEFAULT_THEME
765  *
766  * @return bool
767  */
768 function validate_current_theme() {
769         /**
770          * Filter whether to validate the current theme.
771          *
772          * @since 2.7.0
773          *
774          * @param bool true Validation flag to check the current theme.
775          */
776         if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) )
777                 return true;
778
779         if ( ! file_exists( get_template_directory() . '/index.php' ) ) {
780                 // Invalid.
781         } elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) {
782                 // Invalid.
783         } elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
784                 // Invalid.
785         } else {
786                 // Valid.
787                 return true;
788         }
789
790         $default = wp_get_theme( WP_DEFAULT_THEME );
791         if ( $default->exists() ) {
792                 switch_theme( WP_DEFAULT_THEME );
793                 return false;
794         }
795
796         /**
797          * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
798          * switch to the latest core default theme that's installed.
799          * If it turns out that this latest core default theme is our current
800          * theme, then there's nothing we can do about that, so we have to bail,
801          * rather than going into an infinite loop. (This is why there are
802          * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
803          * if it turns out there is no default theme installed. (That's `false`.)
804          */
805         $default = WP_Theme::get_core_default_theme();
806         if ( false === $default || get_stylesheet() == $default->get_stylesheet() ) {
807                 return true;
808         }
809
810         switch_theme( $default->get_stylesheet() );
811         return false;
812 }
813
814 /**
815  * Retrieve all theme modifications.
816  *
817  * @since 3.1.0
818  *
819  * @return array|void Theme modifications.
820  */
821 function get_theme_mods() {
822         $theme_slug = get_option( 'stylesheet' );
823         $mods = get_option( "theme_mods_$theme_slug" );
824         if ( false === $mods ) {
825                 $theme_name = get_option( 'current_theme' );
826                 if ( false === $theme_name )
827                         $theme_name = wp_get_theme()->get('Name');
828                 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
829                 if ( is_admin() && false !== $mods ) {
830                         update_option( "theme_mods_$theme_slug", $mods );
831                         delete_option( "mods_$theme_name" );
832                 }
833         }
834         return $mods;
835 }
836
837 /**
838  * Retrieve theme modification value for the current theme.
839  *
840  * If the modification name does not exist, then the $default will be passed
841  * through {@link http://php.net/sprintf sprintf()} PHP function with the first
842  * string the template directory URI and the second string the stylesheet
843  * directory URI.
844  *
845  * @since 2.1.0
846  *
847  * @param string      $name    Theme modification name.
848  * @param bool|string $default
849  * @return string
850  */
851 function get_theme_mod( $name, $default = false ) {
852         $mods = get_theme_mods();
853
854         if ( isset( $mods[$name] ) ) {
855                 /**
856                  * Filter the theme modification, or 'theme_mod', value.
857                  *
858                  * The dynamic portion of the hook name, `$name`, refers to
859                  * the key name of the modification array. For example,
860                  * 'header_textcolor', 'header_image', and so on depending
861                  * on the theme options.
862                  *
863                  * @since 2.2.0
864                  *
865                  * @param string $current_mod The value of the current theme modification.
866                  */
867                 return apply_filters( "theme_mod_{$name}", $mods[$name] );
868         }
869
870         if ( is_string( $default ) )
871                 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
872
873         /** This filter is documented in wp-includes/theme.php */
874         return apply_filters( "theme_mod_{$name}", $default );
875 }
876
877 /**
878  * Update theme modification value for the current theme.
879  *
880  * @since 2.1.0
881  *
882  * @param string $name  Theme modification name.
883  * @param mixed  $value Theme modification value.
884  */
885 function set_theme_mod( $name, $value ) {
886         $mods = get_theme_mods();
887         $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
888
889         /**
890          * Filter the theme mod value on save.
891          *
892          * The dynamic portion of the hook name, `$name`, refers to the key name of
893          * the modification array. For example, 'header_textcolor', 'header_image',
894          * and so on depending on the theme options.
895          *
896          * @since 3.9.0
897          *
898          * @param string $value     The new value of the theme mod.
899          * @param string $old_value The current value of the theme mod.
900          */
901         $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value );
902
903         $theme = get_option( 'stylesheet' );
904         update_option( "theme_mods_$theme", $mods );
905 }
906
907 /**
908  * Remove theme modification name from current theme list.
909  *
910  * If removing the name also removes all elements, then the entire option will
911  * be removed.
912  *
913  * @since 2.1.0
914  *
915  * @param string $name Theme modification name.
916  */
917 function remove_theme_mod( $name ) {
918         $mods = get_theme_mods();
919
920         if ( ! isset( $mods[ $name ] ) )
921                 return;
922
923         unset( $mods[ $name ] );
924
925         if ( empty( $mods ) ) {
926                 remove_theme_mods();
927                 return;
928         }
929         $theme = get_option( 'stylesheet' );
930         update_option( "theme_mods_$theme", $mods );
931 }
932
933 /**
934  * Remove theme modifications option for current theme.
935  *
936  * @since 2.1.0
937  */
938 function remove_theme_mods() {
939         delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
940
941         // Old style.
942         $theme_name = get_option( 'current_theme' );
943         if ( false === $theme_name )
944                 $theme_name = wp_get_theme()->get('Name');
945         delete_option( 'mods_' . $theme_name );
946 }
947
948 /**
949  * Retrieve text color for custom header.
950  *
951  * @since 2.1.0
952  *
953  * @return string
954  */
955 function get_header_textcolor() {
956         return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
957 }
958
959 /**
960  * Display text color for custom header.
961  *
962  * @since 2.1.0
963  */
964 function header_textcolor() {
965         echo get_header_textcolor();
966 }
967
968 /**
969  * Whether to display the header text.
970  *
971  * @since 3.4.0
972  *
973  * @return bool
974  */
975 function display_header_text() {
976         if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
977                 return false;
978
979         $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
980         return 'blank' !== $text_color;
981 }
982
983 /**
984  * Check whether a header image is set or not.
985  *
986  * @since 4.2.0
987  *
988  * @see get_header_image()
989  *
990  * @return bool Whether a header image is set or not.
991  */
992 function has_header_image() {
993         return (bool) get_header_image();
994 }
995
996 /**
997  * Retrieve header image for custom header.
998  *
999  * @since 2.1.0
1000  *
1001  * @return string|false
1002  */
1003 function get_header_image() {
1004         $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
1005
1006         if ( 'remove-header' == $url )
1007                 return false;
1008
1009         if ( is_random_header_image() )
1010                 $url = get_random_header_image();
1011
1012         return esc_url_raw( set_url_scheme( $url ) );
1013 }
1014
1015 /**
1016  * Create image tag markup for a custom header image.
1017  *
1018  * @since 4.4.0
1019  *
1020  * @param array $attr Optional. Additional attributes for the image tag. Can be used
1021  *                              to override the default attributes. Default empty.
1022  * @return string HTML image element markup or empty string on failure.
1023  */
1024 function get_header_image_tag( $attr = array() ) {
1025         $header = get_custom_header();
1026
1027         if ( empty( $header->url ) ) {
1028                 return '';
1029         }
1030
1031         $width = absint( $header->width );
1032         $height = absint( $header->height );
1033
1034         $attr = wp_parse_args(
1035                 $attr,
1036                 array(
1037                         'src' => $header->url,
1038                         'width' => $width,
1039                         'height' => $height,
1040                         'alt' => get_bloginfo( 'name' ),
1041                 )
1042         );
1043
1044         // Generate 'srcset' and 'sizes' if not already present.
1045         if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) {
1046                 $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true );
1047                 $size_array = array( $width, $height );
1048
1049                 if ( is_array( $image_meta ) ) {
1050                         $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id );
1051                         $sizes = ! empty( $attr['sizes'] ) ? $attr['sizes'] : wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );
1052
1053                         if ( $srcset && $sizes ) {
1054                                 $attr['srcset'] = $srcset;
1055                                 $attr['sizes'] = $sizes;
1056                         }
1057                 }
1058         }
1059
1060         $attr = array_map( 'esc_attr', $attr );
1061         $html = '<img';
1062
1063         foreach ( $attr as $name => $value ) {
1064                 $html .= ' ' . $name . '="' . $value . '"';
1065         }
1066
1067         $html .= ' />';
1068
1069         /**
1070          * Filter the markup of header images.
1071          *
1072          * @since 4.4.0
1073          *
1074          * @param string $html   The HTML image tag markup being filtered.
1075          * @param object $header The custom header object returned by 'get_custom_header()'.
1076          * @param array  $attr   Array of the attributes for the image tag.
1077          */
1078         return apply_filters( 'get_header_image_tag', $html, $header, $attr );
1079 }
1080
1081 /**
1082  * Display the image markup for a custom header image.
1083  *
1084  * @since 4.4.0
1085  *
1086  * @param array $attr Optional. Attributes for the image markup. Default empty.
1087  */
1088 function the_header_image_tag( $attr = array() ) {
1089         echo get_header_image_tag( $attr );
1090 }
1091
1092 /**
1093  * Get random header image data from registered images in theme.
1094  *
1095  * @since 3.4.0
1096  *
1097  * @access private
1098  *
1099  * @global array  $_wp_default_headers
1100  * @staticvar object $_wp_random_header
1101  *
1102  * @return object
1103  */
1104 function _get_random_header_data() {
1105         static $_wp_random_header = null;
1106
1107         if ( empty( $_wp_random_header ) ) {
1108                 global $_wp_default_headers;
1109                 $header_image_mod = get_theme_mod( 'header_image', '' );
1110                 $headers = array();
1111
1112                 if ( 'random-uploaded-image' == $header_image_mod )
1113                         $headers = get_uploaded_header_images();
1114                 elseif ( ! empty( $_wp_default_headers ) ) {
1115                         if ( 'random-default-image' == $header_image_mod ) {
1116                                 $headers = $_wp_default_headers;
1117                         } else {
1118                                 if ( current_theme_supports( 'custom-header', 'random-default' ) )
1119                                         $headers = $_wp_default_headers;
1120                         }
1121                 }
1122
1123                 if ( empty( $headers ) )
1124                         return new stdClass;
1125
1126                 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
1127
1128                 $_wp_random_header->url =  sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1129                 $_wp_random_header->thumbnail_url =  sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1130         }
1131         return $_wp_random_header;
1132 }
1133
1134 /**
1135  * Get random header image url from registered images in theme.
1136  *
1137  * @since 3.2.0
1138  *
1139  * @return string Path to header image
1140  */
1141 function get_random_header_image() {
1142         $random_image = _get_random_header_data();
1143         if ( empty( $random_image->url ) )
1144                 return '';
1145         return $random_image->url;
1146 }
1147
1148 /**
1149  * Check if random header image is in use.
1150  *
1151  * Always true if user expressly chooses the option in Appearance > Header.
1152  * Also true if theme has multiple header images registered, no specific header image
1153  * is chosen, and theme turns on random headers with add_theme_support().
1154  *
1155  * @since 3.2.0
1156  *
1157  * @param string $type The random pool to use. any|default|uploaded
1158  * @return bool
1159  */
1160 function is_random_header_image( $type = 'any' ) {
1161         $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
1162
1163         if ( 'any' == $type ) {
1164                 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
1165                         return true;
1166         } else {
1167                 if ( "random-$type-image" == $header_image_mod )
1168                         return true;
1169                 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
1170                         return true;
1171         }
1172
1173         return false;
1174 }
1175
1176 /**
1177  * Display header image URL.
1178  *
1179  * @since 2.1.0
1180  */
1181 function header_image() {
1182         $image = get_header_image();
1183         if ( $image ) {
1184                 echo esc_url( $image );
1185         }
1186 }
1187
1188 /**
1189  * Get the header images uploaded for the current theme.
1190  *
1191  * @since 3.2.0
1192  *
1193  * @return array
1194  */
1195 function get_uploaded_header_images() {
1196         $header_images = array();
1197
1198         // @todo caching
1199         $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1200
1201         if ( empty( $headers ) )
1202                 return array();
1203
1204         foreach ( (array) $headers as $header ) {
1205                 $url = esc_url_raw( wp_get_attachment_url( $header->ID ) );
1206                 $header_data = wp_get_attachment_metadata( $header->ID );
1207                 $header_index = basename($url);
1208
1209                 $header_images[$header_index] = array();
1210                 $header_images[$header_index]['attachment_id'] = $header->ID;
1211                 $header_images[$header_index]['url'] =  $url;
1212                 $header_images[$header_index]['thumbnail_url'] = $url;
1213                 $header_images[$header_index]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
1214
1215                 if ( isset( $header_data['width'] ) )
1216                         $header_images[$header_index]['width'] = $header_data['width'];
1217                 if ( isset( $header_data['height'] ) )
1218                         $header_images[$header_index]['height'] = $header_data['height'];
1219         }
1220
1221         return $header_images;
1222 }
1223
1224 /**
1225  * Get the header image data.
1226  *
1227  * @since 3.4.0
1228  *
1229  * @global array $_wp_default_headers
1230  *
1231  * @return object
1232  */
1233 function get_custom_header() {
1234         global $_wp_default_headers;
1235
1236         if ( is_random_header_image() ) {
1237                 $data = _get_random_header_data();
1238         } else {
1239                 $data = get_theme_mod( 'header_image_data' );
1240                 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
1241                         $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
1242                         $data = array();
1243                         $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
1244                         if ( ! empty( $_wp_default_headers ) ) {
1245                                 foreach ( (array) $_wp_default_headers as $default_header ) {
1246                                         $url = vsprintf( $default_header['url'], $directory_args );
1247                                         if ( $data['url'] == $url ) {
1248                                                 $data = $default_header;
1249                                                 $data['url'] = $url;
1250                                                 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
1251                                                 break;
1252                                         }
1253                                 }
1254                         }
1255                 }
1256         }
1257
1258         $default = array(
1259                 'url'           => '',
1260                 'thumbnail_url' => '',
1261                 'width'         => get_theme_support( 'custom-header', 'width' ),
1262                 'height'        => get_theme_support( 'custom-header', 'height' ),
1263         );
1264         return (object) wp_parse_args( $data, $default );
1265 }
1266
1267 /**
1268  * Register a selection of default headers to be displayed by the custom header admin UI.
1269  *
1270  * @since 3.0.0
1271  *
1272  * @global array $_wp_default_headers
1273  *
1274  * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1275  */
1276 function register_default_headers( $headers ) {
1277         global $_wp_default_headers;
1278
1279         $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1280 }
1281
1282 /**
1283  * Unregister default headers.
1284  *
1285  * This function must be called after register_default_headers() has already added the
1286  * header you want to remove.
1287  *
1288  * @see register_default_headers()
1289  * @since 3.0.0
1290  *
1291  * @global array $_wp_default_headers
1292  *
1293  * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1294  * @return bool|void A single header returns true on success, false on failure.
1295  *                   There is currently no return value for multiple headers.
1296  */
1297 function unregister_default_headers( $header ) {
1298         global $_wp_default_headers;
1299         if ( is_array( $header ) ) {
1300                 array_map( 'unregister_default_headers', $header );
1301         } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1302                 unset( $_wp_default_headers[ $header ] );
1303                 return true;
1304         } else {
1305                 return false;
1306         }
1307 }
1308
1309 /**
1310  * Retrieve background image for custom background.
1311  *
1312  * @since 3.0.0
1313  *
1314  * @return string
1315  */
1316 function get_background_image() {
1317         return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1318 }
1319
1320 /**
1321  * Display background image path.
1322  *
1323  * @since 3.0.0
1324  */
1325 function background_image() {
1326         echo get_background_image();
1327 }
1328
1329 /**
1330  * Retrieve value for custom background color.
1331  *
1332  * @since 3.0.0
1333  *
1334  * @return string
1335  */
1336 function get_background_color() {
1337         return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1338 }
1339
1340 /**
1341  * Display background color value.
1342  *
1343  * @since 3.0.0
1344  */
1345 function background_color() {
1346         echo get_background_color();
1347 }
1348
1349 /**
1350  * Default custom background callback.
1351  *
1352  * @since 3.0.0
1353  * @access protected
1354  */
1355 function _custom_background_cb() {
1356         // $background is the saved custom image, or the default image.
1357         $background = set_url_scheme( get_background_image() );
1358
1359         // $color is the saved custom color.
1360         // A default has to be specified in style.css. It will not be printed here.
1361         $color = get_background_color();
1362
1363         if ( $color === get_theme_support( 'custom-background', 'default-color' ) ) {
1364                 $color = false;
1365         }
1366
1367         if ( ! $background && ! $color )
1368                 return;
1369
1370         $style = $color ? "background-color: #$color;" : '';
1371
1372         if ( $background ) {
1373                 $image = " background-image: url('$background');";
1374
1375                 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
1376                 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1377                         $repeat = 'repeat';
1378                 $repeat = " background-repeat: $repeat;";
1379
1380                 $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
1381                 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1382                         $position = 'left';
1383                 $position = " background-position: top $position;";
1384
1385                 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
1386                 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1387                         $attachment = 'scroll';
1388                 $attachment = " background-attachment: $attachment;";
1389
1390                 $style .= $image . $repeat . $position . $attachment;
1391         }
1392 ?>
1393 <style type="text/css" id="custom-background-css">
1394 body.custom-background { <?php echo trim( $style ); ?> }
1395 </style>
1396 <?php
1397 }
1398
1399 /**
1400  * Add callback for custom TinyMCE editor stylesheets.
1401  *
1402  * The parameter $stylesheet is the name of the stylesheet, relative to
1403  * the theme root. It also accepts an array of stylesheets.
1404  * It is optional and defaults to 'editor-style.css'.
1405  *
1406  * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1407  * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1408  * If an array of stylesheets is passed to add_editor_style(),
1409  * RTL is only added for the first stylesheet.
1410  *
1411  * Since version 3.4 the TinyMCE body has .rtl CSS class.
1412  * It is a better option to use that class and add any RTL styles to the main stylesheet.
1413  *
1414  * @since 3.0.0
1415  *
1416  * @global array $editor_styles
1417  *
1418  * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1419  *                                     Defaults to 'editor-style.css'
1420  */
1421 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1422         add_theme_support( 'editor-style' );
1423
1424         if ( ! is_admin() )
1425                 return;
1426
1427         global $editor_styles;
1428         $editor_styles = (array) $editor_styles;
1429         $stylesheet    = (array) $stylesheet;
1430         if ( is_rtl() ) {
1431                 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1432                 $stylesheet[] = $rtl_stylesheet;
1433         }
1434
1435         $editor_styles = array_merge( $editor_styles, $stylesheet );
1436 }
1437
1438 /**
1439  * Removes all visual editor stylesheets.
1440  *
1441  * @since 3.1.0
1442  *
1443  * @global array $editor_styles
1444  *
1445  * @return bool True on success, false if there were no stylesheets to remove.
1446  */
1447 function remove_editor_styles() {
1448         if ( ! current_theme_supports( 'editor-style' ) )
1449                 return false;
1450         _remove_theme_support( 'editor-style' );
1451         if ( is_admin() )
1452                 $GLOBALS['editor_styles'] = array();
1453         return true;
1454 }
1455
1456 /**
1457  * Retrieve any registered editor stylesheets
1458  *
1459  * @since 4.0.0
1460  *
1461  * @global array $editor_styles Registered editor stylesheets
1462  *
1463  * @return array If registered, a list of editor stylesheet URLs.
1464  */
1465 function get_editor_stylesheets() {
1466         $stylesheets = array();
1467         // load editor_style.css if the current theme supports it
1468         if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
1469                 $editor_styles = $GLOBALS['editor_styles'];
1470
1471                 $editor_styles = array_unique( array_filter( $editor_styles ) );
1472                 $style_uri = get_stylesheet_directory_uri();
1473                 $style_dir = get_stylesheet_directory();
1474
1475                 // Support externally referenced styles (like, say, fonts).
1476                 foreach ( $editor_styles as $key => $file ) {
1477                         if ( preg_match( '~^(https?:)?//~', $file ) ) {
1478                                 $stylesheets[] = esc_url_raw( $file );
1479                                 unset( $editor_styles[ $key ] );
1480                         }
1481                 }
1482
1483                 // Look in a parent theme first, that way child theme CSS overrides.
1484                 if ( is_child_theme() ) {
1485                         $template_uri = get_template_directory_uri();
1486                         $template_dir = get_template_directory();
1487
1488                         foreach ( $editor_styles as $key => $file ) {
1489                                 if ( $file && file_exists( "$template_dir/$file" ) ) {
1490                                         $stylesheets[] = "$template_uri/$file";
1491                                 }
1492                         }
1493                 }
1494
1495                 foreach ( $editor_styles as $file ) {
1496                         if ( $file && file_exists( "$style_dir/$file" ) ) {
1497                                 $stylesheets[] = "$style_uri/$file";
1498                         }
1499                 }
1500         }
1501
1502         /**
1503          * Filter the array of stylesheets applied to the editor.
1504          *
1505          * @since 4.3.0
1506          *
1507          * @param array $stylesheets Array of stylesheets to be applied to the editor.
1508          */
1509         return apply_filters( 'editor_stylesheets', $stylesheets );
1510 }
1511
1512 /**
1513  * Allows a theme to register its support of a certain feature
1514  *
1515  * Must be called in the theme's functions.php file to work.
1516  * If attached to a hook, it must be after_setup_theme.
1517  * The init hook may be too late for some features.
1518  *
1519  * @since 2.9.0
1520  *
1521  * @global array $_wp_theme_features
1522  *
1523  * @param string $feature The feature being added.
1524  * @return void|bool False on failure, void otherwise.
1525  */
1526 function add_theme_support( $feature ) {
1527         global $_wp_theme_features;
1528
1529         if ( func_num_args() == 1 )
1530                 $args = true;
1531         else
1532                 $args = array_slice( func_get_args(), 1 );
1533
1534         switch ( $feature ) {
1535                 case 'post-formats' :
1536                         if ( is_array( $args[0] ) ) {
1537                                 $post_formats = get_post_format_slugs();
1538                                 unset( $post_formats['standard'] );
1539
1540                                 $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
1541                         }
1542                         break;
1543
1544                 case 'html5' :
1545                         // You can't just pass 'html5', you need to pass an array of types.
1546                         if ( empty( $args[0] ) ) {
1547                                 // Build an array of types for back-compat.
1548                                 $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
1549                         } elseif ( ! is_array( $args[0] ) ) {
1550                                 _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );
1551                                 return false;
1552                         }
1553
1554                         // Calling 'html5' again merges, rather than overwrites.
1555                         if ( isset( $_wp_theme_features['html5'] ) )
1556                                 $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
1557                         break;
1558
1559                 case 'custom-header-uploads' :
1560                         return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1561
1562                 case 'custom-header' :
1563                         if ( ! is_array( $args ) )
1564                                 $args = array( 0 => array() );
1565
1566                         $defaults = array(
1567                                 'default-image' => '',
1568                                 'random-default' => false,
1569                                 'width' => 0,
1570                                 'height' => 0,
1571                                 'flex-height' => false,
1572                                 'flex-width' => false,
1573                                 'default-text-color' => '',
1574                                 'header-text' => true,
1575                                 'uploads' => true,
1576                                 'wp-head-callback' => '',
1577                                 'admin-head-callback' => '',
1578                                 'admin-preview-callback' => '',
1579                         );
1580
1581                         $jit = isset( $args[0]['__jit'] );
1582                         unset( $args[0]['__jit'] );
1583
1584                         // Merge in data from previous add_theme_support() calls.
1585                         // The first value registered wins. (A child theme is set up first.)
1586                         if ( isset( $_wp_theme_features['custom-header'] ) )
1587                                 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1588
1589                         // Load in the defaults at the end, as we need to insure first one wins.
1590                         // This will cause all constants to be defined, as each arg will then be set to the default.
1591                         if ( $jit )
1592                                 $args[0] = wp_parse_args( $args[0], $defaults );
1593
1594                         // If a constant was defined, use that value. Otherwise, define the constant to ensure
1595                         // the constant is always accurate (and is not defined later,  overriding our value).
1596                         // As stated above, the first value wins.
1597                         // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1598                         // Constants are lame. Don't reference them. This is just for backwards compatibility.
1599
1600                         if ( defined( 'NO_HEADER_TEXT' ) )
1601                                 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1602                         elseif ( isset( $args[0]['header-text'] ) )
1603                                 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1604
1605                         if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1606                                 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1607                         elseif ( isset( $args[0]['width'] ) )
1608                                 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1609
1610                         if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1611                                 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1612                         elseif ( isset( $args[0]['height'] ) )
1613                                 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1614
1615                         if ( defined( 'HEADER_TEXTCOLOR' ) )
1616                                 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1617                         elseif ( isset( $args[0]['default-text-color'] ) )
1618                                 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1619
1620                         if ( defined( 'HEADER_IMAGE' ) )
1621                                 $args[0]['default-image'] = HEADER_IMAGE;
1622                         elseif ( isset( $args[0]['default-image'] ) )
1623                                 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1624
1625                         if ( $jit && ! empty( $args[0]['default-image'] ) )
1626                                 $args[0]['random-default'] = false;
1627
1628                         // If headers are supported, and we still don't have a defined width or height,
1629                         // we have implicit flex sizes.
1630                         if ( $jit ) {
1631                                 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1632                                         $args[0]['flex-width'] = true;
1633                                 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1634                                         $args[0]['flex-height'] = true;
1635                         }
1636
1637                         break;
1638
1639                 case 'custom-background' :
1640                         if ( ! is_array( $args ) )
1641                                 $args = array( 0 => array() );
1642
1643                         $defaults = array(
1644                                 'default-image'          => '',
1645                                 'default-repeat'         => 'repeat',
1646                                 'default-position-x'     => 'left',
1647                                 'default-attachment'     => 'scroll',
1648                                 'default-color'          => '',
1649                                 'wp-head-callback'       => '_custom_background_cb',
1650                                 'admin-head-callback'    => '',
1651                                 'admin-preview-callback' => '',
1652                         );
1653
1654                         $jit = isset( $args[0]['__jit'] );
1655                         unset( $args[0]['__jit'] );
1656
1657                         // Merge in data from previous add_theme_support() calls. The first value registered wins.
1658                         if ( isset( $_wp_theme_features['custom-background'] ) )
1659                                 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1660
1661                         if ( $jit )
1662                                 $args[0] = wp_parse_args( $args[0], $defaults );
1663
1664                         if ( defined( 'BACKGROUND_COLOR' ) )
1665                                 $args[0]['default-color'] = BACKGROUND_COLOR;
1666                         elseif ( isset( $args[0]['default-color'] ) || $jit )
1667                                 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1668
1669                         if ( defined( 'BACKGROUND_IMAGE' ) )
1670                                 $args[0]['default-image'] = BACKGROUND_IMAGE;
1671                         elseif ( isset( $args[0]['default-image'] ) || $jit )
1672                                 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1673
1674                         break;
1675
1676                 // Ensure that 'title-tag' is accessible in the admin.
1677                 case 'title-tag' :
1678                         // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
1679                         if ( did_action( 'wp_loaded' ) ) {
1680                                 /* translators: 1: Theme support 2: hook name */
1681                                 _doing_it_wrong( "add_theme_support( 'title-tag' )", sprintf( __( 'Theme support for %1$s should be registered before the %2$s hook.' ),
1682                                         '<code>title-tag</code>', '<code>wp_loaded</code>' ), '4.1' );
1683
1684                                 return false;
1685                         }
1686         }
1687
1688         $_wp_theme_features[ $feature ] = $args;
1689 }
1690
1691 /**
1692  * Registers the internal custom header and background routines.
1693  *
1694  * @since 3.4.0
1695  * @access private
1696  *
1697  * @global Custom_Image_Header $custom_image_header
1698  * @global Custom_Background   $custom_background
1699  */
1700 function _custom_header_background_just_in_time() {
1701         global $custom_image_header, $custom_background;
1702
1703         if ( current_theme_supports( 'custom-header' ) ) {
1704                 // In case any constants were defined after an add_custom_image_header() call, re-run.
1705                 add_theme_support( 'custom-header', array( '__jit' => true ) );
1706
1707                 $args = get_theme_support( 'custom-header' );
1708                 if ( $args[0]['wp-head-callback'] )
1709                         add_action( 'wp_head', $args[0]['wp-head-callback'] );
1710
1711                 if ( is_admin() ) {
1712                         require_once( ABSPATH . 'wp-admin/custom-header.php' );
1713                         $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1714                 }
1715         }
1716
1717         if ( current_theme_supports( 'custom-background' ) ) {
1718                 // In case any constants were defined after an add_custom_background() call, re-run.
1719                 add_theme_support( 'custom-background', array( '__jit' => true ) );
1720
1721                 $args = get_theme_support( 'custom-background' );
1722                 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1723
1724                 if ( is_admin() ) {
1725                         require_once( ABSPATH . 'wp-admin/custom-background.php' );
1726                         $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1727                 }
1728         }
1729 }
1730
1731 /**
1732  * Gets the theme support arguments passed when registering that support
1733  *
1734  * @since 3.1.0
1735  *
1736  * @global array $_wp_theme_features
1737  *
1738  * @param string $feature the feature to check
1739  * @return mixed The array of extra arguments or the value for the registered feature.
1740  */
1741 function get_theme_support( $feature ) {
1742         global $_wp_theme_features;
1743         if ( ! isset( $_wp_theme_features[ $feature ] ) )
1744                 return false;
1745
1746         if ( func_num_args() <= 1 )
1747                 return $_wp_theme_features[ $feature ];
1748
1749         $args = array_slice( func_get_args(), 1 );
1750         switch ( $feature ) {
1751                 case 'custom-header' :
1752                 case 'custom-background' :
1753                         if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1754                                 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1755                         return false;
1756
1757                 default :
1758                         return $_wp_theme_features[ $feature ];
1759         }
1760 }
1761
1762 /**
1763  * Allows a theme to de-register its support of a certain feature
1764  *
1765  * Should be called in the theme's functions.php file. Generally would
1766  * be used for child themes to override support from the parent theme.
1767  *
1768  * @since 3.0.0
1769  * @see add_theme_support()
1770  * @param string $feature the feature being added
1771  * @return bool|void Whether feature was removed.
1772  */
1773 function remove_theme_support( $feature ) {
1774         // Blacklist: for internal registrations not used directly by themes.
1775         if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1776                 return false;
1777
1778         return _remove_theme_support( $feature );
1779 }
1780
1781 /**
1782  * Do not use. Removes theme support internally, ignorant of the blacklist.
1783  *
1784  * @access private
1785  * @since 3.1.0
1786  *
1787  * @global array               $_wp_theme_features
1788  * @global Custom_Image_Header $custom_image_header
1789  * @global Custom_Background   $custom_background
1790  *
1791  * @param string $feature
1792  */
1793 function _remove_theme_support( $feature ) {
1794         global $_wp_theme_features;
1795
1796         switch ( $feature ) {
1797                 case 'custom-header-uploads' :
1798                         if ( ! isset( $_wp_theme_features['custom-header'] ) )
1799                                 return false;
1800                         add_theme_support( 'custom-header', array( 'uploads' => false ) );
1801                         return; // Do not continue - custom-header-uploads no longer exists.
1802         }
1803
1804         if ( ! isset( $_wp_theme_features[ $feature ] ) )
1805                 return false;
1806
1807         switch ( $feature ) {
1808                 case 'custom-header' :
1809                         if ( ! did_action( 'wp_loaded' ) )
1810                                 break;
1811                         $support = get_theme_support( 'custom-header' );
1812                         if ( $support[0]['wp-head-callback'] )
1813                                 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1814                         remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1815                         unset( $GLOBALS['custom_image_header'] );
1816                         break;
1817
1818                 case 'custom-background' :
1819                         if ( ! did_action( 'wp_loaded' ) )
1820                                 break;
1821                         $support = get_theme_support( 'custom-background' );
1822                         remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1823                         remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1824                         unset( $GLOBALS['custom_background'] );
1825                         break;
1826         }
1827
1828         unset( $_wp_theme_features[ $feature ] );
1829         return true;
1830 }
1831
1832 /**
1833  * Checks a theme's support for a given feature
1834  *
1835  * @since 2.9.0
1836  *
1837  * @global array $_wp_theme_features
1838  *
1839  * @param string $feature the feature being checked
1840  * @return bool
1841  */
1842 function current_theme_supports( $feature ) {
1843         global $_wp_theme_features;
1844
1845         if ( 'custom-header-uploads' == $feature )
1846                 return current_theme_supports( 'custom-header', 'uploads' );
1847
1848         if ( !isset( $_wp_theme_features[$feature] ) )
1849                 return false;
1850
1851         // If no args passed then no extra checks need be performed
1852         if ( func_num_args() <= 1 )
1853                 return true;
1854
1855         $args = array_slice( func_get_args(), 1 );
1856
1857         switch ( $feature ) {
1858                 case 'post-thumbnails':
1859                         // post-thumbnails can be registered for only certain content/post types by passing
1860                         // an array of types to add_theme_support(). If no array was passed, then
1861                         // any type is accepted
1862                         if ( true === $_wp_theme_features[$feature] )  // Registered for all types
1863                                 return true;
1864                         $content_type = $args[0];
1865                         return in_array( $content_type, $_wp_theme_features[$feature][0] );
1866
1867                 case 'html5':
1868                 case 'post-formats':
1869                         // specific post formats can be registered by passing an array of types to
1870                         // add_theme_support()
1871
1872                         // Specific areas of HTML5 support *must* be passed via an array to add_theme_support()
1873
1874                         $type = $args[0];
1875                         return in_array( $type, $_wp_theme_features[$feature][0] );
1876
1877                 case 'custom-header':
1878                 case 'custom-background' :
1879                         // specific custom header and background capabilities can be registered by passing
1880                         // an array to add_theme_support()
1881                         $header_support = $args[0];
1882                         return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1883         }
1884
1885         /**
1886          * Filter whether the current theme supports a specific feature.
1887          *
1888          * The dynamic portion of the hook name, `$feature`, refers to the specific theme
1889          * feature. Possible values include 'post-formats', 'post-thumbnails', 'custom-background',
1890          * 'custom-header', 'menus', 'automatic-feed-links', and 'html5'.
1891          *
1892          * @since 3.4.0
1893          *
1894          * @param bool   true     Whether the current theme supports the given feature. Default true.
1895          * @param array  $args    Array of arguments for the feature.
1896          * @param string $feature The theme feature.
1897          */
1898         return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[$feature] );
1899 }
1900
1901 /**
1902  * Checks a theme's support for a given feature before loading the functions which implement it.
1903  *
1904  * @since 2.9.0
1905  *
1906  * @param string $feature The feature being checked.
1907  * @param string $include Path to the file.
1908  * @return bool True if the current theme supports the supplied feature, false otherwise.
1909  */
1910 function require_if_theme_supports( $feature, $include ) {
1911         if ( current_theme_supports( $feature ) ) {
1912                 require ( $include );
1913                 return true;
1914         }
1915         return false;
1916 }
1917
1918 /**
1919  * Checks an attachment being deleted to see if it's a header or background image.
1920  *
1921  * If true it removes the theme modification which would be pointing at the deleted
1922  * attachment.
1923  *
1924  * @access private
1925  * @since 3.0.0
1926  * @since 4.3.0 Also removes `header_image_data`.
1927  *
1928  * @param int $id The attachment id.
1929  */
1930 function _delete_attachment_theme_mod( $id ) {
1931         $attachment_image = wp_get_attachment_url( $id );
1932         $header_image     = get_header_image();
1933         $background_image = get_background_image();
1934
1935         if ( $header_image && $header_image == $attachment_image ) {
1936                 remove_theme_mod( 'header_image' );
1937                 remove_theme_mod( 'header_image_data' );
1938         }
1939
1940         if ( $background_image && $background_image == $attachment_image ) {
1941                 remove_theme_mod( 'background_image' );
1942         }
1943 }
1944
1945 /**
1946  * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1947  *
1948  * @since 3.3.0
1949  */
1950 function check_theme_switched() {
1951         if ( $stylesheet = get_option( 'theme_switched' ) ) {
1952                 $old_theme = wp_get_theme( $stylesheet );
1953
1954                 // Prevent retrieve_widgets() from running since Customizer already called it up front
1955                 if ( get_option( 'theme_switched_via_customizer' ) ) {
1956                         remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
1957                         update_option( 'theme_switched_via_customizer', false );
1958                 }
1959
1960                 if ( $old_theme->exists() ) {
1961                         /**
1962                          * Fires on the first WP load after a theme switch if the old theme still exists.
1963                          *
1964                          * This action fires multiple times and the parameters differs
1965                          * according to the context, if the old theme exists or not.
1966                          * If the old theme is missing, the parameter will be the slug
1967                          * of the old theme.
1968                          *
1969                          * @since 3.3.0
1970                          *
1971                          * @param string   $old_name  Old theme name.
1972                          * @param WP_Theme $old_theme WP_Theme instance of the old theme.
1973                          */
1974                         do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
1975                 } else {
1976                         /** This action is documented in wp-includes/theme.php */
1977                         do_action( 'after_switch_theme', $stylesheet );
1978                 }
1979                 flush_rewrite_rules();
1980
1981                 update_option( 'theme_switched', false );
1982         }
1983 }
1984
1985 /**
1986  * Includes and instantiates the WP_Customize_Manager class.
1987  *
1988  * Loads the Customizer at plugins_loaded when accessing the customize.php admin
1989  * page or when any request includes a wp_customize=on param, either as a GET
1990  * query var or as POST data. This param is a signal for whether to bootstrap
1991  * the Customizer when WordPress is loading, especially in the Customizer preview
1992  * or when making Customizer Ajax requests for widgets or menus.
1993  *
1994  * @since 3.4.0
1995  *
1996  * @global WP_Customize_Manager $wp_customize
1997  */
1998 function _wp_customize_include() {
1999         if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
2000                 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
2001         ) ) {
2002                 return;
2003         }
2004
2005         require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
2006         $GLOBALS['wp_customize'] = new WP_Customize_Manager();
2007 }
2008
2009 /**
2010  * Adds settings for the customize-loader script.
2011  *
2012  * @since 3.4.0
2013  */
2014 function _wp_customize_loader_settings() {
2015         $admin_origin = parse_url( admin_url() );
2016         $home_origin  = parse_url( home_url() );
2017         $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
2018
2019         $browser = array(
2020                 'mobile' => wp_is_mobile(),
2021                 'ios'    => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
2022         );
2023
2024         $settings = array(
2025                 'url'           => esc_url( admin_url( 'customize.php' ) ),
2026                 'isCrossDomain' => $cross_domain,
2027                 'browser'       => $browser,
2028                 'l10n'          => array(
2029                         'saveAlert'       => __( 'The changes you made will be lost if you navigate away from this page.' ),
2030                         'mainIframeTitle' => __( 'Customizer' ),
2031                 ),
2032         );
2033
2034         $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings ) . ';';
2035
2036         $wp_scripts = wp_scripts();
2037         $data = $wp_scripts->get_data( 'customize-loader', 'data' );
2038         if ( $data )
2039                 $script = "$data\n$script";
2040
2041         $wp_scripts->add_data( 'customize-loader', 'data', $script );
2042 }
2043
2044 /**
2045  * Returns a URL to load the Customizer.
2046  *
2047  * @since 3.4.0
2048  *
2049  * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
2050  *                               The theme's stylesheet will be urlencoded if necessary.
2051  * @return string
2052  */
2053 function wp_customize_url( $stylesheet = null ) {
2054         $url = admin_url( 'customize.php' );
2055         if ( $stylesheet )
2056                 $url .= '?theme=' . urlencode( $stylesheet );
2057         return esc_url( $url );
2058 }
2059
2060 /**
2061  * Prints a script to check whether or not the Customizer is supported,
2062  * and apply either the no-customize-support or customize-support class
2063  * to the body.
2064  *
2065  * This function MUST be called inside the body tag.
2066  *
2067  * Ideally, call this function immediately after the body tag is opened.
2068  * This prevents a flash of unstyled content.
2069  *
2070  * It is also recommended that you add the "no-customize-support" class
2071  * to the body tag by default.
2072  *
2073  * @since 3.4.0
2074  */
2075 function wp_customize_support_script() {
2076         $admin_origin = parse_url( admin_url() );
2077         $home_origin  = parse_url( home_url() );
2078         $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
2079
2080         ?>
2081         <script type="text/javascript">
2082                 (function() {
2083                         var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
2084
2085 <?php           if ( $cross_domain ): ?>
2086                         request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
2087 <?php           else: ?>
2088                         request = true;
2089 <?php           endif; ?>
2090
2091                         b[c] = b[c].replace( rcs, ' ' );
2092                         b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
2093                 }());
2094         </script>
2095         <?php
2096 }
2097
2098 /**
2099  * Whether the site is being previewed in the Customizer.
2100  *
2101  * @since 4.0.0
2102  *
2103  * @global WP_Customize_Manager $wp_customize Customizer instance.
2104  *
2105  * @return bool True if the site is being previewed in the Customizer, false otherwise.
2106  */
2107 function is_customize_preview() {
2108         global $wp_customize;
2109
2110         return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
2111 }