@@ -194,13 +194,21 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
194
194
195
195
case 'left ' :
196
196
case 'right ' :
197
- $ layout_width = sprintf ( '%1$spx ' , $ image_width );
198
- break ;
199
-
200
197
case 'center ' :
201
198
default :
202
- $ alignment = auto_sizes_get_layout_width ( 'default ' );
203
- $ layout_width = sprintf ( '%1$spx ' , min ( (int ) $ alignment , $ image_width ) );
199
+ $ layout_alignment = in_array ( $ alignment , array ( 'left ' , 'right ' ), true ) ? 'wide ' : 'default ' ;
200
+ $ layout_width = auto_sizes_get_layout_width ( $ layout_alignment );
201
+
202
+ /*
203
+ * If the layout width is in pixels, we can compare against the image width
204
+ * on the server. Otherwise, we need to rely on CSS functions.
205
+ */
206
+ if ( str_ends_with ( $ layout_width , 'px ' ) ) {
207
+ $ layout_width = sprintf ( '%1$spx ' , min ( (int ) $ layout_width , $ image_width ) );
208
+ } else {
209
+ $ layout_width = sprintf ( 'min(%1$s, %2$spx) ' , $ layout_width , $ image_width );
210
+ }
211
+
204
212
break ;
205
213
}
206
214
@@ -212,14 +220,14 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
212
220
return $ layout_width ;
213
221
}
214
222
215
- /**
216
- * Retrieves the layout width for an alignment defined in theme.json.
217
- *
218
- * @since n.e.x.t
219
- *
220
- * @param string $alignment The alignment value.
221
- * @return string The alignment width based.
222
- */
223
+ /**
224
+ * Retrieves the layout width for an alignment defined in theme.json.
225
+ *
226
+ * @since n.e.x.t
227
+ *
228
+ * @param string $alignment The alignment value.
229
+ * @return string The alignment width based.
230
+ */
223
231
function auto_sizes_get_layout_width ( string $ alignment ): string {
224
232
$ layout = auto_sizes_get_layout_settings ();
225
233
@@ -232,15 +240,15 @@ function auto_sizes_get_layout_width( string $alignment ): string {
232
240
return $ layout_widths [ $ alignment ] ?? '' ;
233
241
}
234
242
235
- /**
236
- * Filters the context keys that a block type uses.
237
- *
238
- * @since n.e.x.t
239
- *
240
- * @param string[] $uses_context Array of registered uses context for a block type.
241
- * @param WP_Block_Type $block_type The full block type object.
242
- * @return string[] The filtered context keys used by the block type.
243
- */
243
+ /**
244
+ * Filters the context keys that a block type uses.
245
+ *
246
+ * @since n.e.x.t
247
+ *
248
+ * @param string[] $uses_context Array of registered uses context for a block type.
249
+ * @param WP_Block_Type $block_type The full block type object.
250
+ * @return string[] The filtered context keys used by the block type.
251
+ */
244
252
function auto_sizes_filter_uses_context ( array $ uses_context , WP_Block_Type $ block_type ): array {
245
253
// The list of blocks that can consume outer layout context.
246
254
$ consumer_blocks = array (
@@ -255,15 +263,15 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo
255
263
return $ uses_context ;
256
264
}
257
265
258
- /**
259
- * Modifies the block context during rendering to blocks.
260
- *
261
- * @since n.e.x.t
262
- *
263
- * @param array<string, mixed> $context Current block context.
264
- * @param array<string, mixed> $block The block being rendered.
265
- * @return array<string, mixed> Modified block context.
266
- */
266
+ /**
267
+ * Modifies the block context during rendering to blocks.
268
+ *
269
+ * @since n.e.x.t
270
+ *
271
+ * @param array<string, mixed> $context Current block context.
272
+ * @param array<string, mixed> $block The block being rendered.
273
+ * @return array<string, mixed> Modified block context.
274
+ */
267
275
function auto_sizes_filter_render_block_context ( array $ context , array $ block ): array {
268
276
// When no max alignment is set, the maximum is assumed to be 'full'.
269
277
$ context ['max_alignment ' ] = $ context ['max_alignment ' ] ?? 'full ' ;
@@ -288,13 +296,13 @@ function auto_sizes_filter_render_block_context( array $context, array $block ):
288
296
return $ context ;
289
297
}
290
298
291
- /**
292
- * Retrieves the layout settings defined in theme.json.
293
- *
294
- * @since n.e.x.t
295
- *
296
- * @return array<string, mixed> Associative array of layout settings.
297
- */
299
+ /**
300
+ * Retrieves the layout settings defined in theme.json.
301
+ *
302
+ * @since n.e.x.t
303
+ *
304
+ * @return array<string, mixed> Associative array of layout settings.
305
+ */
298
306
function auto_sizes_get_layout_settings (): array {
299
307
static $ layout = array ();
300
308
if ( count ( $ layout ) === 0 ) {
0 commit comments