-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 API: Preserve unknown, respect null
in server attributes preparation
#12003
Conversation
Funny enough, I caught myself staring at @azaozz 's #11973 wondering: How does Well, in fact, this ties back to the following note of the original comment:
So a In master, you'd find the following, which appears nonsensical to me: ( new WP_Block_Type( 'core/dummy', [] ) )->prepare_blocks_for_attributes( [ 'exists' => 'keep' ] );
// [ 'exists' => 'keep' ]
( new WP_Block_Type( 'core/dummy', [ 'attributes' => [] ] ) )->prepare_blocks_for_attributes( [ 'exists' => 'keep' ] );
// [] |
This is huge. Right now you have to (annoyingly) register It would be nice if these (and any other "protected" properties) could be validated on the server as well, but that's a discussion for the future.
You can say that again! Figuring out how server-registered attributes worked was a huge time sink for me when we first started building blocks. This seems like a huge step forward into making this behaviour more consistent and easily understandable. |
Previously we would pass `NULL` to `render`. While `render` provides a default `array()` value, since an explicit value is provided, it would not take effect. With this change, the endpoint assures a default value to be provided as a parameter to get the `get_item` callback that is compatible with the function signature of `WP_BlockType::render`. Alternatively, it could be considered to have separate invocations of `render`, depending on whether the request parameter is set.
c7bb6d6
to
eb40939
Compare
@@ -169,14 +174,26 @@ function test_prepare_attributes() { | |||
$this->assertEquals( | |||
array( | |||
'correct' => 'include', | |||
'wrongType' => null, | |||
/* wrongType */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this change about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's indicating that we expect that 'wrongType'
will not appear in $prepared_attributes
.
I agree with this update and the reasoning. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this logic makes a lot more sense to me.
I tested this by registering a render_callback
on paragraph blocks and checking that it renders className
and align
properly.
register_block_type(
'core/paragraph',
array(
'render_callback' => function( $attributes ) {
return '<p>' . json_encode( $attributes ) . '</p>';
},
)
);
This pull request seeks to revise the behavior of the server-side
BlockType::prepare_attributes_for_render
with regards to validation:render_callback
.render_callback
cannot receive a block'salign
andcustomClassName
attribute values, since these are defined as a client-side filter (related).null
as a proper value in its own right{"attribute":null}
as an explicitly empty value, and the server would wrongly initiate defaulting behavior.This is partly intended to serve for consistency:
attributes
.wp.blocks.parse( '<!-- wp:latest-posts {"className":2} /-->')[ 0 ].attributes.className === undefined
One potential downside is a newly introduced inconsistency in that the client still does not respect unknown attributes. This may be simple enough to add support if desired.
Testing instructions:
Ensure PHP tests pass:
Optionally, you may try registering server-side a
render_callback
for a block which supports alignment or custom class name in the client, ensuring that the value is passed to therender_callback
.Possible future tasks:
supports
attributes should be defined by the server. This is likely to be blocked by broader conversation of Block API: Server-side awareness of block types #2751.cc @mtias @azaozz