Skip to content

Commit

Permalink
Merge pull request #1744 from WordPress/fix/check-classic-theme
Browse files Browse the repository at this point in the history
Accurate sizes: Check sizes issue for classic theme
  • Loading branch information
joemcgill authored Dec 16, 2024
2 parents efabb2b + 7865b24 commit 4c97d52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugins/auto-sizes/includes/improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions plugins/auto-sizes/tests/test-improve-calculate-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array<string>> 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.
*
Expand Down

0 comments on commit 4c97d52

Please sign in to comment.