-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Gutenberg] Add AMP Carousel for Gallery and AMP Lightbox features for Gallery and Image #1121
Changes from 4 commits
52943ed
fd662d7
54f9555
ed1f9de
0263878
40486fe
96bd515
553c4da
22060ec
5b63476
2e102ea
c3aad13
dd924ec
ca133a4
22597fa
b70af66
438567d
7ee5fd3
58430e4
55e8fff
f658c43
6900e2b
a69dd55
ff2cdd2
d5169ef
13267f6
134335e
26c491e
46c3a50
201654f
f9f3a05
cc53173
dbe281c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Class AMP_Gallery_Block_Sanitizer. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Gallery_Block_Sanitizer | ||
* | ||
* Modifies gallery block to match the block's AMP-specific configuration. | ||
*/ | ||
class AMP_Gallery_Block_Sanitizer extends AMP_Base_Sanitizer { | ||
|
||
/** | ||
* Value used for width of amp-carousel. | ||
* | ||
* @since 1.0 | ||
* | ||
* @const int | ||
*/ | ||
const FALLBACK_WIDTH = 600; | ||
|
||
/** | ||
* Value used for height of amp-carousel. | ||
* | ||
* @since 1.0 | ||
* | ||
* @const int | ||
*/ | ||
const FALLBACK_HEIGHT = 480; | ||
|
||
/** | ||
* Tag. | ||
* | ||
* @var string Ul tag to identify wrapper around gallery block. | ||
* @since 1.0 | ||
*/ | ||
public static $tag = 'ul'; | ||
|
||
/** | ||
* Sanitize the gallery block contained by <ul> element where necessary. | ||
* | ||
* @since 0.2 | ||
*/ | ||
public function sanitize() { | ||
$nodes = $this->dom->getElementsByTagName( self::$tag ); | ||
$num_nodes = $nodes->length; | ||
if ( 0 === $num_nodes ) { | ||
return; | ||
} | ||
|
||
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { | ||
$node = $nodes->item( $i ); | ||
|
||
// We're looking for <ul> elements that at least one child. | ||
if ( 0 === count( $node->childNodes ) ) { | ||
continue; | ||
} | ||
|
||
$attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); | ||
|
||
// We are only looking for <ul> elements which have amp-carousel as class. | ||
if ( ! isset( $attributes['class'] ) || false === strpos( $attributes['class'], 'amp-carousel' ) ) { | ||
continue; | ||
} | ||
|
||
$images = $node->getElementsByTagName( 'a' ); | ||
$num_images = $images->length; | ||
|
||
if ( 0 === $num_images ) { | ||
continue; | ||
} | ||
|
||
$attributes = array( | ||
'width' => self::FALLBACK_WIDTH, | ||
'height' => self::FALLBACK_HEIGHT, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what could be a better alternative for setting width and height. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking that this might also change the looks for default gallery in an unexpected way. |
||
'type' => 'slides', | ||
'layout' => 'responsive', | ||
); | ||
$amp_carousel = AMP_DOM_Utils::create_node( $this->dom, 'amp-carousel', $attributes ); | ||
|
||
for ( $j = $num_images - 1; $j >= 0; $j-- ) { | ||
$amp_carousel->appendChild( $images->item( $j ) ); | ||
} | ||
|
||
$node->parentNode->replaceChild( $amp_carousel, $node ); | ||
$this->did_convert_elements = true; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Class AMP_Gallery_Block_Sanitizer_Test. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Gallery_Block_Sanitizer_Test | ||
*/ | ||
class AMP_Gallery_Block_Sanitizer_Test extends WP_UnitTestCase { | ||
|
||
/** | ||
* Get data. | ||
* | ||
* @return array | ||
*/ | ||
public function get_data() { | ||
return array( | ||
'no_ul' => array( | ||
'<p>Lorem Ipsum Demet Delorit.</p>', | ||
'<p>Lorem Ipsum Demet Delorit.</p>', | ||
), | ||
|
||
'no_a' => array( | ||
'<ul class="amp-carousel"><amp-img></amp-img></ul>', | ||
'<ul class="amp-carousel"><amp-img></amp-img></ul>', | ||
), | ||
|
||
'no_amp_carousel' => array( | ||
'<ul><a><amp-img></amp-img></a></ul>', | ||
'<ul><a><amp-img></amp-img></a></ul>', | ||
), | ||
|
||
'data_amp_with_gallery' => array( | ||
'<ul class="amp-carousel"><li class="blocks-gallery-item"><figure><a href="http://example.com"><amp-img src="http://example.com/img.png" width="600" height="400"></amp-img></a></figure></li></ul>', | ||
'<amp-carousel width="600" height="480" type="slides" layout="responsive"><a href="http://example.com"><amp-img src="http://example.com/img.png" width="600" height="400"></amp-img></a></amp-carousel>', | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Test sanitizer. | ||
* | ||
* @dataProvider get_data | ||
* @param string $source Source. | ||
* @param string $expected Expected. | ||
*/ | ||
public function test_sanitizer( $source, $expected ) { | ||
$dom = AMP_DOM_Utils::get_dom_from_content( $source ); | ||
$sanitizer = new AMP_Gallery_Block_Sanitizer( $dom ); | ||
$sanitizer->sanitize(); | ||
$content = AMP_DOM_Utils::get_content_from_dom( $dom ); | ||
$content = preg_replace( '/(?<=>)\s+(?=<)/', '', $content ); | ||
$this->assertEquals( $expected, $content ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @miina,
In my browser, this didn't assign the new
props.className
value, causing the lightbox to not appear on the front-end.But with this change (borrowing from Weston's solution here), it did:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @kienstra, thanks for finding this, makes sense that the
props.className
can't be directly set this way.