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
|
|
|
if ( isset( $args['version_requirements'] ) |
31
|
|
|
&& ! Jetpack_Gutenberg::is_gutenberg_version_available( $args['version_requirements'], $slug ) ) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Checking whether block is registered to ensure it isn't registered twice. |
36
|
|
|
if ( Jetpack_Gutenberg::is_registered( $slug ) ) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$feature_name = Jetpack_Gutenberg::remove_extension_prefix( $slug ); |
41
|
|
|
// If the block is dynamic, and a Jetpack block, wrap the render_callback to check availability. |
42
|
|
|
if ( |
43
|
|
|
isset( $args['plan_check'], $args['render_callback'] ) |
44
|
|
|
&& true === $args['plan_check'] |
45
|
|
|
) { |
46
|
|
|
$args['render_callback'] = Jetpack_Gutenberg::get_render_callback_with_availability_check( $feature_name, $args['render_callback'] ); |
47
|
|
|
$method_name = 'set_availability_for_plan'; |
48
|
|
|
} else { |
49
|
|
|
$method_name = 'set_extension_available'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
add_action( |
53
|
|
|
'jetpack_register_gutenberg_extensions', |
54
|
|
|
function() use ( $feature_name, $method_name ) { |
55
|
|
|
call_user_func( array( 'Jetpack_Gutenberg', $method_name ), $feature_name ); |
56
|
|
|
} |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return register_block_type( $slug, $args ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* General Gutenberg editor specific functionality |
64
|
|
|
*/ |
65
|
|
|
class Jetpack_Gutenberg { |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Only these extensions can be registered. Used to control availability of beta blocks. |
69
|
|
|
* |
70
|
|
|
* @var array Extensions allowed list. |
71
|
|
|
*/ |
72
|
|
|
private static $extensions = array(); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Keeps track of the reasons why a given extension is unavailable. |
76
|
|
|
* |
77
|
|
|
* @var array Extensions availability information |
78
|
|
|
*/ |
79
|
|
|
private static $availability = array(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
83
|
|
|
* php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
84
|
|
|
* optionally fall back to that. |
85
|
|
|
* |
86
|
|
|
* @param array $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version. |
87
|
|
|
* @param string $slug The slug of the block or plugin that has the gutenberg version requirement. |
88
|
|
|
* |
89
|
|
|
* @since 8.3.0 |
90
|
|
|
* |
91
|
|
|
* @return boolean True if the version of gutenberg required by the block or plugin is available. |
92
|
|
|
*/ |
93
|
|
|
public static function is_gutenberg_version_available( $version_requirements, $slug ) { |
94
|
|
|
global $wp_version; |
95
|
|
|
|
96
|
|
|
// Bail if we don't at least have the gutenberg version requirement, the WP version is optional. |
97
|
|
|
if ( empty( $version_requirements['gutenberg'] ) ) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// If running a local dev build of gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version. |
102
|
|
|
if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) { |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$version_available = false; |
107
|
|
|
|
108
|
|
|
// If running a production build of the gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version |
109
|
|
|
// with required version of Gutenberg is known check that. |
110
|
|
|
if ( defined( 'GUTENBERG_VERSION' ) ) { |
111
|
|
|
$version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' ); |
112
|
|
|
} elseif ( ! empty( $version_requirements['wp'] ) ) { |
113
|
|
|
$version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ( ! $version_available ) { |
117
|
|
|
self::set_extension_unavailable( |
118
|
|
|
$slug, |
119
|
|
|
'incorrect_gutenberg_version', |
120
|
|
|
array( |
121
|
|
|
'required_feature' => $slug, |
122
|
|
|
'required_version' => $version_requirements, |
123
|
|
|
'current_version' => array( |
124
|
|
|
'wp' => $wp_version, |
125
|
|
|
'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null, |
126
|
|
|
), |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $version_available; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Remove the 'jetpack/' or jetpack-' prefix from an extension name |
136
|
|
|
* |
137
|
|
|
* @param string $extension_name The extension name. |
138
|
|
|
* |
139
|
|
|
* @return string The unprefixed extension name. |
140
|
|
|
*/ |
141
|
|
|
public static function remove_extension_prefix( $extension_name ) { |
142
|
|
|
if ( 0 === strpos( $extension_name, 'jetpack/' ) || 0 === strpos( $extension_name, 'jetpack-' ) ) { |
143
|
|
|
return substr( $extension_name, strlen( 'jetpack/' ) ); |
144
|
|
|
} |
145
|
|
|
return $extension_name; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Whether two arrays share at least one item |
150
|
|
|
* |
151
|
|
|
* @param array $a An array. |
152
|
|
|
* @param array $b Another array. |
153
|
|
|
* |
154
|
|
|
* @return boolean True if $a and $b share at least one item |
155
|
|
|
*/ |
156
|
|
|
protected static function share_items( $a, $b ) { |
157
|
|
|
return count( array_intersect( $a, $b ) ) > 0; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set a (non-block) extension as available |
162
|
|
|
* |
163
|
|
|
* @param string $slug Slug of the extension. |
164
|
|
|
*/ |
165
|
|
|
public static function set_extension_available( $slug ) { |
166
|
|
|
self::$availability[ self::remove_extension_prefix( $slug ) ] = true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set the reason why an extension (block or plugin) is unavailable |
171
|
|
|
* |
172
|
|
|
* @param string $slug Slug of the extension. |
173
|
|
|
* @param string $reason A string representation of why the extension is unavailable. |
174
|
|
|
* @param array $details A free-form array containing more information on why the extension is unavailable. |
175
|
|
|
*/ |
176
|
|
|
public static function set_extension_unavailable( $slug, $reason, $details = array() ) { |
177
|
|
|
if ( |
178
|
|
|
// Extensions that require a plan may be eligible for upgrades. |
179
|
|
|
'missing_plan' === $reason |
180
|
|
|
&& ( |
181
|
|
|
/** |
182
|
|
|
* Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false` |
183
|
|
|
* to disable paid feature upgrade nudges in the block editor. |
184
|
|
|
* |
185
|
|
|
* When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php` |
186
|
|
|
* See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378 |
187
|
|
|
* |
188
|
|
|
* @since 7.7.0 |
189
|
|
|
* |
190
|
|
|
* @param boolean |
191
|
|
|
*/ |
192
|
|
|
! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ) |
193
|
|
|
/** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */ |
194
|
|
|
|| ! apply_filters( 'jetpack_show_promotions', true ) |
195
|
|
|
) |
196
|
|
|
) { |
197
|
|
|
// The block editor may apply an upgrade nudge if `missing_plan` is the reason. |
198
|
|
|
// Add a descriptive suffix to disable behavior but provide informative reason. |
199
|
|
|
$reason .= '__nudge_disabled'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
self::$availability[ self::remove_extension_prefix( $slug ) ] = array( |
203
|
|
|
'reason' => $reason, |
204
|
|
|
'details' => $details, |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set up a list 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
|
|
|
* Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks. |
231
|
|
|
* |
232
|
|
|
* @since 8.4.0 |
233
|
|
|
* |
234
|
|
|
* @param boolean |
235
|
|
|
*/ |
236
|
|
|
if ( apply_filters( 'jetpack_load_experimental_blocks', false ) ) { |
237
|
|
|
Constants::set_constant( 'JETPACK_EXPERIMENTAL_BLOCKS', true ); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Filter the list of block editor extensions that are available through Jetpack. |
242
|
|
|
* |
243
|
|
|
* @since 7.0.0 |
244
|
|
|
* |
245
|
|
|
* @param array |
246
|
|
|
*/ |
247
|
|
|
self::$extensions = apply_filters( 'jetpack_set_available_extensions', array(), self::blocks_variation() ); |
|
|
|
|
248
|
|
|
|
249
|
|
|
// exclude blocks banned using a database option |
250
|
|
|
$exclusions = get_option( 'jetpack_excluded_extensions', false ); |
251
|
|
|
if ( is_array( $exclusions ) ) { |
252
|
|
|
self::$extensions = array_diff( self::$extensions, $exclusions ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
error_log( 'available blocks ' . json_encode( 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
|
|
|
* Returns a list of Jetpack Gutenberg extensions (blocks and plugins) |
288
|
|
|
* |
289
|
|
|
* @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
290
|
|
|
*/ |
291
|
|
|
public static function get_jetpack_gutenberg_extensions_allowed_list() { |
292
|
|
|
$preset_extensions_manifest = (object) array(); |
293
|
|
|
$blocks_variation = self::blocks_variation(); |
294
|
|
|
|
295
|
|
|
return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Returns a list of Jetpack Gutenberg extensions (blocks and plugins) |
300
|
|
|
* |
301
|
|
|
* @deprecated 8.7.0 Use get_jetpack_gutenberg_extensions_allowed_list() |
302
|
|
|
* |
303
|
|
|
* @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
304
|
|
|
*/ |
305
|
|
|
public static function get_jetpack_gutenberg_extensions_whitelist() { |
306
|
|
|
_deprecated_function( __FUNCTION__, 'jetpack-8.7.0', 'Jetpack_Gutenberg::get_jetpack_gutenberg_extensions_allowed_list' ); |
307
|
|
|
return self::get_jetpack_gutenberg_extensions_allowed_list(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Return true if the extension has been registered and there's nothing in the availablilty array. |
312
|
|
|
* |
313
|
|
|
* @param string $extension The name of the extension. |
314
|
|
|
* |
315
|
|
|
* @return bool whether the extension has been registered and there's nothing in the availablilty array. |
316
|
|
|
*/ |
317
|
|
|
public static function is_registered_and_no_entry_in_availability( $extension ) { |
318
|
|
|
return self::is_registered( 'jetpack/' . $extension ) && ! isset( self::$availability[ $extension ] ); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Return true if the extension has a true entry in the availablilty array. |
323
|
|
|
* |
324
|
|
|
* @param string $extension The name of the extension. |
325
|
|
|
* |
326
|
|
|
* @return bool whether the extension has a true entry in the availablilty array. |
327
|
|
|
*/ |
328
|
|
|
public static function is_available( $extension ) { |
329
|
|
|
return isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Get availability of each block / plugin. |
334
|
|
|
* |
335
|
|
|
* @return array A list of block and plugins and their availablity status |
336
|
|
|
*/ |
337
|
|
|
public static function get_availability() { |
338
|
|
|
/** |
339
|
|
|
* Fires before Gutenberg extensions availability is computed. |
340
|
|
|
* |
341
|
|
|
* In the function call you supply, use `jetpack_register_block()` to set a block as available. |
342
|
|
|
* Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and |
343
|
|
|
* `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered |
344
|
|
|
* but marked as unavailable). |
345
|
|
|
* |
346
|
|
|
* @since 7.0.0 |
347
|
|
|
*/ |
348
|
|
|
do_action( 'jetpack_register_gutenberg_extensions' ); |
349
|
|
|
|
350
|
|
|
$available_extensions = array(); |
351
|
|
|
|
352
|
|
|
foreach ( self::$extensions as $extension ) { |
353
|
|
|
$is_available = self::is_registered_and_no_entry_in_availability( $extension ) || self::is_available( $extension ); |
354
|
|
|
$available_extensions[ $extension ] = array( |
355
|
|
|
'available' => $is_available, |
356
|
|
|
); |
357
|
|
|
|
358
|
|
|
if ( ! $is_available ) { |
359
|
|
|
$reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['reason'] : 'missing_module'; |
360
|
|
|
$details = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['details'] : array(); |
361
|
|
|
$available_extensions[ $extension ]['unavailable_reason'] = $reason; |
362
|
|
|
$available_extensions[ $extension ]['details'] = $details; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
return $available_extensions; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Check if an extension/block is already registered |
371
|
|
|
* |
372
|
|
|
* @since 7.2 |
373
|
|
|
* |
374
|
|
|
* @param string $slug Name of extension/block to check. |
375
|
|
|
* |
376
|
|
|
* @return bool |
377
|
|
|
*/ |
378
|
|
|
public static function is_registered( $slug ) { |
379
|
|
|
return WP_Block_Type_Registry::get_instance()->is_registered( $slug ); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Check if Gutenberg editor is available |
384
|
|
|
* |
385
|
|
|
* @since 6.7.0 |
386
|
|
|
* |
387
|
|
|
* @return bool |
388
|
|
|
*/ |
389
|
|
|
public static function is_gutenberg_available() { |
390
|
|
|
return true; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded |
395
|
|
|
* |
396
|
|
|
* Loading blocks and plugins is enabled by default and may be disabled via filter: |
397
|
|
|
* add_filter( 'jetpack_gutenberg', '__return_false' ); |
398
|
|
|
* |
399
|
|
|
* @since 6.9.0 |
400
|
|
|
* |
401
|
|
|
* @return bool |
402
|
|
|
*/ |
403
|
|
|
public static function should_load() { |
404
|
|
|
if ( ! Jetpack::is_active() && ! ( new Status() )->is_development_mode() ) { |
405
|
|
|
return false; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Filter to disable Gutenberg blocks |
410
|
|
|
* |
411
|
|
|
* @since 6.5.0 |
412
|
|
|
* |
413
|
|
|
* @param bool true Whether to load Gutenberg blocks |
414
|
|
|
*/ |
415
|
|
|
return (bool) apply_filters( 'jetpack_gutenberg', true ); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Only enqueue block assets when needed. |
420
|
|
|
* |
421
|
|
|
* @param string $type Slug of the block. |
422
|
|
|
* @param array $script_dependencies Script dependencies. Will be merged with automatically |
423
|
|
|
* detected script dependencies from the webpack build. |
424
|
|
|
* |
425
|
|
|
* @return void |
426
|
|
|
*/ |
427
|
|
|
public static function load_assets_as_required( $type, $script_dependencies = array() ) { |
428
|
|
|
if ( is_admin() ) { |
429
|
|
|
// A block's view assets will not be required in wp-admin. |
430
|
|
|
return; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
$type = sanitize_title_with_dashes( $type ); |
434
|
|
|
self::load_styles_as_required( $type ); |
435
|
|
|
self::load_scripts_as_required( $type, $script_dependencies ); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Only enqueue block sytles when needed. |
440
|
|
|
* |
441
|
|
|
* @param string $type Slug of the block. |
442
|
|
|
* |
443
|
|
|
* @since 7.2.0 |
444
|
|
|
* |
445
|
|
|
* @return void |
446
|
|
|
*/ |
447
|
|
|
public static function load_styles_as_required( $type ) { |
448
|
|
|
if ( is_admin() ) { |
449
|
|
|
// A block's view assets will not be required in wp-admin. |
450
|
|
|
return; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
// Enqueue styles. |
454
|
|
|
$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css'; |
455
|
|
|
if ( self::block_has_asset( $style_relative_path ) ) { |
456
|
|
|
$style_version = self::get_asset_version( $style_relative_path ); |
457
|
|
|
$view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
458
|
|
|
wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version ); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Only enqueue block scripts 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
|
|
|
* @since 7.2.0 |
471
|
|
|
* |
472
|
|
|
* @return void |
473
|
|
|
*/ |
474
|
|
|
public static function load_scripts_as_required( $type, $script_dependencies = array() ) { |
475
|
|
|
if ( is_admin() ) { |
476
|
|
|
// A block's view assets will not be required in wp-admin. |
477
|
|
|
return; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
// Enqueue script. |
481
|
|
|
$script_relative_path = self::get_blocks_directory() . $type . '/view.js'; |
482
|
|
|
$script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php'; |
483
|
|
|
$script_dependencies[] = 'wp-polyfill'; |
484
|
|
|
if ( file_exists( $script_deps_path ) ) { |
485
|
|
|
$asset_manifest = include $script_deps_path; |
486
|
|
|
$script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) ); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
if ( ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) && self::block_has_asset( $script_relative_path ) ) { |
490
|
|
|
$script_version = self::get_asset_version( $script_relative_path ); |
491
|
|
|
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
492
|
|
|
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
wp_localize_script( |
496
|
|
|
'jetpack-block-' . $type, |
497
|
|
|
'Jetpack_Block_Assets_Base_Url', |
498
|
|
|
plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ) |
499
|
|
|
); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Check if an asset exists for a block. |
504
|
|
|
* |
505
|
|
|
* @param string $file Path of the file we are looking for. |
506
|
|
|
* |
507
|
|
|
* @return bool $block_has_asset Does the file exist. |
508
|
|
|
*/ |
509
|
|
|
public static function block_has_asset( $file ) { |
510
|
|
|
return file_exists( JETPACK__PLUGIN_DIR . $file ); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Get the version number to use when loading the file. Allows us to bypass cache when developing. |
515
|
|
|
* |
516
|
|
|
* @param string $file Path of the file we are looking for. |
517
|
|
|
* |
518
|
|
|
* @return string $script_version Version number. |
519
|
|
|
*/ |
520
|
|
|
public static function get_asset_version( $file ) { |
521
|
|
|
return Jetpack::is_development_version() && self::block_has_asset( $file ) |
522
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . $file ) |
523
|
|
|
: JETPACK__VERSION; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Load Gutenberg editor assets |
528
|
|
|
* |
529
|
|
|
* @since 6.7.0 |
530
|
|
|
* |
531
|
|
|
* @return void |
532
|
|
|
*/ |
533
|
|
|
public static function enqueue_block_editor_assets() { |
534
|
|
|
if ( ! self::should_load() ) { |
535
|
|
|
return; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
// Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php. |
539
|
|
|
if ( ! ( new Status() )->is_development_mode() && Jetpack::is_active() ) { |
540
|
|
|
wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
$rtl = is_rtl() ? '.rtl' : ''; |
544
|
|
|
$blocks_dir = self::get_blocks_directory(); |
545
|
|
|
$blocks_variation = self::blocks_variation(); |
546
|
|
|
|
547
|
|
|
if ( 'production' !== $blocks_variation ) { |
548
|
|
|
$blocks_env = '-' . esc_attr( $blocks_variation ); |
549
|
|
|
} else { |
550
|
|
|
$blocks_env = ''; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
$editor_script = plugins_url( "{$blocks_dir}editor{$blocks_env}.js", JETPACK__PLUGIN_FILE ); |
554
|
|
|
$editor_style = plugins_url( "{$blocks_dir}editor{$blocks_env}{$rtl}.css", JETPACK__PLUGIN_FILE ); |
555
|
|
|
|
556
|
|
|
$editor_deps_path = JETPACK__PLUGIN_DIR . $blocks_dir . "editor{$blocks_env}.asset.php"; |
557
|
|
|
$editor_deps = array( 'wp-polyfill' ); |
558
|
|
|
if ( file_exists( $editor_deps_path ) ) { |
559
|
|
|
$asset_manifest = include $editor_deps_path; |
560
|
|
|
$editor_deps = $asset_manifest['dependencies']; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
564
|
|
|
? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' ) |
565
|
|
|
: JETPACK__VERSION; |
566
|
|
|
|
567
|
|
|
if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
568
|
|
|
$site_fragment = Jetpack::build_raw_urls( home_url() ); |
569
|
|
|
} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
570
|
|
|
$site_fragment = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
571
|
|
|
} else { |
572
|
|
|
$site_fragment = ''; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
wp_enqueue_script( |
576
|
|
|
'jetpack-blocks-editor', |
577
|
|
|
$editor_script, |
578
|
|
|
$editor_deps, |
579
|
|
|
$version, |
580
|
|
|
false |
581
|
|
|
); |
582
|
|
|
|
583
|
|
|
wp_localize_script( |
584
|
|
|
'jetpack-blocks-editor', |
585
|
|
|
'Jetpack_Block_Assets_Base_Url', |
586
|
|
|
plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ) |
587
|
|
|
); |
588
|
|
|
|
589
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
590
|
|
|
$user = wp_get_current_user(); |
591
|
|
|
$user_data = array( |
592
|
|
|
'userid' => $user->ID, |
593
|
|
|
'username' => $user->user_login, |
594
|
|
|
); |
595
|
|
|
$blog_id = get_current_blog_id(); |
596
|
|
|
$is_current_user_connected = true; |
597
|
|
|
} else { |
598
|
|
|
$user_data = Jetpack_Tracks_Client::get_connected_user_tracks_identity(); |
599
|
|
|
$blog_id = Jetpack_Options::get_option( 'id', 0 ); |
600
|
|
|
$is_current_user_connected = Jetpack::is_user_connected(); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
wp_localize_script( |
604
|
|
|
'jetpack-blocks-editor', |
605
|
|
|
'Jetpack_Editor_Initial_State', |
606
|
|
|
array( |
607
|
|
|
'available_blocks' => self::get_availability(), |
608
|
|
|
'jetpack' => array( |
609
|
|
|
'is_active' => Jetpack::is_active(), |
610
|
|
|
'is_current_user_connected' => $is_current_user_connected, |
611
|
|
|
), |
612
|
|
|
'siteFragment' => $site_fragment, |
613
|
|
|
'tracksUserData' => $user_data, |
614
|
|
|
'wpcomBlogId' => $blog_id, |
615
|
|
|
'allowedMimeTypes' => wp_get_mime_types(), |
616
|
|
|
) |
617
|
|
|
); |
618
|
|
|
|
619
|
|
|
wp_set_script_translations( 'jetpack-blocks-editor', 'jetpack' ); |
620
|
|
|
|
621
|
|
|
wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); |
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
|
|
|
/** |
647
|
|
|
* Get CSS classes for a block. |
648
|
|
|
* |
649
|
|
|
* @since 7.7.0 |
650
|
|
|
* |
651
|
|
|
* @param string $slug Block slug. |
652
|
|
|
* @param array $attr Block attributes. |
653
|
|
|
* @param array $extra Potential extra classes you may want to provide. |
654
|
|
|
* |
655
|
|
|
* @return string $classes List of CSS classes for a block. |
656
|
|
|
*/ |
657
|
|
|
public static function block_classes( $slug = '', $attr, $extra = array() ) { |
658
|
|
|
if ( empty( $slug ) ) { |
659
|
|
|
return ''; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
// Basic block name class. |
663
|
|
|
$classes = array( |
664
|
|
|
'wp-block-jetpack-' . $slug, |
665
|
|
|
); |
666
|
|
|
|
667
|
|
|
// Add alignment if provided. |
668
|
|
|
if ( |
669
|
|
|
! empty( $attr['align'] ) |
670
|
|
|
&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) |
671
|
|
|
) { |
672
|
|
|
array_push( $classes, 'align' . $attr['align'] ); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
// Add custom classes if provided in the block editor. |
676
|
|
|
if ( ! empty( $attr['className'] ) ) { |
677
|
|
|
array_push( $classes, $attr['className'] ); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
// Add any extra classes. |
681
|
|
|
if ( is_array( $extra ) && ! empty( $extra ) ) { |
682
|
|
|
$classes = array_merge( $classes, array_filter( $extra ) ); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
return implode( ' ', $classes ); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Determine whether a site should use the default set of blocks, or a custom set. |
690
|
|
|
* Possible variations are currently beta, experimental, and production. |
691
|
|
|
* |
692
|
|
|
* @since 8.1.0 |
693
|
|
|
* |
694
|
|
|
* @return string $block_varation production|beta|experimental |
695
|
|
|
*/ |
696
|
|
|
public static function blocks_variation() { |
697
|
|
|
// Default to production blocks. |
698
|
|
|
$block_varation = 'production'; |
699
|
|
|
|
700
|
|
|
if ( Constants::is_true( 'JETPACK_BETA_BLOCKS' ) ) { |
701
|
|
|
$block_varation = 'beta'; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/* |
705
|
|
|
* Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant. |
706
|
|
|
*/ |
707
|
|
|
if ( Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' ) ) { |
708
|
|
|
$block_varation = 'experimental'; |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Allow customizing the variation of blocks in use on a site. |
713
|
|
|
* |
714
|
|
|
* @since 8.1.0 |
715
|
|
|
* |
716
|
|
|
* @param string $block_variation Can be beta, experimental, and production. Defaults to production. |
717
|
|
|
*/ |
718
|
|
|
return apply_filters( 'jetpack_blocks_variation', $block_varation ); |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* Get a list of extensions available for the variation you chose. |
723
|
|
|
* |
724
|
|
|
* @since 8.1.0 |
725
|
|
|
* |
726
|
|
|
* @param obj $preset_extensions_manifest List of extensions available in Jetpack. |
727
|
|
|
* @param string $blocks_variation Subset of blocks. production|beta|experimental. |
728
|
|
|
* |
729
|
|
|
* @return array $preset_extensions Array of extensions for that variation |
730
|
|
|
*/ |
731
|
|
|
public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) { |
732
|
|
|
$preset_extensions = isset( $preset_extensions_manifest->{ $blocks_variation } ) |
733
|
|
|
? (array) $preset_extensions_manifest->{ $blocks_variation } |
734
|
|
|
: array(); |
735
|
|
|
|
736
|
|
|
/* |
737
|
|
|
* Experimental and Beta blocks need the production blocks as well. |
738
|
|
|
*/ |
739
|
|
View Code Duplication |
if ( |
740
|
|
|
'experimental' === $blocks_variation |
741
|
|
|
|| 'beta' === $blocks_variation |
742
|
|
|
) { |
743
|
|
|
$production_extensions = isset( $preset_extensions_manifest->production ) |
744
|
|
|
? (array) $preset_extensions_manifest->production |
745
|
|
|
: array(); |
746
|
|
|
|
747
|
|
|
$preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) ); |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/* |
751
|
|
|
* Beta blocks need the experimental blocks as well. |
752
|
|
|
* |
753
|
|
|
* If you've chosen to see Beta blocks, |
754
|
|
|
* we want to make all blocks available to you: |
755
|
|
|
* - Production |
756
|
|
|
* - Experimental |
757
|
|
|
* - Beta |
758
|
|
|
*/ |
759
|
|
View Code Duplication |
if ( 'beta' === $blocks_variation ) { |
760
|
|
|
$production_extensions = isset( $preset_extensions_manifest->experimental ) |
761
|
|
|
? (array) $preset_extensions_manifest->experimental |
762
|
|
|
: array(); |
763
|
|
|
|
764
|
|
|
$preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) ); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
return $preset_extensions; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
/** |
771
|
|
|
* Validate a URL used in a SSR block. |
772
|
|
|
* |
773
|
|
|
* @since 8.3.0 |
774
|
|
|
* |
775
|
|
|
* @param string $url URL saved as an attribute in block. |
776
|
|
|
* @param array $allowed Array of allowed hosts for that block, or regexes to check against. |
777
|
|
|
* @param bool $is_regex Array of regexes matching the URL that could be used in block. |
778
|
|
|
* |
779
|
|
|
* @return bool|string |
780
|
|
|
*/ |
781
|
|
|
public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) { |
782
|
|
|
if ( |
783
|
|
|
empty( $url ) |
784
|
|
|
|| ! is_array( $allowed ) |
785
|
|
|
|| empty( $allowed ) |
786
|
|
|
) { |
787
|
|
|
return false; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
$url_components = wp_parse_url( $url ); |
791
|
|
|
|
792
|
|
|
// Bail early if we cannot find a host. |
793
|
|
|
if ( empty( $url_components['host'] ) ) { |
794
|
|
|
return false; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
// Normalize URL. |
798
|
|
|
$url = sprintf( |
799
|
|
|
'%s://%s%s%s', |
800
|
|
|
isset( $url_components['scheme'] ) ? $url_components['scheme'] : 'https', |
801
|
|
|
$url_components['host'], |
802
|
|
|
isset( $url_components['path'] ) ? $url_components['path'] : '/', |
803
|
|
|
isset( $url_components['query'] ) ? '?' . $url_components['query'] : '' |
804
|
|
|
); |
805
|
|
|
|
806
|
|
|
if ( ! empty( $url_components['fragment'] ) ) { |
807
|
|
|
$url = $url . '#' . rawurlencode( $url_components['fragment'] ); |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/* |
811
|
|
|
* If we're using an allowed list of hosts, |
812
|
|
|
* check if the URL belongs to one of the domains allowed for that block. |
813
|
|
|
*/ |
814
|
|
|
if ( |
815
|
|
|
false === $is_regex |
816
|
|
|
&& in_array( $url_components['host'], $allowed, true ) |
817
|
|
|
) { |
818
|
|
|
return $url; |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/* |
822
|
|
|
* If we are using an array of regexes to check against, |
823
|
|
|
* loop through that. |
824
|
|
|
*/ |
825
|
|
|
if ( true === $is_regex ) { |
826
|
|
|
foreach ( $allowed as $regex ) { |
827
|
|
|
if ( 1 === preg_match( $regex, $url ) ) { |
828
|
|
|
return $url; |
829
|
|
|
} |
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
return false; |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Output an UpgradeNudge Component on the frontend of a site. |
838
|
|
|
* |
839
|
|
|
* @since 8.4.0 |
840
|
|
|
* |
841
|
|
|
* @param string $plan The plan that users need to purchase to make the block work. |
842
|
|
|
* |
843
|
|
|
* @return string |
844
|
|
|
*/ |
845
|
|
|
public static function upgrade_nudge( $plan ) { |
846
|
|
|
if ( |
847
|
|
|
! current_user_can( 'manage_options' ) |
848
|
|
|
/** This filter is documented in class.jetpack-gutenberg.php */ |
849
|
|
|
|| ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ) |
850
|
|
|
/** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */ |
851
|
|
|
|| ! apply_filters( 'jetpack_show_promotions', true ) |
852
|
|
|
|| is_feed() |
853
|
|
|
) { |
854
|
|
|
return; |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
jetpack_require_lib( 'components' ); |
858
|
|
|
return Jetpack_Components::render_upgrade_nudge( |
859
|
|
|
array( |
860
|
|
|
'plan' => $plan, |
861
|
|
|
) |
862
|
|
|
); |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
/** |
866
|
|
|
* Output a notice within a block. |
867
|
|
|
* |
868
|
|
|
* @since 8.6.0 |
869
|
|
|
* |
870
|
|
|
* @param string $message Notice we want to output. |
871
|
|
|
* @param string $status Status of the notice. Can be one of success, info, warning, error. info by default. |
872
|
|
|
* @param string $classes List of CSS classes. |
873
|
|
|
* |
874
|
|
|
* @return string |
875
|
|
|
*/ |
876
|
|
|
public static function notice( $message, $status = 'info', $classes = '' ) { |
877
|
|
|
if ( |
878
|
|
|
empty( $message ) |
879
|
|
|
|| ! in_array( $status, array( 'success', 'info', 'warning', 'error' ), true ) |
880
|
|
|
) { |
881
|
|
|
return ''; |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
$color = ''; |
|
|
|
|
885
|
|
|
switch ( $status ) { |
886
|
|
|
case 'success': |
887
|
|
|
$color = '#46b450'; |
888
|
|
|
break; |
889
|
|
|
case 'warning': |
890
|
|
|
$color = '#ffb900'; |
891
|
|
|
break; |
892
|
|
|
case 'error': |
893
|
|
|
$color = '#dc3232'; |
894
|
|
|
break; |
895
|
|
|
case 'info': |
896
|
|
|
default: |
897
|
|
|
$color = '#00a0d2'; |
898
|
|
|
break; |
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
return sprintf( |
902
|
|
|
'<div class="jetpack-block__notice %1$s %3$s" style="border-left:5px solid %4$s;padding:1em;background-color:#f8f9f9;">%2$s</div>', |
903
|
|
|
esc_attr( $status ), |
904
|
|
|
wp_kses( |
905
|
|
|
$message, |
906
|
|
|
array( |
907
|
|
|
'br' => array(), |
908
|
|
|
'p' => array(), |
909
|
|
|
) |
910
|
|
|
), |
911
|
|
|
esc_attr( $classes ), |
912
|
|
|
sanitize_hex_color( $color ) |
913
|
|
|
); |
914
|
|
|
} |
915
|
|
|
|
916
|
|
|
/** |
917
|
|
|
* Set the availability of the block as the editor |
918
|
|
|
* is loaded. |
919
|
|
|
* |
920
|
|
|
* @param string $slug Slug of the block. |
921
|
|
|
*/ |
922
|
|
|
public static function set_availability_for_plan( $slug ) { |
923
|
|
|
$is_available = true; |
924
|
|
|
$plan = ''; |
925
|
|
|
$slug = self::remove_extension_prefix( $slug ); |
926
|
|
|
|
927
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
928
|
|
|
if ( ! class_exists( 'Store_Product_List' ) ) { |
929
|
|
|
require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php'; |
930
|
|
|
} |
931
|
|
|
$features_data = Store_Product_List::get_site_specific_features_data(); |
932
|
|
|
$is_available = in_array( $slug, $features_data['active'], true ); |
933
|
|
|
if ( ! empty( $features_data['available'][ $slug ] ) ) { |
934
|
|
|
$plan = $features_data['available'][ $slug ][0]; |
935
|
|
|
} |
936
|
|
|
} elseif ( ! jetpack_is_atomic_site() ) { |
937
|
|
|
/* |
938
|
|
|
* If it's Atomic then assume all features are available |
939
|
|
|
* otherwise check against the Jetpack plan. |
940
|
|
|
*/ |
941
|
|
|
$is_available = Jetpack_Plan::supports( $slug ); |
942
|
|
|
$plan = Jetpack_Plan::get_minimum_plan_for_feature( $slug ); |
943
|
|
|
} |
944
|
|
|
if ( $is_available ) { |
945
|
|
|
self::set_extension_available( $slug ); |
946
|
|
|
} else { |
947
|
|
|
self::set_extension_unavailable( |
948
|
|
|
$slug, |
949
|
|
|
'missing_plan', |
950
|
|
|
array( |
951
|
|
|
'required_feature' => $slug, |
952
|
|
|
'required_plan' => $plan, |
953
|
|
|
) |
954
|
|
|
); |
955
|
|
|
} |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* Wraps the suplied render_callback in a function to check |
960
|
|
|
* the availability of the block before rendering it. |
961
|
|
|
* |
962
|
|
|
* @param string $slug The block slug, used to check for availability. |
963
|
|
|
* @param callable $render_callback The render_callback that will be called if the block is available. |
964
|
|
|
*/ |
965
|
|
|
public static function get_render_callback_with_availability_check( $slug, $render_callback ) { |
966
|
|
|
return function ( $prepared_attributes, $block_content ) use ( $render_callback, $slug ) { |
967
|
|
|
$availability = self::get_availability(); |
968
|
|
|
$bare_slug = self::remove_extension_prefix( $slug ); |
969
|
|
|
if ( isset( $availability[ $bare_slug ] ) && $availability[ $bare_slug ]['available'] ) { |
970
|
|
|
return call_user_func( $render_callback, $prepared_attributes, $block_content ); |
971
|
|
|
} elseif ( isset( $availability[ $bare_slug ]['details']['required_plan'] ) ) { |
972
|
|
|
return self::upgrade_nudge( $availability[ $bare_slug ]['details']['required_plan'] ); |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
return null; |
976
|
|
|
}; |
977
|
|
|
} |
978
|
|
|
} |
979
|
|
|
|
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: