Completed
Push — add/videopress-gutenberg-exten... ( 0f8e96...4eb8c1 )
by
unknown
10:28 queued 13s
created

videopress-gutenberg-extension.php ➔ jetpack_register_videopress_extension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
jetpack_register_extension( 'videopress', 'jetpack_register_videopress_extension' );
4
5
/**
6
 * Register the Jetpack Gutenberg extension that adds VideoPress support to the Core video block.
7
 */
8
function jetpack_register_videopress_extension() {
9
	register_block_type( 'core/video', array(
10
		'render_callback' => 'jetpack_render_block_core_video_with_videopress',
11
	) );
12
}
13
14
/**
15
 * Render the Core video block replacing the src attribute with the VideoPress URL
16
 *
17
 * @param array  $attributes Array containing the video block attributes.
18
 * @param string $content    String containing the video block content.
19
 *
20
 * @return string
21
 */
22
function jetpack_render_block_core_video_with_videopress( $attributes, $content ) {
23
	if ( ! isset( $attributes['id'] ) ) {
24
		return $content;
25
	}
26
27
	$videopress_url = videopress_get_attachment_url( $attributes['id'] );
28
29
	if ( ! $videopress_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $videopress_url of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30
		return $content;
31
	}
32
33
	return preg_replace(
34
		'/src="([^"]+)/',
35
		sprintf(
36
			'src="%1$s',
37
			esc_attr( $videopress_url )
38
		),
39
		$content,
40
		1
41
	);
42
}