Skip to content

Commit 4c97d52

Browse files
authored
Merge pull request #1744 from WordPress/fix/check-classic-theme
Accurate sizes: Check sizes issue for classic theme
2 parents efabb2b + 7865b24 commit 4c97d52

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
140140
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
141141
*/
142142
function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $resize_width, string $max_alignment ) {
143+
// Bail early if not a block theme.
144+
if ( ! wp_is_block_theme() ) {
145+
return false;
146+
}
147+
143148
// Without an image ID or a resize width, we cannot calculate a better size.
144149
if ( 0 === $id && 0 === $resize_width ) {
145150
return false;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,38 @@ public function data_image_blocks_with_relative_alignment(): array {
614614
);
615615
}
616616

617+
/**
618+
* Test the image block with different alignment in classic theme.
619+
*
620+
* @dataProvider data_image_blocks_with_relative_alignment_for_classic_theme
621+
*
622+
* @param string $image_alignment Image alignment.
623+
*/
624+
public function test_image_block_with_different_alignment_in_classic_theme( string $image_alignment ): void {
625+
switch_theme( 'twentytwentyone' );
626+
627+
$block_content = $this->get_image_block_markup( self::$image_id, 'large', $image_alignment );
628+
629+
$result = apply_filters( 'the_content', $block_content );
630+
631+
$this->assertStringContainsString( 'sizes="(max-width: 1024px) 100vw, 1024px" ', $result );
632+
}
633+
634+
/**
635+
* Data provider.
636+
*
637+
* @return array<array<string>> The ancestor and image alignments.
638+
*/
639+
public function data_image_blocks_with_relative_alignment_for_classic_theme(): array {
640+
return array(
641+
array( '' ),
642+
array( 'wide' ),
643+
array( 'left' ),
644+
array( 'center' ),
645+
array( 'right' ),
646+
);
647+
}
648+
617649
/**
618650
* Filter the theme.json data to include relative layout sizes.
619651
*

0 commit comments

Comments
 (0)