1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Story Block. |
4
|
|
|
* |
5
|
|
|
* @since 8.6.1 |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Extensions\Story; |
11
|
|
|
|
12
|
|
|
use Jetpack_Gutenberg; |
13
|
|
|
|
14
|
|
|
const FEATURE_NAME = 'story'; |
15
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
16
|
|
|
|
17
|
|
|
const EMBED_SIZE = array( 180, 320 ); |
18
|
|
|
const CROP_UP_TO = 0.2; |
19
|
|
|
const IMAGE_BREAKPOINTS = '(max-width: 460px) 576w, (max-width: 614px) 768w, 120vw'; // 120vw to match the 20% CROP_UP_TO ratio |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Registers the block for use in Gutenberg |
23
|
|
|
* This is done via an action so that we can disable |
24
|
|
|
* registration if we need to. |
25
|
|
|
*/ |
26
|
|
|
function register_block() { |
27
|
|
|
jetpack_register_block( |
28
|
|
|
BLOCK_NAME, |
29
|
|
|
array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_block' ); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add missing `width`, `height`, `srcset` and `sizes` properties to images of the mediaFiles block attributes |
36
|
|
|
* |
37
|
|
|
* @param array $media_files List of media, each as an array containing the media attributes. |
38
|
|
|
* |
39
|
|
|
* @return array $media_files |
40
|
|
|
*/ |
41
|
|
|
function with_width_height_srcset_and_sizes( $media_files ) { |
42
|
|
|
return array_map( |
43
|
|
|
function( $media_file ) { |
44
|
|
|
if ( ! isset( $media_file['id'] ) || ! empty( $media_file['srcset'] ) ) { |
45
|
|
|
return $media_file; |
46
|
|
|
} |
47
|
|
|
$attachment_id = $media_file['id']; |
48
|
|
|
if ( 'image' === $media_file['type'] ) { |
49
|
|
|
$image = wp_get_attachment_image_src( $attachment_id, EMBED_SIZE, false ); |
50
|
|
|
if ( ! $image ) { |
51
|
|
|
return $media_file; |
52
|
|
|
} |
53
|
|
|
list( $src, $width, $height ) = $image; |
54
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
55
|
|
|
if ( ! is_array( $image_meta ) ) { |
56
|
|
|
return $media_file; |
57
|
|
|
} |
58
|
|
|
$size_array = array( absint( $width ), absint( $height ) ); |
59
|
|
|
return array_merge( |
60
|
|
|
$media_file, |
61
|
|
|
array( |
62
|
|
|
'width' => absint( $width ), |
63
|
|
|
'height' => absint( $height ), |
64
|
|
|
'srcset' => wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ), |
65
|
|
|
'sizes' => IMAGE_BREAKPOINTS, |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
} else { |
69
|
|
|
$video_meta = wp_get_attachment_metadata( $attachment_id ); |
70
|
|
|
if ( ! isset( $video_meta['width'] ) || ! isset( $video_meta['width'] ) ) { |
71
|
|
|
return $media_file; |
72
|
|
|
} |
73
|
|
|
$url = ! empty( $video_meta['original']['url'] ) ? $video_meta['original']['url'] : $media_file['url']; |
74
|
|
|
$description = ! empty( $video_meta['videopress']['description'] ) ? $video_meta['videopress']['description'] : $media_file['alt']; |
75
|
|
|
return array_merge( |
76
|
|
|
$media_file, |
77
|
|
|
array( |
78
|
|
|
'width' => absint( $video_meta['width'] ), |
79
|
|
|
'height' => absint( $video_meta['height'] ), |
80
|
|
|
'alt' => $description, |
81
|
|
|
'url' => $url, |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
}, |
86
|
|
|
$media_files |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Render an image inside a slide |
92
|
|
|
* |
93
|
|
|
* @param array $media Image information. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
function render_image( $media ) { |
98
|
|
|
if ( empty( $media['id'] ) || empty( $media['url'] ) ) { |
99
|
|
|
return __( 'Error retrieving media', 'jetpack' ); |
100
|
|
|
} |
101
|
|
|
$image = wp_get_attachment_image_src( $media['id'], 'full', false ); |
102
|
|
|
$crop_class = ''; |
103
|
|
|
if ( $image ) { |
104
|
|
|
list( , $width, $height ) = $image; |
105
|
|
|
$crop_class = get_image_crop_class( $width, $height ); |
106
|
|
|
} |
107
|
|
|
// need to specify the size of the embed so it picks an image that is large enough for the `src` attribute |
108
|
|
|
// `sizes` is optimized for 1080x1920 (9:16) images |
109
|
|
|
// Note that the Story block does not have thumbnail support, it will load the right |
110
|
|
|
// image based on the viewport size only. |
111
|
|
|
return wp_get_attachment_image( |
112
|
|
|
$media['id'], |
113
|
|
|
EMBED_SIZE, |
114
|
|
|
false, |
115
|
|
|
array( |
116
|
|
|
'class' => sprintf( 'wp-story-image wp-image-%d %s', $media['id'], $crop_class ), |
117
|
|
|
'sizes' => IMAGE_BREAKPOINTS, |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the css crop class if image width and height requires it |
124
|
|
|
* |
125
|
|
|
* @param array $width Image width. |
126
|
|
|
* @param array $height Image height. |
127
|
|
|
* |
128
|
|
|
* @return string The CSS class which will display a cropped image |
129
|
|
|
*/ |
130
|
|
|
function get_image_crop_class( $width, $height ) { |
131
|
|
|
$crop_class = ''; |
132
|
|
|
$media_aspect_ratio = $width / $height; |
133
|
|
|
$target_aspect_ratio = EMBED_SIZE[0] / EMBED_SIZE[1]; |
134
|
|
|
if ( $media_aspect_ratio >= $target_aspect_ratio ) { |
135
|
|
|
// image wider than canvas. |
136
|
|
|
$media_too_wide_to_crop = $media_aspect_ratio > $target_aspect_ratio / ( 1 - CROP_UP_TO ); |
137
|
|
|
if ( ! $media_too_wide_to_crop ) { |
138
|
|
|
$crop_class = 'wp-story-crop-wide'; |
139
|
|
|
} |
140
|
|
|
} else { |
141
|
|
|
// image narrower than canvas. |
142
|
|
|
$media_too_narrow_to_crop = $media_aspect_ratio < $target_aspect_ratio * ( 1 - CROP_UP_TO ); |
143
|
|
|
if ( ! $media_too_narrow_to_crop ) { |
144
|
|
|
$crop_class = 'wp-story-crop-narrow'; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $crop_class; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Render a video inside a slide |
152
|
|
|
* |
153
|
|
|
* @param array $media Video information. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
function render_video( $media ) { |
158
|
|
|
if ( empty( $media['id'] ) || empty( $media['mime'] ) || empty( $media['url'] ) ) { |
159
|
|
|
return __( 'Error retrieving media', 'jetpack' ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$metadata = wp_get_attachment_metadata( $media['id'] ); |
163
|
|
|
if ( ! empty( $metadata ) && ! empty( $metadata['videopress'] ) ) { |
164
|
|
|
$poster_url = $metadata['videopress']['poster']; |
165
|
|
|
$description = ! empty( $metadata['videopress']['description'] ) ? $metadata['videopress']['description'] : ''; |
166
|
|
|
return sprintf( |
167
|
|
|
'<img |
168
|
|
|
alt="%s" |
169
|
|
|
class="wp-block-jetpack-story_image wp-story-image %s" |
170
|
|
|
src="%s">', |
171
|
|
|
esc_attr( $description ), |
172
|
|
|
get_image_crop_class( $metadata['videopress']['width'], $metadata['videopress']['height'] ), |
173
|
|
|
esc_attr( $poster_url ) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return sprintf( |
178
|
|
|
'<video |
179
|
|
|
title="%1$s" |
180
|
|
|
type="%2$s" |
181
|
|
|
class="wp-story-video intrinsic-ignore wp-video-%3$s" |
182
|
|
|
data-id="%3$s" |
183
|
|
|
src="%4$s"> |
184
|
|
|
</video>', |
185
|
|
|
esc_attr( $media['alt'] ), |
186
|
|
|
esc_attr( $media['mime'] ), |
187
|
|
|
$media['id'], |
188
|
|
|
esc_attr( $media['url'] ) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Render a slide |
194
|
|
|
* |
195
|
|
|
* @param array $media Media information. |
196
|
|
|
* @param array $index Index of the slide, first slide will be displayed by default, others hidden. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
function render_slide( $media, $index = 0 ) { |
201
|
|
|
$media_template = ''; |
202
|
|
|
$media_type = ! empty( $media['type'] ) ? $media['type'] : null; |
203
|
|
|
if ( ! $media_type ) { |
204
|
|
|
return ''; |
205
|
|
|
} |
206
|
|
|
switch ( $media_type ) { |
207
|
|
|
case 'image': |
208
|
|
|
$media_template = render_image( $media, $index ); |
209
|
|
|
break; |
210
|
|
|
case 'video': |
211
|
|
|
$media_template = render_video( $media, $index ); |
212
|
|
|
break; |
213
|
|
|
} |
214
|
|
|
return sprintf( |
215
|
|
|
'<div class="wp-story-slide" style="display: %s;"> |
216
|
|
|
<figure> |
217
|
|
|
%s |
218
|
|
|
</figure> |
219
|
|
|
</div>', |
220
|
|
|
0 === $index ? 'block' : 'none', |
221
|
|
|
$media_template |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Render the top right icon on top of the story embed |
227
|
|
|
* |
228
|
|
|
* @param array $settings The block settings. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
function render_top_right_icon( $settings ) { |
233
|
|
|
$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false; |
234
|
|
|
$slide_count = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0; |
235
|
|
|
if ( $show_slide_count ) { |
236
|
|
|
// Render the story block icon along with the slide count. |
237
|
|
|
return sprintf( |
238
|
|
|
'<div class="wp-story-embed-icon"> |
239
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"> |
240
|
|
|
<path d="M0 0h24v24H0z" fill="none"></path> |
241
|
|
|
<path fill-rule="evenodd" clip-rule="evenodd" d="M6 3H14V17H6L6 3ZM4 3C4 1.89543 4.89543 1 6 1H14C15.1046 1 16 1.89543 16 3V17C16 18.1046 15.1046 19 14 19H6C4.89543 19 4 18.1046 4 17V3ZM18 5C19.1046 5 20 5.89543 20 7V21C20 22.1046 19.1046 23 18 23H10C8.89543 23 8 22.1046 8 21H18V5Z"></path> |
242
|
|
|
</svg> |
243
|
|
|
<span>%d</span> |
244
|
|
|
</div>', |
245
|
|
|
$slide_count |
246
|
|
|
); |
247
|
|
|
} else { |
248
|
|
|
// Render the Fullscreen Gridicon. |
249
|
|
|
return ( |
250
|
|
|
'<div class="wp-story-embed-icon-expand"> |
251
|
|
|
<svg class="gridicon gridicons-fullscreen" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> |
252
|
|
|
<g> |
253
|
|
|
<path d="M21 3v6h-2V6.41l-3.29 3.3-1.42-1.42L17.59 5H15V3zM3 3v6h2V6.41l3.29 3.3 1.42-1.42L6.41 5H9V3zm18 18v-6h-2v2.59l-3.29-3.29-1.41 1.41L17.59 19H15v2zM9 21v-2H6.41l3.29-3.29-1.41-1.42L5 17.59V15H3v6z"></path> |
254
|
|
|
</g> |
255
|
|
|
</svg> |
256
|
|
|
</div>' |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Render a pagination bullet |
263
|
|
|
* |
264
|
|
|
* @param array $slide_index The slide index it corresponds to. |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
function render_pagination_bullet( $slide_index ) { |
269
|
|
|
return sprintf( |
270
|
|
|
'<a href="#" class="wp-story-pagination-bullet" aria-label="%s"> |
271
|
|
|
<div class="wp-story-pagination-bullet-bar"></div> |
272
|
|
|
</a>', |
273
|
|
|
/* translators: %d is the slide number (1, 2, 3...) */ |
274
|
|
|
sprintf( __( 'Go to slide %d', 'jetpack' ), $slide_index ) |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Render pagination on top of the story embed |
280
|
|
|
* |
281
|
|
|
* @param array $settings The block settings. |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
function render_pagination( $settings ) { |
286
|
|
|
$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false; |
287
|
|
|
if ( $show_slide_count ) { |
288
|
|
|
return ''; |
289
|
|
|
} |
290
|
|
|
$slide_count = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0; |
291
|
|
|
return sprintf( |
292
|
|
|
'<div class="wp-story-pagination wp-story-pagination-bullets"> |
293
|
|
|
%s |
294
|
|
|
</div>', |
295
|
|
|
join( "\n", array_map( __NAMESPACE__ . '\render_pagination_bullet', range( 1, $slide_count ) ) ) |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Render story block |
301
|
|
|
* |
302
|
|
|
* @param array $attributes Block attributes. |
303
|
|
|
* |
304
|
|
|
* @return string |
305
|
|
|
*/ |
306
|
|
|
function render_block( $attributes ) { |
307
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
308
|
|
|
|
309
|
|
|
$media_files = isset( $attributes['mediaFiles'] ) ? $attributes['mediaFiles'] : array(); |
310
|
|
|
$settings_from_attributes = isset( $attributes['settings'] ) ? $attributes['settings'] : array(); |
311
|
|
|
|
312
|
|
|
$settings = array_merge( |
313
|
|
|
$settings_from_attributes, |
314
|
|
|
array( |
315
|
|
|
'slides' => with_width_height_srcset_and_sizes( $media_files ), |
316
|
|
|
) |
317
|
|
|
); |
318
|
|
|
|
319
|
|
|
return sprintf( |
320
|
|
|
'<div class="%1$s" data-settings="%2$s"> |
321
|
|
|
<div style="display: contents;"> |
322
|
|
|
<div class="wp-story-container"> |
323
|
|
|
<div class="wp-story-meta"> |
324
|
|
|
<div class="wp-story-icon"> |
325
|
|
|
<img alt="%3$s" src="%4$s" width="32" height=32> |
326
|
|
|
</div> |
327
|
|
|
<div> |
328
|
|
|
<div class="wp-story-title"> |
329
|
|
|
%5$s |
330
|
|
|
</div> |
331
|
|
|
</div> |
332
|
|
|
<a class="wp-story-exit-fullscreen jetpack-mdc-icon-button"> |
333
|
|
|
<i class="jetpack-material-icons close md-24"></i> |
334
|
|
|
</a> |
335
|
|
|
</div> |
336
|
|
|
<div class="wp-story-wrapper"> |
337
|
|
|
%6$s |
338
|
|
|
</div> |
339
|
|
|
<a class="wp-story-overlay" href="%7$s"> |
340
|
|
|
%8$s |
341
|
|
|
</a> |
342
|
|
|
%9$s |
343
|
|
|
</div> |
344
|
|
|
</div> |
345
|
|
|
</div>', |
346
|
|
|
esc_attr( Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ), |
347
|
|
|
filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ), |
348
|
|
|
__( 'Site icon', 'jetpack' ), |
349
|
|
|
esc_attr( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ), |
350
|
|
|
esc_html( get_the_title() ), |
351
|
|
|
! empty( $media_files[0] ) ? render_slide( $media_files[0] ) : '', |
352
|
|
|
get_permalink() . '?wp-story-load-in-fullscreen=true&wp-story-play-on-load=true', |
353
|
|
|
render_top_right_icon( $settings ), |
354
|
|
|
render_pagination( $settings ) |
355
|
|
|
); |
356
|
|
|
} |
357
|
|
|
|