From 634f3cdae0015c24983bb61d7cf8f33cb078c17a Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Tue, 2 May 2017 15:14:09 -0700 Subject: [PATCH 01/39] [WIP] Gallery Widget First pass. Fixes #62. --- core-media-widgets.php | 3 + wp-admin/js/widgets/media-gallery-widget.js | 154 ++++++++++++++++ .../widgets/class-wp-widget-media-gallery.php | 166 ++++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 wp-admin/js/widgets/media-gallery-widget.js create mode 100644 wp-includes/widgets/class-wp-widget-media-gallery.php diff --git a/core-media-widgets.php b/core-media-widgets.php index 6581b18..38f62b0 100644 --- a/core-media-widgets.php +++ b/core-media-widgets.php @@ -41,6 +41,7 @@ function wp32417_default_scripts( WP_Scripts $scripts ) { $scripts->add_inline_script( 'media-widgets', 'wp.mediaWidgets.init();', 'after' ); $scripts->add( 'media-audio-widget', plugin_dir_url( __FILE__ ) . 'wp-admin/js/widgets/media-audio-widget.js', array( 'media-widgets', 'media-audiovideo' ) ); + $scripts->add( 'media-gallery-widget', plugin_dir_url( __FILE__ ) . 'wp-admin/js/widgets/media-gallery-widget.js', array( 'media-widgets' ) ); $scripts->add( 'media-image-widget', plugin_dir_url( __FILE__ ) . 'wp-admin/js/widgets/media-image-widget.js', array( 'media-widgets' ) ); $scripts->add( 'media-video-widget', plugin_dir_url( __FILE__ ) . 'wp-admin/js/widgets/media-video-widget.js', array( 'media-widgets', 'media-audiovideo' ) ); @@ -88,9 +89,11 @@ function wp32417_twentyten_styles() { function wp32417_widgets_init() { require_once( dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media.php' ); require_once( dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-audio.php' ); + require_once( dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-gallery.php' ); require_once( dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-image.php' ); require_once( dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-video.php' ); + register_widget( 'WP_Widget_Media_Gallery' ); register_widget( 'WP_Widget_Media_Image' ); register_widget( 'WP_Widget_Media_Video' ); register_widget( 'WP_Widget_Media_Audio' ); diff --git a/wp-admin/js/widgets/media-gallery-widget.js b/wp-admin/js/widgets/media-gallery-widget.js new file mode 100644 index 0000000..2a4aeeb --- /dev/null +++ b/wp-admin/js/widgets/media-gallery-widget.js @@ -0,0 +1,154 @@ +/* eslint consistent-this: [ "error", "control" ] */ +(function( component ) { + 'use strict'; + + var GalleryWidgetModel, GalleryWidgetControl, GalleryDetailsMediaFrame; + + /** + * Custom gallery details frame. + * + * @class GalleryDetailsMediaFrame + * @constructor + */ + GalleryDetailsMediaFrame = wp.media.view.MediaFrame.Post.extend( { + + /** + * Create the default states. + * + * @returns {void} + */ + createStates: function createStates() { + this.states.add([ + new wp.media.controller.Library({ + id: 'gallery', + title: wp.media.view.l10n.createGalleryTitle, + priority: 40, + toolbar: 'main-gallery', + filterable: 'uploaded', + multiple: 'add', + editable: false, + + library: wp.media.query( _.defaults({ + type: 'image' + }, this.options.library ) ) + }), + + // Gallery states. + new wp.media.controller.GalleryEdit({ + library: this.options.selection, + editing: this.options.editing, + menu: 'gallery' + }), + + new wp.media.controller.GalleryAdd() + ]); + } + } ); + + /** + * Gallery widget model. + * + * See WP_Widget_Gallery::enqueue_admin_scripts() for amending prototype from PHP exports. + * + * @class GalleryWidgetModel + * @constructor + */ + GalleryWidgetModel = component.MediaWidgetModel.extend( {} ); + + /** + * Gallery widget control. + * + * See WP_Widget_Gallery::enqueue_admin_scripts() for amending prototype from PHP exports. + * + * @class GalleryWidgetControl + * @constructor + */ + GalleryWidgetControl = component.MediaWidgetControl.extend( { + + + /** + * Render preview. + * + * @returns {void} + */ + renderPreview: function renderPreview() { + var control = this, previewContainer, previewTemplate; + previewContainer = control.$el.find( '.media-widget-preview' ); + previewTemplate = wp.template( 'wp-media-widget-gallery-preview' ); + previewContainer.html( previewTemplate( _.extend( control.previewTemplateProps.toJSON() ) ) ); + }, + + /** + * Open the media select frame to chose an item. + * + * @returns {void} + */ + selectMedia: function selectMedia() { + var control = this, selection, mediaFrame, defaultSync, mediaFrameProps; + + if ( control.isSelected() && 0 !== control.model.get( 'attachment_id' ) ) { + selection = new wp.media.model.Selection( [ control.selectedAttachment ] ); + } else { + selection = null; + } + + mediaFrameProps = control.mapModelToMediaFrameProps( control.model.toJSON() ); + if ( mediaFrameProps.size ) { + control.displaySettings.set( 'size', mediaFrameProps.size ); + } + + mediaFrame = new GalleryDetailsMediaFrame({ + frame: 'select', + text: control.l10n.add_to_widget, + selection: selection, + mimeType: control.mime_type, + selectedDisplaySettings: control.displaySettings, + showDisplaySettings: control.showDisplaySettings, + metadata: mediaFrameProps, + state: 'gallery' + }); + wp.media.frame = mediaFrame; // See wp.media(). + + // Handle selection of a media item. + mediaFrame.on( 'reset', function onInsert() { + var state = mediaFrame.state(); +console.log(wp.media.controller.state().get('selection')); + }); + + // Disable syncing of attachment changes back to server. See . + defaultSync = wp.media.model.Attachment.prototype.sync; + wp.media.model.Attachment.prototype.sync = function rejectedSync() { + return $.Deferred().rejectWith( this ).promise(); + }; + mediaFrame.on( 'close', function onClose() { + wp.media.model.Attachment.prototype.sync = defaultSync; + }); + + mediaFrame.$el.addClass( 'media-widget' ); + mediaFrame.open(); + + // Clear the selected attachment when it is deleted in the media select frame. + if ( selection ) { + selection.on( 'destroy', function onDestroy( attachment ) { + if ( control.model.get( 'attachment_id' ) === attachment.get( 'id' ) ) { + control.model.set({ + attachment_id: 0, + url: '' + }); + } + }); + } + + /* + * Make sure focus is set inside of modal so that hitting Esc will close + * the modal and not inadvertently cause the widget to collapse in the customizer. + */ + mediaFrame.$el.find( ':focusable:first' ).focus(); + } + } ); + + // Exports. + component.controlConstructors.media_gallery = GalleryWidgetControl; + component.modelConstructors.media_gallery = GalleryWidgetModel; + +})( wp.mediaWidgets ); diff --git a/wp-includes/widgets/class-wp-widget-media-gallery.php b/wp-includes/widgets/class-wp-widget-media-gallery.php new file mode 100644 index 0000000..ad2ad18 --- /dev/null +++ b/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -0,0 +1,166 @@ + __( 'Displays an image gallery.' ), + 'mime_type' => 'image', + ) ); + + $this->l10n = array_merge( $this->l10n, array( + 'no_media_selected' => __( 'No images selected' ), + 'select_media' => _x( 'Select Images', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), + 'change_media' => _x( 'Add Image', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), + 'edit_media' => _x( 'Edit Gallery', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), + 'missing_attachment' => sprintf( + /* translators: placeholder is URL to media library */ + __( 'We can’t find that gallery. Check your media library and make sure it wasn’t deleted.' ), + esc_url( admin_url( 'upload.php' ) ) + ), + 'media_library_state_multi' => '', + 'media_library_state_single' => '', + ) ); + } + + /** + * Get schema for properties of a widget instance (item). + * + * @since 4.8.0 + * @access public + * + * @see WP_REST_Controller::get_item_schema() + * @see WP_REST_Controller::get_additional_fields() + * @link https://core.trac.wordpress.org/ticket/35574 + * @return array Schema for properties. + */ + public function get_instance_schema() { + return array_merge( + parent::get_instance_schema(), + array( + 'ids' => array( + 'type' => 'array', + 'default' => array(), + ), + 'columns' => array( + 'type' => 'integer', + 'default' => 3, + ), + 'size' => array( + 'type' => 'string', + 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ), + 'default' => 'thumbnail', + ), + 'link' => array( + 'type' => 'string', + 'default' => '', + 'format' => 'uri', + 'sanitize_callback' => 'esc_url', + 'should_preview_update' => false, + ), + ) + ); + } + + /** + * Render the media on the frontend. + * + * @since 4.8.0 + * @access public + * + * @param array $instance Widget instance props. + * @return void + */ + public function render_media( $instance ) { + $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); + + echo gallery_shortcode( array( + 'ids' => $instance['ids'], + ) ); + } + + /** + * Loads the required media files for the media manager and scripts for media widgets. + * + * @since 4.8.0 + * @access public + */ + public function enqueue_admin_scripts() { + parent::enqueue_admin_scripts(); + + $handle = 'media-gallery-widget'; + wp_enqueue_script( $handle ); + + $exported_schema = array(); + foreach ( $this->get_instance_schema() as $field => $field_schema ) { + $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) ); + } + wp_add_inline_script( + $handle, + sprintf( + 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;', + wp_json_encode( $this->id_base ), + wp_json_encode( $exported_schema ) + ) + ); + + wp_add_inline_script( + $handle, + sprintf( + ' + wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s; + _.extend( wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s ); + ', + wp_json_encode( $this->id_base ), + wp_json_encode( $this->widget_options['mime_type'] ), + wp_json_encode( $this->l10n ) + ) + ); + } + + /** + * Render form template scripts. + * + * @since 4.8.0 + * @access public + */ + public function render_control_template_scripts() { + parent::render_control_template_scripts(); + + ?> + + Date: Wed, 3 May 2017 11:20:02 -0700 Subject: [PATCH 02/39] Add preview for galleries Very basic, needs much more polish. And styles. --- wp-admin/js/widgets/media-gallery-widget.js | 18 +++++- .../widgets/class-wp-widget-media-gallery.php | 55 ++++++++++++++++--- 2 files changed, 61 insertions(+), 12 deletions(-) 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'] ); ?>

From 905edbccfc2a623b73a93dac803c5335519d7837 Mon Sep 17 00:00:00 2001 From: Timmy Crawford Date: Wed, 3 May 2017 13:59:25 -0700 Subject: [PATCH 03/39] Fix travis issues, prevent JS error. --- wp-admin/js/widgets/media-gallery-widget.js | 23 +++++++++---------- .../widgets/class-wp-widget-media-gallery.php | 6 +++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/wp-admin/js/widgets/media-gallery-widget.js b/wp-admin/js/widgets/media-gallery-widget.js index f0dbbea..843b0c2 100644 --- a/wp-admin/js/widgets/media-gallery-widget.js +++ b/wp-admin/js/widgets/media-gallery-widget.js @@ -1,5 +1,5 @@ /* eslint consistent-this: [ "error", "control" ] */ -(function( component ) { +(function( component, $ ) { 'use strict'; var GalleryWidgetModel, GalleryWidgetControl, GalleryDetailsMediaFrame; @@ -64,8 +64,6 @@ * @constructor */ GalleryWidgetControl = component.MediaWidgetControl.extend( { - - /** * Render preview. * @@ -75,7 +73,7 @@ var control = this, previewContainer, previewTemplate; previewContainer = control.$el.find( '.media-widget-preview' ); previewTemplate = wp.template( 'wp-media-widget-gallery-preview' ); - previewContainer.html( previewTemplate( _.extend( control.previewTemplateProps.toJSON() ) ) ); + previewContainer.html( previewTemplate( control.previewTemplateProps.toJSON() ) ); }, /** @@ -109,21 +107,22 @@ }); wp.media.frame = mediaFrame; // See wp.media(). - // Handle selection of a media item. - mediaFrame.on( 'update', function onUpdate( selection ) { - var state = mediaFrame.state(); + mediaFrame.on( 'update', function onUpdate( selections ) { + var state = mediaFrame.state(), selectedImages; - selection = selection || state.get( 'selection' ); + selectedImages = selections || state.get( 'selection' ); - if ( ! selection ) { + if ( ! selectedImages ) { return; } // Update widget instance. control.model.set( { - ids: _.pluck( selection.models, 'id' ).join( ',' ), - attachments: selection.models.map( function( model ) { return model.toJSON(); } ) + ids: _.pluck( selectedImages.models, 'id' ).join( ',' ), + attachments: selectedImages.models.map( function( model ) { + return model.toJSON(); + } ) } ); } ); @@ -163,4 +162,4 @@ component.controlConstructors.media_gallery = GalleryWidgetControl; component.modelConstructors.media_gallery = GalleryWidgetModel; -})( wp.mediaWidgets ); +})( wp.mediaWidgets, jQuery ); diff --git a/wp-includes/widgets/class-wp-widget-media-gallery.php b/wp-includes/widgets/class-wp-widget-media-gallery.php index 2872e11..b2b191c 100644 --- a/wp-includes/widgets/class-wp-widget-media-gallery.php +++ b/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -34,7 +34,7 @@ public function __construct() { 'change_media' => _x( 'Add Image', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), 'edit_media' => _x( 'Edit Gallery', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), 'missing_attachment' => sprintf( - /* translators: placeholder is URL to media library */ + /* translators: placeholder is URL to media library */ __( 'We can’t find that gallery. Check your media library and make sure it wasn’t deleted.' ), esc_url( admin_url( 'upload.php' ) ) ), @@ -107,9 +107,11 @@ public function render_media( $instance ) { 'ids' => $instance['ids'], ); + // @codingStandardsIgnoreStart if ( $instance['orderby_random'] ) { $shortcode_atts['orderby'] = 'rand'; } + // @codingStandardsIgnoreEnd echo gallery_shortcode( $shortcode_atts ); } @@ -170,7 +172,7 @@ public function render_control_template_scripts() {

l10n['missing_attachment']; ?>

- <# } else if ( data.attachments.length ) { #> + <# } else if ( Array.isArray( data.attachments ) && data.attachments.length ) { #>