Skip to content

Commit aecb3de

Browse files
committed
Force resize width to be an int
1 parent 266e457 commit aecb3de

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
8282
*/
8383
$filter = static function ( $sizes, $size ) use ( $block ) {
8484

85-
$id = $block->attributes['id'] ?? 0;
85+
$id = isset( $block->attributes['id'] ) ? (int) $block->attributes['id'] : 0;
8686
$alignment = $block->attributes['align'] ?? '';
87-
$width = $block->attributes['width'] ?? '';
87+
$width = isset( $block->attributes['width'] ) ? (int) $block->attributes['width'] : 0;
8888
$max_alignment = $block->context['max_alignment'] ?? '';
8989

9090
/*
@@ -95,7 +95,7 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
9595
$size = array( 420, 420 );
9696
}
9797

98-
$better_sizes = auto_sizes_calculate_better_sizes( (int) $id, $size, (string) $alignment, (string) $width, (string) $max_alignment );
98+
$better_sizes = auto_sizes_calculate_better_sizes( $id, $size, $alignment, $width, $max_alignment );
9999

100100
// If better sizes can't be calculated, use the default sizes.
101101
return false !== $better_sizes ? $better_sizes : $sizes;
@@ -135,33 +135,32 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
135135
* @param int $id The image attachment post ID.
136136
* @param string|array{int, int} $size Image size name or array of width and height.
137137
* @param string $align The image alignment.
138-
* @param string $resize_width Resize image width.
138+
* @param int $resize_width Resize image width.
139139
* @param string $max_alignment The maximum usable layout alignment.
140140
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
141141
*/
142-
function auto_sizes_calculate_better_sizes( int $id, $size, string $align, string $resize_width, string $max_alignment ) {
142+
function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $resize_width, string $max_alignment ) {
143143
// Without an image ID or a resize width, we cannot calculate a better size.
144-
if ( ! (bool) $id && ! (bool) $resize_width ) {
144+
if ( 0 === $id && 0 === $resize_width ) {
145145
return false;
146146
}
147147

148148
$image_data = wp_get_attachment_image_src( $id, $size );
149149

150-
$resize_width = (int) $resize_width;
151-
$image_width = false !== $image_data ? $image_data[1] : 0;
150+
$image_width = false !== $image_data ? $image_data[1] : 0;
152151

153152
// If we don't have an image width or a resize width, we cannot calculate a better size.
154-
if ( ! ( (bool) $image_width || (bool) $resize_width ) ) {
153+
if ( 0 === $image_width && 0 === $resize_width ) {
155154
return false;
156155
}
157156

158157
/*
159158
* If we don't have an image width, use the resize width.
160159
* If we have both an image width and a resize width, use the smaller of the two.
161160
*/
162-
if ( ! (bool) $image_width ) {
161+
if ( 0 === $image_width ) {
163162
$image_width = $resize_width;
164-
} elseif ( (bool) $resize_width ) {
163+
} elseif ( 0 !== $resize_width ) {
165164
$image_width = min( $image_width, $resize_width );
166165
}
167166

0 commit comments

Comments
 (0)