PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 13.8.3
Jetpack – WP Security, Backup, Speed, & Growth v13.8.3
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 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 1 year ago css 2 years ago extensions 1 year ago images 1 year ago jetpack_vendor 1 day ago json-endpoints 1 year ago modules 1 year ago sal 2 years ago src 2 years ago vendor 1 year ago views 2 years ago CHANGELOG.md 1 year 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 1 year ago class.frame-nonce-preview.php 4 years ago class.jetpack-admin.php 1 year 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 1 year 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 3 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 1 year ago class.json-api-endpoints.php 2 years ago class.json-api.php 2 years ago class.photon.php 3 years ago composer.json 1 year 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 global.d.ts 2 years ago jetpack.php 1 day 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 day ago uninstall.php 2 years ago wpml-config.xml 4 years ago
class.jetpack-gutenberg.php
1299 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\My_Jetpack\Initializer as My_Jetpack_Initializer;
16 use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Dismissed_Notices;
17 use Automattic\Jetpack\Status;
18 use Automattic\Jetpack\Status\Host;
19
20 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move the functions and such to some other file.
21
22 /**
23 * General Gutenberg editor specific functionality
24 */
25 class Jetpack_Gutenberg {
26
27 /**
28 * Only these extensions can be registered. Used to control availability of beta blocks.
29 *
30 * @var array|null Extensions allowed list or `null` if not initialized yet.
31 * @see static::get_extensions()
32 */
33 private static $extensions = null;
34
35 /**
36 * Keeps track of the reasons why a given extension is unavailable.
37 *
38 * @var array Extensions availability information
39 */
40 private static $availability = array();
41
42 /**
43 * A cached array of the fully processed availability data. Keeps track of
44 * reasons why an extension is unavailable or missing.
45 *
46 * @var array Extensions availability information.
47 */
48 private static $cached_availability = null;
49
50 /**
51 * Site-specific features available.
52 * Their calculation can be expensive and slow, so we're caching it for the request.
53 *
54 * @var array Site-specific features
55 */
56 private static $site_specific_features = array();
57
58 /**
59 * List of deprecated blocks.
60 *
61 * @var array List of deprecated blocks.
62 */
63 private static $deprecated_blocks = array(
64 'jetpack/revue',
65 );
66
67 /**
68 * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in
69 * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can
70 * optionally fall back to that.
71 *
72 * @param array $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version.
73 * @param string $slug The slug of the block or plugin that has the gutenberg version requirement.
74 *
75 * @since 8.3.0
76 *
77 * @return boolean True if the version of gutenberg required by the block or plugin is available.
78 */
79 public static function is_gutenberg_version_available( $version_requirements, $slug ) {
80 global $wp_version;
81
82 // Bail if we don't at least have the gutenberg version requirement, the WP version is optional.
83 if ( empty( $version_requirements['gutenberg'] ) ) {
84 return false;
85 }
86
87 // If running a local dev build of gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version.
88 if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) {
89 return true;
90 }
91
92 $version_available = false;
93
94 // If running a production build of the gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version
95 // with required version of Gutenberg is known check that.
96 if ( defined( 'GUTENBERG_VERSION' ) ) {
97 $version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' );
98 } elseif ( ! empty( $version_requirements['wp'] ) ) {
99 $version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' );
100 }
101
102 if ( ! $version_available ) {
103 self::set_extension_unavailable(
104 $slug,
105 'incorrect_gutenberg_version',
106 array(
107 'required_feature' => $slug,
108 'required_version' => $version_requirements,
109 'current_version' => array(
110 'wp' => $wp_version,
111 'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null,
112 ),
113 )
114 );
115 }
116
117 return $version_available;
118 }
119
120 /**
121 * Prepend the 'jetpack/' prefix to a block name
122 *
123 * @param string $block_name The block name.
124 *
125 * @return string The prefixed block name.
126 */
127 private static function prepend_block_prefix( $block_name ) {
128 return 'jetpack/' . $block_name;
129 }
130
131 /**
132 * Remove the 'jetpack/' or jetpack-' prefix from an extension name
133 *
134 * @param string $extension_name The extension name.
135 *
136 * @return string The unprefixed extension name.
137 */
138 public static function remove_extension_prefix( $extension_name ) {
139 if ( str_starts_with( $extension_name, 'jetpack/' ) || str_starts_with( $extension_name, 'jetpack-' ) ) {
140 return substr( $extension_name, strlen( 'jetpack/' ) );
141 }
142 return $extension_name;
143 }
144
145 /**
146 * Whether two arrays share at least one item
147 *
148 * @param array $a An array.
149 * @param array $b Another array.
150 *
151 * @return boolean True if $a and $b share at least one item
152 */
153 protected static function share_items( $a, $b ) {
154 return array_intersect( $a, $b ) !== array();
155 }
156
157 /**
158 * Set a (non-block) extension as available
159 *
160 * @param string $slug Slug of the extension.
161 */
162 public static function set_extension_available( $slug ) {
163 self::$availability[ self::remove_extension_prefix( $slug ) ] = true;
164 }
165
166 /**
167 * Set the reason why an extension (block or plugin) is unavailable
168 *
169 * @param string $slug Slug of the extension.
170 * @param string $reason A string representation of why the extension is unavailable.
171 * @param array $details A free-form array containing more information on why the extension is unavailable.
172 */
173 public static function set_extension_unavailable( $slug, $reason, $details = array() ) {
174 if (
175 // Extensions that require a plan may be eligible for upgrades.
176 'missing_plan' === $reason
177 && (
178 /**
179 * Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false`
180 * to disable paid feature upgrade nudges in the block editor.
181 *
182 * When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php`
183 * See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378
184 *
185 * @since 7.7.0
186 *
187 * @param boolean
188 */
189 ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false )
190 /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
191 || ! apply_filters( 'jetpack_show_promotions', true )
192 )
193 ) {
194 // The block editor may apply an upgrade nudge if `missing_plan` is the reason.
195 // Add a descriptive suffix to disable behavior but provide informative reason.
196 $reason .= '__nudge_disabled';
197 }
198
199 self::$availability[ self::remove_extension_prefix( $slug ) ] = array(
200 'reason' => $reason,
201 'details' => $details,
202 );
203 }
204
205 /**
206 * Used to initialize the class, no longer in use.
207 *
208 * @return void
209 * @deprecated 12.2 No longer needed.
210 */
211 public static function init() {
212 _deprecated_function( __METHOD__, '12.2' );
213 }
214
215 /**
216 * Resets the class to its original state
217 *
218 * Used in unit tests
219 *
220 * @return void
221 */
222 public static function reset() {
223 self::$extensions = null;
224 self::$availability = array();
225 self::$cached_availability = null;
226 }
227
228 /**
229 * Return the Gutenberg extensions (blocks and plugins) directory
230 *
231 * @return string The Gutenberg extensions directory
232 */
233 public static function get_blocks_directory() {
234 /**
235 * Filter to select Gutenberg blocks directory
236 *
237 * @since 6.9.0
238 *
239 * @param string default: '_inc/blocks/'
240 */
241 return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
242 }
243
244 /**
245 * Checks for a given .json file in the blocks folder.
246 *
247 * @param string $preset The name of the .json file to look for.
248 *
249 * @return bool True if the file is found.
250 */
251 public static function preset_exists( $preset ) {
252 return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
253 }
254
255 /**
256 * Decodes JSON loaded from a preset file in the blocks folder
257 *
258 * @param string $preset The name of the .json file to load.
259 *
260 * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
261 */
262 public static function get_preset( $preset ) {
263 return json_decode(
264 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
265 file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
266 );
267 }
268
269 /**
270 * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
271 *
272 * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
273 */
274 public static function get_jetpack_gutenberg_extensions_allowed_list() {
275 $preset_extensions_manifest = self::preset_exists( 'index' )
276 ? self::get_preset( 'index' )
277 : (object) array();
278 $blocks_variation = self::blocks_variation();
279
280 return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation );
281 }
282
283 /**
284 * Returns a diff from a combined list of allowed extensions and extensions determined to be excluded
285 *
286 * @param array $allowed_extensions An array of allowed extensions.
287 *
288 * @return array A list of blocks: eg array( 'publicize', 'markdown' )
289 */
290 public static function get_available_extensions( $allowed_extensions = null ) {
291 $exclusions = get_option( 'jetpack_excluded_extensions', array() );
292 $allowed_extensions = $allowed_extensions === null ? self::get_jetpack_gutenberg_extensions_allowed_list() : $allowed_extensions;
293
294 return array_diff( $allowed_extensions, $exclusions );
295 }
296
297 /**
298 * Return true if the extension has been registered and there's nothing in the availablilty array.
299 *
300 * @param string $extension The name of the extension.
301 *
302 * @return bool whether the extension has been registered and there's nothing in the availablilty array.
303 */
304 public static function is_registered_and_no_entry_in_availability( $extension ) {
305 return self::is_registered( 'jetpack/' . $extension ) && ! isset( self::$availability[ $extension ] );
306 }
307
308 /**
309 * Return true if the extension has a true entry in the availablilty array.
310 *
311 * @param string $extension The name of the extension.
312 *
313 * @return bool whether the extension has a true entry in the availablilty array.
314 */
315 public static function is_available( $extension ) {
316 return isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ];
317 }
318
319 /**
320 * Get the availability of each block / plugin, or return the cached availability
321 * if it has already been calculated. Avoids re-registering extensions when not
322 * necessary.
323 *
324 * @return array A list of block and plugins and their availability status.
325 */
326 public static function get_cached_availability() {
327 if ( null === self::$cached_availability ) {
328 self::$cached_availability = self::get_availability();
329 }
330 return self::$cached_availability;
331 }
332
333 /**
334 * Get availability of each block / plugin.
335 *
336 * @return array A list of block and plugins and their availablity status
337 */
338 public static function get_availability() {
339 /**
340 * Fires before Gutenberg extensions availability is computed.
341 *
342 * In the function call you supply, use `Blocks::jetpack_register_block()` to set a block as available.
343 * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and
344 * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered
345 * but marked as unavailable).
346 *
347 * @since 7.0.0
348 */
349 do_action( 'jetpack_register_gutenberg_extensions' );
350
351 $available_extensions = array();
352
353 foreach ( static::get_extensions() as $extension ) {
354 $is_available = self::is_registered_and_no_entry_in_availability( $extension ) || self::is_available( $extension );
355 $available_extensions[ $extension ] = array(
356 'available' => $is_available,
357 );
358
359 if ( ! $is_available ) {
360 $reason = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['reason'] : 'missing_module';
361 $details = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['details'] : array();
362 $available_extensions[ $extension ]['unavailable_reason'] = $reason;
363 $available_extensions[ $extension ]['details'] = $details;
364 }
365 }
366
367 return $available_extensions;
368 }
369
370 /**
371 * Return the list of extensions that are available.
372 *
373 * @since 11.9
374 *
375 * @return array A list of block and plugins and their availability status.
376 */
377 public static function get_extensions() {
378 if ( ! static::should_load() ) {
379 return array();
380 }
381
382 if ( null === self::$extensions ) {
383 /**
384 * Filter the list of block editor extensions that are available through Jetpack.
385 *
386 * @since 7.0.0
387 *
388 * @param array
389 */
390 self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() );
391 }
392
393 return self::$extensions;
394 }
395
396 /**
397 * Check if an extension/block is already registered
398 *
399 * @since 7.2
400 *
401 * @param string $slug Name of extension/block to check.
402 *
403 * @return bool
404 */
405 public static function is_registered( $slug ) {
406 return WP_Block_Type_Registry::get_instance()->is_registered( $slug );
407 }
408
409 /**
410 * Check if Gutenberg editor is available
411 *
412 * @since 6.7.0
413 *
414 * @return bool
415 */
416 public static function is_gutenberg_available() {
417 return true;
418 }
419
420 /**
421 * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
422 *
423 * Loading blocks and plugins is enabled by default and may be disabled via filter:
424 * add_filter( 'jetpack_gutenberg', '__return_false' );
425 *
426 * @since 6.9.0
427 *
428 * @return bool
429 */
430 public static function should_load() {
431 if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) {
432 return false;
433 }
434
435 if ( get_option( 'jetpack_blocks_disabled', false ) ) {
436 return false;
437 }
438
439 /**
440 * Filter to disable Gutenberg blocks
441 *
442 * @since 6.5.0
443 *
444 * @param bool true Whether to load Gutenberg blocks
445 */
446 return (bool) apply_filters( 'jetpack_gutenberg', true );
447 }
448
449 /**
450 * Queue a script to set `Jetpack_Block_Assets_Base_Url`.
451 *
452 * In certain cases Webpack needs to know a base to load additional assets from.
453 * Normally it can determine that itself, but when JS concatenation is involved that tends to confuse it.
454 * We work around that by explicitly outputting a variable with the correct URL.
455 * We set that as its own "script" so we can reliably only output it once.
456 */
457 private static function register_blocks_assets_base_url() {
458 if ( ! wp_script_is( 'jetpack-blocks-assets-base-url', 'registered' ) ) {
459 // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- No actual script, so no version needed.
460 wp_register_script( 'jetpack-blocks-assets-base-url', false, array(), null, array( 'in_footer' => false ) );
461 $json_encode_flags = JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP;
462 if ( get_option( 'blog_charset' ) === 'UTF-8' ) {
463 $json_encode_flags |= JSON_UNESCAPED_UNICODE;
464 }
465 wp_add_inline_script(
466 'jetpack-blocks-assets-base-url',
467 'var Jetpack_Block_Assets_Base_Url=' . wp_json_encode( plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ), $json_encode_flags ) . ';',
468 'before'
469 );
470 }
471 }
472
473 /**
474 * Only enqueue block assets when needed.
475 *
476 * @param string $type Slug of the block or absolute path to the block source code directory.
477 * @param array $script_dependencies Script dependencies. Will be merged with automatically
478 * detected script dependencies from the webpack build.
479 *
480 * @return void
481 */
482 public static function load_assets_as_required( $type, $script_dependencies = array() ) {
483 if ( is_admin() ) {
484 // A block's view assets will not be required in wp-admin.
485 return;
486 }
487
488 // Retrieve the feature from block.json if a path is passed.
489 if ( path_is_absolute( $type ) ) {
490 $metadata = Blocks::get_block_metadata_from_file( Blocks::get_path_to_block_metadata( $type ) );
491 $feature = Blocks::get_block_feature_from_metadata( $metadata );
492
493 if ( ! empty( $feature ) ) {
494 $type = $feature;
495 }
496 }
497
498 $type = sanitize_title_with_dashes( $type );
499 self::load_styles_as_required( $type );
500 self::load_scripts_as_required( $type, $script_dependencies );
501 }
502
503 /**
504 * Only enqueue block sytles when needed.
505 *
506 * @param string $type Slug of the block.
507 *
508 * @since 7.2.0
509 *
510 * @return void
511 */
512 public static function load_styles_as_required( $type ) {
513 if ( is_admin() ) {
514 // A block's view assets will not be required in wp-admin.
515 return;
516 }
517
518 // Enqueue styles.
519 $style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
520 if ( self::block_has_asset( $style_relative_path ) ) {
521 $style_version = self::get_asset_version( $style_relative_path );
522 $view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
523 $view_style = add_query_arg( 'minify', 'false', $view_style );
524
525 // If this is a customizer preview, render the style directly to the preview after autosave.
526 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
527 if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) {
528 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
529 echo '<link rel="stylesheet" id="jetpack-block-' . esc_attr( $type ) . '" href="' . esc_attr( $view_style ) . '&amp;ver=' . esc_attr( $style_version ) . '" media="all">';
530 } else {
531 wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
532 wp_style_add_data( 'jetpack-block-' . $type, 'path', JETPACK__PLUGIN_DIR . $style_relative_path );
533 }
534 }
535 }
536
537 /**
538 * Only enqueue block scripts when needed.
539 *
540 * @param string $type Slug of the block.
541 * @param array $script_dependencies Script dependencies. Will be merged with automatically
542 * detected script dependencies from the webpack build.
543 *
544 * @since 7.2.0
545 *
546 * @return void
547 */
548 public static function load_scripts_as_required( $type, $script_dependencies = array() ) {
549 if ( is_admin() ) {
550 // A block's view assets will not be required in wp-admin.
551 return;
552 }
553
554 self::register_blocks_assets_base_url();
555
556 // Enqueue script.
557 $script_relative_path = self::get_blocks_directory() . $type . '/view.js';
558 $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php';
559 $script_dependencies[] = 'wp-polyfill';
560 $script_dependencies[] = 'jetpack-blocks-assets-base-url';
561 if ( file_exists( $script_deps_path ) ) {
562 $asset_manifest = include $script_deps_path;
563 $script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) );
564 }
565
566 if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) {
567 $script_version = self::get_asset_version( $script_relative_path );
568 $view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
569 $view_script = add_query_arg( 'minify', 'false', $view_script );
570
571 // Enqueue dependencies.
572 wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
573
574 // If this is a customizer preview, enqueue the dependencies and render the script directly to the preview after autosave.
575 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
576 if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) {
577 // The Map block is dependent on wp-element, and it doesn't appear to to be possible to load
578 // this dynamically into the customizer iframe currently.
579 if ( 'map' === $type ) {
580 echo '<div>' . esc_html_e( 'No map preview available. Publish and refresh to see this widget.', 'jetpack' ) . '</div>';
581 echo '<script>';
582 echo 'Array.from(document.getElementsByClassName(\'wp-block-jetpack-map\')).forEach(function(element){element.style.display = \'none\';})';
583 echo '</script>';
584 } else {
585 echo '<script id="jetpack-block-' . esc_attr( $type ) . '" src="' . esc_attr( $view_script ) . '&amp;ver=' . esc_attr( $script_version ) . '"></script>';
586 }
587 }
588 }
589 }
590
591 /**
592 * Check if an asset exists for a block.
593 *
594 * @param string $file Path of the file we are looking for.
595 *
596 * @return bool $block_has_asset Does the file exist.
597 */
598 public static function block_has_asset( $file ) {
599 return file_exists( JETPACK__PLUGIN_DIR . $file );
600 }
601
602 /**
603 * Get the version number to use when loading the file. Allows us to bypass cache when developing.
604 *
605 * @param string $file Path of the file we are looking for.
606 *
607 * @return string $script_version Version number.
608 */
609 public static function get_asset_version( $file ) {
610 return Jetpack::is_development_version() && self::block_has_asset( $file )
611 ? filemtime( JETPACK__PLUGIN_DIR . $file )
612 : JETPACK__VERSION;
613 }
614
615 /**
616 * Load Gutenberg editor assets
617 *
618 * @since 6.7.0
619 *
620 * @return void
621 */
622 public static function enqueue_block_editor_assets() {
623 if ( ! self::should_load() ) {
624 return;
625 }
626
627 /**
628 * This can be called multiple times per page load in the admin, during the `enqueue_block_assets` action.
629 * These assets are necessary for the admin for editing but are not necessary for each pattern preview.
630 * Therefore we dequeue them, so they don't load for each pattern preview iframe.
631 */
632 if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
633 wp_dequeue_script( 'jp-tracks' );
634 wp_dequeue_script( 'jetpack-blocks-editor' );
635
636 return;
637 }
638
639 $status = new Status();
640
641 // Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php.
642 if ( ! $status->is_offline_mode() && Jetpack::is_connection_ready() ) {
643 wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
644 }
645
646 $blocks_dir = self::get_blocks_directory();
647 $blocks_variation = self::blocks_variation();
648
649 if ( 'production' !== $blocks_variation ) {
650 $blocks_env = '-' . esc_attr( $blocks_variation );
651 } else {
652 $blocks_env = '';
653 }
654
655 self::register_blocks_assets_base_url();
656
657 Assets::register_script(
658 'jetpack-blocks-editor',
659 "{$blocks_dir}editor{$blocks_env}.js",
660 JETPACK__PLUGIN_FILE,
661 array(
662 'textdomain' => 'jetpack',
663 'dependencies' => array( 'jetpack-blocks-assets-base-url' ),
664 )
665 );
666
667 // Hack around #20357 (specifically, that the editor bundle depends on
668 // wp-edit-post but wp-edit-post's styles break the Widget Editor and
669 // Site Editor) until a real fix gets unblocked.
670 // @todo Remove this once #20357 is properly fixed.
671 wp_styles()->query( 'jetpack-blocks-editor', 'registered' )->deps = array();
672
673 Assets::enqueue_script( 'jetpack-blocks-editor' );
674
675 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
676 $user = wp_get_current_user();
677 $user_data = array(
678 'email' => $user->user_email,
679 'userid' => $user->ID,
680 'username' => $user->user_login,
681 );
682 $blog_id = get_current_blog_id();
683 $is_current_user_connected = true;
684 } else {
685 $user_data = Jetpack_Tracks_Client::get_connected_user_tracks_identity();
686 $blog_id = Jetpack_Options::get_option( 'id', 0 );
687 $is_current_user_connected = ( new Connection_Manager( 'jetpack' ) )->is_user_connected();
688 }
689
690 if ( $blocks_variation === 'beta' && $is_current_user_connected ) {
691 wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Constants::get_constant( 'JETPACK__VERSION' ) );
692 }
693 // AI Assistant
694 $ai_assistant_state = array(
695 'is-enabled' => apply_filters( 'jetpack_ai_enabled', true ),
696 );
697
698 $screen_base = null;
699 if ( function_exists( 'get_current_screen' ) ) {
700 $screen_base = get_current_screen()->base;
701 }
702
703 $modules = array();
704 if ( class_exists( 'Jetpack_Core_API_Module_List_Endpoint' ) ) {
705 $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint();
706 $modules = $module_list_endpoint->get_modules();
707 }
708
709 $initial_state = array(
710 'available_blocks' => self::get_availability(),
711 'blocks_variation' => $blocks_variation,
712 'modules' => $modules,
713 'jetpack' => array(
714 'is_active' => Jetpack::is_connection_ready(),
715 'is_current_user_connected' => $is_current_user_connected,
716 /** This filter is documented in class.jetpack-gutenberg.php */
717 'enable_upgrade_nudge' => apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ),
718 'is_private_site' => $status->is_private_site(),
719 'is_coming_soon' => $status->is_coming_soon(),
720 'is_offline_mode' => $status->is_offline_mode(),
721 'is_newsletter_feature_enabled' => class_exists( '\Jetpack_Memberships' ),
722 // this is the equivalent of JP initial state siteData.showMyJetpack (class-jetpack-redux-state-helper)
723 // used to determine if we can link to My Jetpack from the block editor
724 'is_my_jetpack_available' => My_Jetpack_Initializer::should_initialize(),
725 /**
726 * Enable the RePublicize UI in the block editor context.
727 *
728 * @module publicize
729 *
730 * @since 10.3.0
731 * @deprecated 11.5 This is a feature flag that is no longer used.
732 *
733 * @param bool true Enable the RePublicize UI in the block editor context. Defaults to true.
734 */
735 'republicize_enabled' => apply_filters( 'jetpack_block_editor_republicize_feature', true ),
736 ),
737 'siteFragment' => $status->get_site_suffix(),
738 'adminUrl' => esc_url( admin_url() ),
739 'tracksUserData' => $user_data,
740 'wpcomBlogId' => $blog_id,
741 'allowedMimeTypes' => wp_get_mime_types(),
742 'siteLocale' => str_replace( '_', '-', get_locale() ),
743 'ai-assistant' => $ai_assistant_state,
744 'screenBase' => $screen_base,
745 'pluginBasePath' => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ),
746 );
747
748 if ( Jetpack::is_module_active( 'publicize' ) && function_exists( 'publicize_init' ) ) {
749 $publicize = publicize_init();
750 $jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
751 $social_initial_state = $jetpack_social_settings->get_initial_state();
752
753 $initial_state['social'] = array(
754 'sharesData' => $publicize->get_publicize_shares_info( $blog_id ),
755 'hasPaidPlan' => $publicize->has_paid_plan(),
756 'hasPaidFeatures' => $publicize->has_paid_features(),
757 'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(),
758 'isSocialImageGeneratorAvailable' => $social_initial_state['socialImageGeneratorSettings']['available'],
759 'isSocialImageGeneratorEnabled' => $social_initial_state['socialImageGeneratorSettings']['enabled'],
760 'dismissedNotices' => Dismissed_Notices::get_dismissed_notices(),
761 'supportedAdditionalConnections' => $publicize->get_supported_additional_connections(),
762 'autoConversionSettings' => $social_initial_state['autoConversionSettings'],
763 'jetpackSharingSettingsUrl' => esc_url_raw( admin_url( 'admin.php?page=jetpack#/sharing' ) ),
764 'userConnectionUrl' => esc_url_raw( admin_url( 'admin.php?page=my-jetpack#/connection' ) ),
765 'useAdminUiV1' => $social_initial_state['useAdminUiV1'],
766 );
767
768 // Add connectionData if we are using the new Connection UI.
769 if ( $social_initial_state['useAdminUiV1'] ) {
770 $initial_state['social']['connectionData'] = $social_initial_state['connectionData'];
771
772 $initial_state['social']['connectionRefreshPath'] = $social_initial_state['connectionRefreshPath'];
773 }
774
775 $initial_state['social']['featureFlags'] = $social_initial_state['featureFlags'];
776 }
777
778 wp_localize_script(
779 'jetpack-blocks-editor',
780 'Jetpack_Editor_Initial_State',
781 $initial_state
782 );
783
784 // Adds Connection package initial state.
785 Connection_Initial_State::render_script( 'jetpack-blocks-editor' );
786 }
787
788 /**
789 * Some blocks do not depend on a specific module,
790 * and can consequently be loaded outside of the usual modules.
791 * We will look for such modules in the extensions/ directory.
792 *
793 * @since 7.1.0
794 * @see wp_common_block_scripts_and_styles()
795 */
796 public static function load_independent_blocks() {
797 if ( self::should_load() ) {
798 /**
799 * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
800 * If available, load them.
801 */
802 $directories = array( 'blocks', 'plugins', 'extended-blocks', 'shared', 'store' );
803
804 foreach ( static::get_extensions() as $extension ) {
805 foreach ( $directories as $dirname ) {
806 $path = JETPACK__PLUGIN_DIR . "extensions/{$dirname}/{$extension}/{$extension}.php";
807
808 if ( file_exists( $path ) ) {
809 include_once $path;
810 continue 2;
811 }
812 }
813 }
814 }
815 }
816
817 /**
818 * Loads PHP components of block editor extensions.
819 *
820 * @since 8.9.0
821 */
822 public static function load_block_editor_extensions() {
823 if ( self::should_load() ) {
824 // Block editor extensions to load.
825 $extensions_to_load = array(
826 'extended-blocks',
827 'plugins',
828 );
829
830 // Collect the extension paths.
831 foreach ( $extensions_to_load as $extension_to_load ) {
832 $extensions_folder = glob( JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/*' );
833
834 // Require each of the extension files, in case it exists.
835 foreach ( $extensions_folder as $extension_folder ) {
836 $name = basename( $extension_folder );
837 $extension_file_path = JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/' . $name . '/' . $name . '.php';
838
839 if ( file_exists( $extension_file_path ) ) {
840 include_once $extension_file_path;
841 }
842 }
843 }
844 }
845 }
846
847 /**
848 * Determine whether a site should use the default set of blocks, or a custom set.
849 * Possible variations are currently beta, experimental, and production.
850 *
851 * @since 8.1.0
852 *
853 * @return string $block_varation production|beta|experimental
854 */
855 public static function blocks_variation() {
856 // Default to production blocks.
857 $block_varation = 'production';
858
859 /*
860 * Prefer to use this JETPACK_BLOCKS_VARIATION constant
861 * or the jetpack_blocks_variation filter
862 * to set the block variation in your code.
863 */
864 $default = Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' );
865 if ( ! empty( $default ) && in_array( $default, array( 'beta', 'experimental', 'production' ), true ) ) {
866 $block_varation = $default;
867 }
868
869 /**
870 * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
871 *
872 * @since 6.9.0
873 * @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
874 *
875 * @param boolean
876 */
877 $is_beta = apply_filters_deprecated(
878 'jetpack_load_beta_blocks',
879 array( false ),
880 'jetpack-11.8.0',
881 'jetpack_blocks_variation'
882 );
883
884 /*
885 * Switch to beta blocks if you use the JETPACK_BETA_BLOCKS constant
886 * or the deprecated jetpack_load_beta_blocks filter.
887 * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
888 */
889 if (
890 empty( $default )
891 && (
892 $is_beta
893 || Constants::is_true( 'JETPACK_BETA_BLOCKS' )
894 )
895 ) {
896 $block_varation = 'beta';
897 }
898
899 /**
900 * Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks.
901 *
902 * @since 6.9.0
903 * @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
904 *
905 * @param boolean
906 */
907 $is_experimental = apply_filters_deprecated(
908 'jetpack_load_experimental_blocks',
909 array( false ),
910 'jetpack-11.8.0',
911 'jetpack_blocks_variation'
912 );
913
914 /*
915 * Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant
916 * or the deprecated jetpack_load_experimental_blocks filter.
917 * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
918 */
919 if (
920 empty( $default )
921 && (
922 $is_experimental
923 || Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' )
924 )
925 ) {
926 $block_varation = 'experimental';
927 }
928
929 /**
930 * Allow customizing the variation of blocks in use on a site.
931 * Overwrites any previously set values, whether by constant or filter.
932 *
933 * @since 8.1.0
934 *
935 * @param string $block_variation Can be beta, experimental, and production. Defaults to production.
936 */
937 return apply_filters( 'jetpack_blocks_variation', $block_varation );
938 }
939
940 /**
941 * Get a list of extensions available for the variation you chose.
942 *
943 * @since 8.1.0
944 *
945 * @param object $preset_extensions_manifest List of extensions available in Jetpack.
946 * @param string $blocks_variation Subset of blocks. production|beta|experimental.
947 *
948 * @return array $preset_extensions Array of extensions for that variation
949 */
950 public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) {
951 $preset_extensions = isset( $preset_extensions_manifest->{ $blocks_variation } )
952 ? (array) $preset_extensions_manifest->{ $blocks_variation }
953 : array();
954
955 /*
956 * Experimental and Beta blocks need the production blocks as well.
957 */
958 if (
959 'experimental' === $blocks_variation
960 || 'beta' === $blocks_variation
961 ) {
962 $production_extensions = isset( $preset_extensions_manifest->production )
963 ? (array) $preset_extensions_manifest->production
964 : array();
965
966 $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
967 }
968
969 /*
970 * Beta blocks need the experimental blocks as well.
971 *
972 * If you've chosen to see Beta blocks,
973 * we want to make all blocks available to you:
974 * - Production
975 * - Experimental
976 * - Beta
977 */
978 if ( 'beta' === $blocks_variation ) {
979 $production_extensions = isset( $preset_extensions_manifest->experimental )
980 ? (array) $preset_extensions_manifest->experimental
981 : array();
982
983 $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
984 }
985
986 return $preset_extensions;
987 }
988
989 /**
990 * Validate a URL used in a SSR block.
991 *
992 * @since 8.3.0
993 *
994 * @param string $url URL saved as an attribute in block.
995 * @param array $allowed Array of allowed hosts for that block, or regexes to check against.
996 * @param bool $is_regex Array of regexes matching the URL that could be used in block.
997 *
998 * @return bool|string
999 */
1000 public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) {
1001 if (
1002 empty( $url )
1003 || ! is_array( $allowed )
1004 || empty( $allowed )
1005 ) {
1006 return false;
1007 }
1008
1009 $url_components = wp_parse_url( $url );
1010
1011 // Bail early if we cannot find a host.
1012 if ( empty( $url_components['host'] ) ) {
1013 return false;
1014 }
1015
1016 // Normalize URL.
1017 $url = sprintf(
1018 '%s://%s%s%s',
1019 isset( $url_components['scheme'] ) ? $url_components['scheme'] : 'https',
1020 $url_components['host'],
1021 isset( $url_components['path'] ) ? $url_components['path'] : '/',
1022 isset( $url_components['query'] ) ? '?' . $url_components['query'] : ''
1023 );
1024
1025 if ( ! empty( $url_components['fragment'] ) ) {
1026 $url = $url . '#' . rawurlencode( $url_components['fragment'] );
1027 }
1028
1029 /*
1030 * If we're using an allowed list of hosts,
1031 * check if the URL belongs to one of the domains allowed for that block.
1032 */
1033 if (
1034 false === $is_regex
1035 && in_array( $url_components['host'], $allowed, true )
1036 ) {
1037 return $url;
1038 }
1039
1040 /*
1041 * If we are using an array of regexes to check against,
1042 * loop through that.
1043 */
1044 if ( true === $is_regex ) {
1045 foreach ( $allowed as $regex ) {
1046 if ( 1 === preg_match( $regex, $url ) ) {
1047 return $url;
1048 }
1049 }
1050 }
1051
1052 return false;
1053 }
1054
1055 /**
1056 * Determines whether a preview of the block with an upgrade nudge should
1057 * be displayed for admins on the site frontend.
1058 *
1059 * @since 8.4.0
1060 *
1061 * @param array $availability_for_block The availability for the block.
1062 *
1063 * @return bool
1064 */
1065 public static function should_show_frontend_preview( $availability_for_block ) {
1066 return (
1067 isset( $availability_for_block['details']['required_plan'] )
1068 && current_user_can( 'manage_options' )
1069 && ! is_feed()
1070 );
1071 }
1072
1073 /**
1074 * Output an UpgradeNudge Component on the frontend of a site.
1075 *
1076 * @since 8.4.0
1077 *
1078 * @param string $plan The plan that users need to purchase to make the block work.
1079 *
1080 * @return string
1081 */
1082 public static function upgrade_nudge( $plan ) {
1083 require_once JETPACK__PLUGIN_DIR . '_inc/lib/components.php';
1084 return Jetpack_Components::render_upgrade_nudge(
1085 array(
1086 'plan' => $plan,
1087 )
1088 );
1089 }
1090
1091 /**
1092 * Output a notice within a block.
1093 *
1094 * @since 8.6.0
1095 *
1096 * @param string $message Notice we want to output.
1097 * @param string $status Status of the notice. Can be one of success, info, warning, error. info by default.
1098 * @param string $classes List of CSS classes.
1099 *
1100 * @return string
1101 */
1102 public static function notice( $message, $status = 'info', $classes = '' ) {
1103 if (
1104 empty( $message )
1105 || ! in_array( $status, array( 'success', 'info', 'warning', 'error' ), true )
1106 ) {
1107 return '';
1108 }
1109
1110 $color = '';
1111 switch ( $status ) {
1112 case 'success':
1113 $color = '#00a32a';
1114 break;
1115 case 'warning':
1116 $color = '#dba617';
1117 break;
1118 case 'error':
1119 $color = '#d63638';
1120 break;
1121 case 'info':
1122 default:
1123 $color = '#72aee6';
1124 break;
1125 }
1126
1127 return sprintf(
1128 '<div class="jetpack-block__notice %1$s %3$s" style="border-left:5px solid %4$s;padding:1em;background-color:#f8f9f9;">%2$s</div>',
1129 esc_attr( $status ),
1130 wp_kses(
1131 $message,
1132 array(
1133 'br' => array(),
1134 'p' => array(),
1135 'a' => array(
1136 'href' => array(),
1137 'target' => array(),
1138 'rel' => array(),
1139 ),
1140 )
1141 ),
1142 esc_attr( $classes ),
1143 sanitize_hex_color( $color )
1144 );
1145 }
1146
1147 /**
1148 * Retrieve site-specific features for Simple sites.
1149 *
1150 * We're caching the data for the lifetime of the request, because it can be slow to calculate,
1151 * and it can be called multiple times per single request.
1152 *
1153 * We intentionally don't use object caching or any other type of persistent caching,
1154 * in order to avoid complex cache invalidation on subscription addition or removal.
1155 *
1156 * @since 10.7
1157 *
1158 * @return array
1159 */
1160 private static function get_site_specific_features() {
1161 $current_blog_id = get_current_blog_id();
1162
1163 if ( isset( self::$site_specific_features[ $current_blog_id ] ) ) {
1164 return self::$site_specific_features[ $current_blog_id ];
1165 }
1166
1167 if ( ! class_exists( 'Store_Product_List' ) ) {
1168 require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
1169 }
1170
1171 $site_specific_features = Store_Product_List::get_site_specific_features_data( $current_blog_id );
1172 self::$site_specific_features[ $current_blog_id ] = $site_specific_features;
1173
1174 return $site_specific_features;
1175 }
1176
1177 /**
1178 * Set the availability of the block as the editor
1179 * is loaded.
1180 *
1181 * @param string $slug Slug of the block.
1182 */
1183 public static function set_availability_for_plan( $slug ) {
1184 $slug = self::remove_extension_prefix( $slug );
1185
1186 if ( Jetpack_Plan::supports( $slug ) ) {
1187 self::set_extension_available( $slug );
1188 return;
1189 }
1190
1191 // Check what's the minimum plan where the feature is available.
1192 $plan = '';
1193 $features_data = array();
1194 $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
1195 $is_atomic_site = ( new Host() )->is_woa_site();
1196
1197 if ( $is_simple_site || $is_atomic_site ) {
1198 // Simple sites.
1199 if ( $is_simple_site ) {
1200 $features_data = self::get_site_specific_features();
1201 } else {
1202 // Atomic sites.
1203 $option = get_option( 'jetpack_active_plan' );
1204 if ( isset( $option['features'] ) ) {
1205 $features_data = $option['features'];
1206 }
1207 }
1208
1209 if ( ! empty( $features_data['available'][ $slug ] ) ) {
1210 $plan = $features_data['available'][ $slug ][0];
1211 }
1212 } else {
1213 // Jetpack sites.
1214 $plan = Jetpack_Plan::get_minimum_plan_for_feature( $slug );
1215 }
1216
1217 self::set_extension_unavailable(
1218 $slug,
1219 'missing_plan',
1220 array(
1221 'required_feature' => $slug,
1222 'required_plan' => $plan,
1223 )
1224 );
1225 }
1226
1227 /**
1228 * Wraps the suplied render_callback in a function to check
1229 * the availability of the block before rendering it.
1230 *
1231 * @param string $slug The block slug, used to check for availability.
1232 * @param callable $render_callback The render_callback that will be called if the block is available.
1233 */
1234 public static function get_render_callback_with_availability_check( $slug, $render_callback ) {
1235 return function ( $prepared_attributes, $block_content, $block ) use ( $render_callback, $slug ) {
1236 $availability = self::get_cached_availability();
1237 $bare_slug = self::remove_extension_prefix( $slug );
1238 if ( isset( $availability[ $bare_slug ] ) && $availability[ $bare_slug ]['available'] ) {
1239 return call_user_func( $render_callback, $prepared_attributes, $block_content );
1240 }
1241
1242 // A preview of the block is rendered for admins on the frontend with an upgrade nudge.
1243 if ( isset( $availability[ $bare_slug ] ) ) {
1244 if ( self::should_show_frontend_preview( $availability[ $bare_slug ] ) ) {
1245 $block_preview = call_user_func( $render_callback, $prepared_attributes, $block_content );
1246
1247 // If the upgrade nudge isn't already being displayed by a parent block, display the nudge.
1248 if ( isset( $block->attributes['shouldDisplayFrontendBanner'] ) && $block->attributes['shouldDisplayFrontendBanner'] ) {
1249 $upgrade_nudge = self::upgrade_nudge( $availability[ $bare_slug ]['details']['required_plan'] );
1250 return $upgrade_nudge . $block_preview;
1251 }
1252
1253 return $block_preview;
1254 }
1255 }
1256
1257 return null;
1258 };
1259 }
1260
1261 /**
1262 * Display a message to site editors and roles above when a block is no longer supported.
1263 * This is only displayed on the frontend.
1264 *
1265 * @since 12.3
1266 *
1267 * @param string $block_content The block content.
1268 * @param array $block The full block, including name and attributes.
1269 *
1270 * @return string
1271 */
1272 public static function display_deprecated_block_message( $block_content, $block ) {
1273 if ( in_array( $block['blockName'], self::$deprecated_blocks, true ) ) {
1274 if ( current_user_can( 'edit_posts' ) ) {
1275 $block_content = self::notice(
1276 __( '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' ),
1277 'warning',
1278 'jetpack-block-deprecated'
1279 );
1280 } else {
1281 $block_content = '';
1282 }
1283 }
1284
1285 return $block_content;
1286 }
1287 }
1288
1289 if ( ( new Host() )->is_woa_site() ) {
1290 /**
1291 * Enable upgrade nudge for Atomic sites.
1292 * This feature is false as default,
1293 * so let's enable it through this filter.
1294 *
1295 * More doc: https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#upgrades-for-blocks
1296 */
1297 add_filter( 'jetpack_block_editor_enable_upgrade_nudge', '__return_true' );
1298 }
1299