From 0484b25bf9001909f8a8611391955664517fc9f8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:05:14 +0200 Subject: [PATCH 01/20] Add variations field to block.json --- src/wp-includes/blocks.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 3b1fc25d48824..a5f29e7645013 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -385,6 +385,7 @@ function get_block_metadata_i18n_schema() { * @since 6.3.0 Added `selectors` field. * @since 6.4.0 Added support for `blockHooks` field. * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields. + * @since 6.6.0 Added support for `variations` field. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. @@ -522,6 +523,29 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + if ( ! empty( $metadata['variations'] ) ) { + $variations_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['variations'] ) + ) + ); + if ( $variations_path ) { + /** + * Generates the list of block variations. + * + * @since 6.6.0 + * + * @return string Returns the list of block variations. + */ + $settings['render_callback'] = static function () use ( $variations_path ) { + ob_start(); + require $variations_path; + return ob_get_clean(); + }; + } + } + $settings = array_merge( $settings, $args ); $script_fields = array( From d950e4e68bdc03fa9f24e2c5757408858d866404 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:18:56 +0200 Subject: [PATCH 02/20] Reconcile with existing variations argument --- src/wp-includes/blocks.php | 50 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index a5f29e7645013..114adf2a9a945 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -385,7 +385,7 @@ function get_block_metadata_i18n_schema() { * @since 6.3.0 Added `selectors` field. * @since 6.4.0 Added support for `blockHooks` field. * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields. - * @since 6.6.0 Added support for `variations` field. + * @since 6.6.0 Allow PHP filename as `variations` argument. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. @@ -463,6 +463,31 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + // variations can either be an array of block variations, or the name of + // a PHP file that generates that array. + if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) { + $variations_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['variations'] ) + ) + ); + if ( $variations_path ) { + /** + * Generates the list of block variations. + * + * @since 6.6.0 + * + * @return string Returns the list of block variations. + */ + $settings['render_callback'] = static function () use ( $variations_path ) { + ob_start(); + require $variations_path; + return ob_get_clean(); + }; + } + } + $settings = array(); $property_mappings = array( 'apiVersion' => 'api_version', @@ -523,29 +548,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } - if ( ! empty( $metadata['variations'] ) ) { - $variations_path = wp_normalize_path( - realpath( - dirname( $metadata['file'] ) . '/' . - remove_block_asset_path_prefix( $metadata['variations'] ) - ) - ); - if ( $variations_path ) { - /** - * Generates the list of block variations. - * - * @since 6.6.0 - * - * @return string Returns the list of block variations. - */ - $settings['render_callback'] = static function () use ( $variations_path ) { - ob_start(); - require $variations_path; - return ob_get_clean(); - }; - } - } - $settings = array_merge( $settings, $args ); $script_fields = array( From d5172fe97a3e7747c2fb22b88b501a28ca249276 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:28:41 +0200 Subject: [PATCH 03/20] Update example to use variations.php file --- tests/phpunit/data/blocks/notice/block.json | 9 +-------- tests/phpunit/data/blocks/notice/variations.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 tests/phpunit/data/blocks/notice/variations.php diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index 7ccaef2d1312d..fe34de80a8e4d 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -52,14 +52,7 @@ "label": "Other" } ], - "variations": [ - { - "name": "error", - "title": "Error", - "description": "Shows error.", - "keywords": [ "failure" ] - } - ], + "variations": "variations.php", "example": { "attributes": { "message": "This is a notice!" diff --git a/tests/phpunit/data/blocks/notice/variations.php b/tests/phpunit/data/blocks/notice/variations.php new file mode 100644 index 0000000000000..3be98133431a7 --- /dev/null +++ b/tests/phpunit/data/blocks/notice/variations.php @@ -0,0 +1,10 @@ + 'error', + 'title' => 'Error', + 'description' => 'Shows error.', + 'keywords' => array( 'failure' ) + ) +); From 39c0aaa540792a6a83765f117424f96adde37698 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:37:37 +0200 Subject: [PATCH 04/20] Set variations_callback, not render_callback --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 114adf2a9a945..fea538c689d4b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -480,7 +480,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * * @return string Returns the list of block variations. */ - $settings['render_callback'] = static function () use ( $variations_path ) { + $settings['variations_callback'] = static function () use ( $variations_path ) { ob_start(); require $variations_path; return ob_get_clean(); From d645c4abecbcdaa1a6786f701eba1343b1a2341b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:38:15 +0200 Subject: [PATCH 05/20] Set metadata rather than settings (for i18n) --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index fea538c689d4b..c1cd680c011dc 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -480,7 +480,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * * @return string Returns the list of block variations. */ - $settings['variations_callback'] = static function () use ( $variations_path ) { + $metadata['variations_callback'] = static function () use ( $variations_path ) { ob_start(); require $variations_path; return ob_get_clean(); From 8cbe61a4d73969102bf723a1399e67979df11519 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:39:08 +0200 Subject: [PATCH 06/20] Use return value --- src/wp-includes/blocks.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index c1cd680c011dc..48e7db28e490d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -473,18 +473,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { ) ); if ( $variations_path ) { - /** - * Generates the list of block variations. - * - * @since 6.6.0 - * - * @return string Returns the list of block variations. - */ - $metadata['variations_callback'] = static function () use ( $variations_path ) { - ob_start(); - require $variations_path; - return ob_get_clean(); - }; + $metadata['variations_callback'] = require $variations_path; } } From 53a03d5cb2b6cf3027b9081e2621de316c6f3628 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:49:57 +0200 Subject: [PATCH 07/20] Revert "Use return value" This reverts commit b7b928d9d66de6cbeb151d03523135cc9173311b. --- src/wp-includes/blocks.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 48e7db28e490d..c1cd680c011dc 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -473,7 +473,18 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { ) ); if ( $variations_path ) { - $metadata['variations_callback'] = require $variations_path; + /** + * Generates the list of block variations. + * + * @since 6.6.0 + * + * @return string Returns the list of block variations. + */ + $metadata['variations_callback'] = static function () use ( $variations_path ) { + ob_start(); + require $variations_path; + return ob_get_clean(); + }; } } From ecc79de303d466222b31185d16ed5dcc5fd4bfbc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:51:02 +0200 Subject: [PATCH 08/20] Use return value correctly --- src/wp-includes/blocks.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index c1cd680c011dc..94b669c60f08d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -481,9 +481,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * @return string Returns the list of block variations. */ $metadata['variations_callback'] = static function () use ( $variations_path ) { - ob_start(); - require $variations_path; - return ob_get_clean(); + $variations = require $variations_path; + return $variations; }; } } From 82abaf1bc23c32b350d8005315c2d12d1b0cae8d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 12:52:06 +0200 Subject: [PATCH 09/20] Add assertion --- tests/phpunit/tests/blocks/register.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1dbc688bb16cf..550333cf2dc8e 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -1102,6 +1102,8 @@ public function test_block_registers_with_metadata_fixture() { // @ticket 53148 $this->assertIsCallable( $result->render_callback ); + // @ticket 61280 + $this->assertIsCallable( $result->variations_callback ); } /** From 60fe05ef91bd77788026e2f5121d049959e730d2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 13:01:55 +0200 Subject: [PATCH 10/20] Singular --- src/wp-includes/blocks.php | 2 +- tests/phpunit/tests/blocks/register.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 94b669c60f08d..d5f4b23f68b6a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -480,7 +480,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * * @return string Returns the list of block variations. */ - $metadata['variations_callback'] = static function () use ( $variations_path ) { + $metadata['variation_callback'] = static function () use ( $variations_path ) { $variations = require $variations_path; return $variations; }; diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 550333cf2dc8e..2b434475cd954 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -1103,7 +1103,7 @@ public function test_block_registers_with_metadata_fixture() { // @ticket 53148 $this->assertIsCallable( $result->render_callback ); // @ticket 61280 - $this->assertIsCallable( $result->variations_callback ); + $this->assertIsCallable( $result->variation_callback ); } /** From fc249aaf60aa29e3617f2e4b337af2ee63d7e681 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 May 2024 13:08:14 +0200 Subject: [PATCH 11/20] Move code back down --- src/wp-includes/blocks.php | 48 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index d5f4b23f68b6a..154c0f9b4e7c3 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -463,30 +463,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } - // variations can either be an array of block variations, or the name of - // a PHP file that generates that array. - if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) { - $variations_path = wp_normalize_path( - realpath( - dirname( $metadata['file'] ) . '/' . - remove_block_asset_path_prefix( $metadata['variations'] ) - ) - ); - if ( $variations_path ) { - /** - * Generates the list of block variations. - * - * @since 6.6.0 - * - * @return string Returns the list of block variations. - */ - $metadata['variation_callback'] = static function () use ( $variations_path ) { - $variations = require $variations_path; - return $variations; - }; - } - } - $settings = array(); $property_mappings = array( 'apiVersion' => 'api_version', @@ -547,6 +523,30 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + // If `variations` is a string, it's the name of a PHP file that + // generates the variations. + if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) { + $variations_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['variations'] ) + ) + ); + if ( $variations_path ) { + /** + * Generates the list of block variations. + * + * @since 6.6.0 + * + * @return string Returns the list of block variations. + */ + $settings['variation_callback'] = static function () use ( $variations_path ) { + $variations = require $variations_path; + return $variations; + }; + } + } + $settings = array_merge( $settings, $args ); $script_fields = array( From d5245b829aff053782e8d0dc5ec1f0458c4ea6b5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 6 Jun 2024 11:19:55 +0200 Subject: [PATCH 12/20] Revert "Update example to use variations.php file" This reverts commit 794876d7a45ef364a31fea94c8506ff930a05fc1. --- tests/phpunit/data/blocks/notice/block.json | 9 ++++++++- tests/phpunit/data/blocks/notice/variations.php | 10 ---------- 2 files changed, 8 insertions(+), 11 deletions(-) delete mode 100644 tests/phpunit/data/blocks/notice/variations.php diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index fe34de80a8e4d..7ccaef2d1312d 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -52,7 +52,14 @@ "label": "Other" } ], - "variations": "variations.php", + "variations": [ + { + "name": "error", + "title": "Error", + "description": "Shows error.", + "keywords": [ "failure" ] + } + ], "example": { "attributes": { "message": "This is a notice!" diff --git a/tests/phpunit/data/blocks/notice/variations.php b/tests/phpunit/data/blocks/notice/variations.php deleted file mode 100644 index 3be98133431a7..0000000000000 --- a/tests/phpunit/data/blocks/notice/variations.php +++ /dev/null @@ -1,10 +0,0 @@ - 'error', - 'title' => 'Error', - 'description' => 'Shows error.', - 'keywords' => array( 'failure' ) - ) -); From b9ae2442e17c012a928036c1e0039f0dd2abacfe Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 6 Jun 2024 11:28:18 +0200 Subject: [PATCH 13/20] Remove now-obsolete assertion --- tests/phpunit/tests/blocks/register.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 2b434475cd954..1dbc688bb16cf 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -1102,8 +1102,6 @@ public function test_block_registers_with_metadata_fixture() { // @ticket 53148 $this->assertIsCallable( $result->render_callback ); - // @ticket 61280 - $this->assertIsCallable( $result->variation_callback ); } /** From 6754fe0918e914a2e46cf9e44d8ca443d699c6a4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 6 Jun 2024 11:20:31 +0200 Subject: [PATCH 14/20] Add test coverage --- .../phpunit/data/blocks/notice/variations.php | 10 ++++++ tests/phpunit/tests/blocks/register.php | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/phpunit/data/blocks/notice/variations.php diff --git a/tests/phpunit/data/blocks/notice/variations.php b/tests/phpunit/data/blocks/notice/variations.php new file mode 100644 index 0000000000000..59a62c7244108 --- /dev/null +++ b/tests/phpunit/data/blocks/notice/variations.php @@ -0,0 +1,10 @@ + 'warning', + 'title' => 'warning', + 'description' => 'Shows warning.', + 'keywords' => array( 'warning' ) + ) +); diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1dbc688bb16cf..9a7f811127a92 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -957,6 +957,39 @@ public function data_register_block_registers_with_args_override_returns_false_w ); } + /** + * Tests registering a block with variations from a PHP file. + * + * @ticket 61280 + * + * @covers ::register_block_type_from_metadata + */ + public function test_register_block_type_from_metadata_with_variations_php_file() { + $result = register_block_type_from_metadata( + DIR_TESTDATA . '/blocks/notice', + array( + 'api_version' => 2, + 'name' => 'tests/notice-with-variations-php', + 'title' => 'Notice with variations from a PHP file', + 'variations' => 'variations.php' + ) + ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 2, $result->api_version, 'The API version is incorrect' ); + $this->assertSame( 'tests/notice-with-variations-php', $result->name, 'The block name is incorrect' ); + $this->assertSame( 'Notice with variations from a PHP file', $result->title, 'The block title is incorrect' ); + $this->assertSame( 'variations.php', $result->variations, 'The block variations are incorrect' ); + $this->assertIsCallable( $result->variation_callback, 'The variation callback hasn\'t been set' ); + + $expected_variations = require DIR_TESTDATA . '/blocks/notice/variations.php'; + $this->assertSame( + $expected_variations, + call_user_func( $result->variation_callback ), + 'The variation callback hasn\'t been set correctly' + ); + } + /** * Tests that the function returns the registered block when the `block.json` * is found in the fixtures directory. From b66e0aabd1ff095b0f2a0c57f7941e2cb479fbc6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 11 Jun 2024 14:40:41 +0200 Subject: [PATCH 15/20] Coding Standards --- tests/phpunit/tests/blocks/register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 9a7f811127a92..bca4163f0c07e 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -971,7 +971,7 @@ public function test_register_block_type_from_metadata_with_variations_php_file( 'api_version' => 2, 'name' => 'tests/notice-with-variations-php', 'title' => 'Notice with variations from a PHP file', - 'variations' => 'variations.php' + 'variations' => 'variations.php', ) ); From be3b1d5a7a2617530ff1789cf2bcb6855f6bc951 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 11 Jun 2024 14:41:54 +0200 Subject: [PATCH 16/20] Bump @since to 6.7.0 --- 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 154c0f9b4e7c3..7323e75d8ef9a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -385,7 +385,7 @@ function get_block_metadata_i18n_schema() { * @since 6.3.0 Added `selectors` field. * @since 6.4.0 Added support for `blockHooks` field. * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields. - * @since 6.6.0 Allow PHP filename as `variations` argument. + * @since 6.7.0 Allow PHP filename as `variations` argument. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. @@ -536,7 +536,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { /** * Generates the list of block variations. * - * @since 6.6.0 + * @since 6.7.0 * * @return string Returns the list of block variations. */ From dc2494c204f2c5a6128de97fd4dd8a6ba51179d3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 11 Jun 2024 14:53:51 +0200 Subject: [PATCH 17/20] Fix test --- tests/phpunit/tests/blocks/register.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index bca4163f0c07e..4e34c750304f3 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -965,20 +965,18 @@ public function data_register_block_registers_with_args_override_returns_false_w * @covers ::register_block_type_from_metadata */ public function test_register_block_type_from_metadata_with_variations_php_file() { + $filter_metadata_registration = static function ( $metadata ) { + $metadata['variations'] = 'variations.php'; + return $metadata; + }; + + add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 ); $result = register_block_type_from_metadata( - DIR_TESTDATA . '/blocks/notice', - array( - 'api_version' => 2, - 'name' => 'tests/notice-with-variations-php', - 'title' => 'Notice with variations from a PHP file', - 'variations' => 'variations.php', - ) + DIR_TESTDATA . '/blocks/notice' ); + remove_filter( 'block_type_metadata', $filter_metadata_registration ); $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); - $this->assertSame( 2, $result->api_version, 'The API version is incorrect' ); - $this->assertSame( 'tests/notice-with-variations-php', $result->name, 'The block name is incorrect' ); - $this->assertSame( 'Notice with variations from a PHP file', $result->title, 'The block title is incorrect' ); $this->assertSame( 'variations.php', $result->variations, 'The block variations are incorrect' ); $this->assertIsCallable( $result->variation_callback, 'The variation callback hasn\'t been set' ); From 283b826c31c55eb14d27de19d78dae93e2e6f690 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jun 2024 11:44:34 +0200 Subject: [PATCH 18/20] Unset block instance's variations field --- src/wp-includes/blocks.php | 4 ++++ tests/phpunit/tests/blocks/register.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 7323e75d8ef9a..9b24b989d4024 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -544,6 +544,10 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $variations = require $variations_path; return $variations; }; + // The block instance's `variations` field is only allowed to be an array + // (of known block variations). We unset it so that the block instance will + // provide a getter that returns the result of the `variation_callback` instead. + unset( $settings['variations'] ); } } diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 4e34c750304f3..a78ac440f6478 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -977,15 +977,15 @@ public function test_register_block_type_from_metadata_with_variations_php_file( remove_filter( 'block_type_metadata', $filter_metadata_registration ); $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); - $this->assertSame( 'variations.php', $result->variations, 'The block variations are incorrect' ); - $this->assertIsCallable( $result->variation_callback, 'The variation callback hasn\'t been set' ); + $this->assertIsCallable( $result->variation_callback, 'The variation callback hasn\'t been set' ); $expected_variations = require DIR_TESTDATA . '/blocks/notice/variations.php'; $this->assertSame( $expected_variations, call_user_func( $result->variation_callback ), 'The variation callback hasn\'t been set correctly' ); + $this->assertSame( $expected_variations, $result->variations, 'The block variations are incorrect' ); } /** From 8c20b4e74a42dc9347ce2893bc5adad6b5c430a7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jun 2024 14:38:12 +0200 Subject: [PATCH 19/20] Coding Standards --- tests/phpunit/data/blocks/notice/variations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/data/blocks/notice/variations.php b/tests/phpunit/data/blocks/notice/variations.php index 59a62c7244108..bed66d9544176 100644 --- a/tests/phpunit/data/blocks/notice/variations.php +++ b/tests/phpunit/data/blocks/notice/variations.php @@ -5,6 +5,6 @@ 'name' => 'warning', 'title' => 'warning', 'description' => 'Shows warning.', - 'keywords' => array( 'warning' ) - ) + 'keywords' => array( 'warning' ), + ), ); From e7128b3d9bb09bb6505358821981452c48ba1e3e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 18 Jun 2024 16:24:56 +0200 Subject: [PATCH 20/20] Use 'file:' prefix --- tests/phpunit/tests/blocks/register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index a78ac440f6478..7e0c391e1f226 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -966,7 +966,7 @@ public function data_register_block_registers_with_args_override_returns_false_w */ public function test_register_block_type_from_metadata_with_variations_php_file() { $filter_metadata_registration = static function ( $metadata ) { - $metadata['variations'] = 'variations.php'; + $metadata['variations'] = 'file:./variations.php'; return $metadata; };