diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index a9c1739c79b5c..c4d2c7e6d7c26 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' ); } /**