1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
<?php /** * Class Carousel. * * @package AmpProject\AmpWP */
namespace AmpProject\AmpWP\Component;
use AmpProject\Attribute; use AmpProject\Dom\Document; use AmpProject\AmpWP\Dom\ElementList; use AmpProject\Tag; use DOMElement; use AMP_DOM_Utils;
/** * Class Carousel * * Gets the markup for an <amp-carousel>. * * @internal * @since 1.5.0 */ final class Carousel {
/** * Value used for width of amp-carousel. * * @var int */ const FALLBACK_WIDTH = 600;
/** * Value used for height of amp-carousel. * * @var int */ const FALLBACK_HEIGHT = 480;
/** * An object representation of the DOM. * * @var Document */ private $dom;
/** * The slides to add to the carousel, possibly images. * * @var ElementList */ private $slides;
/** * Instantiates the class. * * @param Document $dom The dom to use to create a carousel. * @param ElementList $slides The slides from which to create a carousel. */ public function __construct( Document $dom, ElementList $slides ) { $this->dom = $dom; $this->slides = $slides; }
/** * Gets the carousel element. * * @return DOMElement An <amp-carousel> with the slides. */ public function get_dom_element() { list( $width, $height ) = $this->get_dimensions(); $amp_carousel = AMP_DOM_Utils::create_node( $this->dom, 'amp-carousel', [ 'width' => $width, 'height' => $height, 'type' => 'slides', 'layout' => 'responsive', ] );
foreach ( $this->slides as $slide ) { $slide_node = $slide instanceof CaptionedSlide ? $slide->get_slide_element() : $slide; $caption_element = $slide instanceof HasCaption ? $slide->get_caption_element() : null; $slide_container = AMP_DOM_Utils::create_node( $this->dom, Tag::FIGURE, // This cannot be a <div> because if the gallery is inside of a <p>, then the DOM will break. [ 'class' => 'slide' ] );
// Ensure an image fills the entire <amp-carousel>, so the possible caption looks right. if ( $this->is_image_element( $slide_node ) ) { $slide_node->setAttribute( 'layout', 'fill' ); $slide_node->setAttribute( 'object-fit', 'cover' ); } elseif ( $slide_node->firstChild instanceof DOMElement && $this->is_image_element( $slide_node->firstChild ) ) { // If the <amp-img> is wrapped in an <a>. $slide_node->firstChild->setAttribute( 'layout', 'fill' ); $slide_node->firstChild->setAttribute( 'object-fit', 'cover' ); }
$slide_container->appendChild( $slide_node );
// If there's a caption, append it to the slide. if ( null !== $caption_element ) { // If the caption is not a <figcaption>, wrap it in one. if ( Tag::FIGCAPTION !== $caption_element->nodeName ) { $caption_content = $caption_element; $caption_element = AMP_DOM_Utils::create_node( $this->dom, Tag::FIGCAPTION, [] ); $caption_element->appendChild( $caption_content ); }
$has_caption_class = AMP_DOM_Utils::has_class( $caption_element, 'amp-wp-gallery-caption' );
/** @var DOMElement $caption_element */ if ( ! $has_caption_class ) { $caption_element->setAttribute( Attribute::CLASS_, 'amp-wp-gallery-caption' ); }
$slide_container->appendChild( $caption_element ); }
$amp_carousel->appendChild( $slide_container ); }
return $amp_carousel; }
/** * Gets the carousel's width and height, based on its elements. * * This will return the width and height of the slide (possibly image) with the widest aspect ratio, * not necessarily that with the biggest absolute width. * * @return array { * The carousel dimensions. * * @type int $width The width of the carousel, at index 0. * @type int $height The height of the carousel, at index 1. * } */ private function get_dimensions() { if ( 0 === count( $this->slides ) ) { return [ self::FALLBACK_WIDTH, self::FALLBACK_HEIGHT ]; }
$max_aspect_ratio = 0; $carousel_width = 0; $carousel_height = 0;
foreach ( $this->slides as $slide ) { $slide_node = $slide instanceof CaptionedSlide ? $slide->get_slide_element() : $slide; // Account for an <amp-img> that's wrapped in an <a>. if ( ! $this->is_image_element( $slide_node ) && $slide_node->firstChild instanceof DOMElement && $this->is_image_element( $slide_node->firstChild ) ) { $slide_node = $slide_node->firstChild; }
if ( ! is_numeric( $slide_node->getAttribute( 'width' ) ) || ! is_numeric( $slide_node->getAttribute( 'height' ) ) ) { continue; }
$width = (float) $slide_node->getAttribute( 'width' ); $height = (float) $slide_node->getAttribute( 'height' );
if ( empty( $width ) || empty( $height ) ) { continue; }
$this_aspect_ratio = $width / $height; if ( $this_aspect_ratio > $max_aspect_ratio ) { $max_aspect_ratio = $this_aspect_ratio; $carousel_width = $width; $carousel_height = $height; } }
if ( empty( $carousel_width ) && empty( $carousel_height ) ) { return [ self::FALLBACK_WIDTH, self::FALLBACK_HEIGHT ]; }
return [ $carousel_width, $carousel_height ]; }
/** * Determine whether an element is an image (either an <amp-img> or an <img>). * * @param DOMElement $element Element. * @return bool If it is an image. */ private function is_image_element( DOMElement $element ) { return 'amp-img' === $element->tagName || 'img' === $element->tagName; } }
|