🔥 HOT: Changeset/ - Full Gallery 2025

Changeset 59662


Ignore:
Timestamp:
01/17/2025 09:35:50 PM (12 months ago)
Author:
joemcgill
Message:

Editor: Improve consistency of render_block_context filter.

This ensures that when block context is filtered via render_block_context, the filtered value is provided as available context to inner blocks.

For backwards compatibility reasons, filtered context is added to inner block context regardless of whether that block has declared support via the uses_context property.

Props mukesh27, flixos90, gziolo, dlh, joemcgill, santosguillamot.
Fixes #62046.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block.php

    r59361 r59662  
    5757     * @access protected
    5858     */
    59     protected $available_context;
     59    protected $available_context = array();
    6060
    6161    /**
     
    141141        $this->available_context = $available_context;
    142142
     143        $this->refresh_context_dependents();
     144    }
     145
     146    /**
     147     * Updates the context for the current block and its inner blocks.
     148     *
     149     * The method updates the context of inner blocks, if any, by passing down
     150     * any context values the block provides (`provides_context`).
     151     *
     152     * If the block has inner blocks, the method recursively processes them by creating new instances of `WP_Block`
     153     * for each inner block and updating their context based on the block's `provides_context` property.
     154     *
     155     * @since 6.8.0
     156     */
     157    public function refresh_context_dependents() {
     158        /*
     159         * Merging the `$context` property here is not ideal, but for now needs to happen because of backward compatibility.
     160         * Ideally, the `$context` property itself would not be filterable directly and only the `$available_context` would be filterable.
     161         * However, this needs to be separately explored whether it's possible without breakage.
     162         */
     163        $this->available_context = array_merge( $this->available_context, $this->context );
     164
    143165        if ( ! empty( $this->block_type->uses_context ) ) {
    144166            foreach ( $this->block_type->uses_context as $context_name ) {
     
    149171        }
    150172
    151         if ( ! empty( $block['innerBlocks'] ) ) {
     173        $this->refresh_parsed_block_dependents();
     174    }
     175
     176    /**
     177     * Updates the parsed block content for the current block and its inner blocks.
     178     *
     179     * This method sets the `inner_html` and `inner_content` properties of the block based on the parsed
     180     * block content provided during initialization. It ensures that the block instance reflects the
     181     * most up-to-date content for both the inner HTML and any string fragments around inner blocks.
     182     *
     183     * If the block has inner blocks, this method initializes a new `WP_Block_List` for them, ensuring the
     184     * correct content and context are updated for each nested block.
     185     *
     186     * @since 6.8.0
     187     */
     188    public function refresh_parsed_block_dependents() {
     189        if ( ! empty( $this->parsed_block['innerBlocks'] ) ) {
    152190            $child_context = $this->available_context;
    153191
     
    160198            }
    161199
    162             $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
    163         }
    164 
    165         if ( ! empty( $block['innerHTML'] ) ) {
    166             $this->inner_html = $block['innerHTML'];
    167         }
    168 
    169         if ( ! empty( $block['innerContent'] ) ) {
    170             $this->inner_content = $block['innerContent'];
     200            $this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $child_context, $this->registry );
     201        }
     202
     203        if ( ! empty( $this->parsed_block['innerHTML'] ) ) {
     204            $this->inner_html = $this->parsed_block['innerHTML'];
     205        }
     206
     207        if ( ! empty( $this->parsed_block['innerContent'] ) ) {
     208            $this->inner_content = $this->parsed_block['innerContent'];
    171209        }
    172210    }
     
    507545                        $block_content .= $pre_render;
    508546                    } else {
    509                         $source_block = $inner_block->parsed_block;
     547                        $source_block        = $inner_block->parsed_block;
     548                        $inner_block_context = $inner_block->context;
    510549
    511550                        /** This filter is documented in wp-includes/blocks.php */
     
    514553                        /** This filter is documented in wp-includes/blocks.php */
    515554                        $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
     555
     556                        /*
     557                         * The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`.
     558                         * Therefore the second condition is irrelevant if the first one is satisfied.
     559                         */
     560                        if ( $inner_block->context !== $inner_block_context ) {
     561                            $inner_block->refresh_context_dependents();