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

Migrate the [tweet] shortcode AMP handling to Jetpack #14054

Merged
merged 5 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
11 changes: 11 additions & 0 deletions modules/shortcodes/tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public static function jetpack_tweet_shortcode( $atts ) {
}
}

if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
jeherve marked this conversation as resolved.
Show resolved Hide resolved
$width = ! empty( $attr['width'] ) ? $attr['width'] : 600;
$height = 480;
jeherve marked this conversation as resolved.
Show resolved Hide resolved
return 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 )
);
}

/*
* Fetch tweet.
*
Expand Down
65 changes: 65 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,69 @@ 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 ) {
add_filter( 'jetpack_is_amp_request', '__return_true' );
kienstra marked this conversation as resolved.
Show resolved Hide resolved
$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
jeherve marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 ) );
}
}