Skip to content

Commit a36410d

Browse files
Apply suggestions from code review
Co-authored-by: Felix Arntz <[email protected]>
1 parent 859231e commit a36410d

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/wp-includes/class-wp-block.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,13 @@ public function render( $options = array() ) {
554554
/** This filter is documented in wp-includes/blocks.php */
555555
$inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
556556

557+
/*
558+
* The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`.
559+
* Therefore the second condition is irrelevant if the first one is satisfied.
560+
*/
557561
if ( $inner_block->context !== $inner_block_context ) {
558562
$inner_block->refresh_context_dependents();
559-
}
560-
561-
if ( $inner_block->parsed_block !== $source_block ) {
563+
} elseif ( $inner_block->parsed_block !== $source_block ) {
562564
$inner_block->refresh_parsed_block_dependents();
563565
}
564566

tests/phpunit/tests/blocks/renderBlock.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ function ( $context, $parsed_block ) {
298298
<!-- wp:tests/context-consumer /-->
299299
HTML
300300
);
301-
$this->assertTrue( isset( $provided_context['arbitrary'] ), 'Test block is top-level block: Block context should include "arbitrary"' );
302-
$this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is top-level block: "arbitrary" in context should be "ok"' );
301+
$this->assertFalse( isset( $provided_context['arbitrary'] ), 'Test block is top-level block: Block context should not include "arbitrary"' );
303302

304303
do_blocks(
305304
<<<HTML
@@ -308,6 +307,16 @@ function ( $context, $parsed_block ) {
308307
<!-- /wp:group -->
309308
HTML
310309
);
310+
311+
/*
312+
* These assertions assert something that ideally should not be the case: Inner blocks should respect the
313+
* `uses_context` value just like top-level blocks do. However, due to logic in `WP_Block::render()`, the
314+
* `context` property value itself is filterable when it should rather only apply to the `available_context`
315+
* property.
316+
* However, changing this behavior now would be a backward compatibility break, hence the assertion here.
317+
* Potentially it can be reconsidered in the future, so that these two assertions could be replaced with an
318+
* `assertFalse( isset( $provided_context['arbitrary'] ) )`.
319+
*/
311320
$this->assertTrue( isset( $provided_context['arbitrary'] ), 'Test block is inner block: Block context should include "arbitrary"' );
312321
$this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is inner block: "arbitrary" in context should be "ok"' );
313322
}

0 commit comments

Comments
 (0)