Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accurate sizes: Disable layout calculations for classic themes #1744

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading