1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slideshow Block. |
4
|
|
|
* |
5
|
|
|
* @since 7.1.0 |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
jetpack_register_block( |
11
|
|
|
'jetpack/slideshow', |
12
|
|
|
array( |
13
|
|
|
'render_callback' => 'jetpack_slideshow_block_load_assets', |
14
|
|
|
) |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Slideshow block registration/dependency declaration. |
19
|
|
|
* |
20
|
|
|
* @param array $attr Array containing the slideshow block attributes. |
21
|
|
|
* @param string $content String containing the slideshow block content. |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
function jetpack_slideshow_block_load_assets( $attr, $content ) { |
26
|
|
|
Jetpack_Gutenberg::load_assets_as_required( 'slideshow' ); |
27
|
|
|
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
28
|
|
|
return jetpack_slideshow_block_render_amp( $attr ); |
29
|
|
|
} |
30
|
|
|
return $content; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Render slideshow block for AMP |
35
|
|
|
* |
36
|
|
|
* @param array $attr Array containing the slideshow block attributes. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
function jetpack_slideshow_block_render_amp( $attr ) { |
41
|
|
|
static $wp_block_jetpack_slideshow_id = 0; |
42
|
|
|
$wp_block_jetpack_slideshow_id++; |
43
|
|
|
|
44
|
|
|
$ids = empty( $attr['ids'] ) ? array() : $attr['ids']; |
45
|
|
|
$autoplay = empty( $attr['autoplay'] ) ? false : $attr['autoplay']; |
46
|
|
|
|
47
|
|
|
$extras = array( |
48
|
|
|
'wp-amp-block', |
49
|
|
|
$autoplay ? 'wp-block-jetpack-slideshow__autoplay' : null, |
50
|
|
|
$autoplay ? 'wp-block-jetpack-slideshow__autoplay-playing' : null, |
51
|
|
|
); |
52
|
|
|
$classes = Jetpack_Gutenberg::block_classes( 'slideshow', $attr, $extras ); |
53
|
|
|
|
54
|
|
|
return sprintf( |
55
|
|
|
'<div class="%1$s" id="wp-block-jetpack-slideshow__%2$d"><div class="wp-block-jetpack-slideshow_container swiper-container">%3$s%4$s%5$s</div></div>', |
56
|
|
|
esc_attr( $classes ), |
57
|
|
|
absint( $wp_block_jetpack_slideshow_id ), |
58
|
|
|
jetpack_slideshow_block_amp_carousel( $attr, $wp_block_jetpack_slideshow_id ), |
59
|
|
|
$autoplay ? jetpack_slideshow_block_autoplay_ui( $wp_block_jetpack_slideshow_id ) : '', |
60
|
|
|
jetpack_slideshow_block_bullets( $ids, $wp_block_jetpack_slideshow_id ) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generate amp-carousel markup |
66
|
|
|
* |
67
|
|
|
* @param array $attr Array of block attributes. |
68
|
|
|
* @param int $block_ordinal The ordinal number of the block, used in unique ID. |
69
|
|
|
* |
70
|
|
|
* @return string amp-carousel markup. |
71
|
|
|
*/ |
72
|
|
|
function jetpack_slideshow_block_amp_carousel( $attr, $block_ordinal ) { |
73
|
|
|
$ids = empty( $attr['ids'] ) ? array() : $attr['ids']; |
74
|
|
|
$first_image = wp_get_attachment_metadata( $ids[0] ); |
75
|
|
|
$delay = empty( $attr['delay'] ) ? 3 : absint( $attr['delay'] ); |
76
|
|
|
$autoplay = empty( $attr['autoplay'] ) ? false : $attr['autoplay']; |
77
|
|
|
$width = empty( $first_image['width'] ) ? 800 : $first_image['width']; |
78
|
|
|
$height = empty( $first_image['height'] ) ? 600 : $first_image['height']; |
79
|
|
|
return sprintf( |
80
|
|
|
'<amp-carousel width="%1$d" height="%2$d" layout="responsive" type="slides" data-next-button-aria-label="%3$s" data-prev-button-aria-label="%4$s" controls loop %5$s id="wp-block-jetpack-slideshow__amp-carousel__%6$s" on="slideChange:wp-block-jetpack-slideshow__amp-pagination__%6$s.toggle(index=event.index, value=true)">%7$s</amp-carousel>', |
81
|
|
|
esc_attr( $width ), |
82
|
|
|
esc_attr( $height ), |
83
|
|
|
esc_attr__( 'Next Slide', 'jetpack' ), |
84
|
|
|
esc_attr__( 'Previous Slide', 'jetpack' ), |
85
|
|
|
$autoplay ? 'autoplay delay=' . esc_attr( $delay * 1000 ) : '', |
86
|
|
|
absint( $block_ordinal ), |
87
|
|
|
implode( '', jetpack_slideshow_block_slides( $ids, $width, $height ) ) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Generate array of slides markup |
93
|
|
|
* |
94
|
|
|
* @param array $ids Array of image ids. |
95
|
|
|
* @param int $width Width of the container. |
96
|
|
|
* @param int $height Height of the container. |
97
|
|
|
* |
98
|
|
|
* @return array Array of slides markup. |
99
|
|
|
*/ |
100
|
|
|
function jetpack_slideshow_block_slides( $ids = array(), $width = 400, $height = 300 ) { |
101
|
|
|
return array_map( |
102
|
|
|
function( $id ) use ( $width, $height ) { |
103
|
|
|
$caption = wp_get_attachment_caption( $id ); |
104
|
|
|
$figcaption = $caption ? sprintf( |
105
|
|
|
'<figcaption class="wp-block-jetpack-slideshow_caption gallery-caption">%s</figcaption>', |
106
|
|
|
wp_kses_post( $caption ) |
107
|
|
|
) : ''; |
108
|
|
|
$image = wp_get_attachment_image( |
109
|
|
|
$id, |
110
|
|
|
array( $width, $height ), |
111
|
|
|
false, |
112
|
|
|
array( |
113
|
|
|
'class' => 'wp-block-jetpack-slideshow_image', |
114
|
|
|
'object-fit' => 'contain', |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
return sprintf( |
118
|
|
|
'<div class="wp-block-jetpack-slideshow_slide"><figure>%s%s</figure></div>', |
119
|
|
|
$image, |
120
|
|
|
$figcaption |
121
|
|
|
); |
122
|
|
|
}, |
123
|
|
|
$ids |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Generate array of bullets markup |
129
|
|
|
* |
130
|
|
|
* @param array $ids Array of image ids. |
131
|
|
|
* @param int $block_ordinal The ordinal number of the block, used in unique ID. |
132
|
|
|
* |
133
|
|
|
* @return array Array of bullets markup. |
134
|
|
|
*/ |
135
|
|
|
function jetpack_slideshow_block_bullets( $ids = array(), $block_ordinal = 0 ) { |
136
|
|
|
$buttons = array_map( |
137
|
|
|
function( $index ) { |
138
|
|
|
$aria_label = sprintf( |
139
|
|
|
/* translators: %d: Slide number. */ |
140
|
|
|
__( 'Go to slide %d', 'jetpack' ), |
141
|
|
|
absint( $index + 1 ) |
142
|
|
|
); |
143
|
|
|
return sprintf( |
144
|
|
|
'<button option="%d" class="swiper-pagination-bullet" tabindex="0" role="button" aria-label="%s" %s></button>', |
145
|
|
|
absint( $index ), |
146
|
|
|
esc_attr( $aria_label ), |
147
|
|
|
0 === $index ? 'selected' : '' |
148
|
|
|
); |
149
|
|
|
}, |
150
|
|
|
array_keys( $ids ) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
return sprintf( |
154
|
|
|
'<amp-selector id="wp-block-jetpack-slideshow__amp-pagination__%1$d" class="wp-block-jetpack-slideshow_pagination swiper-pagination swiper-pagination-bullets amp-pagination" on="select:wp-block-jetpack-slideshow__amp-carousel__%1$d.goToSlide(index=event.targetOption)" layout="container">%2$s</amp-selector>', |
155
|
|
|
absint( $block_ordinal ), |
156
|
|
|
implode( '', $buttons ) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Generate autoplay play/pause UI. |
162
|
|
|
* |
163
|
|
|
* @param int $block_ordinal The ordinal number of the block, used in unique ID. |
164
|
|
|
* |
165
|
|
|
* @return string Autoplay UI markup. |
166
|
|
|
*/ |
167
|
|
|
function jetpack_slideshow_block_autoplay_ui( $block_ordinal = 0 ) { |
168
|
|
|
$block_id = sprintf( |
169
|
|
|
'wp-block-jetpack-slideshow__%d', |
170
|
|
|
absint( $block_ordinal ) |
171
|
|
|
); |
172
|
|
|
$amp_carousel_id = sprintf( |
173
|
|
|
'wp-block-jetpack-slideshow__amp-carousel__%d', |
174
|
|
|
absint( $block_ordinal ) |
175
|
|
|
); |
176
|
|
|
$autoplay_pause = sprintf( |
177
|
|
|
'<a aria-label="%s" class="wp-block-jetpack-slideshow_button-pause" role="button" on="tap:%s.toggleAutoplay(toggleOn=false),%s.toggleClass(class=wp-block-jetpack-slideshow__autoplay-playing,force=false)"></a>', |
178
|
|
|
esc_attr__( 'Pause Slideshow', 'jetpack' ), |
179
|
|
|
esc_attr( $amp_carousel_id ), |
180
|
|
|
esc_attr( $block_id ) |
181
|
|
|
); |
182
|
|
|
$autoplay_play = sprintf( |
183
|
|
|
'<a aria-label="%s" class="wp-block-jetpack-slideshow_button-play" role="button" on="tap:%s.toggleAutoplay(toggleOn=true),%s.toggleClass(class=wp-block-jetpack-slideshow__autoplay-playing,force=true)"></a>', |
184
|
|
|
esc_attr__( 'Play Slideshow', 'jetpack' ), |
185
|
|
|
esc_attr( $amp_carousel_id ), |
186
|
|
|
esc_attr( $block_id ) |
187
|
|
|
); |
188
|
|
|
return $autoplay_pause . $autoplay_play; |
189
|
|
|
} |
190
|
|
|
|