Skip to content

Commit c9e98f3

Browse files
Merge pull request #1818 from WordPress/fix/add-column-block-context
Accurate sizes: Get accurate sizes base on ancestor block context
2 parents 2c76736 + 80cc029 commit c9e98f3

File tree

2 files changed

+708
-13
lines changed

2 files changed

+708
-13
lines changed

plugins/auto-sizes/includes/improve-calculate-sizes.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
9595
*/
9696
$filter = static function ( $sizes, $size ) use ( $block ) {
9797

98-
$id = isset( $block->attributes['id'] ) ? (int) $block->attributes['id'] : 0;
99-
$alignment = $block->attributes['align'] ?? '';
100-
$width = isset( $block->attributes['width'] ) ? (int) $block->attributes['width'] : 0;
101-
$max_alignment = $block->context['max_alignment'] ?? '';
98+
$id = isset( $block->attributes['id'] ) ? (int) $block->attributes['id'] : 0;
99+
$alignment = $block->attributes['align'] ?? '';
100+
$width = isset( $block->attributes['width'] ) ? (int) $block->attributes['width'] : 0;
101+
$max_alignment = $block->context['max_alignment'] ?? '';
102+
$container_relative_width = $block->context['container_relative_width'] ?? 1.0;
102103

103104
/*
104105
* Update width for cover block.
@@ -108,7 +109,7 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
108109
$size = array( 420, 420 );
109110
}
110111

111-
$better_sizes = auto_sizes_calculate_better_sizes( $id, $size, $alignment, $width, $max_alignment );
112+
$better_sizes = auto_sizes_calculate_better_sizes( $id, $size, $alignment, $width, $max_alignment, $container_relative_width );
112113

113114
// If better sizes can't be calculated, use the default sizes.
114115
return false !== $better_sizes ? $better_sizes : $sizes;
@@ -145,14 +146,15 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
145146
*
146147
* @since 1.4.0
147148
*
148-
* @param int $id The image attachment post ID.
149-
* @param string|array{int, int} $size Image size name or array of width and height.
150-
* @param string $align The image alignment.
151-
* @param int $resize_width Resize image width.
152-
* @param string $max_alignment The maximum usable layout alignment.
149+
* @param int $id The image attachment post ID.
150+
* @param string|array{int, int} $size Image size name or array of width and height.
151+
* @param string $align The image alignment.
152+
* @param int $resize_width Resize image width.
153+
* @param string $max_alignment The maximum usable layout alignment.
154+
* @param float $container_relative_width Container relative width.
153155
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
154156
*/
155-
function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $resize_width, string $max_alignment ) {
157+
function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $resize_width, string $max_alignment, float $container_relative_width ) {
156158
// Bail early if not a block theme.
157159
if ( ! wp_is_block_theme() ) {
158160
return false;
@@ -198,6 +200,18 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
198200

199201
case 'wide':
200202
$layout_width = auto_sizes_get_layout_width( 'wide' );
203+
// TODO: Add support for em, rem, vh, and vw.
204+
if (
205+
str_ends_with( $layout_width, 'px' ) &&
206+
( $container_relative_width > 0.0 ||
207+
$container_relative_width < 1.0 )
208+
) {
209+
// First remove 'px' from width.
210+
$layout_width = str_replace( 'px', '', $layout_width );
211+
// Convert to float for better precision.
212+
$layout_width = (float) $layout_width * $container_relative_width;
213+
$layout_width = sprintf( '%dpx', (int) $layout_width );
214+
}
201215
break;
202216

203217
case 'left':
@@ -210,8 +224,18 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
210224
/*
211225
* If the layout width is in pixels, we can compare against the image width
212226
* on the server. Otherwise, we need to rely on CSS functions.
227+
*
228+
* TODO: Add support for em, rem, vh, and vw.
213229
*/
214-
if ( str_ends_with( $layout_width, 'px' ) ) {
230+
if (
231+
str_ends_with( $layout_width, 'px' ) &&
232+
( $container_relative_width > 0.0 ||
233+
$container_relative_width < 1.0 )
234+
) {
235+
// First remove 'px' from width.
236+
$layout_width = str_replace( 'px', '', $layout_width );
237+
// Convert to float for better precision.
238+
$layout_width = (float) $layout_width * $container_relative_width;
215239
$layout_width = sprintf( '%dpx', min( (int) $layout_width, $image_width ) );
216240
} else {
217241
$layout_width = sprintf( 'min(%1$s, %2$spx)', $layout_width, $image_width );
@@ -324,7 +348,10 @@ function auto_sizes_filter_render_block_context( array $context, array $block, ?
324348
}
325349

326350
// Multiply with parent's width if available.
327-
if ( isset( $parent_block->context['container_relative_width'] ) ) {
351+
if (
352+
isset( $parent_block->context['container_relative_width'] ) &&
353+
( $current_width > 0.0 || $current_width < 1.0 )
354+
) {
328355
$context['container_relative_width'] = $parent_block->context['container_relative_width'] * $current_width;
329356
} else {
330357
$context['container_relative_width'] = $current_width;

0 commit comments

Comments
 (0)