From f5ed2012420962338699acfda4286e091031dbab Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 14 Oct 2024 12:13:48 +0530 Subject: [PATCH 1/3] Test phpcs --- src/wp-includes/class-wp-block.php | 57 +++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 8cfa996028da2..1d8fa257bab7e 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -56,7 +56,7 @@ class WP_Block { * @var array * @access protected */ - protected $available_context; + protected $available_context = array(); /** * Block type registry. @@ -140,6 +140,26 @@ public function __construct( $block, $available_context = array(), $registry = n $this->available_context = $available_context; + $this->update_block_context(); + $this->update_parsed_block_content(); + } + + /** + * 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. 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 the inner + * blocks by passing any context values the block provides (`provides_context`). + * + * The method recursively processes inner blocks by creating new instances of `WP_Block` for each inner block, + * passing down the updated context. + * + * @since 6.7.0 + */ + public function update_block_context() { + $this->context = array_merge( $this->context, $this->available_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 ) ) { @@ -148,7 +168,7 @@ public function __construct( $block, $available_context = array(), $registry = n } } - if ( ! empty( $block['innerBlocks'] ) ) { + if ( ! empty( $this->parsed_block['innerBlocks'] ) ) { $child_context = $this->available_context; if ( ! empty( $this->block_type->provides_context ) ) { @@ -159,15 +179,26 @@ 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 ); } + } - if ( ! empty( $block['innerHTML'] ) ) { - $this->inner_html = $block['innerHTML']; + /** + * Updates the parsed block content for the current block. + * + * This method sets the `inner_html` and `inner_content` properties of the block based on the parsed + * block content provided during the block's 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. + * + * @since 6.7.0 + */ + public function update_parsed_block_content() { + 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,7 +537,8 @@ 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 ); @@ -514,6 +546,15 @@ public function render( $options = array() ) { /** 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 ) { + $inner_block->available_context = array_merge( $this->available_context, $inner_block->context ); + $inner_block->update_block_context(); + } + + if ( $inner_block->parsed_block !== $source_block ) { + $inner_block->update_parsed_block_content(); + } + $block_content .= $inner_block->render(); } From e4d2a9541c64a7962d80132d8ebd7ce8d9b590ae Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 16 Oct 2024 10:55:33 +0530 Subject: [PATCH 2/3] Update class-wp-block.php --- src/wp-includes/class-wp-block.php | 50 ++++++++++++++++++------------ 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 1d8fa257bab7e..b6b3af7600631 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -140,25 +140,32 @@ public function __construct( $block, $available_context = array(), $registry = n $this->available_context = $available_context; - $this->update_block_context(); - $this->update_parsed_block_content(); + $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. 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 the inner - * blocks by passing any context values the block provides (`provides_context`). + * 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`). * - * The method recursively processes inner blocks by creating new instances of `WP_Block` for each inner block, - * passing down the updated 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 update_block_context() { - $this->context = array_merge( $this->context, $this->available_context ); + public function refresh_context_dependents() { + $this->context = $this->available_context; + + // Remove "postId" and "postType" keys. + if ( isset( $this->context['postId'] ) || isset( $this->context['postType'] ) ) { + unset( $this->context['postId'], $this->context['postType'] ); + } if ( ! empty( $this->block_type->uses_context ) ) { foreach ( $this->block_type->uses_context as $context_name ) { @@ -169,30 +176,33 @@ public function update_block_context() { } if ( ! empty( $this->parsed_block['innerBlocks'] ) ) { - $child_context = $this->available_context; - 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 ]; } } } - - $this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $child_context, $this->registry ); } } /** - * Updates the parsed block content for the current block. + * 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 the block's 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. + * 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 update_parsed_block_content() { + 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( $this->parsed_block['innerHTML'] ) ) { $this->inner_html = $this->parsed_block['innerHTML']; } @@ -548,11 +558,11 @@ public function render( $options = array() ) { if ( $inner_block->context !== $inner_block_context ) { $inner_block->available_context = array_merge( $this->available_context, $inner_block->context ); - $inner_block->update_block_context(); + $inner_block->refresh_context_dependents(); } if ( $inner_block->parsed_block !== $source_block ) { - $inner_block->update_parsed_block_content(); + $inner_block->refresh_parsed_block_dependents(); } $block_content .= $inner_block->render(); From 4456c2411c14650f0ad4e78d1af81023c2234c63 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 16 Oct 2024 10:57:55 +0530 Subject: [PATCH 3/3] Apply suggestions from code review --- src/wp-includes/class-wp-block.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index b6b3af7600631..f02a690ffb262 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -151,10 +151,10 @@ public function __construct( $block, $available_context = array(), $registry = n * 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 + * 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` + * 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