From 011e47838e5154bb77af4f36010ff56d8bedd9eb Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Thu, 1 Mar 2018 15:56:43 -0600 Subject: [PATCH] Issue #864: Move add_layout() to AMP_Theme_Support. Also, remove $context_type parameter. It's no longer used. Otherwise, there's no change to the function. --- includes/class-amp-theme-support.php | 18 ++++++++++++- includes/utils/class-amp-wp-utils.php | 17 ------------- tests/test-amp-wp-utils.php | 35 -------------------------- tests/test-class-amp-theme-support.php | 35 ++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 53 deletions(-) diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 22eb7f85579..71237df7aa3 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -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. } @@ -834,4 +834,20 @@ public static function prepare_response( $response, $args = array() ) { return $response; } + + /** + * Adds 'data-amp-layout' to the allowed 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; + } + } diff --git a/includes/utils/class-amp-wp-utils.php b/includes/utils/class-amp-wp-utils.php index 737e79a3c94..bacb72458af 100644 --- a/includes/utils/class-amp-wp-utils.php +++ b/includes/utils/class-amp-wp-utils.php @@ -58,21 +58,4 @@ protected static function _wp_translate_php_url_constant_to_key( $constant ) { return false; } - - /** - * Adds 'data-amp-layout' to the allowed 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; - } - } diff --git a/tests/test-amp-wp-utils.php b/tests/test-amp-wp-utils.php index 685915022c0..43f3b3a572e 100644 --- a/tests/test-amp-wp-utils.php +++ b/tests/test-amp-wp-utils.php @@ -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 = ''; - $this->assertEquals( $image, wp_kses_post( $image ) ); - } - } diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php index b71c4d41228..a339bd76d89 100644 --- a/tests/test-class-amp-theme-support.php +++ b/tests/test-class-amp-theme-support.php @@ -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 = ''; + $this->assertEquals( $image, wp_kses_post( $image ) ); + } + }