Completed
Push — update/mailchimp-ui-changes ( f4a7d6...e71283 )
by
unknown
11:27
created

gif.php ➔ jetpack_gif_block_render()   B

Complexity

Conditions 9
Paths 144

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 144
nop 1
dl 0
loc 44
rs 7.3671
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
register_block_type(
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'] ) ? $attr['giphyUrl'] : null;
28
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
29
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
30
31
	if ( ! $giphy_url ) {
32
		return null;
33
	}
34
35
	/* TODO: replace with centralized block_class function */
36
	$align   = isset( $attr['align'] ) ? $attr['align'] : 'center';
37
	$type    = 'gif';
38
	$classes = array(
39
		'wp-block-jetpack-' . $type,
40
		'align' . $align,
41
	);
42
	if ( isset( $attr['className'] ) ) {
43
		array_push( $classes, $attr['className'] );
44
	}
45
	$classes = implode( $classes, ' ' );
46
47
	ob_start();
48
	?>
49
	<div class="<?php echo esc_attr( $classes ); ?>">
50
		<figure>
51
			<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
52
				<iframe src="<?php echo esc_url( $giphy_url ); ?>"
53
						title="<?php echo esc_attr( $search_text ); ?>"></iframe>
54
			</div>
55
			<?php if ( $caption ) : ?>
56
				<figcaption
57
						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