Skip to content
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

Make layout supports compatible with enhanced pagination #5528

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/wp-includes/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,16 @@ function wp_render_layout_support_flag( $block_content, $block ) {

$class_names = array();
$layout_definitions = wp_get_layout_definitions();
$container_class = wp_unique_id( 'wp-container-' );

/*
* We use an incremental ID that is independent per prefix to make sure that
cbravobernal marked this conversation as resolved.
Show resolved Hide resolved
* rendering different numbers of blocks doesn't affect the IDs of other
* blocks. We need this to make the CSS class names stable across paginations
cbravobernal marked this conversation as resolved.
Show resolved Hide resolved
* for features like the enhanced pagination of the Query block.
*/
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
$container_class = wp_unique_prefixed_id(
'wp-container-' . sanitize_title( $block['blockName'] ) . '-layout-'
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
);

// Set the correct layout type for blocks using legacy content width.
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
Expand Down
22 changes: 22 additions & 0 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7830,6 +7830,28 @@ function wp_unique_id( $prefix = '' ) {
return $prefix . (string) ++$id_counter;
}

/**
* Generates an incremental ID that is independent per each different prefix.
*
* It is similar to `wp_unique_id`, but each prefix has its own internal ID
* counter to make each prefix independent from each other. The ID starts at 1
* and increments on each call. The returned value is not universally unique,
* but it is unique across the life of the PHP process and it's stable per
* prefix.
*
* @since 6.4
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $prefix Optional. Prefix for the returned ID. Default empty string.
* @return string Incremental ID per prefix.
*/
function wp_unique_prefixed_id( $prefix = '' ) {
aaronjorbin marked this conversation as resolved.
Show resolved Hide resolved
static $id_counters = array();
if ( ! array_key_exists( $prefix, $id_counters ) ) {
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
$id_counters[ $prefix ] = 0;
}
return $prefix . (string) ++$id_counters[ $prefix ];
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I find the incrementing inside the return to be a bit harder to read. I wonder if it might be better to increment and then return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's consistent with wp_unique_id(). That said, I thought the same thing : separate the increment from the string build/return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can refactor both in 6.5, as it is code quality. I can leave the PR ready after the RC, pointing trunk branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what makes it less readable as compared to wp_unique_id() is the array lookup. To help, split the increment in 11f1cac.

}

/**
* Gets last changed date for the specified cache group.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/blocks/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ public function test_do_block_output( $html_filename, $server_html_filename ) {

$html = do_blocks( self::strip_r( file_get_contents( $html_path ) ) );
// If blocks opt into Gutenberg's layout implementation
// the container will receive an added classname of `wp_unique_id( 'wp-container-' )`
// the container will receive an additional, unique classname based on "wp-container-[blockname]-layout"
// so we need to normalize the random id.
$normalized_html = preg_replace( '/wp-container-\d+/', 'wp-container-1', $html );
$normalized_html = preg_replace( '/wp-container-[a-z-]+\d+/', 'wp-container-1', $html );

// The gallery block uses a unique class name of `wp_unique_id( 'wp-block-gallery-' )`
// so we need to normalize the random id.
Expand Down
26 changes: 26 additions & 0 deletions tests/phpunit/tests/functions/wpUniquePrefixedId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Test cases for the `wp_unique_prefixed_id()` function.
*
* @package WordPress\UnitTests
*
* @since 6.4
*
* @group functions.php
* @covers ::wp_unique_prefixed_id
*/
class Tests_Functions_WpUniquePrefixedId extends WP_UnitTestCase {

public function test_wp_unique_prefixed_id() {
$first = wp_unique_prefixed_id( 'test' );
$second = wp_unique_prefixed_id( 'test' );
$null_id = wp_unique_prefixed_id( null );
$second_null_id = wp_unique_prefixed_id( null );
$default = wp_unique_prefixed_id();
$second_default = wp_unique_prefixed_id();
$this->assertNotEquals( $first, $second );
$this->assertNotEquals( $default, $second_default );
$this->assertNotEquals( $null_id, $second_null_id );
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading