diff --git a/wp-admin/js/widgets/media-gallery-widget.js b/wp-admin/js/widgets/media-gallery-widget.js index 2a4aeeb..f0dbbea 100644 --- a/wp-admin/js/widgets/media-gallery-widget.js +++ b/wp-admin/js/widgets/media-gallery-widget.js @@ -109,11 +109,23 @@ }); wp.media.frame = mediaFrame; // See wp.media(). + // Handle selection of a media item. - mediaFrame.on( 'reset', function onInsert() { + mediaFrame.on( 'update', function onUpdate( selection ) { var state = mediaFrame.state(); -console.log(wp.media.controller.state().get('selection')); - }); + + selection = selection || state.get( 'selection' ); + + if ( ! selection ) { + return; + } + + // Update widget instance. + control.model.set( { + ids: _.pluck( selection.models, 'id' ).join( ',' ), + attachments: selection.models.map( function( model ) { return model.toJSON(); } ) + } ); + } ); // Disable syncing of attachment changes back to server. See . defaultSync = wp.media.model.Attachment.prototype.sync; diff --git a/wp-includes/widgets/class-wp-widget-media-gallery.php b/wp-includes/widgets/class-wp-widget-media-gallery.php index ad2ad18..2872e11 100644 --- a/wp-includes/widgets/class-wp-widget-media-gallery.php +++ b/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -59,8 +59,8 @@ public function get_instance_schema() { parent::get_instance_schema(), array( 'ids' => array( - 'type' => 'array', - 'default' => array(), + 'type' => 'string', + 'default' => '', ), 'columns' => array( 'type' => 'integer', @@ -71,13 +71,23 @@ public function get_instance_schema() { 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ), 'default' => 'thumbnail', ), - 'link' => array( + 'link_type' => array( 'type' => 'string', - 'default' => '', - 'format' => 'uri', - 'sanitize_callback' => 'esc_url', + 'enum' => array( 'none', 'file', 'post' ), + 'default' => 'none', + 'media_prop' => 'link', 'should_preview_update' => false, ), + 'orderby_random' => array( + 'type' => 'boolean', + 'default' => false, + 'media_prop' => '_orderbyRandom', + 'should_preview_update' => false, + ), + 'attachments' => array( + 'type' => 'array', + 'default' => array(), + ), ) ); } @@ -93,10 +103,15 @@ public function get_instance_schema() { */ public function render_media( $instance ) { $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); - - echo gallery_shortcode( array( + $shortcode_atts = array( 'ids' => $instance['ids'], - ) ); + ); + + if ( $instance['orderby_random'] ) { + $shortcode_atts['orderby'] = 'rand'; + } + + echo gallery_shortcode( $shortcode_atts ); } /** @@ -155,6 +170,28 @@ public function render_control_template_scripts() {

l10n['missing_attachment']; ?>

+ <# } else if ( data.attachments.length ) { #> + <# } else { #>

l10n['no_media_selected'] ); ?>