|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Story Block. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 8.6.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @package automattic/jetpack |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Extensions\Story; |
|
11
|
|
|
|
|
12
|
|
|
use Automattic\Jetpack\Blocks; |
|
13
|
|
|
use Jetpack; |
|
14
|
|
|
use Jetpack_Gutenberg; |
|
15
|
|
|
|
|
16
|
|
|
const FEATURE_NAME = 'story'; |
|
17
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
|
18
|
|
|
|
|
19
|
|
|
const EMBED_SIZE = array( 360, 640 ); // twice as many pixels for retina displays. |
|
20
|
|
|
const CROP_UP_TO = 0.2; |
|
21
|
|
|
const MAX_BULLETS = 7; |
|
22
|
|
|
const IMAGE_BREAKPOINTS = '(max-width: 460px) 576w, (max-width: 614px) 768w, 120vw'; // 120vw to match the 20% CROP_UP_TO ratio |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Registers the block for use in Gutenberg |
|
26
|
|
|
* This is done via an action so that we can disable |
|
27
|
|
|
* registration if we need to. |
|
28
|
|
|
*/ |
|
29
|
|
|
function register_block() { |
|
30
|
|
|
Blocks::jetpack_register_block( |
|
31
|
|
|
BLOCK_NAME, |
|
32
|
|
|
array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_block' ); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Compare 2 urls and return true if they likely correspond to the same resource. |
|
39
|
|
|
* Ignore scheme, ports, query params and hashes and only compare hostname and pathname. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $url1 - First url used in comparison. |
|
42
|
|
|
* @param string $url2 - Second url used in comparison. |
|
43
|
|
|
* |
|
44
|
|
|
* @returns boolean |
|
45
|
|
|
*/ |
|
46
|
|
|
function is_same_resource( $url1, $url2 ) { |
|
47
|
|
|
$url1_parsed = wp_parse_url( $url1 ); |
|
48
|
|
|
$url2_parsed = wp_parse_url( $url2 ); |
|
49
|
|
|
return isset( $url1_parsed['host'] ) && |
|
50
|
|
|
isset( $url2_parsed['host'] ) && |
|
51
|
|
|
isset( $url1_parsed['path'] ) && |
|
52
|
|
|
isset( $url2_parsed['path'] ) && |
|
53
|
|
|
$url1_parsed['host'] === $url2_parsed['host'] && |
|
54
|
|
|
$url1_parsed['path'] === $url2_parsed['path']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Enrich media files retrieved from the story block attributes |
|
59
|
|
|
* with extra information we can retrieve from the media library. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $media_files - List of media, each as an array containing the media attributes. |
|
62
|
|
|
* |
|
63
|
|
|
* @returns array $media_files |
|
64
|
|
|
*/ |
|
65
|
|
|
function enrich_media_files( $media_files ) { |
|
66
|
|
|
return array_filter( |
|
67
|
|
|
array_map( |
|
68
|
|
|
function ( $media_file ) { |
|
69
|
|
|
if ( 'image' === $media_file['type'] ) { |
|
70
|
|
|
return enrich_image_meta( $media_file ); |
|
71
|
|
|
} |
|
72
|
|
|
// VideoPress videos can sometimes have type 'file', and mime 'video/videopress' or 'video/mp4'. |
|
73
|
|
|
// Let's fix `type` for those. |
|
74
|
|
|
if ( 'file' === $media_file['type'] && 'video' === substr( $media_file['mime'], 0, 5 ) ) { |
|
75
|
|
|
$media_file['type'] = 'video'; |
|
76
|
|
|
} |
|
77
|
|
|
if ( 'video' !== $media_file['type'] ) { // we only support images and videos at this point. |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
return enrich_video_meta( $media_file ); |
|
81
|
|
|
}, |
|
82
|
|
|
$media_files |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Enrich image information with extra data we can retrieve from the media library. |
|
89
|
|
|
* Add missing `width`, `height`, `srcset`, `sizes`, `title`, `alt` and `caption` properties to the image. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $media_file - An array containing the media attributes for a specific image. |
|
92
|
|
|
* |
|
93
|
|
|
* @returns array $media_file_enriched |
|
94
|
|
|
*/ |
|
95
|
|
|
function enrich_image_meta( $media_file ) { |
|
96
|
|
|
$attachment_id = isset( $media_file['id'] ) ? $media_file['id'] : null; |
|
97
|
|
|
$image = wp_get_attachment_image_src( $attachment_id, 'full', false ); |
|
98
|
|
|
if ( ! $image ) { |
|
99
|
|
|
return $media_file; |
|
100
|
|
|
} |
|
101
|
|
|
list( $src, $width, $height ) = $image; |
|
102
|
|
|
// Bail if url stored in block attributes is different than the media library one for that id. |
|
103
|
|
|
if ( isset( $media_file['url'] ) && ! is_same_resource( $media_file['url'], $src ) ) { |
|
104
|
|
|
return $media_file; |
|
105
|
|
|
} |
|
106
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
107
|
|
|
if ( ! is_array( $image_meta ) ) { |
|
108
|
|
|
return $media_file; |
|
109
|
|
|
} |
|
110
|
|
|
$size_array = array( absint( $width ), absint( $height ) ); |
|
111
|
|
|
return array_merge( |
|
112
|
|
|
$media_file, |
|
113
|
|
|
array( |
|
114
|
|
|
'width' => absint( $width ), |
|
115
|
|
|
'height' => absint( $height ), |
|
116
|
|
|
'srcset' => wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ), |
|
117
|
|
|
'sizes' => IMAGE_BREAKPOINTS, |
|
118
|
|
|
'title' => get_the_title( $attachment_id ), |
|
119
|
|
|
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), |
|
120
|
|
|
'caption' => wp_get_attachment_caption( $attachment_id ), |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Enrich video information with extra data we can retrieve from the media library. |
|
127
|
|
|
* Add missing `width`, `height`, `alt`, `url`, `title`, `caption` and `poster` properties to the image. |
|
128
|
|
|
* |
|
129
|
|
|
* @param array $media_file - An array containing the media attributes for a specific video. |
|
130
|
|
|
* |
|
131
|
|
|
* @returns array $media_file_enriched |
|
132
|
|
|
*/ |
|
133
|
|
|
function enrich_video_meta( $media_file ) { |
|
134
|
|
|
$attachment_id = isset( $media_file['id'] ) ? $media_file['id'] : null; |
|
135
|
|
|
$video_meta = wp_get_attachment_metadata( $attachment_id ); |
|
136
|
|
|
if ( ! $video_meta ) { |
|
137
|
|
|
return $media_file; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$video_url = ! empty( $video_meta['original']['url'] ) ? $video_meta['original']['url'] : wp_get_attachment_url( $attachment_id ); |
|
141
|
|
|
|
|
142
|
|
|
// Set the poster attribute for the video tag if a poster image is available. |
|
143
|
|
|
$poster_url = null; |
|
144
|
|
|
if ( ! empty( $video_meta['videopress']['poster'] ) ) { |
|
145
|
|
|
$poster_url = $video_meta['videopress']['poster']; |
|
146
|
|
|
} elseif ( ! empty( $video_meta['thumb'] ) ) { |
|
147
|
|
|
$poster_url = str_replace( wp_basename( $video_url ), $video_meta['thumb'], $video_url ); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if ( $poster_url ) { |
|
151
|
|
|
// Use the global content width for thumbnail resize so we match the `w=` query parameter |
|
152
|
|
|
// that jetpack is going to add when "Enable site accelerator" is enabled for images. |
|
153
|
|
|
$content_width = (int) Jetpack::get_content_width(); |
|
154
|
|
|
$new_width = $content_width > 0 ? $content_width : EMBED_SIZE[0]; |
|
155
|
|
|
$poster_url = add_query_arg( 'w', $new_width, $poster_url ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return array_merge( |
|
159
|
|
|
$media_file, |
|
160
|
|
|
array( |
|
161
|
|
|
'width' => absint( ! empty( $video_meta['width'] ) ? $video_meta['width'] : $media_file['width'] ), |
|
162
|
|
|
'height' => absint( ! empty( $video_meta['height'] ) ? $video_meta['height'] : $media_file['height'] ), |
|
163
|
|
|
'alt' => ! empty( $video_meta['videopress']['description'] ) ? $video_meta['videopress']['description'] : $media_file['alt'], |
|
164
|
|
|
'url' => $video_url, |
|
165
|
|
|
'title' => get_the_title( $attachment_id ), |
|
166
|
|
|
'caption' => wp_get_attachment_caption( $attachment_id ), |
|
167
|
|
|
'poster' => $poster_url, |
|
168
|
|
|
) |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Render an image inside a slide |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $media - Image information. |
|
176
|
|
|
* |
|
177
|
|
|
* @returns string |
|
178
|
|
|
*/ |
|
179
|
|
|
function render_image( $media ) { |
|
180
|
|
|
if ( empty( $media['id'] ) || empty( $media['url'] ) ) { |
|
181
|
|
|
return __( 'Error retrieving media', 'jetpack' ); |
|
182
|
|
|
} |
|
183
|
|
|
$image = wp_get_attachment_image_src( $media['id'], 'full', false ); |
|
184
|
|
|
if ( $image ) { |
|
185
|
|
|
list( $src, $width, $height ) = $image; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// if image does not match. |
|
189
|
|
|
if ( ! $image || isset( $media['url'] ) && ! is_same_resource( $media['url'], $src ) ) { |
|
|
|
|
|
|
190
|
|
|
$width = isset( $media['width'] ) ? $media['width'] : null; |
|
191
|
|
|
$height = isset( $media['height'] ) ? $media['height'] : null; |
|
192
|
|
|
$title = isset( $media['title'] ) ? $media['title'] : ''; |
|
193
|
|
|
$alt = isset( $media['alt'] ) ? $media['alt'] : ''; |
|
194
|
|
|
return sprintf( |
|
195
|
|
|
'<img |
|
196
|
|
|
title="%1$s" |
|
197
|
|
|
alt="%2$s" |
|
198
|
|
|
class="wp-block-jetpack-story_image wp-story-image %3$s" |
|
199
|
|
|
src="%4$s" |
|
200
|
|
|
/>', |
|
201
|
|
|
esc_attr( $title ), |
|
202
|
|
|
esc_attr( $alt ), |
|
203
|
|
|
$width && $height ? get_image_crop_class( $width, $height ) : '', |
|
204
|
|
|
esc_attr( $media['url'] ) |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$crop_class = get_image_crop_class( $width, $height ); |
|
|
|
|
|
|
209
|
|
|
// need to specify the size of the embed so it picks an image that is large enough for the `src` attribute |
|
210
|
|
|
// `sizes` is optimized for 1080x1920 (9:16) images |
|
211
|
|
|
// Note that the Story block does not have thumbnail support, it will load the right |
|
212
|
|
|
// image based on the viewport size only. |
|
213
|
|
|
return wp_get_attachment_image( |
|
214
|
|
|
$media['id'], |
|
215
|
|
|
EMBED_SIZE, |
|
216
|
|
|
false, |
|
217
|
|
|
array( |
|
218
|
|
|
'class' => sprintf( 'wp-story-image wp-image-%d %s', $media['id'], $crop_class ), |
|
219
|
|
|
'sizes' => IMAGE_BREAKPOINTS, |
|
220
|
|
|
'title' => get_the_title( $media['id'] ), |
|
221
|
|
|
) |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Return the css crop class if image width and height requires it |
|
227
|
|
|
* |
|
228
|
|
|
* @param int $width - Image width. |
|
229
|
|
|
* @param int $height - Image height. |
|
230
|
|
|
* |
|
231
|
|
|
* @returns string The CSS class which will display a cropped image |
|
232
|
|
|
*/ |
|
233
|
|
|
function get_image_crop_class( $width, $height ) { |
|
234
|
|
|
$crop_class = ''; |
|
235
|
|
|
$media_aspect_ratio = $width / $height; |
|
236
|
|
|
$target_aspect_ratio = EMBED_SIZE[0] / EMBED_SIZE[1]; |
|
237
|
|
|
if ( $media_aspect_ratio >= $target_aspect_ratio ) { |
|
238
|
|
|
// image wider than canvas. |
|
239
|
|
|
$media_too_wide_to_crop = $media_aspect_ratio > $target_aspect_ratio / ( 1 - CROP_UP_TO ); |
|
240
|
|
|
if ( ! $media_too_wide_to_crop ) { |
|
241
|
|
|
$crop_class = 'wp-story-crop-wide'; |
|
242
|
|
|
} |
|
243
|
|
|
} else { |
|
244
|
|
|
// image narrower than canvas. |
|
245
|
|
|
$media_too_narrow_to_crop = $media_aspect_ratio < $target_aspect_ratio * ( 1 - CROP_UP_TO ); |
|
246
|
|
|
if ( ! $media_too_narrow_to_crop ) { |
|
247
|
|
|
$crop_class = 'wp-story-crop-narrow'; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
return $crop_class; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Render a video inside a slide |
|
255
|
|
|
* |
|
256
|
|
|
* @param array $media - Video information. |
|
257
|
|
|
* |
|
258
|
|
|
* @returns string |
|
259
|
|
|
*/ |
|
260
|
|
|
function render_video( $media ) { |
|
261
|
|
|
if ( empty( $media['id'] ) || empty( $media['mime'] ) || empty( $media['url'] ) ) { |
|
262
|
|
|
return __( 'Error retrieving media', 'jetpack' ); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if ( ! empty( $media['poster'] ) ) { |
|
266
|
|
|
return render_image( |
|
267
|
|
|
array_merge( |
|
268
|
|
|
$media, |
|
269
|
|
|
array( |
|
270
|
|
|
'type' => 'image', |
|
271
|
|
|
'url' => $media['poster'], |
|
272
|
|
|
) |
|
273
|
|
|
) |
|
274
|
|
|
); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return sprintf( |
|
278
|
|
|
'<video |
|
279
|
|
|
title="%1$s" |
|
280
|
|
|
type="%2$s" |
|
281
|
|
|
class="wp-story-video intrinsic-ignore wp-video-%3$s" |
|
282
|
|
|
data-id="%3$s" |
|
283
|
|
|
src="%4$s"> |
|
284
|
|
|
</video>', |
|
285
|
|
|
esc_attr( get_the_title( $media['id'] ) ), |
|
286
|
|
|
esc_attr( $media['mime'] ), |
|
287
|
|
|
$media['id'], |
|
288
|
|
|
esc_attr( $media['url'] ) |
|
289
|
|
|
); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Pick a thumbnail to render a static/embedded story |
|
294
|
|
|
* |
|
295
|
|
|
* @param array $media_files - list of Media files. |
|
296
|
|
|
* |
|
297
|
|
|
* @returns string |
|
298
|
|
|
*/ |
|
299
|
|
|
function render_static_slide( $media_files ) { |
|
300
|
|
|
$media_template = ''; |
|
301
|
|
|
if ( empty( $media_files ) ) { |
|
302
|
|
|
return ''; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
// find an image to showcase. |
|
306
|
|
|
foreach ( $media_files as $media ) { |
|
307
|
|
|
switch ( $media['type'] ) { |
|
308
|
|
|
case 'image': |
|
309
|
|
|
$media_template = render_image( $media ); |
|
310
|
|
|
break 2; |
|
311
|
|
|
case 'video': |
|
312
|
|
|
// ignore videos without a poster image. |
|
313
|
|
|
if ( empty( $media['poster'] ) ) { |
|
314
|
|
|
continue 2; |
|
315
|
|
|
} |
|
316
|
|
|
$media_template = render_video( $media ); |
|
317
|
|
|
break 2; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
// if no "static" media was found for the thumbnail try to render a video tag without poster. |
|
322
|
|
|
if ( empty( $media_template ) && ! empty( $media_files ) ) { |
|
323
|
|
|
$media_template = render_video( $media_files[0] ); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return sprintf( |
|
327
|
|
|
'<div class="wp-story-slide" style="display: block;"> |
|
328
|
|
|
<figure>%s</figure> |
|
329
|
|
|
</div>', |
|
330
|
|
|
$media_template |
|
331
|
|
|
); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Render the top right icon on top of the story embed |
|
336
|
|
|
* |
|
337
|
|
|
* @param array $settings - The block settings. |
|
338
|
|
|
* |
|
339
|
|
|
* @returns string |
|
340
|
|
|
*/ |
|
341
|
|
|
function render_top_right_icon( $settings ) { |
|
342
|
|
|
$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false; |
|
343
|
|
|
$slide_count = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0; |
|
344
|
|
|
if ( $show_slide_count ) { |
|
345
|
|
|
// Render the story block icon along with the slide count. |
|
346
|
|
|
return sprintf( |
|
347
|
|
|
'<div class="wp-story-embed-icon"> |
|
348
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"> |
|
349
|
|
|
<path d="M0 0h24v24H0z" fill="none"></path> |
|
350
|
|
|
<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> |
|
351
|
|
|
</svg> |
|
352
|
|
|
<span>%d</span> |
|
353
|
|
|
</div>', |
|
354
|
|
|
$slide_count |
|
355
|
|
|
); |
|
356
|
|
|
} else { |
|
357
|
|
|
// Render the Fullscreen Gridicon. |
|
358
|
|
|
return ( |
|
359
|
|
|
'<div class="wp-story-embed-icon-expand"> |
|
360
|
|
|
<svg class="gridicon gridicons-fullscreen" role="img" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> |
|
361
|
|
|
<g> |
|
362
|
|
|
<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> |
|
363
|
|
|
</g> |
|
364
|
|
|
</svg> |
|
365
|
|
|
</div>' |
|
366
|
|
|
); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Render a pagination bullet |
|
372
|
|
|
* |
|
373
|
|
|
* @param int $slide_index - The slide index it corresponds to. |
|
374
|
|
|
* @param string $class_name - Optional css class name(s) to customize the bullet element. |
|
375
|
|
|
* |
|
376
|
|
|
* @returns string |
|
377
|
|
|
*/ |
|
378
|
|
|
function render_pagination_bullet( $slide_index, $class_name = '' ) { |
|
379
|
|
|
return sprintf( |
|
380
|
|
|
'<div class="wp-story-pagination-bullet %s" aria-label="%s"> |
|
381
|
|
|
<div class="wp-story-pagination-bullet-bar"></div> |
|
382
|
|
|
</div>', |
|
383
|
|
|
esc_attr( $class_name ), |
|
384
|
|
|
/* translators: %d is the slide number (1, 2, 3...) */ |
|
385
|
|
|
sprintf( __( 'Go to slide %d', 'jetpack' ), $slide_index ) |
|
386
|
|
|
); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* Render pagination on top of the story embed |
|
391
|
|
|
* |
|
392
|
|
|
* @param array $settings - The block settings. |
|
393
|
|
|
* |
|
394
|
|
|
* @returns string |
|
395
|
|
|
*/ |
|
396
|
|
|
function render_pagination( $settings ) { |
|
397
|
|
|
$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false; |
|
398
|
|
|
if ( $show_slide_count ) { |
|
399
|
|
|
return ''; |
|
400
|
|
|
} |
|
401
|
|
|
$slide_count = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0; |
|
402
|
|
|
$bullet_count = min( $slide_count, MAX_BULLETS ); |
|
403
|
|
|
$bullet_ellipsis = $slide_count > $bullet_count |
|
404
|
|
|
? render_pagination_bullet( $bullet_count + 1, 'wp-story-pagination-ellipsis' ) |
|
405
|
|
|
: ''; |
|
406
|
|
|
return sprintf( |
|
407
|
|
|
'<div class="wp-story-pagination wp-story-pagination-bullets"> |
|
408
|
|
|
%s |
|
409
|
|
|
</div>', |
|
410
|
|
|
join( "\n", array_map( __NAMESPACE__ . '\render_pagination_bullet', range( 1, $bullet_count ) ) ) . $bullet_ellipsis |
|
411
|
|
|
); |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Render story block |
|
416
|
|
|
* |
|
417
|
|
|
* @param array $attributes - Block attributes. |
|
418
|
|
|
* |
|
419
|
|
|
* @returns string |
|
420
|
|
|
*/ |
|
421
|
|
|
function render_block( $attributes ) { |
|
422
|
|
|
// Let's use a counter to have a different id for each story rendered in the same context. |
|
423
|
|
|
static $story_block_counter = 0; |
|
424
|
|
|
|
|
425
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
|
426
|
|
|
|
|
427
|
|
|
$media_files = isset( $attributes['mediaFiles'] ) ? enrich_media_files( $attributes['mediaFiles'] ) : array(); |
|
428
|
|
|
$settings_from_attributes = isset( $attributes['settings'] ) ? $attributes['settings'] : array(); |
|
429
|
|
|
|
|
430
|
|
|
$settings = array_merge( |
|
431
|
|
|
$settings_from_attributes, |
|
432
|
|
|
array( |
|
433
|
|
|
'slides' => $media_files, |
|
434
|
|
|
) |
|
435
|
|
|
); |
|
436
|
|
|
|
|
437
|
|
|
return sprintf( |
|
438
|
|
|
'<div class="%1$s" data-id="%2$s" data-settings="%3$s"> |
|
439
|
|
|
<div class="wp-story-app"> |
|
440
|
|
|
<div class="wp-story-display-contents" style="display: contents;"> |
|
441
|
|
|
<a class="wp-story-container" href="%4$s" title="%5$s"> |
|
442
|
|
|
<div class="wp-story-meta"> |
|
443
|
|
|
<div class="wp-story-icon"> |
|
444
|
|
|
<img alt="%6$s" src="%7$s" width="40" height="40"> |
|
445
|
|
|
</div> |
|
446
|
|
|
<div> |
|
447
|
|
|
<div class="wp-story-title"> |
|
448
|
|
|
%8$s |
|
449
|
|
|
</div> |
|
450
|
|
|
</div> |
|
451
|
|
|
</div> |
|
452
|
|
|
<div class="wp-story-wrapper"> |
|
453
|
|
|
%9$s |
|
454
|
|
|
</div> |
|
455
|
|
|
<div class="wp-story-overlay"> |
|
456
|
|
|
%10$s |
|
457
|
|
|
</div> |
|
458
|
|
|
%11$s |
|
459
|
|
|
</a> |
|
460
|
|
|
</div> |
|
461
|
|
|
</div> |
|
462
|
|
|
</div>', |
|
463
|
|
|
esc_attr( Blocks::classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ), |
|
464
|
|
|
esc_attr( 'wp-story-' . get_the_ID() . '-' . strval( ++$story_block_counter ) ), |
|
465
|
|
|
filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ), |
|
466
|
|
|
get_permalink() . '?wp-story-load-in-fullscreen=true&wp-story-play-on-load=true', |
|
467
|
|
|
__( 'Play story in new tab', 'jetpack' ), |
|
468
|
|
|
__( 'Site icon', 'jetpack' ), |
|
469
|
|
|
esc_attr( get_site_icon_url( 80, includes_url( 'images/w-logo-blue.png' ) ) ), |
|
470
|
|
|
esc_html( get_the_title() ), |
|
471
|
|
|
render_static_slide( $media_files ), |
|
472
|
|
|
render_top_right_icon( $settings ), |
|
473
|
|
|
render_pagination( $settings ) |
|
474
|
|
|
); |
|
475
|
|
|
} |
|
476
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: