Completed
Push — update/story-block-loading-med... ( a5ea27...f88aa5 )
by
unknown
21:52 queued 13:26
created

story.php ➔ render_image()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 1
dl 0
loc 38
rs 8.3786
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 story block
189
 *
190
 * @param array $attributes  Block attributes.
191
 *
192
 * @return string
193
 */
194
function render_block( $attributes ) {
195
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
196
197
	$media_files              = isset( $attributes['mediaFiles'] ) ? $attributes['mediaFiles'] : array();
198
	$settings_from_attributes = isset( $attributes['settings'] ) ? $attributes['settings'] : array();
199
200
	$settings = array_merge(
201
		$settings_from_attributes,
202
		array(
203
			'slides' => with_width_height_srcset_and_sizes( $media_files ),
204
		)
205
	);
206
207
	return sprintf(
208
		'<div class="%1$s" data-settings="%2$s">
209
			<div style="display: contents;">
210
				<div class="wp-story-container">
211
					<div class="wp-story-meta">
212
						<div class="wp-story-icon">
213
							<img alt="%3$s" src="%4$s" width="32" height=32>
214
						</div>
215
						<div>
216
							<div class="wp-story-title">
217
								%5$s
218
							</div>
219
						</div>
220
						<button class="wp-story-exit-fullscreen jetpack-mdc-icon-button">
221
							<i class="jetpack-material-icons close md-24"></i>
222
						</button>
223
					</div>
224
					<div class="wp-story-wrapper">
225
						%6$s
226
					</div>
227
					<div role="button" class="wp-story-overlay wp-story-clickable">
228
						<div class="wp-story-embed-icon">
229
							<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">
230
								<path d="M0 0h24v24H0z" fill="none"></path>
231
								<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>
232
							</svg>
233
							<span>%7$s</span>
234
						</div>
235
					</div>
236
				</div>
237
			</div>
238
		</div>',
239
		esc_attr( Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ),
240
		filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ),
241
		__( 'Site icon', 'jetpack' ),
242
		esc_attr( get_site_icon_url( 32, includes_url( 'images/w-logo-blue.png' ) ) ),
243
		esc_html( get_the_title() ),
244
		! empty( $media_files[0] ) ? render_slide( $media_files[0] ) : '',
245
		count( $media_files )
246
	);
247
}
248