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

Try adding layout support to Cover block. #43504

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 48 additions & 8 deletions lib/block-supports/layout.php
Expand Up @@ -284,6 +284,29 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
return '';
}

/**
* Adds an identifying classname to the inner block wrapper.
*
* @param array $parsed_block A parsed block object.
* @return array The updated block object.
*/
function gutenberg_identify_inner_block_wrapper( $parsed_block ) {

if ( ! isset( $parsed_block['innerContent'][0] ) ) {
return $parsed_block;
}

$inner_class_position = strrpos( $parsed_block['innerContent'][0], 'class="' );

if ( ! $inner_class_position ) {
return $parsed_block;
}

$parsed_block['innerContent'][0] = substr_replace( $parsed_block['innerContent'][0], 'class="is-inner-block-wrapper ', $inner_class_position, strlen( 'class="' ) );

return $parsed_block;
}

/**
* Renders the layout config to the block wrapper.
*
Expand Down Expand Up @@ -387,14 +410,25 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
}
}

// This assumes the hook only applies to blocks with a single wrapper.
// I think this is a reasonable limitation for that particular hook.
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="' . esc_attr( implode( ' ', $class_names ) ) . ' ',
$block_content,
1
);
// Ideally we'll be able to attach this class to all inner block wrappers and
// we won't need the condition or the fallback.
if ( strpos( $block_content, 'is-inner-block-wrapper' ) ) {
$content = preg_replace(
'/' . preg_quote( 'is-inner-block-wrapper', '/' ) . '/',
esc_attr( implode( ' ', $class_names ) ),
$block_content,
1
);
} else {
// This assumes the hook only applies to blocks with a single wrapper.
// I think this is a reasonable limitation for that particular hook.
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="' . esc_attr( implode( ' ', $class_names ) ) . ' ',
$block_content,
1
);
}

return $content;
}
Expand All @@ -406,6 +440,12 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
'register_attribute' => 'gutenberg_register_layout_support',
)
);

if ( function_exists( 'wp_identify_inner_block_wrapper' ) ) {
remove_filter( 'render_block', 'wp_identify_inner_block_wrapper' );
}
add_filter( 'render_block_data', 'gutenberg_identify_inner_block_wrapper' );

if ( function_exists( 'wp_render_layout_support_flag' ) ) {
remove_filter( 'render_block', 'wp_render_layout_support_flag' );
}
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/cover/block.json
Expand Up @@ -105,6 +105,13 @@
"__experimentalDefaultControls": {
"fontSize": true
}
},
"__experimentalLayout": {
"allowSwitching": true,
"allowInheriting": false,
"default": {
"type": "flow"
}
}
},
"editorStyle": "wp-block-cover-editor",
Expand Down