Completed
Push — add/sync-partial-sync-checksum... ( 93f4e2...c9ed3f )
by
unknown
253:26 queued 243:12
created

story.php ➔ render_video()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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