Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcode: Fix the youtube url cannot be embedded due to the trailing question mark of the youtube id #39309

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
);
}
}
Loading