Skip to content
This repository has been archived by the owner on Aug 24, 2018. It is now read-only.

Gallery Widget #120

Merged
merged 43 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
634f3cd
[WIP] Gallery Widget
obenland May 2, 2017
f8e7949
Add preview for galleries
obenland May 3, 2017
905edbc
Fix travis issues, prevent JS error.
timmyc May 3, 2017
77463cd
[WIP] Gallery Widget
obenland May 2, 2017
46fe3b4
Add preview for galleries
obenland May 3, 2017
75a466c
Fix travis issues, prevent JS error.
timmyc May 3, 2017
88a5d0b
Rebase and get things somewhat operational again.
timmyc Aug 2, 2017
984e68f
Fix preview for gallery
Aug 9, 2017
9aae63b
Fix linting errors.
timmyc Aug 9, 2017
053147f
Fix edit gallery button.
Aug 15, 2017
a063060
Not sure how to fix travis failing, trying this.
timmyc Aug 15, 2017
69e7a8f
try that again
timmyc Aug 15, 2017
ac13f7c
Remove another rule to try and get CI to pass.
timmyc Aug 16, 2017
41b6ea9
Fix edit gallery button
Aug 16, 2017
0203390
Merge branch 'add/gallery-widget' of https://github.com/xwp/wp-core-m…
Aug 16, 2017
9469601
Fix unit tests for WP 4.8
westonruter Aug 16, 2017
aebe864
Skip phpcs on PHP 5.2
westonruter Aug 16, 2017
19c44bd
Update dev-lib
westonruter Aug 16, 2017
18896da
Updates and qunit tests.
timmyc Aug 23, 2017
bfde3ee
Fix backwards compat issue, add phpunit test
timmyc Aug 23, 2017
f072ef3
Remove attachment data from gallery widget schema.
joemcgill Sep 7, 2017
4363f80
Update dev-lib and use precise distro so PHP 5.2 is available
westonruter Sep 7, 2017
1f490cd
Fix jscs and jshint issues
westonruter Sep 7, 2017
bcad6c1
Remove attachments from expected schema in unit test
joemcgill Sep 8, 2017
c1d7c4f
Merge pull request #187 from xwp/add/gallery-widget-without-attachmen…
westonruter Sep 8, 2017
cfe62b4
Update the widget preview design.
joemcgill Sep 8, 2017
2e8c03a
Register gallery widget CSS via wp_default_styles() and enqueue in wi…
westonruter Sep 11, 2017
fdf4f14
Show number of additional images if more than 5
joemcgill Sep 11, 2017
d61f3e9
Update preview to use an image as final placeholder
joemcgill Sep 11, 2017
ff52e6c
Merge pull request #188 from xwp/update/gallery-widget-preview-design
westonruter Sep 11, 2017
a4dacce
Allow gallery preview clicking to be shortcut for edit gallery button
westonruter Sep 15, 2017
fd47023
Persist random order setting from gallery; refactor method for fetchi…
westonruter Sep 16, 2017
ece64e9
Fix rendering on change and reset
westonruter Sep 16, 2017
be35f36
Add missing replace_media label
westonruter Sep 16, 2017
e1429f0
Ensure all gallery settings get synced to widget
westonruter Sep 16, 2017
94d6ab2
Update button label to 'Replace Gallery' instead of 'Replace Images' …
westonruter Sep 16, 2017
f2c6a49
Remove obsolete qunit test
westonruter Sep 16, 2017
4e343ef
Add handleAttachmentDestroy to factor out former removeAttachmentId l…
westonruter Sep 16, 2017
70480f5
Ensure r41248 from core is applied to defaultSync logic
westonruter Sep 16, 2017
daa0039
Remove obsolete access phpDoc tags
westonruter Sep 16, 2017
e82a420
Use all gallery shortcode properties when rendering shortcode.
joemcgill Sep 16, 2017
775f498
Whitespace fix
joemcgill Sep 16, 2017
e5ee972
Remove disabling of syncing attachment changes to server so captions …
westonruter Sep 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dev-lib
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ CHECK_SCOPE=changed-files
PHPCS_RULESET_FILE=phpcs.xml.dist
WPCS_BRANCH=develop
PHPCS_PHAR_URL=https://github.com/squizlabs/PHP_CodeSniffer/releases/download/2.9.0/phpcs.phar

if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.2" ]]; then
DEV_LIB_SKIP="$DEV_LIB_SKIP,phpcs"
fi
12 changes: 7 additions & 5 deletions core-media-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ function wp32417_default_scripts( WP_Scripts $scripts ) {
$scripts->add( $handle, $src, 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' ) );

if ( ! WP_CORE_MEDIA_WIDGETS_MERGED ) {
$scripts->add_inline_script( 'customize-selective-refresh', file_get_contents( dirname( __FILE__ ) . '/wp-includes/js/customize-selective-refresh-extras.js' ) );
}
Expand Down Expand Up @@ -133,12 +135,12 @@ function wp32417_twentyten_styles() {
* @codeCoverageIgnore
*/
function wp32417_widgets_init() {

$class_files = array(
'WP_Widget_Media' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media.php',
'WP_Widget_Media_Image' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-image.php',
'WP_Widget_Media_Video' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-video.php',
'WP_Widget_Media_Audio' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-audio.php',
'WP_Widget_Media' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media.php',
'WP_Widget_Media_Image' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-image.php',
'WP_Widget_Media_Video' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-video.php',
'WP_Widget_Media_Audio' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-audio.php',
'WP_Widget_Media_Gallery' => dirname( __FILE__ ) . '/wp-includes/widgets/class-wp-widget-media-gallery.php',
);
foreach ( $class_files as $class => $file ) {
if ( ! class_exists( $class ) ) {
Expand Down
2 changes: 1 addition & 1 deletion dev-lib
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

<rule ref="WordPress">
<exclude name="WordPress.XSS.EscapeOutput"/>
<exclude name="WordPress.Arrays.ArrayIndentation"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to remove these two lines now that @westonruter has fixed up the CI problems.

<exclude name="WordPress.Arrays.ArrayDeclarationSpacing"/>
</rule>

<rule ref="WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents">
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/test-class-wp-widget-media-video.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,13 @@ function test_enqueue_preview_scripts() {
$widget->enqueue_preview_scripts();
$this->assertTrue( wp_script_is( 'wp-mediaelement' ) );
$this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
$this->assertTrue( wp_script_is( 'froogaloop' ) );

$wp_scripts = null;
$wp_styles = null;
add_filter( 'wp_video_shortcode_library', '__return_empty_string' );
$widget->enqueue_preview_scripts();
$this->assertFalse( wp_script_is( 'wp-mediaelement' ) );
$this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
$this->assertTrue( wp_script_is( 'froogaloop' ) );
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/test-class-wp-widget-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ function test_enqueue_admin_scripts() {
$widget->enqueue_admin_scripts();

$this->assertTrue( wp_script_is( 'media-widgets' ) );
$this->assertTrue( wp_style_is( 'media-widgets' ) );
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.8', '<=' ) ) {
$this->assertTrue( wp_style_is( 'media-widgets' ) );
}
}

/**
Expand Down
254 changes: 254 additions & 0 deletions wp-admin/js/widgets/media-gallery-widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
/* 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: true,

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( control.previewTemplateProps.toJSON() ) );
},
isSelected: function isSelected() {
var control = this;

if ( control.model.get( 'error' ) ) {
return false;
}

return Boolean( control.model.get( 'ids' ) || control.model.get( 'attachments' ) );
},
/**
* Open the media select frame to edit images.
*
* @returns {void}
*/
editMedia: function editMedia() {
var control = this, selection, mediaFrame, defaultSync, mediaFrameProps;
if ( control.isSelected() && 0 !== control.model.get( 'selection' ) ) {
selection = new wp.media.model.Selection( JSON.parse( control.model.get( 'attachments' ) ), {
multiple: true
});
} else {
selection = null;
}

mediaFrameProps = control.mapModelToMediaFrameProps( control.model.toJSON() );
if ( mediaFrameProps.size ) {
control.displaySettings.set( 'size', mediaFrameProps.size );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used?

}
mediaFrame = new GalleryDetailsMediaFrame({
frame: 'manage',
text: control.l10n.add_to_widget,
selection: selection,
mimeType: control.mime_type,
selectedDisplaySettings: control.displaySettings,
showDisplaySettings: control.showDisplaySettings,
metadata: mediaFrameProps,
editing: true,
multiple: true,
state: 'gallery-edit'
});
wp.media.frame = mediaFrame; // See wp.media().

// Handle selection of a media item.
mediaFrame.on( 'update', function onUpdate( selections ) {
var state = mediaFrame.state(), selectedImages;

selectedImages = selections || state.get( 'selection' );

if ( ! selectedImages ) {
return;
}

// Update widget instance.
control.model.set( {
ids: _.pluck( selectedImages.models, 'id' ).join( ',' ),
attachments: JSON.stringify(
selectedImages.models.map( function( model ) {
return model.toJSON();
} )
),
selection: selectedImages
} );
} );

// Disable syncing of attachment changes back to server. See <https://core.trac.wordpress.org/ticket/40403>.
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: ''
});
}
});
}
},
/**
* 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( 'selection' ) ) {
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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used?

}
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( 'update', function onUpdate( selections ) {
var state = mediaFrame.state(), selectedImages;

selectedImages = selections || state.get( 'selection' );

if ( ! selectedImages ) {
return;
}

// Update widget instance.
control.model.set( {
ids: _.pluck( selectedImages.models, 'id' ).join( ',' ),
attachments: JSON.stringify(
selectedImages.models.map( function( model ) {
return model.toJSON();
} )
),
selection: selectedImages
} );
} );

// Disable syncing of attachment changes back to server. See <https://core.trac.wordpress.org/ticket/40403>.
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this logic here... but will test to confirm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was needed. I confirmed.

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, jQuery );
Loading