From 016dc5862048aecfeb98dc24b0717f695f2687fd Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 20 May 2017 04:17:33 +0000 Subject: [PATCH] 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 --- Gruntfile.js | 2 +- src/wp-admin/js/widgets/media-video-widget.js | 16 +++++++++++++--- .../js/widgets/test-media-video-widget.js | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 37848c1449..c7e7f73b2b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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-*', diff --git a/src/wp-admin/js/widgets/media-video-widget.js b/src/wp-admin/js/widgets/media-video-widget.js index 547c4efc48..79ff84d86b 100644 --- a/src/wp-admin/js/widgets/media-video-widget.js +++ b/src/wp-admin/js/widgets/media-video-widget.js @@ -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 ) { diff --git a/tests/qunit/wp-admin/js/widgets/test-media-video-widget.js b/tests/qunit/wp-admin/js/widgets/test-media-video-widget.js index f3b111ecc5..bef61c5317 100644 --- a/tests/qunit/wp-admin/js/widgets/test-media-video-widget.js +++ b/tests/qunit/wp-admin/js/widgets/test-media-video-widget.js @@ -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 ) {