Skip to content

Commit

Permalink
strip whitespaces in render_block_core_cover (#40859)
Browse files Browse the repository at this point in the history
Stopgap solution to remove whitespaces from html tags generated by render_block_core_covermarkup before injecting the featured image.

Follows on from [#39658]( #39658).
Remove whitespaces from html tags generated by `render_block_core_covermarkup` before injecting the featured image.

For the block layout see: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/cover/edit/index.js#L327-L367
For the HTML tags capturing by class name see: https://github.com/WordPress/gutenberg/blob/51db4bf888e6b18cf9d18266108114d61070f3ad/lib/block-supports/layout.php#L232

Co-authored-by: Adam Zielinski <adam@adamziel.com>
  • Loading branch information
navigatrum and adamziel authored Jun 27, 2022
1 parent 8006757 commit f2fa0c9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/block-library/src/cover/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ function render_block_core_cover( $attributes, $content ) {

$image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );

$content = str_replace(
'</span><div',
'</span>' . $image . '<div',
$content
);

/*
* Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`,
* and removes eventual withespace characters between the two (typically introduced at template level)
*/
$inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U';
if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) {
$offset = $matches[0][1];
$content = substr( $content, 0, $offset ) . $image . substr( $content, $offset );
}
} else {
if ( in_the_loop() ) {
update_post_thumbnail_cache();
Expand Down

0 comments on commit f2fa0c9

Please sign in to comment.