Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#50146 new defect (bug)

Blocks: `serialize_block()` doesn't support `WP_Block_Parser_Block`

Reported by: dlh Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.1
Component: Editor Keywords: has-patch
Focuses: Cc:


serialize_block() is documented as accepting a WP_Block_Parser_Block, but it treats the passed value as an array. Because WP_Block_Parser_Block doesn't implement ArrayAccess, passing one to serialize_block() generates a fatal error.

The attached patch would allow instances of WP_Block_Parser_Block to be used with serialize_block() by casting them to arrays. It would make the same change to filter_block_kses(), which exhibits the same mismatch in behavior.

If adding support for WP_Block_Parser_Block to these functions is seen as too risky from a perspective of maintaining compatibility with future iterations on block parsing, then simply changing the DocBlocks on these two functions might instead be the way to go.

Attachments (2)

50146.diff (1.9 KB) - added by dlh 3 months ago.
50146.2.diff (3.9 KB) - added by dlh 3 months ago.

Download all attachments as: .zip

Change History (5)

3 months ago

#1 @aduth
3 months ago

If I recall correctly, the implementation was based on the fact that WP_Block_Parser::parse claims to return type WP_Block_Parser_Block[], when in fact it returns an array of arrays.

Which isn't to say this change request isn't justified, and likely correct, but the full extent of the problem permeates beyond just this one function.

Aside: Possibly related: #49926

Note: `render_block` and serialize_block currently receive $block in the parsed array form. While I remarked differently on the original pull request, I don't know that these actually need to change, since it seems rather sensible that these would except to receive blocks in their parsed forms. If one has access to a WP_Block instance and needs to create its rendered representation, the instance's render function can be used directly.

#2 @dlh
3 months ago

@aduth Thanks for that context. It sounds like the docs within class-wp-block-parser.php might need a fresh look with respect to WP_Block_Parser_Block. I'll be happy to do that while the original change here collects any additional feedback, and I'll include any other changes from that review in a second patch.

#3 @dlh
3 months ago

50146.2.diff updates the documentation references to WP_Block_Parser_Block in class-wp-block-parser.php where needed, as far as I could determine.

This patch also omits the cast to (array) in serialize_block() from the first patch, which simplifies the change to one of just documentation. While supporting WP_Block_Parser_Block in these functions would still be useful, the discussion in and around #50009 makes me wonder whether it's premature to bind these functions to a specific implementation.

3 months ago

Note: See TracTickets for help on using tickets.