@@ -101,12 +101,12 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
101
101
*/
102
102
$ filter = static function ( $ sizes , $ size ) use ( $ block ) {
103
103
104
- $ id = $ block ->attributes ['id ' ] ?? 0 ;
105
- $ alignment = $ block ->attributes ['align ' ] ?? '' ;
106
- $ width = $ block ->attributes ['width ' ] ?? '' ;
107
- $ ancestor_block_align = $ block ->context ['ancestor_block_align ' ] ?? ' full ' ;
104
+ $ id = $ block ->attributes ['id ' ] ?? 0 ;
105
+ $ alignment = $ block ->attributes ['align ' ] ?? '' ;
106
+ $ width = $ block ->attributes ['width ' ] ?? '' ;
107
+ $ max_alignment = $ block ->context ['max_alignment ' ] ;
108
108
109
- $ better_sizes = auto_sizes_calculate_better_sizes ( (int ) $ id , (string ) $ size , (string ) $ alignment , (string ) $ width , (string ) $ ancestor_block_align );
109
+ $ better_sizes = auto_sizes_calculate_better_sizes ( (int ) $ id , (string ) $ size , (string ) $ alignment , (string ) $ width , (string ) $ max_alignment );
110
110
111
111
// If better sizes can't be calculated, use the default sizes.
112
112
return false !== $ better_sizes ? $ better_sizes : $ sizes ;
@@ -147,10 +147,10 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
147
147
* @param string $size The image size data.
148
148
* @param string $align The image alignment.
149
149
* @param string $resize_width Resize image width.
150
- * @param string $ancestor_block_align The ancestor block alignment.
150
+ * @param string $max_alignment The maximum usable layout alignment.
151
151
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
152
152
*/
153
- function auto_sizes_calculate_better_sizes ( int $ id , string $ size , string $ align , string $ resize_width , string $ ancestor_block_align ) {
153
+ function auto_sizes_calculate_better_sizes ( int $ id , string $ size , string $ align , string $ resize_width , string $ max_alignment ) {
154
154
// Without an image ID or a resize width, we cannot calculate a better size.
155
155
if ( ! (bool ) $ id && ! (bool ) $ resize_width ) {
156
156
return false ;
@@ -177,11 +177,13 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
177
177
}
178
178
179
179
// Normalize default alignment values.
180
- $ align = '' !== $ align ? $ align : 'default ' ;
181
- $ ancestor_block_align = '' !== $ ancestor_block_align ? $ ancestor_block_align : 'default ' ;
180
+ $ align = (bool ) $ align ? $ align : 'default ' ;
182
181
183
- // We'll choose which alignment to use, based on which is more constraining.
184
- $ constraint = array (
182
+ /*
183
+ * Map alignment values to a weighting value so they can be compared.
184
+ * Note that 'left', 'right', and 'center' alignments are only constrained by default containers.
185
+ */
186
+ $ constraints = array (
185
187
'full ' => 0 ,
186
188
'wide ' => 1 ,
187
189
'left ' => 2 ,
@@ -190,43 +192,7 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
190
192
'default ' => 3 ,
191
193
);
192
194
193
- $ alignment = $ constraint [ $ align ] > $ constraint [ $ ancestor_block_align ] ? $ align : $ ancestor_block_align ;
194
-
195
- return auto_sizes_calculate_width ( $ alignment , $ image_width , $ ancestor_block_align );
196
- }
197
-
198
- /**
199
- * Retrieves the layout width for an alignment defined in theme.json.
200
- *
201
- * @since n.e.x.t
202
- *
203
- * @param string $alignment The alignment value.
204
- * @return string The alignment width based.
205
- */
206
- function auto_sizes_get_layout_width ( string $ alignment ): string {
207
- $ layout = auto_sizes_get_layout_settings ();
208
-
209
- $ layout_widths = array (
210
- 'full ' => '100vw ' ,
211
- 'wide ' => array_key_exists ( 'wideSize ' , $ layout ) ? $ layout ['wideSize ' ] : '' ,
212
- 'default ' => array_key_exists ( 'contentSize ' , $ layout ) ? $ layout ['contentSize ' ] : '' ,
213
- );
214
-
215
- return $ layout_widths [ $ alignment ] ?? '' ;
216
- }
217
-
218
- /**
219
- * Calculates the width value for the `sizes` attribute based on block information.
220
- *
221
- * @since n.e.x.t
222
- *
223
- * @param string $alignment The alignment.
224
- * @param int $image_width The image width.
225
- * @param string $ancestor_alignment The ancestor alignment.
226
- * @return string The calculated width value.
227
- */
228
- function auto_sizes_calculate_width ( string $ alignment , int $ image_width , string $ ancestor_alignment ): string {
229
- $ sizes = '' ;
195
+ $ alignment = $ constraints [ $ align ] > $ constraints [ $ max_alignment ] ? $ align : $ max_alignment ;
230
196
231
197
// Handle different alignment use cases.
232
198
switch ( $ alignment ) {
@@ -241,19 +207,43 @@ function auto_sizes_calculate_width( string $alignment, int $image_width, string
241
207
case 'left ' :
242
208
case 'right ' :
243
209
case 'center ' :
244
- // Todo: use smaller fo the two values .
245
- $ content_width = auto_sizes_get_layout_width ( $ ancestor_alignment );
246
- $ layout_width = sprintf ( '%1$spx ' , $ image_width );
210
+ // These alignment get constrained by the wide layout size but do not get stretched .
211
+ $ alignment = auto_sizes_get_layout_width ( ' wide ' );
212
+ $ layout_width = sprintf ( '%1$spx ' , min ( ( int ) $ alignment , $ image_width ) );
247
213
break ;
248
214
249
215
default :
250
- // Todo: use smaller fo the two values.
251
- $ content_width = auto_sizes_get_layout_width ( 'default ' );
252
- $ layout_width = min ( (int ) $ content_width , $ image_width ) . 'px ' ;
216
+ $ alignment = auto_sizes_get_layout_width ( 'default ' );
217
+ $ layout_width = sprintf ( '%1$spx ' , min ( (int ) $ alignment , $ image_width ) );
253
218
break ;
254
219
}
255
220
256
- return 'full ' === $ alignment ? $ layout_width : sprintf ( '(max-width: %1$s) 100vw, %1$s ' , $ layout_width );
221
+ // Format layout width when not 'full'.
222
+ if ( 'full ' !== $ alignment ) {
223
+ $ layout_width = sprintf ( '(max-width: %1$s) 100vw, %1$s ' , $ layout_width );
224
+ }
225
+
226
+ return $ layout_width ;
227
+ }
228
+
229
+ /**
230
+ * Retrieves the layout width for an alignment defined in theme.json.
231
+ *
232
+ * @since n.e.x.t
233
+ *
234
+ * @param string $alignment The alignment value.
235
+ * @return string The alignment width based.
236
+ */
237
+ function auto_sizes_get_layout_width ( string $ alignment ): string {
238
+ $ layout = auto_sizes_get_layout_settings ();
239
+
240
+ $ layout_widths = array (
241
+ 'full ' => '100vw ' , // Todo: incorporate useRootPaddingAwareAlignments.
242
+ 'wide ' => array_key_exists ( 'wideSize ' , $ layout ) ? $ layout ['wideSize ' ] : '' ,
243
+ 'default ' => array_key_exists ( 'contentSize ' , $ layout ) ? $ layout ['contentSize ' ] : '' ,
244
+ );
245
+
246
+ return $ layout_widths [ $ alignment ] ?? '' ;
257
247
}
258
248
259
249
/**
@@ -266,9 +256,15 @@ function auto_sizes_calculate_width( string $alignment, int $image_width, string
266
256
* @return array<string> The filtered context keys used by the block type.
267
257
*/
268
258
function auto_sizes_filter_uses_context ( array $ uses_context , WP_Block_Type $ block_type ): array {
269
- if ( 'core/image ' === $ block_type ->name ) {
259
+ // The list of blocks that can consume outer layout context.
260
+ $ consumer_blocks = array (
261
+ 'core/cover ' ,
262
+ 'core/image ' ,
263
+ );
264
+
265
+ if ( in_array ( $ block_type ->name , $ consumer_blocks , true ) ) {
270
266
// Use array_values to reset the array keys after merging.
271
- return array_values ( array_unique ( array_merge ( $ uses_context , array ( 'ancestor_block_align ' ) ) ) );
267
+ return array_values ( array_unique ( array_merge ( $ uses_context , array ( 'max_alignment ' ) ) ) );
272
268
}
273
269
return $ uses_context ;
274
270
}
@@ -283,9 +279,26 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo
283
279
* @return array<string, mixed> Modified block context.
284
280
*/
285
281
function auto_sizes_filter_render_block_context ( array $ context , array $ block ): array {
286
- if ( 'core/group ' === $ block ['blockName ' ] || 'core/columns ' === $ block ['blockName ' ] ) {
287
- $ context ['ancestor_block_align ' ] = $ block ['attrs ' ]['align ' ] ?? '' ;
282
+ // When no max alignment is set, the maximum is assumed to be 'full'.
283
+ $ context ['max_alignment ' ] = $ context ['max_alignment ' ] ?? 'full ' ;
284
+
285
+ // The list of blocks that can modify outer layout context.
286
+ $ provider_blocks = array (
287
+ 'core/columns ' ,
288
+ 'core/group ' ,
289
+ );
290
+
291
+ if ( in_array ( $ block ['blockName ' ], $ provider_blocks , true ) ) {
292
+ $ alignment = $ block ['attrs ' ]['align ' ] ?? '' ;
293
+
294
+ // If the container block doesn't have alignment, it's assumed to be 'default'.
295
+ if ( ! (bool ) $ alignment ) {
296
+ $ context ['max_alignment ' ] = 'default ' ;
297
+ } elseif ( 'wide ' === $ block ['attrs ' ]['align ' ] ) {
298
+ $ context ['max_alignment ' ] = 'wide ' ;
299
+ }
288
300
}
301
+
289
302
return $ context ;
290
303
}
291
304
0 commit comments