Skip to content

Commit

Permalink
Migrate the [tweet] shortcode AMP handling to Jetpack (#14054)
Browse files Browse the repository at this point in the history
* Migrate [tweet] shortcode AMP conversion to AMP

This mainly copies the logic in the
AMP plugin into Jetpack.

* Add a test case for width=0

In that case, the width
should simply be the $default_width.

* Remove extra variable $initial_width

There's probably no need to run absint() first,
so simply set the $width.

* Commit Jeremy's suggestion to possibly markTestSkipped()

Co-Authored-By: Jeremy Herve <jeremy@tagada.hu>

* Move the 'wp_footer' hook to an else block

As Jeremy mentioned,
this should not run for AMP endpoints.
  • Loading branch information
kienstra authored and jeherve committed Nov 19, 2019
1 parent 4a5d42e commit 2479f65
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
17 changes: 14 additions & 3 deletions modules/shortcodes/tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,23 @@ public static function jetpack_tweet_shortcode( $atts ) {
remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
}

// Add Twitter widgets.js script to the footer.
add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );

/** This action is documented in modules/widgets/social-media-icons.php */
do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );

if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
$width = ! empty( $attr['width'] ) ? $attr['width'] : 600;
$height = 480;
$output = sprintf(
'<amp-twitter data-tweetid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-twitter>',
esc_attr( $tweet_id ),
absint( $width ),
absint( $height )
);
} else {
// Add Twitter widgets.js script to the footer.
add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
}

return $output;
}

Expand Down
70 changes: 70 additions & 0 deletions tests/php/modules/shortcodes/test-class.tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,74 @@ public function test_shortcodes_tweet_card_via_oembed_http_request() {
$this->assertContains( '<blockquote class="twitter-tweet"', $shortcode_content );
// Not testing here for actual URL because wp_oembed_get might return a shortened Twitter URL with t.co domain
}

/**
* Gets the test data for test_shortcodes_tweet_amp().
*
* @return array The test data.
*/
public function get_tweet_amp_data() {
$tweet_id = 95234262;
$default_height = 480;
$default_width = 600;

return array(
'no_attributes' => array(
'[tweet]',
'<!-- Invalid tweet id -->',
),
'id_in_attributes' => array(
'[tweet ' . $tweet_id . ']',
'<amp-twitter data-tweetid="'. $tweet_id .'" layout="responsive" width="' . $default_width . '" height="' . $default_height .'"></amp-twitter>',
),
'width_in_attributes' => array(
'[tweet ' . $tweet_id . ' width=300]',
'<amp-twitter data-tweetid="'. $tweet_id .'" layout="responsive" width="300" height="' . $default_height .'"></amp-twitter>',
),
'0_width_in_attributes' => array(
'[tweet ' . $tweet_id . ' width=0]',
'<amp-twitter data-tweetid="'. $tweet_id .'" layout="responsive" width="' . $default_width . '" height="' . $default_height .'"></amp-twitter>',
),
'id_as_part_of_url' => array(
'[tweet https://twitter.com/jetpack/status/' . $tweet_id . ']',
'<amp-twitter data-tweetid="'. $tweet_id .'" layout="responsive" width="' . $default_width . '" height="' . $default_height .'"></amp-twitter>',
),
'id_in_tweet_attribute' => array(
'[tweet tweet=' . $tweet_id . ']',
'<amp-twitter data-tweetid="'. $tweet_id .'" layout="responsive" width="' . $default_width . '" height="' . $default_height .'"></amp-twitter>',
),
);
}

/**
* Test the AMP-compatible [tweet] shortcode on an AMP endpoint.
*
* @dataProvider get_tweet_amp_data
* @since 8.0.0
*
* @param string $shortcode_content The shortcode, like [tweet 1234].
* @param string $expected The expected return value of the function.
*/
public function test_shortcodes_tweet_amp( $shortcode_content, $expected ) {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
self::markTestSkipped( 'WordPress.com does not run the latest version of the AMP plugin yet.' );
return;
}

add_filter( 'jetpack_is_amp_request', '__return_true' );
$this->assertEquals( $expected, do_shortcode( $shortcode_content ) );
}

/**
* Test that the AMP [tweet] shortcode logic doesn't run on a non-AMP endpoint.
*
* @dataProvider get_tweet_amp_data
* @since 8.0.0
*
* @param string $shortcode_content The shortcode as entered in the editor.
*/
public function test_shortcodes_tweet_non_amp( $shortcode_content ) {
add_filter( 'jetpack_is_amp_request', '__return_false' );
$this->assertNotContains( 'amp-twitter', do_shortcode( $shortcode_content ) );
}
}

0 comments on commit 2479f65

Please sign in to comment.