Skip to content

Commit

Permalink
Shortcode: Get rid of custom shortcodes for YouTube and Vimeo in favo…
Browse files Browse the repository at this point in the history
…r of Core (#39096)

* Shortcode: Get rid of custom shortcodes for Youtube and Vimeo in favor of Core

* changelog

* Fix tests

* Fix lint

* Fix tests

* Extend regex

* Don't need to mock http response

* Add youtube test back

* Update phan

* Deprecate functions instead of removal

* Update changelog

* Rename tests

* Fix lint

* Use 54910next-version54910
  • Loading branch information
arthur791004 authored and gogdzl committed Oct 25, 2024
1 parent a610746 commit 6d8c7f5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
'modules/shortcodes/vimeo.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchArgument'],
'modules/shortcodes/vr.php' => ['PhanPluginDuplicateConditionalNullCoalescing'],
'modules/shortcodes/wordads.php' => ['PhanNoopNew'],
'modules/shortcodes/youtube.php' => ['PhanRedefineFunction', 'PhanTypeMismatchArgument'],
'modules/shortcodes/youtube.php' => ['PhanRedefineFunction'],
'modules/shortlinks.php' => ['PhanPluginDuplicateExpressionAssignmentOperation', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentProbablyReal'],
'modules/simple-payments/simple-payments.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchReturn'],
'modules/sitemaps/sitemap-buffer-fallback.php' => ['PhanTypeArraySuspicious'],
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/feat-remove-custom-embed
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: compat

Embeds: prioritize YouTube and Vimeo embeds provided by WordPress itself, over Jetpack's embedding solution.
37 changes: 21 additions & 16 deletions projects/plugins/jetpack/modules/shortcodes/vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@
function jetpack_shortcode_get_vimeo_id( $atts ) {
if ( isset( $atts[0] ) ) {
$atts[0] = trim( $atts[0], '=' );
$id = false;
if ( is_numeric( $atts[0] ) ) {
$id = (int) $atts[0];
} elseif ( preg_match( '|vimeo\.com/(\d+)/?$|i', $atts[0], $match ) ) {
$id = (int) $match[1];
} elseif ( preg_match( '|player\.vimeo\.com/video/(\d+)/?$|i', $atts[0], $match ) ) {
$id = (int) $match[1];
return (int) $atts[0];
}

return $id;
/**
* Extract Vimeo ID from the URL. For examples:
* https://vimeo.com/12345
* https://vimeo.com/289091934/cd1f466bcc
* https://vimeo.com/album/2838732/video/6342264
* https://vimeo.com/groups/758728/videos/897094040
* https://vimeo.com/channels/staffpicks/123456789
* https://vimeo.com/album/1234567/video/7654321
* https://player.vimeo.com/video/18427511
*/
$pattern = '/(?:https?:\/\/)?vimeo\.com\/(?:groups\/\d+\/videos\/|album\/\d+\/video\/|video\/|channels\/[^\/]+\/videos\/|[^\/]+\/)?([0-9]+)(?:[^\'\"0-9<]|$)/i';
$match = array();
if ( preg_match( $pattern, $atts[0], $match ) ) {
return (int) $match[1];
}
}

return 0;
Expand Down Expand Up @@ -250,6 +259,7 @@ class_exists( 'Jetpack_AMP_Support' )
* Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
*
* @since 3.9
* @deprecated since $$next-version$$
*
* @param array $matches Regex partial matches against the URL passed.
* @param array $attr Attributes received in embed response.
Expand All @@ -258,6 +268,7 @@ class_exists( 'Jetpack_AMP_Support' )
* @return string Return output of Vimeo shortcode with the proper markup.
*/
function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
_deprecated_function( __FUNCTION__, 'jetpack-$$next-version$$' );
$vimeo_info = array( $url );

// If we are able to extract a video ID, use it in the shortcode instead of the full URL.
Expand All @@ -278,21 +289,15 @@ function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
* http://player.vimeo.com/video/18427511
*
* @since 3.9
* @deprecated since $$next-version$$
*
* @uses wpcom_vimeo_embed_url
*/
function wpcom_vimeo_embed_url_init() {
_deprecated_function( __FUNCTION__, 'jetpack-$$next-version$$' );
wp_embed_register_handler( 'wpcom_vimeo_embed_url', '#https?://(?:[^/]+\.)?vimeo\.com/(?:album/(?<album_id>\d+)/)?(?:video/)?(?<video_id>\d+)(?:/.*)?$#i', 'wpcom_vimeo_embed_url' );
}

/*
* Register handler to modify Vimeo embeds using Jetpack's shortcode output.
* This does not happen on WordPress.com, since embeds are handled by core there.
*/
if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
add_action( 'init', 'wpcom_vimeo_embed_url_init' );
}

/**
* Transform a Vimeo embed iFrame into a Vimeo shortcode.
*
Expand Down Expand Up @@ -374,7 +379,7 @@ function vimeo_link( $content ) {
* Could erroneously capture:
* <a href="some.link/maybe/even/vimeo">This video (vimeo.com/12345) is teh cat's meow!</a>
*/
$plain_url = "(?:[^'\">]?\/?(?:https?:\/\/)?vimeo\.com[^0-9]+)([0-9]+)(?:[^'\"0-9<]|$)";
$plain_url = "(?:[^'\">]?\/?(?:https?:\/\/)?vimeo\.com\/(?:groups\/\d+\/videos\/|album\/\d+\/video\/|video\/|channels\/[^\/]+\/videos\/|[^\/]+\/)?)([0-9]+)(?:[^'\"0-9<]|$)";

return jetpack_preg_replace_callback_outside_tags(
sprintf( '#%s|%s#i', $shortcode, $plain_url ),
Expand Down
13 changes: 9 additions & 4 deletions projects/plugins/jetpack/modules/shortcodes/youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,21 +527,26 @@ function jetpack_shortcode_youtube_dimensions( $query_args ) {
* For bare URLs on their own line of the form
* http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
*
* @param array $matches Regex partial matches against the URL passed.
* @param array $attr Attributes received in embed response.
* @param array $url Requested URL to be embedded.
* @deprecated since $$next-version$$
*
* @param array $matches Regex partial matches against the URL passed.
* @param array $attr Attributes received in embed response.
* @param string $url Requested URL to be embedded.
*/
function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
_deprecated_function( __FUNCTION__, 'jetpack-$$next-version$$' );
return youtube_id( $url );
}

/**
* Add a new handler to automatically transform custom Youtube URLs (like playlists) into embeds.
*
* @deprecated since $$next-version$$
*/
function wpcom_youtube_embed_crazy_url_init() {
_deprecated_function( __FUNCTION__, 'jetpack-$$next-version$$' );
wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
}
add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );

if (
! is_admin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function get_vimeo_urls() {
* @param string $url The URL to test.
* @param string $video_id The expected video ID.
*/
public function test_replace_url_with_iframe_in_the_content( $url, $video_id ) {
public function test_shortcodes_vimeo_replace_url_with_iframe_in_the_content( $url, $video_id ) {
if (
( defined( 'IS_WPCOM' ) && IS_WPCOM )
|| ( defined( 'IS_ATOMIC' ) && IS_ATOMIC )
Expand All @@ -186,7 +186,7 @@ public function test_replace_url_with_iframe_in_the_content( $url, $video_id ) {

global $post;

$post = self::factory()->post->create_and_get( array( 'post_content' => $url ) );
$post = self::factory()->post->create_and_get( array( 'post_content' => "[vimeo $url]" ) );

do_action( 'init' );
setup_postdata( $post );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ public function get_youtube_id_options() {
* @covers ::youtube_shortcode
* @since 3.9
*/
public function test_replace_url_with_iframe_in_the_content() {
public function test_shortcodes_youtube_replace_url_with_iframe_in_the_content() {
global $post;

$youtube_id = 'JaNH56Vpg-A';
$url = 'http://www.youtube.com/watch?v=' . $youtube_id;
$post = self::factory()->post->create_and_get( array( 'post_content' => $url ) );
$post = self::factory()->post->create_and_get( array( 'post_content' => "[youtube $url]" ) );

wpcom_youtube_embed_crazy_url_init();
setup_postdata( $post );
ob_start();
// This below is needed since Core inserts "loading=lazy" right after the iframe opener.
Expand Down

0 comments on commit 6d8c7f5

Please sign in to comment.