From c43d4043bdb13f7a6277a1f16b2640069e85fee0 Mon Sep 17 00:00:00 2001 From: arthur791004 Date: Fri, 30 Aug 2024 10:36:15 +0800 Subject: [PATCH] Shortcode: Get rid of custom shortcodes for YouTube and Vimeo in favor 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 --- projects/plugins/jetpack/.phan/baseline.php | 2 +- .../changelog/feat-remove-custom-embed | 4 ++ .../jetpack/modules/shortcodes/vimeo.php | 37 +++++++++++-------- .../jetpack/modules/shortcodes/youtube.php | 13 +++++-- .../modules/shortcodes/test-class.vimeo.php | 4 +- .../modules/shortcodes/test-class.youtube.php | 5 +-- 6 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/feat-remove-custom-embed diff --git a/projects/plugins/jetpack/.phan/baseline.php b/projects/plugins/jetpack/.phan/baseline.php index 30a47dd3b9b97..0e5821691d13d 100644 --- a/projects/plugins/jetpack/.phan/baseline.php +++ b/projects/plugins/jetpack/.phan/baseline.php @@ -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'], diff --git a/projects/plugins/jetpack/changelog/feat-remove-custom-embed b/projects/plugins/jetpack/changelog/feat-remove-custom-embed new file mode 100644 index 0000000000000..fa651da5d0d6c --- /dev/null +++ b/projects/plugins/jetpack/changelog/feat-remove-custom-embed @@ -0,0 +1,4 @@ +Significance: patch +Type: compat + +Embeds: prioritize YouTube and Vimeo embeds provided by WordPress itself, over Jetpack's embedding solution. diff --git a/projects/plugins/jetpack/modules/shortcodes/vimeo.php b/projects/plugins/jetpack/modules/shortcodes/vimeo.php index b462c9a09fab9..b155083f478e5 100644 --- a/projects/plugins/jetpack/modules/shortcodes/vimeo.php +++ b/projects/plugins/jetpack/modules/shortcodes/vimeo.php @@ -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; @@ -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. @@ -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. @@ -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/(?\d+)/)?(?:video/)?(?\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. * @@ -374,7 +379,7 @@ function vimeo_link( $content ) { * Could erroneously capture: * This video (vimeo.com/12345) is teh cat's meow! */ - $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 ), diff --git a/projects/plugins/jetpack/modules/shortcodes/youtube.php b/projects/plugins/jetpack/modules/shortcodes/youtube.php index 14acabc318087..12a04cda37be7 100644 --- a/projects/plugins/jetpack/modules/shortcodes/youtube.php +++ b/projects/plugins/jetpack/modules/shortcodes/youtube.php @@ -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() diff --git a/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.vimeo.php b/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.vimeo.php index 09dc5ce546e9b..52dccdc8416d6 100644 --- a/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.vimeo.php +++ b/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.vimeo.php @@ -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 ) @@ -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 ); diff --git a/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.youtube.php b/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.youtube.php index 44620dd0c6b96..b2268909170d0 100644 --- a/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.youtube.php +++ b/projects/plugins/jetpack/tests/php/modules/shortcodes/test-class.youtube.php @@ -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.