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

Conversation

mukeshpanchal27
Copy link
Member

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.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mukeshpanchal27
Copy link
Member Author

The POC code used from #7344 (comment)

If we refactor core code per POC the ancestors context will be available in available_context protected key.

For Column block i add two context. 1) how much column wpp-columns 2) column width wpp-col-width

[available_context:protected] => Array
        (
            [postId] => 6
            [postType] => page
            [wpp-columns] => 1
            [wpp-col-width] => 50%
        )

If we use get_block_type_uses_context filter then we get those context in $block->context public array key.

/*
 * 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%
        )

Copy link
Member

@felixarntz felixarntz left a 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 );
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?

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

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

@mukeshpanchal27
Copy link
Member Author

Closing in favour of #7522

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants