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 2 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 Add auth token required by Instagram's oEmbed REST API, or proxy through WP.com.
*
* 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
Loading