From e98cb09c1ff3d2c22f59bc58ea9cb9e4202ce8e9 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Mon, 29 Jan 2018 18:25:01 -0600 Subject: [PATCH] Issue #864: Support in 'Gallery' widget. There's an existing handler to create 'amp-carousel' elements: class AMP_Gallery_Embed_Handler. So override the 'Gallery' widget class. And use that in render_media(). Otherwise, that function is copied from the parent. The parent calls gallery_shortcode() at the end of render_media(). And that function doesn't have a filter for the markup. --- includes/class-amp-autoloader.php | 1 + .../class-amp-widget-media-gallery.php | 51 +++++++++++++ tests/test-class-amp-widget-media-gallery.php | 74 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 includes/widgets/class-amp-widget-media-gallery.php create mode 100644 tests/test-class-amp-widget-media-gallery.php diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index b0904dd69e8..ee5e0bd9a82 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -86,6 +86,7 @@ class AMP_Autoloader { 'AMP_WP_Utils' => 'includes/utils/class-amp-wp-utils', '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_Recent_Comments' => 'includes/widgets/class-amp-widget-recent-comments', 'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed', 'AMP_Test_Stub_Sanitizer' => 'tests/stubs', diff --git a/includes/widgets/class-amp-widget-media-gallery.php b/includes/widgets/class-amp-widget-media-gallery.php new file mode 100644 index 00000000000..9bd645d0962 --- /dev/null +++ b/includes/widgets/class-amp-widget-media-gallery.php @@ -0,0 +1,51 @@ +get_instance_schema(), 'default' ), $instance ); + $shortcode_atts = array_merge( + $instance, + array( + 'link' => $instance['link_type'], + ) + ); + + if ( isset( $instance['orderby_random'] ) && ( true === $instance['orderby_random'] ) ) { + $shortcode_atts['orderby'] = 'rand'; + } + + $handler = new AMP_Gallery_Embed_Handler(); + echo $handler->shortcode( $shortcode_atts ); // WPCS: XSS ok. + } + +} diff --git a/tests/test-class-amp-widget-media-gallery.php b/tests/test-class-amp-widget-media-gallery.php new file mode 100644 index 00000000000..5f374b19f38 --- /dev/null +++ b/tests/test-class-amp-widget-media-gallery.php @@ -0,0 +1,74 @@ +instance = new AMP_Widget_Media_Gallery(); + } + + /** + * Test render_media(). + * + * @see AMP_Widget_Media_Gallery::widget(). + */ + public function test_render_media() { + $first_test_image = '/tmp/test-image.jpg'; + copy( DIR_TESTDATA . '/images/test-image.jpg', $first_test_image ); + $first_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( $first_attachment_id, wp_generate_attachment_metadata( $first_attachment_id, $first_test_image ) ); + $ids[] = $first_attachment_id; + + $second_test_image = '/tmp/test-image.jpg'; + copy( DIR_TESTDATA . '/images/test-image.jpg', $second_test_image ); + $second_attachment_id = self::factory()->attachment->create_object( array( + 'file' => $second_test_image, + 'post_parent' => 0, + 'post_mime_type' => 'image/jpeg', + 'post_title' => 'Test Image', + ) ); + wp_update_attachment_metadata( $second_attachment_id, wp_generate_attachment_metadata( $second_attachment_id, $second_test_image ) ); + $ids[] = $second_attachment_id; + $instance = array( + 'title' => 'Test Gallery Widget', + 'ids' => $ids, + ); + + ob_start(); + $this->instance->render_media( $instance ); + $output = ob_get_clean(); + + $this->assertContains( 'amp-carousel', $output ); + $this->assertContains( $first_test_image, $output ); + $this->assertContains( $second_test_image, $output ); + } + +}