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

Fix block gap in group blocks in classic themes #47451

Closed
wants to merge 9 commits into from
13 changes: 11 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ function gutenberg_restore_group_inner_container( $block_content, $block ) {
'/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
preg_quote( $tag_name, '/' )
);

if (
wp_theme_has_theme_json() ||
1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
Expand All @@ -836,14 +837,22 @@ function gutenberg_restore_group_inner_container( $block_content, $block ) {
return $block_content;
}

$constrained = ! wp_theme_has_theme_json() && ( isset( $block['attrs']['layout']['type'] ) && 'constrained' === $block['attrs']['layout']['type'] );
$constrained_class = $constrained ? ' is-layout-constrained' : '';
// 'is-layout-constrained' is not in the className attribute, so we need to remove it in a less optimal way.
// This needs to be replaced with a proper regex.
if ( true === $constrained ) {
$block_content = str_replace( 'is-layout-constrained ', '', $block_content );
}

$replace_regex = sprintf(
'/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
preg_quote( $tag_name, '/' )
);
$updated_content = preg_replace_callback(
$replace_regex,
static function ( $matches ) {
return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
static function ( $matches ) use ( $constrained_class ) {
return $matches[1] . '<div class="wp-block-group__inner-container' . $constrained_class . '">' . $matches[2] . '</div>' . $matches[3];
},
$block_content
);
Expand Down