From 09bc421c05d254c69ca040629b65e46302c8cf85 Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Tue, 25 Oct 2022 12:23:37 -0400 Subject: [PATCH 1/3] Post Featured Image: Fix height/scale overwriting img inline styles - GB PR 44213 --- .../blocks/post-featured-image.php | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/blocks/post-featured-image.php b/src/wp-includes/blocks/post-featured-image.php index de5683b297027..efc8613c77e43 100644 --- a/src/wp-includes/blocks/post-featured-image.php +++ b/src/wp-includes/blocks/post-featured-image.php @@ -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'] ) . '"' : ''; @@ -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 "
{$featured_image}
"; - } - - 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 "
{$featured_image}
"; } From e46f570acecaeac279025b631cbf79f51bc19da3 Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Tue, 25 Oct 2022 13:25:54 -0400 Subject: [PATCH 2/3] Revert "Post Featured Image: Fix height/scale overwriting img inline styles - GB PR 44213" This reverts commit 09bc421c05d254c69ca040629b65e46302c8cf85. --- .../blocks/post-featured-image.php | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/blocks/post-featured-image.php b/src/wp-includes/blocks/post-featured-image.php index efc8613c77e43..de5683b297027 100644 --- a/src/wp-includes/blocks/post-featured-image.php +++ b/src/wp-includes/blocks/post-featured-image.php @@ -29,20 +29,11 @@ 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'] ) . '"' : ''; @@ -57,9 +48,24 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) } else { $featured_image = $featured_image . $overlay_markup; } - $wrapper_attributes = empty( $attributes['width'] ) - ? get_block_wrapper_attributes() - : get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) ); + + $has_width = ! empty( $attributes['width'] ); + $has_height = ! empty( $attributes['height'] ); + if ( ! $has_height && ! $has_width ) { + return "
{$featured_image}
"; + } + + 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 ); + } return "
{$featured_image}
"; } From 23c87bd03f285e5e486726223cbb33ec149b6cf3 Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Tue, 25 Oct 2022 13:28:38 -0400 Subject: [PATCH 3/3] Featured Image Block: Add missing output escaping - GB PR 45163 --- src/wp-includes/blocks/post-featured-image.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks/post-featured-image.php b/src/wp-includes/blocks/post-featured-image.php index de5683b297027..495c8ec534a41 100644 --- a/src/wp-includes/blocks/post-featured-image.php +++ b/src/wp-includes/blocks/post-featured-image.php @@ -64,7 +64,7 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) if ( ! empty( $attributes['scale'] ) ) { $image_styles .= "object-fit:{$attributes['scale']};"; } - $featured_image = str_replace( 'src=', 'style="' . esc_attr( $image_styles ) . '" src=', $featured_image ); + $featured_image = str_replace( '{$featured_image}";