Skip to content

Commit

Permalink
Issue #864: Move add_layout() to AMP_Theme_Support.
Browse files Browse the repository at this point in the history
Also, remove $context_type parameter.
It's no longer used.
Otherwise, there's no change to the function.
  • Loading branch information
Ryan Kienstra committed Mar 1, 2018
1 parent a233b59 commit 011e478
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 53 deletions.
18 changes: 17 additions & 1 deletion includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static function register_hooks() {
add_filter( 'comment_reply_link', array( __CLASS__, 'filter_comment_reply_link' ), 10, 4 );
add_filter( 'cancel_comment_reply_link', array( __CLASS__, 'filter_cancel_comment_reply_link' ), 10, 3 );
add_action( 'comment_form', array( __CLASS__, 'add_amp_comment_form_templates' ), 100 );
add_filter( 'wp_kses_allowed_html', 'AMP_WP_Utils::add_layout', 10, 2 );
add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'add_layout' ), 10 );

// @todo Add character conversion.
}
Expand Down Expand Up @@ -834,4 +834,20 @@ public static function prepare_response( $response, $args = array() ) {

return $response;
}

/**
* Adds 'data-amp-layout' to the allowed <img> attributes for wp_kses().
*
* @since 0.7
*
* @param array $context Allowed tags and their allowed attributes.
* @return array $context Filtered allowed tags and attributes.
*/
public static function add_layout( $context ) {
if ( ! empty( $context['img']['width'] ) && ! empty( $context['img']['height'] ) ) {
$context['img']['data-amp-layout'] = true;
}
return $context;
}

}
17 changes: 0 additions & 17 deletions includes/utils/class-amp-wp-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,4 @@ protected static function _wp_translate_php_url_constant_to_key( $constant ) {

return false;
}

/**
* Adds 'data-amp-layout' to the allowed <img> attributes for wp_kses() in a 'post' context.
*
* @since 0.7
*
* @param array $context Allowed tags and their allowed attributes.
* @param string $context_type Type of context.
* @return array $context Filtered allowed tags and attributes.
*/
public static function add_layout( $context, $context_type ) {
if ( ! empty( $context['img']['width'] ) && ! empty( $context['img']['height'] ) ) {
$context['img']['data-amp-layout'] = true;
}
return $context;
}

}
35 changes: 0 additions & 35 deletions tests/test-amp-wp-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,4 @@ function test__method( $url, $expected, $component ) {

$this->assertEquals( $expected, $actual );
}

/**
* Test AMP_WP_Utils::add_layout().
*
* @see AMP_WP_Utils::add_layout()
*/
public function test_add_layout() {
$attribute = 'data-amp-layout';
$image_no_dimensions = array(
'img' => array(
$attribute => true,
),
);
$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">';
$this->assertEquals( $image, wp_kses_post( $image ) );
}

}
35 changes: 35 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,39 @@ public function test_get_amp_scripts() {
$scripts
);
}

/**
* Test AMP_Theme_Support::add_layout().
*
* @see AMP_Theme_Support::add_layout()
*/
public function test_add_layout() {
$attribute = 'data-amp-layout';
$image_no_dimensions = array(
'img' => array(
$attribute => true,
),
);
$image_with_dimensions = array_merge(
$image_no_dimensions,
array(
'height' => '100',
'width' => '100',
)
);

$this->assertEquals( array(), AMP_Theme_Support::add_layout( array() ) );
$this->assertEquals( $image_no_dimensions, AMP_Theme_Support::add_layout( $image_no_dimensions ) );

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

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

add_filter( 'wp_kses_allowed_html', 'AMP_Theme_Support::add_layout', 10, 2 );
$image = '<img data-amp-layout="fill">';
$this->assertEquals( $image, wp_kses_post( $image ) );
}

}

0 comments on commit 011e478

Please sign in to comment.