diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 000ded53e6..1b5427340f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -201,6 +201,15 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } $metadata['file'] = $metadata_file; + /** + * Filters the metadata provided for registering a block type. + * + * @since 5.7.0 + * + * @param array $metadata Metadata for registering a block type. + */ + $metadata = apply_filters( 'block_type_metadata', $metadata ); + $settings = array(); $property_mappings = array( 'title' => 'title', @@ -252,12 +261,26 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { ); } - return register_block_type( - $metadata['name'], + /** + * Filters the settings determined from the block type metadata. + * + * @since 5.7.0 + * + * @param array $settings Array of determined settings for registering a block type. + * @param array $metadata Metadata provided for registering a block type. + */ + $settings = apply_filters( + 'block_type_metadata_settings', array_merge( $settings, $args - ) + ), + $metadata + ); + + return register_block_type( + $metadata['name'], + $settings ); } diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index e9e607d89a..7cdc37d5e9 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -62,9 +62,7 @@ class WP_Test_Block_Register extends WP_UnitTestCase { $registry = WP_Block_Type_Registry::get_instance(); - foreach ( array( 'test-static', 'test-dynamic' ) as $block_name ) { - $block_name = 'core/' . $block_name; - + foreach ( array( 'core/test-static', 'core/test-dynamic', 'my-plugin/notice' ) as $block_name ) { if ( $registry->is_registered( $block_name ) ) { $registry->unregister( $block_name ); } @@ -423,4 +421,40 @@ class WP_Test_Block_Register extends WP_UnitTestCase { $block_type = $registry->get_registered( 'core/test-filtered' ); $this->assertSame( 'boolean', $block_type->attributes['core/test-filtered']['type'] ); } + + /** + * @ticket 52138 + */ + public function test_filter_block_registration_metadata() { + $filter_metadata_registration = function( $metadata ) { + $metadata['apiVersion'] = 3; + return $metadata; + }; + + add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 ); + $result = register_block_type_from_metadata( + __DIR__ . '/fixtures' + ); + remove_filter( 'block_type_metadata', $filter_metadata_registration ); + + $this->assertSame( 3, $result->api_version ); + } + + /** + * @ticket 52138 + */ + public function test_filter_block_registration_metadata_settings() { + $filter_metadata_registration = function( $settings, $metadata ) { + $settings['api_version'] = $metadata['apiVersion'] + 1; + return $settings; + }; + + add_filter( 'block_type_metadata_settings', $filter_metadata_registration, 10, 2 ); + $result = register_block_type_from_metadata( + __DIR__ . '/fixtures' + ); + remove_filter( 'block_type_metadata_settings', $filter_metadata_registration ); + + $this->assertSame( 3, $result->api_version ); + } }