From a425f15dd7c63d3c603cb23c238d2590e5b1ba95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Wed, 1 Jul 2020 13:08:11 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@48263 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-block-type.php | 11 +++++++++++ tests/phpunit/tests/blocks/register.php | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 4758e172ba..3052affdc1 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -262,6 +262,17 @@ class WP_Block_Type { $args['name'] = $this->name; + /** + * Filters the arguments for registering a block type. + * + * @since 5.5.0 + * + * @param array $args Array of arguments for registering a + * block type. + * @param string $block_type Block type name including namespace. + */ + $args = apply_filters( 'register_block_type_args', $args, $this->name ); + foreach ( $args as $property_name => $property_value ) { $this->$property_name = $property_value; } diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 514fa80750..d0387f0148 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -380,4 +380,22 @@ class WP_Test_Block_Register extends WP_UnitTestCase { $content = file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-expected.html' ); $this->assertFalse( has_blocks( $content ) ); } + + /** + * @ticket 49615 + */ + public function test_filter_block_registration() { + $filter_registration = function( $args, $name ) { + $args['attributes'] = array( $name => array( 'type' => 'boolean' ) ); + return $args; + }; + + add_filter( 'register_block_type_args', $filter_registration, 10, 2 ); + register_block_type( 'core/test-filtered', array() ); + remove_filter( 'register_block_type_args', $filter_registration ); + + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( 'core/test-filtered' ); + $this->assertEquals( 'boolean', $block_type->attributes['core/test-filtered']['type'] ); + } }