From 684091ac572a6d342ebcaf60d2232ecb8c5751e2 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 31 Jan 2014 18:58:25 +0000 Subject: [PATCH] Let the video shortcode accept a YouTube URL as the value of its `src` attribute, as MediaElement.js supports Chromeless YouTube videos by using a pseudo-mime-type `video/youtube`. HTTP and HTTPS www, non-www, and short url fronts are supported: http://www.youtube.com/watch https://www.youtube.com/watch http://youtube.com/watch https://youtube.com/watch http://youtu.be https://youtu.be Fixes #24764. git-svn-id: https://develop.svn.wordpress.org/trunk@27063 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 5073087474..b5f130d010 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1147,11 +1147,16 @@ function wp_video_shortcode( $attr, $content = '' ) { $width = $w; + $yt_pattern = '#^https?://(:?www\.)?(:?youtube\.com/watch|youtu\.be/)#'; + $primary = false; if ( ! empty( $src ) ) { - $type = wp_check_filetype( $src, wp_get_mime_types() ); - if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) - return sprintf( '%s', esc_url( $src ), esc_html( $src ) ); + if ( ! preg_match( $yt_pattern, $src ) ) { + $type = wp_check_filetype( $src, wp_get_mime_types() ); + if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { + return sprintf( '%s', esc_url( $src ), esc_html( $src ) ); + } + } $primary = true; array_unshift( $default_types, 'src' ); } else { @@ -1216,10 +1221,15 @@ function wp_video_shortcode( $attr, $content = '' ) { if ( ! empty( $$fallback ) ) { if ( empty( $fileurl ) ) $fileurl = $$fallback; - $type = wp_check_filetype( $$fallback, wp_get_mime_types() ); - // m4v sometimes shows up as video/mpeg which collides with mp4 - if ( 'm4v' === $type['ext'] ) - $type['type'] = 'video/m4v'; + + if ( 'src' === $fallback && preg_match( $yt_pattern, $src ) ) { + $type = array( 'type' => 'video/youtube' ); + } else { + $type = wp_check_filetype( $$fallback, wp_get_mime_types() ); + // m4v sometimes shows up as video/mpeg which collides with mp4 + if ( 'm4v' === $type['ext'] ) + $type['type'] = 'video/m4v'; + } $html .= sprintf( $source, $type['type'], esc_url( $$fallback ) ); } }