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

Testing: render_block_context POC #7420

Closed
Closed
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
24 changes: 1 addition & 23 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2090,29 +2090,7 @@ function render_block( $parsed_block ) {
$context['postType'] = $post->post_type;
}

/**
* Filters the default context provided to a rendered block.
*
* @since 5.5.0
* @since 5.9.0 The `$parent_block` parameter was added.
*
* @param array $context Default context.
* @param array $parsed_block {
* An associative array of the block being rendered. See WP_Block_Parser_Block.
*
* @type string $blockName Name of block.
* @type array $attrs Attributes from block comment delimiters.
* @type array[] $innerBlocks List of inner blocks. An array of arrays that
* have the same structure as this one.
* @type string $innerHTML HTML from inside block comment delimiters.
* @type array $innerContent List of string fragments and null markers where
* inner blocks were found.
* }
* @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 );
Copy link
Member

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? 🤔

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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?


$block = new WP_Block( $parsed_block, $context );
$block = new WP_Block( $parsed_block, $context, null, $parent_block );

return $block->render();
}
Expand Down
16 changes: 14 additions & 2 deletions src/wp-includes/class-wp-block-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,37 @@ class WP_Block_List implements Iterator, ArrayAccess, Countable {
*/
protected $registry;

/**
* Reference to the parent block.
*
* @since 6.7.0
* @var WP_Block|null
* @access protected
*/
protected $parent_block;

/**
* Constructor.
*
* Populates object properties from the provided block instance argument.
*
* @since 5.5.0
* @since 6.7.0 Added the optional `$parent_block` argument.
*
* @param array[]|WP_Block[] $blocks Array of parsed block data, or block instances.
* @param array $available_context Optional array of ancestry context values.
* @param WP_Block_Type_Registry $registry Optional block type registry.
* @param WP_Block|null $parent_block Optional. If this is a nested block, a reference to the parent block.
*/
public function __construct( $blocks, $available_context = array(), $registry = null ) {
public function __construct( $blocks, $available_context = array(), $registry = null, $parent_block = null ) {
if ( ! $registry instanceof WP_Block_Type_Registry ) {
$registry = WP_Block_Type_Registry::get_instance();
}

$this->blocks = $blocks;
$this->available_context = $available_context;
$this->registry = $registry;
$this->parent_block = $parent_block;
}

/**
Expand Down Expand Up @@ -93,7 +105,7 @@ public function offsetGet( $offset ) {
$block = $this->blocks[ $offset ];

if ( isset( $block ) && is_array( $block ) ) {
$block = new WP_Block( $block, $this->available_context, $this->registry );
$block = new WP_Block( $block, $this->available_context, $this->registry, $this->parent_block );

$this->blocks[ $offset ] = $block;
}
Expand Down
7 changes: 4 additions & 3 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class WP_Block {
* its registered type will be assigned to the block's `context` property.
*
* @since 5.5.0
* @since 6.7.0 Added the optional `$parent_block` argument.
*
* @param array $block {
* An associative array of a single parsed block object. See WP_Block_Parser_Block.
Expand All @@ -126,7 +127,7 @@ class WP_Block {
* @param array $available_context Optional array of ancestry context values.
* @param WP_Block_Type_Registry $registry Optional block type registry.
*/
public function __construct( $block, $available_context = array(), $registry = null ) {
public function __construct( $block, $available_context = array(), $registry = null, $parent_block = null ) {
$this->parsed_block = $block;
$this->name = $block['blockName'];

Expand All @@ -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 );
Copy link
Member

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.


if ( ! empty( $this->block_type->uses_context ) ) {
foreach ( $this->block_type->uses_context as $context_name ) {
Expand All @@ -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 );
Copy link
Member

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".

Copy link
Member Author

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

Copy link
Member

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.

}

if ( ! empty( $block['innerHTML'] ) ) {
Expand Down
Loading