Skip to content

Commit 5c6f7fd

Browse files
committed
Simplify context logic further
1 parent 9677bca commit 5c6f7fd

File tree

2 files changed

+73
-60
lines changed

2 files changed

+73
-60
lines changed

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

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
101101
*/
102102
$filter = static function ( $sizes, $size ) use ( $block ) {
103103

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'];
108108

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 );
110110

111111
// If better sizes can't be calculated, use the default sizes.
112112
return false !== $better_sizes ? $better_sizes : $sizes;
@@ -147,10 +147,10 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
147147
* @param string $size The image size data.
148148
* @param string $align The image alignment.
149149
* @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.
151151
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
152152
*/
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 ) {
154154
// Without an image ID or a resize width, we cannot calculate a better size.
155155
if ( ! (bool) $id && ! (bool) $resize_width ) {
156156
return false;
@@ -177,11 +177,13 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
177177
}
178178

179179
// 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';
182181

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(
185187
'full' => 0,
186188
'wide' => 1,
187189
'left' => 2,
@@ -190,43 +192,7 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
190192
'default' => 3,
191193
);
192194

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;
230196

231197
// Handle different alignment use cases.
232198
switch ( $alignment ) {
@@ -241,19 +207,43 @@ function auto_sizes_calculate_width( string $alignment, int $image_width, string
241207
case 'left':
242208
case 'right':
243209
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 ) );
247213
break;
248214

249215
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 ) );
253218
break;
254219
}
255220

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 ] ?? '';
257247
}
258248

259249
/**
@@ -266,9 +256,15 @@ function auto_sizes_calculate_width( string $alignment, int $image_width, string
266256
* @return array<string> The filtered context keys used by the block type.
267257
*/
268258
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 ) ) {
270266
// 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' ) ) ) );
272268
}
273269
return $uses_context;
274270
}
@@ -283,9 +279,26 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo
283279
* @return array<string, mixed> Modified block context.
284280
*/
285281
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+
}
288300
}
301+
289302
return $context;
290303
}
291304

plugins/auto-sizes/tests/test-improve-calculate-sizes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public function get_group_block_markup( string $content, array $atts = array() )
553553
)
554554
);
555555

556-
$align_class = isset( $atts['align'] ) ? ' align' . $atts['align'] : '';
556+
$align_class = (bool) $atts['align'] ? ' align' . $atts['align'] : '';
557557

558558
return '<!-- wp:group ' . wp_json_encode( $atts ) . ' -->
559559
<div class="wp-block-group' . $align_class . '">' . $content . '</div>

0 commit comments

Comments
 (0)