Skip to content

Commit

Permalink
Update the AMP_Twitter_Embed_Handler class to support all of the cust…
Browse files Browse the repository at this point in the history
…om data attributes supported by the amp-twitter tag

Cherry-picks #3051
  • Loading branch information
benbowler authored and westonruter committed Aug 27, 2019
1 parent a7878d1 commit 32d2d70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
20 changes: 14 additions & 6 deletions includes/embeds/class-amp-twitter-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,23 @@ private function create_amp_twitter_and_replace_node( $dom, $node ) {
return;
}

$attributes = [
'width' => $this->DEFAULT_WIDTH,
'height' => $this->DEFAULT_HEIGHT,
'layout' => 'responsive',
'data-tweetid' => $tweet_id,
];

if ( $node->hasAttributes() ) {
foreach ( $node->attributes as $attr ) {
$attributes[ $attr->nodeName ] = $attr->nodeValue;
}
}

$new_node = AMP_DOM_Utils::create_node(
$dom,
$this->amp_tag,
[
'width' => $this->DEFAULT_WIDTH,
'height' => $this->DEFAULT_HEIGHT,
'layout' => 'responsive',
'data-tweetid' => $tweet_id,
]
$attributes
);

$this->sanitize_embed_script( $node );
Expand Down
22 changes: 16 additions & 6 deletions tests/php/test-amp-twitter-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,33 @@ public function test__get_scripts( $source, $expected ) {
*/
public function get_raw_embed_dataset() {
return [
'no_embed' => [
'no_embed' => [
'<p>Hello world.</p>',
'<p>Hello world.</p>',
],
'embed_blockquote_without_twitter' => [
'embed_blockquote_without_twitter' => [
'<blockquote>lorem ipsum</blockquote>',
'<blockquote>lorem ipsum</blockquote>',
],

'blockquote_embed' => [
'blockquote_embed' => [
wpautop( '<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Celebrate the WordPress 15th Anniversary on May 27 <a href="https://t.co/jv62WkI9lr">https://t.co/jv62WkI9lr</a> <a href="https://t.co/4ZECodSK78">pic.twitter.com/4ZECodSK78</a></p>&mdash; WordPress (@WordPress) <a href="https://twitter.com/WordPress/status/987437752164737025?ref_src=twsrc%5Etfw">April 20, 2018</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-twitter width="600" height="480" layout="responsive" data-tweetid="987437752164737025"></amp-twitter>' . "\n\n",
'<amp-twitter width="600" height="480" layout="responsive" data-tweetid="987437752164737025" class="twitter-tweet" data-lang="en"></amp-twitter>' . "\n\n",
],

'blockquote_embed_not_autop' => [
'blockquote_embed_with_data_conversation' => [
wpautop( '<blockquote class="twitter-tweet" data-conversation="none"><p lang="en" dir="ltr">Celebrate the WordPress 15th Anniversary on May 27 <a href="https://t.co/jv62WkI9lr">https://t.co/jv62WkI9lr</a> <a href="https://t.co/4ZECodSK78">pic.twitter.com/4ZECodSK78</a></p>&mdash; WordPress (@WordPress) <a href="https://twitter.com/WordPress/status/987437752164737025?ref_src=twsrc%5Etfw">April 20, 2018</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-twitter width="600" height="480" layout="responsive" data-tweetid="987437752164737025" class="twitter-tweet" data-conversation="none"></amp-twitter>' . "\n\n",
],

'blockquote_embed_with_data_theme' => [
wpautop( '<blockquote class="twitter-tweet" data-theme="en"><p lang="en" dir="ltr">Celebrate the WordPress 15th Anniversary on May 27 <a href="https://t.co/jv62WkI9lr">https://t.co/jv62WkI9lr</a> <a href="https://t.co/4ZECodSK78">pic.twitter.com/4ZECodSK78</a></p>&mdash; WordPress (@WordPress) <a href="https://twitter.com/WordPress/status/987437752164737025?ref_src=twsrc%5Etfw">April 20, 2018</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-twitter width="600" height="480" layout="responsive" data-tweetid="987437752164737025" class="twitter-tweet" data-theme="en"></amp-twitter>' . "\n\n",
],

'blockquote_embed_not_autop' => [
'<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Celebrate the WordPress 15th Anniversary on May 27 <a href="https://t.co/jv62WkI9lr">https://t.co/jv62WkI9lr</a> <a href="https://t.co/4ZECodSK78">pic.twitter.com/4ZECodSK78</a></p>&mdash; WordPress (@WordPress) <a href="https://twitter.com/WordPress/status/987437752164737025?ref_src=twsrc%5Etfw">April 20, 2018</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-twitter width="600" height="480" layout="responsive" data-tweetid="987437752164737025"></amp-twitter> ',
'<amp-twitter width="600" height="480" layout="responsive" data-tweetid="987437752164737025" class="twitter-tweet" data-lang="en"></amp-twitter> ',
],
];
}
Expand Down

0 comments on commit 32d2d70

Please sign in to comment.