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 10 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-' );

/*
* Uses an incremental ID that is independent per prefix to make sure that
* rendering different numbers of blocks doesn't affect the IDs of other
* blocks. Makes the CSS class names stable across paginations
* for features like the enhanced pagination of the Query block.
*/
$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
34 changes: 34 additions & 0 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7830,6 +7830,40 @@
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.0
*
* @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 ( ! is_string( $prefix ) ) {
wp_trigger_error(
__FUNCTION__,

Check failure on line 7852 in src/wp-includes/functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Multi-line function call not indented correctly; expected 12 spaces but found 16
sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) )

Check failure on line 7853 in src/wp-includes/functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Multi-line function call not indented correctly; expected 12 spaces but found 16
);
$prefix = '';
}

if ( ! isset( $id_counters[ $prefix ] ) ) {
$id_counters[ $prefix ] = 0;
}

$id = ++$id_counters[ $prefix ];

return $prefix . (string) $id;
}

/**
* 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
110 changes: 110 additions & 0 deletions tests/phpunit/tests/functions/wpUniquePrefixedId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

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

/**
* Tests that the expected unique prefixed IDs are created.
*
* @ticket 59681
*
* @dataProvider data_should_create_unique_prefixed_ids
*
* @param mixed $prefix The prefix.
* @param array $expected The next two expected IDs.
*/
public function test_should_create_unique_prefixed_ids( $prefix, $expected ) {
$id1 = wp_unique_prefixed_id( $prefix );
$id2 = wp_unique_prefixed_id( $prefix );

$this->assertNotSame( $id1, $id2, 'The IDs are not unique.' );
$this->assertSame( $expected, array( $id1, $id2 ), 'The IDs did not match the expected values.' );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_should_create_unique_prefixed_ids() {
return array(
'prefix as empty string' => array(
'prefix' => '',
'expected' => array( '1', '2' ),
),
'prefix as (string) "0"' => array(
'prefix' => '0',
'expected' => array( '01', '02' ),
),
'prefix as string' => array(
'prefix' => 'test',
'expected' => array( 'test1', 'test2' ),
),
'prefix as string with spaces' => array(
'prefix' => ' ',
'expected' => array( ' 1', ' 2' ),
),
'prefix as (string) "1"' => array(
'prefix' => '1',
'expected' => array( '11', '12' ),
),
Copy link
Contributor

@costdev costdev Oct 23, 2023

Choose a reason for hiding this comment

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

As wp_unique_prefixed_id() is a general function not specific to block names, it's possible that an ID may contain . and a number of other characters. Reference.

It's also possible that the "id" in question does not link directly to the HTML id attribute`.

Coverage should also be added for a variety of possible "id" values, such as (string) "0.0", etc. to protect behavior as this general function could be used to generate unique IDs for a variety of purposes/accepted formats.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure that can be added, though would be part of the prefix, and not the incremented integer value.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @costdev, commit 7a32e26 adds 2 more happy path datasets:

  • One for '.'
  • Another for the prefix with a block name.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks Tonya!

If there are any other potential "gotcha" values that a potential future refactor could regress, we should consider those too. The non-string dataset looks good for "gotcha" values, though I think we should complete the scalar datasets with (float) 0.0 and (float) 1.0.

Copy link
Contributor

Choose a reason for hiding this comment

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

@costdev 28cbcad adds a double dataset.

Copy link
Contributor

Choose a reason for hiding this comment

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

@costdev here's the added coverage we talked about in Make/Core slack DM 5151440.

);
}

/**
* @ticket 59681
*
* @dataProvider data_should_raise_notice_and_use_empty_string_prefix_when_nonstring_given
*
* @param mixed $non_string_prefix Non-string prefix.
* @param string $expected_message Expected notice message.
* @param string $expected_value Expected unique ID.
*/
public function test_should_raise_notice_and_use_empty_string_prefix_when_nonstring_given( $non_string_prefix, $expected_message, $expected_value ) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be a post commit cleanup, but If this test is ever run before test_should_create_unique_prefixed_ids then they will fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the order of the tests within the class does currently matter. The tests in this class runs in a separate process to avoid a situation where the static$id_counters already has values in it.

To handle the test run order within the class, could change that to run each test in a separate process. By doing so, the static $id_counters will always be reset to an empty array when each test starts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Commit 5aac204 runs each test dataset in a separate process to avoid test order concerns,

Copy link
Contributor

Choose a reason for hiding this comment

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

I also tweaked the unhappy path tests to ensure the IDs remain unique f4f99fc

How? By allowing each dataset to determine how many unique IDs to generate.

Why? Each of these non-string prefix datasets would have the same ID, as the prefix is omitted (empty string). One more check just to make sure.

$this->expectNotice();
$this->expectNoticeMessage( $expected_message );

$actual = wp_unique_prefixed_id( $non_string_prefix );
$this->assertSame( $expected_value, $actual );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_should_raise_notice_and_use_empty_string_prefix_when_nonstring_given() {
$message = 'wp_unique_prefixed_id(): The prefix must be a string. "%s" data type given.';

Check failure on line 86 in tests/phpunit/tests/functions/wpUniquePrefixedId.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after "="; 2 found
return array(
'prefix as null' => array(
'non_string_prefix' => null,
'expected_message' => sprintf( $message, 'NULL' ),
'expected_value' => '3',
),
'prefix as (int) 0' => array(
'non_string_prefix' => 0,
'expected_message' => sprintf( $message, 'integer' ),
'expected_value' => '4',
),
'prefix as (int) 1' => array(
'non_string_prefix' => 1,
'expected_data_type' => sprintf( $message, 'integer' ),
'expected_value' => '5',
),
'prefix as (bool) false' => array(
'non_string_prefix' => false,
'expected_data_type' => sprintf( $message, 'boolean' ),
'expected_value' => '6',
),
);
}
}
Loading