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