Completed
Push — try/wp-client-i18n ( 5ca50e...f7e161 )
by Jeremy
26:36 queued 16:52
created

blocks.php ➔ jetpack_gif_block_render()   B

Complexity

Conditions 9
Paths 160

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 160
nop 1
dl 0
loc 38
rs 7.3564
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
/**
9
 * Map block.
10
 *
11
 * @since 6.8.0
12
 */
13
jetpack_register_block(
14
	'map',
15
	array(
16
		'render_callback' => 'jetpack_map_block_load_assets',
17
	)
18
);
19
20
/**
21
 * Map block registration/dependency declaration.
22
 *
23
 * @param array  $attr - Array containing the map block attributes.
24
 * @param string $content - String containing the map block content.
25
 *
26
 * @return string
27
 */
28
function jetpack_map_block_load_assets( $attr, $content ) {
29
	$dependencies = array(
30
		'lodash',
31
		'wp-element',
32
		'wp-i18n',
33
	);
34
35
	$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
36
37
	Jetpack_Gutenberg::load_assets_as_required( 'map', $dependencies );
38
	return preg_replace( '/<div /', '<div data-api-key="'. esc_attr( $api_key ) .'" ', $content, 1 );
39
}
40
41
/**
42
 * Tiled Gallery block. Depends on the Photon module.
43
 *
44
 * @since 6.9.0
45
*/
46
if (
47
	( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
48
	class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' )
49
) {
50
	jetpack_register_block(
51
		'tiled-gallery',
52
		array(
53
			'render_callback' => 'jetpack_tiled_gallery_load_block_assets',
54
		)
55
	);
56
57
	/**
58
	 * Tiled gallery block registration/dependency declaration.
59
	 *
60
	 * @param array $attr - Array containing the block attributes.
61
	 * @param string $content - String containing the block content.
62
	 *
63
	 * @return string
64
	 */
65
	function jetpack_tiled_gallery_load_block_assets( $attr, $content ) {
66
		$dependencies = array(
67
			'lodash',
68
			'wp-i18n',
69
			'wp-token-list',
70
		);
71
		Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery', $dependencies );
72
73
		/**
74
		 * Filter the output of the Tiled Galleries content.
75
		 *
76
		 * @module tiled-gallery
77
		 *
78
		 * @since 6.9.0
79
		 *
80
		 * @param string $content Tiled Gallery block content.
81
		 */
82
		return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
83
	}
84
}
85
86
/**
87
 * GIF Block.
88
 *
89
 * @since 7.0.0
90
 */
91
jetpack_register_block(
92
	'gif',
93
	array(
94
		'render_callback' => 'jetpack_gif_block_render',
95
	)
96
);
97
98
/**
99
 * Gif block registration/dependency declaration.
100
 *
101
 * @param array $attr - Array containing the map block attributes.
102
 *
103
 * @return string
104
 */
105
function jetpack_gif_block_render( $attr ) {
106
	$align       = isset( $attr['align'] ) ? $attr['align'] : 'center';
107
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
108
	$style       = 'padding-top:' . $padding_top;
109
	$giphy_url   = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
110
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
111
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
112
113
	if ( ! $giphy_url ) {
114
		return null;
115
	}
116
117
	$classes = array(
118
		'wp-block-jetpack-gif',
119
		'align' . $align,
120
	);
121
	if ( isset( $attr['className'] ) ) {
122
		array_push( $classes, $attr['className'] );
123
	}
124
125
	ob_start();
126
	?>
127
	<div class="<?php echo esc_attr( implode( $classes, ' ' ) ); ?>">
128
		<figure>
129
			<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
130
				<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
131
			</div>
132
			<?php if ( $caption ) : ?>
133
				<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
134
			<?php endif; ?>
135
		</figure>
136
	</div>
137
	<?php
138
	$html = ob_get_clean();
139
140
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
141
	return $html;
142
}
143
144
/**
145
 * Contact Info block and its child blocks.
146
 */
147
jetpack_register_block( 'contact-info' );
148
jetpack_register_block(
149
	'email',
150
	array( 'parent' => array( 'jetpack/contact-info' ) )
151
);
152
jetpack_register_block(
153
	'address',
154
	array( 'parent' => array( 'jetpack/contact-info' ) )
155
);
156
jetpack_register_block(
157
	'phone',
158
	array( 'parent' => array( 'jetpack/contact-info' ) )
159
);
160
161
/**
162
 * VR Block.
163
 */
164
jetpack_register_block( 'vr' );
165
166
/**
167
 * Slideshow Block.
168
 */
169
jetpack_register_block(
170
	'slideshow',
171
	array(
172
		'render_callback' => 'jetpack_slideshow_block_load_assets',
173
	)
174
);
175
176
/**
177
 * Slideshow block registration/dependency declaration.
178
 *
179
 * @param array  $attr - Array containing the map block attributes.
180
 * @param string $content - String containing the map block content.
181
 *
182
 * @return string
183
 */
184
function jetpack_slideshow_block_load_assets( $attr, $content ) {
185
	$dependencies = array(
186
		'lodash',
187
		'wp-element',
188
		'wp-i18n',
189
	);
190
	Jetpack_Gutenberg::load_assets_as_required( 'slideshow', $dependencies );
191
	return $content;
192
}
193