From 84911b9707c9c2aa8547cf72cd6580475cc283d9 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 31 Jan 2014 18:59:13 +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. Built from https://develop.svn.wordpress.org/trunk@27063 git-svn-id: http://core.svn.wordpress.org/trunk@26936 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 5073087474..b5f130d010 100644 --- a/wp-includes/media.php +++ b/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 ) ); } }