[wp-trac] [WordPress Trac] #62046: `render_block_context` filter works differently on top-level vs. inner blocks
WordPress Trac
noreply at wordpress.org
Fri Sep 13 00:29:31 UTC 2024
#62046: `render_block_context` filter works differently on top-level vs. inner
blocks
--------------------------+-----------------------------
Reporter: dlh | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Editor | Version: 5.9
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
The `render_block_context` filter has a different effect depending on
whether the block being rendered is a top-level block or an inner block.
When the block is a top-level block, then context provided with
`render_block_context` is made available as context to its inner blocks.
When the block is an inner block, the context provided with the filter
isn't made available to its inner blocks.
By "top-level block," I mean a single block that's rendered with
`render_block()`, such as within `do_blocks()`. By "inner block," I mean
an inner block rendered within `WP_Block::render()`.
I think this difference in behavior can be traced back to when the filter
is applied.
The logic to make context available to inner blocks occurs within the
`WP_Block` constructor. In `render_block()`, the value returned by the
`render_block_context` filter is passed to the new `WP_Block` object, so
the filtered value is the one made available to inner blocks.
Within `WP_Block::render()`, the `WP_Block` object for the inner block has
already been instantiated (in `WP_Block_List`). The value returned by
`render_block_context` is assigned directly to the inner block's
`$context` property, so the logic in the constructor to provide that
context to deeper inner blocks isn't invoked.
The linked PR contains a unit test demonstrating this behavior. The test
is derived from the real situation in which we observed it: A custom block
capable of passing `query` context to an inner `core/post-template` block
via `render_block_context`. The post template block received the expected
context when the custom block was a top-level block but not when it was an
inner block, such as inside a group.
`render_block_context` was applied to inner blocks in #51612 along with
related filters, and there's quite a bit of discussion there about when
exactly to apply them, so the logic in place now certainly has reasons
behind it.
Still, I would expect the filter to behave the same way with respect to
inner blocks regardless of where in the tree the block being filtered is.
Between the two options, I would expect the filter to behave like it does
in `render_block()`: Context supplied with a filter is made available to
inner blocks.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/62046>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list