Completed
Push — update/block-loading-split ( 14d411 )
by Jeremy
07:04
created

gif.php ➔ jetpack_gif_block_render()   B

Complexity

Conditions 9
Paths 160

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 160
nop 1
dl 0
loc 41
rs 7.3084
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
	'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
	$align       = isset( $attr['align'] ) ? $attr['align'] : 'center';
26
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
27
	$style       = 'padding-top:' . $padding_top;
28
	$giphy_url   = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
29
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
30
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
31
32
	if ( ! $giphy_url ) {
33
		return null;
34
	}
35
36
	$classes = array(
37
		'wp-block-jetpack-gif',
38
		'align' . $align,
39
	);
40
	if ( isset( $attr['className'] ) ) {
41
		array_push( $classes, $attr['className'] );
42
	}
43
44
	ob_start();
45
	?>
46
	<div class="<?php echo esc_attr( implode( $classes, ' ' ) ); ?>">
47
		<figure>
48
			<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
49
				<iframe src="<?php echo esc_url( $giphy_url ); ?>"
50
						title="<?php echo esc_attr( $search_text ); ?>"></iframe>
51
			</div>
52
			<?php if ( $caption ) : ?>
53
				<figcaption
54
						class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
55
			<?php endif; ?>
56
		</figure>
57
	</div>
58
	<?php
59
	$html = ob_get_clean();
60
61
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
62
63
	return $html;
64
}
65