Completed
Push — fix/709 ( 5dc225...9040d5 )
by
unknown
06:42
created

Jetpack_Gutenberg::blocks_variation()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 24
rs 9.536
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
use Automattic\Jetpack\Constants;
10
use Automattic\Jetpack\Status;
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 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...
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 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...
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 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
		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 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 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...
170
		} else {
171
			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...
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
	 * @param array  $details A free-form array containing more information on why the extension is unavailable.
190
	 */
191
	public static function set_extension_unavailable( $slug, $reason, $details = array() ) {
192
		if (
193
			// Extensions that require a plan may be eligible for upgrades.
194
			'missing_plan' === $reason
195
			&& (
196
				/**
197
				 * Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false`
198
				 * to disable paid feature upgrade nudges in the block editor.
199
				 *
200
				 * When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php`
201
				 * See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378
202
				 *
203
				 * @since 7.7.0
204
				 *
205
				 * @param boolean
206
				 */
207
				! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false )
208
				/** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
209
				|| ! apply_filters( 'jetpack_show_promotions', true )
210
			)
211
		) {
212
			// The block editor may apply an upgrade nudge if `missing_plan` is the reason.
213
			// Add a descriptive suffix to disable behavior but provide informative reason.
214
			$reason .= '__nudge_disabled';
215
		}
216
217
		self::$availability[ self::remove_extension_prefix( $slug ) ] = array(
218
			'reason'  => $reason,
219
			'details' => $details,
220
		);
221
	}
222
223
	/**
224
	 * Set the reason why an extension (block or plugin) is unavailable
225
	 *
226
	 * @deprecated 7.1.0 Use set_extension_unavailable() instead
227
	 *
228
	 * @param string $slug Slug of the extension.
229
	 * @param string $reason A string representation of why the extension is unavailable.
230
	 */
231
	public static function set_extension_unavailability_reason( $slug, $reason ) {
232
		_deprecated_function( __METHOD__, '7.1', 'Jetpack_Gutenberg::set_extension_unavailable' );
233
234
		self::set_extension_unavailable( $slug, $reason );
235
	}
236
237
	/**
238
	 * Set up a whitelist of allowed block editor extensions
239
	 *
240
	 * @return void
241
	 */
242
	public static function init() {
243
		if ( ! self::should_load() ) {
244
			return;
245
		}
246
247
		/**
248
		 * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
249
		 *
250
		 * @since 6.9.0
251
		 *
252
		 * @param boolean
253
		 */
254
		if ( apply_filters( 'jetpack_load_beta_blocks', false ) ) {
255
			Constants::set_constant( 'JETPACK_BETA_BLOCKS', true );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
256
		}
257
258
		/**
259
		 * Filter the whitelist of block editor extensions that are available through Jetpack.
260
		 *
261
		 * @since 7.0.0
262
		 *
263
		 * @param array
264
		 */
265
		self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() );
266
267
		/**
268
		 * Filter the whitelist of block editor plugins that are available through Jetpack.
269
		 *
270
		 * @deprecated 7.0.0 Use jetpack_set_available_extensions instead
271
		 *
272
		 * @since 6.8.0
273
		 *
274
		 * @param array
275
		 */
276
		self::$extensions = apply_filters( 'jetpack_set_available_blocks', self::$extensions );
277
278
		/**
279
		 * Filter the whitelist of block editor plugins that are available through Jetpack.
280
		 *
281
		 * @deprecated 7.0.0 Use jetpack_set_available_extensions instead
282
		 *
283
		 * @since 6.9.0
284
		 *
285
		 * @param array
286
		 */
287
		self::$extensions = apply_filters( 'jetpack_set_available_plugins', self::$extensions );
288
	}
289
290
	/**
291
	 * Resets the class to its original state
292
	 *
293
	 * Used in unit tests
294
	 *
295
	 * @return void
296
	 */
297
	public static function reset() {
298
		self::$extensions   = array();
299
		self::$availability = array();
300
	}
301
302
	/**
303
	 * Return the Gutenberg extensions (blocks and plugins) directory
304
	 *
305
	 * @return string The Gutenberg extensions directory
306
	 */
307
	public static function get_blocks_directory() {
308
		/**
309
		 * Filter to select Gutenberg blocks directory
310
		 *
311
		 * @since 6.9.0
312
		 *
313
		 * @param string default: '_inc/blocks/'
314
		 */
315
		return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
316
	}
317
318
	/**
319
	 * Checks for a given .json file in the blocks folder.
320
	 *
321
	 * @param string $preset The name of the .json file to look for.
322
	 *
323
	 * @return bool True if the file is found.
324
	 */
325
	public static function preset_exists( $preset ) {
326
		return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
327
	}
328
329
	/**
330
	 * Decodes JSON loaded from a preset file in the blocks folder
331
	 *
332
	 * @param string $preset The name of the .json file to load.
333
	 *
334
	 * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
335
	 */
336
	public static function get_preset( $preset ) {
337
		return json_decode(
338
			// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
339
			file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
340
		);
341
	}
342
343
	/**
344
	 * Returns a whitelist of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
345
	 *
346
	 * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
347
	 */
348
	public static function get_jetpack_gutenberg_extensions_whitelist() {
349
		$preset_extensions_manifest = self::preset_exists( 'index' ) ? self::get_preset( 'index' ) : (object) array();
350
351
		$preset_extensions = isset( $preset_extensions_manifest->production ) ? (array) $preset_extensions_manifest->production : array();
352
353
		if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) {
354
			$beta_extensions = isset( $preset_extensions_manifest->beta ) ? (array) $preset_extensions_manifest->beta : array();
355
			return array_unique( array_merge( $preset_extensions, $beta_extensions ) );
356
		}
357
358
		return $preset_extensions;
359
	}
360
361
	/**
362
	 * Returns a diff from a combined list of whitelisted extensions and extensions determined to be excluded
363
	 *
364
	 * @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...
365
	 *
366
	 * @return array A list of blocks: eg array( 'publicize', 'markdown' )
367
	 */
368
	public static function get_available_extensions( $whitelisted_extensions = null ) {
369
		$exclusions             = get_option( 'jetpack_excluded_extensions', array() );
370
		$whitelisted_extensions = is_null( $whitelisted_extensions ) ? self::get_jetpack_gutenberg_extensions_whitelist() : $whitelisted_extensions;
371
372
		return array_diff( $whitelisted_extensions, $exclusions );
373
	}
374
375
	/**
376
	 * Get availability of each block / plugin.
377
	 *
378
	 * @return array A list of block and plugins and their availablity status
379
	 */
380
	public static function get_availability() {
381
		/**
382
		 * Fires before Gutenberg extensions availability is computed.
383
		 *
384
		 * In the function call you supply, use `jetpack_register_block()` to set a block as available.
385
		 * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and
386
		 * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered
387
		 * but marked as unavailable).
388
		 *
389
		 * @since 7.0.0
390
		 */
391
		do_action( 'jetpack_register_gutenberg_extensions' );
392
393
		$available_extensions = array();
394
395
		foreach ( self::$extensions as $extension ) {
396
			$is_available = self::is_registered( 'jetpack/' . $extension ) ||
397
			( isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ] );
398
399
			$available_extensions[ $extension ] = array(
400
				'available' => $is_available,
401
			);
402
403
			if ( ! $is_available ) {
404
				$reason  = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['reason'] : 'missing_module';
405
				$details = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['details'] : array();
406
				$available_extensions[ $extension ]['unavailable_reason'] = $reason;
407
				$available_extensions[ $extension ]['details']            = $details;
408
			}
409
		}
410
411
		return $available_extensions;
412
	}
413
414
	/**
415
	 * Check if an extension/block is already registered
416
	 *
417
	 * @since 7.2
418
	 *
419
	 * @param string $slug Name of extension/block to check.
420
	 *
421
	 * @return bool
422
	 */
423
	public static function is_registered( $slug ) {
424
		return WP_Block_Type_Registry::get_instance()->is_registered( $slug );
425
	}
426
427
	/**
428
	 * Check if Gutenberg editor is available
429
	 *
430
	 * @since 6.7.0
431
	 *
432
	 * @return bool
433
	 */
434
	public static function is_gutenberg_available() {
435
		return true;
436
	}
437
438
	/**
439
	 * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
440
	 *
441
	 * Loading blocks and plugins is enabled by default and may be disabled via filter:
442
	 *   add_filter( 'jetpack_gutenberg', '__return_false' );
443
	 *
444
	 * @since 6.9.0
445
	 *
446
	 * @return bool
447
	 */
448
	public static function should_load() {
449
		if ( ! Jetpack::is_active() && ! ( new Status() )->is_development_mode() ) {
450
			return false;
451
		}
452
453
		/**
454
		 * Filter to disable Gutenberg blocks
455
		 *
456
		 * @since 6.5.0
457
		 *
458
		 * @param bool true Whether to load Gutenberg blocks
459
		 */
460
		return (bool) apply_filters( 'jetpack_gutenberg', true );
461
	}
462
463
	/**
464
	 * Only enqueue block assets when needed.
465
	 *
466
	 * @param string $type Slug of the block.
467
	 * @param array  $script_dependencies Script dependencies. Will be merged with automatically
468
	 *                                    detected script dependencies from the webpack build.
469
	 *
470
	 * @return void
471
	 */
472
	public static function load_assets_as_required( $type, $script_dependencies = array() ) {
473
		if ( is_admin() ) {
474
			// A block's view assets will not be required in wp-admin.
475
			return;
476
		}
477
478
		$type = sanitize_title_with_dashes( $type );
479
		self::load_styles_as_required( $type );
480
		self::load_scripts_as_required( $type, $script_dependencies );
481
	}
482
483
	/**
484
	 * Only enqueue block sytles when needed.
485
	 *
486
	 * @param string $type Slug of the block.
487
	 *
488
	 * @since 7.2.0
489
	 *
490
	 * @return void
491
	 */
492
	public static function load_styles_as_required( $type ) {
493
		if ( is_admin() ) {
494
			// A block's view assets will not be required in wp-admin.
495
			return;
496
		}
497
498
		// Enqueue styles.
499
		$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
500 View Code Duplication
		if ( self::block_has_asset( $style_relative_path ) ) {
501
			$style_version = self::get_asset_version( $style_relative_path );
502
			$view_style    = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
503
			wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
504
		}
505
506
	}
507
508
	/**
509
	 * Only enqueue block scripts when needed.
510
	 *
511
	 * @param string $type Slug of the block.
512
	 * @param array  $dependencies Script dependencies. Will be merged with automatically
513
	 *                             detected script dependencies from the webpack build.
514
	 *
515
	 * @since 7.2.0
516
	 *
517
	 * @return void
518
	 */
519
	public static function load_scripts_as_required( $type, $dependencies = array() ) {
520
		if ( is_admin() ) {
521
			// A block's view assets will not be required in wp-admin.
522
			return;
523
		}
524
525
		// Enqueue script.
526
		$script_relative_path = self::get_blocks_directory() . $type . '/view.js';
527
		$script_deps_path     = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php';
528
		$script_dependencies  = array( 'wp-polyfill' );
529
		if ( file_exists( $script_deps_path ) ) {
530
			$asset_manifest      = include $script_deps_path;
531
			$script_dependencies = $asset_manifest['dependencies'];
532
		}
533
534
		if ( ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) && self::block_has_asset( $script_relative_path ) ) {
535
			$script_version = self::get_asset_version( $script_relative_path );
536
			$view_script    = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
537
			wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
538
		}
539
540
		wp_localize_script(
541
			'jetpack-block-' . $type,
542
			'Jetpack_Block_Assets_Base_Url',
543
			plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE )
544
		);
545
	}
546
547
	/**
548
	 * Check if an asset exists for a block.
549
	 *
550
	 * @param string $file Path of the file we are looking for.
551
	 *
552
	 * @return bool $block_has_asset Does the file exist.
553
	 */
554
	public static function block_has_asset( $file ) {
555
		return file_exists( JETPACK__PLUGIN_DIR . $file );
556
	}
557
558
	/**
559
	 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
560
	 *
561
	 * @param string $file Path of the file we are looking for.
562
	 *
563
	 * @return string $script_version Version number.
564
	 */
565
	public static function get_asset_version( $file ) {
566
		return Jetpack::is_development_version() && self::block_has_asset( $file )
567
			? filemtime( JETPACK__PLUGIN_DIR . $file )
568
			: JETPACK__VERSION;
569
	}
570
571
	/**
572
	 * Load Gutenberg editor assets
573
	 *
574
	 * @since 6.7.0
575
	 *
576
	 * @return void
577
	 */
578
	public static function enqueue_block_editor_assets() {
579
		if ( ! self::should_load() ) {
580
			return;
581
		}
582
583
		// Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php.
584
		if ( ! ( new Status() )->is_development_mode() && Jetpack::is_active() ) {
585
			wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
586
		}
587
588
		$rtl        = is_rtl() ? '.rtl' : '';
589
		$beta       = Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
590
		$blocks_dir = self::get_blocks_directory();
591
592
		$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE );
593
		$editor_style  = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );
594
595
		$editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$beta}.asset.php";
596
		$editor_deps      = array( 'wp-polyfill' );
597
		if ( file_exists( $editor_deps_path ) ) {
598
			$asset_manifest = include $editor_deps_path;
599
			$editor_deps    = $asset_manifest['dependencies'];
600
		}
601
602
		$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
603
			? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
604
			: JETPACK__VERSION;
605
606
		if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) {
607
			$site_fragment = Jetpack::build_raw_urls( home_url() );
608
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
609
			$site_fragment = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
610
		} else {
611
			$site_fragment = '';
612
		}
613
614
		wp_enqueue_script(
615
			'jetpack-blocks-editor',
616
			$editor_script,
617
			$editor_deps,
618
			$version,
619
			false
620
		);
621
622
		wp_localize_script(
623
			'jetpack-blocks-editor',
624
			'Jetpack_Block_Assets_Base_Url',
625
			plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE )
626
		);
627
628
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
629
			$user      = wp_get_current_user();
630
			$user_data = array(
631
				'userid'   => $user->ID,
632
				'username' => $user->user_login,
633
			);
634
			$blog_id   = get_current_blog_id();
635
		} else {
636
			$user_data = Jetpack_Tracks_Client::get_connected_user_tracks_identity();
637
			$blog_id   = Jetpack_Options::get_option( 'id', 0 );
638
		}
639
640
		wp_localize_script(
641
			'jetpack-blocks-editor',
642
			'Jetpack_Editor_Initial_State',
643
			array(
644
				'available_blocks' => self::get_availability(),
645
				'jetpack'          => array( 'is_active' => Jetpack::is_active() ),
646
				'siteFragment'     => $site_fragment,
647
				'tracksUserData'   => $user_data,
648
				'wpcomBlogId'      => $blog_id,
649
			)
650
		);
651
652
		wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack' );
653
654
		wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version );
655
	}
656
657
	/**
658
	 * Some blocks do not depend on a specific module,
659
	 * and can consequently be loaded outside of the usual modules.
660
	 * We will look for such modules in the extensions/ directory.
661
	 *
662
	 * @since 7.1.0
663
	 */
664
	public static function load_independent_blocks() {
665
		if ( self::should_load() ) {
666
			/**
667
			 * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
668
			 * If available, load them.
669
			 */
670
			foreach ( self::$extensions as $extension ) {
671
				$extension_file_glob = glob( JETPACK__PLUGIN_DIR . 'extensions/*/' . $extension . '/' . $extension . '.php' );
672
				if ( ! empty( $extension_file_glob ) ) {
673
					include_once $extension_file_glob[0];
674
				}
675
			}
676
		}
677
	}
678
679
	/**
680
	 * Get CSS classes for a block.
681
	 *
682
	 * @since 7.7.0
683
	 *
684
	 * @param string $slug  Block slug.
685
	 * @param array  $attr  Block attributes.
686
	 * @param array  $extra Potential extra classes you may want to provide.
687
	 *
688
	 * @return string $classes List of CSS classes for a block.
689
	 */
690
	public static function block_classes( $slug = '', $attr, $extra = array() ) {
691
		if ( empty( $slug ) ) {
692
			return '';
693
		}
694
695
		// Basic block name class.
696
		$classes = array(
697
			'wp-block-jetpack-' . $slug,
698
		);
699
700
		// Add alignment if provided.
701
		if (
702
			! empty( $attr['align'] )
703
			&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true )
704
		) {
705
			array_push( $classes, 'align' . $attr['align'] );
706
		}
707
708
		// Add custom classes if provided in the block editor.
709
		if ( ! empty( $attr['className'] ) ) {
710
			array_push( $classes, $attr['className'] );
711
		}
712
713
		// Add any extra classes.
714
		if ( is_array( $extra ) && ! empty( $extra ) ) {
715
			$classes = array_merge( $classes, $extra );
716
		}
717
718
		return implode( ' ', $classes );
719
	}
720
}
721