From 91e6a8c01717fa68765c7403bd281c63ee5bc367 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 Jun 2014 01:22:15 +0000 Subject: [PATCH] Because of YouTube's overwhelming dominance in the online video space, we should help our users who paste bad URLs when possible. It is easy to fix `http://youtube.com/embed/acb1233` URLs by turning them into `http://youtube.com/watch?v=abc1233` embeds using an embed handler (we already have one to fix Google Video embeds). We should avoid doing this in almost every other case, but it's YouTube, and it's an easy error for a novice to make. Fixes #24660. Built from https://develop.svn.wordpress.org/trunk@28652 git-svn-id: http://core.svn.wordpress.org/trunk@28470 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/wp-includes/media.php b/wp-includes/media.php index 43b5aab4f9..8a79f8f98e 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -2184,6 +2184,8 @@ function wp_maybe_load_embeds() { return; } + wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/embed/([^/]+)#i', 'wp_embed_handler_youtube' ); + wp_embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', 'wp_embed_handler_googlevideo' ); /** @@ -2240,6 +2242,33 @@ function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_googlevideo', '', $matches, $attr, $url, $rawattr ); } +/** + * The YouTube embed handler callback. Catches URLs that can be parsed but aren't supported by oEmbed. + * + * @since 4.0.0 + * + * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}. + * @param array $attr Embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + * @return string The embed HTML. + */ +function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { + global $wp_embed; + $embed = $wp_embed->autoembed( "https://youtube.com/watch?v={$matches[2]}" ); + /** + * Filter the YoutTube embed output. + * + * @since 4.0.0 + * + * @param string $embed YouTube embed output. + * @param array $attr An array of embed attributes. + * @param string $url The original URL that was matched by the regex. + * @param array $rawattr The original unmodified attributes. + */ + return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr ); +} + /** * Audio embed handler callback. *