-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Testing: render_block_context
POC
#7420
Testing: render_block_context
POC
#7420
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
The POC code used from #7344 (comment) If we refactor core code per POC the ancestors context will be available in For Column block i add two context. 1) how much column [available_context:protected] => Array
(
[postId] => 6
[postType] => page
[wpp-columns] => 1
[wpp-col-width] => 50%
) If we use /*
* Inspiration taken from current block binding implementation.
* https://github.com/WordPress/wordpress-develop/blob/6.6/src/wp-includes/class-wp-block-bindings-registry.php#L193-L204
*/
add_filter(
'get_block_type_uses_context',
function ( $uses_context, $block_type ) {
if ( 'core/image' === $block_type->name ) {
// Use array_values to reset the array keys.
return array_values( array_unique( array_merge( $uses_context, array( 'wpp-columns','wpp-col-width' ) ) ) );
}
return $uses_context;
},
10,
2
); [context] => Array
(
[postId] => 6
[postType] => page
[wpp-columns] => 1
[wpp-col-width] => 50%
) |
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.
@mukeshpanchal27 A few questions on this. Most importantly, I don't understand how the parent block is set. Not related to this PR, even just generally in Core today.
* } | ||
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. | ||
*/ | ||
$context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block ); |
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.
Question: How is $parent_block
ever set here? Looking at the full function, it seems that it is only set to null
at the top of the function, but then never changed. Will this always be null
? 🤔
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 was introduce in https://core.trac.wordpress.org/changeset/51894 with message.
Introdices another param to these filters: $parent_block that is the "parent" WP_Block instance for nested blocks and null for top level blocks. Adds unit tests for the filters.
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.
Yes, but my question was how does this actually work? As far as I can tell $parent_block
is always null
. Does it ever get populated with an actual WP_Block
instance anywhere?
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.
I don't think. It always null for parent block.
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.
But then is that a bug? How does the parent block ever get set for any block?
@@ -159,7 +160,7 @@ public function __construct( $block, $available_context = array(), $registry = n | |||
} | |||
} | |||
|
|||
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); | |||
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry, $parent_block ); |
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.
Is this right? Shouldn't the parent block of the inner blocks of this block be $this
? Passing $parent_block
here I think would effectively pass the "grandparent block".
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.
No effect if i pass $this
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.
Can you please clarify what you mean? How do you mean there's no effect?
Logically, the child blocks should access their parent block. But by passing the parent block of the current block here to its child block(s), you're effectively passing the grandparent block, which seems wrong.
@@ -138,7 +139,7 @@ public function __construct( $block, $available_context = array(), $registry = n | |||
|
|||
$this->block_type = $registry->get_registered( $this->name ); | |||
|
|||
$this->available_context = $available_context; | |||
$this->available_context = apply_filters( 'render_block_context', $available_context, $block, $parent_block ); |
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.
I know this is a POC, but just noting that if we proceed with implementing this, this will need the original filter comment doc to be added.
Closing in favour of #7522 |
Testing PR. DO NOT REVIEW.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.