From 43fc92e4a9077d1c15740d2a143d92e3c1ac0151 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 28 Jul 2017 02:35:56 +0000 Subject: [PATCH] Media: Improve acceptance of YouTube /embed/ URLs when inserting in media modal. Props timmydcrawford. Fixes #41201. git-svn-id: https://develop.svn.wordpress.org/trunk@41179 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/widgets/media-widgets.js | 14 ++++++++++++-- src/wp-includes/js/media-views.js | 12 ++++++++++-- src/wp-includes/js/media/views/embed/link.js | 12 ++++++++++-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/wp-admin/js/widgets/media-widgets.js b/src/wp-admin/js/widgets/media-widgets.js index 598d94d8b1..e922670a2c 100644 --- a/src/wp-admin/js/widgets/media-widgets.js +++ b/src/wp-admin/js/widgets/media-widgets.js @@ -145,7 +145,7 @@ wp.mediaWidgets = ( function( $ ) { * @returns {void} */ fetch: function() { - var embedLinkView = this, fetchSuccess, matches, fileExt, urlParser; // eslint-disable-line consistent-this + var embedLinkView = this, fetchSuccess, matches, fileExt, urlParser, url, re, youTubeEmbedMatch; // eslint-disable-line consistent-this if ( embedLinkView.dfd && 'pending' === embedLinkView.dfd.state() ) { embedLinkView.dfd.abort(); @@ -190,10 +190,20 @@ wp.mediaWidgets = ( function( $ ) { return; } + // Support YouTube embed links. + url = embedLinkView.model.get( 'url' ); + re = /https?:\/\/www\.youtube\.com\/embed\/([^/]+)/; + youTubeEmbedMatch = re.exec( url ); + if ( youTubeEmbedMatch ) { + url = 'https://www.youtube.com/watch?v=' + youTubeEmbedMatch[ 1 ]; + // silently change url to proper oembed-able version. + embedLinkView.model.attributes.url = url; + } + embedLinkView.dfd = $.ajax({ url: wp.media.view.settings.oEmbedProxyUrl, data: { - url: embedLinkView.model.get( 'url' ), + url: url, maxwidth: embedLinkView.model.get( 'width' ), maxheight: embedLinkView.model.get( 'height' ), _wpnonce: wp.media.view.settings.nonce.wpRestApi, diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 29b56c66a8..991b3d09c8 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -4624,9 +4624,10 @@ EmbedLink = wp.media.view.Settings.extend({ }, wp.media.controller.Embed.sensitivity ), fetch: function() { + var url = this.model.get( 'url' ), re, youTubeEmbedMatch; // check if they haven't typed in 500 ms - if ( $('#embed-url-field').val() !== this.model.get('url') ) { + if ( $('#embed-url-field').val() !== url ) { return; } @@ -4634,10 +4635,17 @@ EmbedLink = wp.media.view.Settings.extend({ this.dfd.abort(); } + // Support YouTube embed urls, since they work once in the editor. + re = /https?:\/\/www\.youtube\.com\/embed\/([^/]+)/; + youTubeEmbedMatch = re.exec( url ); + if ( youTubeEmbedMatch ) { + url = 'https://www.youtube.com/watch?v=' + youTubeEmbedMatch[ 1 ]; + } + this.dfd = $.ajax({ url: wp.media.view.settings.oEmbedProxyUrl, data: { - url: this.model.get( 'url' ), + url: url, maxwidth: this.model.get( 'width' ), maxheight: this.model.get( 'height' ), _wpnonce: wp.media.view.settings.nonce.wpRestApi diff --git a/src/wp-includes/js/media/views/embed/link.js b/src/wp-includes/js/media/views/embed/link.js index 1af96cfa5d..36925f4974 100644 --- a/src/wp-includes/js/media/views/embed/link.js +++ b/src/wp-includes/js/media/views/embed/link.js @@ -35,9 +35,10 @@ EmbedLink = wp.media.view.Settings.extend({ }, wp.media.controller.Embed.sensitivity ), fetch: function() { + var url = this.model.get( 'url' ), re, youTubeEmbedMatch; // check if they haven't typed in 500 ms - if ( $('#embed-url-field').val() !== this.model.get('url') ) { + if ( $('#embed-url-field').val() !== url ) { return; } @@ -45,10 +46,17 @@ EmbedLink = wp.media.view.Settings.extend({ this.dfd.abort(); } + // Support YouTube embed urls, since they work once in the editor. + re = /https?:\/\/www\.youtube\.com\/embed\/([^/]+)/; + youTubeEmbedMatch = re.exec( url ); + if ( youTubeEmbedMatch ) { + url = 'https://www.youtube.com/watch?v=' + youTubeEmbedMatch[ 1 ]; + } + this.dfd = $.ajax({ url: wp.media.view.settings.oEmbedProxyUrl, data: { - url: this.model.get( 'url' ), + url: url, maxwidth: this.model.get( 'width' ), maxheight: this.model.get( 'height' ), _wpnonce: wp.media.view.settings.nonce.wpRestApi