web/wp-includes/theme.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
   102 		elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
   102 		elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
   103 			$theme_root = WP_CONTENT_DIR . $theme_root;
   103 			$theme_root = WP_CONTENT_DIR . $theme_root;
   104 	}
   104 	}
   105 
   105 
   106 	return new WP_Theme( $stylesheet, $theme_root );
   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();
   107 }
   119 }
   108 
   120 
   109 /**
   121 /**
   110  * Whether a child theme is in use.
   122  * Whether a child theme is in use.
   111  *
   123  *
   636 		$link = "?$link";
   648 		$link = "?$link";
   637 	return $matches[1] . esc_attr( $link ) . $matches[4];
   649 	return $matches[1] . esc_attr( $link ) . $matches[4];
   638 }
   650 }
   639 
   651 
   640 /**
   652 /**
   641  * Switches current theme to new template and stylesheet names.
   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.
   642  *
   657  *
   643  * @since 2.5.0
   658  * @since 2.5.0
   644  * @uses do_action() Calls 'switch_theme' action, passing the new theme.
   659  * @uses do_action() Calls 'switch_theme' action, passing the new theme.
   645  *
   660  *
   646  * @param string $template Template name
   661  * @param string $stylesheet Stylesheet name
   647  * @param string $stylesheet Stylesheet name.
   662  */
   648  */
   663 function switch_theme( $stylesheet ) {
   649 function switch_theme( $template, $stylesheet ) {
       
   650 	global $wp_theme_directories, $sidebars_widgets;
   664 	global $wp_theme_directories, $sidebars_widgets;
   651 
   665 
   652 	if ( is_array( $sidebars_widgets ) )
   666 	if ( is_array( $sidebars_widgets ) )
   653 		set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
   667 		set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
   654 
   668 
   655 	$old_theme  = wp_get_theme();
   669 	$old_theme  = wp_get_theme();
   656 	$new_theme = wp_get_theme( $stylesheet );
   670 	$new_theme = wp_get_theme( $stylesheet );
   657 	$new_name  = $new_theme->get('Name');
   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 	}
   658 
   678 
   659 	update_option( 'template', $template );
   679 	update_option( 'template', $template );
   660 	update_option( 'stylesheet', $stylesheet );
   680 	update_option( 'stylesheet', $stylesheet );
   661 
   681 
   662 	if ( count( $wp_theme_directories ) > 1 ) {
   682 	if ( count( $wp_theme_directories ) > 1 ) {
   663 		update_option( 'template_root', get_raw_theme_root( $template, true ) );
   683 		update_option( 'template_root', get_raw_theme_root( $template, true ) );
   664 		update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
   684 		update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
   665 	}
   685 	} else {
       
   686 		delete_option( 'template_root' );
       
   687 		delete_option( 'stylesheet_root' );
       
   688 	}
       
   689 
       
   690 	$new_name  = $new_theme->get('Name');
   666 
   691 
   667 	update_option( 'current_theme', $new_name );
   692 	update_option( 'current_theme', $new_name );
   668 
   693 
   669 	if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
   694 	if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
   670 		$default_theme_mods = (array) get_option( 'mods_' . $new_name );
   695 		$default_theme_mods = (array) get_option( 'mods_' . $new_name );
   692 	// Don't validate during an install/upgrade.
   717 	// Don't validate during an install/upgrade.
   693 	if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
   718 	if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
   694 		return true;
   719 		return true;
   695 
   720 
   696 	if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
   721 	if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
   697 		switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
   722 		switch_theme( WP_DEFAULT_THEME );
   698 		return false;
   723 		return false;
   699 	}
   724 	}
   700 
   725 
   701 	if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
   726 	if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
   702 		switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
   727 		switch_theme( WP_DEFAULT_THEME );
   703 		return false;
   728 		return false;
   704 	}
   729 	}
   705 
   730 
   706 	if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
   731 	if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
   707 		switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
   732 		switch_theme( WP_DEFAULT_THEME );
   708 		return false;
   733 		return false;
   709 	}
   734 	}
   710 
   735 
   711 	return true;
   736 	return true;
   712 }
   737 }
   867 		return false;
   892 		return false;
   868 
   893 
   869 	if ( is_random_header_image() )
   894 	if ( is_random_header_image() )
   870 		$url = get_random_header_image();
   895 		$url = get_random_header_image();
   871 
   896 
   872 	if ( is_ssl() )
   897 	return esc_url_raw( set_url_scheme( $url ) );
   873 		$url = str_replace( 'http://', 'https://', $url );
       
   874 	else
       
   875 		$url = str_replace( 'https://', 'http://', $url );
       
   876 
       
   877 	return esc_url_raw( $url );
       
   878 }
   898 }
   879 
   899 
   880 /**
   900 /**
   881  * Get random header image data from registered images in theme.
   901  * Get random header image data from registered images in theme.
   882  *
   902  *
  1006  * @since 3.4.0
  1026  * @since 3.4.0
  1007  *
  1027  *
  1008  * @return object
  1028  * @return object
  1009  */
  1029  */
  1010 function get_custom_header() {
  1030 function get_custom_header() {
  1011 	$data = is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data' );
  1031 	global $_wp_default_headers;
       
  1032 
       
  1033 	if ( is_random_header_image() ) {
       
  1034 		$data = _get_random_header_data();
       
  1035 	} else {
       
  1036 		$data = get_theme_mod( 'header_image_data' );
       
  1037 		if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
       
  1038 			$directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
       
  1039 			$data = array();
       
  1040 			$data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
       
  1041 			if ( ! empty( $_wp_default_headers ) ) {
       
  1042 				foreach ( (array) $_wp_default_headers as $default_header ) {
       
  1043 					$url = vsprintf( $default_header['url'], $directory_args );
       
  1044 					if ( $data['url'] == $url ) {
       
  1045 						$data = $default_header;
       
  1046 						$data['url'] = $url;
       
  1047 						$data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
       
  1048 						break;
       
  1049 					}
       
  1050 				}
       
  1051 			}
       
  1052 		}
       
  1053 	}
       
  1054 
  1012 	$default = array(
  1055 	$default = array(
  1013 		'url'           => '',
  1056 		'url'           => '',
  1014 		'thumbnail_url' => '',
  1057 		'thumbnail_url' => '',
  1015 		'width'         => get_theme_support( 'custom-header', 'width' ),
  1058 		'width'         => get_theme_support( 'custom-header', 'width' ),
  1016 		'height'        => get_theme_support( 'custom-header', 'height' ),
  1059 		'height'        => get_theme_support( 'custom-header', 'height' ),
  1101  * @since 3.0.0
  1144  * @since 3.0.0
  1102  * @access protected
  1145  * @access protected
  1103  */
  1146  */
  1104 function _custom_background_cb() {
  1147 function _custom_background_cb() {
  1105 	// $background is the saved custom image, or the default image.
  1148 	// $background is the saved custom image, or the default image.
  1106 	$background = get_background_image();
  1149 	$background = set_url_scheme( get_background_image() );
  1107 
  1150 
  1108 	// $color is the saved custom color.
  1151 	// $color is the saved custom color.
  1109 	// A default has to be specified in style.css. It will not be printed here.
  1152 	// A default has to be specified in style.css. It will not be printed here.
  1110 	$color = get_theme_mod( 'background_color' );
  1153 	$color = get_theme_mod( 'background_color' );
  1111 
  1154 
  1444 	if ( ! isset( $_wp_theme_features[ $feature ] ) )
  1487 	if ( ! isset( $_wp_theme_features[ $feature ] ) )
  1445 		return false;
  1488 		return false;
  1446 
  1489 
  1447 	switch ( $feature ) {
  1490 	switch ( $feature ) {
  1448 		case 'custom-header' :
  1491 		case 'custom-header' :
       
  1492 			if ( false === did_action( 'wp_loaded', '_custom_header_background_just_in_time' ) )
       
  1493 				break;
  1449 			$support = get_theme_support( 'custom-header' );
  1494 			$support = get_theme_support( 'custom-header' );
  1450 			if ( $support[0]['wp-head-callback'] )
  1495 			if ( $support[0]['wp-head-callback'] )
  1451 				remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  1496 				remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  1452 			remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
  1497 			remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
  1453 			unset( $GLOBALS['custom_image_header'] );
  1498 			unset( $GLOBALS['custom_image_header'] );
  1454 			break;
  1499 			break;
  1455 
  1500 
  1456 		case 'custom-background' :
  1501 		case 'custom-background' :
       
  1502 			if ( false === did_action( 'wp_loaded', '_custom_header_background_just_in_time' ) )
       
  1503 				break;
  1457 			$support = get_theme_support( 'custom-background' );
  1504 			$support = get_theme_support( 'custom-background' );
  1458 			remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  1505 			remove_action( 'wp_head', $support[0]['wp-head-callback'] );
  1459 			remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
  1506 			remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
  1460 			unset( $GLOBALS['custom_background'] );
  1507 			unset( $GLOBALS['custom_background'] );
  1461 			break;
  1508 			break;
  1667 			request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
  1714 			request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
  1668 <?php		else: ?>
  1715 <?php		else: ?>
  1669 			request = true;
  1716 			request = true;
  1670 <?php		endif; ?>
  1717 <?php		endif; ?>
  1671 
  1718 
  1672 			b[c] = b[c].replace( rcs, '' );
  1719 			b[c] = b[c].replace( rcs, ' ' );
  1673 			b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
  1720 			b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
  1674 		}());
  1721 		}());
  1675 	</script>
  1722 	</script>
  1676 	<?php
  1723 	<?php
  1677 }
  1724 }