+/**
+ * Register a selection of default headers to be displayed by the custom header admin UI.
+ *
+ * @since 3.0.0
+ *
+ * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
+ */
+function register_default_headers( $headers ) {
+ global $_wp_default_headers;
+
+ $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
+}
+
+/**
+ * Unregister default headers.
+ *
+ * This function must be called after register_default_headers() has already added the
+ * header you want to remove.
+ *
+ * @see register_default_headers()
+ * @since 3.0.0
+ *
+ * @param string|array The header string id (key of array) to remove, or an array thereof.
+ * @return True on success, false on failure.
+ */
+function unregister_default_headers( $header ) {
+ global $_wp_default_headers;
+ if ( is_array( $header ) ) {
+ array_map( 'unregister_default_headers', $header );
+ } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
+ unset( $_wp_default_headers[ $header ] );
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/**
+ * Retrieve background image for custom background.
+ *
+ * @since 3.0.0
+ *
+ * @return string
+ */
+function get_background_image() {
+ $default = defined('BACKGROUND_IMAGE') ? BACKGROUND_IMAGE : '';
+
+ return get_theme_mod('background_image', $default);
+}
+
+/**
+ * Display background image path.
+ *
+ * @since 3.0.0
+ */
+function background_image() {
+ echo get_background_image();
+}
+
+/**
+ * Retrieve value for custom background color.
+ *
+ * @since 3.0.0
+ * @uses BACKGROUND_COLOR
+ *
+ * @return string
+ */
+function get_background_color() {
+ $default = defined('BACKGROUND_COLOR') ? BACKGROUND_COLOR : '';
+
+ return get_theme_mod('background_color', $default);
+}
+
+/**
+ * Display background color value.
+ *
+ * @since 3.0.0
+ */
+function background_color() {
+ echo get_background_color();
+}
+
+/**
+ * Add callbacks for background image display.
+ *
+ * The parameter $header_callback callback will be required to display the
+ * content for the 'wp_head' action. The parameter $admin_header_callback
+ * callback will be added to Custom_Background class and that will be added
+ * to the 'admin_menu' action.
+ *
+ * @since 3.0.0
+ * @uses Custom_Background Sets up for $admin_header_callback for administration panel display.
+ *
+ * @param callback $header_callback Call on 'wp_head' action.
+ * @param callback $admin_header_callback Call on custom background administration screen.
+ * @param callback $admin_image_div_callback Output a custom background image div on the custom background administration screen. Optional.
+ */
+function add_custom_background($header_callback = '', $admin_header_callback = '', $admin_image_div_callback = '') {
+ if ( isset($GLOBALS['custom_background']) )
+ return;
+
+ if ( empty($header_callback) )
+ $header_callback = '_custom_background_cb';
+
+ add_action('wp_head', $header_callback);
+
+ add_theme_support( 'custom-background' );
+
+ if ( ! is_admin() )
+ return;
+ require_once(ABSPATH . 'wp-admin/custom-background.php');
+ $GLOBALS['custom_background'] =& new Custom_Background($admin_header_callback, $admin_image_div_callback);
+ add_action('admin_menu', array(&$GLOBALS['custom_background'], 'init'));
+}
+
+/**
+ * Default custom background callback.
+ *
+ * @since 3.0.0
+ * @see add_custom_background()
+ * @access protected
+ */
+function _custom_background_cb() {
+ $background = get_background_image();
+ $color = get_background_color();
+ if ( ! $background && ! $color )
+ return;
+
+ $style = $color ? "background-color: #$color;" : '';
+
+ if ( $background ) {
+ $image = " background-image: url('$background');";
+
+ $repeat = get_theme_mod( 'background_repeat', 'repeat' );
+ if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
+ $repeat = 'repeat';
+ $repeat = " background-repeat: $repeat;";
+
+ $position = get_theme_mod( 'background_position_x', 'left' );
+ if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
+ $position = 'left';
+ $position = " background-position: top $position;";
+
+ $attachment = get_theme_mod( 'background_attachment', 'scroll' );
+ if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
+ $attachment = 'scroll';
+ $attachment = " background-attachment: $attachment;";
+
+ $style .= $image . $repeat . $position . $attachment;
+ }
+?>
+<style type="text/css">
+body { <?php echo trim( $style ); ?> }
+</style>
+<?php
+}
+
+/**
+ * Add callback for custom TinyMCE editor stylesheets.
+ *
+ * The parameter $stylesheet is the name of the stylesheet, relative to
+ * the theme root. It also accepts an array of stylesheets.
+ * It is optional and defaults to 'editor-style.css'.
+ *
+ * @since 3.0.0
+ *
+ * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
+ * Defaults to 'editor-style.css'
+ */
+function add_editor_style( $stylesheet = 'editor-style.css' ) {
+
+ add_theme_support( 'editor-style' );
+
+ if ( ! is_admin() )
+ return;
+
+ global $editor_styles;
+ $editor_styles = (array) $editor_styles;
+ $stylesheet = (array) $stylesheet;
+ if ( is_rtl() ) {
+ $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
+ $stylesheet[] = $rtl_stylesheet;
+ }
+
+ $editor_styles = array_merge( $editor_styles, $stylesheet );
+}
+
+/**
+ * Allows a theme to register its support of a certain feature
+ *
+ * Must be called in the theme's functions.php file to work.
+ * If attached to a hook, it must be after_setup_theme.
+ * The init hook may be too late for some features.
+ *
+ * @since 2.9.0
+ * @param string $feature the feature being added
+ */
+function add_theme_support( $feature ) {
+ global $_wp_theme_features;
+
+ if ( func_num_args() == 1 )
+ $_wp_theme_features[$feature] = true;
+ else
+ $_wp_theme_features[$feature] = array_slice( func_get_args(), 1 );
+}
+
+/**
+ * Allows a theme to de-register its support of a certain feature
+ *
+ * Should be called in the theme's functions.php file. Generally would
+ * be used for child themes to override support from the parent theme.
+ *
+ * @since 3.0.0
+ * @see add_theme_support()
+ * @param string $feature the feature being added
+ * @return bool Whether feature was removed.
+ */
+function remove_theme_support( $feature ) {
+ // Blacklist: for internal registrations not used directly by themes.
+ if ( in_array( $feature, array( 'custom-background', 'custom-header', 'editor-style', 'widgets', 'menus' ) ) )
+ return false;
+
+ global $_wp_theme_features;
+
+ if ( ! isset( $_wp_theme_features[$feature] ) )
+ return false;
+ unset( $_wp_theme_features[$feature] );
+ return true;
+}
+
+/**
+ * Checks a theme's support for a given feature
+ *
+ * @since 2.9.0
+ * @param string $feature the feature being checked
+ * @return boolean
+ */
+function current_theme_supports( $feature ) {
+ global $_wp_theme_features;
+
+ if ( !isset( $_wp_theme_features[$feature] ) )
+ return false;
+
+ // If no args passed then no extra checks need be performed
+ if ( func_num_args() <= 1 )
+ return true;
+
+ $args = array_slice( func_get_args(), 1 );
+
+ // @todo Allow pluggable arg checking
+ switch ( $feature ) {
+ case 'post-thumbnails':
+ // post-thumbnails can be registered for only certain content/post types by passing
+ // an array of types to add_theme_support(). If no array was passed, then
+ // any type is accepted
+ if ( true === $_wp_theme_features[$feature] ) // Registered for all types
+ return true;
+ $content_type = $args[0];
+ if ( in_array($content_type, $_wp_theme_features[$feature][0]) )
+ return true;
+ else
+ return false;
+ break;
+ }
+
+ return true;
+}
+
+/**
+ * Checks a theme's support for a given feature before loading the functions which implement it.
+ *
+ * @since 2.9.0
+ * @param string $feature the feature being checked
+ * @param string $include the file containing the functions that implement the feature
+ */
+function require_if_theme_supports( $feature, $include) {
+ if ( current_theme_supports( $feature ) )
+ require ( $include );
+}
+
+/**
+ * Checks an attachment being deleted to see if it's a header or background image.
+ *
+ * If true it removes the theme modification which would be pointing at the deleted
+ * attachment
+ *
+ * @access private
+ * @since 3.0.0
+ * @param int $id the attachment id
+ */
+function _delete_attachment_theme_mod( $id ) {
+ $attachment_image = wp_get_attachment_url( $id );
+ $header_image = get_header_image();
+ $background_image = get_background_image();
+
+ if ( $header_image && $header_image == $attachment_image )
+ remove_theme_mod( 'header_image' );
+
+ if ( $background_image && $background_image == $attachment_image )
+ remove_theme_mod( 'background_image' );
+}
+
+add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
+