Completed
Push — update/story-block-loading-med... ( f88aa5...aaf741 )
by
unknown
08:20
created

story.php ➔ render_pagination_bullet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
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, 'full', 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
				return array_merge(
74
					$media_file,
75
					array(
76
						'width'  => absint( $video_meta['width'] ),
77
						'height' => absint( $video_meta['height'] ),
78
					)
79
				);
80
			}
81
		},
82
		$media_files
83
	);
84
}
85
86
/**
87
 * Render an image inside a slide
88
 *
89
 * @param array $media  Image information.
90
 *
91
 * @return string
92
 */
93
function render_image( $media ) {
94
	if ( empty( $media['id'] ) || empty( $media['url'] ) ) {
95
		return __( 'Error retrieving media', 'jetpack' );
96
	}
97
	$image      = wp_get_attachment_image_src( $media['id'], EMBED_SIZE, false );
98
	$crop_class = '';
99
	if ( $image ) {
100
		list( , $width, $height ) = $image;
101
		$media_aspect_ratio       = $width / $height;
102
		$target_aspect_ratio      = EMBED_SIZE[0] / EMBED_SIZE[1];
103
		if ( $media_aspect_ratio >= $target_aspect_ratio ) {
104
			// image wider than canvas.
105
			$media_too_wide_to_crop = $media_aspect_ratio > $target_aspect_ratio / ( 1 - CROP_UP_TO );
106
			if ( ! $media_too_wide_to_crop ) {
107
				$crop_class = 'wp-story-crop-wide';
108
			}
109
		} else {
110
			// image narrower than canvas.
111
			$media_too_narrow_to_crop = $media_aspect_ratio < $target_aspect_ratio * ( 1 - CROP_UP_TO );
112
			if ( ! $media_too_narrow_to_crop ) {
113
				$crop_class = 'wp-story-crop-narrow';
114
			}
115
		}
116
	}
117
	// need to specify the size of the embed so it picks an image that is large enough for the `src` attribute
118
	// `sizes` is optimized for 1080x1920 (9:16) images
119
	// Note that the Story block does not have thumbnail support, it will load the right
120
	// image based on the viewport size only.
121
	return wp_get_attachment_image(
122
		$media['id'],
123
		EMBED_SIZE,
124
		false,
125
		array(
126
			'class' => sprintf( 'wp-story-image wp-image-%d %s', $media['id'], $crop_class ),
127
			'sizes' => IMAGE_BREAKPOINTS,
128
		)
129
	);
130
}
131
132
/**
133
 * Render a video inside a slide
134
 *
135
 * @param array $media  Video information.
136
 *
137
 * @return string
138
 */
139
function render_video( $media ) {
140
	if ( empty( $media['id'] ) || empty( $media['mime'] ) || empty( $media['url'] ) ) {
141
		return __( 'Error retrieving media', 'jetpack' );
142
	}
143
	return sprintf(
144
		'<video
145
			title="%1$s"
146
			type="%2$s"
147
			class="wp-story-video intrinsic-ignore wp-video-%3$s"
148
			data-id="%3$s"
149
			src="%4$s">
150
		</video>',
151
		esc_attr( $media['alt'] ),
152
		esc_attr( $media['mime'] ),
153
		$media['id'],
154
		esc_attr( $media['url'] )
155
	);
156
}
157
158
/**
159
 * Render a slide
160
 *
161
 * @param array $media  Media information.
162
 * @param array $index  Index of the slide, first slide will be displayed by default, others hidden.
163
 *
164
 * @return string
165
 */
166
function render_slide( $media, $index = 0 ) {
167
	$media_template = '';
168
	switch ( ! empty( $media['type'] ) && $media['type'] ) {
169
		case 'image':
170
			$media_template = render_image( $media, $index );
171
			break;
172
		case 'video':
173
			$media_template = render_video( $media, $index );
174
			break;
175
	}
176
	return sprintf(
177
		'<div class="wp-story-slide" style="display: %s;">
178
			<figure>
179
				%s
180
			</figure>
181
		</div>',
182
		0 === $index ? 'block' : 'none',
183
		$media_template
184
	);
185
}
186
187
/**
188
 * Render the top right icon on top of the story embed
189
 *
190
 * @param array $settings The block settings.
191
 *
192
 * @return string
193
 */
194
function render_top_right_icon( $settings ) {
195
	$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false;
196
	$slide_count      = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0;
197
	if ( $show_slide_count ) {
198
		// Render the story block icon along with the slide count.
199
		return sprintf(
200
			'<div class="wp-story-embed-icon">
201
				<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">
202
					<path d="M0 0h24v24H0z" fill="none"></path>
203
					<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>
204
				</svg>
205
				<span>%s</span>
206
			</div>',
207
			$slide_count
208
		);
209
	} else {
210
		// Render the Fullscreen Gridicon.
211
		return (
212
			'<div class="wp-story-embed-icon-expand">
213
				<svg class="gridicon gridicons-fullscreen" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
214
					<g>
215
						<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>
216
					</g>
217
				</svg>
218
			</div>'
219
		);
220
	}
221
}
222
223
/**
224
 * Render a pagination bullet
225
 *
226
 * @param array $slide_index The slide index it corresponds to.
227
 *
228
 * @return string
229
 */
230
function render_pagination_bullet( $slide_index ) {
231
	return sprintf(
232
		'<button class="wp-story-pagination-bullet" aria-label="Go to slide %d">
233
			<div class="wp-story-pagination-bullet-bar"></div>
234
		</button>',
235
		$slide_index
236
	);
237
}
238
239
/**
240
 * Render pagination on top of the story embed
241
 *
242
 * @param array $settings The block settings.
243
 *
244
 * @return string
245
 */
246
function render_pagination( $settings ) {
247
	$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false;
248
	if ( $show_slide_count ) {
249
		return '';
250
	}
251
	$slide_count = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0;
252
	return sprintf(
253
		'<div class="wp-story-pagination wp-story-pagination-bullets">
254
			%s
255
		</div>',
256
		join( "\n", array_map( __NAMESPACE__ . '\render_pagination_bullet', range( 1, $slide_count ) ) )
257
	);
258
}
259
260
/**
261
 * Render story block
262
 *
263
 * @param array $attributes  Block attributes.
264
 *
265
 * @return string
266
 */
267
function render_block( $attributes ) {
268
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
269
270
	$media_files              = isset( $attributes['mediaFiles'] ) ? $attributes['mediaFiles'] : array();
271
	$settings_from_attributes = isset( $attributes['settings'] ) ? $attributes['settings'] : array();
272
273
	$settings = array_merge(
274
		$settings_from_attributes,
275
		array(
276
			'slides' => with_width_height_srcset_and_sizes( $media_files ),
277
		)
278
	);
279
280
	return sprintf(
281
		'<div class="%1$s" data-settings="%2$s">
282
			<div style="display: contents;">
283
				<div class="wp-story-container">
284
					<div class="wp-story-meta">
285
						<div class="wp-story-icon">
286
							<img alt="%3$s" src="%4$s" width="32" height=32>
287
						</div>
288
						<div>
289
							<div class="wp-story-title">
290
								%5$s
291
							</div>
292
						</div>
293
						<button class="wp-story-exit-fullscreen jetpack-mdc-icon-button">
294
							<i class="jetpack-material-icons close md-24"></i>
295
						</button>
296
					</div>
297
					<div class="wp-story-wrapper">
298
						%6$s
299
					</div>
300
					<div role="button" class="wp-story-overlay wp-story-clickable">
301
						%7$s
302
					</div>
303
					%8$s
304
				</div>
305
			</div>
306
		</div>',
307
		esc_attr( Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ),
308
		filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ),
309
		__( 'Site icon', 'jetpack' ),
310
		esc_attr( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ),
311
		esc_html( get_the_title() ),
312
		! empty( $media_files[0] ) ? render_slide( $media_files[0] ) : '',
313
		render_top_right_icon( $settings ),
314
		render_pagination( $settings )
315
	);
316
}
317