Completed
Push — update/gutenberg-unit-tests-re... ( 29e10f...0256b9 )
by
unknown
30:54 queued 23:09
created

Jetpack_Gutenberg   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 573
Duplicated Lines 2.97 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 17
loc 573
rs 2.96
c 0
b 0
f 0
wmc 68
lcom 1
cbo 2

28 Methods

Rating   Name   Duplication   Size   Complexity  
A prepend_block_prefix() 0 3 1
A remove_extension_prefix() 0 6 3
A share_items() 0 3 1
A register_block() 0 5 1
A register_plugin() 0 5 1
A register() 0 9 3
A set_extension_available() 0 3 1
A set_extension_unavailable() 0 3 1
A set_extension_unavailability_reason() 0 5 1
A init() 0 47 3
A reset() 0 4 1
A get_blocks_directory() 0 10 1
A preset_exists() 0 3 1
A get_preset() 0 6 1
A get_jetpack_gutenberg_extensions_whitelist() 0 12 5
B get_availability() 0 31 6
A is_registered() 0 3 1
A is_gutenberg_available() 0 3 1
A should_load() 0 14 3
A load_assets_as_required() 0 10 2
A load_styles_as_required() 5 15 4
A load_scripts_as_required() 5 27 4
A block_has_asset() 0 3 1
A get_asset_version() 0 5 3
C enqueue_block_editor_assets() 7 61 10
A filter_pre_load_script_translations() 0 7 2
A load_independent_blocks() 0 14 4
A get_available_extensions() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Gutenberg often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Gutenberg, and based on these observations, apply Extract Interface, too.

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_available_extensions() );
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
	 * Checks for a given .json file in the blocks folder.
288
	 *
289
	 * @param string $preset The name of the .json file to look for.
290
	 *
291
	 * @return bool True if the file is found.
292
	 */
293
	public static function preset_exists( $preset ) {
294
		return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
295
	}
296
297
	/**
298
	 * Decodes JSON loaded from a preset file in the blocks folder
299
	 *
300
	 * @param string $preset The name of the .json file to load.
301
	 *
302
	 * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
303
	 */
304
	public static function get_preset( $preset ) {
305
		return json_decode(
306
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
307
			file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
308
		);
309
	}
310
311
	/**
312
	 * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
313
	 *
314
	 * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
315
	 */
316
	public static function get_jetpack_gutenberg_extensions_whitelist() {
317
		$preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array();
318
319
		$preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array();
320
321
		if ( Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
322
			$beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array();
323
			return array_unique( array_merge( $preset_extensions, $beta_extensions ) );
324
		}
325
326
		return $preset_extensions;
327
	}
328
329
	/**
330
	 * Returns a diff from a combined list of whitelisted extensions and extensions determined to be excluded
331
	 *
332
	 * @param  array $whitelisted_extensions An array of whitelisted extensions.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $whitelisted_extensions not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
333
	 *
334
	 * @return array A list of blocks: eg array( 'publicize', 'markdown' )
335
	 */
336
	public static function get_available_extensions( $whitelisted_extensions = null ) {
337
		$exclusions             = get_option( 'jetpack_excluded_extensions', array() );
338
		$whitelisted_extensions = is_null( $whitelisted_extensions ) ? self::get_jetpack_gutenberg_extensions_whitelist() : $whitelisted_extensions;
339
340
		return array_diff( $whitelisted_extensions, $exclusions );
341
	}
342
343
	/**
344
	 * Get availability of each block / plugin.
345
	 *
346
	 * @return array A list of block and plugins and their availablity status
347
	 */
348
	public static function get_availability() {
349
		/**
350
		 * Fires before Gutenberg extensions availability is computed.
351
		 *
352
		 * In the function call you supply, use `jetpack_register_block()` to set a block as available.
353
		 * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and
354
		 * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered
355
		 * but marked as unavailable).
356
		 *
357
		 * @since 7.0.0
358
		 */
359
		do_action( 'jetpack_register_gutenberg_extensions' );
360
361
		$available_extensions = array();
362
363
		foreach ( self::$extensions as $extension ) {
364
			$is_available = self::is_registered( 'jetpack/' . $extension ) ||
365
			( isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ] );
366
367
			$available_extensions[ $extension ] = array(
368
				'available' => $is_available,
369
			);
370
371
			if ( ! $is_available ) {
372
				$reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ] : 'missing_module';
373
				$available_extensions[ $extension ]['unavailable_reason'] = $reason;
374
			}
375
		}
376
377
		return $available_extensions;
378
	}
379
380
	/**
381
	 * Check if an extension/block is already registered
382
	 *
383
	 * @since 7.2
384
	 *
385
	 * @param string $slug Name of extension/block to check.
386
	 *
387
	 * @return bool
388
	 */
389
	public static function is_registered( $slug ) {
390
		return WP_Block_Type_Registry::get_instance()->is_registered( $slug );
391
	}
392
393
	/**
394
	 * Check if Gutenberg editor is available
395
	 *
396
	 * @since 6.7.0
397
	 *
398
	 * @return bool
399
	 */
400
	public static function is_gutenberg_available() {
401
		return true;
402
	}
403
404
	/**
405
	 * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
406
	 *
407
	 * Loading blocks and plugins is enabled by default and may be disabled via filter:
408
	 *   add_filter( 'jetpack_gutenberg', '__return_false' );
409
	 *
410
	 * @since 6.9.0
411
	 *
412
	 * @return bool
413
	 */
414
	public static function should_load() {
415
		if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
416
			return false;
417
		}
418
419
		/**
420
		 * Filter to disable Gutenberg blocks
421
		 *
422
		 * @since 6.5.0
423
		 *
424
		 * @param bool true Whether to load Gutenberg blocks
425
		 */
426
		return (bool) apply_filters( 'jetpack_gutenberg', true );
427
	}
428
429
	/**
430
	 * Only enqueue block assets when needed.
431
	 *
432
	 * @param string $type Slug of the block.
433
	 * @param array  $script_dependencies Script dependencies. Will be merged with automatically
434
	 *                                    detected script dependencies from the webpack build.
435
	 *
436
	 * @return void
437
	 */
438
	public static function load_assets_as_required( $type, $script_dependencies = array() ) {
439
		if ( is_admin() ) {
440
			// A block's view assets will not be required in wp-admin.
441
			return;
442
		}
443
444
		$type = sanitize_title_with_dashes( $type );
445
		self::load_styles_as_required( $type );
446
		self::load_scripts_as_required( $type, $script_dependencies );
447
	}
448
449
	/**
450
	 * Only enqueue block sytles when needed.
451
	 *
452
	 * @param string $type Slug of the block.
453
	 *
454
	 * @since 7.2.0
455
	 *
456
	 * @return void
457
	 */
458
	public static function load_styles_as_required( $type ) {
459
		if ( is_admin() ) {
460
			// A block's view assets will not be required in wp-admin.
461
			return;
462
		}
463
464
		// Enqueue styles.
465
		$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
466 View Code Duplication
		if ( self::block_has_asset( $style_relative_path ) ) {
467
			$style_version = self::get_asset_version( $style_relative_path );
468
			$view_style    = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
469
			wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
470
		}
471
472
	}
473
474
	/**
475
	 * Only enqueue block scripts when needed.
476
	 *
477
	 * @param string $type Slug of the block.
478
	 * @param array  $dependencies Script dependencies. Will be merged with automatically
479
	 *                             detected script dependencies from the webpack build.
480
	 *
481
	 * @since 7.2.0
482
	 *
483
	 * @return void
484
	 */
485
	public static function load_scripts_as_required( $type, $dependencies = array() ) {
486
		if ( is_admin() ) {
487
			// A block's view assets will not be required in wp-admin.
488
			return;
489
		}
490
491
		// Enqueue script.
492
		$script_relative_path = self::get_blocks_directory() . $type . '/view.js';
493
		$script_deps_path     = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.deps.json';
494
495
		$script_dependencies = file_exists( $script_deps_path )
496
			? json_decode( file_get_contents( $script_deps_path ) )
497
			: array();
498
		$script_dependencies = array_merge( $script_dependencies, $dependencies, array( 'wp-polyfill' ) );
499
500 View Code Duplication
		if ( self::block_has_asset( $script_relative_path ) ) {
501
			$script_version = self::get_asset_version( $script_relative_path );
502
			$view_script    = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
503
			wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
504
		}
505
506
		wp_localize_script(
507
			'jetpack-block-' . $type,
508
			'Jetpack_Block_Assets_Base_Url',
509
			plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE )
510
		);
511
	}
512
513
	/**
514
	 * Check if an asset exists for a block.
515
	 *
516
	 * @param string $file Path of the file we are looking for.
517
	 *
518
	 * @return bool $block_has_asset Does the file exist.
519
	 */
520
	public static function block_has_asset( $file ) {
521
		return file_exists( JETPACK__PLUGIN_DIR . $file );
522
	}
523
524
	/**
525
	 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
526
	 *
527
	 * @param string $file Path of the file we are looking for.
528
	 *
529
	 * @return string $script_version Version number.
530
	 */
531
	public static function get_asset_version( $file ) {
532
		return Jetpack::is_development_version() && self::block_has_asset( $file )
533
			? filemtime( JETPACK__PLUGIN_DIR . $file )
534
			: JETPACK__VERSION;
535
	}
536
537
	/**
538
	 * Load Gutenberg editor assets
539
	 *
540
	 * @since 6.7.0
541
	 *
542
	 * @return void
543
	 */
544
	public static function enqueue_block_editor_assets() {
545
		if ( ! self::should_load() ) {
546
			return;
547
		}
548
549
		$rtl        = is_rtl() ? '.rtl' : '';
550
		$beta       = Jetpack_Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
551
		$blocks_dir = self::get_blocks_directory();
552
553
		$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE );
554
		$editor_style  = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );
555
556
		$editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$beta}.deps.json";
557
		$editor_deps      = file_exists( $editor_deps_path )
558
			? json_decode( file_get_contents( $editor_deps_path ) )
559
			: array();
560
		$editor_deps[]    = 'wp-polyfill';
561
562
		$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
563
			? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
564
			: JETPACK__VERSION;
565
566 View Code Duplication
		if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) {
567
			$site_fragment = Jetpack::build_raw_urls( home_url() );
568
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
569
			$site_fragment = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
570
		} else {
571
			$site_fragment = '';
572
		}
573
574
		wp_enqueue_script(
575
			'jetpack-blocks-editor',
576
			$editor_script,
577
			$editor_deps,
578
			$version,
579
			false
580
		);
581
582
		wp_localize_script(
583
			'jetpack-blocks-editor',
584
			'Jetpack_Block_Assets_Base_Url',
585
			plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE )
586
		);
587
588
		wp_localize_script(
589
			'jetpack-blocks-editor',
590
			'Jetpack_Editor_Initial_State',
591
			array(
592
				'available_blocks' => self::get_availability(),
593
				'jetpack'          => array( 'is_active' => Jetpack::is_active() ),
594
				'siteFragment'     => $site_fragment,
595
			)
596
		);
597
598
		wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack', plugins_url( 'languages/json', JETPACK__PLUGIN_FILE ) );
599
600
		// Adding a filter late to allow every other filter to process the path, including the CDN.
601
		add_filter( 'pre_load_script_translations', array( __CLASS__, 'filter_pre_load_script_translations' ), 1000, 3 );
602
603
		wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
604
	}
605
606
	/**
607
	 * A workaround for setting i18n data for WordPress client-side i18n mechanism.
608
	 * We are not yet using dotorg language packs for the editor file, so this short-circuits
609
	 * the translation loading and feeds our JSON data directly into the translation getter.
610
	 *
611
	 * @param NULL   $null     not used.
612
	 * @param String $file     the file path that is being loaded, ignored.
613
	 * @param String $handle   the script handle.
614
	 * @return NULL|String the translation data only if we're working with our handle.
615
	 */
616
	public static function filter_pre_load_script_translations( $null, $file, $handle ) {
617
		if ( 'jetpack-blocks-editor' !== $handle ) {
618
			return null;
619
		}
620
621
		return Jetpack::get_i18n_data_json();
622
	}
623
624
	/**
625
	 * Some blocks do not depend on a specific module,
626
	 * and can consequently be loaded outside of the usual modules.
627
	 * We will look for such modules in the extensions/ directory.
628
	 *
629
	 * @since 7.1.0
630
	 */
631
	public static function load_independent_blocks() {
632
		if ( self::should_load() ) {
633
			/**
634
			 * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
635
			 * If available, load them.
636
			 */
637
			foreach ( self::$extensions as $extension ) {
638
				$extension_file_glob = glob( JETPACK__PLUGIN_DIR . 'extensions/*/' . $extension . '/' . $extension . '.php' );
639
				if ( ! empty( $extension_file_glob ) ) {
640
					include_once $extension_file_glob[0];
641
				}
642
			}
643
		}
644
	}
645
}
646