Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

gif.php ➔ render_block()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 80
nop 1
dl 0
loc 44
rs 7.6604
c 0
b 0
f 0
1
<?php
2
/**
3
 * GIF Block.
4
 *
5
 * @since 7.0.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Gif;
11
12
use Jetpack_AMP_Support;
13
use Jetpack_Gutenberg;
14
15
const FEATURE_NAME = 'gif';
16
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
17
18
/**
19
 * Registers the block for use in Gutenberg
20
 * This is done via an action so that we can disable
21
 * registration if we need to.
22
 */
23
function register_block() {
24
	jetpack_register_block(
25
		BLOCK_NAME,
26
		array( 'render_callback' => __NAMESPACE__ . '\render_block' )
27
	);
28
}
29
add_action( 'init', __NAMESPACE__ . '\register_block' );
30
31
/**
32
 * Gif block registration/dependency declaration.
33
 *
34
 * @param array $attr - Array containing the gif block attributes.
35
 *
36
 * @return string
37
 */
38
function render_block( $attr ) {
39
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
40
	$style       = 'padding-top:' . $padding_top;
41
	$giphy_url   = isset( $attr['giphyUrl'] )
42
		? Jetpack_Gutenberg::validate_block_embed_url( $attr['giphyUrl'], array( 'giphy.com' ) )
43
		: null;
44
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
45
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
46
47
	if ( ! $giphy_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $giphy_url of type string|null 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...
48
		return null;
49
	}
50
51
	$classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr );
52
53
	$placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) );
54
55
	ob_start();
56
	?>
57
	<div class="<?php echo esc_attr( $classes ); ?>">
58
		<figure>
59
			<?php if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) : ?>
60
				<amp-iframe src="<?php echo esc_url( $giphy_url ); ?>" width="100" height="<?php echo absint( $padding_top ); ?>" sandbox="allow-scripts allow-same-origin" layout="responsive">
61
					<div placeholder>
62
						<?php echo wp_kses_post( $placeholder ); ?>
63
					</div>
64
				</amp-iframe>
65
			<?php else : ?>
66
				<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
67
					<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
68
				</div>
69
			<?php endif; ?>
70
			<?php if ( $caption ) : ?>
71
				<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
72
			<?php endif; ?>
73
		</figure>
74
	</div>
75
	<?php
76
	$html = ob_get_clean();
77
78
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
79
80
	return $html;
81
}
82