From 0dee3a771af2d029e4846e8101c3f795b81232ff Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Wed, 8 Apr 2015 22:27:57 +0000 Subject: [PATCH] Customize Headers: Improve handling of the initial header model. Headers are currently keyed by the basename of the image, see #31786. When two images have the same key only one image will be listed and the current image can be empty in the header control. To prevent this we now fall back to the raw current header image if the image isn't in `_wpCustomizeHeader.uploads`. props sirbrillig. fixes #31742. git-svn-id: https://develop.svn.wordpress.org/trunk@32091 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/customize-controls.js | 34 ++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/js/customize-controls.js b/src/wp-admin/js/customize-controls.js index 0570d2c385..aeee50f227 100644 --- a/src/wp-admin/js/customize-controls.js +++ b/src/wp-admin/js/customize-controls.js @@ -1692,7 +1692,7 @@ this.btnNew.on( 'click', this.openMedia ); this.btnRemove.on( 'click', this.removeImage ); - api.HeaderTool.currentHeader = new api.HeaderTool.ImageModel(); + api.HeaderTool.currentHeader = this.getInitialHeaderImage(); new api.HeaderTool.CurrentView({ model: api.HeaderTool.currentHeader, @@ -1715,6 +1715,38 @@ ]); }, + /** + * Returns a new instance of api.HeaderTool.ImageModel based on the currently + * saved header image (if any). + * + * @since 4.2.0 + * + * @returns {Object} Options + */ + getInitialHeaderImage: function() { + if ( ! api.get().header_image || ! api.get().header_image_data || _.contains( [ 'remove-header', 'random-default-image', 'random-uploaded-image' ], api.get().header_image ) ) { + return new api.HeaderTool.ImageModel(); + } + + // Get the matching uploaded image object. + var currentHeaderObject = _.find( _wpCustomizeHeader.uploads, function( imageObj ) { + return ( imageObj.attachment_id === api.get().header_image_data.attachment_id ); + } ); + // Fall back to raw current header image. + if ( ! currentHeaderObject ) { + currentHeaderObject = { + url: api.get().header_image, + thumbnail_url: api.get().header_image, + attachment_id: api.get().header_image_data.attachment_id + }; + } + + return new api.HeaderTool.ImageModel({ + header: currentHeaderObject, + choice: currentHeaderObject.url.split( '/' ).pop() + }); + }, + /** * Returns a set of options, computed from the attached image data and * theme-specific data, to be fed to the imgAreaSelect plugin in