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