diff --git a/plugins/auto-sizes/includes/improve-calculate-sizes.php b/plugins/auto-sizes/includes/improve-calculate-sizes.php index ba2ba49d1..db1573c43 100644 --- a/plugins/auto-sizes/includes/improve-calculate-sizes.php +++ b/plugins/auto-sizes/includes/improve-calculate-sizes.php @@ -140,6 +140,11 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b * @return string|false An improved sizes attribute or false if a better size cannot be calculated. */ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $resize_width, string $max_alignment ) { + // Bail early if not a block theme. + if ( ! wp_is_block_theme() ) { + return false; + } + // Without an image ID or a resize width, we cannot calculate a better size. if ( 0 === $id && 0 === $resize_width ) { return false; diff --git a/plugins/auto-sizes/tests/test-improve-calculate-sizes.php b/plugins/auto-sizes/tests/test-improve-calculate-sizes.php index 484fb3f95..693161ee9 100644 --- a/plugins/auto-sizes/tests/test-improve-calculate-sizes.php +++ b/plugins/auto-sizes/tests/test-improve-calculate-sizes.php @@ -614,6 +614,38 @@ public function data_image_blocks_with_relative_alignment(): array { ); } + /** + * Test the image block with different alignment in classic theme. + * + * @dataProvider data_image_blocks_with_relative_alignment_for_classic_theme + * + * @param string $image_alignment Image alignment. + */ + public function test_image_block_with_different_alignment_in_classic_theme( string $image_alignment ): void { + switch_theme( 'twentytwentyone' ); + + $block_content = $this->get_image_block_markup( self::$image_id, 'large', $image_alignment ); + + $result = apply_filters( 'the_content', $block_content ); + + $this->assertStringContainsString( 'sizes="(max-width: 1024px) 100vw, 1024px" ', $result ); + } + + /** + * Data provider. + * + * @return array> The ancestor and image alignments. + */ + public function data_image_blocks_with_relative_alignment_for_classic_theme(): array { + return array( + array( '' ), + array( 'wide' ), + array( 'left' ), + array( 'center' ), + array( 'right' ), + ); + } + /** * Filter the theme.json data to include relative layout sizes. *