Widgets: Introduce isHostedVideo method on VideoWidgetControl to allow plugins to extend for recognizing services beyond YouTube and Vimeo.

Also update jshint configuration in Gruntfile to include the widget scripts among the JS files linted.

Props timmydcrawford.
See #39994.
Fixes #40808.


git-svn-id: https://develop.svn.wordpress.org/trunk@40810 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-05-20 04:17:33 +00:00
parent 2a5f7a8901
commit 016dc58620
3 changed files with 20 additions and 4 deletions

View File

@ -329,7 +329,7 @@ module.exports = function(grunt) {
expand: true,
cwd: SOURCE_DIR,
src: [
'wp-admin/js/*.js',
'wp-admin/js/**/*.js',
'wp-includes/js/*.js',
// Built scripts.
'!wp-includes/js/media-*',

View File

@ -134,6 +134,18 @@
});
},
/**
* Whether a url is a supported external host.
*
* @param {String} url - Video url.
* @returns {boolean} Whether url is a supported video host.
*/
isHostedVideo: function isHostedVideo( url ) {
var parsedUrl = document.createElement( 'a' );
parsedUrl.href = url;
return /vimeo|youtu\.?be/.test( parsedUrl.host );
},
/**
* Render preview.
*
@ -150,9 +162,7 @@
}
if ( ! attachmentId && attachmentUrl ) {
parsedUrl = document.createElement( 'a' );
parsedUrl.href = attachmentUrl;
isHostedEmbed = /vimeo|youtu\.?be/.test( parsedUrl.host );
isHostedEmbed = control.isHostedVideo( attachmentUrl );
}
if ( isHostedEmbed ) {

View File

@ -32,6 +32,12 @@
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 isHostedVideo().
equal( videoWidgetControlInstance.isHostedVideo( 'https://www.youtube.com/watch?v=OQSNhk5ICTI' ), true, 'isHostedVideo should return true for full YouTube url.' );
equal( videoWidgetControlInstance.isHostedVideo( 'https://youtu.be/OQSNhk5ICTI' ), true, 'isHostedVideo should return true for shortened youtube url' );
equal( videoWidgetControlInstance.isHostedVideo( 'https://vimeo.com/190372437' ), true, 'isHostedVideo should return true for vimeo url.' );
equal( videoWidgetControlInstance.isHostedVideo( 'https://wordpress.org/' ), false, 'isHostedVideo should return false for non-supported video url.' );
});
test( 'video widget control renderPreview', function( assert ) {