Completed
Push — try/namespacing-all-the-things ( 457764 )
by
unknown
08:24
created

class.jetpack-gutenberg.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
namespace Automattic\Jetpack;
3
4
use Automattic\Jetpack;
5
/**
6
 * Handles server-side registration and use of all blocks and plugins available in Jetpack for the block editor, aka Gutenberg.
7
 * Works in tandem with client-side block registration via `index.json`
8
 *
9
 * @package Jetpack
10
 */
11
12
/**
13
 * Wrapper function to safely register a gutenberg block type
14
 *
15
 * @param string $slug Slug of the block.
16
 * @param array  $args Arguments that are passed into register_block_type.
17
 *
18
 * @see register_block_type
19
 *
20
 * @since 6.7.0
21
 *
22
 * @return WP_Block_Type|false The registered block type on success, or false on failure.
23
 */
24
function jetpack_register_block( $slug, $args = array() ) {
25
	if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) {
26
		_doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', '7.1.0' );
27
		$slug = 'jetpack/' . $slug;
28
	}
29
30
	// Checking whether block is registered to ensure it isn't registered twice.
31
	if ( Jetpack_Gutenberg::is_registered( $slug ) ) {
32
		return false;
33
	}
34
35
	return register_block_type( $slug, $args );
36
}
37
38
/**
39
 * Helper function to register a Jetpack Gutenberg plugin
40
 *
41
 * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead
42
 *
43
 * @param string $slug Slug of the plugin.
44
 *
45
 * @since 6.9.0
46
 *
47
 * @return void
48
 */
49
function jetpack_register_plugin( $slug ) {
50
	_deprecated_function( __FUNCTION__, '7.1', 'Jetpack_Gutenberg::set_extension_available' );
51
52
	Jetpack_Gutenberg::register_plugin( $slug );
0 ignored issues
show
Deprecated Code introduced by
The method Automattic\Jetpack\Jetpa...berg::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...
53
}
54
55
/**
56
 * Set the reason why an extension (block or plugin) is unavailable
57
 *
58
 * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_unavailable() instead
59
 *
60
 * @param string $slug Slug of the block.
61
 * @param string $reason A string representation of why the extension is unavailable.
62
 *
63
 * @since 7.0.0
64
 *
65
 * @return void
66
 */
67
function jetpack_set_extension_unavailability_reason( $slug, $reason ) {
68
	_deprecated_function( __FUNCTION__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' );
69
70
	Jetpack_Gutenberg::set_extension_unavailability_reason( $slug, $reason );
0 ignored issues
show
Deprecated Code introduced by
The method Automattic\Jetpack\Jetpa...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...
71
}
72
73
/**
74
 * General Gutenberg editor specific functionality
75
 */
76
class Jetpack_Gutenberg {
77
78
	/**
79
	 * Only these extensions can be registered. Used to control availability of beta blocks.
80
	 *
81
	 * @var array Extensions whitelist
82
	 */
83
	private static $extensions = array();
84
85
	/**
86
	 * Keeps track of the reasons why a given extension is unavailable.
87
	 *
88
	 * @var array Extensions availability information
89
	 */
90
	private static $availability = array();
91
92
	/**
93
	 * Prepend the 'jetpack/' prefix to a block name
94
	 *
95
	 * @param string $block_name The block name.
96
	 *
97
	 * @return string The prefixed block name.
98
	 */
99
	private static function prepend_block_prefix( $block_name ) {
100
		return 'jetpack/' . $block_name;
101
	}
102
103
	/**
104
	 * Remove the 'jetpack/' or jetpack-' prefix from an extension name
105
	 *
106
	 * @param string $extension_name The extension name.
107
	 *
108
	 * @return string The unprefixed extension name.
109
	 */
110
	private static function remove_extension_prefix( $extension_name ) {
111
		if ( wp_startswith( $extension_name, 'jetpack/' ) || wp_startswith( $extension_name, 'jetpack-' ) ) {
112
			return substr( $extension_name, strlen( 'jetpack/' ) );
113
		}
114
		return $extension_name;
115
	}
116
117
	/**
118
	 * Whether two arrays share at least one item
119
	 *
120
	 * @param array $a An array.
121
	 * @param array $b Another array.
122
	 *
123
	 * @return boolean True if $a and $b share at least one item
124
	 */
125
	protected static function share_items( $a, $b ) {
126
		return count( array_intersect( $a, $b ) ) > 0;
127
	}
128
129
	/**
130
	 * Register a block
131
	 *
132
	 * @deprecated 7.1.0 Use \Automattic\Jetpack\jetpack_register_block() instead
133
	 *
134
	 * @param string $slug Slug of the block.
135
	 * @param array  $args Arguments that are passed into register_block_type().
136
	 */
137
	public static function register_block( $slug, $args ) {
138
		_deprecated_function( __METHOD__, '7.1', 'jetpack_register_block' );
139
140
		\Automattic\Jetpack\jetpack_register_block( 'jetpack/' . $slug, $args );
141
	}
142
143
	/**
144
	 * Register a plugin
145
	 *
146
	 * @deprecated 7.1.0 Use Jetpack_Gutenberg::set_extension_available() instead
147
	 *
148
	 * @param string $slug Slug of the plugin.
149
	 */
150
	public static function register_plugin( $slug ) {
151
		_deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_available' );
152
153
		self::set_extension_available( $slug );
154
	}
155
156
	/**
157
	 * Register a block
158
	 *
159
	 * @deprecated 7.0.0 Use \Automattic\Jetpack\jetpack_register_block() instead
160
	 *
161
	 * @param string $slug Slug of the block.
162
	 * @param array  $args Arguments that are passed into the register_block_type.
163
	 * @param array  $availability array containing if a block is available and the reason when it is not.
164
	 */
165
	public static function register( $slug, $args, $availability ) {
166
		_deprecated_function( __METHOD__, '7.0', 'jetpack_register_block' );
167
168
		if ( isset( $availability['available'] ) && ! $availability['available'] ) {
169
			self::set_extension_unavailability_reason( $slug, $availability['unavailable_reason'] );
0 ignored issues
show
Deprecated Code introduced by
The method Automattic\Jetpack\Jetpa...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...
170
		} else {
171
			self::register_block( $slug, $args );
0 ignored issues
show
Deprecated Code introduced by
The method Automattic\Jetpack\Jetpa...nberg::register_block() has been deprecated with message: 7.1.0 Use \Automattic\Jetpack\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...
172
		}
173
	}
174
175
	/**
176
	 * Set a (non-block) extension as available
177
	 *
178
	 * @param string $slug Slug of the extension.
179
	 */
180
	public static function set_extension_available( $slug ) {
181
		self::$availability[ self::remove_extension_prefix( $slug ) ] = true;
182
	}
183
184
	/**
185
	 * Set the reason why an extension (block or plugin) is unavailable
186
	 *
187
	 * @param string $slug Slug of the extension.
188
	 * @param string $reason A string representation of why the extension is unavailable.
189
	 */
190
	public static function set_extension_unavailable( $slug, $reason ) {
191
		self::$availability[ self::remove_extension_prefix( $slug ) ] = $reason;
192
	}
193
194
	/**
195
	 * Set the reason why an extension (block or plugin) is unavailable
196
	 *
197
	 * @deprecated 7.1.0 Use set_extension_unavailable() instead
198
	 *
199
	 * @param string $slug Slug of the extension.
200
	 * @param string $reason A string representation of why the extension is unavailable.
201
	 */
202
	public static function set_extension_unavailability_reason( $slug, $reason ) {
203
		_deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' );
204
205
		self::set_extension_unavailable( $slug, $reason );
206
	}
207
208
	/**
209
	 * Set up a whitelist of allowed block editor extensions
210
	 *
211
	 * @return void
212
	 */
213
	public static function init() {
214
		if ( ! self::should_load() ) {
215
			return;
216
		}
217
218
		/**
219
		 * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
220
		 *
221
		 * @since 6.9.0
222
		 *
223
		 * @param boolean
224
		 */
225
		if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
226
			Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
227
		}
228
229
		/**
230
		 * Filter the whitelist of block editor extensions that are available through Jetpack.
231
		 *
232
		 * @since 7.0.0
233
		 *
234
		 * @param array
235
		 */
236
		self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() );
237
238
		/**
239
		 * Filter the whitelist of block editor plugins that are available through Jetpack.
240
		 *
241
		 * @deprecated 7.0.0 Use jetpack_set_available_extensions instead
242
		 *
243
		 * @since 6.8.0
244
		 *
245
		 * @param array
246
		 */
247
		self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions );
248
249
		/**
250
		 * Filter the whitelist of block editor plugins that are available through Jetpack.
251
		 *
252
		 * @deprecated 7.0.0 Use jetpack_set_available_extensions instead
253
		 *
254
		 * @since 6.9.0
255
		 *
256
		 * @param array
257
		 */
258
		self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions );
259
	}
260
261
	/**
262
	 * Resets the class to its original state
263
	 *
264
	 * Used in unit tests
265
	 *
266
	 * @return void
267
	 */
268
	public static function reset() {
269
		self::$extensions   = array();
270
		self::$availability = array();
271
	}
272
273
	/**
274
	 * Return the Gutenberg extensions (blocks and plugins) directory
275
	 *
276
	 * @return string The Gutenberg extensions directory
277
	 */
278
	public static function get_blocks_directory() {
279
		/**
280
		 * Filter to select Gutenberg blocks directory
281
		 *
282
		 * @since 6.9.0
283
		 *
284
		 * @param string default: '_inc/blocks/'
285
		 */
286
		return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
287
	}
288
289
	/**
290
	 * Checks for a given .json file in the blocks folder.
291
	 *
292
	 * @param string $preset The name of the .json file to look for.
293
	 *
294
	 * @return bool True if the file is found.
295
	 */
296
	public static function preset_exists( $preset ) {
297
		return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
298
	}
299
300
	/**
301
	 * Decodes JSON loaded from a preset file in the blocks folder
302
	 *
303
	 * @param string $preset The name of the .json file to load.
304
	 *
305
	 * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
306
	 */
307
	public static function get_preset( $preset ) {
308
		return json_decode(
309
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
310
			file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
311
		);
312
	}
313
314
	/**
315
	 * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
316
	 *
317
	 * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
318
	 */
319
	public static function get_jetpack_gutenberg_extensions_whitelist() {
320
		$preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array();
321
322
		$preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array();
323
324
		if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
325
			$beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array();
326
			return array_unique( array_merge( $preset_extensions, $beta_extensions ) );
327
		}
328
329
		return $preset_extensions;
330
	}
331
332
	/**
333
	 * Returns a diff from a combined list of whitelisted extensions and extensions determined to be excluded
334
	 *
335
	 * @param  array $whitelisted_extensions An array of whitelisted extensions.
336
	 *
337
	 * @return array A list of blocks: eg array( 'publicize', 'markdown' )
338
	 */
339
	public static function get_available_extensions( $whitelisted_extensions = null ) {
340
		$exclusions             = get_option( 'jetpack_excluded_extensions', array() );
341
		$whitelisted_extensions = is_null( $whitelisted_extensions ) ? self::get_jetpack_gutenberg_extensions_whitelist() : $whitelisted_extensions;
342
343
		return array_diff( $whitelisted_extensions, $exclusions );
344
	}
345
346
	/**
347
	 * Get availability of each block / plugin.
348
	 *
349
	 * @return array A list of block and plugins and their availablity status
350
	 */
351
	public static function get_availability() {
352
		/**
353
		 * Fires before Gutenberg extensions availability is computed.
354
		 *
355
		 * In the function call you supply, use `\Automattic\Jetpack\jetpack_register_block()` to set a block as available.
356
		 * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and
357
		 * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered
358
		 * but marked as unavailable).
359
		 *
360
		 * @since 7.0.0
361
		 */
362
		do_action( 'jetpack_register_gutenberg_extensions' );
363
364
		$available_extensions = array();
365
366
		foreach ( self::$extensions as $extension ) {
367
			$is_available = self::is_registered( 'jetpack/' . $extension ) ||
368
			( isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ] );
369
370
			$available_extensions[ $extension ] = array(
371
				'available' => $is_available,
372
			);
373
374
			if ( ! $is_available ) {
375
				$reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module';
376
				$available_extensions[ $extension ]['unavailable_reason'] = $reason;
377
			}
378
		}
379
380
		return $available_extensions;
381
	}
382
383
	/**
384
	 * Check if an extension/block is already registered
385
	 *
386
	 * @since 7.2
387
	 *
388
	 * @param string $slug Name of extension/block to check.
389
	 *
390
	 * @return bool
391
	 */
392
	public static function is_registered( $slug ) {
393
		return \WP_Block_Type_Registry::get_instance()->is_registered( $slug );
394
	}
395
396
	/**
397
	 * Check if Gutenberg editor is available
398
	 *
399
	 * @since 6.7.0
400
	 *
401
	 * @return bool
402
	 */
403
	public static function is_gutenberg_available() {
404
		return true;
405
	}
406
407
	/**
408
	 * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
409
	 *
410
	 * Loading blocks and plugins is enabled by default and may be disabled via filter:
411
	 *   add_filter( 'jetpack_gutenberg', '__return_false' );
412
	 *
413
	 * @since 6.9.0
414
	 *
415
	 * @return bool
416
	 */
417
	public static function should_load() {
418
		if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
419
			return false;
420
		}
421
422
		/**
423
		 * Filter to disable Gutenberg blocks
424
		 *
425
		 * @since 6.5.0
426
		 *
427
		 * @param bool true Whether to load Gutenberg blocks
428
		 */
429
		return (bool) apply_filters( 'jetpack_gutenberg', true );
430
	}
431
432
	/**
433
	 * Only enqueue block assets when needed.
434
	 *
435
	 * @param string $type Slug of the block.
436
	 * @param array  $script_dependencies Script dependencies. Will be merged with automatically
437
	 *                                    detected script dependencies from the webpack build.
438
	 *
439
	 * @return void
440
	 */
441
	public static function load_assets_as_required( $type, $script_dependencies = array() ) {
442
		if ( is_admin() ) {
443
			// A block's view assets will not be required in wp-admin.
444
			return;
445
		}
446
447
		$type = sanitize_title_with_dashes( $type );
448
		self::load_styles_as_required( $type );
449
		self::load_scripts_as_required( $type, $script_dependencies );
450
	}
451
452
	/**
453
	 * Only enqueue block sytles when needed.
454
	 *
455
	 * @param string $type Slug of the block.
456
	 *
457
	 * @since 7.2.0
458
	 *
459
	 * @return void
460
	 */
461
	public static function load_styles_as_required( $type ) {
462
		if ( is_admin() ) {
463
			// A block's view assets will not be required in wp-admin.
464
			return;
465
		}
466
467
		// Enqueue styles.
468
		$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
469
		if ( self::block_has_asset( $style_relative_path ) ) {
470
			$style_version = self::get_asset_version( $style_relative_path );
471
			$view_style    = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
472
			wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
473
		}
474
475
	}
476
477
	/**
478
	 * Only enqueue block scripts when needed.
479
	 *
480
	 * @param string $type Slug of the block.
481
	 * @param array  $dependencies Script dependencies. Will be merged with automatically
482
	 *                             detected script dependencies from the webpack build.
483
	 *
484
	 * @since 7.2.0
485
	 *
486
	 * @return void
487
	 */
488
	public static function load_scripts_as_required( $type, $dependencies = array() ) {
489
		if ( is_admin() ) {
490
			// A block's view assets will not be required in wp-admin.
491
			return;
492
		}
493
494
		// Enqueue script.
495
		$script_relative_path = self::get_blocks_directory() . $type . '/view.js';
496
		$script_deps_path     = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.deps.json';
497
498
		$script_dependencies = file_exists( $script_deps_path )
499
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
500
			? json_decode( file_get_contents( $script_deps_path ) )
501
			: array();
502
		$script_dependencies = array_merge( $script_dependencies, $dependencies, array( 'wp-polyfill' ) );
503
504
		if ( ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) && self::block_has_asset( $script_relative_path ) ) {
505
			$script_version = self::get_asset_version( $script_relative_path );
506
			$view_script    = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
507
			wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
508
		}
509
510
		wp_localize_script(
511
			'jetpack-block-' . $type,
512
			'Jetpack_Block_Assets_Base_Url',
513
			plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE )
514
		);
515
	}
516
517
	/**
518
	 * Check if an asset exists for a block.
519
	 *
520
	 * @param string $file Path of the file we are looking for.
521
	 *
522
	 * @return bool $block_has_asset Does the file exist.
523
	 */
524
	public static function block_has_asset( $file ) {
525
		return file_exists( JETPACK__PLUGIN_DIR . $file );
526
	}
527
528
	/**
529
	 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
530
	 *
531
	 * @param string $file Path of the file we are looking for.
532
	 *
533
	 * @return string $script_version Version number.
534
	 */
535
	public static function get_asset_version( $file ) {
536
		return Jetpack::is_development_version() && self::block_has_asset( $file )
537
			? filemtime( JETPACK__PLUGIN_DIR . $file )
538
			: JETPACK__VERSION;
539
	}
540
541
	/**
542
	 * Load Gutenberg editor assets
543
	 *
544
	 * @since 6.7.0
545
	 *
546
	 * @return void
547
	 */
548
	public static function enqueue_block_editor_assets() {
549
		if ( ! self::should_load() ) {
550
			return;
551
		}
552
553
		$rtl        = is_rtl() ? '.rtl' : '';
554
		$beta       = Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
555
		$blocks_dir = self::get_blocks_directory();
556
557
		$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE );
558
		$editor_style  = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );
559
560
		$editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$beta}.deps.json";
561
		$editor_deps      = file_exists( $editor_deps_path )
562
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
563
			? json_decode( file_get_contents( $editor_deps_path ) )
564
			: array();
565
		$editor_deps[] = 'wp-polyfill';
566
567
		$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
568
			? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
569
			: JETPACK__VERSION;
570
571
		if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) {
572
			$site_fragment = Jetpack::build_raw_urls( home_url() );
573
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
574
			$site_fragment = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
575
		} else {
576
			$site_fragment = '';
577
		}
578
579
		wp_enqueue_script(
580
			'jetpack-blocks-editor',
581
			$editor_script,
582
			$editor_deps,
583
			$version,
584
			false
585
		);
586
587
		wp_localize_script(
588
			'jetpack-blocks-editor',
589
			'Jetpack_Block_Assets_Base_Url',
590
			plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE )
591
		);
592
593
		wp_localize_script(
594
			'jetpack-blocks-editor',
595
			'Jetpack_Editor_Initial_State',
596
			array(
597
				'available_blocks' => self::get_availability(),
598
				'jetpack'          => array( 'is_active' => Jetpack::is_active() ),
599
				'siteFragment'     => $site_fragment,
600
			)
601
		);
602
603
		wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack', plugins_url( 'languages/json', JETPACK__PLUGIN_FILE ) );
604
605
		// Adding a filter late to allow every other filter to process the path, including the CDN.
606
		add_filter( 'pre_load_script_translations', array( __CLASS__, 'filter_pre_load_script_translations' ), 1000, 3 );
607
608
		wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
609
	}
610
611
	/**
612
	 * A workaround for setting i18n data for WordPress client-side i18n mechanism.
613
	 * We are not yet using dotorg language packs for the editor file, so this short-circuits
614
	 * the translation loading and feeds our JSON data directly into the translation getter.
615
	 *
616
	 * @param NULL   $null     not used.
617
	 * @param String $file     the file path that is being loaded, ignored.
618
	 * @param String $handle   the script handle.
619
	 * @return NULL|String the translation data only if we're working with our handle.
620
	 */
621
	public static function filter_pre_load_script_translations( $null, $file, $handle ) {
622
		if ( 'jetpack-blocks-editor' !== $handle ) {
623
			return null;
624
		}
625
626
		return Jetpack::get_i18n_data_json();
627
	}
628
629
	/**
630
	 * Some blocks do not depend on a specific module,
631
	 * and can consequently be loaded outside of the usual modules.
632
	 * We will look for such modules in the extensions/ directory.
633
	 *
634
	 * @since 7.1.0
635
	 */
636
	public static function load_independent_blocks() {
637
		if ( self::should_load() ) {
638
			/**
639
			 * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
640
			 * If available, load them.
641
			 */
642
			foreach ( self::$extensions as $extension ) {
643
				$extension_file_glob = glob( JETPACK__PLUGIN_DIR . 'extensions/*/' . $extension . '/' . $extension . '.php' );
644
				if ( ! empty( $extension_file_glob ) ) {
645
					include_once $extension_file_glob[0];
646
				}
647
			}
648
		}
649
	}
650
}
651