Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
);
}

Expand Down
40 changes: 37 additions & 3 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ function tearDown() {

$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 );
}
Expand Down Expand Up @@ -423,4 +421,40 @@ public function test_filter_block_registration() {
$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 );
}
}