← All changes
|
jetpack_vendor/automattic/jetpack-blocks/src/class-blocks.php
+165
-5
13.1.5
→
16.3-beta
View file →
| @@ -37,9 +37,9 @@ | ||
| 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. |
| @@ -53,9 +53,9 @@ | ||
| 53 | 53 | // If a path is passed, make sure to get the block.json file from the build directory and get |
| 54 | 54 | // the block name from that file. |
| 55 | 55 | if ( path_is_absolute( $slug ) ) { |
| 56 | 56 | $block_type = self::get_path_to_block_metadata( $slug ); |
| 57 | - $slug = self::get_block_name( $block_type ); | |
| 57 | + $slug = self::get_block_name_from_path_convention( $slug ); | |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | if ( |
| 61 | 61 | isset( $args['version_requirements'] ) |
| @@ -74,13 +74,29 @@ | ||
| 74 | 74 | // This is only useful in Jetpack. |
| 75 | 75 | if ( ! self::is_standalone_block() ) { |
| 76 | 76 | // If the block is dynamic, and a Jetpack block, wrap the render_callback to check availability. |
| 77 | 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 | + | |
| 78 | 93 | // Set up attributes. |
| 79 | 94 | if ( ! isset( $args['attributes'] ) ) { |
| 80 | 95 | $args['attributes'] = array(); |
| 81 | 96 | } |
| 82 | 97 | $args['attributes'] = array_merge( |
| 98 | + $existing_attributes, | |
| 83 | 99 | $args['attributes'], |
| 84 | 100 | array( |
| 85 | 101 | // Indicates that this block should display an upgrade nudge on the frontend when applicable. |
| 86 | 102 | 'shouldDisplayFrontendBanner' => array( |
| @@ -108,8 +124,13 @@ | ||
| 108 | 124 | // editor style dependency when copying styles to the editor iframe. |
| 109 | 125 | if ( ! isset( $args['editor_style'] ) ) { |
| 110 | 126 | $args['editor_style'] = 'jetpack-blocks-editor'; |
| 111 | 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 | + } | |
| 112 | 133 | } |
| 113 | 134 | |
| 114 | 135 | return register_block_type( $block_type, $args ); |
| 115 | 136 | } |
| @@ -134,9 +155,9 @@ | ||
| 134 | 155 | $metadata = self::get_block_metadata_from_file( self::get_path_to_block_metadata( $path ) ); |
| 135 | 156 | } |
| 136 | 157 | } |
| 137 | 158 | |
| 138 | - return isset( $metadata ) ? $metadata : array(); | |
| 159 | + return $metadata ?? array(); | |
| 139 | 160 | } |
| 140 | 161 | |
| 141 | 162 | /** |
| 142 | 163 | * Read block metadata from a block.json file. |
| @@ -174,8 +195,44 @@ | ||
| 174 | 195 | return self::get_block_name_from_metadata( $metadata ); |
| 175 | 196 | } |
| 176 | 197 | |
| 177 | 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 | + /** | |
| 178 | 235 | * Get the block name from the its metadata. |
| 179 | 236 | * |
| 180 | 237 | * @param array $metadata The block metadata. |
| 181 | 238 | * |
| @@ -281,8 +338,9 @@ | ||
| 281 | 338 | if ( |
| 282 | 339 | ! $version_available |
| 283 | 340 | && ! self::is_standalone_block() // This is only useful in Jetpack. |
| 284 | 341 | ) { |
| 342 | + $slug = Jetpack_Gutenberg::remove_extension_prefix( $slug ); | |
| 285 | 343 | Jetpack_Gutenberg::set_extension_unavailable( |
| 286 | 344 | $slug, |
| 287 | 345 | 'incorrect_gutenberg_version', |
| 288 | 346 | array( |
| @@ -412,10 +470,112 @@ | ||
| 412 | 470 | * @return string The path to the directory. |
| 413 | 471 | */ |
| 414 | 472 | public static function get_path_to_block_metadata( $block_src_dir, $package_dist_dir = '' ) { |
| 415 | 473 | $dir = basename( $block_src_dir ); |
| 416 | - $dist_path = empty( $package_dist_dir ) ? dirname( Jetpack_Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ) . '/_inc/blocks' : $package_dist_dir; | |
| 417 | - $result = realpath( "$dist_path/$dir" ); | |
| 474 | + $dist_path = $package_dist_dir; | |
| 418 | 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 | + | |
| 419 | 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 ); | |
| 420 | 580 | } |
| 421 | 581 | } |