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 support compatible with enhanced pagination #55416

Merged
Merged
Changes from all 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
31 changes: 30 additions & 1 deletion lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,26 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
return '';
}

/**
* Generates an incremental ID that is independent per each different prefix.
*
* It is similar to `wp_unique_id`, but each prefix has it's 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.
*
* @param string $prefix Prefix for the returned ID.
* @return string Incremental ID per prefix.
*/
function gutenberg_incremental_id_per_prefix( $prefix = '' ) {
static $id_counters = array();
if ( ! array_key_exists( $prefix, $id_counters ) ) {
$id_counters[ $prefix ] = 0;
}
return $prefix . (string) ++$id_counters[ $prefix ];
}

/**
* Renders the layout config to the block wrapper.
*
Expand Down Expand Up @@ -608,7 +628,16 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {

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

/*
* We use 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. We need this to make the CSS class names stable across paginations
* for features like the enhanced pagination of the Query block.
*/
$container_class = gutenberg_incremental_id_per_prefix(
'wp-container-' . sanitize_title( $block['blockName'] ) . '-layout-'
);

// 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
Loading