From 7bbd87b61d6beaa9cc6cdf13d825820f9199d7e0 Mon Sep 17 00:00:00 2001 From: Daryl Koopersmith Date: Sat, 10 Nov 2012 08:40:35 +0000 Subject: [PATCH] Media: Keep the selected featured image in the media modal in sync with the page. fixes #22404, see #21390. git-svn-id: https://develop.svn.wordpress.org/trunk@22528 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/meta-boxes.php | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index bd81cb1e32..f6dad298d6 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/wp-admin/includes/meta-boxes.php @@ -1009,15 +1009,29 @@ function post_thumbnail_meta_box( $post ) { $thumbnailId = $element.find('input[name="thumbnail_id"]'), title = '', update = '', + Attachment = wp.media.model.Attachment, frame, setFeaturedImage; setFeaturedImage = function( thumbnailId ) { + var selection; + $element.find('img').remove(); $element.toggleClass( 'has-featured-image', -1 != thumbnailId ); $thumbnailId.val( thumbnailId ); + + if ( frame ) { + selection = frame.get('library').get('selection'); + + if ( -1 === thumbnailId ) + selection.clear(); + else + selection.add( Attachment.get( thumbnailId ) ); + } }; $element.on( 'click', '.choose, img', function( event ) { + var options, thumbnailId; + event.preventDefault(); if ( frame ) { @@ -1025,13 +1039,18 @@ function post_thumbnail_meta_box( $post ) { return; } - frame = wp.media({ - title: title, - selection: [ wp.media.model.Attachment.get( $thumbnailId.val() ) ], - library: { + options = { + title: title, + library: { type: 'image' } - }); + }; + + thumbnailId = $thumbnailId.val(); + if ( '' !== thumbnailId && -1 !== thumbnailId ) + options.selection = [ Attachment.get( thumbnailId ) ]; + + frame = wp.media( options ); frame.toolbar.on( 'activate:select', function() { frame.toolbar.view().add({