Widgets: Introduce media widgets for images, audio, and video with extensible base for additional media widgets in the future.

The last time a new widget was introduced, Vuvuzelas were a thing, Angry Birds started taking over phones, and WordPress stopped shipping with Kubrick. Seven years and 17 releases without new widgets have been enough, time to spice up your sidebar!

Props westonruter, melchoyce, obenland, timmydcrawford, adamsilverstein, gonom9, wonderboymusic, Fab1en, DrewAPicture, sirbrillig, joen, matias, samikeijonen, afercia, celloexpressions, designsimply, michelleweber, ranh, kjellr, karmatosed.
Fixes #32417, #39993, #39994, #39995.


git-svn-id: https://develop.svn.wordpress.org/trunk@40640 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-05-11 21:10:54 +00:00
parent 51f545e72a
commit da32c2f630
28 changed files with 6288 additions and 20 deletions

View File

@ -55,6 +55,110 @@
color: #a0a5aa;
}
/* Media Widgets */
.wp-core-ui .media-widget-control.selected .placeholder,
.wp-core-ui .media-widget-control.selected .not-selected,
.wp-core-ui .media-widget-control .selected {
display: none;
}
.media-widget-control.selected .selected {
display: inline-block;
}
.media-widget-buttons {
text-align: left;
margin-bottom: 10px;
}
.media-widget-control .media-widget-buttons .button {
margin-left: 8px;
width: auto;
}
.media-widget-control:not(.selected) .media-widget-buttons .button,
.media-widget-buttons .button:first-child {
margin-left: 0;
}
.media-widget-control .placeholder {
border: 1px dashed #b4b9be;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
cursor: default;
line-height: 20px;
padding: 9px 0;
position: relative;
text-align: center;
width: 100%;
}
.media-widget-control .media-widget-preview {
text-align: center;
}
.media-widget-control .media-widget-preview .notice {
text-align: initial;
}
.media-frame .media-widget-embed-notice p code,
.media-widget-control .notice p code {
padding: 0 3px 0 0;
}
.media-frame .media-widget-embed-notice {
margin-top: 16px;
}
.media-widget-control .media-widget-preview img {
max-width: 100%;
}
.media-widget-control .media-widget-preview .wp-video-shortcode {
background: #000;
}
.media-frame.media-widget .media-toolbar-secondary {
min-width: 300px;
}
.media-frame.media-widget .image-details .embed-media-settings .setting.align,
.media-frame.media-widget .attachment-display-settings .setting.align,
.media-frame.media-widget .embed-media-settings .setting.align,
.media-frame.media-widget .embed-link-settings .setting.link-text,
.media-frame.media-widget .replace-attachment,
.media-frame.media-widget .checkbox-setting.autoplay {
display: none;
}
.media-widget-video-preview {
width: 100%;
}
.media-widget-video-link {
display: inline-block;
min-height: 132px;
width: 100%;
background: black;
}
.media-widget-video-link .dashicons {
font: normal 60px/1 'dashicons';
position: relative;
width: 100%;
top: -90px;
color: white;
text-decoration: none;
}
.media-widget-video-link.no-poster .dashicons {
top: 30px;
}
.media-frame #embed-url-field.invalid {
border: 1px solid #f00;
}
.wp-customizer .mejs-controls a:focus > .mejs-offscreen,
.widgets-php .mejs-controls a:focus > .mejs-offscreen {
z-index: 2;
}
/* Widget Dragging Helpers */
.widget.ui-draggable-dragging {
min-width: 100%;

View File

@ -0,0 +1,150 @@
/* eslint consistent-this: [ "error", "control" ] */
(function( component ) {
'use strict';
var AudioWidgetModel, AudioWidgetControl, AudioDetailsMediaFrame;
/**
* Custom audio details frame that removes the replace-audio state.
*
* @class AudioDetailsMediaFrame
* @constructor
*/
AudioDetailsMediaFrame = wp.media.view.MediaFrame.AudioDetails.extend({
/**
* Create the default states.
*
* @returns {void}
*/
createStates: function createStates() {
this.states.add([
new wp.media.controller.AudioDetails( {
media: this.media
} ),
new wp.media.controller.MediaLibrary( {
type: 'audio',
id: 'add-audio-source',
title: wp.media.view.l10n.audioAddSourceTitle,
toolbar: 'add-audio-source',
media: this.media,
menu: false
} )
]);
}
});
/**
* Audio widget model.
*
* See WP_Widget_Audio::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class AudioWidgetModel
* @constructor
*/
AudioWidgetModel = component.MediaWidgetModel.extend( {} );
/**
* Audio widget control.
*
* See WP_Widget_Audio::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class AudioWidgetModel
* @constructor
*/
AudioWidgetControl = component.MediaWidgetControl.extend( {
/**
* Show display settings.
*
* @type {boolean}
*/
showDisplaySettings: false,
/**
* Map model props to media frame props.
*
* @param {Object} modelProps - Model props.
* @returns {Object} Media frame props.
*/
mapModelToMediaFrameProps: function mapModelToMediaFrameProps( modelProps ) {
var control = this, mediaFrameProps;
mediaFrameProps = component.MediaWidgetControl.prototype.mapModelToMediaFrameProps.call( control, modelProps );
mediaFrameProps.link = 'embed';
return mediaFrameProps;
},
/**
* Render preview.
*
* @returns {void}
*/
renderPreview: function renderPreview() {
var control = this, previewContainer, previewTemplate, attachmentId, attachmentUrl;
attachmentId = control.model.get( 'attachment_id' );
attachmentUrl = control.model.get( 'url' );
if ( ! attachmentId && ! attachmentUrl ) {
return;
}
previewContainer = control.$el.find( '.media-widget-preview' );
previewTemplate = wp.template( 'wp-media-widget-audio-preview' );
previewContainer.html( previewTemplate( {
model: {
attachment_id: control.model.get( 'attachment_id' ),
src: attachmentUrl
},
error: control.model.get( 'error' )
} ) );
wp.mediaelement.initialize();
},
/**
* Open the media audio-edit frame to modify the selected item.
*
* @returns {void}
*/
editMedia: function editMedia() {
var control = this, mediaFrame, metadata, updateCallback;
metadata = control.mapModelToMediaFrameProps( control.model.toJSON() );
// Set up the media frame.
mediaFrame = new AudioDetailsMediaFrame({
frame: 'audio',
state: 'audio-details',
metadata: metadata
} );
wp.media.frame = mediaFrame;
mediaFrame.$el.addClass( 'media-widget' );
updateCallback = function( mediaFrameProps ) {
// Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview.
control.selectedAttachment.set( mediaFrameProps );
control.model.set( _.extend(
control.model.defaults(),
control.mapMediaToModelProps( mediaFrameProps ),
{ error: false }
) );
};
mediaFrame.state( 'audio-details' ).on( 'update', updateCallback );
mediaFrame.state( 'replace-audio' ).on( 'replace', updateCallback );
mediaFrame.on( 'close', function() {
mediaFrame.detach();
});
mediaFrame.open();
}
} );
// Exports.
component.controlConstructors.media_audio = AudioWidgetControl;
component.modelConstructors.media_audio = AudioWidgetModel;
})( wp.mediaWidgets );

View File

@ -0,0 +1,129 @@
/* eslint consistent-this: [ "error", "control" ] */
(function( component, $ ) {
'use strict';
var ImageWidgetModel, ImageWidgetControl;
/**
* Image widget model.
*
* See WP_Widget_Media_Image::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class ImageWidgetModel
* @constructor
*/
ImageWidgetModel = component.MediaWidgetModel.extend({});
/**
* Image widget control.
*
* See WP_Widget_Media_Image::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class ImageWidgetModel
* @constructor
*/
ImageWidgetControl = component.MediaWidgetControl.extend({
/**
* Render preview.
*
* @returns {void}
*/
renderPreview: function renderPreview() {
var control = this, previewContainer, previewTemplate;
if ( ! control.model.get( 'attachment_id' ) && ! control.model.get( 'url' ) ) {
return;
}
previewContainer = control.$el.find( '.media-widget-preview' );
previewTemplate = wp.template( 'wp-media-widget-image-preview' );
previewContainer.html( previewTemplate( _.extend( control.previewTemplateProps.toJSON() ) ) );
},
/**
* Open the media image-edit frame to modify the selected item.
*
* @returns {void}
*/
editMedia: function editMedia() {
var control = this, mediaFrame, updateCallback, defaultSync, metadata;
metadata = control.mapModelToMediaFrameProps( control.model.toJSON() );
// Needed or else none will not be selected if linkUrl is not also empty.
if ( 'none' === metadata.link ) {
metadata.linkUrl = '';
}
// Set up the media frame.
mediaFrame = wp.media({
frame: 'image',
state: 'image-details',
metadata: metadata
});
mediaFrame.$el.addClass( 'media-widget' );
updateCallback = function() {
var mediaProps;
// Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview.
mediaProps = mediaFrame.state().attributes.image.toJSON();
control.selectedAttachment.set( mediaProps );
control.model.set( _.extend(
control.mapMediaToModelProps( mediaProps ),
{ error: false }
) );
};
mediaFrame.state( 'image-details' ).on( 'update', updateCallback );
mediaFrame.state( 'replace-image' ).on( 'replace', updateCallback );
// 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() {
mediaFrame.detach();
wp.media.model.Attachment.prototype.sync = defaultSync;
});
mediaFrame.open();
},
/**
* Get props which are merged on top of the model when an embed is chosen (as opposed to an attachment).
*
* @returns {Object} Reset/override props.
*/
getEmbedResetProps: function getEmbedResetProps() {
return _.extend(
component.MediaWidgetControl.prototype.getEmbedResetProps.call( this ),
{
size: 'full',
width: 0,
height: 0
}
);
},
/**
* Map model props to preview template props.
*
* @returns {Object} Preview template props.
*/
mapModelToPreviewTemplateProps: function mapModelToPreviewTemplateProps() {
var control = this, mediaFrameProps, url;
url = control.model.get( 'url' );
mediaFrameProps = component.MediaWidgetControl.prototype.mapModelToPreviewTemplateProps.call( control );
mediaFrameProps.currentFilename = url ? url.replace( /\?.*$/, '' ).replace( /^.+\//, '' ) : '';
return mediaFrameProps;
}
});
// Exports.
component.controlConstructors.media_image = ImageWidgetControl;
component.modelConstructors.media_image = ImageWidgetModel;
})( wp.mediaWidgets, jQuery );

View File

@ -0,0 +1,228 @@
/* eslint consistent-this: [ "error", "control" ] */
(function( component ) {
'use strict';
var VideoWidgetModel, VideoWidgetControl, VideoDetailsMediaFrame;
/**
* Custom video details frame that removes the replace-video state.
*
* @class VideoDetailsMediaFrame
* @constructor
*/
VideoDetailsMediaFrame = wp.media.view.MediaFrame.VideoDetails.extend({
/**
* Create the default states.
*
* @returns {void}
*/
createStates: function createStates() {
this.states.add([
new wp.media.controller.VideoDetails({
media: this.media
}),
new wp.media.controller.MediaLibrary( {
type: 'video',
id: 'add-video-source',
title: wp.media.view.l10n.videoAddSourceTitle,
toolbar: 'add-video-source',
media: this.media,
menu: false
} ),
new wp.media.controller.MediaLibrary( {
type: 'text',
id: 'add-track',
title: wp.media.view.l10n.videoAddTrackTitle,
toolbar: 'add-track',
media: this.media,
menu: 'video-details'
} )
]);
}
});
/**
* Video widget model.
*
* See WP_Widget_Video::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class VideoWidgetModel
* @constructor
*/
VideoWidgetModel = component.MediaWidgetModel.extend( {} );
/**
* Video widget control.
*
* See WP_Widget_Video::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class VideoWidgetControl
* @constructor
*/
VideoWidgetControl = component.MediaWidgetControl.extend( {
/**
* Show display settings.
*
* @type {boolean}
*/
showDisplaySettings: false,
/**
* Cache of oembed responses.
*
* @type {Object}
*/
oembedResponses: {},
/**
* Map model props to media frame props.
*
* @param {Object} modelProps - Model props.
* @returns {Object} Media frame props.
*/
mapModelToMediaFrameProps: function mapModelToMediaFrameProps( modelProps ) {
var control = this, mediaFrameProps;
mediaFrameProps = component.MediaWidgetControl.prototype.mapModelToMediaFrameProps.call( control, modelProps );
mediaFrameProps.link = 'embed';
return mediaFrameProps;
},
/**
* Fetches embed data for external videos.
*
* @returns {void}
*/
fetchEmbed: function fetchEmbed() {
var control = this, url;
url = control.model.get( 'url' );
// If we already have a local cache of the embed response, return.
if ( control.oembedResponses[ url ] ) {
return;
}
// If there is an in-flight embed request, abort it.
if ( control.fetchEmbedDfd && 'pending' === control.fetchEmbedDfd.state() ) {
control.fetchEmbedDfd.abort();
}
control.fetchEmbedDfd = jQuery.ajax({
url: 'https://noembed.com/embed',
data: {
url: control.model.get( 'url' ),
maxwidth: control.model.get( 'width' ),
maxheight: control.model.get( 'height' )
},
type: 'GET',
crossDomain: true,
dataType: 'json'
});
control.fetchEmbedDfd.done( function( response ) {
control.oembedResponses[ url ] = response;
control.renderPreview();
});
control.fetchEmbedDfd.fail( function() {
control.oembedResponses[ url ] = null;
});
},
/**
* Render preview.
*
* @returns {void}
*/
renderPreview: function renderPreview() {
var control = this, previewContainer, previewTemplate, attachmentId, attachmentUrl, poster, isHostedEmbed = false, parsedUrl, mime, error;
attachmentId = control.model.get( 'attachment_id' );
attachmentUrl = control.model.get( 'url' );
error = control.model.get( 'error' );
if ( ! attachmentId && ! attachmentUrl ) {
return;
}
if ( ! attachmentId && attachmentUrl ) {
parsedUrl = document.createElement( 'a' );
parsedUrl.href = attachmentUrl;
isHostedEmbed = /vimeo|youtu\.?be/.test( parsedUrl.host );
}
if ( isHostedEmbed ) {
control.fetchEmbed();
poster = control.oembedResponses[ attachmentUrl ] ? control.oembedResponses[ attachmentUrl ].thumbnail_url : null;
}
// Verify the selected attachment mime is supported.
mime = control.selectedAttachment.get( 'mime' );
if ( mime && attachmentId ) {
if ( ! _.contains( _.values( wp.media.view.settings.embedMimes ), mime ) ) {
error = 'unsupported_file_type';
}
}
previewContainer = control.$el.find( '.media-widget-preview' );
previewTemplate = wp.template( 'wp-media-widget-video-preview' );
previewContainer.html( previewTemplate( {
model: {
attachment_id: control.model.get( 'attachment_id' ),
src: attachmentUrl,
poster: poster
},
is_hosted_embed: isHostedEmbed,
error: error
} ) );
},
/**
* Open the media image-edit frame to modify the selected item.
*
* @returns {void}
*/
editMedia: function editMedia() {
var control = this, mediaFrame, metadata, updateCallback;
metadata = control.mapModelToMediaFrameProps( control.model.toJSON() );
// Set up the media frame.
mediaFrame = new VideoDetailsMediaFrame({
frame: 'video',
state: 'video-details',
metadata: metadata
});
wp.media.frame = mediaFrame;
mediaFrame.$el.addClass( 'media-widget' );
updateCallback = function( mediaFrameProps ) {
// Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview.
control.selectedAttachment.set( mediaFrameProps );
control.model.set( _.extend(
_.omit( control.model.defaults(), 'title' ),
control.mapMediaToModelProps( mediaFrameProps ),
{ error: false }
) );
};
mediaFrame.state( 'video-details' ).on( 'update', updateCallback );
mediaFrame.state( 'replace-video' ).on( 'replace', updateCallback );
mediaFrame.on( 'close', function() {
mediaFrame.detach();
});
mediaFrame.open();
}
} );
// Exports.
component.controlConstructors.media_video = VideoWidgetControl;
component.modelConstructors.media_video = VideoWidgetModel;
})( wp.mediaWidgets );

File diff suppressed because it is too large Load Diff

View File

@ -841,6 +841,9 @@ img.aligncenter {
padding: 4px;
text-align: center;
}
.widget-container .wp-caption {
max-width: 100% !important;
}
.wp-caption img {
margin: 5px 5px 0;
max-width: 622px; /* caption width - 10px */

View File

@ -19,6 +19,21 @@ require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-search.php' );
/** WP_Widget_Archives class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-archives.php' );
/** WP_Widget_Media class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media.php' );
/** WP_Widget_Media_Audio class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-audio.php' );
/** WP_Widget_Media_Image class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-image.php' );
/** WP_Widget_Media_Video class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-video.php' );
/** WP_Widget_Meta class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-meta.php' );
/** WP_Widget_Meta class */
require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-meta.php' );

View File

@ -469,6 +469,15 @@ wp.customize.selectiveRefresh = ( function( $, api ) {
// Prevent placement container from being being re-triggered as being rendered among nested partials.
placement.container.data( 'customize-partial-content-rendered', true );
/*
* Note that the 'wp_audio_shortcode_library' and 'wp_video_shortcode_library' filters
* will determine whether or not wp.mediaelement is loaded and whether it will
* initialize audio and video respectively. See also https://core.trac.wordpress.org/ticket/40144
*/
if ( wp.mediaelement ) {
wp.mediaelement.initialize();
}
/**
* Announce when a partial's placement has been rendered so that dynamic elements can be re-built.
*/

View File

@ -608,7 +608,7 @@ function wp_print_media_templates() {
<h2><?php _e( 'Attachment Display Settings' ); ?></h2>
<# if ( 'image' === data.type ) { #>
<label class="setting">
<label class="setting align">
<span><?php _e('Alignment'); ?></span>
<select class="alignment"
data-setting="align"
@ -1087,7 +1087,7 @@ function wp_print_media_templates() {
</div>
</div>
<label class="setting checkbox-setting">
<label class="setting checkbox-setting autoplay">
<input type="checkbox" data-setting="autoplay" />
<span><?php _e( 'Autoplay' ); ?></span>
</label>
@ -1176,7 +1176,7 @@ function wp_print_media_templates() {
</div>
</div>
<label class="setting checkbox-setting">
<label class="setting checkbox-setting autoplay">
<input type="checkbox" data-setting="autoplay" />
<span><?php _e( 'Autoplay' ); ?></span>
</label>

View File

@ -602,6 +602,12 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'admin-gallery', "/wp-admin/js/gallery$suffix.js", array( 'jquery-ui-sortable' ) );
$scripts->add( 'admin-widgets', "/wp-admin/js/widgets$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable' ), false, 1 );
$scripts->add( 'media-widgets', "/wp-admin/js/widgets/media-widgets$suffix.js", array( 'jquery', 'media-models', 'media-views' ) );
$scripts->add_inline_script( 'media-widgets', 'wp.mediaWidgets.init();', 'after' );
$scripts->add( 'media-audio-widget', "/wp-admin/js/widgets/media-audio-widget$suffix.js", array( 'media-widgets', 'media-audiovideo' ) );
$scripts->add( 'media-image-widget', "/wp-admin/js/widgets/media-image-widget$suffix.js", array( 'media-widgets' ) );
$scripts->add( 'media-video-widget', "/wp-admin/js/widgets/media-video-widget$suffix.js", array( 'media-widgets', 'media-audiovideo' ) );
$scripts->add( 'text-widgets', "/wp-admin/js/widgets/text-widgets$suffix.js", array( 'jquery', 'backbone', 'editor', 'wp-util' ) );
$scripts->add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );

View File

@ -1436,35 +1436,43 @@ function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
* @since 2.2.0
*/
function wp_widgets_init() {
if ( !is_blog_installed() )
if ( ! is_blog_installed() ) {
return;
}
register_widget('WP_Widget_Pages');
register_widget( 'WP_Widget_Pages' );
register_widget('WP_Widget_Calendar');
register_widget( 'WP_Widget_Calendar' );
register_widget('WP_Widget_Archives');
register_widget( 'WP_Widget_Archives' );
if ( get_option( 'link_manager_enabled' ) )
register_widget('WP_Widget_Links');
if ( get_option( 'link_manager_enabled' ) ) {
register_widget( 'WP_Widget_Links' );
}
register_widget('WP_Widget_Meta');
register_widget( 'WP_Widget_Media_Audio' );
register_widget('WP_Widget_Search');
register_widget( 'WP_Widget_Media_Image' );
register_widget('WP_Widget_Text');
register_widget( 'WP_Widget_Media_Video' );
register_widget('WP_Widget_Categories');
register_widget( 'WP_Widget_Meta' );
register_widget('WP_Widget_Recent_Posts');
register_widget( 'WP_Widget_Search' );
register_widget('WP_Widget_Recent_Comments');
register_widget( 'WP_Widget_Text' );
register_widget('WP_Widget_RSS');
register_widget( 'WP_Widget_Categories' );
register_widget('WP_Widget_Tag_Cloud');
register_widget( 'WP_Widget_Recent_Posts' );
register_widget('WP_Nav_Menu_Widget');
register_widget( 'WP_Widget_Recent_Comments' );
register_widget( 'WP_Widget_RSS' );
register_widget( 'WP_Widget_Tag_Cloud' );
register_widget( 'WP_Nav_Menu_Widget' );
/**
* Fires after all default WordPress widgets have been registered.

View File

@ -0,0 +1,204 @@
<?php
/**
* Widget API: WP_Widget_Media_Audio class
*
* @package WordPress
* @subpackage Widgets
* @since 4.8.0
*/
/**
* Core class that implements an audio widget.
*
* @since 4.8.0
*
* @see WP_Widget
*/
class WP_Widget_Media_Audio extends WP_Widget_Media {
/**
* Constructor.
*
* @since 4.8.0
* @access public
*/
public function __construct() {
parent::__construct( 'media_audio', __( 'Audio' ), array(
'description' => __( 'Displays an audio player.' ),
'mime_type' => 'audio',
) );
$this->l10n = array_merge( $this->l10n, array(
'no_media_selected' => __( 'No audio selected' ),
'add_media' => _x( 'Add File', 'label for button in the audio widget; should not be longer than ~13 characters long' ),
'replace_media' => _x( 'Replace Audio', 'label for button in the audio widget; should not be longer than ~13 characters long' ),
'edit_media' => _x( 'Edit Audio', 'label for button in the audio widget; should not be longer than ~13 characters long' ),
'missing_attachment' => sprintf(
/* translators: placeholder is URL to media library */
__( 'We can&#8217;t find that audio file. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
esc_url( admin_url( 'upload.php' ) )
),
/* translators: %d is widget count */
'media_library_state_multi' => _n_noop( 'Audio Widget (%d)', 'Audio Widget (%d)' ),
'media_library_state_single' => __( 'Audio Widget' ),
'unsupported_file_type' => __( 'Looks like this isn&#8217;t the correct kind of file. Please link to an audio file instead.' ),
) );
}
/**
* 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() {
$schema = array_merge(
parent::get_instance_schema(),
array(
'preload' => array(
'type' => 'string',
'enum' => array( 'none', 'auto', 'metadata' ),
'default' => 'none',
),
'loop' => array(
'type' => 'boolean',
'default' => false,
),
)
);
foreach ( wp_get_audio_extensions() as $audio_extension ) {
$schema[ $audio_extension ] = array(
'type' => 'string',
'default' => '',
'format' => 'uri',
/* translators: placeholder is audio extension */
'description' => sprintf( __( 'URL to the %s audio source file' ), $audio_extension ),
);
}
return $schema;
}
/**
* 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 );
$attachment = null;
if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
$attachment = get_post( $instance['attachment_id'] );
}
if ( $attachment ) {
$src = wp_get_attachment_url( $attachment->ID );
} else {
$src = $instance['url'];
}
echo wp_audio_shortcode(
array_merge(
$instance,
compact( 'src' )
)
);
}
/**
* Enqueue preview scripts.
*
* These scripts normally are enqueued just-in-time when an audio shortcode is used.
* In the customizer, however, widgets can be dynamically added and rendered via
* selective refresh, and so it is important to unconditionally enqueue them in
* case a widget does get added.
*
* @since 4.8.0
* @access public
*/
public function enqueue_preview_scripts() {
/** This filter is documented in wp-includes/media.php */
if ( 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ) ) {
wp_enqueue_style( 'wp-mediaelement' );
wp_enqueue_script( 'wp-mediaelement' );
}
}
/**
* 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();
wp_enqueue_style( 'wp-mediaelement' );
wp_enqueue_script( 'wp-mediaelement' );
$handle = 'media-audio-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;
wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.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()
?>
<script type="text/html" id="tmpl-wp-media-widget-audio-preview">
<# if ( data.error && 'missing_attachment' === data.error ) { #>
<div class="notice notice-error notice-alt notice-missing-attachment">
<p><?php echo $this->l10n['missing_attachment']; ?></p>
</div>
<# } else if ( data.error ) { #>
<div class="notice notice-error notice-alt">
<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
</div>
<# } else if ( data.model && data.model.src ) { #>
<?php wp_underscore_audio_template() ?>
<# } #>
</script>
<?php
}
}

View File

@ -0,0 +1,326 @@
<?php
/**
* Widget API: WP_Widget_Media_Image class
*
* @package WordPress
* @subpackage Widgets
* @since 4.8.0
*/
/**
* Core class that implements an image widget.
*
* @since 4.8.0
*
* @see WP_Widget
*/
class WP_Widget_Media_Image extends WP_Widget_Media {
/**
* Constructor.
*
* @since 4.8.0
* @access public
*/
public function __construct() {
parent::__construct( 'media_image', __( 'Image' ), array(
'description' => __( 'Displays an image.' ),
'mime_type' => 'image',
) );
$this->l10n = array_merge( $this->l10n, array(
'no_media_selected' => __( 'No image selected' ),
'add_media' => _x( 'Add Image', 'label for button in the image widget; should not be longer than ~13 characters long' ),
'replace_media' => _x( 'Replace Image', 'label for button in the image widget; should not be longer than ~13 characters long' ),
'edit_media' => _x( 'Edit Image', 'label for button in the image widget; should not be longer than ~13 characters long' ),
'missing_attachment' => sprintf(
/* translators: placeholder is URL to media library */
__( 'We can&#8217;t find that image. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
esc_url( admin_url( 'upload.php' ) )
),
/* translators: %d is widget count */
'media_library_state_multi' => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
'media_library_state_single' => __( 'Image Widget' ),
) );
}
/**
* 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(
'size' => array(
'type' => 'string',
'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
'default' => 'medium',
),
'width' => array( // Via 'customWidth', only when size=custom; otherwise via 'width'.
'type' => 'integer',
'minimum' => 0,
'default' => 0,
),
'height' => array( // Via 'customHeight', only when size=custom; otherwise via 'height'.
'type' => 'integer',
'minimum' => 0,
'default' => 0,
),
'caption' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => 'wp_kses_post',
'should_preview_update' => false,
),
'alt' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'link_type' => array(
'type' => 'string',
'enum' => array( 'none', 'file', 'post', 'custom' ),
'default' => 'none',
'media_prop' => 'link',
'should_preview_update' => false,
),
'link_url' => array(
'type' => 'string',
'default' => '',
'format' => 'uri',
'media_prop' => 'linkUrl',
'should_preview_update' => false,
),
'image_classes' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => array( $this, 'sanitize_token_list' ),
'media_prop' => 'extraClasses',
'should_preview_update' => false,
),
'link_classes' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => array( $this, 'sanitize_token_list' ),
'media_prop' => 'linkClassName',
'should_preview_update' => false,
),
'link_rel' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => array( $this, 'sanitize_token_list' ),
'media_prop' => 'linkRel',
'should_preview_update' => false,
),
'link_target_blank' => array( // Via 'linkTargetBlank' property.
'type' => 'boolean',
'default' => false,
'media_prop' => 'linkTargetBlank',
'should_preview_update' => false,
),
'image_title' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
'media_prop' => 'title',
'should_preview_update' => false,
),
/*
* There are two additional properties exposed by the PostImage modal
* that don't seem to be relevant, as they may only be derived read-only
* values:
* - originalUrl
* - aspectRatio
* - height (redundant when size is not custom)
* - width (redundant when size is not custom)
*/
)
);
}
/**
* 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 );
$instance = wp_parse_args( $instance, array(
'size' => 'thumbnail',
) );
$attachment = null;
if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
$attachment = get_post( $instance['attachment_id'] );
}
if ( $attachment ) {
$caption = $attachment->post_excerpt;
if ( $instance['caption'] ) {
$caption = $instance['caption'];
}
$image_attributes = array(
'class' => sprintf( 'image wp-image-%d %s', $attachment->ID, $instance['image_classes'] ),
'style' => 'max-width: 100%; height: auto;',
);
if ( ! empty( $instance['image_title'] ) ) {
$image_attributes['title'] = $instance['image_title'];
}
if ( $instance['alt'] ) {
$image_attributes['alt'] = $instance['alt'];
}
$size = $instance['size'];
if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
$size = array( $instance['width'], $instance['height'] );
}
$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
$width = empty( $caption_size[0] ) ? 0 : $caption_size[0];
} else {
if ( empty( $instance['url'] ) ) {
return;
}
$instance['size'] = 'custom';
$caption = $instance['caption'];
$width = $instance['width'];
$classes = 'image ' . $instance['image_classes'];
if ( 0 === $instance['width'] ) {
$instance['width'] = '';
}
if ( 0 === $instance['height'] ) {
$instance['height'] = '';
}
$image = sprintf( '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />',
esc_attr( $classes ),
esc_url( $instance['url'] ),
esc_attr( $instance['alt'] ),
esc_attr( $instance['width'] ),
esc_attr( $instance['height'] )
);
} // End if().
$url = '';
if ( 'file' === $instance['link_type'] ) {
$url = $attachment ? wp_get_attachment_url( $attachment->ID ) : $instance['url'];
} elseif ( $attachment && 'post' === $instance['link_type'] ) {
$url = get_attachment_link( $attachment->ID );
} elseif ( 'custom' === $instance['link_type'] && ! empty( $instance['link_url'] ) ) {
$url = $instance['link_url'];
}
if ( $url ) {
$image = sprintf(
'<a href="%1$s" class="%2$s" rel="%3$s" target="%4$s">%5$s</a>',
esc_url( $url ),
esc_attr( $instance['link_classes'] ),
esc_attr( $instance['link_rel'] ),
! empty( $instance['link_target_blank'] ) ? '_blank' : '',
$image
);
}
if ( $caption ) {
$image = img_caption_shortcode( array(
'width' => $width,
'caption' => $caption,
), $image );
}
echo $image;
}
/**
* 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-image-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;
wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.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();
?>
<script type="text/html" id="tmpl-wp-media-widget-image-preview">
<#
var describedById = 'describedBy-' + String( Math.random() );
#>
<# if ( data.error && 'missing_attachment' === data.error ) { #>
<div class="notice notice-error notice-alt notice-missing-attachment">
<p><?php echo $this->l10n['missing_attachment']; ?></p>
</div>
<# } else if ( data.error ) { #>
<div class="notice notice-error notice-alt">
<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
</div>
<# } else if ( data.url ) { #>
<img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}" <# if ( ! data.alt && data.currentFilename ) { #> aria-describedby="{{ describedById }}" <# } #> />
<# if ( ! data.alt && data.currentFilename ) { #>
<p class="hidden" id="{{ describedById }}"><?php
/* translators: placeholder is image filename */
echo sprintf( __( 'Current image: %s' ), '{{ data.currentFilename }}' );
?></p>
<# } #>
<# } #>
</script>
<?php
}
}

View File

@ -0,0 +1,255 @@
<?php
/**
* Widget API: WP_Widget_Media_Video class
*
* @package WordPress
* @subpackage Widgets
* @since 4.8.0
*/
/**
* Core class that implements a video widget.
*
* @since 4.8.0
*
* @see WP_Widget
*/
class WP_Widget_Media_Video extends WP_Widget_Media {
/**
* Constructor.
*
* @since 4.8.0
* @access public
*/
public function __construct() {
parent::__construct( 'media_video', __( 'Video' ), array(
'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ),
'mime_type' => 'video',
) );
$this->l10n = array_merge( $this->l10n, array(
'no_media_selected' => __( 'No video selected' ),
'add_media' => _x( 'Add Video', 'label for button in the video widget; should not be longer than ~13 characters long' ),
'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should not be longer than ~13 characters long' ),
'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should not be longer than ~13 characters long' ),
'missing_attachment' => sprintf(
/* translators: placeholder is URL to media library */
__( 'We can&#8217;t find that video. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
esc_url( admin_url( 'upload.php' ) )
),
/* translators: %d is widget count */
'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ),
'media_library_state_single' => __( 'Video Widget' ),
/* translators: placeholder is a list of valid video file extensions */
'unsupported_file_type' => sprintf( __( 'Sorry, we can&#8217;t display the video file type selected. Please select a supported video file (%1$s) or stream (YouTube or Vimeo) instead.' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ),
) );
}
/**
* 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() {
$schema = array_merge(
parent::get_instance_schema(),
array(
'preload' => array(
'type' => 'string',
'enum' => array( 'none', 'auto', 'metadata' ),
'default' => 'metadata',
'should_preview_update' => false,
),
'loop' => array(
'type' => 'boolean',
'default' => false,
'should_preview_update' => false,
),
'content' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => 'wp_kses_post',
'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ),
'should_preview_update' => false,
),
)
);
foreach ( wp_get_video_extensions() as $video_extension ) {
$schema[ $video_extension ] = array(
'type' => 'string',
'default' => '',
'format' => 'uri',
/* translators: placeholder is video extension */
'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ),
);
}
return $schema;
}
/**
* 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 );
$attachment = null;
if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
$attachment = get_post( $instance['attachment_id'] );
}
if ( $attachment ) {
$src = wp_get_attachment_url( $attachment->ID );
} else {
// Manually add the loop query argument.
$loop = $instance['loop'] ? '1' : '0';
$src = empty( $instance['url'] ) ? $instance['url'] : add_query_arg( 'loop', $loop, $instance['url'] );
}
if ( empty( $src ) ) {
return;
}
add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
echo wp_video_shortcode(
array_merge(
$instance,
compact( 'src' )
),
$instance['content']
);
remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
}
/**
* Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
*
* @since 4.8.0
* @access public
*
* @param string $html Video shortcode HTML output.
* @return string HTML Output.
*/
public function inject_video_max_width_style( $html ) {
$html = preg_replace( '/\sheight="\d+"/', '', $html );
$html = preg_replace( '/\swidth="\d+"/', '', $html );
$html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
return $html;
}
/**
* Enqueue preview scripts.
*
* These scripts normally are enqueued just-in-time when a video shortcode is used.
* In the customizer, however, widgets can be dynamically added and rendered via
* selective refresh, and so it is important to unconditionally enqueue them in
* case a widget does get added.
*
* @since 4.8.0
* @access public
*/
public function enqueue_preview_scripts() {
/** This filter is documented in wp-includes/media.php */
if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
wp_enqueue_style( 'wp-mediaelement' );
wp_enqueue_script( 'wp-mediaelement' );
}
// Enqueue script needed by Vimeo; see wp_video_shortcode().
wp_enqueue_script( 'froogaloop' );
}
/**
* Loads the required scripts and styles for the widget control.
*
* @since 4.8.0
* @access public
*/
public function enqueue_admin_scripts() {
parent::enqueue_admin_scripts();
$handle = 'media-video-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;
wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.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()
?>
<script type="text/html" id="tmpl-wp-media-widget-video-preview">
<# if ( data.error && 'missing_attachment' === data.error ) { #>
<div class="notice notice-error notice-alt notice-missing-attachment">
<p><?php echo $this->l10n['missing_attachment']; ?></p>
</div>
<# } else if ( data.error && 'unsupported_file_type' === data.error ) { #>
<div class="notice notice-error notice-alt notice-missing-attachment">
<p><?php echo $this->l10n['unsupported_file_type']; ?></p>
</div>
<# } else if ( data.error ) { #>
<div class="notice notice-error notice-alt">
<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
</div>
<# } else if ( data.is_hosted_embed && data.model.poster ) { #>
<a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
<img src="{{ data.model.poster }}" />
</a>
<# } else if ( data.is_hosted_embed ) { #>
<a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
<span class="dashicons dashicons-format-video"></span>
</a>
<# } else if ( data.model.src ) { #>
<?php wp_underscore_video_template() ?>
<# } #>
</script>
<?php
}
}

View File

@ -0,0 +1,422 @@
<?php
/**
* Widget API: WP_Media_Widget class
*
* @package WordPress
* @subpackage Widgets
* @since 4.8.0
*/
/**
* Core class that implements a media widget.
*
* @since 4.8.0
*
* @see WP_Widget
*/
abstract class WP_Widget_Media extends WP_Widget {
/**
* Translation labels.
*
* @since 4.8.0
* @var array
*/
public $l10n = array(
'add_to_widget' => '',
'replace_media' => '',
'edit_media' => '',
'media_library_state_multi' => '',
'media_library_state_single' => '',
'missing_attachment' => '',
'no_media_selected' => '',
'add_media' => '',
);
/**
* Constructor.
*
* @since 4.8.0
* @access public
*
* @param string $id_base Base ID for the widget, lowercase and unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
* information on accepted arguments. Default empty array.
* @param array $control_options Optional. Widget control options. See wp_register_widget_control()
* for information on accepted arguments. Default empty array.
*/
public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
$widget_opts = wp_parse_args( $widget_options, array(
'description' => __( 'A media item.' ),
'customize_selective_refresh' => true,
'mime_type' => '',
) );
$control_opts = wp_parse_args( $control_options, array() );
$l10n_defaults = array(
'no_media_selected' => __( 'No media selected' ),
'add_media' => _x( 'Add Media', 'label for button in the media widget; should not be longer than ~13 characters long' ),
'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should not be longer than ~13 characters long' ),
'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should not be longer than ~13 characters long' ),
'add_to_widget' => __( 'Add to Widget' ),
'missing_attachment' => sprintf(
/* translators: placeholder is URL to media library */
__( 'We can&#8217;t find that file. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
esc_url( admin_url( 'upload.php' ) )
),
/* translators: %d is widget count */
'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
'media_library_state_single' => __( 'Media Widget' ),
'unsupported_file_type' => __( 'Looks like this isn&#8217;t the correct kind of file. Please link to an appropriate file instead.' ),
);
$this->l10n = array_merge( $l10n_defaults, array_filter( $this->l10n ) );
parent::__construct(
$id_base,
$name,
$widget_opts,
$control_opts
);
}
/**
* Add hooks while registering all widget instances of this widget class.
*
* @since 4.8.0
* @access public
*/
public function _register() {
// Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
if ( $this->is_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
}
// Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 );
parent::_register();
}
/**
* 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(
'attachment_id' => array(
'type' => 'integer',
'default' => 0,
'minimum' => 0,
'description' => __( 'Attachment post ID' ),
'media_prop' => 'id',
),
'url' => array(
'type' => 'string',
'default' => '',
'format' => 'uri',
'description' => __( 'URL to the media file' ),
),
'title' => array(
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
'description' => __( 'Title for the widget' ),
'should_preview_update' => false,
),
);
}
/**
* Determine if the supplied attachment is for a valid attachment post with the specified MIME type.
*
* @since 4.8.0
* @access public
*
* @param int|WP_Post $attachment Attachment post ID or object.
* @param string $mime_type MIME type.
* @return bool Is matching MIME type.
*/
public function is_attachment_with_mime_type( $attachment, $mime_type ) {
if ( empty( $attachment ) ) {
return false;
}
$attachment = get_post( $attachment );
if ( ! $attachment ) {
return false;
}
if ( 'attachment' !== $attachment->post_type ) {
return false;
}
return wp_attachment_is( $mime_type, $attachment );
}
/**
* Sanitize a token list string, such as used in HTML rel and class attributes.
*
* @since 4.8.0
* @access public
*
* @link http://w3c.github.io/html/infrastructure.html#space-separated-tokens
* @link https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList
* @param string|array $tokens List of tokens separated by spaces, or an array of tokens.
* @return string Sanitized token string list.
*/
public function sanitize_token_list( $tokens ) {
if ( is_string( $tokens ) ) {
$tokens = preg_split( '/\s+/', trim( $tokens ) );
}
$tokens = array_map( 'sanitize_html_class', $tokens );
$tokens = array_filter( $tokens );
return join( ' ', $tokens );
}
/**
* Displays the widget on the front-end.
*
* @since 4.8.0
* @access public
*
* @see WP_Widget::widget()
*
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
* @param array $instance Saved setting from the database.
*/
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, wp_list_pluck( $this->get_instance_schema(), 'default' ) );
// Short-circuit if no media is selected.
if ( ! $this->has_content( $instance ) ) {
return;
}
echo $args['before_widget'];
if ( $instance['title'] ) {
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
echo $args['before_title'] . $title . $args['after_title'];
}
/**
* Filters the media widget instance prior to rendering the media.
*
* @since 4.8.0
*
* @param array $instance Instance data.
* @param array $args Widget args.
* @param WP_Widget_Media $this Widget object.
*/
$instance = apply_filters( "widget_{$this->id_base}_instance", $instance, $args, $this );
$this->render_media( $instance );
echo $args['after_widget'];
}
/**
* Sanitizes the widget form values as they are saved.
*
* @since 4.8.0
* @access public
*
* @see WP_Widget::update()
* @see WP_REST_Request::has_valid_params()
* @see WP_REST_Request::sanitize_params()
*
* @param array $new_instance Values just sent to be saved.
* @param array $instance Previously saved values from database.
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $instance ) {
$schema = $this->get_instance_schema();
foreach ( $schema as $field => $field_schema ) {
if ( ! array_key_exists( $field, $new_instance ) ) {
continue;
}
$value = $new_instance[ $field ];
if ( true !== rest_validate_value_from_schema( $value, $field_schema, $field ) ) {
continue;
}
$value = rest_sanitize_value_from_schema( $value, $field_schema );
// @codeCoverageIgnoreStart
if ( is_wp_error( $value ) ) {
continue; // Handle case when rest_sanitize_value_from_schema() ever returns WP_Error as its phpdoc @return tag indicates.
}
// @codeCoverageIgnoreEnd
if ( isset( $field_schema['sanitize_callback'] ) ) {
$value = call_user_func( $field_schema['sanitize_callback'], $value );
}
if ( is_wp_error( $value ) ) {
continue;
}
$instance[ $field ] = $value;
}
return $instance;
}
/**
* Render the media on the frontend.
*
* @since 4.8.0
* @access public
*
* @param array $instance Widget instance props.
* @return string
*/
abstract public function render_media( $instance );
/**
* Outputs the settings update form.
*
* Note that the widget UI itself is rendered with JavaScript via `MediaWidgetControl#render()`.
*
* @since 4.8.0
* @access public
*
* @see \WP_Widget_Media::render_control_template_scripts() Where the JS template is located.
* @param array $instance Current settings.
* @return void
*/
final public function form( $instance ) {
$instance_schema = $this->get_instance_schema();
$instance = wp_array_slice_assoc(
wp_parse_args( (array) $instance, wp_list_pluck( $instance_schema, 'default' ) ),
array_keys( $instance_schema )
);
foreach ( $instance as $name => $value ) : ?>
<input
type="hidden"
data-property="<?php echo esc_attr( $name ); ?>"
class="media-widget-instance-property"
name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
id="<?php echo esc_attr( $this->get_field_id( $name ) ); // Needed specifically by wpWidgets.appendTitle(). ?>"
value="<?php echo esc_attr( strval( $value ) ); ?>"
/>
<?php
endforeach;
}
/**
* Filters the default media display states for items in the Media list table.
*
* @since 4.8.0
* @access public
*
* @param array $states An array of media states.
* @param WP_Post $post The current attachment object.
* @return array
*/
public function display_media_state( $states, $post = null ) {
if ( ! $post ) {
$post = get_post();
}
// Count how many times this attachment is used in widgets.
$use_count = 0;
foreach ( $this->get_settings() as $instance ) {
if ( isset( $instance['attachment_id'] ) && $instance['attachment_id'] === $post->ID ) {
$use_count++;
}
}
if ( 1 === $use_count ) {
$states[] = $this->l10n['media_library_state_single'];
} elseif ( $use_count > 0 ) {
$states[] = sprintf( translate_nooped_plural( $this->l10n['media_library_state_multi'], $use_count ), number_format_i18n( $use_count ) );
}
return $states;
}
/**
* Enqueue preview scripts.
*
* These scripts normally are enqueued just-in-time when a widget is rendered.
* In the customizer, however, widgets can be dynamically added and rendered via
* selective refresh, and so it is important to unconditionally enqueue them in
* case a widget does get added.
*
* @since 4.8.0
* @access public
*/
public function enqueue_preview_scripts() {}
/**
* Loads the required scripts and styles for the widget control.
*
* @since 4.8.0
* @access public
*/
public function enqueue_admin_scripts() {
wp_enqueue_media();
wp_enqueue_script( 'media-widgets' );
}
/**
* Render form template scripts.
*
* @since 4.8.0
* @access public
*/
public function render_control_template_scripts() {
?>
<script type="text/html" id="tmpl-widget-media-<?php echo esc_attr( $this->id_base ); ?>-control">
<# var elementIdPrefix = 'el' + String( Math.random() ) + '_' #>
<p>
<label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
<input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
</p>
<div class="media-widget-preview">
<div class="attachment-media-view">
<div class="placeholder"><?php echo esc_html( $this->l10n['no_media_selected'] ); ?></div>
</div>
</div>
<p class="media-widget-buttons">
<button type="button" class="button edit-media selected">
<?php echo esc_html( $this->l10n['edit_media'] ); ?>
</button>
<button type="button" class="button change-media select-media selected">
<?php echo esc_html( $this->l10n['replace_media'] ); ?>
</button>
<button type="button" class="button select-media not-selected">
<?php echo esc_html( $this->l10n['add_media'] ); ?>
</button>
</p>
</script>
<?php
}
/**
* Whether the widget has content to show.
*
* @since 4.8.0
* @access protected
*
* @param array $instance Widget instance props.
* @return bool Whether widget has content.
*/
protected function has_content( $instance ) {
return ( $instance['attachment_id'] && 'attachment' === get_post_type( $instance['attachment_id'] ) ) || $instance['url'];
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,266 @@
<?php
/**
* Unit tests covering WP_Widget_Media_Audio functionality.
*
* @package WordPress
* @subpackage widgets
*/
/**
* Test wp-includes/widgets/class-wp-widget-audio.php
*
* @group widgets
*/
class Test_WP_Widget_Media_Audio extends WP_UnitTestCase {
/**
* Clean up global scope.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
*/
function clean_up_global_scope() {
global $wp_scripts, $wp_styles;
parent::clean_up_global_scope();
$wp_scripts = null;
$wp_styles = null;
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media_Audio::get_instance_schema
*/
function test_get_instance_schema() {
$wp_widget_audio = new WP_Widget_Media_Audio();
$schema = $wp_widget_audio->get_instance_schema();
$this->assertEqualSets(
array_merge(
array(
'attachment_id',
'preload',
'loop',
'title',
'url',
),
wp_get_audio_extensions()
),
array_keys( $schema )
);
}
/**
* Test constructor.
*
* @covers WP_Widget_Media_Audio::__construct()
*/
function test_constructor() {
$widget = new WP_Widget_Media_Audio();
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
$this->assertArrayHasKey( 'description', $widget->widget_options );
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'audio', $widget->widget_options['mime_type'] );
$this->assertEqualSets( array(
'add_to_widget',
'replace_media',
'edit_media',
'media_library_state_multi',
'media_library_state_single',
'missing_attachment',
'no_media_selected',
'add_media',
'unsupported_file_type',
), array_keys( $widget->l10n ) );
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media_Audio::update
*/
function test_update() {
$widget = new WP_Widget_Media_Audio();
$instance = array();
// Should return valid attachment ID.
$expected = array(
'attachment_id' => 1,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment ID.
$result = $widget->update( array(
'attachment_id' => 'media',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid attachment url.
$expected = array(
'url' => 'https://chickenandribs.org',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment url.
$result = $widget->update( array(
'url' => 'not_a_url',
), $instance );
$this->assertNotSame( $result, $instance );
$this->assertStringStartsWith( 'http://', $result['url'] );
// Should return loop setting.
$expected = array(
'loop' => true,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid loop setting.
$result = $widget->update( array(
'loop' => 'not-boolean',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid attachment title.
$expected = array(
'title' => 'An audio sample of parrots',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment title.
$result = $widget->update( array(
'title' => '<h1>Cute Baby Goats</h1>',
), $instance );
$this->assertNotSame( $result, $instance );
// Should return valid preload setting.
$expected = array(
'preload' => 'none',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid preload setting.
$result = $widget->update( array(
'preload' => 'nope',
), $instance );
$this->assertSame( $result, $instance );
// Should filter invalid key.
$result = $widget->update( array(
'h4x' => 'value',
), $instance );
$this->assertSame( $result, $instance );
}
/**
* Test render_media method.
*
* @covers WP_Widget_Media_Audio::render_media
*/
function test_render_media() {
$test_audio_file = __FILE__ . '../../data/uploads/small-audio.mp3';
$widget = new WP_Widget_Media_Audio();
$attachment_id = self::factory()->attachment->create_object( array(
'file' => $test_audio_file,
'post_parent' => 0,
'post_mime_type' => 'audio/mp3',
'post_title' => 'Test Audio',
) );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $test_audio_file ) );
// Should be empty when there is no attachment_id.
ob_start();
$widget->render_media( array() );
$output = ob_get_clean();
$this->assertEmpty( $output );
// Should be empty when there is an invalid attachment_id.
ob_start();
$widget->render_media( array(
'attachment_id' => 777,
) );
$output = ob_get_clean();
$this->assertEmpty( $output );
// Tests with audio from library.
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
) );
$output = ob_get_clean();
// Check default outputs.
$this->assertContains( 'preload="none"', $output );
$this->assertContains( 'class="wp-audio-shortcode"', $output );
$this->assertContains( 'small-audio.mp3', $output );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'title' => 'Funny',
'preload' => 'auto',
'loop' => true,
) );
$output = ob_get_clean();
// Custom attributes.
$this->assertContains( 'preload="auto"', $output );
$this->assertContains( 'loop="1"', $output );
}
/**
* Test enqueue_preview_scripts method.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
* @covers WP_Widget_Media_Audio::enqueue_preview_scripts
*/
function test_enqueue_preview_scripts() {
global $wp_scripts, $wp_styles;
$wp_scripts = null;
$wp_styles = null;
$widget = new WP_Widget_Media_Audio();
$this->assertFalse( wp_script_is( 'wp-mediaelement' ) );
$this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
$widget->enqueue_preview_scripts();
$this->assertTrue( wp_script_is( 'wp-mediaelement' ) );
$this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
}
/**
* Test enqueue_admin_scripts method.
*
* @covers WP_Widget_Media_Audio::enqueue_admin_scripts
*/
function test_enqueue_admin_scripts() {
set_current_screen( 'widgets.php' );
$widget = new WP_Widget_Media_Audio();
$widget->enqueue_admin_scripts();
$this->assertTrue( wp_script_is( 'media-audio-widget' ) );
}
/**
* Test render_control_template_scripts method.
*
* @covers WP_Widget_Media_Audio::render_control_template_scripts
*/
function test_render_control_template_scripts() {
$widget = new WP_Widget_Media_Audio();
ob_start();
$widget->render_control_template_scripts();
$output = ob_get_clean();
$this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-audio-preview">', $output );
}
}

View File

@ -0,0 +1,480 @@
<?php
/**
* Unit tests covering WP_Widget_Media_Image functionality.
*
* @package WordPress
* @subpackage widgets
*/
/**
* Test wp-includes/widgets/class-wp-widget-image.php
*
* @group widgets
*/
class Test_WP_Widget_Media_Image extends WP_UnitTestCase {
/**
* Clean up global scope.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
*/
function clean_up_global_scope() {
global $wp_scripts, $wp_styles;
parent::clean_up_global_scope();
$wp_scripts = null;
$wp_styles = null;
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media_Image::get_instance_schema
*/
function test_get_instance_schema() {
$widget = new WP_Widget_Media_Image();
$schema = $widget->get_instance_schema();
$this->assertEqualSets( array(
'alt',
'attachment_id',
'caption',
'height',
'image_classes',
'image_title',
'link_classes',
'link_rel',
'link_target_blank',
'link_type',
'link_url',
'size',
'title',
'url',
'width',
), array_keys( $schema ) );
}
/**
* Test constructor.
*
* @covers WP_Widget_Media_Image::__construct()
*/
function test_constructor() {
$widget = new WP_Widget_Media_Image();
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
$this->assertArrayHasKey( 'description', $widget->widget_options );
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'image', $widget->widget_options['mime_type'] );
$this->assertEqualSets( array(
'add_to_widget',
'replace_media',
'edit_media',
'media_library_state_multi',
'media_library_state_single',
'missing_attachment',
'no_media_selected',
'add_media',
'unsupported_file_type',
), array_keys( $widget->l10n ) );
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media_Image::update
*/
function test_update() {
$widget = new WP_Widget_Media_Image();
$instance = array();
// Should return valid attachment ID.
$expected = array(
'attachment_id' => 1,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment ID.
$result = $widget->update( array(
'attachment_id' => 'media',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid attachment url.
$expected = array(
'url' => 'https://example.org',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment url.
$result = $widget->update( array(
'url' => 'not_a_url',
), $instance );
$this->assertNotSame( $result, $instance );
$this->assertStringStartsWith( 'http://', $result['url'] );
// Should return valid attachment title.
$expected = array(
'title' => 'What a title',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment title.
$result = $widget->update( array(
'title' => '<h1>W00t!</h1>',
), $instance );
$this->assertNotSame( $result, $instance );
// Should return valid image size.
$expected = array(
'size' => 'thumbnail',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid image size.
$result = $widget->update( array(
'size' => 'big league',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid image width.
$expected = array(
'width' => 300,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid image width.
$result = $widget->update( array(
'width' => 'wide',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid image height.
$expected = array(
'height' => 200,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid image height.
$result = $widget->update( array(
'height' => 'high',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid image caption.
$expected = array(
'caption' => 'A caption with <a href="#">link</a>',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid image caption.
$result = $widget->update( array(
'caption' => '"><i onload="alert(\'hello\')" />',
), $instance );
$this->assertSame( $result, array(
'caption' => '"&gt;<i />',
) );
// Should return valid alt text.
$expected = array(
'alt' => 'A water tower',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid alt text.
$result = $widget->update( array(
'alt' => '"><i onload="alert(\'hello\')" />',
), $instance );
$this->assertSame( $result, array(
'alt' => '">',
) );
// Should return valid link type.
$expected = array(
'link_type' => 'file',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid link type.
$result = $widget->update( array(
'link_type' => 'interesting',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid link url.
$expected = array(
'link_url' => 'https://example.org',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid link url.
$result = $widget->update( array(
'link_url' => 'not_a_url',
), $instance );
$this->assertNotSame( $result, $instance );
$this->assertStringStartsWith( 'http://', $result['link_url'] );
// Should return valid image classes.
$expected = array(
'image_classes' => 'A water tower',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid image classes.
$result = $widget->update( array(
'image_classes' => '"><i onload="alert(\'hello\')" />',
), $instance );
$this->assertSame( $result, array(
'image_classes' => 'i onloadalerthello',
) );
// Should return valid link classes.
$expected = array(
'link_classes' => 'A water tower',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid link classes.
$result = $widget->update( array(
'link_classes' => '"><i onload="alert(\'hello\')" />',
), $instance );
$this->assertSame( $result, array(
'link_classes' => 'i onloadalerthello',
) );
// Should return valid rel text.
$expected = array(
'link_rel' => 'previous',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid rel text.
$result = $widget->update( array(
'link_rel' => '"><i onload="alert(\'hello\')" />',
), $instance );
$this->assertSame( $result, array(
'link_rel' => 'i onloadalerthello',
) );
// Should return valid link target.
$expected = array(
'link_target_blank' => false,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid link target.
$result = $widget->update( array(
'link_target_blank' => 'top',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid image title.
$expected = array(
'image_title' => 'What a title',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid image title.
$result = $widget->update( array(
'image_title' => '<h1>W00t!</h1>',
), $instance );
$this->assertNotSame( $result, $instance );
// Should filter invalid key.
$result = $widget->update( array(
'imaginary_key' => 'value',
), $instance );
$this->assertSame( $result, $instance );
}
/**
* Test render_media method.
*
* @covers WP_Widget_Media_Image::render_media
*/
function test_render_media() {
$widget = new WP_Widget_Media_Image();
$attachment_id = self::factory()->attachment->create_object( array(
'file' => DIR_TESTDATA . '/images/canola.jpg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_title' => 'Canola',
) );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, DIR_TESTDATA . '/images/canola.jpg' ) );
// Should be empty when there is no attachment_id.
ob_start();
$widget->render_media( array() );
$output = ob_get_clean();
$this->assertEmpty( $output );
// Should be empty when there is an invalid attachment_id.
ob_start();
$widget->render_media( array(
'attachment_id' => 666,
) );
$output = ob_get_clean();
$this->assertEmpty( $output );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
) );
$output = ob_get_clean();
// No default title.
$this->assertNotContains( 'title="', $output );
// Default image classes.
$this->assertContains( 'class="image wp-image-' . $attachment_id, $output );
$this->assertContains( 'style="max-width: 100%; height: auto;"', $output );
$this->assertContains( 'alt=""', $output );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'image_title' => 'Custom Title',
'image_classes' => 'custom-class',
'alt' => 'A flower',
'size' => 'custom',
'width' => 100,
'height' => 100,
) );
$output = ob_get_clean();
// Custom image title.
$this->assertContains( 'title="Custom Title"', $output );
// Custom image class.
$this->assertContains( 'class="image wp-image-' . $attachment_id . ' custom-class', $output );
$this->assertContains( 'alt="A flower"', $output );
$this->assertContains( 'width="100"', $output );
$this->assertContains( 'height="100"', $output );
// Embeded images.
ob_start();
$widget->render_media( array(
'attachment_id' => null,
'caption' => 'With caption',
'height' => 100,
'link_type' => 'file',
'url' => 'http://example.org/url/to/image.jpg',
'width' => 100,
) );
$output = ob_get_clean();
// Custom image class.
$this->assertContains( 'src="http://example.org/url/to/image.jpg"', $output );
// Link settings.
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'link_type' => 'file',
) );
$output = ob_get_clean();
$link = '<a href="' . wp_get_attachment_url( $attachment_id ) . '"';
$this->assertContains( $link, $output );
$link .= ' class=""';
$this->assertContains( $link, $output );
$link .= ' rel=""';
$this->assertContains( $link, $output );
$link .= ' target=""';
$this->assertContains( $link, $output );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'link_type' => 'post',
'link_classes' => 'custom-link-class',
'link_rel' => 'attachment',
'link_target_blank' => false,
) );
$output = ob_get_clean();
$this->assertContains( '<a href="' . get_attachment_link( $attachment_id ) . '"', $output );
$this->assertContains( 'class="custom-link-class"', $output );
$this->assertContains( 'rel="attachment"', $output );
$this->assertContains( 'target=""', $output );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'link_type' => 'custom',
'link_url' => 'https://example.org',
'link_target_blank' => true,
) );
$output = ob_get_clean();
$this->assertContains( '<a href="https://example.org"', $output );
$this->assertContains( 'target="_blank"', $output );
// Caption settings.
wp_update_post( array(
'ID' => $attachment_id,
'post_excerpt' => 'Default caption',
) );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
) );
$output = ob_get_clean();
$this->assertContains( 'class="wp-caption alignnone"', $output );
$this->assertContains( '<p class="wp-caption-text">Default caption</p>', $output );
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'caption' => 'Custom caption',
) );
$output = ob_get_clean();
$this->assertContains( 'class="wp-caption alignnone"', $output );
$this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output );
}
/**
* Test enqueue_admin_scripts method.
*
* @covers WP_Widget_Media_Image::enqueue_admin_scripts
*/
function test_enqueue_admin_scripts() {
set_current_screen( 'widgets.php' );
$widget = new WP_Widget_Media_Image();
$widget->enqueue_admin_scripts();
$this->assertTrue( wp_script_is( 'media-image-widget' ) );
}
/**
* Test render_control_template_scripts method.
*
* @covers WP_Widget_Media_Image::render_control_template_scripts
*/
function test_render_control_template_scripts() {
$widget = new WP_Widget_Media_Image();
ob_start();
$widget->render_control_template_scripts();
$output = ob_get_clean();
$this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-image-preview">', $output );
}
}

View File

@ -0,0 +1,292 @@
<?php
/**
* Unit tests covering WP_Widget_Media_Video functionality.
*
* @package WordPress
* @subpackage widgets
*/
/**
* Test wp-includes/widgets/class-wp-widget-video.php
*
* @group widgets
*/
class Test_WP_Widget_Media_Video extends WP_UnitTestCase {
/**
* Clean up global scope.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
*/
function clean_up_global_scope() {
global $wp_scripts, $wp_styles;
parent::clean_up_global_scope();
$wp_scripts = null;
$wp_styles = null;
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media_Video::get_instance_schema()
*/
function test_get_instance_schema() {
$widget = new WP_Widget_Media_Video();
$schema = $widget->get_instance_schema();
$this->assertEqualSets(
array_merge(
array(
'attachment_id',
'preload',
'loop',
'title',
'url',
'content',
),
wp_get_video_extensions()
),
array_keys( $schema )
);
}
/**
* Test constructor.
*
* @covers WP_Widget_Media_Video::__construct()
*/
function test_constructor() {
$widget = new WP_Widget_Media_Video();
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
$this->assertArrayHasKey( 'description', $widget->widget_options );
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'video', $widget->widget_options['mime_type'] );
$this->assertEqualSets( array(
'add_to_widget',
'replace_media',
'unsupported_file_type',
'edit_media',
'media_library_state_multi',
'media_library_state_single',
'missing_attachment',
'no_media_selected',
'add_media',
), array_keys( $widget->l10n ) );
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media_Video::update()
*/
function test_update() {
$widget = new WP_Widget_Media_Video();
$instance = array();
// Should return valid attachment ID.
$expected = array(
'attachment_id' => 1,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment ID.
$result = $widget->update( array(
'attachment_id' => 'media',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid attachment url.
$expected = array(
'url' => 'https://chickenandribs.org',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment url.
$result = $widget->update( array(
'url' => 'not_a_url',
), $instance );
$this->assertNotSame( $result, $instance );
$this->assertStringStartsWith( 'http://', $result['url'] );
// Should return loop setting.
$expected = array(
'loop' => true,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid loop setting.
$result = $widget->update( array(
'loop' => 'not-boolean',
), $instance );
$this->assertSame( $result, $instance );
// Should return valid attachment title.
$expected = array(
'title' => 'A video of goats',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment title.
$result = $widget->update( array(
'title' => '<h1>Cute Baby Goats</h1>',
), $instance );
$this->assertNotSame( $result, $instance );
// Should return valid preload setting.
$expected = array(
'preload' => 'none',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid preload setting.
$result = $widget->update( array(
'preload' => 'nope',
), $instance );
$this->assertSame( $result, $instance );
// Should filter invalid key.
$result = $widget->update( array(
'h4x' => 'value',
), $instance );
$this->assertSame( $result, $instance );
}
/**
* Test render_media method.
*
* @covers WP_Widget_Media_Video::render_media()
* @covers WP_Widget_Media_Video::inject_video_max_width_style()
*/
function test_render_media() {
$test_movie_file = __FILE__ . '../../data/uploads/small-video.m4v';
$widget = new WP_Widget_Media_Video();
$attachment_id = self::factory()->attachment->create_object( array(
'file' => $test_movie_file,
'post_parent' => 0,
'post_mime_type' => 'video/mp4',
'post_title' => 'Test Video',
) );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $test_movie_file ) );
// Should be empty when there is no attachment_id.
ob_start();
$widget->render_media( array() );
$output = ob_get_clean();
$this->assertEmpty( $output );
// Should be empty when there is an invalid attachment_id.
ob_start();
$widget->render_media( array(
'attachment_id' => 777,
) );
$output = ob_get_clean();
$this->assertEmpty( $output );
// Tests with video from library.
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
) );
$output = ob_get_clean();
// Check default outputs.
$this->assertContains( 'preload="metadata"', $output );
$this->assertContains( 'class="wp-video"', $output );
$this->assertContains( 'width:100%', $output );
$this->assertNotContains( 'height=', $output );
$this->assertNotContains( 'width="', $output );
$this->assertContains( 'small-video.m4v', $output );// Auto parses dimensions.
ob_start();
$widget->render_media( array(
'attachment_id' => $attachment_id,
'title' => 'Open Source Cartoon',
'preload' => 'metadata',
'loop' => true,
) );
$output = ob_get_clean();
// Custom attributes.
$this->assertContains( 'preload="metadata"', $output );
$this->assertContains( 'loop="1"', $output );
// Externally hosted video.
ob_start();
$content = '<track srclang="en" label="English" kind="subtitles" src="http://example.com/wp-content/uploads/2017/04/subtitles-en.vtt">';
$widget->render_media( array(
'attachment_id' => null,
'loop' => false,
'url' => 'https://www.youtube.com/watch?v=OQSNhk5ICTI',
'content' => $content,
) );
$output = ob_get_clean();
// Custom attributes.
$this->assertContains( 'preload="metadata"', $output );
$this->assertContains( 'src="https://www.youtube.com/watch?v=OQSNhk5ICTI', $output );
$this->assertContains( $content, $output );
}
/**
* Test enqueue_preview_scripts method.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
* @covers WP_Widget_Media_Video::enqueue_preview_scripts()
*/
function test_enqueue_preview_scripts() {
global $wp_scripts, $wp_styles;
$widget = new WP_Widget_Media_Video();
$wp_scripts = null;
$wp_styles = null;
$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' ) );
}
/**
* Test enqueue_admin_scripts method.
*
* @covers WP_Widget_Media_Video::enqueue_admin_scripts()
*/
function test_enqueue_admin_scripts() {
set_current_screen( 'widgets.php' );
$widget = new WP_Widget_Media_Video();
$widget->enqueue_admin_scripts();
$this->assertTrue( wp_script_is( 'media-video-widget' ) );
}
/**
* Test render_control_template_scripts method.
*
* @covers WP_Widget_Media_Video::render_control_template_scripts()
*/
function test_render_control_template_scripts() {
$widget = new WP_Widget_Media_Video();
ob_start();
$widget->render_control_template_scripts();
$output = ob_get_clean();
$this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-video-preview">', $output );
}
}

View File

@ -0,0 +1,467 @@
<?php
/**
* Unit tests covering WP_Widget_Media functionality.
*
* @package WordPress
* @subpackage widgets
*/
/**
* Class Test_WP_Widget_Media
*
* @group widgets
*/
class Test_WP_Widget_Media extends WP_UnitTestCase {
/**
* Clean up global scope.
*
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
*/
function clean_up_global_scope() {
global $wp_scripts, $wp_styles;
parent::clean_up_global_scope();
$wp_scripts = null;
$wp_styles = null;
}
/**
* Get instance for mocked media widget class.
*
* @param string $id_base Base ID for the widget, lowercase and unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options.
* @param array $control_options Optional. Widget control options.
* @return PHPUnit_Framework_MockObject_MockObject|WP_Widget_Media Mocked instance.
*/
function get_mocked_class_instance( $id_base = 'mocked', $name = 'Mocked', $widget_options = array(), $control_options = array() ) {
$original_class_name = 'WP_Widget_Media';
$arguments = array( $id_base, $name, $widget_options, $control_options );
$mock_class_name = '';
$call_original_constructor = true;
$call_original_clone = true;
$call_autoload = true;
$mocked_methods = array( 'render_media' );
return $this->getMockForAbstractClass( $original_class_name, $arguments, $mock_class_name, $call_original_constructor, $call_original_clone, $call_autoload, $mocked_methods );
}
/**
* Test constructor.
*
* @covers WP_Widget_Media::__construct()
* @covers WP_Widget_Media::_register()
*/
function test_constructor() {
$widget = $this->get_mocked_class_instance();
$widget->_register();
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
$this->assertArrayHasKey( 'description', $widget->widget_options );
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
$this->assertEmpty( $widget->widget_options['mime_type'] );
$this->assertEqualSets( array(
'add_to_widget',
'replace_media',
'edit_media',
'media_library_state_multi',
'media_library_state_single',
'missing_attachment',
'no_media_selected',
'add_media',
'unsupported_file_type',
), array_keys( $widget->l10n ) );
$this->assertEquals( count( $widget->l10n ), count( array_filter( $widget->l10n ) ), 'Expected all translation strings to be defined.' );
$this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
$this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ), 'Did not expect preview scripts to be enqueued when not in customize preview context.' );
$this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( $widget, 'render_control_template_scripts' ) ) );
// With non-default args.
$id_base = 'media_pdf';
$name = 'PDF';
$widget_options = array(
'mime_type' => 'application/pdf',
);
$control_options = array(
'width' => 850,
'height' => 1100,
);
$widget = $this->get_mocked_class_instance( $id_base, $name, $widget_options, $control_options );
$this->assertEquals( $id_base, $widget->id_base );
$this->assertEquals( $name, $widget->name );
// Method assertArraySubset doesn't exist in phpunit versions compatible with PHP 5.2.
if ( method_exists( $this, 'assertArraySubset' ) ) {
$this->assertArraySubset( $widget_options, $widget->widget_options );
$this->assertArraySubset( $control_options, $widget->control_options );
}
}
/**
* Test constructor in customize preview.
*
* @global WP_Customize_Manager $wp_customize
* @covers WP_Widget_Media::__construct()
* @covers WP_Widget_Media::_register()
*/
function test_constructor_in_customize_preview() {
global $wp_customize;
wp_set_current_user( $this->factory()->user->create( array(
'role' => 'administrator',
) ) );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager( array(
'changeset_uuid' => wp_generate_uuid4(),
) );
$wp_customize->start_previewing_theme();
$widget = $this->get_mocked_class_instance();
$widget->_register();
$this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
}
/**
* Test is_attachment_with_mime_type method.
*
* @covers WP_Widget_Media::is_attachment_with_mime_type
*/
function test_is_attachment_with_mime_type() {
$attachment_id = self::factory()->attachment->create_object( array(
'file' => DIR_TESTDATA . '/images/canola.jpg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_title' => 'Canola',
) );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, DIR_TESTDATA . '/images/canola.jpg' ) );
$widget = $this->get_mocked_class_instance();
$this->assertFalse( $widget->is_attachment_with_mime_type( 0, 'image' ) );
$this->assertFalse( $widget->is_attachment_with_mime_type( -123, 'image' ) );
$post_id = $this->factory()->post->create();
$this->assertFalse( $widget->is_attachment_with_mime_type( $post_id, 'image' ) );
$this->assertFalse( $widget->is_attachment_with_mime_type( $attachment_id, 'video' ) );
$this->assertTrue( $widget->is_attachment_with_mime_type( $attachment_id, 'image' ) );
}
/**
* Test sanitize_token_list method.
*
* @covers WP_Widget_Media::sanitize_token_list
*/
function test_sanitize_token_list_string() {
$widget = $this->get_mocked_class_instance();
$result = $widget->sanitize_token_list( 'What A false class with-token <a href="#">and link</a>' );
$this->assertEquals( 'What A false class with-token a hrefand linka', $result );
$result = $widget->sanitize_token_list( array( 'foo', '<i>bar', '">NO' ) );
$this->assertEquals( $result, 'foo ibar NO' );
}
/**
* Test get_instance_schema method.
*
* @covers WP_Widget_Media::get_instance_schema
*/
function test_get_instance_schema() {
$widget = $this->get_mocked_class_instance();
$schema = $widget->get_instance_schema();
$this->assertEqualSets( array(
'attachment_id',
'title',
'url',
), array_keys( $schema ) );
}
/**
* Test update method.
*
* @covers WP_Widget_Media::update()
*/
function test_update() {
$widget = $this->get_mocked_class_instance();
$instance = array();
// Should return valid attachment ID.
$expected = array(
'attachment_id' => 1,
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment ID.
$result = $widget->update(
array(
'attachment_id' => 'media',
),
$instance
);
$this->assertSame( $result, $instance );
// Should return valid attachment url.
$expected = array(
'url' => 'https://example.org',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment url.
$result = $widget->update(
array(
'url' => 'not_a_url',
),
$instance
);
$this->assertNotSame( $result, $instance );
// Should return valid attachment title.
$expected = array(
'title' => 'What a title',
);
$result = $widget->update( $expected, $instance );
$this->assertSame( $result, $expected );
// Should filter invalid attachment title.
$result = $widget->update(
array(
'title' => '<h1>W00t!</h1>',
),
$instance
);
$this->assertNotSame( $result, $instance );
// Should filter invalid key.
$result = $widget->update(
array(
'imaginary_key' => 'value',
),
$instance
);
$this->assertSame( $result, $instance );
add_filter( 'sanitize_text_field', array( $this, '_return_wp_error' ) );
$result = $widget->update(
array(
'title' => 'Title',
),
$instance
);
remove_filter( 'sanitize_text_field', array( $this, '_return_wp_error' ) );
$this->assertSame( $result, $instance );
}
/**
* Helper function for Test_WP_Widget_Media::test_update().
*
* @return \WP_Error
*/
function _return_wp_error() {
return new WP_Error( 'some-error', 'This is not valid!' );
}
/**
* Test widget method.
*
* @covers WP_Widget_Media::widget()
* @covers WP_Widget_Media::render_media()
*/
function test_widget() {
$args = array(
'before_title' => '<h2>',
'after_title' => "</h2>\n",
'before_widget' => '<section>',
'after_widget' => "</section>\n",
);
$instance = array(
'title' => 'Foo',
'url' => 'http://example.com/image.jpg',
'attachment_id' => 0,
);
add_filter( 'widget_mocked_instance', array( $this, 'filter_widget_mocked_instance' ), 10, 3 );
ob_start();
$widget = $this->get_mocked_class_instance();
$widget->expects( $this->atLeastOnce() )->method( 'render_media' )->with( $instance );
$this->widget_instance_filter_args = array();
$widget->widget( $args, $instance );
$this->assertCount( 3, $this->widget_instance_filter_args );
$this->assertEquals( $instance, $this->widget_instance_filter_args[0] );
$this->assertEquals( $args, $this->widget_instance_filter_args[1] );
$this->assertEquals( $widget, $this->widget_instance_filter_args[2] );
$output = ob_get_clean();
$this->assertContains( '<h2>Foo</h2>', $output );
$this->assertContains( '<section>', $output );
$this->assertContains( '</section>', $output );
// No title.
ob_start();
$widget = $this->get_mocked_class_instance();
$instance['title'] = '';
$widget->expects( $this->atLeastOnce() )->method( 'render_media' )->with( $instance );
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertNotContains( '<h2>Foo</h2>', $output );
// No attachment_id nor url.
$instance['url'] = '';
$instance['attachment_id'] = 0;
ob_start();
$widget = $this->get_mocked_class_instance();
$widget->widget( $args, $instance );
$output = ob_get_clean();
$this->assertEmpty( $output );
}
/**
* Args passed to the widget_{$id_base}_instance filter.
*
* @var array
*/
protected $widget_instance_filter_args = array();
/**
* Filters the media widget instance prior to rendering the media.
*
* @param array $instance Instance data.
* @param array $args Widget args.
* @param WP_Widget_Media $object Widget object.
* @return array Instance.
*/
function filter_widget_mocked_instance( $instance, $args, $object ) {
$this->widget_instance_filter_args = func_get_args();
return $instance;
}
/**
* Test form method.
*
* @covers WP_Widget_Media::form()
*/
function test_form() {
$widget = $this->get_mocked_class_instance();
ob_start();
$widget->form( array() );
$output = ob_get_clean();
$this->assertContains( 'name="widget-mocked[][attachment_id]"', $output );
$this->assertContains( 'name="widget-mocked[][title]"', $output );
$this->assertContains( 'name="widget-mocked[][url]"', $output );
}
/**
* Test display_media_state method.
*
* @covers WP_Widget_Media::display_media_state()
*/
function test_display_media_state() {
$widget = $this->get_mocked_class_instance();
$attachment_id = self::factory()->attachment->create_object( array(
'file' => DIR_TESTDATA . '/images/canola.jpg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
) );
$result = $widget->display_media_state( array(), get_post( $attachment_id ) );
$this->assertEqualSets( array(), $result );
$widget->save_settings( array(
array(
'attachment_id' => $attachment_id,
),
) );
$result = $widget->display_media_state( array(), get_post( $attachment_id ) );
$this->assertEqualSets( array( $widget->l10n['media_library_state_single'] ), $result );
$widget->save_settings( array(
array(
'attachment_id' => $attachment_id,
),
array(
'attachment_id' => $attachment_id,
),
) );
$result = $widget->display_media_state( array(), get_post( $attachment_id ) );
$this->assertEqualSets( array( sprintf( $widget->l10n['media_library_state_multi']['singular'], 2 ) ), $result );
}
/**
* Test enqueue_admin_scripts method.
*
* @covers WP_Widget_Media::enqueue_admin_scripts()
*/
function test_enqueue_admin_scripts() {
set_current_screen( 'widgets.php' );
$widget = $this->get_mocked_class_instance();
$widget->enqueue_admin_scripts();
$this->assertTrue( wp_script_is( 'media-widgets' ) );
}
/**
* Test render_control_template_scripts method.
*
* @covers WP_Widget_Media::render_control_template_scripts
*/
function test_render_control_template_scripts() {
$widget = $this->get_mocked_class_instance();
ob_start();
$widget->render_control_template_scripts();
$output = ob_get_clean();
$this->assertContains( '<script type="text/html" id="tmpl-widget-media-mocked-control">', $output );
}
/**
* Test has_content method.
*
* @covers WP_Widget_Media::has_content()
*/
function test_has_content() {
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
$this->markTestSkipped( 'ReflectionMethod::setAccessible is only available for PHP 5.3+' );
return;
}
$attachment_id = self::factory()->attachment->create_object( array(
'file' => DIR_TESTDATA . '/images/canola.jpg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
) );
$wp_widget_media = new ReflectionClass( 'WP_Widget_Media' );
$has_content = $wp_widget_media->getMethod( 'has_content' );
$has_content->setAccessible( true );
$result = $has_content->invokeArgs( $this->get_mocked_class_instance(), array(
array(
'attachment_id' => 0,
'url' => '',
),
) );
$this->assertFalse( $result );
$result = $has_content->invokeArgs( $this->get_mocked_class_instance(), array(
array(
'attachment_id' => $attachment_id,
'url' => '',
),
) );
$this->assertTrue( $result );
$result = $has_content->invokeArgs( $this->get_mocked_class_instance(), array(
array(
'attachment_id' => 0,
'url' => 'http://example.com/image.jpg',
),
) );
$this->assertTrue( $result );
}
}

View File

@ -178,15 +178,15 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
$expected['text'] = $instance['text'];
$result = $widget->update( $instance, array() );
$this->assertEquals( $result, $expected );
remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) );
add_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 );
$this->assertFalse( current_user_can( 'unfiltered_html' ) );
$instance['text'] = '<script>alert( "Howdy!" );</script>';
$expected['text'] = wp_kses_post( $instance['text'] );
$result = $widget->update( $instance, array() );
$this->assertEquals( $result, $expected );
remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 );
remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 );
}
/**

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,113 @@
/* globals wp */
/* jshint qunit: true */
/* eslint-env qunit */
/* eslint-disable no-magic-numbers */
( function() {
'use strict';
module( 'Image Media Widget' );
test( 'image widget control', function() {
var ImageWidgetControl, imageWidgetControlInstance, imageWidgetModelInstance, mappedProps, testImageUrl, templateProps;
testImageUrl = 'http://s.w.org/style/images/wp-header-logo.png';
equal( typeof wp.mediaWidgets.controlConstructors.media_image, 'function', 'wp.mediaWidgets.controlConstructors.media_image is a function' );
ImageWidgetControl = wp.mediaWidgets.controlConstructors.media_image;
ok( ImageWidgetControl.prototype instanceof wp.mediaWidgets.MediaWidgetControl, 'wp.mediaWidgets.controlConstructors.media_image subclasses wp.mediaWidgets.MediaWidgetControl' );
imageWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_image();
imageWidgetControlInstance = new ImageWidgetControl({
model: imageWidgetModelInstance
});
// Test mapModelToPreviewTemplateProps() when no data is set.
templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
equal( templateProps.caption, undefined, 'mapModelToPreviewTemplateProps should not return attributes that are should_preview_update false' );
equal( templateProps.attachment_id, 0, 'mapModelToPreviewTemplateProps should return default values' );
equal( templateProps.currentFilename, '', 'mapModelToPreviewTemplateProps should return a currentFilename' );
// Test mapModelToPreviewTemplateProps() when data is set on model.
imageWidgetControlInstance.model.set( { url: testImageUrl, alt: 'some alt text', link_type: 'none' } );
templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
equal( templateProps.currentFilename, 'wp-header-logo.png', 'mapModelToPreviewTemplateProps should set currentFilename based off of url' );
equal( templateProps.url, testImageUrl, 'mapModelToPreviewTemplateProps should return the proper url' );
equal( templateProps.alt, 'some alt text', 'mapModelToPreviewTemplateProps should return the proper alt text' );
equal( templateProps.link_type, undefined, 'mapModelToPreviewTemplateProps should ignore attributes that are not needed in the preview' );
equal( templateProps.error, false, 'mapModelToPreviewTemplateProps should return error state' );
// Test mapModelToPreviewTemplateProps() when error is set on model.
imageWidgetControlInstance.model.set( 'error', 'missing_attachment' );
templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
equal( templateProps.error, 'missing_attachment', 'mapModelToPreviewTemplateProps should return error string' );
// Reset model.
imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: null });
// Test isSelected().
equal( imageWidgetControlInstance.isSelected(), false, 'media_image.isSelected() should return false when no media is selected' );
imageWidgetControlInstance.model.set({ error: 'missing_attachment', attachment_id: 777 });
equal( imageWidgetControlInstance.isSelected(), false, 'media_image.isSelected() should return false when media is selected and error is set' );
imageWidgetControlInstance.model.set({ error: false, attachment_id: 777 });
equal( imageWidgetControlInstance.isSelected(), true, 'media_image.isSelected() should return true when media is selected and no error exists' );
imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: testImageUrl });
equal( imageWidgetControlInstance.isSelected(), true, 'media_image.isSelected() should return true when url is set and no error exists' );
// Reset model.
imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: null });
// Test editing of widget title.
imageWidgetControlInstance.render();
imageWidgetControlInstance.$el.find( '.title' ).val( 'Chicken and Ribs' ).trigger( 'input' );
equal( imageWidgetModelInstance.get( 'title' ), 'Chicken and Ribs', 'Changing title should update model title attribute' );
// Test mapMediaToModelProps
mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'file', url: testImageUrl } );
equal( mappedProps.link_url, testImageUrl, 'mapMediaToModelProps should set file link_url according to mediaFrameProps.link' );
mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'post', postUrl: 'https://wordpress.org/image-2/' } );
equal( mappedProps.link_url, 'https://wordpress.org/image-2/', 'mapMediaToModelProps should set file link_url according to mediaFrameProps.link' );
mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'custom', linkUrl: 'https://wordpress.org' } );
equal( mappedProps.link_url, 'https://wordpress.org', 'mapMediaToModelProps should set custom link_url according to mediaFrameProps.linkUrl' );
// Test mapModelToMediaFrameProps().
imageWidgetControlInstance.model.set({ error: false, url: testImageUrl, 'link_type': 'custom', 'link_url': 'https://wordpress.org', 'size': 'custom', 'width': 100, 'height': 150, 'title': 'widget title', 'image_title': 'title of image' });
mappedProps = imageWidgetControlInstance.mapModelToMediaFrameProps( imageWidgetControlInstance.model.toJSON() );
equal( mappedProps.linkUrl, 'https://wordpress.org', 'mapModelToMediaFrameProps should set linkUrl from model.link_url' );
equal( mappedProps.link, 'custom', 'mapModelToMediaFrameProps should set link from model.link_type' );
equal( mappedProps.width, 100, 'mapModelToMediaFrameProps should set width when model.size is custom' );
equal( mappedProps.height, 150, 'mapModelToMediaFrameProps should set height when model.size is custom' );
equal( mappedProps.title, 'title of image', 'mapModelToMediaFrameProps should set title from model.image_title' );
});
test( 'image widget control renderPreview', function( assert ) {
var imageWidgetControlInstance, imageWidgetModelInstance, done;
done = assert.async();
imageWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_image();
imageWidgetControlInstance = new wp.mediaWidgets.controlConstructors.media_image({
model: imageWidgetModelInstance
});
equal( imageWidgetControlInstance.$el.find( 'img' ).length, 0, 'No images should be rendered' );
imageWidgetControlInstance.model.set({ error: false, url: 'http://s.w.org/style/images/wp-header-logo.png' });
// Due to renderPreview being deferred.
setTimeout( function() {
equal( imageWidgetControlInstance.$el.find( 'img[src="http://s.w.org/style/images/wp-header-logo.png"]' ).length, 1, 'One image should be rendered' );
done();
}, 50 );
start();
});
test( 'image media model', function() {
var ImageWidgetModel, imageWidgetModelInstance;
equal( typeof wp.mediaWidgets.modelConstructors.media_image, 'function', 'wp.mediaWidgets.modelConstructors.media_image is a function' );
ImageWidgetModel = wp.mediaWidgets.modelConstructors.media_image;
ok( ImageWidgetModel.prototype instanceof wp.mediaWidgets.MediaWidgetModel, 'wp.mediaWidgets.modelConstructors.media_image subclasses wp.mediaWidgets.MediaWidgetModel' );
imageWidgetModelInstance = new ImageWidgetModel();
_.each( imageWidgetModelInstance.attributes, function( value, key ) {
equal( value, ImageWidgetModel.prototype.schema[ key ][ 'default' ], 'Should properly set default for ' + key );
});
});
})();

View File

@ -0,0 +1,68 @@
/* globals wp */
/* jshint qunit: true */
/* eslint-env qunit */
/* eslint-disable no-magic-numbers */
( function() {
'use strict';
module( 'Video Media Widget' );
test( 'video widget control', function() {
var VideoWidgetControl, videoWidgetControlInstance, videoWidgetModelInstance, mappedProps, testVideoUrl;
testVideoUrl = 'https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4';
equal( typeof wp.mediaWidgets.controlConstructors.media_video, 'function', 'wp.mediaWidgets.controlConstructors.media_video is a function' );
VideoWidgetControl = wp.mediaWidgets.controlConstructors.media_video;
ok( VideoWidgetControl.prototype instanceof wp.mediaWidgets.MediaWidgetControl, 'wp.mediaWidgets.controlConstructors.media_video subclasses wp.mediaWidgets.MediaWidgetControl' );
videoWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_video();
videoWidgetControlInstance = new VideoWidgetControl({
model: videoWidgetModelInstance
});
// Test mapModelToMediaFrameProps().
videoWidgetControlInstance.model.set({ error: false, url: testVideoUrl, loop: false, preload: 'meta' });
mappedProps = videoWidgetControlInstance.mapModelToMediaFrameProps( videoWidgetControlInstance.model.toJSON() );
equal( mappedProps.url, testVideoUrl, 'mapModelToMediaFrameProps should set url' );
equal( mappedProps.loop, false, 'mapModelToMediaFrameProps should set loop' );
equal( mappedProps.preload, 'meta', 'mapModelToMediaFrameProps should set preload' );
// Test mapMediaToModelProps().
mappedProps = videoWidgetControlInstance.mapMediaToModelProps( { loop: false, preload: 'meta', url: testVideoUrl, title: 'random movie file title' } );
equal( mappedProps.title, undefined, 'mapMediaToModelProps should ignore title inputs' );
equal( mappedProps.loop, false, 'mapMediaToModelProps should set loop' );
equal( mappedProps.preload, 'meta', 'mapMediaToModelProps should set preload' );
});
test( 'video widget control renderPreview', function( assert ) {
var videoWidgetControlInstance, videoWidgetModelInstance, done;
done = assert.async();
videoWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_video();
videoWidgetControlInstance = new wp.mediaWidgets.controlConstructors.media_video({
model: videoWidgetModelInstance
});
equal( videoWidgetControlInstance.$el.find( 'a' ).length, 0, 'No video links should be rendered' );
videoWidgetControlInstance.model.set({ error: false, url: 'https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4' });
// Due to renderPreview being deferred.
setTimeout( function() {
equal( videoWidgetControlInstance.$el.find( 'a[href="https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4"]' ).length, 1, 'One video link should be rendered' );
done();
}, 50 );
start();
});
test( 'video media model', function() {
var VideoWidgetModel, videoWidgetModelInstance;
equal( typeof wp.mediaWidgets.modelConstructors.media_video, 'function', 'wp.mediaWidgets.modelConstructors.media_video is a function' );
VideoWidgetModel = wp.mediaWidgets.modelConstructors.media_video;
ok( VideoWidgetModel.prototype instanceof wp.mediaWidgets.MediaWidgetModel, 'wp.mediaWidgets.modelConstructors.media_video subclasses wp.mediaWidgets.MediaWidgetModel' );
videoWidgetModelInstance = new VideoWidgetModel();
_.each( videoWidgetModelInstance.attributes, function( value, key ) {
equal( value, VideoWidgetModel.prototype.schema[ key ][ 'default' ], 'Should properly set default for ' + key );
});
});
})();

View File

@ -0,0 +1,45 @@
/* globals wp, Backbone */
/* jshint qunit: true */
/* eslint-env qunit */
(function() {
'use strict';
module( 'Media Widgets' );
test( 'namespace', function() {
equal( typeof wp.mediaWidgets, 'object', 'wp.mediaWidgets is an object' );
equal( typeof wp.mediaWidgets.controlConstructors, 'object', 'wp.mediaWidgets.controlConstructors is an object' );
equal( typeof wp.mediaWidgets.modelConstructors, 'object', 'wp.mediaWidgets.modelConstructors is an object' );
equal( typeof wp.mediaWidgets.widgetControls, 'object', 'wp.mediaWidgets.widgetControls is an object' );
equal( typeof wp.mediaWidgets.handleWidgetAdded, 'function', 'wp.mediaWidgets.handleWidgetAdded is an function' );
equal( typeof wp.mediaWidgets.handleWidgetUpdated, 'function', 'wp.mediaWidgets.handleWidgetUpdated is an function' );
equal( typeof wp.mediaWidgets.init, 'function', 'wp.mediaWidgets.init is an function' );
});
test( 'media widget control', function() {
equal( typeof wp.mediaWidgets.MediaWidgetControl, 'function', 'wp.mediaWidgets.MediaWidgetControl' );
ok( wp.mediaWidgets.MediaWidgetControl.prototype instanceof Backbone.View, 'wp.mediaWidgets.MediaWidgetControl subclasses Backbone.View' );
});
test( 'media widget model', function() {
var widgetModelInstance;
equal( typeof wp.mediaWidgets.MediaWidgetModel, 'function', 'wp.mediaWidgets.MediaWidgetModel is a function' );
ok( wp.mediaWidgets.MediaWidgetModel.prototype instanceof Backbone.Model, 'wp.mediaWidgets.MediaWidgetModel subclasses Backbone.Model' );
widgetModelInstance = new wp.mediaWidgets.MediaWidgetModel();
equal( widgetModelInstance.get( 'title' ), '', 'wp.mediaWidgets.MediaWidgetModel defaults title to empty string' );
equal( widgetModelInstance.get( 'attachment_id' ), 0, 'wp.mediaWidgets.MediaWidgetModel defaults attachment_id to 0' );
equal( widgetModelInstance.get( 'url' ), 0, 'wp.mediaWidgets.MediaWidgetModel defaults url to empty string' );
widgetModelInstance.set({
title: 'chicken and ribs',
attachment_id: '1',
url: 'https://wordpress.org'
});
equal( widgetModelInstance.get( 'title' ), 'chicken and ribs', 'wp.mediaWidgets.MediaWidgetModel properly sets the title attribute' );
equal( widgetModelInstance.get( 'url' ), 'https://wordpress.org', 'wp.mediaWidgets.MediaWidgetModel properly sets the url attribute' );
equal( widgetModelInstance.get( 'attachment_id' ), 1, 'wp.mediaWidgets.MediaWidgetModel properly sets and casts the attachment_id attribute' );
});
})();