Make WordPress Core


Ignore:
Timestamp:
01/08/2021 04:43:29 PM (4 years ago)
Author:
gziolo
Message:

Editor: Support filtering arguments in block type registration from metadata

Adds 2 new hooks in register_block_type_from_metadata:

  • Named block_type_metadata to filter the content of metadata read from block.json
  • Named block_type_metadata_settings to filter the settings object determined from the metadata that is passed to register_block_type call

Props swissspidy.
Fixes #52138.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/register.php

    r49850 r49948  
    6363        $registry = WP_Block_Type_Registry::get_instance();
    6464
    65         foreach ( array( 'test-static', 'test-dynamic' ) as $block_name ) {
    66             $block_name = 'core/' . $block_name;
    67 
     65        foreach ( array( 'core/test-static', 'core/test-dynamic', 'my-plugin/notice' ) as $block_name ) {
    6866            if ( $registry->is_registered( $block_name ) ) {
    6967                $registry->unregister( $block_name );
     
    424422        $this->assertSame( 'boolean', $block_type->attributes['core/test-filtered']['type'] );
    425423    }
     424
     425    /**
     426     * @ticket 52138
     427     */
     428    public function test_filter_block_registration_metadata() {
     429        $filter_metadata_registration = function( $metadata ) {
     430            $metadata['apiVersion'] = 3;
     431            return $metadata;
     432        };
     433
     434        add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 );
     435        $result = register_block_type_from_metadata(
     436            __DIR__ . '/fixtures'
     437        );
     438        remove_filter( 'block_type_metadata', $filter_metadata_registration );
     439
     440        $this->assertSame( 3, $result->api_version );
     441    }
     442
     443    /**
     444     * @ticket 52138
     445     */
     446    public function test_filter_block_registration_metadata_settings() {
     447        $filter_metadata_registration = function( $settings, $metadata ) {
     448            $settings['api_version'] = $metadata['apiVersion'] + 1;
     449            return $settings;
     450        };
     451
     452        add_filter( 'block_type_metadata_settings', $filter_metadata_registration, 10, 2 );
     453        $result = register_block_type_from_metadata(
     454            __DIR__ . '/fixtures'
     455        );
     456        remove_filter( 'block_type_metadata_settings', $filter_metadata_registration );
     457
     458        $this->assertSame( 3, $result->api_version );
     459    }
    426460}
Note: See TracChangeset for help on using the changeset viewer.