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: Get rid of custom shortcodes for YouTube and Vimeo in favor of Core #39096

Merged
merged 14 commits into from
Aug 30, 2024
18 changes: 6 additions & 12 deletions projects/plugins/jetpack/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@
// PhanTypeArraySuspiciousNullable : 60+ occurrences
// PhanRedefineFunction : 50+ occurrences
// PhanTypeMismatchArgumentNullable : 50+ occurrences
// PhanTypeExpectedObjectPropAccess : 45+ occurrences
// PhanParamTooMany : 40+ occurrences
// PhanPluginDuplicateAdjacentStatement : 40+ occurrences
// PhanTypeExpectedObjectPropAccess : 40+ occurrences
// PhanTypeMismatchArgumentInternal : 40+ occurrences
// PhanUndeclaredProperty : 35+ occurrences
// PhanParamSignatureMismatch : 25+ occurrences
// PhanPluginSimplifyExpressionBool : 25+ occurrences
// PhanTypeMismatchDefault : 25+ occurrences
// PhanTypeMismatchPropertyProbablyReal : 25+ occurrences
// PhanTypeMissingReturn : 25+ occurrences
// PhanTypeSuspiciousNonTraversableForeach : 25+ occurrences
// PhanDeprecatedProperty : 20+ occurrences
// PhanPluginSimplifyExpressionBool : 20+ occurrences
// PhanTypeArraySuspicious : 20+ occurrences
// PhanTypeMismatchDimFetch : 20+ occurrences
// PhanPluginMixedKeyNoKey : 15+ occurrences
// PhanSuspiciousMagicConstant : 15+ occurrences
// PhanTypeExpectedObjectPropAccessButGotNull : 15+ occurrences
// PhanTypeMismatchArgumentNullableInternal : 15+ occurrences
// PhanTypeMismatchPropertyDefault : 15+ occurrences
// PhanUndeclaredMethod : 15+ occurrences
// PhanPluginDuplicateExpressionAssignmentOperation : 10+ occurrences
// PhanRedefineClass : 10+ occurrences
// PhanRedundantConditionInLoop : 10+ occurrences
// PhanTypeInvalidDimOffset : 10+ occurrences
// PhanTypeMismatchArgumentNullableInternal : 10+ occurrences
// PhanTypeMismatchProperty : 10+ occurrences
// PhanTypeMismatchReturnNullable : 10+ occurrences
// PhanUndeclaredFunction : 10+ occurrences
Expand All @@ -56,12 +56,12 @@
// PhanTypeMismatchArgumentInternalReal : 7 occurrences
// PhanCommentAbstractOnInheritedMethod : 6 occurrences
// PhanDeprecatedClass : 5 occurrences
// PhanImpossibleCondition : 5 occurrences
// PhanNonClassMethodCall : 5 occurrences
// PhanTypeArraySuspiciousNull : 5 occurrences
// PhanTypeMismatchDimAssignment : 5 occurrences
// PhanTypeSuspiciousStringExpression : 5 occurrences
// PhanAccessMethodInternal : 4 occurrences
// PhanImpossibleCondition : 4 occurrences
// PhanTypeInvalidLeftOperandOfAdd : 4 occurrences
// PhanTypeInvalidLeftOperandOfBitwiseOp : 4 occurrences
// PhanTypeInvalidRightOperandOfBitwiseOp : 4 occurrences
Expand All @@ -71,29 +71,23 @@
// PhanImpossibleTypeComparison : 3 occurrences
// PhanPluginUnreachableCode : 3 occurrences
// PhanStaticPropIsStaticType : 3 occurrences
// PhanTypeConversionFromArray : 3 occurrences
// PhanTypeMismatchArgumentReal : 3 occurrences
// PhanTypeObjectUnsetDeclaredProperty : 3 occurrences
// PhanUndeclaredMethodInCallable : 3 occurrences
// PhanCompatibleNegativeStringOffset : 2 occurrences
// PhanImpossibleConditionInLoop : 2 occurrences
// PhanParamTooManyCallable : 2 occurrences
// PhanPluginDuplicateSwitchCaseLooseEquality : 2 occurrences
// PhanRedefineFunctionInternal : 2 occurrences
// PhanStaticCallToNonStatic : 2 occurrences
// PhanTypeMismatchArgumentInternalProbablyReal : 2 occurrences
// PhanUndeclaredClassInCallable : 2 occurrences
// PhanUndeclaredClassMethod : 2 occurrences
// PhanDeprecatedPartiallySupportedCallable : 1 occurrence
// PhanEmptyFQSENInCallable : 1 occurrence
// PhanEmptyForeach : 1 occurrence
// PhanInfiniteRecursion : 1 occurrence
// PhanPluginDuplicateSwitchCase : 1 occurrence
// PhanPluginInvalidPregRegex : 1 occurrence
// PhanPluginRedundantAssignmentInLoop : 1 occurrence
// PhanPluginUseReturnValueInternalKnown : 1 occurrence
// PhanTypeComparisonFromArray : 1 occurrence
// PhanTypeInvalidRightOperandOfNumericOp : 1 occurrence
// PhanTypeConversionFromArray : 1 occurrence
// PhanTypeVoidArgument : 1 occurrence
// PhanUndeclaredConstant : 1 occurrence
// PhanUndeclaredExtendedClass : 1 occurrence
Expand Down Expand Up @@ -392,7 +386,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.
35 changes: 19 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 x.x.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the $$next-version$$ placeholder; it will be automatically replaced on release.

When needing to add a package version number inside a DocBlock, please use `$$next-version$$` as such:
- `@since $$next-version$$`
- `@deprecated $$next-version$$`
- `@deprecated since $$next-version$$`
- `_deprecated_function( __METHOD__, 'package-$$next-version$$' );` (other WordPress deprecation functions also work, but note it must be all on one line).
The `$$next-version$$` specifier will be automatically replaced with the correct package version number the next time a new version of that package is released.

Suggested change
* @deprecated since x.x.x
* @deprecated since $$next-version$$

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On top of that deprecation warning, I think you could add a warning to developers via _deprecated_function:
https://developer.wordpress.org/reference/functions/_deprecated_function/

- Deprecate classes, [functions](https://developer.wordpress.org/reference/functions/_deprecated_function/), and methods in the same way, while still returning its replacement if there is one.
- Deprecated code should remain in Jetpack for 6 months, so third-parties have time to find out about the deprecations and update their codebase.

*
* @param array $matches Regex partial matches against the URL passed.
* @param array $attr Attributes received in embed response.
Expand Down Expand Up @@ -278,21 +288,14 @@ function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
* http://player.vimeo.com/video/18427511
*
* @since 3.9
* @deprecated since x.x.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @deprecated since x.x.x
* @deprecated since $$next-version$$

*
* @uses wpcom_vimeo_embed_url
*/
function wpcom_vimeo_embed_url_init() {
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 +377,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
11 changes: 7 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,24 @@ 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 x.x.x
*
* @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 ) {
return youtube_id( $url );
}

/**
* Add a new handler to automatically transform custom Youtube URLs (like playlists) into embeds.
*
* @deprecated since x.x.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @deprecated since x.x.x
* @deprecated since $$next-version$$

*/
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' );
}
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]" ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the content goes from just a URL to a shortcode with a URL, should the function name be updated accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to test_shortcodes_vimeo_replace_url_with_iframe_in_the_content


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
Loading