Skip to content

Commit

Permalink
Issue #864: Change logic for adding 'data-amp-layout'.
Browse files Browse the repository at this point in the history
Per feedback, this should apply to all contexts,
As long as they allow <img> with a width and height.
Uses Weston's snippet in the filter callback.
@todo: move it to AMP_Theme_Support.
  • Loading branch information
Ryan Kienstra committed Mar 1, 2018
1 parent 037f6ef commit a233b59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
11 changes: 2 additions & 9 deletions includes/utils/class-amp-wp-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,9 @@ protected static function _wp_translate_php_url_constant_to_key( $constant ) {
* @return array $context Filtered allowed tags and attributes.
*/
public static function add_layout( $context, $context_type ) {
if ( 'post' !== $context_type ) {
return $context;
if ( ! empty( $context['img']['width'] ) && ! empty( $context['img']['height'] ) ) {
$context['img']['data-amp-layout'] = true;
}
$img = isset( $context['img'] ) ? $context['img'] : array();
$context['img'] = array_merge(
$img,
array(
'data-amp-layout' => true,
)
);
return $context;
}

Expand Down
27 changes: 20 additions & 7 deletions tests/test-amp-wp-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,28 @@ function test__method( $url, $expected, $component ) {
* @see AMP_WP_Utils::add_layout()
*/
public function test_add_layout() {
$this->assertEquals( array(), AMP_WP_Utils::add_layout( array(), 'explicit' ) );
$this->assertEquals(
array(
'img' => array(
'data-amp-layout' => true,
),
$attribute = 'data-amp-layout';
$image_no_dimensions = array(
'img' => array(
$attribute => true,
),
AMP_WP_Utils::add_layout( array(), 'post' )
);
$image_with_dimensions = array_merge(
$image_no_dimensions,
array(
'height' => '100',
'width' => '100',
)
);

$this->assertEquals( array(), AMP_WP_Utils::add_layout( array(), 'explicit' ) );
$this->assertEquals( $image_no_dimensions, AMP_WP_Utils::add_layout( $image_no_dimensions, 'post' ) );

$context = AMP_WP_Utils::add_layout( $image_with_dimensions, 'post' );
$this->assertTrue( $context['img'][ $attribute ] );

$context = AMP_WP_Utils::add_layout( $image_with_dimensions, 'explicit' );
$this->assertTrue( $context['img'][ $attribute ] );

add_filter( 'wp_kses_allowed_html', 'AMP_WP_Utils::add_layout', 10, 2 );
$image = '<img data-amp-layout="fill">';
Expand Down

0 comments on commit a233b59

Please sign in to comment.