PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 13.3.2
Jetpack – WP Security, Backup, Speed, & Growth v13.3.2
15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / class.jetpack-gutenberg.php
jetpack Last commit date
3rd-party 2 years ago _inc 2 years ago css 2 years ago extensions 2 years ago images 2 years ago jetpack_vendor 1 year ago json-endpoints 2 years ago modules 2 years ago sal 2 years ago src 2 years ago vendor 2 years ago views 3 years ago CHANGELOG.md 2 years ago LICENSE.txt 5 years ago SECURITY.md 2 years ago class-jetpack-connection-status.php 2 years ago class-jetpack-gallery-settings.php 3 years ago class-jetpack-pre-connection-jitms.php 2 years ago class-jetpack-stats-dashboard-widget.php 2 years ago class-jetpack-xmlrpc-methods.php 2 years ago class.frame-nonce-preview.php 4 years ago class.jetpack-admin.php 2 years ago class.jetpack-affiliate.php 2 years ago class.jetpack-autoupdate.php 2 years ago class.jetpack-bbpress-json-api.compat.php 2 years ago class.jetpack-cli.php 2 years ago class.jetpack-client-server.php 2 years ago class.jetpack-gutenberg.php 2 years ago class.jetpack-heartbeat.php 2 years ago class.jetpack-modules-list-table.php 2 years ago class.jetpack-network-sites-list-table.php 2 years ago class.jetpack-network.php 2 years ago class.jetpack-plan.php 2 years ago class.jetpack-post-images.php 2 years ago class.jetpack-twitter-cards.php 2 years ago class.jetpack-user-agent.php 2 years ago class.jetpack.php 2 years ago class.json-api-endpoints.php 2 years ago class.json-api.php 2 years ago class.photon.php 3 years ago composer.json 2 years ago enhanced-open-graph.php 3 years ago functions.compat.php 2 years ago functions.cookies.php 2 years ago functions.global.php 2 years ago functions.is-mobile.php 2 years ago functions.opengraph.php 2 years ago functions.photon.php 2 years ago jetpack.php 1 year ago json-api-config.php 3 years ago json-endpoints.php 2 years ago load-jetpack.php 2 years ago locales.php 4 years ago readme.txt 1 year ago uninstall.php 2 years ago wpml-config.xml 3 years ago
class.jetpack-gutenberg.php
1285 lines
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 automattic/jetpack
7 */
8
9 use Automattic\Jetpack\Assets;
10 use Automattic\Jetpack\Blocks;
11 use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
12 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13 use Automattic\Jetpack\Constants;
14 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
15 use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Dismissed_Notices;
16 use Automattic\Jetpack\Status;
17 use Automattic\Jetpack\Status\Host;
18
19 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move the functions and such to some other file.
20
21 /**
22 * General Gutenberg editor specific functionality
23 */
24 class Jetpack_Gutenberg {
25
26 /**
27 * Only these extensions can be registered. Used to control availability of beta blocks.
28 *
29 * @var array|null Extensions allowed list or `null` if not initialized yet.
30 * @see static::get_extensions()
31 */
32 private static $extensions = null;
33
34 /**
35 * Keeps track of the reasons why a given extension is unavailable.
36 *
37 * @var array Extensions availability information
38 */
39 private static $availability = array();
40
41 /**
42 * A cached array of the fully processed availability data. Keeps track of
43 * reasons why an extension is unavailable or missing.
44 *
45 * @var array Extensions availability information.
46 */
47 private static $cached_availability = null;
48
49 /**
50 * Site-specific features available.
51 * Their calculation can be expensive and slow, so we're caching it for the request.
52 *
53 * @var array Site-specific features
54 */
55 private static $site_specific_features = array();
56
57 /**
58 * List of deprecated blocks.
59 *
60 * @var array List of deprecated blocks.
61 */
62 private static $deprecated_blocks = array(
63 'jetpack/revue',
64 );
65
66 /**
67 * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in
68 * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can
69 * optionally fall back to that.
70 *
71 * @param array $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version.
72 * @param string $slug The slug of the block or plugin that has the gutenberg version requirement.
73 *
74 * @since 8.3.0
75 *
76 * @return boolean True if the version of gutenberg required by the block or plugin is available.
77 */
78 public static function is_gutenberg_version_available( $version_requirements, $slug ) {
79 global $wp_version;
80
81 // Bail if we don't at least have the gutenberg version requirement, the WP version is optional.
82 if ( empty( $version_requirements['gutenberg'] ) ) {
83 return false;
84 }
85
86 // If running a local dev build of gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version.
87 if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) {
88 return true;
89 }
90
91 $version_available = false;
92
93 // If running a production build of the gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version
94 // with required version of Gutenberg is known check that.
95 if ( defined( 'GUTENBERG_VERSION' ) ) {
96 $version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' );
97 } elseif ( ! empty( $version_requirements['wp'] ) ) {
98 $version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' );
99 }
100
101 if ( ! $version_available ) {
102 self::set_extension_unavailable(
103 $slug,
104 'incorrect_gutenberg_version',
105 array(
106 'required_feature' => $slug,
107 'required_version' => $version_requirements,
108 'current_version' => array(
109 'wp' => $wp_version,
110 'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null,
111 ),
112 )
113 );
114 }
115
116 return $version_available;
117 }
118
119 /**
120 * Prepend the 'jetpack/' prefix to a block name
121 *
122 * @param string $block_name The block name.
123 *
124 * @return string The prefixed block name.
125 */
126 private static function prepend_block_prefix( $block_name ) {
127 return 'jetpack/' . $block_name;
128 }
129
130 /**
131 * Remove the 'jetpack/' or jetpack-' prefix from an extension name
132 *
133 * @param string $extension_name The extension name.
134 *
135 * @return string The unprefixed extension name.
136 */
137 public static function remove_extension_prefix( $extension_name ) {
138 if ( str_starts_with( $extension_name, 'jetpack/' ) || str_starts_with( $extension_name, 'jetpack-' ) ) {
139 return substr( $extension_name, strlen( 'jetpack/' ) );
140 }
141 return $extension_name;
142 }
143
144 /**
145 * Whether two arrays share at least one item
146 *
147 * @param array $a An array.
148 * @param array $b Another array.
149 *
150 * @return boolean True if $a and $b share at least one item
151 */
152 protected static function share_items( $a, $b ) {
153 return array_intersect( $a, $b ) !== array();
154 }
155
156 /**
157 * Set a (non-block) extension as available
158 *
159 * @param string $slug Slug of the extension.
160 */
161 public static function set_extension_available( $slug ) {
162 self::$availability[ self::remove_extension_prefix( $slug ) ] = true;
163 }
164
165 /**
166 * Set the reason why an extension (block or plugin) is unavailable
167 *
168 * @param string $slug Slug of the extension.
169 * @param string $reason A string representation of why the extension is unavailable.
170 * @param array $details A free-form array containing more information on why the extension is unavailable.
171 */
172 public static function set_extension_unavailable( $slug, $reason, $details = array() ) {
173 if (
174 // Extensions that require a plan may be eligible for upgrades.
175 'missing_plan' === $reason
176 && (
177 /**
178 * Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false`
179 * to disable paid feature upgrade nudges in the block editor.
180 *
181 * When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php`
182 * See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378
183 *
184 * @since 7.7.0
185 *
186 * @param boolean
187 */
188 ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false )
189 /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
190 || ! apply_filters( 'jetpack_show_promotions', true )
191 )
192 ) {
193 // The block editor may apply an upgrade nudge if `missing_plan` is the reason.
194 // Add a descriptive suffix to disable behavior but provide informative reason.
195 $reason .= '__nudge_disabled';
196 }
197
198 self::$availability[ self::remove_extension_prefix( $slug ) ] = array(
199 'reason' => $reason,
200 'details' => $details,
201 );
202 }
203
204 /**
205 * Used to initialize the class, no longer in use.
206 *
207 * @return void
208 * @deprecated 12.2 No longer needed.
209 */
210 public static function init() {
211 _deprecated_function( __METHOD__, '12.2' );
212 }
213
214 /**
215 * Resets the class to its original state
216 *
217 * Used in unit tests
218 *
219 * @return void
220 */
221 public static function reset() {
222 self::$extensions = null;
223 self::$availability = array();
224 self::$cached_availability = null;
225 }
226
227 /**
228 * Return the Gutenberg extensions (blocks and plugins) directory
229 *
230 * @return string The Gutenberg extensions directory
231 */
232 public static function get_blocks_directory() {
233 /**
234 * Filter to select Gutenberg blocks directory
235 *
236 * @since 6.9.0
237 *
238 * @param string default: '_inc/blocks/'
239 */
240 return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
241 }
242
243 /**
244 * Checks for a given .json file in the blocks folder.
245 *
246 * @param string $preset The name of the .json file to look for.
247 *
248 * @return bool True if the file is found.
249 */
250 public static function preset_exists( $preset ) {
251 return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
252 }
253
254 /**
255 * Decodes JSON loaded from a preset file in the blocks folder
256 *
257 * @param string $preset The name of the .json file to load.
258 *
259 * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
260 */
261 public static function get_preset( $preset ) {
262 return json_decode(
263 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
264 file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
265 );
266 }
267
268 /**
269 * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
270 *
271 * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
272 */
273 public static function get_jetpack_gutenberg_extensions_allowed_list() {
274 $preset_extensions_manifest = self::preset_exists( 'index' )
275 ? self::get_preset( 'index' )
276 : (object) array();
277 $blocks_variation = self::blocks_variation();
278
279 return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation );
280 }
281
282 /**
283 * Returns a diff from a combined list of allowed extensions and extensions determined to be excluded
284 *
285 * @param array $allowed_extensions An array of allowed extensions.
286 *
287 * @return array A list of blocks: eg array( 'publicize', 'markdown' )
288 */
289 public static function get_available_extensions( $allowed_extensions = null ) {
290 $exclusions = get_option( 'jetpack_excluded_extensions', array() );
291 $allowed_extensions = $allowed_extensions === null ? self::get_jetpack_gutenberg_extensions_allowed_list() : $allowed_extensions;
292
293 return array_diff( $allowed_extensions, $exclusions );
294 }
295
296 /**
297 * Return true if the extension has been registered and there's nothing in the availablilty array.
298 *
299 * @param string $extension The name of the extension.
300 *
301 * @return bool whether the extension has been registered and there's nothing in the availablilty array.
302 */
303 public static function is_registered_and_no_entry_in_availability( $extension ) {
304 return self::is_registered( 'jetpack/' . $extension ) && ! isset( self::$availability[ $extension ] );
305 }
306
307 /**
308 * Return true if the extension has a true entry in the availablilty array.
309 *
310 * @param string $extension The name of the extension.
311 *
312 * @return bool whether the extension has a true entry in the availablilty array.
313 */
314 public static function is_available( $extension ) {
315 return isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ];
316 }
317
318 /**
319 * Get the availability of each block / plugin, or return the cached availability
320 * if it has already been calculated. Avoids re-registering extensions when not
321 * necessary.
322 *
323 * @return array A list of block and plugins and their availability status.
324 */
325 public static function get_cached_availability() {
326 if ( null === self::$cached_availability ) {
327 self::$cached_availability = self::get_availability();
328 }
329 return self::$cached_availability;
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 `Blocks::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 ( static::get_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 * Return the list of extensions that are available.
371 *
372 * @since 11.9
373 *
374 * @return array A list of block and plugins and their availability status.
375 */
376 public static function get_extensions() {
377 if ( ! static::should_load() ) {
378 return array();
379 }
380
381 if ( null === self::$extensions ) {
382 /**
383 * Filter the list of block editor extensions that are available through Jetpack.
384 *
385 * @since 7.0.0
386 *
387 * @param array
388 */
389 self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() );
390 }
391
392 return self::$extensions;
393 }
394
395 /**
396 * Check if an extension/block is already registered
397 *
398 * @since 7.2
399 *
400 * @param string $slug Name of extension/block to check.
401 *
402 * @return bool
403 */
404 public static function is_registered( $slug ) {
405 return WP_Block_Type_Registry::get_instance()->is_registered( $slug );
406 }
407
408 /**
409 * Check if Gutenberg editor is available
410 *
411 * @since 6.7.0
412 *
413 * @return bool
414 */
415 public static function is_gutenberg_available() {
416 return true;
417 }
418
419 /**
420 * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
421 *
422 * Loading blocks and plugins is enabled by default and may be disabled via filter:
423 * add_filter( 'jetpack_gutenberg', '__return_false' );
424 *
425 * @since 6.9.0
426 *
427 * @return bool
428 */
429 public static function should_load() {
430 if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) {
431 return false;
432 }
433
434 if ( get_option( 'jetpack_blocks_disabled', false ) ) {
435 return false;
436 }
437
438 /**
439 * Filter to disable Gutenberg blocks
440 *
441 * @since 6.5.0
442 *
443 * @param bool true Whether to load Gutenberg blocks
444 */
445 return (bool) apply_filters( 'jetpack_gutenberg', true );
446 }
447
448 /**
449 * Queue a script to set `Jetpack_Block_Assets_Base_Url`.
450 *
451 * In certain cases Webpack needs to know a base to load additional assets from.
452 * Normally it can determine that itself, but when JS concatenation is involved that tends to confuse it.
453 * We work around that by explicitly outputting a variable with the correct URL.
454 * We set that as its own "script" so we can reliably only output it once.
455 */
456 private static function register_blocks_assets_base_url() {
457 if ( ! wp_script_is( 'jetpack-blocks-assets-base-url', 'registered' ) ) {
458 // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- No actual script, so no version needed.
459 wp_register_script( 'jetpack-blocks-assets-base-url', false, array(), null, array( 'in_footer' => false ) );
460 wp_add_inline_script(
461 'jetpack-blocks-assets-base-url',
462 'var Jetpack_Block_Assets_Base_Url=' . wp_json_encode( plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
463 'before'
464 );
465 }
466 }
467
468 /**
469 * Only enqueue block assets when needed.
470 *
471 * @param string $type Slug of the block or absolute path to the block source code directory.
472 * @param array $script_dependencies Script dependencies. Will be merged with automatically
473 * detected script dependencies from the webpack build.
474 *
475 * @return void
476 */
477 public static function load_assets_as_required( $type, $script_dependencies = array() ) {
478 if ( is_admin() ) {
479 // A block's view assets will not be required in wp-admin.
480 return;
481 }
482
483 // Retrieve the feature from block.json if a path is passed.
484 if ( path_is_absolute( $type ) ) {
485 $metadata = Blocks::get_block_metadata_from_file( Blocks::get_path_to_block_metadata( $type ) );
486 $feature = Blocks::get_block_feature_from_metadata( $metadata );
487
488 if ( ! empty( $feature ) ) {
489 $type = $feature;
490 }
491 }
492
493 $type = sanitize_title_with_dashes( $type );
494 self::load_styles_as_required( $type );
495 self::load_scripts_as_required( $type, $script_dependencies );
496 }
497
498 /**
499 * Only enqueue block sytles when needed.
500 *
501 * @param string $type Slug of the block.
502 *
503 * @since 7.2.0
504 *
505 * @return void
506 */
507 public static function load_styles_as_required( $type ) {
508 if ( is_admin() ) {
509 // A block's view assets will not be required in wp-admin.
510 return;
511 }
512
513 // Enqueue styles.
514 $style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
515 if ( self::block_has_asset( $style_relative_path ) ) {
516 $style_version = self::get_asset_version( $style_relative_path );
517 $view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
518 $view_style = add_query_arg( 'minify', 'false', $view_style );
519
520 // If this is a customizer preview, render the style directly to the preview after autosave.
521 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
522 if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) {
523 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
524 echo '<link rel="stylesheet" id="jetpack-block-' . esc_attr( $type ) . '" href="' . esc_attr( $view_style ) . '&amp;ver=' . esc_attr( $style_version ) . '" media="all">';
525 } else {
526 wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
527 wp_style_add_data( 'jetpack-block-' . $type, 'path', JETPACK__PLUGIN_DIR . $style_relative_path );
528 }
529 }
530 }
531
532 /**
533 * Only enqueue block scripts when needed.
534 *
535 * @param string $type Slug of the block.
536 * @param array $script_dependencies Script dependencies. Will be merged with automatically
537 * detected script dependencies from the webpack build.
538 *
539 * @since 7.2.0
540 *
541 * @return void
542 */
543 public static function load_scripts_as_required( $type, $script_dependencies = array() ) {
544 if ( is_admin() ) {
545 // A block's view assets will not be required in wp-admin.
546 return;
547 }
548
549 self::register_blocks_assets_base_url();
550
551 // Enqueue script.
552 $script_relative_path = self::get_blocks_directory() . $type . '/view.js';
553 $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php';
554 $script_dependencies[] = 'wp-polyfill';
555 $script_dependencies[] = 'jetpack-blocks-assets-base-url';
556 if ( file_exists( $script_deps_path ) ) {
557 $asset_manifest = include $script_deps_path;
558 $script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) );
559 }
560
561 if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) {
562 $script_version = self::get_asset_version( $script_relative_path );
563 $view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
564 $view_script = add_query_arg( 'minify', 'false', $view_script );
565
566 // Enqueue dependencies.
567 wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
568
569 // If this is a customizer preview, enqueue the dependencies and render the script directly to the preview after autosave.
570 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
571 if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) {
572 // The Map block is dependent on wp-element, and it doesn't appear to to be possible to load
573 // this dynamically into the customizer iframe currently.
574 if ( 'map' === $type ) {
575 echo '<div>' . esc_html_e( 'No map preview available. Publish and refresh to see this widget.', 'jetpack' ) . '</div>';
576 echo '<script>';
577 echo 'Array.from(document.getElementsByClassName(\'wp-block-jetpack-map\')).forEach(function(element){element.style.display = \'none\';})';
578 echo '</script>';
579 } else {
580 echo '<script id="jetpack-block-' . esc_attr( $type ) . '" src="' . esc_attr( $view_script ) . '&amp;ver=' . esc_attr( $script_version ) . '"></script>';
581 }
582 }
583 }
584 }
585
586 /**
587 * Check if an asset exists for a block.
588 *
589 * @param string $file Path of the file we are looking for.
590 *
591 * @return bool $block_has_asset Does the file exist.
592 */
593 public static function block_has_asset( $file ) {
594 return file_exists( JETPACK__PLUGIN_DIR . $file );
595 }
596
597 /**
598 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
599 *
600 * @param string $file Path of the file we are looking for.
601 *
602 * @return string $script_version Version number.
603 */
604 public static function get_asset_version( $file ) {
605 return Jetpack::is_development_version() && self::block_has_asset( $file )
606 ? filemtime( JETPACK__PLUGIN_DIR . $file )
607 : JETPACK__VERSION;
608 }
609
610 /**
611 * Load Gutenberg editor assets
612 *
613 * @since 6.7.0
614 *
615 * @return void
616 */
617 public static function enqueue_block_editor_assets() {
618 if ( ! self::should_load() ) {
619 return;
620 }
621
622 /**
623 * This can be called multiple times per page load in the admin, during the `enqueue_block_assets` action.
624 * These assets are necessary for the admin for editing but are not necessary for each pattern preview.
625 * Therefore we dequeue them, so they don't load for each pattern preview iframe.
626 */
627 if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
628 wp_dequeue_script( 'jp-tracks' );
629 wp_dequeue_script( 'jetpack-blocks-editor' );
630
631 return;
632 }
633
634 $status = new Status();
635
636 // Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php.
637 if ( ! $status->is_offline_mode() && Jetpack::is_connection_ready() ) {
638 wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
639 }
640
641 $blocks_dir = self::get_blocks_directory();
642 $blocks_variation = self::blocks_variation();
643
644 if ( 'production' !== $blocks_variation ) {
645 $blocks_env = '-' . esc_attr( $blocks_variation );
646 } else {
647 $blocks_env = '';
648 }
649
650 self::register_blocks_assets_base_url();
651
652 Assets::register_script(
653 'jetpack-blocks-editor',
654 "{$blocks_dir}editor{$blocks_env}.js",
655 JETPACK__PLUGIN_FILE,
656 array(
657 'textdomain' => 'jetpack',
658 'dependencies' => array( 'jetpack-blocks-assets-base-url' ),
659 )
660 );
661
662 // Hack around #20357 (specifically, that the editor bundle depends on
663 // wp-edit-post but wp-edit-post's styles break the Widget Editor and
664 // Site Editor) until a real fix gets unblocked.
665 // @todo Remove this once #20357 is properly fixed.
666 wp_styles()->query( 'jetpack-blocks-editor', 'registered' )->deps = array();
667
668 Assets::enqueue_script( 'jetpack-blocks-editor' );
669
670 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
671 $user = wp_get_current_user();
672 $user_data = array(
673 'email' => $user->user_email,
674 'userid' => $user->ID,
675 'username' => $user->user_login,
676 );
677 $blog_id = get_current_blog_id();
678 $is_current_user_connected = true;
679 } else {
680 $user_data = Jetpack_Tracks_Client::get_connected_user_tracks_identity();
681 $blog_id = Jetpack_Options::get_option( 'id', 0 );
682 $is_current_user_connected = ( new Connection_Manager( 'jetpack' ) )->is_user_connected();
683 }
684
685 if ( $blocks_variation === 'beta' && $is_current_user_connected ) {
686 wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Constants::get_constant( 'JETPACK__VERSION' ) );
687 }
688 // AI Assistant
689 $ai_assistant_state = array(
690 'is-enabled' => apply_filters( 'jetpack_ai_enabled', true ),
691 'is-playground-visible' => Constants::is_true( 'JETPACK_AI_ASSISTANT_PLAYGROUND' ),
692 );
693
694 $screen_base = null;
695 if ( function_exists( 'get_current_screen' ) ) {
696 $screen_base = get_current_screen()->base;
697 }
698
699 $modules = array();
700 if ( class_exists( 'Jetpack_Core_API_Module_List_Endpoint' ) ) {
701 $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint();
702 $modules = $module_list_endpoint->get_modules();
703 }
704
705 $initial_state = array(
706 'available_blocks' => self::get_availability(),
707 'blocks_variation' => $blocks_variation,
708 'modules' => $modules,
709 'jetpack' => array(
710 'is_active' => Jetpack::is_connection_ready(),
711 'is_current_user_connected' => $is_current_user_connected,
712 /** This filter is documented in class.jetpack-gutenberg.php */
713 'enable_upgrade_nudge' => apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ),
714 'is_private_site' => $status->is_private_site(),
715 'is_coming_soon' => $status->is_coming_soon(),
716 'is_offline_mode' => $status->is_offline_mode(),
717 'is_newsletter_feature_enabled' => class_exists( '\Jetpack_Memberships' ),
718 /**
719 * Enable the RePublicize UI in the block editor context.
720 *
721 * @module publicize
722 *
723 * @since 10.3.0
724 * @deprecated $$next_version$$ This is a feature flag that is no longer used.
725 *
726 * @param bool true Enable the RePublicize UI in the block editor context. Defaults to true.
727 */
728 'republicize_enabled' => apply_filters( 'jetpack_block_editor_republicize_feature', true ),
729 /**
730 * Prevent the registration of the blocks from extensions/blocks/contact-form
731 * if the Forms package is enabled.
732 */
733 'is_form_package_enabled' => apply_filters( 'jetpack_contact_form_use_package', true ),
734 ),
735 'siteFragment' => $status->get_site_suffix(),
736 'adminUrl' => esc_url( admin_url() ),
737 'tracksUserData' => $user_data,
738 'wpcomBlogId' => $blog_id,
739 'allowedMimeTypes' => wp_get_mime_types(),
740 'siteLocale' => str_replace( '_', '-', get_locale() ),
741 'ai-assistant' => $ai_assistant_state,
742 'screenBase' => $screen_base,
743 'pluginBasePath' => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ),
744 );
745
746 if ( Jetpack::is_module_active( 'publicize' ) && function_exists( 'publicize_init' ) ) {
747 $publicize = publicize_init();
748 $jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
749 $settings = $jetpack_social_settings->get_settings( true );
750
751 $initial_state['social'] = array(
752 'sharesData' => $publicize->get_publicize_shares_info( $blog_id ),
753 'hasPaidPlan' => $publicize->has_paid_plan(),
754 'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(),
755 'isSocialImageGeneratorAvailable' => $settings['socialImageGeneratorSettings']['available'],
756 'isSocialImageGeneratorEnabled' => $settings['socialImageGeneratorSettings']['enabled'],
757 'dismissedNotices' => Dismissed_Notices::get_dismissed_notices(),
758 'supportedAdditionalConnections' => $publicize->get_supported_additional_connections(),
759 'autoConversionSettings' => $settings['autoConversionSettings'],
760 'jetpackSharingSettingsUrl' => esc_url_raw( admin_url( 'admin.php?page=jetpack#/sharing' ) ),
761 );
762 }
763
764 wp_localize_script(
765 'jetpack-blocks-editor',
766 'Jetpack_Editor_Initial_State',
767 $initial_state
768 );
769
770 // Adds Connection package initial state.
771 Connection_Initial_State::render_script( 'jetpack-blocks-editor' );
772 }
773
774 /**
775 * Some blocks do not depend on a specific module,
776 * and can consequently be loaded outside of the usual modules.
777 * We will look for such modules in the extensions/ directory.
778 *
779 * @since 7.1.0
780 * @see wp_common_block_scripts_and_styles()
781 */
782 public static function load_independent_blocks() {
783 if ( self::should_load() ) {
784 /**
785 * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
786 * If available, load them.
787 */
788 $directories = array( 'blocks', 'plugins', 'extended-blocks', 'shared', 'store' );
789
790 foreach ( static::get_extensions() as $extension ) {
791 foreach ( $directories as $dirname ) {
792 $path = JETPACK__PLUGIN_DIR . "extensions/{$dirname}/{$extension}/{$extension}.php";
793
794 if ( file_exists( $path ) ) {
795 include_once $path;
796 continue 2;
797 }
798 }
799 }
800 }
801 }
802
803 /**
804 * Loads PHP components of block editor extensions.
805 *
806 * @since 8.9.0
807 */
808 public static function load_block_editor_extensions() {
809 if ( self::should_load() ) {
810 // Block editor extensions to load.
811 $extensions_to_load = array(
812 'extended-blocks',
813 'plugins',
814 );
815
816 // Collect the extension paths.
817 foreach ( $extensions_to_load as $extension_to_load ) {
818 $extensions_folder = glob( JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/*' );
819
820 // Require each of the extension files, in case it exists.
821 foreach ( $extensions_folder as $extension_folder ) {
822 $name = basename( $extension_folder );
823 $extension_file_path = JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/' . $name . '/' . $name . '.php';
824
825 if ( file_exists( $extension_file_path ) ) {
826 include_once $extension_file_path;
827 }
828 }
829 }
830 }
831 }
832
833 /**
834 * Determine whether a site should use the default set of blocks, or a custom set.
835 * Possible variations are currently beta, experimental, and production.
836 *
837 * @since 8.1.0
838 *
839 * @return string $block_varation production|beta|experimental
840 */
841 public static function blocks_variation() {
842 // Default to production blocks.
843 $block_varation = 'production';
844
845 /*
846 * Prefer to use this JETPACK_BLOCKS_VARIATION constant
847 * or the jetpack_blocks_variation filter
848 * to set the block variation in your code.
849 */
850 $default = Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' );
851 if ( ! empty( $default ) && in_array( $default, array( 'beta', 'experimental', 'production' ), true ) ) {
852 $block_varation = $default;
853 }
854
855 /**
856 * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
857 *
858 * @since 6.9.0
859 * @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
860 *
861 * @param boolean
862 */
863 $is_beta = apply_filters_deprecated(
864 'jetpack_load_beta_blocks',
865 array( false ),
866 'jetpack-11.8.0',
867 'jetpack_blocks_variation'
868 );
869
870 /*
871 * Switch to beta blocks if you use the JETPACK_BETA_BLOCKS constant
872 * or the deprecated jetpack_load_beta_blocks filter.
873 * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
874 */
875 if (
876 empty( $default )
877 && (
878 $is_beta
879 || Constants::is_true( 'JETPACK_BETA_BLOCKS' )
880 )
881 ) {
882 $block_varation = 'beta';
883 }
884
885 /**
886 * Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks.
887 *
888 * @since 6.9.0
889 * @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
890 *
891 * @param boolean
892 */
893 $is_experimental = apply_filters_deprecated(
894 'jetpack_load_experimental_blocks',
895 array( false ),
896 'jetpack-11.8.0',
897 'jetpack_blocks_variation'
898 );
899
900 /*
901 * Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant
902 * or the deprecated jetpack_load_experimental_blocks filter.
903 * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
904 */
905 if (
906 empty( $default )
907 && (
908 $is_experimental
909 || Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' )
910 )
911 ) {
912 $block_varation = 'experimental';
913 }
914
915 /**
916 * Allow customizing the variation of blocks in use on a site.
917 * Overwrites any previously set values, whether by constant or filter.
918 *
919 * @since 8.1.0
920 *
921 * @param string $block_variation Can be beta, experimental, and production. Defaults to production.
922 */
923 return apply_filters( 'jetpack_blocks_variation', $block_varation );
924 }
925
926 /**
927 * Get a list of extensions available for the variation you chose.
928 *
929 * @since 8.1.0
930 *
931 * @param obj $preset_extensions_manifest List of extensions available in Jetpack.
932 * @param string $blocks_variation Subset of blocks. production|beta|experimental.
933 *
934 * @return array $preset_extensions Array of extensions for that variation
935 */
936 public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) {
937 $preset_extensions = isset( $preset_extensions_manifest->{ $blocks_variation } )
938 ? (array) $preset_extensions_manifest->{ $blocks_variation }
939 : array();
940
941 /*
942 * Experimental and Beta blocks need the production blocks as well.
943 */
944 if (
945 'experimental' === $blocks_variation
946 || 'beta' === $blocks_variation
947 ) {
948 $production_extensions = isset( $preset_extensions_manifest->production )
949 ? (array) $preset_extensions_manifest->production
950 : array();
951
952 $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
953 }
954
955 /*
956 * Beta blocks need the experimental blocks as well.
957 *
958 * If you've chosen to see Beta blocks,
959 * we want to make all blocks available to you:
960 * - Production
961 * - Experimental
962 * - Beta
963 */
964 if ( 'beta' === $blocks_variation ) {
965 $production_extensions = isset( $preset_extensions_manifest->experimental )
966 ? (array) $preset_extensions_manifest->experimental
967 : array();
968
969 $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
970 }
971
972 return $preset_extensions;
973 }
974
975 /**
976 * Validate a URL used in a SSR block.
977 *
978 * @since 8.3.0
979 *
980 * @param string $url URL saved as an attribute in block.
981 * @param array $allowed Array of allowed hosts for that block, or regexes to check against.
982 * @param bool $is_regex Array of regexes matching the URL that could be used in block.
983 *
984 * @return bool|string
985 */
986 public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) {
987 if (
988 empty( $url )
989 || ! is_array( $allowed )
990 || empty( $allowed )
991 ) {
992 return false;
993 }
994
995 $url_components = wp_parse_url( $url );
996
997 // Bail early if we cannot find a host.
998 if ( empty( $url_components['host'] ) ) {
999 return false;
1000 }
1001
1002 // Normalize URL.
1003 $url = sprintf(
1004 '%s://%s%s%s',
1005 isset( $url_components['scheme'] ) ? $url_components['scheme'] : 'https',
1006 $url_components['host'],
1007 isset( $url_components['path'] ) ? $url_components['path'] : '/',
1008 isset( $url_components['query'] ) ? '?' . $url_components['query'] : ''
1009 );
1010
1011 if ( ! empty( $url_components['fragment'] ) ) {
1012 $url = $url . '#' . rawurlencode( $url_components['fragment'] );
1013 }
1014
1015 /*
1016 * If we're using an allowed list of hosts,
1017 * check if the URL belongs to one of the domains allowed for that block.
1018 */
1019 if (
1020 false === $is_regex
1021 && in_array( $url_components['host'], $allowed, true )
1022 ) {
1023 return $url;
1024 }
1025
1026 /*
1027 * If we are using an array of regexes to check against,
1028 * loop through that.
1029 */
1030 if ( true === $is_regex ) {
1031 foreach ( $allowed as $regex ) {
1032 if ( 1 === preg_match( $regex, $url ) ) {
1033 return $url;
1034 }
1035 }
1036 }
1037
1038 return false;
1039 }
1040
1041 /**
1042 * Determines whether a preview of the block with an upgrade nudge should
1043 * be displayed for admins on the site frontend.
1044 *
1045 * @since 8.4.0
1046 *
1047 * @param array $availability_for_block The availability for the block.
1048 *
1049 * @return bool
1050 */
1051 public static function should_show_frontend_preview( $availability_for_block ) {
1052 return (
1053 isset( $availability_for_block['details']['required_plan'] )
1054 && current_user_can( 'manage_options' )
1055 && ! is_feed()
1056 );
1057 }
1058
1059 /**
1060 * Output an UpgradeNudge Component on the frontend of a site.
1061 *
1062 * @since 8.4.0
1063 *
1064 * @param string $plan The plan that users need to purchase to make the block work.
1065 *
1066 * @return string
1067 */
1068 public static function upgrade_nudge( $plan ) {
1069 require_once JETPACK__PLUGIN_DIR . '_inc/lib/components.php';
1070 return Jetpack_Components::render_upgrade_nudge(
1071 array(
1072 'plan' => $plan,
1073 )
1074 );
1075 }
1076
1077 /**
1078 * Output a notice within a block.
1079 *
1080 * @since 8.6.0
1081 *
1082 * @param string $message Notice we want to output.
1083 * @param string $status Status of the notice. Can be one of success, info, warning, error. info by default.
1084 * @param string $classes List of CSS classes.
1085 *
1086 * @return string
1087 */
1088 public static function notice( $message, $status = 'info', $classes = '' ) {
1089 if (
1090 empty( $message )
1091 || ! in_array( $status, array( 'success', 'info', 'warning', 'error' ), true )
1092 ) {
1093 return '';
1094 }
1095
1096 $color = '';
1097 switch ( $status ) {
1098 case 'success':
1099 $color = '#00a32a';
1100 break;
1101 case 'warning':
1102 $color = '#dba617';
1103 break;
1104 case 'error':
1105 $color = '#d63638';
1106 break;
1107 case 'info':
1108 default:
1109 $color = '#72aee6';
1110 break;
1111 }
1112
1113 return sprintf(
1114 '<div class="jetpack-block__notice %1$s %3$s" style="border-left:5px solid %4$s;padding:1em;background-color:#f8f9f9;">%2$s</div>',
1115 esc_attr( $status ),
1116 wp_kses(
1117 $message,
1118 array(
1119 'br' => array(),
1120 'p' => array(),
1121 'a' => array(
1122 'href' => array(),
1123 'target' => array(),
1124 'rel' => array(),
1125 ),
1126 )
1127 ),
1128 esc_attr( $classes ),
1129 sanitize_hex_color( $color )
1130 );
1131 }
1132
1133 /**
1134 * Retrieve site-specific features for Simple sites.
1135 *
1136 * We're caching the data for the lifetime of the request, because it can be slow to calculate,
1137 * and it can be called multiple times per single request.
1138 *
1139 * We intentionally don't use object caching or any other type of persistent caching,
1140 * in order to avoid complex cache invalidation on subscription addition or removal.
1141 *
1142 * @since 10.7
1143 *
1144 * @return array
1145 */
1146 private static function get_site_specific_features() {
1147 $current_blog_id = get_current_blog_id();
1148
1149 if ( isset( self::$site_specific_features[ $current_blog_id ] ) ) {
1150 return self::$site_specific_features[ $current_blog_id ];
1151 }
1152
1153 if ( ! class_exists( 'Store_Product_List' ) ) {
1154 require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
1155 }
1156
1157 $site_specific_features = Store_Product_List::get_site_specific_features_data( $current_blog_id );
1158 self::$site_specific_features[ $current_blog_id ] = $site_specific_features;
1159
1160 return $site_specific_features;
1161 }
1162
1163 /**
1164 * Set the availability of the block as the editor
1165 * is loaded.
1166 *
1167 * @param string $slug Slug of the block.
1168 */
1169 public static function set_availability_for_plan( $slug ) {
1170 $slug = self::remove_extension_prefix( $slug );
1171
1172 if ( Jetpack_Plan::supports( $slug ) ) {
1173 self::set_extension_available( $slug );
1174 return;
1175 }
1176
1177 // Check what's the minimum plan where the feature is available.
1178 $plan = '';
1179 $features_data = array();
1180 $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
1181 $is_atomic_site = ( new Host() )->is_woa_site();
1182
1183 if ( $is_simple_site || $is_atomic_site ) {
1184 // Simple sites.
1185 if ( $is_simple_site ) {
1186 $features_data = self::get_site_specific_features();
1187 } else {
1188 // Atomic sites.
1189 $option = get_option( 'jetpack_active_plan' );
1190 if ( isset( $option['features'] ) ) {
1191 $features_data = $option['features'];
1192 }
1193 }
1194
1195 if ( ! empty( $features_data['available'][ $slug ] ) ) {
1196 $plan = $features_data['available'][ $slug ][0];
1197 }
1198 } else {
1199 // Jetpack sites.
1200 $plan = Jetpack_Plan::get_minimum_plan_for_feature( $slug );
1201 }
1202
1203 self::set_extension_unavailable(
1204 $slug,
1205 'missing_plan',
1206 array(
1207 'required_feature' => $slug,
1208 'required_plan' => $plan,
1209 )
1210 );
1211 }
1212
1213 /**
1214 * Wraps the suplied render_callback in a function to check
1215 * the availability of the block before rendering it.
1216 *
1217 * @param string $slug The block slug, used to check for availability.
1218 * @param callable $render_callback The render_callback that will be called if the block is available.
1219 */
1220 public static function get_render_callback_with_availability_check( $slug, $render_callback ) {
1221 return function ( $prepared_attributes, $block_content, $block ) use ( $render_callback, $slug ) {
1222 $availability = self::get_cached_availability();
1223 $bare_slug = self::remove_extension_prefix( $slug );
1224 if ( isset( $availability[ $bare_slug ] ) && $availability[ $bare_slug ]['available'] ) {
1225 return call_user_func( $render_callback, $prepared_attributes, $block_content );
1226 }
1227
1228 // A preview of the block is rendered for admins on the frontend with an upgrade nudge.
1229 if ( isset( $availability[ $bare_slug ] ) ) {
1230 if ( self::should_show_frontend_preview( $availability[ $bare_slug ] ) ) {
1231 $block_preview = call_user_func( $render_callback, $prepared_attributes, $block_content );
1232
1233 // If the upgrade nudge isn't already being displayed by a parent block, display the nudge.
1234 if ( isset( $block->attributes['shouldDisplayFrontendBanner'] ) && $block->attributes['shouldDisplayFrontendBanner'] ) {
1235 $upgrade_nudge = self::upgrade_nudge( $availability[ $bare_slug ]['details']['required_plan'] );
1236 return $upgrade_nudge . $block_preview;
1237 }
1238
1239 return $block_preview;
1240 }
1241 }
1242
1243 return null;
1244 };
1245 }
1246
1247 /**
1248 * Display a message to site editors and roles above when a block is no longer supported.
1249 * This is only displayed on the frontend.
1250 *
1251 * @since 12.3
1252 *
1253 * @param string $block_content The block content.
1254 * @param array $block The full block, including name and attributes.
1255 *
1256 * @return string
1257 */
1258 public static function display_deprecated_block_message( $block_content, $block ) {
1259 if ( in_array( $block['blockName'], self::$deprecated_blocks, true ) ) {
1260 if ( current_user_can( 'edit_posts' ) ) {
1261 $block_content = self::notice(
1262 __( 'This block is no longer supported. Its contents will no longer be displayed to your visitors and as such this block should be removed.', 'jetpack' ),
1263 'warning',
1264 'jetpack-block-deprecated'
1265 );
1266 } else {
1267 $block_content = '';
1268 }
1269 }
1270
1271 return $block_content;
1272 }
1273 }
1274
1275 if ( ( new Host() )->is_woa_site() ) {
1276 /**
1277 * Enable upgrade nudge for Atomic sites.
1278 * This feature is false as default,
1279 * so let's enable it through this filter.
1280 *
1281 * More doc: https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#upgrades-for-blocks
1282 */
1283 add_filter( 'jetpack_block_editor_enable_upgrade_nudge', '__return_true' );
1284 }
1285