WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/06/2012 08:49:35 AM (9 years ago)
Author:
koopersmith
Message:

First pass at integrating featured images with the new media workflow.

Updates the featured image when the publish/update button is clicked (rather than instantly). Uses the existing post_thumbnail_meta_box() function. Does not remove the old featured image meta box JS, ajax handler, or CSS.

see #21776, #21390.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/meta-boxes.php

    r21735 r21770  
    912912 */
    913913function post_thumbnail_meta_box( $post ) {
    914     $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
    915     echo _wp_post_thumbnail_html( $thumbnail_id );
    916 }
     914    global $_wp_additional_image_sizes;
     915
     916    ?><script type="text/javascript">
     917    jQuery( function($) {
     918        var $element     = $('#select-featured-image'),
     919            $thumbnailId = $element.find('input[name="thumbnail_id"]'),
     920            title        = '<?php _e( "Choose a Featured Image" ); ?>',
     921            workflow, setFeaturedImage;
     922
     923        setFeaturedImage = function( thumbnailId ) {
     924            $element.find('img').remove();
     925            $element.toggleClass( 'has-featured-image', -1 != thumbnailId );
     926            $thumbnailId.val( thumbnailId );
     927        };
     928
     929        $element.on( 'click', '.choose, img', function( event ) {
     930            event.preventDefault();
     931
     932            if ( ! workflow ) {
     933                workflow = wp.media();
     934                workflow.selection.on( 'add', function( model ) {
     935                    var sizes = model.get('sizes'),
     936                        size;
     937
     938                    setFeaturedImage( model.id );
     939
     940                    // @todo: might need a size hierarchy equivalent.
     941                    if ( sizes )
     942                        size = sizes['post-thumbnail'] || sizes.medium;
     943
     944                    // @todo: Need a better way of accessing full size
     945                    // data besides just calling toJSON().
     946                    size = size || model.toJSON();
     947
     948                    workflow.modal.close();
     949                    workflow.selection.clear();
     950
     951                    $( '<img />', {
     952                        src:    size.url,
     953                        width:  size.width
     954                    }).prependTo( $element );
     955                });
     956                workflow.modal.title( title );
     957            }
     958
     959            workflow.modal.open();
     960        });
     961
     962        $element.on( 'click', '.remove', function( event ) {
     963            event.preventDefault();
     964            setFeaturedImage( -1 );
     965        });
     966    });
     967    </script>
     968
     969    <?php
     970    $thumbnail_id   = get_post_meta( $post->ID, '_thumbnail_id', true );
     971    $thumbnail_size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : 'medium';
     972    $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $thumbnail_size );
     973
     974    $classes = empty( $thumbnail_id ) ? '' : 'has-featured-image';
     975
     976    ?><div id="select-featured-image"
     977        class="<?php echo esc_attr( $classes ); ?>"
     978        data-post-id="<?php echo esc_attr( $post->ID ); ?>">
     979        <?php echo $thumbnail_html; ?>
     980        <input type="hidden" name="thumbnail_id" value="<?php echo esc_attr( $thumbnail_id ); ?>" />
     981        <a href="#" class="choose button-secondary"><?php _e( 'Choose a Featured Image' ); ?></a>
     982        <a href="#" class="remove"><?php _e( 'Remove Featured Image' ); ?></a>
     983    </div>
     984    <?php
     985}
Note: See TracChangeset for help on using the changeset viewer.