Skip to content

Commit d3a60d0

Browse files
committed
Improve cover block left/right alignment
1 parent 83be032 commit d3a60d0

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,20 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
8282
*/
8383
$filter = static function ( $sizes, $size ) use ( $block ) {
8484

85-
$block_name = $block->name;
8685
$id = $block->attributes['id'] ?? 0;
8786
$alignment = $block->attributes['align'] ?? '';
8887
$width = $block->attributes['width'] ?? '';
8988
$max_alignment = $block->context['max_alignment'];
9089

91-
$better_sizes = auto_sizes_calculate_better_sizes( (string) $block_name, (int) $id, (string) $size, (string) $alignment, (string) $width, (string) $max_alignment );
90+
/*
91+
* Update width for cover block.
92+
* See https://github.com/WordPress/gutenberg/blob/938720602082dc50a1746bd2e33faa3d3a6096d4/packages/block-library/src/cover/style.scss#L82-L87.
93+
*/
94+
if ( 'core/cover' === $block->name && in_array( $alignment, array( 'left', 'right' ), true ) ) {
95+
$size = array( 420, 420 );
96+
}
97+
98+
$better_sizes = auto_sizes_calculate_better_sizes( (int) $id, $size, (string) $alignment, (string) $width, (string) $max_alignment );
9299

93100
// If better sizes can't be calculated, use the default sizes.
94101
return false !== $better_sizes ? $better_sizes : $sizes;
@@ -125,15 +132,14 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
125132
*
126133
* @since n.e.x.t
127134
*
128-
* @param string $block_name The block name.
129-
* @param int $id The image id.
130-
* @param string $size The image size data.
131-
* @param string $align The image alignment.
132-
* @param string $resize_width Resize image width.
133-
* @param string $max_alignment The maximum usable layout alignment.
135+
* @param int $id The image id.
136+
* @param string|int[] $size The image size data.
137+
* @param string $align The image alignment.
138+
* @param string $resize_width Resize image width.
139+
* @param string $max_alignment The maximum usable layout alignment.
134140
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
135141
*/
136-
function auto_sizes_calculate_better_sizes( string $block_name, int $id, string $size, string $align, string $resize_width, string $max_alignment ) {
142+
function auto_sizes_calculate_better_sizes( int $id, $size, string $align, string $resize_width, string $max_alignment ) {
137143
// Without an image ID or a resize width, we cannot calculate a better size.
138144
if ( ! (bool) $id && ! (bool) $resize_width ) {
139145
return false;
@@ -189,13 +195,6 @@ function auto_sizes_calculate_better_sizes( string $block_name, int $id, string
189195

190196
case 'left':
191197
case 'right':
192-
/*
193-
* Update width for cover block.
194-
* See https://github.com/WordPress/gutenberg/blob/938720602082dc50a1746bd2e33faa3d3a6096d4/packages/block-library/src/cover/style.scss#L82-L87.
195-
*/
196-
if ( 'core/cover' === $block_name ) {
197-
$image_width = $image_width * 0.5;
198-
}
199198
$layout_width = sprintf( '%1$spx', $image_width );
200199
break;
201200

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ public function test_cover_block_with_left_right_center_alignment( string $align
368368
*/
369369
public function data_image_left_right_center_alignment(): array {
370370
return array(
371-
array( 'left', 'sizes="(max-width: 540px) 100vw, 540px' ),
372-
array( 'right', 'sizes="(max-width: 540px) 100vw, 540px' ),
371+
array( 'left', 'sizes="(max-width: 420px) 100vw, 420px' ),
372+
array( 'right', 'sizes="(max-width: 420px) 100vw, 420px' ),
373373
array( 'center', 'sizes="(max-width: 620px) 100vw, 620px' ),
374374
);
375375
}

0 commit comments

Comments
 (0)