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