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

Remove handling of Jetpack shortcodes #3678

Merged
merged 36 commits into from
Dec 6, 2019

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Oct 31, 2019

Summary

This plugin's handling of Jetpack shortcodes, like [vimeo] and [tweet], is possibly being moved to Jetpack.

So this PR looks for the presence of a new filter that Jetpack should have. If that's present, this plugin doesn't run the logic for that shortcode.

Related to #3309

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests. (It's not really tested, but it's almost entirely deletions. So I don't think more tests are needed.)
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

This looks for the presence of the
new filter that Jetpack should have,
at least in the current state of the PR.
This class needed to be renamed
becase of a Code Climate report.
This will allow checking for whether
Jetpack already supports AMP for the shortcode,
for any shortcode passed to the function.
If there is support in Jetpack,
there's no need for the AMP plugin
logic to run.
@kienstra kienstra changed the title [WIP] Add a way to deprecate this plugin's handling of Vimeo shortcode [WIP] Add a way to deprecate this plugin's handling of Jetpack shortcodes Nov 1, 2019
Like the other shortcodes,
this could possibly be moved to Jetpack.
if ( function_exists( 'soundcloud_shortcode' ) ) {
// @todo Move this to Jetpack.
add_shortcode( 'soundcloud', [ $this, 'shortcode' ] );
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of this method, wouldn't it be the same to just do ! class_exists( 'Jetpack_AMP_Soundcloud_Shortcode' )? This code will quickly become dead code that should be removed entirely from the AMP plugin, once a new version of Jetpack has been released, so we don't need to worry too much about keeping this code around too long.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, b64392b checks whether the class exists.

// @todo Move this to Jetpack.
add_shortcode( 'soundcloud', [ $this, 'shortcode' ] );
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
add_shortcode( self::SHORTCODE_TAG, [ $this, 'shortcode' ] );
Copy link
Member

Choose a reason for hiding this comment

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

Please mark the shortcode methods as @deprecated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, 3e0e649 does that.

@@ -46,7 +53,9 @@ class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {
* Registers embed.
*/
public function register_embed() {
add_shortcode( 'tweet', [ $this, 'shortcode' ] ); // Note: This is a Jetpack shortcode.
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

See notes above for Soundcloud.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, applied in b64392b

@@ -50,7 +67,9 @@ public function __construct( $args = [] ) {
*/
public function register_embed() {
wp_embed_register_handler( 'amp-vimeo', self::URL_PATTERN, [ $this, 'oembed' ], -1 );
add_shortcode( 'vimeo', [ $this, 'shortcode' ] );
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

See notes above for Soundcloud.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, b64392b changes this

* @inheritDoc
*/
public function tearDown() {
remove_all_filters( 'do_shortcode_tag' );
Copy link
Member

Choose a reason for hiding this comment

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

I believe this isn't necessary because filters get reset after each test anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, sounds good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

3e0e649#diff-58db4548e89d6f4290514ec825c906ecL15 removes this tearDown() method.

*
* @covers AMP_Base_Embed_Handler::is_amp_shortcode_available_in_jetpack()
*/
public function test_is_amp_shortcode_available_in_jetpack() {
Copy link
Member

Choose a reason for hiding this comment

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

This test may not be necessary. I think we can rather just check if the Jetpack_AMP_Vimeo_Shortcode class exists.

As Weston suggested, it's possible to do this

with a simple class_exsts() call.
This should be more descriptive.
Also, delete the remove_all_filters() call.
…nction

Instead of class method,
this now uses a plain function.
The function name is no jetpack_amp_vimeo_shortcode
in Automattic/jetpack#13921
The PR to migrate [vimeo] shortcode has changed:
https://github.com/Automattic/jetpack/pull/13921/files
So change the function_exists() check
and the @deprecated tag.
This is being moved to Jetpack,
though the actual function name might change.
There's now a new function,
jetpack_amp_soundcloud_shortcode(),
instead of the previous class.
…ortcode

For the current Jetpack migration,
this shouldn't be needed.
Also, change the deprecated tag
to match the new function name.
This is being moved to Jetpack,
so there shouldn't be a need for it here.
This is being moved to Jetpack,
so delete the add_shortcode() and remove_shortcode() calls.
*
* @var string
*/
const SHORTCODE_TAG = 'vimeo';
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be tweet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in ec5257e. It turns out that the constant wasn't needed at all 😄

@westonruter
Copy link
Member

Excellent. Let's snooze this issue until 8.0.0 is released.

@westonruter westonruter changed the title [WIP] Remove this plugin's handling of Jetpack shortcodes Remove AMP plugin's handling of Jetpack shortcodes Dec 4, 2019
@westonruter westonruter changed the title Remove AMP plugin's handling of Jetpack shortcodes Remove handling of Jetpack shortcodes Dec 4, 2019
@westonruter westonruter added this to the v1.5 milestone Dec 4, 2019
…meo-shortocode-support

* 'develop' of github.com:ampproject/amp-wp: (159 commits)
  Return allowed KSES tags if not an array (#3879)
  Update dependency @wordpress/date to v3.6.0 (#3739)
  Update dependency @babel/plugin-proposal-object-rest-spread to… (#3813)
  Update dependency @babel/core to v7.7.4 (#3686)
  Update dependency @wordpress/api-fetch to v3.7.0 (#3731)
  Update dependency @wordpress/keycodes to v2.7.0 (#3745)
  Bundle common font files for externalizing data: URLs (#3866)
  Update dependency css-loader to v3.2.1 (#3870)
  Update dependency core-js to v3.4.7 (#3873)
  Update dependency core-js to v3.4.5 (#3695)
  Update dependency eslint-plugin-react to v7.17.0 (#3845)
  Update dependency eslint-plugin-jest to v23.1.1 (#3702)
  Update dependency eslint to v6.7.2 (#3811)
  Update dependency autoprefixer to v9.7.3 (#3850)
  Update dependency browserslist to v4.8.0 (#3849)
  Remove second sentence from paired browsing dialog
  Add async attribute to amp-paired-browsing-client script
  Restrict paired browsing to when dev mode is enabled
  Add robots noindex/nofollow meta tag in paired browsing app
  Remove extraneous method in favor of (temporary) closure
  ...
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

@kienstra I found some references to removed shortcode methods which I removed in e168813. However, there is one left which needs some more work from you, namely AMP_Vimeo_Embed_Handler::video_override(). The shortcode method may need to be restored in this case, as Vimeo actually is something that core directly supports for the video shortcode.

Please also check for YouTube.

@@ -51,7 +51,6 @@ public function __construct( $args = [] ) {
*/
public function register_embed() {
add_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10, 2 );
add_shortcode( 'youtube', [ $this, 'shortcode' ] ); // @todo Deprecated. See <https://github.com/ampproject/amp-wp/issues/3309>.
add_filter( 'wp_video_shortcode_override', [ $this, 'video_override' ], 10, 2 );
Copy link
Member

Choose a reason for hiding this comment

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

This video_override method needs to be restored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. I'll do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's restored in 5097040

There were conflicts in:
includes/embeds/class-amp-twitter-embed-handler.php
tests/php/test-amp-twitter-embed.php
These were trivial.
@kienstra
Copy link
Contributor Author

kienstra commented Dec 4, 2019

It looks like I didn't resolve the conflicts very well.

This was removed in:
4b39169
Apparently, I didn't resolve the merge conflict very well.
There probably wasn't a reason to edit
AMP_Twitter_Embed_Handler::oembed()
so revert that.
As Weston mentioned,
this is a filter callback, and can't
be deleted.
This is called by another deprecated method:
oembed().
So keep this for now.
That method called shortcode(),
so simply move the needed parts of shortcode()
into video_override().
shortcode() was deleted, so instead of
restoring it, copy the needed logic.
@kienstra
Copy link
Contributor Author

kienstra commented Dec 4, 2019

However, there is one left which needs some more work from you, namely AMP_Vimeo_Embed_Handler::video_override()

Good catch. That method's call of shortcode() is now replaced with the small amount of logic from shortcode() that it used.

@kienstra
Copy link
Contributor Author

kienstra commented Dec 4, 2019

The [video] shortcodes for YouTube and Vimeo work again.

For example:

[video src="https://www.youtube.com/watch?v=NeXQEJNWO5w"]
[video src="https://vimeo.com/45879568"]

To follow the latest convention in the plugin,
align these.
Comment on lines 67 to 91
* @param array $matches URL pattern matches.
* @return string Rendered oEmbed.
*/
public function oembed() {
public function oembed( $matches ) {
_deprecated_function( __METHOD__, '1.1' );
return '';
$id = false;

if ( isset( $matches['tweet'] ) && is_numeric( $matches['tweet'] ) ) {
$id = $matches['tweet'];
}

if ( ! $id ) {
return '';
}

$this->did_convert_elements = true;
return AMP_HTML_Utils::build_tag(
$this->amp_tag,
[
'data-tweetid' => $id,
'layout' => 'responsive',
'width' => $this->args['width'],
'height' => $this->args['height'],
]
);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this method anymore, do we? Can't it just be emptied out, as I did in: e168813

	/**
	 * Render oEmbed.
	 *
	 * @deprecated Since 1.1 as now the sanitize_raw_embeds() is used exclusively, allowing the
	 *             original oEmbed response to be wrapped with `amp-twitter`.
	 *
	 * @see \WP_Embed::shortcode()
	 *
	 * @return string Rendered oEmbed.
	 */
	public function oembed() {
		_deprecated_function( __METHOD__, '1.1' );
		return '';
	}

Copy link
Member

Choose a reason for hiding this comment

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

Actually, we might as well just remove it entirely, and it is not being used. It has been deprecated since 1.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, it's now removed in 6ae3419.

Comment on lines 127 to 170
/**
* Gets AMP-compliant markup for the YouTube shortcode.
*
* @deprecated 1.5.0 This was moved to Jetpack, in jetpack_amp_youtube_shortcode().
*
* @param array $attr The YouTube attributes.
* @return string YouTube shortcode markup.
*/
public function shortcode( $attr ) {
_deprecated_function( __METHOD__, '1.5.0' );
$url = false;

if ( isset( $attr[0] ) ) {
$url = ltrim( $attr[0], '=' );
} elseif ( function_exists( 'shortcode_new_to_old_params' ) ) {
$url = shortcode_new_to_old_params( $attr );
}

if ( empty( $url ) ) {
return '';
}

$video_id = $this->get_video_id_from_url( $url );

return $this->render( compact( 'video_id' ), $url );
}

/**
* Render oEmbed.
*
* @see \WP_Embed::shortcode()
* @deprecated This is no longer being used.
*
* @param array $matches URL pattern matches.
* @param array $attr Shortcode attribues.
* @param string $url URL.
* @return string Rendered oEmbed.
*/
public function oembed() {
public function oembed( $matches, $attr, $url ) {
unset( $matches, $attr );
_deprecated_function( __METHOD__, '1.5.0' );
return '';

return $this->shortcode( [ $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 these methods aren't used anymore, can't they just be made to return an empty string while having the _deprecated_function() calls?

We might as well just remove them altogether because they aren't being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, 4726922 removes AMP_YouTube_Embed_Handler::shortcode() and oembed(), as they're not used anywhere.

Comment on lines 67 to 91
* @param array $matches URL pattern matches.
* @return string Rendered oEmbed.
*/
public function oembed() {
public function oembed( $matches ) {
_deprecated_function( __METHOD__, '1.1' );
return '';
$id = false;

if ( isset( $matches['tweet'] ) && is_numeric( $matches['tweet'] ) ) {
$id = $matches['tweet'];
}

if ( ! $id ) {
return '';
}

$this->did_convert_elements = true;
return AMP_HTML_Utils::build_tag(
$this->amp_tag,
[
'data-tweetid' => $id,
'layout' => 'responsive',
'width' => $this->args['width'],
'height' => $this->args['height'],
]
);
Copy link
Member

Choose a reason for hiding this comment

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

Actually, we might as well just remove it entirely, and it is not being used. It has been deprecated since 1.1.

kienstra and others added 3 commits December 5, 2019 04:31
As Weston mentioned, this has been deprecated
since v1.1.
As Weston mentioned,
these aren't being used and can be deleted.
@westonruter westonruter merged commit 6de46e0 into develop Dec 6, 2019
@westonruter westonruter deleted the update/vimeo-shortocode-support branch December 6, 2019 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants