Completed
Push — story-block/fix/external-media... ( 889c72...fedbcf )
by
unknown
47:02 queued 35:38
created

story.php ➔ render_image()   C

Complexity

Conditions 13
Paths 35

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 35
nop 1
dl 0
loc 45
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 with extra information we can retrieve from the media library.
59
 * Add missing `width`, `height`, `srcset` and `sizes` properties to images of the mediaFiles block attributes.
60
 * Add missing `width`, `height`, `poster` properties to videos of the mediaFiles block attributes.
61
 *
62
 * @param array $media_files  - List of media, each as an array containing the media attributes.
63
 *
64
 * @returns array $media_files
65
 */
66
function enrich_media_files( $media_files ) {
67
	return array_filter(
68
		array_map(
69
			function ( $media_file ) {
70
				$attachment_id = isset( $media_file['id'] ) ? $media_file['id'] : null;
71
				if ( 'image' === $media_file['type'] ) {
72
					$image = wp_get_attachment_image_src( $attachment_id, 'full', false );
73
					if ( ! $image ) {
74
						return $media_file;
75
					}
76
					list( $src, $width, $height ) = $image;
77
					// Bail if url stored in block attributes is different than the media library one for that id.
78
					if ( isset( $media_file['url'] ) && ! is_same_resource( $media_file['url'], $src ) ) {
79
						return $media_file;
80
					}
81
					$image_meta = wp_get_attachment_metadata( $attachment_id );
82
					if ( ! is_array( $image_meta ) ) {
83
						return $media_file;
84
					}
85
					$size_array = array( absint( $width ), absint( $height ) );
86
					return array_merge(
87
						$media_file,
88
						array(
89
							'width'   => absint( $width ),
90
							'height'  => absint( $height ),
91
							'srcset'  => wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ),
92
							'sizes'   => IMAGE_BREAKPOINTS,
93
							'title'   => get_the_title( $attachment_id ),
94
							'alt'     => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
95
							'caption' => wp_get_attachment_caption( $attachment_id ),
96
						)
97
					);
98
				}
99
				// VideoPress videos can sometimes have type 'file', and mime 'video/videopress' or 'video/mp4'.
100
				// Let's fix `type` for those.
101
				if ( 'file' === $media_file['type'] && 'video' === substr( $media_file['mime'], 0, 5 ) ) {
102
					$media_file['type'] = 'video';
103
				}
104
				if ( 'video' !== $media_file['type'] ) { // we only support images and videos at this point.
105
					return null;
106
				}
107
				$video_meta = wp_get_attachment_metadata( $attachment_id );
108
				if ( ! $video_meta ) {
109
					return $media_file;
110
				}
111
112
				$video_url = ! empty( $video_meta['original']['url'] ) ? $video_meta['original']['url'] : $media_file['url'];
113
114
				// Set the poster attribute for the video tag if a poster image is available.
115
				$poster_url = null;
116
				if ( ! empty( $video_meta['videopress']['poster'] ) ) {
117
					$poster_url = $video_meta['videopress']['poster'];
118
				} elseif ( ! empty( $video_meta['thumb'] ) ) {
119
					$poster_url = str_replace( wp_basename( $video_url ), $video_meta['thumb'], $video_url );
120
				}
121
122
				if ( $poster_url ) {
123
					$poster_width  = ! empty( $media_file['width'] ) ? $media_file['width'] : EMBED_SIZE[0];
124
					$poster_height = ! empty( $media_file['height'] ) ? $media_file['height'] : EMBED_SIZE[1];
125
					$content_width = (int) Jetpack::get_content_width();
126
					if ( 0 === $content_width ) {
127
						$content_width = EMBED_SIZE[0];
128
					}
129
					$poster_height = round( ( $content_width * $poster_height ) / $poster_width );
130
					$poster_width  = $content_width;
131
					$poster_url    = add_query_arg( 'resize', rawurlencode( $poster_width . ',' . $poster_height ), $poster_url );
132
				}
133
134
				$media_file = array_merge(
135
					$media_file,
136
					array(
137
						'width'   => absint( ! empty( $video_meta['width'] ) ? $video_meta['width'] : $media_file['width'] ),
138
						'height'  => absint( ! empty( $video_meta['height'] ) ? $video_meta['height'] : $media_file['height'] ),
139
						'alt'     => ! empty( $video_meta['videopress']['description'] ) ? $video_meta['videopress']['description'] : $media_file['alt'],
140
						'url'     => $video_url,
141
						'title'   => get_the_title( $attachment_id ),
142
						'caption' => wp_get_attachment_caption( $attachment_id ),
143
						'poster'  => $poster_url,
144
					)
145
				);
146
147
				return $media_file;
148
			},
149
			$media_files
150
		)
151
	);
152
}
153
154
/**
155
 * Render an image inside a slide
156
 *
157
 * @param array $media  - Image information.
158
 *
159
 * @returns string
160
 */
161
function render_image( $media ) {
162
	if ( empty( $media['id'] ) || empty( $media['url'] ) ) {
163
		return __( 'Error retrieving media', 'jetpack' );
164
	}
165
	$image = wp_get_attachment_image_src( $media['id'], 'full', false );
166
	if ( $image ) {
167
		list( $src, $width, $height ) = $image;
168
	}
169
170
	// if image does not match.
171
	if ( ! $image || isset( $media['url'] ) && ! is_same_resource( $media['url'], $src ) ) {
0 ignored issues
show
Bug introduced by
The variable $src does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
172
		$width  = isset( $media['width'] ) ? $media['width'] : null;
173
		$height = isset( $media['height'] ) ? $media['height'] : null;
174
		$title  = isset( $media['title'] ) ? $media['title'] : '';
175
		$alt    = isset( $media['alt'] ) ? $media['alt'] : '';
176
		return sprintf(
177
			'<img
178
				title="%1$s"
179
				alt="%2$s"
180
				class="wp-block-jetpack-story_image wp-story-image %3$s"
181
				src="%4$s"
182
			/>',
183
			esc_attr( $title ),
184
			esc_attr( $alt ),
185
			$width && $height ? get_image_crop_class( $width, $height ) : '',
186
			esc_attr( $media['url'] )
187
		);
188
	}
189
190
	$crop_class = get_image_crop_class( $width, $height );
0 ignored issues
show
Bug introduced by
The variable $height does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $width does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
191
	// need to specify the size of the embed so it picks an image that is large enough for the `src` attribute
192
	// `sizes` is optimized for 1080x1920 (9:16) images
193
	// Note that the Story block does not have thumbnail support, it will load the right
194
	// image based on the viewport size only.
195
	return wp_get_attachment_image(
196
		$media['id'],
197
		EMBED_SIZE,
198
		false,
199
		array(
200
			'class' => sprintf( 'wp-story-image wp-image-%d %s', $media['id'], $crop_class ),
201
			'sizes' => IMAGE_BREAKPOINTS,
202
			'title' => get_the_title( $media['id'] ),
203
		)
204
	);
205
}
206
207
/**
208
 * Return the css crop class if image width and height requires it
209
 *
210
 * @param int $width   - Image width.
211
 * @param int $height  - Image height.
212
 *
213
 * @returns string The CSS class which will display a cropped image
214
 */
215
function get_image_crop_class( $width, $height ) {
216
	$crop_class          = '';
217
	$media_aspect_ratio  = $width / $height;
218
	$target_aspect_ratio = EMBED_SIZE[0] / EMBED_SIZE[1];
219
	if ( $media_aspect_ratio >= $target_aspect_ratio ) {
220
		// image wider than canvas.
221
		$media_too_wide_to_crop = $media_aspect_ratio > $target_aspect_ratio / ( 1 - CROP_UP_TO );
222
		if ( ! $media_too_wide_to_crop ) {
223
			$crop_class = 'wp-story-crop-wide';
224
		}
225
	} else {
226
		// image narrower than canvas.
227
		$media_too_narrow_to_crop = $media_aspect_ratio < $target_aspect_ratio * ( 1 - CROP_UP_TO );
228
		if ( ! $media_too_narrow_to_crop ) {
229
			$crop_class = 'wp-story-crop-narrow';
230
		}
231
	}
232
	return $crop_class;
233
}
234
235
/**
236
 * Render a video inside a slide
237
 *
238
 * @param array $media  - Video information.
239
 *
240
 * @returns string
241
 */
242
function render_video( $media ) {
243
	if ( empty( $media['id'] ) || empty( $media['mime'] ) || empty( $media['url'] ) ) {
244
		return __( 'Error retrieving media', 'jetpack' );
245
	}
246
247
	if ( ! empty( $media['poster'] ) ) {
248
		return render_image(
249
			array_merge(
250
				$media,
251
				array(
252
					'type' => 'image',
253
					'url'  => $media['poster'],
254
				)
255
			)
256
		);
257
	}
258
259
	return sprintf(
260
		'<video
261
			title="%1$s"
262
			type="%2$s"
263
			class="wp-story-video intrinsic-ignore wp-video-%3$s"
264
			data-id="%3$s"
265
			src="%4$s">
266
		</video>',
267
		esc_attr( get_the_title( $media['id'] ) ),
268
		esc_attr( $media['mime'] ),
269
		$media['id'],
270
		esc_attr( $media['url'] )
271
	);
272
}
273
274
/**
275
 * Render a static embed story picking a thumbnail
276
 *
277
 * @param array $media_files  - list of Media files.
278
 *
279
 * @returns string
280
 */
281
function render_static_slide( $media_files ) {
282
	$media_template = '';
283
	if ( empty( $media_files ) ) {
284
		return '';
285
	}
286
287
	// find an image to showcase.
288
	foreach ( $media_files as $media ) {
289
		switch ( $media['type'] ) {
290
			case 'image':
291
				$media_template = render_image( $media );
292
				break 2;
293
			case 'video':
294
				// ignore videos without a poster image.
295
				if ( empty( $media['poster'] ) ) {
296
					continue 2;
297
				}
298
				$media_template = render_video( $media );
299
				break 2;
300
		}
301
	}
302
303
	// if no media was found try to render a video tag without poster.
304
	if ( empty( $media_template ) ) {
305
		foreach ( $media_files as $media ) {
306
			switch ( $media['type'] ) {
307
				case 'video':
308
					$media_template = render_video( $media );
309
					break 2;
310
			}
311
		}
312
	}
313
314
	return sprintf(
315
		'<div class="wp-story-slide" style="display: block;">
316
			<figure>%s</figure>
317
		</div>',
318
		$media_template
319
	);
320
}
321
322
/**
323
 * Render the top right icon on top of the story embed
324
 *
325
 * @param array $settings  - The block settings.
326
 *
327
 * @returns string
328
 */
329
function render_top_right_icon( $settings ) {
330
	$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false;
331
	$slide_count      = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0;
332
	if ( $show_slide_count ) {
333
		// Render the story block icon along with the slide count.
334
		return sprintf(
335
			'<div class="wp-story-embed-icon">
336
				<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false">
337
					<path d="M0 0h24v24H0z" fill="none"></path>
338
					<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>
339
				</svg>
340
				<span>%d</span>
341
			</div>',
342
			$slide_count
343
		);
344
	} else {
345
		// Render the Fullscreen Gridicon.
346
		return (
347
			'<div class="wp-story-embed-icon-expand">
348
				<svg class="gridicon gridicons-fullscreen" role="img" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
349
					<g>
350
						<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>
351
					</g>
352
				</svg>
353
			</div>'
354
		);
355
	}
356
}
357
358
/**
359
 * Render a pagination bullet
360
 *
361
 * @param int    $slide_index  - The slide index it corresponds to.
362
 * @param string $class_name   - Optional css class name(s) to customize the bullet element.
363
 *
364
 * @returns string
365
 */
366
function render_pagination_bullet( $slide_index, $class_name = '' ) {
367
	return sprintf(
368
		'<div class="wp-story-pagination-bullet %s" aria-label="%s">
369
			<div class="wp-story-pagination-bullet-bar"></div>
370
		</div>',
371
		esc_attr( $class_name ),
372
		/* translators: %d is the slide number (1, 2, 3...) */
373
		sprintf( __( 'Go to slide %d', 'jetpack' ), $slide_index )
374
	);
375
}
376
377
/**
378
 * Render pagination on top of the story embed
379
 *
380
 * @param array $settings  - The block settings.
381
 *
382
 * @returns string
383
 */
384
function render_pagination( $settings ) {
385
	$show_slide_count = isset( $settings['showSlideCount'] ) ? $settings['showSlideCount'] : false;
386
	if ( $show_slide_count ) {
387
		return '';
388
	}
389
	$slide_count     = isset( $settings['slides'] ) ? count( $settings['slides'] ) : 0;
390
	$bullet_count    = min( $slide_count, MAX_BULLETS );
391
	$bullet_ellipsis = $slide_count > $bullet_count
392
		? render_pagination_bullet( $bullet_count + 1, 'wp-story-pagination-ellipsis' )
393
		: '';
394
	return sprintf(
395
		'<div class="wp-story-pagination wp-story-pagination-bullets">
396
			%s
397
		</div>',
398
		join( "\n", array_map( __NAMESPACE__ . '\render_pagination_bullet', range( 1, $bullet_count ) ) ) . $bullet_ellipsis
399
	);
400
}
401
402
/**
403
 * Render story block
404
 *
405
 * @param array $attributes  - Block attributes.
406
 *
407
 * @returns string
408
 */
409
function render_block( $attributes ) {
410
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
411
412
	$media_files              = isset( $attributes['mediaFiles'] ) ? enrich_media_files( $attributes['mediaFiles'] ) : array();
413
	$settings_from_attributes = isset( $attributes['settings'] ) ? $attributes['settings'] : array();
414
415
	$settings = array_merge(
416
		$settings_from_attributes,
417
		array(
418
			'slides' => $media_files,
419
		)
420
	);
421
422
	return sprintf(
423
		'<div class="%1$s" data-id="%2$s" data-settings="%3$s">
424
			<div class="wp-story-app">
425
				<div class="wp-story-display-contents" style="display: contents;">
426
					<a class="wp-story-container" href="%4$s" title="%5$s">
427
						<div class="wp-story-meta">
428
							<div class="wp-story-icon">
429
								<img alt="%6$s" src="%7$s" width="40" height="40">
430
							</div>
431
							<div>
432
								<div class="wp-story-title">
433
									%8$s
434
								</div>
435
							</div>
436
						</div>
437
						<div class="wp-story-wrapper">
438
							%9$s
439
						</div>
440
						<div class="wp-story-overlay">
441
							%10$s
442
						</div>
443
						%11$s
444
					</a>
445
				</div>
446
			</div>
447
		</div>',
448
		esc_attr( Blocks::classes( FEATURE_NAME, $attributes, array( 'wp-story', 'aligncenter' ) ) ),
449
		esc_attr( 'wp-story-' . get_the_ID() ),
450
		filter_var( wp_json_encode( $settings ), FILTER_SANITIZE_SPECIAL_CHARS ),
451
		get_permalink() . '?wp-story-load-in-fullscreen=true&amp;wp-story-play-on-load=true',
452
		__( 'Play story in new tab', 'jetpack' ),
453
		__( 'Site icon', 'jetpack' ),
454
		esc_attr( get_site_icon_url( 80, includes_url( 'images/w-logo-blue.png' ) ) ),
455
		esc_html( get_the_title() ),
456
		render_static_slide( $media_files ),
457
		render_top_right_icon( $settings ),
458
		render_pagination( $settings )
459
	);
460
}
461