Completed
Push — recurring-payments/minimum-tra... ( a4df71...65f413 )
by
unknown
133:55 queued 127:48
created

gif.php ➔ jetpack_gif_block_render()   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
jetpack_register_block(
11
	'jetpack/gif',
12
	array(
13
		'render_callback' => 'jetpack_gif_block_render',
14
	)
15
);
16
17
/**
18
 * Gif block registration/dependency declaration.
19
 *
20
 * @param array $attr - Array containing the gif block attributes.
21
 *
22
 * @return string
23
 */
24
function jetpack_gif_block_render( $attr ) {
25
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
26
	$style       = 'padding-top:' . $padding_top;
27
	$giphy_url   = isset( $attr['giphyUrl'] )
28
		? Jetpack_Gutenberg::validate_block_embed_url( $attr['giphyUrl'], array( 'giphy.com' ) )
29
		: null;
30
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
31
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
32
33
	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...
34
		return null;
35
	}
36
37
	$classes = Jetpack_Gutenberg::block_classes( 'gif', $attr );
38
39
	$placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) );
40
41
	ob_start();
42
	?>
43
	<div class="<?php echo esc_attr( $classes ); ?>">
44
		<figure>
45
			<?php if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) : ?>
46
				<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">
47
					<div placeholder>
48
						<?php echo wp_kses_post( $placeholder ); ?>
49
					</div>
50
				</amp-iframe>
51
			<?php else : ?>
52
				<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
53
					<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
54
				</div>
55
			<?php endif; ?>
56
			<?php if ( $caption ) : ?>
57
				<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
58
			<?php endif; ?>
59
		</figure>
60
	</div>
61
	<?php
62
	$html = ob_get_clean();
63
64
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
65
66
	return $html;
67
}
68