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

render_block_context another approach that solve the context issue #7522

Open
wants to merge 31 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3bf219c
Introduce update_available_context()
mukeshpanchal27 Oct 7, 2024
7b54206
Apply suggestions from code review
mukeshpanchal27 Oct 7, 2024
dbd6126
Add unit tests
mukeshpanchal27 Oct 7, 2024
c229a57
Apply suggestions from code review
mukeshpanchal27 Oct 7, 2024
a95ca1d
Pull latest tests
mukeshpanchal27 Oct 8, 2024
976d7d6
Update renderBlock.php
mukeshpanchal27 Oct 8, 2024
adf52a7
Unexpected error
mukeshpanchal27 Oct 8, 2024
d410a3c
Commaaaa
mukeshpanchal27 Oct 8, 2024
b0af042
Add get_block_type_uses_context filter
mukeshpanchal27 Oct 8, 2024
a6db0fd
Remove stale condition
mukeshpanchal27 Oct 9, 2024
5cbb114
Call function if filter change the context value
mukeshpanchal27 Oct 9, 2024
898d29c
Address review feedbacks and add function docblock
mukeshpanchal27 Oct 9, 2024
fc76e2c
Remove whitespace
mukeshpanchal27 Oct 9, 2024
40bfc5e
Update docblock.
mukeshpanchal27 Oct 9, 2024
f50736d
Spaceee
mukeshpanchal27 Oct 9, 2024
ec2c0ab
Merge branch 'WordPress:trunk' into fix/62046-test-approach
mukeshpanchal27 Oct 10, 2024
e1203be
Apply suggestions from code review
mukeshpanchal27 Oct 10, 2024
218b887
Remove `get_block_type_uses_context` filter
mukeshpanchal27 Oct 10, 2024
33930ed
Initialize property as empty array by default
mukeshpanchal27 Oct 10, 2024
192e5ae
Revert context change
mukeshpanchal27 Oct 14, 2024
819e9db
Try to fix context
mukeshpanchal27 Oct 14, 2024
dc6577b
Update function name and docblock
mukeshpanchal27 Oct 16, 2024
28ce083
Fix unit tests for wpBlock.php
mukeshpanchal27 Oct 16, 2024
2c46470
PHPCS
mukeshpanchal27 Oct 16, 2024
9764c52
Apply suggestions from code review
mukeshpanchal27 Oct 16, 2024
de703d9
Fix unit tests for renderBlock.php
mukeshpanchal27 Oct 16, 2024
6c4f6cd
Fix unit tests for render.php
mukeshpanchal27 Oct 16, 2024
c7c5418
Move provides_context in to refresh_parsed_block_dependents()
mukeshpanchal27 Oct 17, 2024
dce8002
Bump the WP version number
mukeshpanchal27 Oct 28, 2024
22772c1
Merge branch 'WordPress:trunk' into fix/62046-test-approach
mukeshpanchal27 Nov 5, 2024
c83ca82
Remove context merging
mukeshpanchal27 Nov 5, 2024
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
73 changes: 62 additions & 11 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class WP_Block {
* @var array
* @access protected
*/
protected $available_context;
protected $available_context = array();

/**
* Block type registry.
Expand Down Expand Up @@ -140,6 +140,33 @@ public function __construct( $block, $available_context = array(), $registry = n

$this->available_context = $available_context;

$this->refresh_context_dependents();
$this->refresh_parsed_block_dependents();
}

/**
* Updates the context for the current block and its inner blocks.
*
* This method updates the block's `context` property by merging the available context from ancestor blocks
* with the block's own context values. It removes specific context keys (`postId` and `postType`) that should
* not be carried over. The block only consumes the context keys specified in its registered block type (`uses_context`).
*
* After updating its own context, the method also updates the context of inner blocks, if any, by passing down
* any context values the block provides (`provides_context`).
*
* If the block has inner blocks, the method recursively processes them by creating new instances of `WP_Block`
* for each inner block and updating their context based on the block's `provides_context` property.
*
* @since 6.7.0
*/
public function refresh_context_dependents() {
$this->context = $this->available_context;

// Remove "postId" and "postType" keys.
if ( isset( $this->context['postId'] ) || isset( $this->context['postType'] ) ) {
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
unset( $this->context['postId'], $this->context['postType'] );
}

if ( ! empty( $this->block_type->uses_context ) ) {
foreach ( $this->block_type->uses_context as $context_name ) {
if ( array_key_exists( $context_name, $this->available_context ) ) {
Expand All @@ -148,26 +175,40 @@ public function __construct( $block, $available_context = array(), $registry = n
}
}

if ( ! empty( $block['innerBlocks'] ) ) {
$child_context = $this->available_context;

if ( ! empty( $this->parsed_block['innerBlocks'] ) ) {
if ( ! empty( $this->block_type->provides_context ) ) {
foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) {
if ( array_key_exists( $attribute_name, $this->attributes ) ) {
$child_context[ $context_name ] = $this->attributes[ $attribute_name ];
$this->available_context[ $context_name ] = $this->attributes[ $attribute_name ];
}
}
}
}
}
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved

$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
/**
* Updates the parsed block content for the current block and its inner blocks.
*
* This method sets the `inner_html` and `inner_content` properties of the block based on the parsed
* block content provided during initialization. It ensures that the block instance reflects the
* most up-to-date content for both the inner HTML and any string fragments around inner blocks.
*
* If the block has inner blocks, this method initializes a new `WP_Block_List` for them, ensuring the
* correct content and context are updated for each nested block.
*
* @since 6.7.0
*/
public function refresh_parsed_block_dependents() {
if ( ! empty( $this->parsed_block['innerBlocks'] ) ) {
$this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $this->available_context, $this->registry );
}

if ( ! empty( $block['innerHTML'] ) ) {
$this->inner_html = $block['innerHTML'];
if ( ! empty( $this->parsed_block['innerHTML'] ) ) {
$this->inner_html = $this->parsed_block['innerHTML'];
}

if ( ! empty( $block['innerContent'] ) ) {
$this->inner_content = $block['innerContent'];
if ( ! empty( $this->parsed_block['innerContent'] ) ) {
$this->inner_content = $this->parsed_block['innerContent'];
}
}

Expand Down Expand Up @@ -506,14 +547,24 @@ public function render( $options = array() ) {
if ( ! is_null( $pre_render ) ) {
$block_content .= $pre_render;
} else {
$source_block = $inner_block->parsed_block;
$source_block = $inner_block->parsed_block;
$inner_block_context = $inner_block->context;

/** This filter is documented in wp-includes/blocks.php */
$inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block );

/** This filter is documented in wp-includes/blocks.php */
$inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );

if ( $inner_block->context !== $inner_block_context ) {
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$inner_block->available_context = array_merge( $this->available_context, $inner_block->context );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line doesn't look right to me. If we want to update $available_context based on $context, this should happen within the refresh_context_dependents() because then $available_context is a dependent of $context.

$inner_block->refresh_context_dependents();
}

if ( $inner_block->parsed_block !== $source_block ) {
$inner_block->refresh_parsed_block_dependents();
}

$block_content .= $inner_block->render();
}

Expand Down
119 changes: 119 additions & 0 deletions tests/phpunit/tests/blocks/renderBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,123 @@ public function test_default_context_is_filterable() {

$this->assertSame( array( 'example' => 'ok' ), $provided_context[0] );
}

/**
* Tests the behavior of the 'render_block_context' filter based on the location of the filtered block.
*
* @ticket 62046
*/
public function test_render_block_context_inner_blocks() {
$provided_context = array();

register_block_type(
'tests/context-provider',
array(
'provides_context' => array( 'example' ),
)
);

register_block_type(
'tests/context-consumer',
array(
'uses_context' => array( 'example' ),
'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context = $block->context;

return '';
},
)
);

// Filter the context provided by the test block.
add_filter(
'render_block_context',
function ( $context, $parsed_block ) {
if ( isset( $parsed_block['blockName'] ) && 'tests/context-provider' === $parsed_block['blockName'] ) {
$context['example'] = 'ok';
}

return $context;
},
10,
2
);

// Test inner block context when the provider block is a top-level block.
do_blocks(
<<<HTML
<!-- wp:tests/context-provider -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:tests/context-provider -->
HTML
);
$this->assertTrue( isset( $provided_context['example'] ), 'Test block is top-level block: Context should include "example"' );
$this->assertSame( 'ok', $provided_context['example'], 'Test block is top-level block: "example" in context should be "ok"' );

// Test inner block context when the provider block is an inner block.
do_blocks(
<<<HTML
<!-- wp:group {"layout":{"type":"constrained"}} -->
<!-- wp:tests/context-provider -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:tests/context-provider -->
<!-- /wp:group -->
HTML
);
$this->assertTrue( isset( $provided_context['example'] ), 'Test block is inner block: Block context should include "example"' );
$this->assertSame( 'ok', $provided_context['example'], 'Test block is inner block: "example" in context should be "ok"' );
}

/**
* Tests that the 'render_block_context' filter arbitrary context.
*
* @ticket 62046
*/
public function test_render_block_context_allowed_context() {
$provided_context = array();

register_block_type(
'tests/context-consumer',
array(
'uses_context' => array( 'example' ),
'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context = $block->context;

return '';
},
)
);

// Filter the context provided to the test block.
add_filter(
'render_block_context',
function ( $context, $parsed_block ) {
if ( isset( $parsed_block['blockName'] ) && 'tests/context-consumer' === $parsed_block['blockName'] ) {
$context['arbitrary'] = 'ok';
}

return $context;
},
10,
2
);

do_blocks(
<<<HTML
<!-- wp:tests/context-consumer /-->
HTML
);
$this->assertTrue( isset( $provided_context['arbitrary'] ), 'Test block is top-level block: Block context should include "arbitrary"' );
$this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is top-level block: "arbitrary" in context should be "ok"' );

do_blocks(
<<<HTML
<!-- wp:group {"layout":{"type":"constrained"}} -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:group -->
HTML
);
$this->assertTrue( isset( $provided_context['arbitrary'] ), 'Test block is inner block: Block context should include "arbitrary"' );
$this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is inner block: "arbitrary" in context should be "ok"' );
}
}
9 changes: 6 additions & 3 deletions tests/phpunit/tests/blocks/wpBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
);
$block = new WP_Block( $parsed_block, $context, $this->registry );

$this->assertSame( array( 'requested' => 'included' ), $block->context );
$this->assertSame( $context, $block->context );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -218,9 +218,12 @@
$context = array( 'unrequested' => 'not included' );
$block = new WP_Block( $parsed_block, $context, $this->registry );

$this->assertCount( 0, $block->context );
$this->assertCount( 1, $block->context );
$this->assertSame(
array( 'core/recordId' => 10 ),
array(
'unrequested' => 'not included',
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'core/recordId' => 10

Check failure on line 225 in tests/phpunit/tests/blocks/wpBlock.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

There should be a comma after the last array item in a multi-line array.
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
),
$block->inner_blocks[0]->context
);
}
Expand Down
Loading