diff --git a/includes/embeds/class-amp-twitter-embed-handler.php b/includes/embeds/class-amp-twitter-embed-handler.php index c6af54996b0..e71dece6d8d 100644 --- a/includes/embeds/class-amp-twitter-embed-handler.php +++ b/includes/embeds/class-amp-twitter-embed-handler.php @@ -16,6 +16,22 @@ */ class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler { + /** + * Default width. + * + * @var int|string + */ + protected $DEFAULT_WIDTH = 'auto'; + + /** + * Default height. + * + * This is the minimum height for a tweet, with just a single line of text. + * + * @var int + */ + protected $DEFAULT_HEIGHT = 197; + /** * URL pattern for a Tweet URL. * @@ -97,9 +113,13 @@ public function oembed_timeline( $matches ) { } } - $attributes['layout'] = 'responsive'; $attributes['width'] = $this->args['width']; $attributes['height'] = $this->args['height']; + if ( empty( $attributes['width'] ) || 'auto' === $attributes['width'] ) { + $attributes['layout'] = 'fixed-height'; + } else { + $attributes['layout'] = 'responsive'; + } $this->did_convert_elements = true; @@ -161,9 +181,9 @@ private function create_amp_twitter_and_replace_node( Document $dom, DOMElement } $attributes = [ - 'width' => $this->DEFAULT_WIDTH, + 'width' => 'auto', 'height' => $this->DEFAULT_HEIGHT, - 'layout' => 'responsive', + 'layout' => 'fixed-height', 'data-tweetid' => $tweet_id, ]; diff --git a/tests/php/test-amp-twitter-embed-handler.php b/tests/php/test-amp-twitter-embed-handler.php index 3300ba184e3..3bbfce65cd8 100644 --- a/tests/php/test-amp-twitter-embed-handler.php +++ b/tests/php/test-amp-twitter-embed-handler.php @@ -103,27 +103,27 @@ public function get_conversion_data() { ], 'url_simple' => [ 'https://twitter.com/wordpress/status/987437752164737025' . PHP_EOL, - "$overflow_button
\n

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

\n

— WordPress (@WordPress) April 20, 2018

\n\n", + "$overflow_button
\n

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

\n

— WordPress (@WordPress) April 20, 2018

\n\n", ], 'url_with_big_tweet_id' => [ 'https://twitter.com/wordpress/status/705219971425574912' . PHP_EOL, - "$overflow_button
\n

On our way to the #GoogleDance! #SMX 💃🏻 pic.twitter.com/N8kZ9M3eN4

\n

— Search Engine Land (@sengineland) March 3, 2016

\n\n", + "$overflow_button
\n

On our way to the #GoogleDance! #SMX 💃🏻 pic.twitter.com/N8kZ9M3eN4

\n

— Search Engine Land (@sengineland) March 3, 2016

\n\n", ], 'timeline_url_with_profile' => [ 'https://twitter.com/wordpress' . PHP_EOL, - "

$overflow_button

\n", + "

$overflow_button

\n", ], 'timeline_url_with_likes' => [ 'https://twitter.com/wordpress/likes' . PHP_EOL, - "

$overflow_button

\n", + "

$overflow_button

\n", ], 'timeline_url_with_list' => [ 'https://twitter.com/wordpress/lists/random_list' . PHP_EOL, - "

$overflow_button

\n", + "

$overflow_button

\n", ], 'timeline_url_with_list2' => [ 'https://twitter.com/robertnyman/lists/web-gdes' . PHP_EOL, - "

$overflow_button

\n", + "

$overflow_button

\n", ], ]; } @@ -208,22 +208,22 @@ public function get_raw_embed_dataset() { 'blockquote_embed' => [ wpautop( '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

-- WordPress (@WordPress) April 20, 2018
' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine - '' . $overflow_button . '
' . "\n" . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

' . "\n" . '

-- WordPress (@WordPress) April 20, 2018

' . "\n\n", + '' . $overflow_button . '
' . "\n" . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

' . "\n" . '

-- WordPress (@WordPress) April 20, 2018

' . "\n\n", ], 'blockquote_embed_with_data_conversation' => [ wpautop( '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

— WordPress (@WordPress) April 20, 2018
' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine - '' . $overflow_button . '
' . "\n" . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

' . "\n" . '

— WordPress (@WordPress) April 20, 2018

' . "\n\n", + '' . $overflow_button . '
' . "\n" . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

' . "\n" . '

— WordPress (@WordPress) April 20, 2018

' . "\n\n", ], 'blockquote_embed_with_data_theme' => [ wpautop( '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

— WordPress (@WordPress) April 20, 2018
' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine - '' . $overflow_button . '
' . "\n" . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

' . "\n" . '

— WordPress (@WordPress) April 20, 2018

' . "\n\n", + '' . $overflow_button . '
' . "\n" . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

' . "\n" . '

— WordPress (@WordPress) April 20, 2018

' . "\n\n", ], 'blockquote_embed_not_autop' => [ '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

-- WordPress (@WordPress) April 20, 2018
', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine - '' . $overflow_button . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

-- WordPress (@WordPress) April 20, 2018
', + '' . $overflow_button . '

Celebrate the WordPress 15th Anniversary on May 27 https://t.co/jv62WkI9lr pic.twitter.com/4ZECodSK78

-- WordPress (@WordPress) April 20, 2018
', ], ]; }