Completed
Push — update/story-block-loading-med... ( e6e62b...d81748 )
by
unknown
28:46 queued 18:45
created

story.php ➔ get_image_crop_class()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 19
rs 9.6333
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 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
	switch ( $media_type ) {
204
		case 'image':
205
			$media_template = render_image( $media, $index );
206
			break;
207
		case 'video':
208
			$media_template = render_video( $media, $index );
209
			break;
210
	}
211
	return sprintf(
212
		'<div class="wp-story-slide" style="display: %s;">
213
			<figure>
214
				%s
215
			</figure>
216
		</div>',
217
		0 === $index ? 'block' : 'none',
218
		$media_template
219
	);
220
}
221
222
/**
223
 * Render the top right icon on top of the story embed
224
 *
225
 * @param array $settings The block settings.
226
 *
227
 * @return string
228
 */
229
function render_top_right_icon( $settings ) {
230
	$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false;
231
	$slide_count      = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0;
232
	if ( $show_slide_count ) {
233
		// Render the story block icon along with the slide count.
234
		return sprintf(
235
			'<div class="wp-story-embed-icon">
236
				<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">
237
					<path d="M0 0h24v24H0z" fill="none"></path>
238
					<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>
239
				</svg>
240
				<span>%s</span>
241
			</div>',
242
			$slide_count
243
		);
244
	} else {
245
		// Render the Fullscreen Gridicon.
246
		return (
247
			'<div class="wp-story-embed-icon-expand">
248
				<svg class="gridicon gridicons-fullscreen" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
249
					<g>
250
						<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>
251
					</g>
252
				</svg>
253
			</div>'
254
		);
255
	}
256
}
257
258
/**
259
 * Render a pagination bullet
260
 *
261
 * @param array $slide_index The slide index it corresponds to.
262
 *
263
 * @return string
264
 */
265
function render_pagination_bullet( $slide_index ) {
266
	return sprintf(
267
		'<button class="wp-story-pagination-bullet" aria-label="Go to slide %d">
268
			<div class="wp-story-pagination-bullet-bar"></div>
269
		</button>',
270
		$slide_index
271
	);
272
}
273
274
/**
275
 * Render pagination on top of the story embed
276
 *
277
 * @param array $settings The block settings.
278
 *
279
 * @return string
280
 */
281
function render_pagination( $settings ) {
282
	$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false;
283
	if ( $show_slide_count ) {
284
		return '';
285
	}
286
	$slide_count = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0;
287
	return sprintf(
288
		'<div class="wp-story-pagination wp-story-pagination-bullets">
289
			%s
290
		</div>',
291
		join( "\n", array_map( __NAMESPACE__ . '\render_pagination_bullet', range( 1, $slide_count ) ) )
292
	);
293
}
294
295
/**
296
 * Render story block
297
 *
298
 * @param array $attributes  Block attributes.
299
 *
300
 * @return string
301
 */
302
function render_block( $attributes ) {
303
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
304
305
	$media_files              = isset( $attributes['mediaFiles'] ) ? $attributes['mediaFiles'] : array();
306
	$settings_from_attributes = isset( $attributes['settings'] ) ? $attributes['settings'] : array();
307
308
	$settings = array_merge(
309
		$settings_from_attributes,
310
		array(
311
			'slides' => with_width_height_srcset_and_sizes( $media_files ),
312
		)
313
	);
314
315
	return sprintf(
316
		'<div class="%1$s" data-settings="%2$s">
317
			<div style="display: contents;">
318
				<div class="wp-story-container">
319
					<div class="wp-story-meta">
320
						<div class="wp-story-icon">
321
							<img alt="%3$s" src="%4$s" width="32" height=32>
322
						</div>
323
						<div>
324
							<div class="wp-story-title">
325
								%5$s
326
							</div>
327
						</div>
328
						<button class="wp-story-exit-fullscreen jetpack-mdc-icon-button">
329
							<i class="jetpack-material-icons close md-24"></i>
330
						</button>
331
					</div>
332
					<div class="wp-story-wrapper">
333
						%6$s
334
					</div>
335
					<div role="button" class="wp-story-overlay wp-story-clickable">
336
						%7$s
337
					</div>
338
					%8$s
339
				</div>
340
			</div>
341
		</div>',
342
		esc_attr( Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ),
343
		filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ),
344
		__( 'Site icon', 'jetpack' ),
345
		esc_attr( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ),
346
		esc_html( get_the_title() ),
347
		! empty( $media_files[0] ) ? render_slide( $media_files[0] ) : '',
348
		render_top_right_icon( $settings ),
349
		render_pagination( $settings )
350
	);
351
}
352