Completed
Push — remove/blocks-preset-copy ( 151dc8 )
by Bernhard
06:54
created

Jetpack_Gutenberg::load_scripts_as_required()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 5
Ratio 25 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 5
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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
 * Wrapper function to safely register a gutenberg block type
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 WP_Block_Type|false The registered block type on success, or false on failure.
20
 */
21
function jetpack_register_block( $slug, $args = array() ) {
22
	if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) {
23
		_doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', '7.1.0' );
24
		$slug = 'jetpack/' . $slug;
25
	}
26
27
	// Checking whether block is registered to ensure it isn't registered twice.
28
	if ( Jetpack_Gutenberg::is_registered( $slug ) ) {
29
		return false;
30
	}
31
32
	return register_block_type( $slug, $args );
33
}
34
35
/**
36
 * Helper function to register a Jetpack Gutenberg plugin
37
 *
38
 * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead
39
 *
40
 * @param string $slug Slug of the plugin.
41
 *
42
 * @since 6.9.0
43
 *
44
 * @return void
45
 */
46
function jetpack_register_plugin( $slug ) {
47
	_deprecated_function( __FUNCTION__, '7.1', 'Jetpack_Gutenberg::set_extension_available' );
48
49
	Jetpack_Gutenberg::register_plugin( $slug );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Gutenberg::register_plugin() has been deprecated with message: 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
50
}
51
52
/**
53
 * Set the reason why an extension (block or plugin) is unavailable
54
 *
55
 * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_unavailable() instead
56
 *
57
 * @param string $slug Slug of the block.
58
 * @param string $reason A string representation of why the extension is unavailable.
59
 *
60
 * @since 7.0.0
61
 *
62
 * @return void
63
 */
64
function jetpack_set_extension_unavailability_reason( $slug, $reason ) {
65
	_deprecated_function( __FUNCTION__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' );
66
67
	Jetpack_Gutenberg::set_extension_unavailability_reason( $slug, $reason );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Gutenberg::set_e...unavailability_reason() has been deprecated with message: 7.1.0 Use set_extension_unavailable() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
68
}
69
70
/**
71
 * General Gutenberg editor specific functionality
72
 */
73
class Jetpack_Gutenberg {
74
75
	/**
76
	 * Only these extensions can be registered. Used to control availability of beta blocks.
77
	 *
78
	 * @var array Extensions whitelist
79
	 */
80
	private static $extensions = array();
81
82
	/**
83
	 * Keeps track of the reasons why a given extension is unavailable.
84
	 *
85
	 * @var array Extensions availability information
86
	 */
87
	private static $availability = array();
88
89
	/**
90
	 * Prepend the 'jetpack/' prefix to a block name
91
	 *
92
	 * @param string $block_name The block name.
93
	 *
94
	 * @return string The prefixed block name.
95
	 */
96
	private static function prepend_block_prefix( $block_name ) {
97
		return 'jetpack/' . $block_name;
98
	}
99
100
	/**
101
	 * Remove the 'jetpack/' or jetpack-' prefix from an extension name
102
	 *
103
	 * @param string $extension_name The extension name.
104
	 *
105
	 * @return string The unprefixed extension name.
106
	 */
107
	private static function remove_extension_prefix( $extension_name ) {
108
		if ( wp_startswith( $extension_name, 'jetpack/' ) || wp_startswith( $extension_name, 'jetpack-' ) ) {
109
			return substr( $extension_name, strlen( 'jetpack/' ) );
110
		}
111
		return $extension_name;
112
	}
113
114
	/**
115
	 * Whether two arrays share at least one item
116
	 *
117
	 * @param array $a An array.
118
	 * @param array $b Another array.
119
	 *
120
	 * @return boolean True if $a and $b share at least one item
121
	 */
122
	protected static function share_items( $a, $b ) {
123
		return count( array_intersect( $a, $b ) ) > 0;
124
	}
125
126
	/**
127
	 * Register a block
128
	 *
129
	 * @deprecated 7.1.0 Use jetpack_register_block() instead
130
	 *
131
	 * @param string $slug Slug of the block.
132
	 * @param array  $args Arguments that are passed into register_block_type().
133
	 */
134
	public static function register_block( $slug, $args ) {
135
		_deprecated_function( __METHOD__, '7.1', 'jetpack_register_block' );
136
137
		jetpack_register_block( 'jetpack/' . $slug, $args );
138
	}
139
140
	/**
141
	 * Register a plugin
142
	 *
143
	 * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead
144
	 *
145
	 * @param string $slug Slug of the plugin.
146
	 */
147
	public static function register_plugin( $slug ) {
148
		_deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_available' );
149
150
		self::set_extension_available( $slug );
151
	}
152
153
	/**
154
	 * Register a block
155
	 *
156
	 * @deprecated 7.0.0 Use jetpack_register_block() instead
157
	 *
158
	 * @param string $slug Slug of the block.
159
	 * @param array  $args Arguments that are passed into the register_block_type.
160
	 * @param array  $availability array containing if a block is available and the reason when it is not.
161
	 */
162
	public static function register( $slug, $args, $availability ) {
163
		_deprecated_function( __METHOD__, '7.0', 'jetpack_register_block' );
164
165
		if ( isset( $availability['available'] ) && ! $availability['available'] ) {
166
			self::set_extension_unavailability_reason( $slug, $availability['unavailable_reason'] );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Gutenberg::set_e...unavailability_reason() has been deprecated with message: 7.1.0 Use set_extension_unavailable() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
167
		} else {
168
			self::register_block( $slug, $args );
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Gutenberg::register_block() has been deprecated with message: 7.1.0 Use jetpack_register_block() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
169
		}
170
	}
171
172
	/**
173
	 * Set a (non-block) extension as available
174
	 *
175
	 * @param string $slug Slug of the extension.
176
	 */
177
	public static function set_extension_available( $slug ) {
178
		self::$availability[ self::remove_extension_prefix( $slug ) ] = true;
179
	}
180
181
	/**
182
	 * Set the reason why an extension (block or plugin) is unavailable
183
	 *
184
	 * @param string $slug Slug of the extension.
185
	 * @param string $reason A string representation of why the extension is unavailable.
186
	 */
187
	public static function set_extension_unavailable( $slug, $reason ) {
188
		self::$availability[ self::remove_extension_prefix( $slug ) ] = $reason;
189
	}
190
191
	/**
192
	 * Set the reason why an extension (block or plugin) is unavailable
193
	 *
194
	 * @deprecated 7.1.0 Use set_extension_unavailable() instead
195
	 *
196
	 * @param string $slug Slug of the extension.
197
	 * @param string $reason A string representation of why the extension is unavailable.
198
	 */
199
	public static function set_extension_unavailability_reason( $slug, $reason ) {
200
		_deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' );
201
202
		self::set_extension_unavailable( $slug, $reason );
203
	}
204
205
	/**
206
	 * Set up a whitelist of allowed block editor extensions
207
	 *
208
	 * @return void
209
	 */
210
	public static function init() {
211
		if ( ! self::should_load() ) {
212
			return;
213
		}
214
215
		/**
216
		 * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
217
		 *
218
		 * @since 6.9.0
219
		 *
220
		 * @param boolean
221
		 */
222
		if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
223
			Jetpack_Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
224
		}
225
226
		/**
227
		 * Filter the whitelist of block editor extensions that are available through Jetpack.
228
		 *
229
		 * @since 7.0.0
230
		 *
231
		 * @param array
232
		 */
233
		self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_jetpack_gutenberg_extensions_whitelist() );
234
235
		/**
236
		 * Filter the whitelist of block editor plugins that are available through Jetpack.
237
		 *
238
		 * @deprecated 7.0.0 Use jetpack_set_available_extensions instead
239
		 *
240
		 * @since 6.8.0
241
		 *
242
		 * @param array
243
		 */
244
		self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions );
245
246
		/**
247
		 * Filter the whitelist of block editor plugins that are available through Jetpack.
248
		 *
249
		 * @deprecated 7.0.0 Use jetpack_set_available_extensions instead
250
		 *
251
		 * @since 6.9.0
252
		 *
253
		 * @param array
254
		 */
255
		self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions );
256
	}
257
258
	/**
259
	 * Resets the class to its original state
260
	 *
261
	 * Used in unit tests
262
	 *
263
	 * @return void
264
	 */
265
	public static function reset() {
266
		self::$extensions   = array();
267
		self::$availability = array();
268
	}
269
270
	/**
271
	 * Return the Gutenberg extensions (blocks and plugins) directory
272
	 *
273
	 * @return string The Gutenberg extensions directory
274
	 */
275
	public static function get_blocks_directory() {
276
		/**
277
		 * Filter to select Gutenberg blocks directory
278
		 *
279
		 * @since 6.9.0
280
		 *
281
		 * @param string default: '_inc/blocks/'
282
		 */
283
		return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
284
	}
285
286
	/**
287
	 * Return the Gutenberg extensions (blocks and plugins) source directory
288
	 *
289
	 * @return string The Gutenberg extensions source directory
290
	 */
291
	public static function get_blocks_source_directory() {
292
		/**
293
		 * Filter to select Gutenberg blocks source directory
294
		 *
295
		 * @since 7.3.0
296
		 *
297
		 * @param string default: 'extensions/'
298
		 */
299
		return apply_filters( 'jetpack_blocks_source_directory', 'extensions/' );
300
	}
301
302
	/**
303
	 * Checks for a given .json file in the blocks folder.
304
	 *
305
	 * @param string $preset The name of the .json file to look for.
306
	 *
307
	 * @return bool True if the file is found.
308
	 */
309
	public static function preset_exists( $preset ) {
310
		return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_source_directory() . 'setup/' . $preset . '.json' );
311
	}
312
313
	/**
314
	 * Decodes JSON loaded from a preset file in the blocks folder
315
	 *
316
	 * @param string $preset The name of the .json file to load.
317
	 *
318
	 * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
319
	 */
320
	public static function get_preset( $preset ) {
321
		return json_decode(
322
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
323
			file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_source_directory() . 'setup/' . $preset . '.json' )
324
		);
325
	}
326
327
	/**
328
	 * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
329
	 *
330
	 * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
331
	 */
332
	public static function get_jetpack_gutenberg_extensions_whitelist() {
333
		$preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array();
334
335
		$preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array();
336
337
		if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
338
			$beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array();
339
			return array_unique( array_merge( $preset_extensions, $beta_extensions ) );
340
		}
341
342
		return $preset_extensions;
343
	}
344
345
	/**
346
	 * Get availability of each block / plugin.
347
	 *
348
	 * @return array A list of block and plugins and their availablity status
349
	 */
350
	public static function get_availability() {
351
		/**
352
		 * Fires before Gutenberg extensions availability is computed.
353
		 *
354
		 * In the function call you supply, use `jetpack_register_block()` to set a block as available.
355
		 * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and
356
		 * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered
357
		 * but marked as unavailable).
358
		 *
359
		 * @since 7.0.0
360
		 */
361
		do_action( 'jetpack_register_gutenberg_extensions' );
362
363
		$available_extensions = array();
364
365
		foreach ( self::$extensions as $extension ) {
366
			$is_available = self::is_registered( 'jetpack/' . $extension ) ||
367
			( isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ] );
368
369
			$available_extensions[ $extension ] = array(
370
				'available' => $is_available,
371
			);
372
373
			if ( ! $is_available ) {
374
				$reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module';
375
				$available_extensions[ $extension ]['unavailable_reason'] = $reason;
376
			}
377
		}
378
379
		$unwhitelisted_blocks  = array();
380
		$all_registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
381
		foreach ( $all_registered_blocks as $block_name => $block_type ) {
382
			if ( ! wp_startswith( $block_name, 'jetpack/' ) || isset( $block_type->parent ) ) {
383
				continue;
384
			}
385
386
			$unprefixed_block_name = self::remove_extension_prefix( $block_name );
387
388
			if ( in_array( $unprefixed_block_name, self::$extensions, true ) ) {
389
				continue;
390
			}
391
392
			$unwhitelisted_blocks[ $unprefixed_block_name ] = array(
393
				'available'          => false,
394
				'unavailable_reason' => 'not_whitelisted',
395
			);
396
		}
397
398
		// Finally: Unwhitelisted non-block extensions. These are in $availability.
399
		$unwhitelisted_extensions = array_fill_keys(
400
			array_diff( array_keys( self::$availability ), self::$extensions ),
401
			array(
402
				'available'          => false,
403
				'unavailable_reason' => 'not_whitelisted',
404
			)
405
		);
406
		return array_merge( $available_extensions, $unwhitelisted_blocks, $unwhitelisted_extensions );
407
	}
408
409
	/**
410
	 * Check if an extension/block is already registered
411
	 *
412
	 * @since 7.2
413
	 *
414
	 * @param string $slug Name of extension/block to check.
415
	 *
416
	 * @return bool
417
	 */
418
	public static function is_registered( $slug ) {
419
		return WP_Block_Type_Registry::get_instance()->is_registered( $slug );
420
	}
421
422
	/**
423
	 * Check if Gutenberg editor is available
424
	 *
425
	 * @since 6.7.0
426
	 *
427
	 * @return bool
428
	 */
429
	public static function is_gutenberg_available() {
430
		return true;
431
	}
432
433
	/**
434
	 * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
435
	 *
436
	 * Loading blocks and plugins is enabled by default and may be disabled via filter:
437
	 *   add_filter( 'jetpack_gutenberg', '__return_false' );
438
	 *
439
	 * @since 6.9.0
440
	 *
441
	 * @return bool
442
	 */
443
	public static function should_load() {
444
		if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
445
			return false;
446
		}
447
448
		/**
449
		 * Filter to disable Gutenberg blocks
450
		 *
451
		 * @since 6.5.0
452
		 *
453
		 * @param bool true Whether to load Gutenberg blocks
454
		 */
455
		return (bool) apply_filters( 'jetpack_gutenberg', true );
456
	}
457
458
	/**
459
	 * Only enqueue block assets when needed.
460
	 *
461
	 * @param string $type Slug of the block.
462
	 * @param array  $script_dependencies An array of view-side Javascript dependencies to be enqueued.
463
	 *
464
	 * @return void
465
	 */
466
	public static function load_assets_as_required( $type, $script_dependencies = array() ) {
467
		if ( is_admin() ) {
468
			// A block's view assets will not be required in wp-admin.
469
			return;
470
		}
471
472
		$type = sanitize_title_with_dashes( $type );
473
		self::load_styles_as_required( $type );
474
		self::load_scripts_as_required( $type, $script_dependencies );
475
	}
476
477
	/**
478
	 * Only enqueue block sytles when needed.
479
	 *
480
	 * @param string $type Slug of the block.
481
	 *
482
	 * @since 7.2.0
483
	 *
484
	 * @return void
485
	 */
486
	public static function load_styles_as_required( $type ) {
487
		if ( is_admin() ) {
488
			// A block's view assets will not be required in wp-admin.
489
			return;
490
		}
491
492
		// Enqueue styles.
493
		$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
494 View Code Duplication
		if ( self::block_has_asset( $style_relative_path ) ) {
495
			$style_version = self::get_asset_version( $style_relative_path );
496
			$view_style    = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
497
			wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
498
		}
499
500
	}
501
	/**
502
	 * Only enqueue block scripts when needed.
503
	 *
504
	 * @param string $type Slug of the block.
505
	 * @param array  $script_dependencies An array of view-side Javascript dependencies to be enqueued.
506
	 *
507
	 * @since 7.2.0
508
	 *
509
	 * @return void
510
	 */
511
	public static function load_scripts_as_required( $type, $script_dependencies = array() ) {
512
		if ( is_admin() ) {
513
			// A block's view assets will not be required in wp-admin.
514
			return;
515
		}
516
517
		// Enqueue script.
518
		$script_relative_path = self::get_blocks_directory() . $type . '/view.js';
519 View Code Duplication
		if ( self::block_has_asset( $script_relative_path ) ) {
520
			$script_version = self::get_asset_version( $script_relative_path );
521
			$view_script    = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
522
			wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
523
		}
524
525
		wp_localize_script(
526
			'jetpack-block-' . $type,
527
			'Jetpack_Block_Assets_Base_Url',
528
			plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE )
529
		);
530
	}
531
532
	/**
533
	 * Check if an asset exists for a block.
534
	 *
535
	 * @param string $file Path of the file we are looking for.
536
	 *
537
	 * @return bool $block_has_asset Does the file exist.
538
	 */
539
	public static function block_has_asset( $file ) {
540
		return file_exists( JETPACK__PLUGIN_DIR . $file );
541
	}
542
543
	/**
544
	 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
545
	 *
546
	 * @param string $file Path of the file we are looking for.
547
	 *
548
	 * @return string $script_version Version number.
549
	 */
550
	public static function get_asset_version( $file ) {
551
		return Jetpack::is_development_version() && self::block_has_asset( $file )
552
			? filemtime( JETPACK__PLUGIN_DIR . $file )
553
			: JETPACK__VERSION;
554
	}
555
556
	/**
557
	 * Load Gutenberg editor assets
558
	 *
559
	 * @since 6.7.0
560
	 *
561
	 * @return void
562
	 */
563
	public static function enqueue_block_editor_assets() {
564
		if ( ! self::should_load() ) {
565
			return;
566
		}
567
568
		$rtl        = is_rtl() ? '.rtl' : '';
569
		$beta       = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
570
		$blocks_dir = self::get_blocks_directory();
571
572
		$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE );
573
		$editor_style  = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );
574
575
		$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
576
			? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
577
			: JETPACK__VERSION;
578
579 View Code Duplication
		if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) {
580
			$site_fragment = Jetpack::build_raw_urls( home_url() );
581
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
582
			$site_fragment = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
583
		} else {
584
			$site_fragment = '';
585
		}
586
587
		wp_enqueue_script(
588
			'jetpack-blocks-editor',
589
			$editor_script,
590
			array(
591
				'lodash',
592
				'wp-api-fetch',
593
				'wp-blob',
594
				'wp-blocks',
595
				'wp-components',
596
				'wp-compose',
597
				'wp-data',
598
				'wp-date',
599
				'wp-edit-post',
600
				'wp-editor',
601
				'wp-element',
602
				'wp-escape-html',
603
				'wp-hooks',
604
				'wp-i18n',
605
				'wp-keycodes',
606
				'wp-plugins',
607
				'wp-polyfill',
608
				'wp-rich-text',
609
				'wp-token-list',
610
				'wp-url',
611
			),
612
			$version,
613
			false
614
		);
615
616
		wp_localize_script(
617
			'jetpack-blocks-editor',
618
			'Jetpack_Block_Assets_Base_Url',
619
			plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE )
620
		);
621
622
		wp_localize_script(
623
			'jetpack-blocks-editor',
624
			'Jetpack_Editor_Initial_State',
625
			array(
626
				'available_blocks' => self::get_availability(),
627
				'jetpack'          => array( 'is_active' => Jetpack::is_active() ),
628
				'siteFragment'     => $site_fragment,
629
			)
630
		);
631
632
		wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack', plugins_url( 'languages/json', JETPACK__PLUGIN_FILE ) );
633
634
		// Adding a filter late to allow every other filter to process the path, including the CDN.
635
		add_filter( 'pre_load_script_translations', array( __CLASS__, 'filter_pre_load_script_translations' ), 1000, 3 );
636
637
		wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
638
	}
639
640
	/**
641
	 * A workaround for setting i18n data for WordPress client-side i18n mechanism.
642
	 * We are not yet using dotorg language packs for the editor file, so this short-circuits
643
	 * the translation loading and feeds our JSON data directly into the translation getter.
644
	 *
645
	 * @param NULL   $null     not used.
646
	 * @param String $file     the file path that is being loaded, ignored.
647
	 * @param String $handle   the script handle.
648
	 * @return NULL|String the translation data only if we're working with our handle.
649
	 */
650
	public static function filter_pre_load_script_translations( $null, $file, $handle ) {
651
		if ( 'jetpack-blocks-editor' !== $handle ) {
652
			return null;
653
		}
654
655
		return Jetpack::get_i18n_data_json();
656
	}
657
658
	/**
659
	 * Some blocks do not depend on a specific module,
660
	 * and can consequently be loaded outside of the usual modules.
661
	 * We will look for such modules in the extensions/ directory.
662
	 *
663
	 * @since 7.1.0
664
	 */
665
	public static function load_independent_blocks() {
666
		if ( self::should_load() ) {
667
			/**
668
			 * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
669
			 * If available, load them.
670
			 */
671
			foreach ( self::$extensions as $extension ) {
672
				$extension_file_glob = glob( JETPACK__PLUGIN_DIR . 'extensions/*/' . $extension . '/' . $extension . '.php' );
673
				if ( ! empty( $extension_file_glob ) ) {
674
					include_once $extension_file_glob[0];
675
				}
676
			}
677
		}
678
	}
679
}
680