Index: wp-admin/css/wp-admin.css
===================================================================
--- wp-admin/css/wp-admin.css (revision 26868)
+++ wp-admin/css/wp-admin.css (working copy)
@@ -7519,6 +7519,24 @@
max-height: 300px;
}
+.appearance_page_custom-background .available-backgrounds .default-background {
+ float: left;
+ margin: 0 20px 20px 0;
+}
+
+.appearance_page_custom-background .available-backgrounds label input {
+ margin-right: 10px;
+}
+
+.appearance_page_custom-background .available-backgrounds label img {
+ vertical-align: middle;
+}
+
+.appearance_page_custom-background .button + .description {
+ display: block;
+ margin-top: 10px;
+}
+
/*------------------------------------------------------------------------------
17.0 - Plugins
Index: wp-admin/custom-background.php
===================================================================
--- wp-admin/custom-background.php (revision 26868)
+++ wp-admin/custom-background.php (working copy)
@@ -69,9 +69,10 @@
$this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array($this, 'admin_page'));
- add_action("load-$page", array($this, 'admin_load'));
- add_action("load-$page", array($this, 'take_action'), 49);
- add_action("load-$page", array($this, 'handle_upload'), 49);
+ add_action( "load-$page", array( $this, 'admin_load' ) );
+ add_action( "load-$page", array( $this, 'take_action' ), 49 );
+ add_action( "load-$page", array( $this, 'handle_upload' ), 49 );
+ add_action( "load-$page", array( $this, 'settings' ), 50 );
if ( $this->admin_header_callback )
add_action("admin_head-$page", $this->admin_header_callback, 51);
@@ -105,6 +106,298 @@
}
/**
+ * Adds settings sections and fields.
+ *
+ * @since 3.9.0
+ */
+ function settings() {
+ add_settings_section( 'background-image', __( 'Background Image' ), '__return_false', 'background-image' );
+ add_settings_section( 'display-options', __( 'Display Options' ), '__return_false', 'display-options' );
+
+ if ( $this->admin_image_div_callback ) {
+ call_user_func( $this->admin_image_div_callback );
+ } else {
+ add_settings_field( 'preview', __( 'Preview' ), array( $this, 'preview_field' ), 'background-image', 'background-image' );
+ }
+
+ // If we have a background image, add the option to remove it.
+ if ( get_background_image() ) {
+ add_settings_field( 'remove-background', __( 'Remove Image' ), array( $this, 'remove_background_field' ), 'background-image', 'background-image' );
+ }
+
+ // If the theme has a default image specified, and it is not the
+ // current background image, add the option to reset it to the default.
+ if ( current_theme_supports( 'custom-background', 'default-image' ) && get_background_image() !== get_theme_support( 'custom-background', 'default-image' ) ) {
+ add_settings_field( 'reset-background', __( 'Restore Original Image' ), array( $this, 'reset_background_field' ), 'background-image', 'background-image' );
+ }
+
+ if ( current_theme_supports( 'custom-background', 'uploads' ) ) {
+ add_settings_field( 'custom-background-upload', __( 'Upload Image' ), array( $this, 'upload_background_field' ), 'background-image', 'background-image' );
+ }
+
+ // Uploaded background images.
+ $uploaded_backgrounds = get_uploaded_background_images();
+ if ( $uploaded_backgrounds ) {
+ add_settings_field( 'uploaded-backgrounds', __( 'Uploaded Images' ), array( $this, 'background_selection_field' ), 'display-options', 'display-options', array(
+ 'backgrounds' => $uploaded_backgrounds,
+ 'label' => __( 'You can choose one of your previously uploaded backgrounds.' ),
+ ) );
+ }
+
+ // Default background images, registered by the theme.
+ $default_backgrounds = get_default_backgrounds();
+ $label = __( 'You can use one of these cool backgrounds.' );
+ if ( current_theme_supports( 'custom-background', 'uploads' ) ) {
+ $label = __( 'If you don‘t want to upload your own image, you can use one of these cool backgrounds.' );
+ }
+ if ( $default_backgrounds ) {
+ add_settings_field( 'default-backgrounds', __( 'Default Images' ), array( $this, 'background_selection_field' ), 'display-options', 'display-options', array(
+ 'backgrounds' => $default_backgrounds,
+ 'label' => $label,
+ ) );
+ }
+
+ if ( get_background_image() ) {
+ add_settings_field( 'background-position', __( 'Position' ), array( $this, 'background_position_field' ), 'display-options', 'display-options' );
+ add_settings_field( 'background-repeat', __( 'Repeat' ), array( $this, 'background_repeat_field' ), 'display-options', 'display-options' );
+ add_settings_field( 'background-attachment', __( 'Attachment' ), array( $this, 'background_attachment_field' ), 'display-options', 'display-options' );
+ }
+ add_settings_field( 'background-color', __( 'Background Color' ), array( $this, 'background_color_field' ), 'display-options', 'display-options' );
+ }
+
+ /**
+ * Renders the background preview settings field.
+ *
+ * @since 3.9.0
+ */
+ function preview_field() {
+ $background_styles = '';
+ if ( $bgcolor = get_background_color() ) {
+ $background_styles .= sprintf( 'background-color: #%s;', $bgcolor );
+ }
+
+ if ( get_background_image() ) {
+ $background_styles .= sprintf(
+ ' background-image: url(%1$s); background-repeat: %2$s; background-position: top %3$s;',
+ esc_url( get_background_image() ),
+ get_theme_mod( 'background_repeat', 'repeat' ),
+ get_theme_mod( 'background_position_x', 'left' )
+ );
+ }
+ ?>
+
+
+
+
+
+
+
+
+
+
+
+
+ '',
+ 'backgrounds' => array(),
+ ) );
+ ?>
+
+
+
+
+
+
+
+ ', esc_attr( get_background_color() ), $default_color );
+ }
+
+ /**
+ * Adds an update notice, if not already set.
+ *
+ * @since 3.9.0
+ */
+ function updated() {
+ if ( ! get_settings_errors( 'custom-background' ) ) {
+ add_settings_error( 'custom-background', 'background-updated', sprintf( __( 'Background updated. Visit your site to see how it looks.' ), home_url( '/' ) ), 'updated' );
+ }
+ }
+
+ /**
* Execute custom background modification.
*
* @since 3.0.0
@@ -118,7 +411,7 @@
check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
remove_theme_mod('background_image');
remove_theme_mod('background_image_thumb');
- $this->updated = true;
+ $this->updated();
return;
}
@@ -127,8 +420,9 @@
check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
set_theme_mod('background_image', '');
set_theme_mod('background_image_thumb', '');
- $this->updated = true;
- wp_safe_redirect( $_POST['_wp_http_referer'] );
+ $this->updated();
+ set_transient( 'settings_errors', get_settings_errors(), 30 );
+ wp_safe_redirect( add_query_arg( 'settings-updated', true, $_POST['_wp_http_referer'] ) );
return;
}
@@ -168,7 +462,25 @@
set_theme_mod('background_color', '');
}
- $this->updated = true;
+ if ( isset( $_POST['default-background'] ) ) {
+ check_admin_referer( 'custom-background' );
+ $uploaded = get_uploaded_background_images();
+ $default = get_default_backgrounds();
+
+ if ( isset( $uploaded[ $_POST['default-background'] ] ) ) {
+ set_theme_mod( 'background_image', esc_url_raw( $uploaded[ $_POST['default-background'] ]['url'] ) );
+ set_theme_mod( 'background_image_thumb', esc_url_raw( $uploaded[ $_POST['default-background'] ]['thumbnail_url'] ) );
+ }
+ elseif ( isset( $default[ $_POST['default-background'] ] ) ) {
+ set_theme_mod( 'background_image', esc_url_raw( $default[ $_POST['default-background'] ]['url'] ) );
+ set_theme_mod( 'background_image_thumb', esc_url_raw( $default[ $_POST['default-background'] ]['thumbnail_url'] ) );
+ }
+ else {
+ return;
+ }
+ }
+
+ $this->updated();
}
/**
@@ -177,163 +489,22 @@
* @since 3.0.0
*/
function admin_page() {
-?>
-
-
-updated) ) { ?>
-
-
Visit your site to see how it looks.' ), home_url( '/' ) ); ?>
-
-
+
+
+
- if ( $this->admin_image_div_callback ) {
- call_user_func($this->admin_image_div_callback);
- } else {
-?>
-
-
-
-
-
-
-
-
+
+
+
+ updated = true;
+ $this->updated();
}
/**
Index: wp-includes/theme.php
===================================================================
--- wp-includes/theme.php (revision 26868)
+++ wp-includes/theme.php (working copy)
@@ -1108,36 +1108,68 @@
}
/**
- * Get the header images uploaded for the current theme.
+ * Get the header or background images uploaded for the current theme.
*
- * @since 3.2.0
+ * @since 3.9.0
+ * @access private
*
+ * @param string $context Either 'custom-header' or 'custom-background'.
* @return array
*/
-function get_uploaded_header_images() {
- $header_images = array();
+function _get_uploaded_images( $context ) {
+ $images = array();
+ $meta_key = '';
+
+ switch ( $context ) {
+ case 'custom-header':
+ $meta_key = '_wp_attachment_is_custom_header';
+ break;
+ case 'custom-background':
+ $meta_key = '_wp_attachment_is_custom_background';
+ break;
+ }
- // @todo caching
- $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
+ $attachments = get_posts( array(
+ 'post_type' => 'attachment',
+ 'meta_key' => $meta_key,
+ 'meta_value' => get_option( 'stylesheet' ),
+ 'orderby' => 'none',
+ 'nopaging' => true,
+ ) );
- if ( empty( $headers ) )
- return array();
+ if ( ! empty( $attachments ) ) {
+ foreach ( (array) $attachments as $attachment ) {
+ $url = esc_url_raw( $attachment->guid );
+ $index = basename( $url );
+ $data = wp_get_attachment_metadata( $attachment->ID );
- foreach ( (array) $headers as $header ) {
- $url = esc_url_raw( $header->guid );
- $header_data = wp_get_attachment_metadata( $header->ID );
- $header_index = basename($url);
- $header_images[$header_index] = array();
- $header_images[$header_index]['attachment_id'] = $header->ID;
- $header_images[$header_index]['url'] = $url;
- $header_images[$header_index]['thumbnail_url'] = $url;
- if ( isset( $header_data['width'] ) )
- $header_images[$header_index]['width'] = $header_data['width'];
- if ( isset( $header_data['height'] ) )
- $header_images[$header_index]['height'] = $header_data['height'];
+ $images[ $index ] = array(
+ 'attachment_id' => $attachment->ID,
+ 'url' => $url,
+ 'thumbnail_url' => wp_get_attachment_thumb_url( $attachment->ID ),
+ );
+
+ if ( isset( $data['width'] ) ) {
+ $images[ $index ]['width'] = $data['width'];
+ }
+ if ( isset( $data['height'] ) ) {
+ $images[ $index ]['height'] = $data['height'];
+ }
+ }
}
- return $header_images;
+ return $images;
+}
+
+/**
+ * Get the header images uploaded for the current theme.
+ *
+ * @since 3.2.0
+ *
+ * @return array
+ */
+function get_uploaded_header_images() {
+ return _get_uploaded_images( 'custom-header' );
}
/**
@@ -1219,6 +1251,98 @@
}
/**
+ * Register a selection of default backgrounds to be displayed by the custom
+ * backgrounds admin UI.
+ *
+ * @since 3.9.0
+ *
+ * @param array $backgrounds Array of backgrounds keyed by a string id. The ids
+ * point to arrays containing 'url', 'thumbnail_url',
+ * and 'description' keys.
+ */
+function register_default_backgrounds( $backgrounds ) {
+ global $_wp_default_backgrounds;
+
+ $_wp_default_backgrounds = array_merge( (array) $_wp_default_backgrounds, (array) $backgrounds );
+}
+
+/**
+ * Unregister default backgrounds.
+ *
+ * This function must be called after register_default_backgrounds() has
+ * already added the background you want to remove.
+ *
+ * @see register_default_backgrounds()
+ * @since 3.9.0
+ *
+ * @param string|array $background The background string id (key of array) to
+ * remove, or an array thereof.
+ * @return True on success, false on failure.
+ */
+function unregister_default_backgrounds( $background ) {
+ global $_wp_default_backgrounds;
+
+ if ( is_array( $background ) ) {
+ array_map( 'unregister_default_backgrounds', $background );
+ } elseif ( isset( $_wp_default_backgrounds[ $background ] ) ) {
+ unset( $_wp_default_backgrounds[ $background ] );
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/**
+ * Array of registered default background images.
+ *
+ * @since 3.9.0
+ *
+ * @return array Backgrounds keyed by a string id. The ids point to arrays
+ * containing 'url', 'thumbnail_url', and 'description' keys.
+ */
+function get_default_backgrounds() {
+ global $_wp_default_backgrounds;
+ $default_backgrounds = array();
+
+ if ( isset( $_wp_default_backgrounds ) ) {
+ $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
+ $default_backgrounds = $_wp_default_backgrounds;
+ foreach ( array_keys( $_wp_default_backgrounds ) as $background ) {
+ $default_backgrounds[ $background ] = array(
+ 'url' => vsprintf( $_wp_default_backgrounds[ $background ]['url'], $directory_args ),
+ 'thumbnail_url' => vsprintf( $_wp_default_backgrounds[ $background ]['thumbnail_url'], $directory_args ),
+ );
+ }
+ }
+
+ if ( current_theme_supports( 'custom-background', 'default-image' ) ) {
+ $default_background = sprintf( get_theme_support( 'custom-background', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() );
+
+ // Only add the default background if it has not been registered.
+ if ( ! in_array( $default_background, wp_list_pluck( $default_backgrounds, 'url' ) ) ) {
+ $default_backgrounds[ basename( $default_background ) ] = array(
+ 'url' => $default_background,
+ 'thumbnail_url' => $default_background,
+ 'description' => '',
+ );
+ }
+ }
+
+ return $default_backgrounds;
+}
+
+/**
+ * Get the background images uploaded for the current theme.
+ *
+ * @since 3.9.0
+ *
+ * @return array
+ */
+function get_uploaded_background_images() {
+ return _get_uploaded_images( 'custom-background' );
+}
+
+/**
* Retrieve background image for custom background.
*
* @since 3.0.0
@@ -1400,6 +1524,10 @@
return add_theme_support( 'custom-header', array( 'uploads' => true ) );
break;
+ case 'custom-background-uploads' :
+ return add_theme_support( 'custom-background', array( 'uploads' => true ) );
+ break;
+
case 'custom-header' :
if ( ! is_array( $args ) )
$args = array( 0 => array() );
@@ -1487,6 +1615,7 @@
'default-position-x' => 'left',
'default-attachment' => 'scroll',
'default-color' => '',
+ 'uploads' => true,
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => '',
@@ -1620,6 +1749,13 @@
return false;
add_theme_support( 'custom-header', array( 'uploads' => false ) );
return; // Do not continue - custom-header-uploads no longer exists.
+
+ case 'custom-background-uploads' :
+ if ( ! isset( $_wp_theme_features['custom-background'] ) ) {
+ return false;
+ }
+ add_theme_support( 'custom-background', array( 'uploads' => false ) );
+ return;
}
if ( ! isset( $_wp_theme_features[ $feature ] ) )
@@ -1663,6 +1799,10 @@
if ( 'custom-header-uploads' == $feature )
return current_theme_supports( 'custom-header', 'uploads' );
+ if ( 'custom-background-uploads' == $feature ) {
+ return current_theme_supports( 'custom-background', 'uploads' );
+ }
+
if ( !isset( $_wp_theme_features[$feature] ) )
return false;
Index: wp-includes/class-wp-customize-control.php
===================================================================
--- wp-includes/class-wp-customize-control.php (revision 26868)
+++ wp-includes/class-wp-customize-control.php (working copy)
@@ -642,6 +642,13 @@
class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
/**
+ * The processed default backgrounds.
+ * @since 3.9.0
+ * @var array
+ */
+ protected $default_backgrounds;
+
+ /**
* Constructor.
*
* @since 3.4.0
@@ -657,29 +664,27 @@
'get_url' => 'get_background_image',
) );
- if ( $this->setting->default )
- $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
+ $this->default_backgrounds = get_default_backgrounds();
+
+ if ( $this->default_backgrounds ) {
+ $this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
+ }
+
+ if ( ! current_theme_supports( 'custom-background', 'uploads' ) ) {
+ $this->remove_tab( 'upload-new' );
+ }
}
/**
* @since 3.4.0
+ * @uses WP_Customize_Image_Control::print_tab_image()
*/
public function tab_uploaded() {
- $backgrounds = get_posts( array(
- 'post_type' => 'attachment',
- 'meta_key' => '_wp_attachment_is_custom_background',
- 'meta_value' => $this->manager->get_stylesheet(),
- 'orderby' => 'none',
- 'nopaging' => true,
- ) );
-
?>print_tab_image( esc_url_raw( $background->guid ) );
+ foreach ( get_uploaded_background_images() as $background ) {
+ $this->print_tab_image( esc_url_raw( $background['url'] ), esc_url_raw( $background['thumbnail_url'] ) );
+ }
}
/**
@@ -687,7 +692,9 @@
* @uses WP_Customize_Image_Control::print_tab_image()
*/
public function tab_default_background() {
- $this->print_tab_image( $this->setting->default );
+ foreach ( $this->default_backgrounds as $background ) {
+ $this->print_tab_image( esc_url_raw( $background['url'] ), esc_url_raw( $background['thumbnail_url'] ) );
+ }
}
}