Bring Featured Images back into the main media dialog.

Most users don't realize that the Featured Image meta box exists; if they do, few use it.

Restores the old meta box UI, including the admin_post_thumbnail_html filter. If a plugin is using _wp_post_thumbnail_html() in conjunction with Thickbox elsewhere, it will also magically still work.

Specific underlying changes:
 * Converts the modal view to use the view manager, which means that a call to open() will automatically call render and attach if necessary.
 * Doesn't automatically set a state in wp.media, to allow code to customize the states to be added before activation.

props koopersmith.
fixes #21776.



git-svn-id: http://core.svn.wordpress.org/trunk@22979 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin
2012-12-03 02:38:10 +00:00
parent 336feff008
commit 5d0933d884
10 changed files with 248 additions and 212 deletions

View File

@@ -1001,119 +1001,6 @@ function link_advanced_meta_box($link) {
* @since 2.9.0
*/
function post_thumbnail_meta_box( $post ) {
global $_wp_additional_image_sizes;
?><script type="text/javascript">
jQuery( function($) {
var $element = $('#select-featured-image'),
$thumbnailId = $element.find('input[name="thumbnail_id"]'),
title = '<?php _e( "Choose a Featured Image" ); ?>',
update = '<?php _e( "Update Featured Image" ); ?>',
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.state('library').get('selection');
if ( -1 === thumbnailId )
selection.clear();
else
selection.add( Attachment.get( thumbnailId ) );
}
};
$element.on( 'click', '.choose, img', function( event ) {
var options, thumbnailId, attachment;
event.preventDefault();
if ( frame ) {
frame.open();
return;
}
options = {
title: title,
library: {
type: 'image'
}
};
thumbnailId = $thumbnailId.val();
if ( '' !== thumbnailId && -1 !== thumbnailId ) {
attachment = Attachment.get( thumbnailId );
attachment.fetch();
options.selection = [ attachment ];
}
frame = wp.media( options );
frame.state('library').set( 'filterable', 'uploaded' );
frame.toolbar.on( 'activate:select', function() {
frame.toolbar.view().set({
select: {
style: 'primary',
text: update,
click: function() {
var selection = frame.state().get('selection'),
model = selection.first(),
sizes = model.get('sizes'),
size;
setFeaturedImage( model.id );
// @todo: might need a size hierarchy equivalent.
if ( sizes )
size = sizes['post-thumbnail'] || sizes.medium;
// @todo: Need a better way of accessing full size
// data besides just calling toJSON().
size = size || model.toJSON();
frame.close();
$( '<img />', {
src: size.url,
width: size.width
}).prependTo( $element );
}
}
});
});
frame.toolbar.mode('select');
});
$element.on( 'click', '.remove', function( event ) {
event.preventDefault();
setFeaturedImage( -1 );
});
});
</script>
<?php
$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
$thumbnail_size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : 'medium';
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, $thumbnail_size );
$classes = empty( $thumbnail_id ) ? '' : 'has-featured-image';
?><div id="select-featured-image"
class="<?php echo esc_attr( $classes ); ?>"
data-post-id="<?php echo esc_attr( $post->ID ); ?>">
<?php echo $thumbnail_html; ?>
<input type="hidden" name="thumbnail_id" value="<?php echo esc_attr( $thumbnail_id ); ?>" />
<a href="#" class="choose button-secondary"><?php _e( 'Choose a Featured Image' ); ?></a>
<a href="#" class="remove"><?php _e( 'Remove Featured Image' ); ?></a>
</div>
<?php
$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
echo _wp_post_thumbnail_html( $thumbnail_id, $post->ID );
}