1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handles server-side registration and use of all blocks and plugins available in Jetpack for the block editor, aka Gutenberg. |
4
|
|
|
* Works in tandem with client-side block registration via `index.json` |
5
|
|
|
* |
6
|
|
|
* @package Jetpack |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Helper function to register a Jetpack Gutenberg block |
11
|
|
|
* |
12
|
|
|
* @param string $slug Slug of the block. |
13
|
|
|
* @param array $args Arguments that are passed into register_block_type. |
14
|
|
|
* |
15
|
|
|
* @see register_block_type |
16
|
|
|
* |
17
|
|
|
* @since 6.7.0 |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
function jetpack_register_block( $slug, $args = array() ) { |
22
|
|
|
Jetpack_Gutenberg::register_block( $slug, $args ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Helper function to register a Jetpack Gutenberg plugin |
27
|
|
|
* |
28
|
|
|
* @param string $slug Slug of the plugin. |
29
|
|
|
* |
30
|
|
|
* @since 6.9.0 |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
function jetpack_register_plugin( $slug ) { |
35
|
|
|
Jetpack_Gutenberg::register_plugin( $slug ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set the reason why an extension (block or plugin) is unavailable |
40
|
|
|
* |
41
|
|
|
* @param string $slug Slug of the block. |
42
|
|
|
* @param string $reason A string representation of why the extension is unavailable. |
43
|
|
|
* |
44
|
|
|
* @since 7.0.0 |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
function jetpack_set_extension_unavailability_reason( $slug, $reason ) { |
49
|
|
|
Jetpack_Gutenberg::set_extension_unavailability_reason( $slug, $reason ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* General Gutenberg editor specific functionality |
54
|
|
|
*/ |
55
|
|
|
class Jetpack_Gutenberg { |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Only these extensions can be registered. Used to control availability of beta blocks. |
59
|
|
|
* |
60
|
|
|
* @var array Extensions whitelist |
61
|
|
|
*/ |
62
|
|
|
private static $extensions = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Keeps track of the reasons why a given extension is unavailable. |
66
|
|
|
* |
67
|
|
|
* @var array Extensions availability information |
68
|
|
|
*/ |
69
|
|
|
private static $availability = array(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Since there is no `register_plugin()` counterpart to `register_block_type()` in Gutenberg, |
73
|
|
|
* we have to keep track of plugin registration ourselves |
74
|
|
|
* |
75
|
|
|
* @var array Plugin registry |
76
|
|
|
*/ |
77
|
|
|
private static $registered_plugins = array(); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Prepend the 'jetpack/' prefix to a block name |
81
|
|
|
* |
82
|
|
|
* @param string $block_name The block name. |
83
|
|
|
* |
84
|
|
|
* @return string The prefixed block name. |
85
|
|
|
*/ |
86
|
|
|
private static function prepend_block_prefix( $block_name ) { |
87
|
|
|
return 'jetpack/' . $block_name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Whether two arrays share at least one item |
92
|
|
|
* |
93
|
|
|
* @param array $a An array. |
94
|
|
|
* @param array $b Another array. |
95
|
|
|
* |
96
|
|
|
* @return boolean True if $a and $b share at least one item |
97
|
|
|
*/ |
98
|
|
|
protected static function share_items( $a, $b ) { |
99
|
|
|
return count( array_intersect( $a, $b ) ) > 0; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Register a block |
104
|
|
|
* |
105
|
|
|
* If the block isn't whitelisted, set its unavailability reason instead. |
106
|
|
|
* |
107
|
|
|
* @param string $slug Slug of the block. |
108
|
|
|
* @param array $args Arguments that are passed into register_block_type(). |
109
|
|
|
*/ |
110
|
|
|
public static function register_block( $slug, $args ) { |
111
|
|
|
$prefixed_extensions = array_map( array( __CLASS__, 'prepend_block_prefix' ), self::$extensions ); |
112
|
|
|
|
113
|
|
|
// Register the block if it's whitelisted, or if it's a child block, and one of its parents is whitelisted. |
114
|
|
|
if ( in_array( $slug, self::$extensions, true ) || ( isset( $args['parent'] ) && self::share_items( $args['parent'], $prefixed_extensions ) ) ) { |
115
|
|
|
register_block_type( 'jetpack/' . $slug, $args ); |
116
|
|
|
} elseif ( ! isset( $args['parent'] ) ) { |
117
|
|
|
// Don't set availability information for child blocks -- we infer it from their parents. |
118
|
|
|
self::set_extension_unavailability_reason( $slug, 'not_whitelisted' ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Register a plugin |
124
|
|
|
* |
125
|
|
|
* If the plugin isn't whitelisted, set its unavailability reason instead. |
126
|
|
|
* |
127
|
|
|
* @param string $slug Slug of the plugin. |
128
|
|
|
*/ |
129
|
|
|
public static function register_plugin( $slug ) { |
130
|
|
|
if ( in_array( $slug, self::$extensions, true ) ) { |
131
|
|
|
self::$registered_plugins[] = 'jetpack-' . $slug; |
132
|
|
|
} else { |
133
|
|
|
self::set_extension_unavailability_reason( $slug, 'not_whitelisted' ); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Register a block |
139
|
|
|
* |
140
|
|
|
* @deprecated 7.0.0 Use register_block() instead |
141
|
|
|
* |
142
|
|
|
* @param string $slug Slug of the block. |
143
|
|
|
* @param array $args Arguments that are passed into the register_block_type. |
144
|
|
|
* @param array $availability array containing if a block is available and the reason when it is not. |
145
|
|
|
*/ |
146
|
|
|
public static function register( $slug, $args, $availability ) { |
147
|
|
|
if ( isset( $availability['available'] ) && ! $availability['available'] ) { |
148
|
|
|
self::set_extension_unavailability_reason( $slug, $availability['unavailable_reason'] ); |
149
|
|
|
} else { |
150
|
|
|
self::register_block( $slug, $args ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the reason why an extension (block or plugin) is unavailable |
156
|
|
|
* |
157
|
|
|
* @param string $slug Slug of the extension. |
158
|
|
|
* @param string $reason A string representation of why the extension is unavailable. |
159
|
|
|
*/ |
160
|
|
|
public static function set_extension_unavailability_reason( $slug, $reason ) { |
161
|
|
|
self::$availability[ $slug ] = $reason; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set up a whitelist of allowed block editor extensions |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public static function init() { |
170
|
|
|
if ( ! self::is_gutenberg_available() ) { |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ( ! self::should_load() ) { |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks. |
180
|
|
|
* |
181
|
|
|
* @since 6.9.0 |
182
|
|
|
* |
183
|
|
|
* @param boolean |
184
|
|
|
*/ |
185
|
|
|
if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) { |
186
|
|
|
Jetpack_Constants::set_constant( 'JETPACK_BETA_BLOCKS', true ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Filter the whitelist of block editor extensions that are available through Jetpack. |
191
|
|
|
* |
192
|
|
|
* @since 7.0.0 |
193
|
|
|
* |
194
|
|
|
* @param array |
195
|
|
|
*/ |
196
|
|
|
self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_jetpack_gutenberg_extensions_whitelist() ); |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Filter the whitelist of block editor plugins that are available through Jetpack. |
200
|
|
|
* |
201
|
|
|
* @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
202
|
|
|
* |
203
|
|
|
* @since 6.8.0 |
204
|
|
|
* |
205
|
|
|
* @param array |
206
|
|
|
*/ |
207
|
|
|
self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions ); |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Filter the whitelist of block editor plugins that are available through Jetpack. |
211
|
|
|
* |
212
|
|
|
* @deprecated 7.0.0 Use jetpack_set_available_extensions instead |
213
|
|
|
* |
214
|
|
|
* @since 6.9.0 |
215
|
|
|
* |
216
|
|
|
* @param array |
217
|
|
|
*/ |
218
|
|
|
self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Resets the class to its original state |
223
|
|
|
* |
224
|
|
|
* Used in unit tests |
225
|
|
|
* |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
public static function reset() { |
229
|
|
|
self::$extensions = array(); |
230
|
|
|
self::$availability = array(); |
231
|
|
|
self::$registered_plugins = array(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Return the Gutenberg extensions (blocks and plugins) directory |
236
|
|
|
* |
237
|
|
|
* @return string The Gutenberg extensions directory |
238
|
|
|
*/ |
239
|
|
|
public static function get_blocks_directory() { |
240
|
|
|
/** |
241
|
|
|
* Filter to select Gutenberg blocks directory |
242
|
|
|
* |
243
|
|
|
* @since 6.9.0 |
244
|
|
|
* |
245
|
|
|
* @param string default: '_inc/blocks/' |
246
|
|
|
*/ |
247
|
|
|
return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Checks for a given .json file in the blocks folder. |
252
|
|
|
* |
253
|
|
|
* @param string $preset The name of the .json file to look for. |
254
|
|
|
* |
255
|
|
|
* @return bool True if the file is found. |
256
|
|
|
*/ |
257
|
|
|
public static function preset_exists( $preset ) { |
258
|
|
|
return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Decodes JSON loaded from a preset file in the blocks folder |
263
|
|
|
* |
264
|
|
|
* @param string $preset The name of the .json file to load. |
265
|
|
|
* |
266
|
|
|
* @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
267
|
|
|
*/ |
268
|
|
|
public static function get_preset( $preset ) { |
269
|
|
|
return json_decode( file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ) ); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
274
|
|
|
* |
275
|
|
|
* @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
276
|
|
|
*/ |
277
|
|
|
public static function get_jetpack_gutenberg_extensions_whitelist() { |
278
|
|
|
$preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array(); |
279
|
|
|
|
280
|
|
|
$preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array(); |
281
|
|
|
|
282
|
|
|
if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { |
283
|
|
|
$beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array(); |
284
|
|
|
return array_unique( array_merge( $preset_extensions, $beta_extensions ) ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $preset_extensions; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get availability of each block / plugin. |
292
|
|
|
* |
293
|
|
|
* @return array A list of block and plugins and their availablity status |
294
|
|
|
*/ |
295
|
|
|
public static function get_availability() { |
296
|
|
|
$available_extensions = array(); |
297
|
|
|
|
298
|
|
|
foreach ( self::$extensions as $extension ) { |
299
|
|
|
$is_available = WP_Block_Type_Registry::get_instance()->is_registered( 'jetpack/' . $extension ) || |
300
|
|
|
in_array( 'jetpack-' . $extension, self::$registered_plugins, true ); |
301
|
|
|
|
302
|
|
|
$available_extensions[ $extension ] = array( |
303
|
|
|
'available' => $is_available, |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
if ( ! $is_available ) { |
307
|
|
|
$reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module'; |
308
|
|
|
$available_extensions[ $extension ]['unavailable_reason'] = $reason; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$unwhitelisted = array_fill_keys( |
313
|
|
|
array_diff( array_keys( self::$availability ), self::$extensions ), |
314
|
|
|
array( |
315
|
|
|
'available' => false, |
316
|
|
|
'unavailable_reason' => 'not_whitelisted', |
317
|
|
|
) |
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
return array_merge( $available_extensions, $unwhitelisted ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Check if Gutenberg editor is available |
325
|
|
|
* |
326
|
|
|
* @since 6.7.0 |
327
|
|
|
* |
328
|
|
|
* @return bool |
329
|
|
|
*/ |
330
|
|
|
public static function is_gutenberg_available() { |
331
|
|
|
return function_exists( 'register_block_type' ); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
336
|
|
|
* |
337
|
|
|
* Loading blocks and plugins is enabled by default and may be disabled via filter: |
338
|
|
|
* add_filter( 'jetpack_gutenberg', '__return_false' ); |
339
|
|
|
* |
340
|
|
|
* @since 6.9.0 |
341
|
|
|
* |
342
|
|
|
* @return bool |
343
|
|
|
*/ |
344
|
|
|
public static function should_load() { |
345
|
|
|
if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
346
|
|
|
return false; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Filter to disable Gutenberg blocks |
351
|
|
|
* |
352
|
|
|
* @since 6.5.0 |
353
|
|
|
* |
354
|
|
|
* @param bool true Whether to load Gutenberg blocks |
355
|
|
|
*/ |
356
|
|
|
return (bool) apply_filters( 'jetpack_gutenberg', true ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Only enqueue block assets when needed. |
361
|
|
|
* |
362
|
|
|
* @param string $type slug of the block. |
363
|
|
|
* @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued. |
364
|
|
|
* |
365
|
|
|
* @return void |
366
|
|
|
*/ |
367
|
|
|
public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
368
|
|
|
if ( is_admin() ) { |
369
|
|
|
// A block's view assets will not be required in wp-admin. |
370
|
|
|
return; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
$type = sanitize_title_with_dashes( $type ); |
374
|
|
|
// Enqueue styles. |
375
|
|
|
$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; |
376
|
|
View Code Duplication |
if ( self::block_has_asset( $style_relative_path ) ) { |
377
|
|
|
$style_version = self::get_asset_version( $style_relative_path ); |
378
|
|
|
$view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
379
|
|
|
wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
// Enqueue script. |
383
|
|
|
$script_relative_path = self::get_blocks_directory() . $type . '/view.js'; |
384
|
|
View Code Duplication |
if ( self::block_has_asset( $script_relative_path ) ) { |
385
|
|
|
$script_version = self::get_asset_version( $script_relative_path ); |
386
|
|
|
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
387
|
|
|
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
wp_localize_script( |
391
|
|
|
'jetpack-block-' . $type, |
392
|
|
|
'Jetpack_Block_Assets_Base_Url', |
393
|
|
|
plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ) |
394
|
|
|
); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Check if an asset exists for a block. |
399
|
|
|
* |
400
|
|
|
* @param string $file Path of the file we are looking for. |
401
|
|
|
* |
402
|
|
|
* @return bool $block_has_asset Does the file exist. |
403
|
|
|
*/ |
404
|
|
|
public static function block_has_asset( $file ) { |
405
|
|
|
return file_exists( JETPACK__PLUGIN_DIR . $file ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Get the version number to use when loading the file. Allows us to bypass cache when developing. |
410
|
|
|
* |
411
|
|
|
* @param string $file Path of the file we are looking for. |
412
|
|
|
* |
413
|
|
|
* @return string $script_version Version number. |
414
|
|
|
*/ |
415
|
|
|
public static function get_asset_version( $file ) { |
416
|
|
|
return Jetpack::is_development_version() && self::block_has_asset( $file ) |
417
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . $file ) |
418
|
|
|
: JETPACK__VERSION; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Load Gutenberg editor assets |
423
|
|
|
* |
424
|
|
|
* @since 6.7.0 |
425
|
|
|
* |
426
|
|
|
* @return void |
427
|
|
|
*/ |
428
|
|
|
public static function enqueue_block_editor_assets() { |
429
|
|
|
if ( ! self::should_load() ) { |
430
|
|
|
return; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
$rtl = is_rtl() ? '.rtl' : ''; |
434
|
|
|
$beta = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : ''; |
435
|
|
|
$blocks_dir = self::get_blocks_directory(); |
436
|
|
|
|
437
|
|
|
$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE ); |
438
|
|
|
$editor_style = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE ); |
439
|
|
|
|
440
|
|
|
$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
441
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
442
|
|
|
: JETPACK__VERSION; |
443
|
|
|
|
444
|
|
|
wp_enqueue_script( |
445
|
|
|
'jetpack-blocks-editor', |
446
|
|
|
$editor_script, |
447
|
|
|
array( |
448
|
|
|
'lodash', |
449
|
|
|
'wp-api-fetch', |
450
|
|
|
'wp-blob', |
451
|
|
|
'wp-blocks', |
452
|
|
|
'wp-components', |
453
|
|
|
'wp-compose', |
454
|
|
|
'wp-data', |
455
|
|
|
'wp-date', |
456
|
|
|
'wp-edit-post', |
457
|
|
|
'wp-editor', |
458
|
|
|
'wp-element', |
459
|
|
|
'wp-hooks', |
460
|
|
|
'wp-i18n', |
461
|
|
|
'wp-keycodes', |
462
|
|
|
'wp-plugins', |
463
|
|
|
'wp-rich-text', |
464
|
|
|
'wp-token-list', |
465
|
|
|
'wp-url', |
466
|
|
|
), |
467
|
|
|
$version, |
468
|
|
|
false |
469
|
|
|
); |
470
|
|
|
|
471
|
|
|
wp_localize_script( |
472
|
|
|
'jetpack-blocks-editor', |
473
|
|
|
'Jetpack_Block_Assets_Base_Url', |
474
|
|
|
plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ) |
475
|
|
|
); |
476
|
|
|
|
477
|
|
|
wp_localize_script( |
478
|
|
|
'jetpack-blocks-editor', |
479
|
|
|
'Jetpack_Editor_Initial_State', |
480
|
|
|
array( |
481
|
|
|
'available_blocks' => self::get_availability(), |
482
|
|
|
'jetpack' => array( 'is_active' => Jetpack::is_active() ), |
483
|
|
|
) |
484
|
|
|
); |
485
|
|
|
|
486
|
|
|
Jetpack::setup_wp_i18n_locale_data(); |
487
|
|
|
|
488
|
|
|
wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|