Skip to content

Commit

Permalink
Shortcode: Fix the youtube url cannot be embedded due to the trailing…
Browse files Browse the repository at this point in the history
… question mark of the youtube id (#39309)

* Shortcode: Fix the youtube url cannot be embeded due to the trailing question mark of the youtube id

* changelog

* Add tests

* Update projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.youtube.php

---------

Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
  • Loading branch information
arthur791004 and jeherve authored Sep 11, 2024
1 parent 63f8b7f commit 94a3510
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Shortcode: Fix the youtube url cannot be embeded due to the trailing question mark of the youtube id
36 changes: 36 additions & 0 deletions projects/plugins/jetpack/modules/shortcodes/youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,42 @@ function wpcom_youtube_embed_crazy_url_init() {
wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
}

/**
* Remove the ending question mark from the video id of the YouTube URL.
*
* Example: https://www.youtube.com/watch?v=AVAWwXeOyyQ?
*
* @since $$next-version$$
*
* @param string $provider URL of the oEmbed provider.
* @param string $url URL of the content to be embedded.
*
* @return string
*/
function wpcom_youtube_oembed_fetch_url( $provider, $url ) {
if ( ! wp_startswith( $provider, 'https://www.youtube.com/oembed' ) ) {
return $provider;
}

$parsed = wp_parse_url( $url );
if ( ! isset( $parsed['query'] ) ) {
return $provider;
}

$query_vars = array();
wp_parse_str( $parsed['query'], $query_vars );
if ( isset( $query_vars['v'] ) && wp_endswith( $query_vars['v'], '?' ) ) {
$url = remove_query_arg( array( 'v' ), $url );
$url = add_query_arg( 'v', preg_replace( '/\?$/', '', $query_vars['v'] ), $url );
}

$provider = remove_query_arg( array( 'url' ), $provider );
$provider = add_query_arg( 'url', rawurlencode( $url ), $provider );

return $provider;
}
add_filter( 'oembed_fetch_url', 'wpcom_youtube_oembed_fetch_url', 10, 2 );

if (
! is_admin()
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,47 @@ public function test_jetpack_shortcode_youtube_dimensions( $query_args, $expecte
$GLOBALS['content_width'] = self::CONTENT_WIDTH;
$this->assertEquals( $expected, jetpack_shortcode_youtube_dimensions( $query_args ) );
}

/**
* List of variation of YouTube embed URLs.
*/
public function get_youtube_urls() {
return array(
'valid_url' => array(
'https://www.youtube.com/watch?v=SVRiktFlWxI',
'https://www.youtube.com/watch?v=SVRiktFlWxI',
),
'short_youtube_url' => array(
'https://youtu.be/gS6_xOABTWo',
'https://youtu.be/gS6_xOABTWo',
),
'video_id_ending_with_question_mark' => array(
'https://www.youtube.com/watch?v=WVbQ-oro7FQ?',
'https://www.youtube.com/watch?v=WVbQ-oro7FQ',
),
);
}

/**
* Test different oEmbed URLs and their output.
*
* @covers ::wpcom_youtube_oembed_fetch_url
* @dataProvider get_youtube_urls
*
* @param string $original The original YouTube provider URL.
* @param string $expected The final YouTube provider URL after wpcom_youtube_oembed_fetch_url.
*/
public function test_youtube_oembed_fetch_url( $original, $expected ) {
$provider_url = apply_filters(
'oembed_fetch_url',
'https://www.youtube.com/oembed?url=' . rawurlencode( $original ),
$original,
''
);

$this->assertEquals(
$provider_url,
'https://www.youtube.com/oembed?url=' . rawurlencode( $expected )
);
}
}

0 comments on commit 94a3510

Please sign in to comment.