Skip to content

Commit 501c65e

Browse files
committed
Initial concept for supporting relative alingment widths
1 parent 59e86f8 commit 501c65e

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,21 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
194194

195195
case 'left':
196196
case 'right':
197-
$layout_width = sprintf( '%1$spx', $image_width );
198-
break;
199-
200197
case 'center':
201198
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+
204212
break;
205213
}
206214

@@ -212,14 +220,14 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
212220
return $layout_width;
213221
}
214222

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+
*/
223231
function auto_sizes_get_layout_width( string $alignment ): string {
224232
$layout = auto_sizes_get_layout_settings();
225233

@@ -232,15 +240,15 @@ function auto_sizes_get_layout_width( string $alignment ): string {
232240
return $layout_widths[ $alignment ] ?? '';
233241
}
234242

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+
*/
244252
function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $block_type ): array {
245253
// The list of blocks that can consume outer layout context.
246254
$consumer_blocks = array(
@@ -255,15 +263,15 @@ function auto_sizes_filter_uses_context( array $uses_context, WP_Block_Type $blo
255263
return $uses_context;
256264
}
257265

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+
*/
267275
function auto_sizes_filter_render_block_context( array $context, array $block ): array {
268276
// When no max alignment is set, the maximum is assumed to be 'full'.
269277
$context['max_alignment'] = $context['max_alignment'] ?? 'full';
@@ -288,13 +296,13 @@ function auto_sizes_filter_render_block_context( array $context, array $block ):
288296
return $context;
289297
}
290298

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+
*/
298306
function auto_sizes_get_layout_settings(): array {
299307
static $layout = array();
300308
if ( count( $layout ) === 0 ) {

0 commit comments

Comments
 (0)