Completed
Push — try/giphy ( 27491b...091c70 )
by
unknown
23:59 queued 16:36
created

blocks.php ➔ jetpack_gif_block_render()   B

Complexity

Conditions 9
Paths 160

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 160
nop 1
dl 0
loc 36
rs 7.5555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Load code specific to Gutenberg blocks which are not tied to a module.
4
 * This file is unusual, and is not an actual `module` as such.
5
 * It is included in ./module-extras.php
6
 */
7
8
jetpack_register_block(
9
	'gif',
10
	array(
11
		'render_callback' => 'jetpack_gif_block_render',
12
	)
13
);
14
15
jetpack_register_block(
16
	'map',
17
	array(
18
		'render_callback' => 'jetpack_map_block_load_assets',
19
	)
20
);
21
22
jetpack_register_block( 'vr' );
23
24
/**
25
 * Tiled Gallery block. Depends on the Photon module.
26
 *
27
 * @since 6.9.0
28
*/
29
if (
30
	( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
31
	class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' )
32
) {
33
	jetpack_register_block(
34
		'tiled-gallery',
35
		array(
36
			'render_callback' => 'jetpack_tiled_gallery_load_block_assets',
37
		)
38
	);
39
40
	/**
41
	 * Tiled gallery block registration/dependency declaration.
42
	 *
43
	 * @param array  $attr - Array containing the block attributes.
44
	 * @param string $content - String containing the block content.
45
	 *
46
	 * @return string
47
	 */
48
	function jetpack_tiled_gallery_load_block_assets( $attr, $content ) {
49
		$dependencies = array(
50
			'lodash',
51
			'wp-i18n',
52
			'wp-token-list',
53
		);
54
		Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery', $dependencies );
55
56
		/**
57
		 * Filter the output of the Tiled Galleries content.
58
		 *
59
		 * @module tiled-gallery
60
		 *
61
		 * @since 6.9.0
62
		 *
63
		 * @param string $content Tiled Gallery block content.
64
		 */
65
		return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
66
	}
67
}
68
69
/**
70
 * Gif block registration/dependency declaration.
71
 *
72
 * @param array $attr - Array containing the map block attributes.
73
 *
74
 * @return string
75
 */
76
function jetpack_gif_block_render( $attr ) {
77
	$align       = isset( $attr['align'] ) ? $attr['align'] : 'center';
78
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
79
	$style       = 'padding-top:' . $padding_top;
80
	$giphy_url   = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
81
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
82
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
83
84
	if ( ! $giphy_url ) {
85
		return null;
86
	}
87
88
	$classes = array(
89
		'wp-block-jetpack-gif',
90
		'align' . $align,
91
	);
92
	if ( isset( $attr['className'] ) ) {
93
		array_push( $classes, $attr['className'] );
94
	}
95
96
	ob_start();
97
	?>
98
	<div class="<?php echo esc_attr( implode( $classes, ' ' ) ); ?>">
99
		<figure style="<?php echo esc_attr( $style ); ?>">
100
			<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
101
		</figure>
102
		<?php if ( $caption ) : ?>
103
			<p class="wp-block-jetpack-gif-caption"><?php echo wp_kses_post( $caption ); ?></p>
104
		<?php endif; ?>
105
	</div>
106
	<?php
107
	$html = ob_get_clean();
108
109
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
110
	return $html;
111
}
112
113
/**
114
 * Map block registration/dependency declaration.
115
 *
116
 * @param array  $attr - Array containing the map block attributes.
117
 * @param string $content - String containing the map block content.
118
 *
119
 * @return string
120
 */
121
function jetpack_map_block_load_assets( $attr, $content ) {
122
	$dependencies = array(
123
		'lodash',
124
		'wp-element',
125
		'wp-i18n',
126
	);
127
128
	$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
129
130
	Jetpack_Gutenberg::load_assets_as_required( 'map', $dependencies );
131
	return preg_replace( '/<div /', '<div data-api-key="'. esc_attr( $api_key ) .'" ', $content, 1 );
132
}
133