Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block bindings: Add filter for supported block attributes #7404

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function __get( $name ) {
*
* @since 6.5.0
* @since 6.6.0 Handle the `__default` attribute for pattern overrides.
* @since 6.7.0 Add filter for supported block attributes for block bindings.
*
* @return array The computed block attributes for the provided block bindings.
*/
Expand All @@ -250,6 +251,20 @@ private function process_block_bindings() {
'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ),
);

/**
* Filters the supported block attributes for block bindings.
*
* @since 6.7.0
*
* @param array $supported_block_attributes The default supported block attributes.
*
* @return array The updated supported block attributes.
*/
$supported_block_attributes = apply_filters(
'block_bindings_supported_block_attributes',
$supported_block_attributes
);

// If the block doesn't have the bindings property, isn't one of the supported
// block types, or the bindings property is not an array, return the block content.
if (
Expand Down
116 changes: 116 additions & 0 deletions tests/phpunit/tests/block-bindings/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,122 @@ public function test_default_binding_for_pattern_overrides() {
);
}

/**
* Tests that filter `block_bindings_supported_block_attributes` is applied.
*
* @ticket 62090
*/
public function test_filter_block_bindings_supported_block_attributes_custom_block_content_attribute() {
register_block_type(
'plugin/custom-block',
array(
'attributes' => array(
'content' => array(
'type' => 'string',
'default' => '',
),
),
'render_callback' => function ( $attributes, $content ) {
return '<p>' . ( $attributes['content'] ?? 'Default content' ) . '</p>';
},
)
);

$filter_supported_attributes = function ( $supported_block_attributes ) {
$supported_block_attributes['plugin/custom-block'] = array( 'content' );
return $supported_block_attributes;
};

add_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes );

$get_value_callback = function ( $source_args ) {
$key = $source_args['key'];
return "Source value: $key";
};

register_block_bindings_source(
self::SOURCE_NAME,
array(
'label' => self::SOURCE_LABEL,
'get_value_callback' => $get_value_callback,
)
);

$block_content = <<<HTML
<!-- wp:plugin/custom-block {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "custom_attribute"}}}}} -->
<p>Default content</p>
<!-- /wp:plugin/custom-block -->
HTML;

$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0] );
$result = $block->render();

remove_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes );

$this->assertSame(
'<p>Source value: custom_attribute</p>',
trim( $result ),
'The block content should show the custom attribute value after applying the filter.'
);
}

/**
* Tests that the 'block_bindings_supported_block_attributes' filter works for custom blocks and attributes.
*
* @ticket 61181
*/
public function test_filter_block_bindings_supported_block_attributes_custom_block_custom_attribute() {
register_block_type(
'plugin/custom-block-with-custom-attr',
array(
'attributes' => array(
'customAttribute' => array(
'type' => 'string',
'default' => '',
),
),
'render_callback' => function ( $attributes ) {
return '<div data-value="' . ( $attributes['customAttribute'] ?? 'Default value' ) . '">static content</div>';
},
)
);

$filter_supported_attributes = function ( $supported_block_attributes ) {
$supported_block_attributes['plugin/custom-block-with-custom-attr'] = array( 'customAttribute' );
return $supported_block_attributes;
};

add_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes );

register_block_bindings_source(
self::SOURCE_NAME,
array(
'label' => self::SOURCE_LABEL,
'get_value_callback' => function ( $source_args ) {
return "Bound value: {$source_args['key']}";
},
)
);

$block_content = <<<HTML
<!-- wp:plugin/custom-block-with-custom-attr {"metadata":{"bindings":{"customAttribute":{"source":"test/source", "args": {"key": "custom_key"}}}}} -->
<!-- /wp:plugin/custom-block-with-custom-attr -->
HTML;

$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0] );
$result = $block->render();

remove_filter( 'block_bindings_supported_block_attributes', $filter_supported_attributes );

$this->assertSame(
'<div data-value="Bound value: custom_key">static content</div>',
trim( $result ),
'The block content should show the bound value for the custom attribute after applying the filter.'
);
}

/**
* Tests that filter `block_bindings_source_value` is applied.
*
Expand Down
Loading