Completed
Push — try/giphy ( c41274...5cb1ca )
by
unknown
09:05
created

blocks.php ➔ jetpack_gif_block_load_assets()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 32
nop 1
dl 0
loc 23
rs 8.9297
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_load_assets',
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_load_assets( $attr ) {
77
	$align       = isset( $attr['align'] ) ? $attr['align'] : 'center';
78
	$style       = 'padding-top:' . $attr['paddingTop'];
79
	$giphy_url   = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : '//giphy.com/embed/ZgTR3UQ9XAWDvqy9jv';
80
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
81
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
82
83
	ob_start();
84
	?>
85
	<div class="wp-block-jetpack-gif align<?php echo esc_attr( $align ); ?>">
86
		<figure style="<?php echo esc_attr( $style ); ?>">
87
			<iframe src="<?php echo esc_attr( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
88
		</figure>
89
		<?php if ( $caption ) : ?>
90
			<p class="wp-block-jetpack-gif-caption"><?php echo wp_kses_post( $caption ); ?></p>
91
		<?php endif; ?>
92
	</div>
93
	<?php
94
	$html = ob_get_clean();
95
96
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
97
	return $html;
98
}
99
100
/**
101
 * Map block registration/dependency declaration.
102
 *
103
 * @param array  $attr - Array containing the map block attributes.
104
 * @param string $content - String containing the map block content.
105
 *
106
 * @return string
107
 */
108
function jetpack_map_block_load_assets( $attr, $content ) {
109
	$dependencies = array(
110
		'lodash',
111
		'wp-element',
112
		'wp-i18n',
113
	);
114
115
	$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
116
117
	Jetpack_Gutenberg::load_assets_as_required( 'map', $dependencies );
118
	return preg_replace( '/<div /', '<div data-api-key="'. esc_attr( $api_key ) .'" ', $content, 1 );
119
}
120