diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 9815c7d08e6..e5b2246b034 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -81,6 +81,7 @@ class AMP_Autoloader { 'AMP_Widget_Archives' => 'includes/widgets/class-amp-widget-archives', 'AMP_Widget_Categories' => 'includes/widgets/class-amp-widget-categories', 'AMP_Widget_Media_Gallery' => 'includes/widgets/class-amp-widget-media-gallery', + 'AMP_Widget_Media_Image' => 'includes/widgets/class-amp-widget-media-image', 'AMP_Widget_Recent_Comments' => 'includes/widgets/class-amp-widget-recent-comments', 'AMP_Widgets' => 'includes/widgets/class-amp-widgets', 'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed', diff --git a/includes/widgets/class-amp-widget-media-image.php b/includes/widgets/class-amp-widget-media-image.php new file mode 100644 index 00000000000..1df6f1ddced --- /dev/null +++ b/includes/widgets/class-amp-widget-media-image.php @@ -0,0 +1,30 @@ + to and remove the 'style' attribute. + * @see https://github.com/Automattic/amp-wp/issues/864 + * @param array $instance Data for widget. + * @return void. + */ + public function render_media( $instance ) { + ob_start(); + parent::render_media( $instance ); + $output = ob_get_clean(); + echo $output; // WPCS: XSS ok. + } + +} diff --git a/includes/widgets/class-amp-widgets.php b/includes/widgets/class-amp-widgets.php index 9cfed9e94c0..a263f784378 100644 --- a/includes/widgets/class-amp-widgets.php +++ b/includes/widgets/class-amp-widgets.php @@ -42,6 +42,7 @@ public function get_widgets() { 'WP_Widget_Archives' => 'AMP_Widget_Archives', 'WP_Widget_Categories' => 'AMP_Widget_Categories', 'WP_Widget_Media_Gallery' => 'AMP_Widget_Media_Gallery', + 'WP_Widget_Media_Image' => 'AMP_Widget_Media_Image', 'WP_Widget_Recent_Comments' => 'AMP_Widget_Recent_Comments', ); } diff --git a/tests/test-class-amp-widget-media-image.php b/tests/test-class-amp-widget-media-image.php new file mode 100644 index 00000000000..39de127ddaa --- /dev/null +++ b/tests/test-class-amp-widget-media-image.php @@ -0,0 +1,83 @@ +register_widgets(); + $this->instance = new AMP_Widget_Media_Image(); + } + + /** + * Test construct(). + * + * @see AMP_Widget_Media_Image::__construct(). + */ + public function test_construct() { + global $wp_widget_factory; + $amp_widget = $wp_widget_factory->widgets['AMP_Widget_Media_Image']; + + $this->assertEquals( 'media_image', $amp_widget->id_base ); + $this->assertEquals( 'Image', $amp_widget->name ); + $this->assertEquals( 'widget_media_image', $amp_widget->widget_options['classname'] ); + $this->assertEquals( true, $amp_widget->widget_options['customize_selective_refresh'] ); + $this->assertEquals( 'Displays an image.', $amp_widget->widget_options['description'] ); + } + + /** + * Test widget(). + * + * Mock image logic mainly copied from Test_WP_Widget_Media_image::test_render_media(). + * + * @see AMP_Widget_Media_Image::widget(). + */ + public function test_render_media() { + $first_test_image = '/tmp/test-image.jpg'; + copy( DIR_TESTDATA . '/images/test-image.jpg', $first_test_image ); + $attachment_id = self::factory()->attachment->create_object( array( + 'file' => $first_test_image, + 'post_parent' => 0, + 'post_mime_type' => 'image/jpeg', + 'post_title' => 'Test Image', + ) ); + wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $first_test_image ) ); + $instance = array( + 'title' => 'Test Image Widget', + 'attachment_id' => $attachment_id, + 'height' => 100, + 'width' => 100, + 'url' => 'https://example.com/amp', + ); + + ob_start(); + $this->instance->render_media( $instance ); + $output = ob_get_clean(); + + $this->assertFalse( strpos( $output, 'assertFalse( strpos( $output, 'style=' ) ); + } + +}