From f3ef3d50db1e1f5291d52857aafb52aa7bc3392d Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Wed, 15 May 2024 16:30:19 +0100 Subject: [PATCH 1/9] Render variations --- src/wp-includes/class-wp-block-parser.php | 17 +++++++++-------- src/wp-includes/class-wp-block.php | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-block-parser.php b/src/wp-includes/class-wp-block-parser.php index 543f53691ccb1..8590bd9d70d44 100644 --- a/src/wp-includes/class-wp-block-parser.php +++ b/src/wp-includes/class-wp-block-parser.php @@ -245,7 +245,7 @@ public function next_token() { * match back in PHP to see which one it was. */ $has_match = preg_match( - '/).)*+)?}\s+)?(?P\/)?-->/s', + '/).)*+)?}\s+)?(?P\/)?-->/s', $this->document, $matches, PREG_OFFSET_CAPTURE, @@ -264,13 +264,14 @@ public function next_token() { list( $match, $started_at ) = $matches[0]; - $length = strlen( $match ); - $is_closer = isset( $matches['closer'] ) && -1 !== $matches['closer'][1]; - $is_void = isset( $matches['void'] ) && -1 !== $matches['void'][1]; - $namespace = $matches['namespace']; - $namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/'; - $name = $namespace . $matches['name'][0]; - $has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1]; + $length = strlen( $match ); + $is_closer = isset( $matches['closer'] ) && -1 !== $matches['closer'][1]; + $is_void = isset( $matches['void'] ) && -1 !== $matches['void'][1]; + $is_variation = isset( $matches['variation'] ) && -1 !== $matches['variation'][1]; + $namespace = $matches['namespace']; + $namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/'; + $name = $is_variation ? $namespace . $matches['name'][0] . '/' . $matches['variation'][0] : $namespace . $matches['name'][0]; + $has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1]; /* * Fun fact! It's not trivial in PHP to create "an empty associative array" since all arrays diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 4885b9921900c..bac0fdd7447ed 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -136,7 +136,8 @@ public function __construct( $block, $available_context = array(), $registry = n $this->registry = $registry; - $this->block_type = $registry->get_registered( $this->name ); + $block_name = $this->get_canonical_block_name( $this->name ); + $this->block_type = $registry->get_registered( $block_name ); $this->available_context = $available_context; @@ -171,6 +172,19 @@ public function __construct( $block, $available_context = array(), $registry = n } } + public function is_variant( $name ) { + return substr_count( $this->name, '/' ) === 2; + } + + public function get_canonical_block_name( $name ) { + if ( $this->is_variant( $name ) ) { + $parts = explode( '/', $name ); + return $parts[0] . '/' . $parts[1]; + } + + return $name; + } + /** * Returns a value from an inaccessible property. * From 24bbcbda89762b38fa13d8230c3abbafb0ec272b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 16 May 2024 12:48:52 +0200 Subject: [PATCH 2/9] Use function argument rather than class member --- src/wp-includes/class-wp-block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index bac0fdd7447ed..960bee92602e9 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -173,7 +173,7 @@ public function __construct( $block, $available_context = array(), $registry = n } public function is_variant( $name ) { - return substr_count( $this->name, '/' ) === 2; + return substr_count( $name, '/' ) === 2; } public function get_canonical_block_name( $name ) { From 358e03c62130b3e65f8e73dd062c484a71bdf84a Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 17 May 2024 11:13:34 +0100 Subject: [PATCH 3/9] Provide utility functions for checking if a block is a variation and getting the canonical block name --- src/wp-includes/blocks.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 986af9a865b7c..d066e9ca8411b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2356,3 +2356,36 @@ function _wp_footnotes_force_filtered_html_on_import_filter( $arg ) { } return $arg; } + +/** + * Checks if the block name is a variation of a block. Variations follow a specific format + * of `namespace/block-name/variation-name`. + * + * @access private + * @since 6.6.0 + * + * @param string $block_name + * @return boolean + */ +function block_is_variation( $block_name ) { + return substr_count( $block_name, '/' ) === 2; +} + +/** + * Returns the canonical block name for a block. If the block is a variation, it will return + * the block name without the variation name. + * + * @access private + * @since 6.6.0 + * + * @param string $block_name + * @return string + */ +function get_canonical_block_name( $block_name ) { + if ( block_is_variation( $block_name ) ) { + $parts = explode( '/', $block_name ); + return $parts[0] . '/' . $parts[1]; + } + + return $block_name; +} From 480b3544ca2345b2bafd2b812a56adc83e489d3c Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 17 May 2024 11:13:49 +0100 Subject: [PATCH 4/9] Get the instance of the canonical block --- src/wp-includes/class-wp-block-supports.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index c90b5e0c54c59..3943e608f1a63 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -95,7 +95,7 @@ public function register( $block_support_name, $block_support_config ) { */ public function apply_block_supports() { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( - self::$block_to_render['blockName'] + get_canonical_block_name( self::$block_to_render['blockName'] ) ); // If no render_callback, assume styles have been previously handled. From 52d6b3d926ed8fc2af6eba4c79a4fecd6aa32767 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 17 May 2024 11:14:04 +0100 Subject: [PATCH 5/9] Set WP_Block $name to be the canonical block name --- src/wp-includes/class-wp-block.php | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 960bee92602e9..438f8af0b8424 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -128,7 +128,7 @@ class WP_Block { */ public function __construct( $block, $available_context = array(), $registry = null ) { $this->parsed_block = $block; - $this->name = $block['blockName']; + $this->name = get_canonical_block_name( $block['blockName'] ); if ( is_null( $registry ) ) { $registry = WP_Block_Type_Registry::get_instance(); @@ -136,8 +136,7 @@ public function __construct( $block, $available_context = array(), $registry = n $this->registry = $registry; - $block_name = $this->get_canonical_block_name( $this->name ); - $this->block_type = $registry->get_registered( $block_name ); + $this->block_type = $registry->get_registered( $this->name ); $this->available_context = $available_context; @@ -172,19 +171,6 @@ public function __construct( $block, $available_context = array(), $registry = n } } - public function is_variant( $name ) { - return substr_count( $name, '/' ) === 2; - } - - public function get_canonical_block_name( $name ) { - if ( $this->is_variant( $name ) ) { - $parts = explode( '/', $name ); - return $parts[0] . '/' . $parts[1]; - } - - return $name; - } - /** * Returns a value from an inaccessible property. * From d7735f8b2bc67bce277f1288bbf7856959cc9851 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 May 2024 13:17:31 +0200 Subject: [PATCH 6/9] Infer block variation name from attributes --- .../block-supports/generated-classname.php | 7 ++++++- src/wp-includes/blocks.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-supports/generated-classname.php b/src/wp-includes/block-supports/generated-classname.php index bbedd0ee53542..40685f3d6e485 100644 --- a/src/wp-includes/block-supports/generated-classname.php +++ b/src/wp-includes/block-supports/generated-classname.php @@ -48,7 +48,7 @@ function wp_get_block_default_classname( $block_name ) { * @param WP_Block_Type $block_type Block Type. * @return array Block CSS classes and inline styles. */ -function wp_apply_generated_classname_support( $block_type ) { +function wp_apply_generated_classname_support( $block_type, $block_attributes ) { $attributes = array(); $has_generated_classname_support = block_has_support( $block_type, 'className', true ); if ( $has_generated_classname_support ) { @@ -57,6 +57,11 @@ function wp_apply_generated_classname_support( $block_type ) { if ( $block_classname ) { $attributes['class'] = $block_classname; } + + $variation = infer_block_variation( $block_type, $block_attributes ); + if ( $variation ) { + $attributes['class'] .= ' ' . $block_classname . '-' . $variation; + } } return $attributes; diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index d066e9ca8411b..df4c6cc7e51ce 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2389,3 +2389,15 @@ function get_canonical_block_name( $block_name ) { return $block_name; } + +function infer_block_variation( $block_type, $attributes ) { + $variations = $block_type->get_variations(); + foreach ( $variations as $variation ) { + foreach ( $variation['attributes'] as $attribute => $value ) { + if ( ! isset( $attributes[ $attribute ] ) || $attributes[ $attribute ] !== $value ) { + continue 2; + } + } + return $variation['name']; + } +} From e9c03e64e0efcc98440bea7083e8f2780571c4bf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 May 2024 15:45:05 +0200 Subject: [PATCH 7/9] Use wp_get_block_default_classname function --- src/wp-includes/block-supports/generated-classname.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-supports/generated-classname.php b/src/wp-includes/block-supports/generated-classname.php index 40685f3d6e485..dd6093e587c06 100644 --- a/src/wp-includes/block-supports/generated-classname.php +++ b/src/wp-includes/block-supports/generated-classname.php @@ -60,7 +60,7 @@ function wp_apply_generated_classname_support( $block_type, $block_attributes ) $variation = infer_block_variation( $block_type, $block_attributes ); if ( $variation ) { - $attributes['class'] .= ' ' . $block_classname . '-' . $variation; + $attributes['class'] .= ' ' . wp_get_block_default_classname( $block_type->name . '/' . $variation ); } } From c1eb57a051d8786d8390cd0e02e67c20cea601f2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 May 2024 15:50:54 +0200 Subject: [PATCH 8/9] Rename argument --- src/wp-includes/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index df4c6cc7e51ce..7c3d8b1072541 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2390,11 +2390,11 @@ function get_canonical_block_name( $block_name ) { return $block_name; } -function infer_block_variation( $block_type, $attributes ) { +function infer_block_variation( $block_type, $block_attributes ) { $variations = $block_type->get_variations(); foreach ( $variations as $variation ) { foreach ( $variation['attributes'] as $attribute => $value ) { - if ( ! isset( $attributes[ $attribute ] ) || $attributes[ $attribute ] !== $value ) { + if ( ! isset( $block_attributes[ $attribute ] ) || $block_attributes[ $attribute ] !== $value ) { continue 2; } } From 320643130a60f6dd53b31a17c0facb515c81cba1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 May 2024 15:59:57 +0200 Subject: [PATCH 9/9] Respect isActive --- src/wp-includes/blocks.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 7c3d8b1072541..133a473bd5a92 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2393,7 +2393,11 @@ function get_canonical_block_name( $block_name ) { function infer_block_variation( $block_type, $block_attributes ) { $variations = $block_type->get_variations(); foreach ( $variations as $variation ) { - foreach ( $variation['attributes'] as $attribute => $value ) { + $attributes = $variation['attributes']; + if ( isset( $variation['isActive'] ) && is_array( $variation['isActive'] ) ) { + $attributes = array_intersect_key( $attributes, array_flip( $variation['isActive'] ) ); + } + foreach ( $attributes as $attribute => $value ) { if ( ! isset( $block_attributes[ $attribute ] ) || $block_attributes[ $attribute ] !== $value ) { continue 2; }