WordPress.org

Make WordPress Core

Changeset 48263


Ignore:
Timestamp:
07/01/2020 01:08:11 PM (6 weeks ago)
Author:
gziolo
Message:

Editor: Support filtering arguments in block type registration

Adds possibility to filter the settings of a block type during its registration.

Props aduth, azaozz.
Fixes #49615.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-type.php

    r48118 r48263  
    263263        $args['name'] = $this->name;
    264264
     265        /**
     266         * Filters the arguments for registering a block type.
     267         *
     268         * @since 5.5.0
     269         *
     270         * @param array  $args       Array of arguments for registering a
     271         *                           block type.
     272         * @param string $block_type Block type name including namespace.
     273         */
     274        $args = apply_filters( 'register_block_type_args', $args, $this->name );
     275
    265276        foreach ( $args as $property_name => $property_value ) {
    266277            $this->$property_name = $property_value;
  • trunk/tests/phpunit/tests/blocks/register.php

    r48141 r48263  
    381381        $this->assertFalse( has_blocks( $content ) );
    382382    }
     383
     384    /**
     385     * @ticket 49615
     386     */
     387    public function test_filter_block_registration() {
     388        $filter_registration = function( $args, $name ) {
     389            $args['attributes'] = array( $name => array( 'type' => 'boolean' ) );
     390            return $args;
     391        };
     392
     393        add_filter( 'register_block_type_args', $filter_registration, 10, 2 );
     394        register_block_type( 'core/test-filtered', array() );
     395        remove_filter( 'register_block_type_args', $filter_registration );
     396
     397        $registry   = WP_Block_Type_Registry::get_instance();
     398        $block_type = $registry->get_registered( 'core/test-filtered' );
     399        $this->assertEquals( 'boolean', $block_type->attributes['core/test-filtered']['type'] );
     400    }
    383401}
Note: See TracChangeset for help on using the changeset viewer.