@@ -95,10 +95,11 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
95
95
*/
96
96
$ filter = static function ( $ sizes , $ size ) use ( $ block ) {
97
97
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 ;
102
103
103
104
/*
104
105
* Update width for cover block.
@@ -108,7 +109,7 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
108
109
$ size = array ( 420 , 420 );
109
110
}
110
111
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 );
112
113
113
114
// If better sizes can't be calculated, use the default sizes.
114
115
return false !== $ better_sizes ? $ better_sizes : $ sizes ;
@@ -145,14 +146,15 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
145
146
*
146
147
* @since 1.4.0
147
148
*
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.
153
155
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
154
156
*/
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 ) {
156
158
// Bail early if not a block theme.
157
159
if ( ! wp_is_block_theme () ) {
158
160
return false ;
@@ -198,6 +200,18 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
198
200
199
201
case 'wide ' :
200
202
$ 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
+ }
201
215
break ;
202
216
203
217
case 'left ' :
@@ -210,8 +224,18 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
210
224
/*
211
225
* If the layout width is in pixels, we can compare against the image width
212
226
* on the server. Otherwise, we need to rely on CSS functions.
227
+ *
228
+ * TODO: Add support for em, rem, vh, and vw.
213
229
*/
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 ;
215
239
$ layout_width = sprintf ( '%dpx ' , min ( (int ) $ layout_width , $ image_width ) );
216
240
} else {
217
241
$ 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, ?
324
348
}
325
349
326
350
// 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
+ ) {
328
355
$ context ['container_relative_width ' ] = $ parent_block ->context ['container_relative_width ' ] * $ current_width ;
329
356
} else {
330
357
$ context ['container_relative_width ' ] = $ current_width ;
0 commit comments