-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #864: Support <amp-carousel> 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.
- Loading branch information
Ryan Kienstra
committed
Jan 30, 2018
1 parent
162fa9e
commit e98cb09
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |