Skip to content

Commit

Permalink
Issue #864: Support <amp-carousel> in 'Gallery' widget.
Browse files Browse the repository at this point in the history
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.
It calls gallery_shortcode() at the end.
Which doesn't have a filter for the markup.
  • Loading branch information
Ryan Kienstra committed Jan 30, 2018
1 parent e209005 commit aed76c6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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',
Expand Down
51 changes: 51 additions & 0 deletions includes/widgets/class-amp-widget-media-gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Class AMP_Widget_Media_Gallery
*
* @since 0.7.0
* @package AMP
*/

/**
* Class AMP_Widget_Media_Gallery
*
* @since 0.7.0
* @package AMP
*/
class AMP_Widget_Media_Gallery extends WP_Widget_Media_Gallery {

/**
* Renders the markup of the widget.
*
* Mainly copied from WP_Widget_Media_Gallery::render_media().
* But instead of calling shortcode_gallery(),
* It uses this plugin's embed handler for galleries.
*
* @since 0.7.0
*
* @param array $instance Data for widget.
* @return void
*/
public function render_media( $instance ) {
if ( ! is_amp_endpoint() ) {
parent::render_media( $instance );
return;
}

$instance = array_merge( wp_list_pluck( $this->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.
}

}
74 changes: 74 additions & 0 deletions tests/test-class-amp-widget-media-gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Tests for class AMP_Widget_Media_Gallery.
*
* @package AMP
*/

/**
* Tests for class AMP_Widget_Media_Gallery.
*
* @package AMP
*/
class Test_AMP_Widget_Media_Gallery extends WP_UnitTestCase {

/**
* Instance of the widget.
*
* @var object
*/
public $instance;

/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::register_widgets();
$this->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 );
}

}

0 comments on commit aed76c6

Please sign in to comment.