Skip to content

Commit

Permalink
Issue #841: Make remove_embed() add previous shortcode.
Browse files Browse the repository at this point in the history
On Weston's suggestion.
This function should return the shortcode
to the state before this embed handler changed it.
So it stores the previous callback in $removed_shortcode.
And it adds that callback in remove_embed().
  • Loading branch information
Ryan Kienstra committed Feb 15, 2018
1 parent f13d2ad commit d3cc386
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
24 changes: 19 additions & 5 deletions includes/embeds/class-amp-playlist-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AMP_Playlist_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* The tag of the shortcode.
*
* @var string.
* @var string
*/
const SHORTCODE = 'playlist';

Expand All @@ -28,21 +28,21 @@ class AMP_Playlist_Embed_Handler extends AMP_Base_Embed_Handler {
* This corresponds to the max-width in wp-mediaelement.css:
* .wp-playlist .wp-playlist-current-item img
*
* @var string.
* @var string
*/
const THUMB_MAX_WIDTH = 60;

/**
* The height of the carousel.
*
* @var string.
* @var string
*/
const CAROUSEL_HEIGHT = 160;

/**
* The pattern to get the playlist data.
*
* @var string.
* @var string
*/
const PLAYLIST_REGEX = ':<script type="application/json" class="wp-playlist-script">(.+?)</script>:s';

Expand All @@ -53,6 +53,13 @@ class AMP_Playlist_Embed_Handler extends AMP_Base_Embed_Handler {
*/
public static $playlist_id = 0;

/**
* The removed shortcode callback.
*
* @var string|array
*/
public $removed_shortcode;

/**
* The parsed data for the playlist.
*
Expand All @@ -62,8 +69,15 @@ class AMP_Playlist_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* Registers the playlist shortcode.
* @global array $shortcode_tags
* @return void
*/
public function register_embed() {
global $shortcode_tags;
if ( shortcode_exists( self::SHORTCODE ) ) {
$this->removed_shortcode = $shortcode_tags[ self::SHORTCODE ];
}
add_shortcode( self::SHORTCODE, array( $this, 'shortcode' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'styling' ) );
}
Expand All @@ -74,7 +88,7 @@ public function register_embed() {
* @return void
*/
public function unregister_embed() {
remove_shortcode( self::SHORTCODE );
add_shortcode( self::SHORTCODE, $this->removed_shortcode );
}

/**
Expand Down
14 changes: 8 additions & 6 deletions tests/test-class-amp-playlist-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public function tearDown() {
*/
public function test_register_embed() {
global $shortcode_tags;
$shortcode = 'playlist';
$this->assertFalse( isset( $shortcode_tags[ $shortcode ] ) );
$removed_shortcode = 'wp_playlist_shortcode';
add_shortcode( 'playlist', $removed_shortcode );
$this->instance->register_embed();
$this->assertEquals( 'AMP_Playlist_Embed_Handler', get_class( $shortcode_tags[ $shortcode ][0] ) );
$this->assertEquals( 'shortcode', $shortcode_tags[ $shortcode ][1] );
$this->assertEquals( 'AMP_Playlist_Embed_Handler', get_class( $shortcode_tags[ AMP_Playlist_Embed_Handler::SHORTCODE ][0] ) );
$this->assertEquals( 'shortcode', $shortcode_tags[ AMP_Playlist_Embed_Handler::SHORTCODE ][1] );
$this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->instance, 'styling' ) ) );
$this->assertEquals( $removed_shortcode, $this->instance->removed_shortcode );
$this->instance->unregister_embed();
}

Expand All @@ -74,9 +75,10 @@ public function test_register_embed() {
*/
public function test_unregister_embed() {
global $shortcode_tags;
$shortcode = 'playlist';
$expected_removed_shortcode = 'wp_playlist_shortcode';
$this->instance->removed_shortcode = $expected_removed_shortcode;
$this->instance->unregister_embed();
$this->assertFalse( isset( $shortcode_tags[ $shortcode ] ) );
$this->assertEquals( $expected_removed_shortcode, $shortcode_tags[ AMP_Playlist_Embed_Handler::SHORTCODE ] );
}

/**
Expand Down

0 comments on commit d3cc386

Please sign in to comment.