mirrored from git://develop.git.wordpress.org/
-
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
render_block_context
another approach that solve the context issue
#7522
Open
mukeshpanchal27
wants to merge
34
commits into
WordPress:trunk
Choose a base branch
from
mukeshpanchal27:fix/62046-test-approach
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
3bf219c
Introduce update_available_context()
mukeshpanchal27 7b54206
Apply suggestions from code review
mukeshpanchal27 dbd6126
Add unit tests
mukeshpanchal27 c229a57
Apply suggestions from code review
mukeshpanchal27 a95ca1d
Pull latest tests
mukeshpanchal27 976d7d6
Update renderBlock.php
mukeshpanchal27 adf52a7
Unexpected error
mukeshpanchal27 d410a3c
Commaaaa
mukeshpanchal27 b0af042
Add get_block_type_uses_context filter
mukeshpanchal27 a6db0fd
Remove stale condition
mukeshpanchal27 5cbb114
Call function if filter change the context value
mukeshpanchal27 898d29c
Address review feedbacks and add function docblock
mukeshpanchal27 fc76e2c
Remove whitespace
mukeshpanchal27 40bfc5e
Update docblock.
mukeshpanchal27 f50736d
Spaceee
mukeshpanchal27 ec2c0ab
Merge branch 'WordPress:trunk' into fix/62046-test-approach
mukeshpanchal27 e1203be
Apply suggestions from code review
mukeshpanchal27 218b887
Remove `get_block_type_uses_context` filter
mukeshpanchal27 33930ed
Initialize property as empty array by default
mukeshpanchal27 192e5ae
Revert context change
mukeshpanchal27 819e9db
Try to fix context
mukeshpanchal27 dc6577b
Update function name and docblock
mukeshpanchal27 28ce083
Fix unit tests for wpBlock.php
mukeshpanchal27 2c46470
PHPCS
mukeshpanchal27 9764c52
Apply suggestions from code review
mukeshpanchal27 de703d9
Fix unit tests for renderBlock.php
mukeshpanchal27 6c4f6cd
Fix unit tests for render.php
mukeshpanchal27 c7c5418
Move provides_context in to refresh_parsed_block_dependents()
mukeshpanchal27 dce8002
Bump the WP version number
mukeshpanchal27 22772c1
Merge branch 'WordPress:trunk' into fix/62046-test-approach
mukeshpanchal27 c83ca82
Remove context merging
mukeshpanchal27 210298f
Merge branch 'WordPress:trunk' into fix/62046-test-approach
mukeshpanchal27 4096831
Update available context in helper function
mukeshpanchal27 cd1a388
Merge branch 'WordPress:trunk' into fix/62046-test-approach
mukeshpanchal27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ class WP_Block { | |
* @var array | ||
* @access protected | ||
*/ | ||
protected $available_context; | ||
protected $available_context = array(); | ||
|
||
/** | ||
* Block type registry. | ||
|
@@ -140,15 +140,47 @@ 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. | ||
* | ||
* The method 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.8.0 | ||
*/ | ||
public function refresh_context_dependents() { | ||
$this->available_context = array_merge( $this->available_context, $this->context ); | ||
|
||
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 ) ) { | ||
$this->context[ $context_name ] = $this->available_context[ $context_name ]; | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this method needs to call That's because we modify the In other words:
|
||
} | ||
|
||
if ( ! empty( $block['innerBlocks'] ) ) { | ||
/** | ||
* 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.8.0 | ||
*/ | ||
public function refresh_parsed_block_dependents() { | ||
if ( ! empty( $this->parsed_block['innerBlocks'] ) ) { | ||
$child_context = $this->available_context; | ||
|
||
if ( ! empty( $this->block_type->provides_context ) ) { | ||
|
@@ -159,15 +191,15 @@ 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( $this->parsed_block['innerBlocks'], $child_context, $this->registry ); | ||
mukeshpanchal27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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']; | ||
} | ||
} | ||
|
||
|
@@ -506,14 +538,23 @@ 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->refresh_context_dependents(); | ||
} | ||
|
||
if ( $inner_block->parsed_block !== $source_block ) { | ||
$inner_block->refresh_parsed_block_dependents(); | ||
} | ||
|
||
$block_content .= $inner_block->render(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe add a comment here to clarify the remaining problem?