Make WordPress Core

Changeset 56994


Ignore:
Timestamp:
10/24/2023 08:49:38 AM (14 months ago)
Author:
Bernhard Reiter
Message:

Blocks: Fix layout support to be compatible with enhanced pagination.

Make layout support compatible with enhanced pagination by ensuring that generated class names are stable across pagination, even when the number of rendered posts is different.

With the previous implementation of enhanced pagination, the CSS corresponding to each block was not detected. Therefore, for enhanced pagination to work correctly, the CSS of the blocks present in the Post Template must be stable on all pages.

The number of posts rendered by the Query block is always the same, except in the last page, where it can be only a fraction. If any of the blocks rendered by the Post Template used the wp_unique_id function, the ID (which is incremental) would have been different than in the previous pages and the class names would have varied.

This is remediated by this changeset by replacing the usage of wp_unique_id in the layout support (which is used by the Query block) with an implementation that uses IDs that are incremental only for that block. That way, the generated class names are never affected by the number of times wp_unique_id runs.

Props luisherranz, andrewserong, isabel_brison, costdev, mukesh27, cbravobernal, hellofromTonya, jorbin.
Fixes #59681.

Location:
trunk
Files:
1 added
3 edited

Legend:

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

    r56709 r56994  
    631631    $class_names        = array();
    632632    $layout_definitions = wp_get_layout_definitions();
    633     $container_class    = wp_unique_id( 'wp-container-' );
     633
     634    /*
     635     * Uses an incremental ID that is independent per prefix to make sure that
     636     * rendering different numbers of blocks doesn't affect the IDs of other
     637     * blocks. Makes the CSS class names stable across paginations
     638     * for features like the enhanced pagination of the Query block.
     639     */
     640    $container_class = wp_unique_prefixed_id(
     641        'wp-container-' . sanitize_title( $block['blockName'] ) . '-layout-'
     642    );
    634643
    635644    // Set the correct layout type for blocks using legacy content width.
  • trunk/src/wp-includes/functions.php

    r56809 r56994  
    78327832
    78337833/**
     7834 * Generates an incremental ID that is independent per each different prefix.
     7835 *
     7836 * It is similar to `wp_unique_id`, but each prefix has its own internal ID
     7837 * counter to make each prefix independent from each other. The ID starts at 1
     7838 * and increments on each call. The returned value is not universally unique,
     7839 * but it is unique across the life of the PHP process and it's stable per
     7840 * prefix.
     7841 *
     7842 * @since 6.4.0
     7843 *
     7844 * @param string $prefix Optional. Prefix for the returned ID. Default empty string.
     7845 * @return string Incremental ID per prefix.
     7846 */
     7847function wp_unique_prefixed_id( $prefix = '' ) {
     7848    static $id_counters = array();
     7849
     7850    if ( ! is_string( $prefix ) ) {
     7851        wp_trigger_error(
     7852            __FUNCTION__,
     7853            sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) )
     7854        );
     7855        $prefix = '';
     7856    }
     7857
     7858    if ( ! isset( $id_counters[ $prefix ] ) ) {
     7859        $id_counters[ $prefix ] = 0;
     7860    }
     7861
     7862    $id = ++$id_counters[ $prefix ];
     7863
     7864    return $prefix . (string) $id;
     7865}
     7866
     7867/**
    78347868 * Gets last changed date for the specified cache group.
    78357869 *
  • trunk/tests/phpunit/tests/blocks/render.php

    r56547 r56994  
    219219        $html = do_blocks( self::strip_r( file_get_contents( $html_path ) ) );
    220220        // If blocks opt into Gutenberg's layout implementation
    221         // the container will receive an added classname of `wp_unique_id( 'wp-container-' )`
     221        // the container will receive an additional, unique classname based on "wp-container-[blockname]-layout"
    222222        // so we need to normalize the random id.
    223         $normalized_html = preg_replace( '/wp-container-\d+/', 'wp-container-1', $html );
     223        $normalized_html = preg_replace( '/wp-container-[a-z-]+\d+/', 'wp-container-1', $html );
    224224
    225225        // The gallery block uses a unique class name of `wp_unique_id( 'wp-block-gallery-' )`
Note: See TracChangeset for help on using the changeset viewer.