Skip to content

Commit

Permalink
Use get_the_post_thumbnail function in the cover block. (#40853)
Browse files Browse the repository at this point in the history
* Improve logic.

* Improve logic.

* Coding Standards: Using single-quotes if double-quotes are not required

Co-authored-by: Ari Stathopoulos <[email protected]>
  • Loading branch information
spacedmonkey and aristath committed May 6, 2022
1 parent a50c717 commit e1bb280
Showing 1 changed file with 20 additions and 37 deletions.
57 changes: 20 additions & 37 deletions packages/block-library/src/cover/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,41 @@
* @return string Returns the cover block markup, if useFeaturedImage is true.
*/
function render_block_core_cover( $attributes, $content ) {
if ( false === $attributes['useFeaturedImage'] ) {
if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) {
return $content;
}

$current_featured_image = get_the_post_thumbnail_url();

if ( false === $current_featured_image ) {
return $content;
}

$is_img_element = ! ( $attributes['hasParallax'] || $attributes['isRepeated'] );
$is_image_background = 'image' === $attributes['backgroundType'];

if ( $is_image_background && ! $is_img_element ) {
$content = preg_replace(
'/class=\".*?\"/',
'${0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"',
$content,
1
if ( ! ( $attributes['hasParallax'] || $attributes['isRepeated'] ) ) {
$attr = array(
'class' => 'wp-block-cover__image-background',
'data-object-fit' => 'cover',
);
}

if ( $is_image_background && $is_img_element ) {
$object_position = '';
if ( isset( $attributes['focalPoint'] ) ) {
$object_position = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' .
round( $attributes['focalPoint']['y'] * 100 ) . '%';
$object_position = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
$attr['data-object-position'] = $object_position;
$attr['style'] = 'object-position: ' . $object_position;
}

$image_template = '<img
class="wp-block-cover__image-background"
alt="%s"
src="%s"
style="object-position: %s"
data-object-fit="cover"
data-object-position="%s"
/>';

$image = sprintf(
$image_template,
esc_attr( get_the_post_thumbnail_caption() ),
esc_url( $current_featured_image ),
esc_attr( $object_position ),
esc_attr( $object_position )
);
$image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );

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

} else {
if ( in_the_loop() ) {
update_post_thumbnail_cache();
}
$current_featured_image = get_the_post_thumbnail_url();
$content = preg_replace(
'/class=\".*?\"/',
'${0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"',
$content,
1
);
}

return $content;
Expand Down

0 comments on commit e1bb280

Please sign in to comment.