Completed
Push — add/new-disconnect-dialog ( b4649f...72298c )
by
unknown
23:43 queued 16:44
created

gif.php ➔ jetpack_gif_block_render()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 80
nop 1
dl 0
loc 42
rs 7.6924
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'] ) ? $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
	$classes = Jetpack_Gutenberg::block_classes( 'gif', $attr );
36
37
	$placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) );
38
39
	ob_start();
40
	?>
41
	<div class="<?php echo esc_attr( $classes ); ?>">
42
		<figure>
43
			<?php if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) : ?>
44
				<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">
45
					<div placeholder>
46
						<?php echo wp_kses_post( $placeholder ); ?>
47
					</div>
48
				</amp-iframe>
49
			<?php else : ?>
50
				<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
51
					<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
52
				</div>
53
			<?php endif; ?>
54
			<?php if ( $caption ) : ?>
55
				<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
56
			<?php endif; ?>
57
		</figure>
58
	</div>
59
	<?php
60
	$html = ob_get_clean();
61
62
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
63
64
	return $html;
65
}
66