Completed
Push — start/7.4-changelog ( b9f2cf...cfa140 )
by Jeremy
49:24 queued 39:50
created

gif.php ➔ jetpack_gif_block_render()   C

Complexity

Conditions 11
Paths 272

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 272
nop 1
dl 0
loc 51
rs 5.5357
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	/* 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
	$placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) );
47
48
	ob_start();
49
	?>
50
	<div class="<?php echo esc_attr( $classes ); ?>">
51
		<figure>
52
			<?php if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) : ?>
53
				<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">
54
					<div placeholder>
55
						<?php echo wp_kses_post( $placeholder ); ?>
56
					</div>
57
				</amp-iframe>
58
			<?php else : ?>
59
				<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
60
					<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
61
				</div>
62
			<?php endif; ?>
63
			<?php if ( $caption ) : ?>
64
				<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
65
			<?php endif; ?>
66
		</figure>
67
	</div>
68
	<?php
69
	$html = ob_get_clean();
70
71
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
72
73
	return $html;
74
}
75