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
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9436ac2
Add a way to deprecate the AMP plugin's handling of Vimeo shortcode
kienstra Oct 31, 2019
73b8b10
Change the name of the class, as it changed in the Jetpack PR
kienstra Nov 1, 2019
5ceb39b
Change the name of the filter again, as it changed in the Jetpack PR
kienstra Nov 1, 2019
26a22c7
Allow using the helper function in any embed handler
kienstra Nov 1, 2019
92d9de1
Use the helper function before adding the [tweet] shortcode
kienstra Nov 1, 2019
a98f88c
Merge branch 'develop' into update/vimeo-shortocode-support
kienstra Nov 1, 2019
85077be
Also defer AMP [soundcloud] shortcode handling to Jetpack
kienstra Nov 1, 2019
b64392b
Simplify check for whether Jetpack AMP shortocode is available
kienstra Nov 4, 2019
3e0e649
Change @deprecated tag to be 'Moved to...'
kienstra Nov 4, 2019
d0b73eb
Update the class_exists() call to funciton_exists(), as it's now a fu…
kienstra Nov 5, 2019
0192ff5
Change function name to reflect the change in the Jetpack PR
kienstra Nov 6, 2019
93cfaba
Update the function names to reflect the latest in the PR
kienstra Nov 7, 2019
fcda766
Deprecate this plugin's handling of the [youtube] shortcode
kienstra Nov 11, 2019
302d611
Update the souncloud conditional for the new function
kienstra Nov 13, 2019
822ca4b
Remove add_shortcode() and remove_shortcode() for the [soundcloud] sh…
kienstra Nov 14, 2019
a4bcd1b
Remove the add_shortcode() and remove_shortcode() calls for [tweet]
kienstra Nov 14, 2019
5d74750
Deprecate the handling of the [instagram] shortcode
kienstra Nov 15, 2019
00b2e2c
Remove [dailymotion] shortcode handling and deprecate function
kienstra Nov 16, 2019
ec5257e
Remove needless SHORTCODE_TAG constants
kienstra Nov 19, 2019
6f6d3f7
For the [vimeo] shortcode, remove the add_shortcode() call
kienstra Nov 19, 2019
e2e2d5c
Merge in develop, resolve conflicts
kienstra Nov 19, 2019
d53df79
Delete shortcode handlers and their tests
kienstra Nov 20, 2019
bac7db1
Remove unused SHORTCODE_TAG constant
kienstra Nov 20, 2019
672997e
Merge branch 'develop' of github.com:ampproject/amp-wp into update/vi…
westonruter Dec 4, 2019
e168813
Remove call to removed method; update phpdoc
westonruter Dec 4, 2019
1a4b700
Merge in develop, resolve conflicts
kienstra Dec 4, 2019
9f9c2ff
Remove AMP_Twitter_Embed_Handler::filter_embed_oembed_html()
kienstra Dec 4, 2019
2d5ae27
Correct an apparent error I made in resolving a merge conflict
kienstra Dec 4, 2019
5097040
Restore AMP_YouTube_Embed_Handler::video_override()
kienstra Dec 4, 2019
b300e9d
Deprecate AMP_YouTube_Embed_Handler::shortcode()
kienstra Dec 4, 2019
fdb463e
Refactor AMP_Vimeo_Embed_Handler::shortcode() into video_override()
kienstra Dec 4, 2019
2d71b6b
Replace a call to ->shortcode() with the logic from shortcode()
kienstra Dec 4, 2019
ba51034
Align @param descriptions in test_video_override
kienstra Dec 4, 2019
6ae3419
Delete AMP_Twitter_Embed_Handler::oembed()
kienstra Dec 5, 2019
4726922
Delete AMP_YouTube_Embed_Handler::shortcode() and oembed()
kienstra Dec 5, 2019
19e05de
Remove unused AMP_YouTube_Embed_Handler::sanitize_v_arg method after …
westonruter Dec 6, 2019
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
14 changes: 14 additions & 0 deletions includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,18 @@ public function __construct( $args = [] ) {
public function get_scripts() {
return [];
}

/**
* Gets whether a certain Jetpack shortcode's AMP implementation is available in Jetpack.
*
* This logic is being migrated to Jetpack, so check whether it's available there
* before running the shortcode logic here.
*
* @param string $shortcode_tag The tag (name) of the shortcode.
* @return bool Whether the AMP version of the passed shortcode is available in Jetpack.
*/
public function is_amp_shortcode_available_in_jetpack( $shortcode_tag ) {
$upper_tag = ucfirst( $shortcode_tag );
return false !== has_filter( 'do_shortcode_tag', [ "Jetpack_AMP_{$upper_tag}_Shortcode", 'filter_shortcode' ] );
}
}
17 changes: 11 additions & 6 deletions includes/embeds/class-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class AMP_SoundCloud_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* The tag (name) of the shortcode.
*
* @var string
*/
const SHORTCODE_TAG = 'soundcloud';

/**
* Default height.
*
Expand All @@ -23,9 +30,8 @@ class AMP_SoundCloud_Embed_Handler extends AMP_Base_Embed_Handler {
* Register embed.
*/
public function register_embed() {
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.

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.

}
add_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10, 2 );
}
Expand All @@ -34,9 +40,8 @@ public function register_embed() {
* Unregister embed.
*/
public function unregister_embed() {
if ( function_exists( 'soundcloud_shortcode' ) ) {
// @todo Move this to Jetpack.
remove_shortcode( 'soundcloud' );
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
remove_shortcode( self::SHORTCODE_TAG );
}
remove_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10 );
}
Expand Down
15 changes: 13 additions & 2 deletions includes/embeds/class-amp-twitter-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {
*/
const URL_PATTERN_TIMELINE = '#https?:\/\/twitter\.com(?:\/\#\!\/|\/)(?P<username>[a-zA-Z0-9_]{1,20})(?:$|\/(?P<type>likes|lists)(\/(?P<id>[a-zA-Z0-9_-]+))?)#i';

/**
* The tag (name) of the shortcode.
*
* @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 😄


/**
* Tag.
*
Expand All @@ -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

add_shortcode( self::SHORTCODE_TAG, [ $this, 'shortcode' ] );
}
wp_embed_register_handler( 'amp-twitter', self::URL_PATTERN, [ $this, 'oembed' ], -1 );
wp_embed_register_handler( 'amp-twitter-timeline', self::URL_PATTERN_TIMELINE, [ $this, 'oembed_timeline' ], -1 );
}
Expand All @@ -55,7 +64,9 @@ public function register_embed() {
* Unregisters embed.
*/
public function unregister_embed() {
remove_shortcode( 'tweet' ); // Note: This is a Jetpack shortcode.
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
remove_shortcode( self::SHORTCODE_TAG );
}
wp_embed_unregister_handler( 'amp-twitter', -1 );
wp_embed_unregister_handler( 'amp-twitter-timeline', -1 );
}
Expand Down
25 changes: 23 additions & 2 deletions includes/embeds/class-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,27 @@
*/
class AMP_Vimeo_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* The embed URL pattern.
*
* @var string
*/
const URL_PATTERN = '#https?:\/\/(www\.)?vimeo\.com\/.*#i';

/**
* The aspect ratio.
*
* @var float
*/
const RATIO = 0.5625;

/**
* The tag (name) of the shortcode.
*
* @var string
*/
const SHORTCODE_TAG = 'vimeo';

/**
* Default width.
*
Expand Down Expand Up @@ -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

add_shortcode( self::SHORTCODE_TAG, [ $this, 'shortcode' ] );
}
add_filter( 'wp_video_shortcode_override', [ $this, 'video_override' ], 10, 2 );
}

Expand All @@ -59,7 +78,9 @@ public function register_embed() {
*/
public function unregister_embed() {
wp_embed_unregister_handler( 'amp-vimeo', -1 );
remove_shortcode( 'vimeo' );
if ( ! $this->is_amp_shortcode_available_in_jetpack( self::SHORTCODE_TAG ) ) {
remove_shortcode( self::SHORTCODE_TAG );
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/php/test-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
*/
class AMP_Vimeo_Embed_Test extends WP_UnitTestCase {

/**
* Tears down the environment after each test.
*
* @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.

}

/**
* Get conversion data.
*
Expand Down Expand Up @@ -109,4 +118,23 @@ public function test__get_scripts( $source, $expected ) {

$this->assertEquals( $expected, $scripts );
}

/**
* Test is_amp_shortcode_available_in_jetpack.
*
* @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.

remove_all_filters( 'do_shortcode_tag' );
$embed = new AMP_Vimeo_Embed_Handler();

// With the filter not added, this filter should return false.
$this->assertFalse( $embed->is_amp_shortcode_available_in_jetpack( 'vimeo' ) );

add_filter( 'do_shortcode_tag', [ 'Jetpack_AMP_Vimeo_Shortcode', 'filter_shortcode' ] );

// With the filter added, this filter should return false.
$this->assertTrue( $embed->is_amp_shortcode_available_in_jetpack( 'vimeo' ) );
$this->assertFalse( $embed->is_amp_shortcode_available_in_jetpack( 'wrongshortcode' ) );
}
}