Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Post Featured Image: Fix height/scale overwriting img inline styles -…
… GB PR 44213
  • Loading branch information
dream-encode committed Oct 25, 2022
commit 09bc421c05d254c69ca040629b65e46302c8cf85
32 changes: 13 additions & 19 deletions src/wp-includes/blocks/post-featured-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
$attr['alt'] = $post_title;
}

if ( ! empty( $attributes['height'] ) ) {
$extra_styles = "height:{$attributes['height']};";

if ( ! empty( $attributes['scale'] ) ) {
$extra_styles .= "object-fit:{$attributes['scale']};";
}

$attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles;
}

$featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
if ( ! $featured_image ) {
return '';
}
$wrapper_attributes = get_block_wrapper_attributes();
if ( $is_link ) {
$link_target = $attributes['linkTarget'];
$rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
Expand All @@ -48,24 +57,9 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
} else {
$featured_image = $featured_image . $overlay_markup;
}

$has_width = ! empty( $attributes['width'] );
$has_height = ! empty( $attributes['height'] );
if ( ! $has_height && ! $has_width ) {
return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
}

if ( $has_width ) {
$wrapper_attributes = get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) );
}

if ( $has_height ) {
$image_styles = "height:{$attributes['height']};";
if ( ! empty( $attributes['scale'] ) ) {
$image_styles .= "object-fit:{$attributes['scale']};";
}
$featured_image = str_replace( 'src=', 'style="' . esc_attr( $image_styles ) . '" src=', $featured_image );
}
$wrapper_attributes = empty( $attributes['width'] )
? get_block_wrapper_attributes()
: get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) );

return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
}
Expand Down