Make WordPress Core


Ignore:
Timestamp:
03/18/2025 12:41:31 PM (8 weeks ago)
Author:
joemcgill
Message:

Editor: Fix layout support classes to be generated with a stable ID.

This fixes a bug reported in https://github.com/WordPress/gutenberg/issues/67308 related to the Interactivity API's client-side navigation feature by replacing the incrementally generated IDs with stable hashes derived from the block's layout style definition.

Fixes #62985.
Props darerodz.

File:
1 edited

Legend:

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

    r58750 r60038  
    581581    // Child layout specific logic.
    582582    if ( $child_layout ) {
    583         $container_content_class   = wp_unique_prefixed_id( 'wp-container-content-' );
     583        /*
     584         * Generates a unique class for child block layout styles.
     585         *
     586         * To ensure consistent class generation across different page renders,
     587         * only properties that affect layout styling are used. These properties
     588         * come from `$block['attrs']['style']['layout']` and `$block['parentLayout']`.
     589         *
     590         * As long as these properties coincide, the generated class will be the same.
     591         */
     592        $container_content_class = wp_unique_id_from_values(
     593            array(
     594                'layout'       => array_intersect_key(
     595                    $block['attrs']['style']['layout'] ?? array(),
     596                    array_flip(
     597                        array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' )
     598                    )
     599                ),
     600                'parentLayout' => array_intersect_key(
     601                    $block['parentLayout'] ?? array(),
     602                    array_flip(
     603                        array( 'minimumColumnWidth', 'columnCount' )
     604                    )
     605                ),
     606            ),
     607            'wp-container-content-'
     608        );
     609
    584610        $child_layout_declarations = array();
    585611        $child_layout_styles       = array();
     
    707733    $layout_definitions = wp_get_layout_definitions();
    708734
    709     /*
    710      * Uses an incremental ID that is independent per prefix to make sure that
    711      * rendering different numbers of blocks doesn't affect the IDs of other
    712      * blocks. Makes the CSS class names stable across paginations
    713      * for features like the enhanced pagination of the Query block.
    714      */
    715     $container_class = wp_unique_prefixed_id(
    716         'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
    717     );
    718 
    719735    // Set the correct layout type for blocks using legacy content width.
    720736    if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
     
    806822            : null;
    807823        $has_block_gap_support = isset( $block_gap );
     824
     825        /*
     826         * Generates a unique ID based on all the data required to obtain the
     827         * corresponding layout style. Keeps the CSS class names the same
     828         * even for different blocks on different places, as long as they have
     829         * the same layout definition. Makes the CSS class names stable across
     830         * paginations for features like the enhanced pagination of the Query block.
     831         */
     832        $container_class = wp_unique_id_from_values(
     833            array(
     834                $used_layout,
     835                $has_block_gap_support,
     836                $gap_value,
     837                $should_skip_gap_serialization,
     838                $fallback_gap_value,
     839                $block_spacing,
     840            ),
     841            'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
     842        );
    808843
    809844        $style = wp_get_layout_style(
Note: See TracChangeset for help on using the changeset viewer.