Skip to content

Commit

Permalink
Make layout support compatible with enhanced pagination (#55416)
Browse files Browse the repository at this point in the history
* Make layout supports compatible with enhanced pagination

* Use sanitize_title and add `layout` to the class name
  • Loading branch information
luisherranz authored and Siobhan committed Oct 19, 2023
1 parent 042dfed commit 3735244
Showing 1 changed file with 30 additions and 1 deletion.
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

0 comments on commit 3735244

Please sign in to comment.