PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 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 All 507 releases
← All changes | jetpack_vendor/automattic/jetpack-blocks/src/class-blocks.php +170 -17 12.7.3 → 16.3-beta View file →
@@ -37,14 +37,14 @@
37 37 * @type array $version_requirements Array containing required Gutenberg version and, if known, the WordPress version that was released with this minimum version.
38 38 * @type bool $plan_check Should we check for a specific plan before registering the block.
39 39 * }
40 40 *
41 - * @return WP_Block_Type|false The registered block type on success, or false on failure.
41 + * @return \WP_Block_Type|false The registered block type on success, or false on failure.
42 42 */
43 43 public static function jetpack_register_block( $slug, $args = array() ) {
44 44 // Slug doesn't start with `jetpack/`, isn't an absolute path, or doesn't contain a slash
45 45 // (synonym of a namespace) at all.
46 - if ( 0 !== strpos( $slug, 'jetpack/' ) && ! path_is_absolute( $slug ) && ! strpos( $slug, '/' ) ) {
46 + if ( ! str_starts_with( $slug, 'jetpack/' ) && ! path_is_absolute( $slug ) && ! strpos( $slug, '/' ) ) {
47 47 _doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', 'Jetpack 9.0.0' );
48 48 $slug = 'jetpack/' . $slug;
49 49 }
50 50
@@ -49,20 +49,13 @@
49 49 }
50 50
51 51 $block_type = $slug;
52 52
53 - // If a path is passed, find the slug in the file then create a block type object to register
54 - // the block.
55 - // Note: passing the path directly to register_block_type seems to loose the interactivity of
56 - // the block once in the editor once it's out of focus.
53 + // If a path is passed, make sure to get the block.json file from the build directory and get
54 + // the block name from that file.
57 55 if ( path_is_absolute( $slug ) ) {
58 - $metadata = self::get_block_metadata_from_file( self::get_path_to_block_metadata( $slug ) );
59 - $name = self::get_block_name_from_metadata( $metadata );
60 -
61 - if ( ! empty( $name ) ) {
62 - $slug = $name;
63 - $block_type = new \WP_Block_Type( $slug, array_merge( $metadata, $args ) );
64 - }
56 + $block_type = self::get_path_to_block_metadata( $slug );
57 + $slug = self::get_block_name_from_path_convention( $slug );
65 58 }
66 59
67 60 if (
68 61 isset( $args['version_requirements'] )
@@ -81,13 +74,29 @@
81 74 // This is only useful in Jetpack.
82 75 if ( ! self::is_standalone_block() ) {
83 76 // If the block is dynamic, and a Jetpack block, wrap the render_callback to check availability.
84 77 if ( ! empty( $args['plan_check'] ) ) {
78 + $existing_attributes = array();
79 + $gated_blocks = array(
80 + 'jetpack/calendly',
81 + 'jetpack/donations',
82 + 'jetpack/payment-buttons',
83 + 'jetpack/paypal-payment-buttons',
84 + );
85 + if ( in_array( $slug, $gated_blocks, true ) &&
86 + is_string( $block_type ) &&
87 + file_exists( $block_type )
88 + ) {
89 + $metadata = self::get_block_metadata( $block_type );
90 + $existing_attributes = $metadata['attributes'] ?? array();
91 + }
92 +
85 93 // Set up attributes.
86 94 if ( ! isset( $args['attributes'] ) ) {
87 95 $args['attributes'] = array();
88 96 }
89 97 $args['attributes'] = array_merge(
98 + $existing_attributes,
90 99 $args['attributes'],
91 100 array(
92 101 // Indicates that this block should display an upgrade nudge on the frontend when applicable.
93 102 'shouldDisplayFrontendBanner' => array(
@@ -115,8 +124,13 @@
115 124 // editor style dependency when copying styles to the editor iframe.
116 125 if ( ! isset( $args['editor_style'] ) ) {
117 126 $args['editor_style'] = 'jetpack-blocks-editor';
118 127 }
128 +
129 + // Keep track of the JS loading strategy for any block that specifies it.
130 + if ( isset( $args['js_loading_strategy'] ) ) {
131 + Jetpack_Gutenberg::set_block_js_loading_strategy( $feature_name, $args['js_loading_strategy'] );
132 + }
119 133 }
120 134
121 135 return register_block_type( $block_type, $args );
122 136 }
@@ -141,9 +155,9 @@
141 155 $metadata = self::get_block_metadata_from_file( self::get_path_to_block_metadata( $path ) );
142 156 }
143 157 }
144 158
145 - return isset( $metadata ) ? $metadata : array();
159 + return $metadata ?? array();
146 160 }
147 161
148 162 /**
149 163 * Read block metadata from a block.json file.
@@ -181,8 +195,44 @@
181 195 return self::get_block_name_from_metadata( $metadata );
182 196 }
183 197
184 198 /**
199 + * Get the block name from the path convention.
200 + * For example, path "./extensions/blocks/pinterest" is assumed to define
201 + * a block named "jetpack/pinterest" without checking any files.
202 + *
203 + * Any exceptions should be added to the $breaks_convention array.
204 + * For example, blocks/premium-content defines "premium-content\/container", not "jetpack/premium-content".
205 + * These paths will use the code that checks the disk for the block name.
206 + *
207 + * The unit test test_get_block_name_from_path_convention_matches_get_block_name() verifies that
208 + * all names are correctly guessed.
209 + *
210 + * Run with `./vendor/bin/phpunit --filter WP_Test_Jetpack_Gutenberg`.
211 + *
212 + * @param string $path The path to extract the block name from.
213 + *
214 + * @return string The block name with 'jetpack/' prefix.
215 + */
216 + public static function get_block_name_from_path_convention( $path ) {
217 + $path_parts = explode( '/', $path );
218 + if ( count( $path_parts ) <= 0 ) {
219 + $block_type = self::get_path_to_block_metadata( $path );
220 + return self::get_block_name( $block_type );
221 + }
222 +
223 + $last_part = $path_parts[ count( $path_parts ) - 1 ];
224 + $breaks_convention = array( 'premium-content' );
225 +
226 + if ( in_array( $last_part, $breaks_convention, true ) ) {
227 + $block_type = self::get_path_to_block_metadata( $path );
228 + return self::get_block_name( $block_type );
229 + }
230 +
231 + return 'jetpack/' . $last_part;
232 + }
233 +
234 + /**
185 235 * Get the block name from the its metadata.
186 236 *
187 237 * @param array $metadata The block metadata.
188 238 *
@@ -238,9 +288,9 @@
238 288 *
239 289 * @return string The unprefixed extension name.
240 290 */
241 291 public static function remove_extension_prefix( $extension_name ) {
242 - if ( 0 === strpos( $extension_name, 'jetpack/' ) || 0 === strpos( $extension_name, 'jetpack-' ) ) {
292 + if ( str_starts_with( $extension_name, 'jetpack/' ) || str_starts_with( $extension_name, 'jetpack-' ) ) {
243 293 return substr( $extension_name, strlen( 'jetpack/' ) );
244 294 }
245 295 return $extension_name;
246 296 }
@@ -288,8 +338,9 @@
288 338 if (
289 339 ! $version_available
290 340 && ! self::is_standalone_block() // This is only useful in Jetpack.
291 341 ) {
342 + $slug = Jetpack_Gutenberg::remove_extension_prefix( $slug );
292 343 Jetpack_Gutenberg::set_extension_unavailable(
293 344 $slug,
294 345 'incorrect_gutenberg_version',
295 346 array(
@@ -419,10 +470,112 @@
419 470 * @return string The path to the directory.
420 471 */
421 472 public static function get_path_to_block_metadata( $block_src_dir, $package_dist_dir = '' ) {
422 473 $dir = basename( $block_src_dir );
423 - $dist_path = empty( $package_dist_dir ) ? dirname( Jetpack_Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ) . '/_inc/blocks' : $package_dist_dir;
424 - $result = realpath( "$dist_path/$dir" );
474 + $dist_path = $package_dist_dir;
425 475
476 + if ( empty( $dist_path ) ) {
477 + $plugin_file = Jetpack_Constants::get_constant( 'JETPACK__PLUGIN_FILE' );
478 + // Guard against strange situations where JETPACK__PLUGIN_FILE is undefined.
479 + if ( empty( $plugin_file ) ) {
480 + return $block_src_dir;
481 + }
482 +
483 + $dist_path = dirname( $plugin_file ) . '/_inc/blocks';
484 + }
485 +
486 + $result = realpath( "$dist_path/$dir" );
487 +
426 488 return false === $result ? $block_src_dir : $result;
489 + }
490 +
491 + /**
492 + * Determine whether a site should use the default set of blocks, or a custom set.
493 + * Possible variations are currently beta, experimental, and production.
494 + *
495 + * @since 3.1.0
496 + *
497 + * @return string $block_variation production|beta|experimental
498 + */
499 + public static function get_variation() {
500 + // Default to production blocks.
501 + $block_variation = 'production';
502 +
503 + /*
504 + * Prefer to use this JETPACK_BLOCKS_VARIATION constant
505 + * or the jetpack_blocks_variation filter
506 + * to set the block variation in your code.
507 + */
508 + $default = Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' );
509 + if ( ! empty( $default ) && in_array( $default, array( 'beta', 'experimental', 'production' ), true ) ) {
510 + $block_variation = $default;
511 + }
512 +
513 + /**
514 + * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
515 + *
516 + * @since jetpack-6.9.0
517 + * @deprecated jetpack-11.8.0 Use jetpack_blocks_variation filter instead.
518 + *
519 + * @param boolean
520 + */
521 + $is_beta = apply_filters_deprecated(
522 + 'jetpack_load_beta_blocks',
523 + array( false ),
524 + 'jetpack-11.8.0',
525 + 'jetpack_blocks_variation'
526 + );
527 +
528 + /*
529 + * Switch to beta blocks if you use the JETPACK_BETA_BLOCKS constant
530 + * or the deprecated jetpack_load_beta_blocks filter.
531 + * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
532 + */
533 + if ( empty( $default )
534 + && (
535 + $is_beta
536 + || Constants::is_true( 'JETPACK_BETA_BLOCKS' )
537 + )
538 + ) {
539 + $block_variation = 'beta';
540 + }
541 +
542 + /**
543 + * Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks.
544 + *
545 + * @since jetpack-6.9.0
546 + * @deprecated jetpack-11.8.0 Use jetpack_blocks_variation filter instead.
547 + *
548 + * @param boolean
549 + */
550 + $is_experimental = apply_filters_deprecated(
551 + 'jetpack_load_experimental_blocks',
552 + array( false ),
553 + 'jetpack-11.8.0',
554 + 'jetpack_blocks_variation'
555 + );
556 +
557 + /*
558 + * Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant
559 + * or the deprecated jetpack_load_experimental_blocks filter.
560 + * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
561 + */
562 + if ( empty( $default )
563 + && (
564 + $is_experimental
565 + || Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' )
566 + )
567 + ) {
568 + $block_variation = 'experimental';
569 + }
570 +
571 + /**
572 + * Allow customizing the variation of blocks in use on a site.
573 + * Overwrites any previously set values, whether by constant or filter.
574 + *
575 + * @since jetpack-8.1.0
576 + *
577 + * @param string $block_variation Can be beta, experimental, and production. Defaults to production.
578 + */
579 + return apply_filters( 'jetpack_blocks_variation', $block_variation );
427 580 }
428 581 }