Skip to content

Commit

Permalink
POC - Update sizes filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Jul 22, 2024
1 parent f70b0a2 commit 0a1ef58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions plugins/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ function auto_sizes_improve_image_sizes_attributes( string $content ): string {

if ( $sizes ) {
$processor->set_attribute( 'sizes', $sizes );

// The sizes filter to reflate improve sizes calculations.
add_filter(
'wp_calculate_image_sizes',
static function () use ( $sizes ): string {
return $sizes;
}
);
}

$processor->remove_attribute( 'data-needs-sizes-update' );
Expand Down
46 changes: 46 additions & 0 deletions plugins/auto-sizes/tests/test-improve-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,50 @@ public function test_no_image(): void {

$this->assertStringContainsString( '<p>No image here</p>', $result );
}

/**
* Test wp_calculate_image_sizes() without improve sizes filter.
*/
public function test_wp_calculate_image_sizes_without_improve_sizes_filter(): void {
// Remove the improve sizes filter.
remove_filter( 'wp_content_img_tag', 'auto_sizes_improve_image_sizes_attributes', 9 );

$image_size = 'large';
$image_url = wp_get_attachment_image_url( self::$image_id, $image_size );
$sizes = wp_calculate_image_sizes( $image_size, $image_url, '', self::$image_id );

$block_content = '<!-- wp:image {"id":' . self::$image_id . ',"sizeSlug":"' . $image_size . '","linkDestination":"none"} --><figure class="wp-block-image size-' . $image_size . '"><img src="' . wp_get_attachment_image_url( self::$image_id, $image_size ) . '" alt="" class="wp-image-' . self::$image_id . '"/></figure><!-- /wp:image -->';

$result = apply_filters( 'the_content', $block_content );

$new_sizes = wp_calculate_image_sizes( $image_size, $image_url, '', self::$image_id );

$this->assertStringContainsString( $sizes, $result );
$this->assertStringContainsString( $new_sizes, $result );
$this->assertSame( $new_sizes, $sizes );
}

/**
* Test wp_calculate_image_sizes() with improve sizes filter.
*/
public function test_wp_calculate_image_sizes_with_improve_sizes_filter(): void {
$image_size = 'large';
$image_url = wp_get_attachment_image_url( self::$image_id, $image_size );
$sizes = wp_calculate_image_sizes( $image_size, $image_url, '', self::$image_id );

$block_content = '<!-- wp:image {"id":' . self::$image_id . ',"sizeSlug":"' . $image_size . '","linkDestination":"none"} --><figure class="wp-block-image size-' . $image_size . '"><img src="' . wp_get_attachment_image_url( self::$image_id, $image_size ) . '" alt="" class="wp-image-' . self::$image_id . '"/></figure><!-- /wp:image -->';

$result = apply_filters( 'the_content', $block_content );

$improve_sizes = wp_calculate_image_sizes( $image_size, $image_url, '', self::$image_id );

$this->assertStringContainsString( $improve_sizes, $result );
$this->assertStringNotContainsString( $sizes, $result );
$this->assertNotSame( $improve_sizes, $sizes );

remove_filter( 'wp_content_img_tag', 'auto_sizes_improve_image_sizes_attributes', 9 );

$new_sizes = wp_calculate_image_sizes( $image_size, $image_url, '', self::$image_id );
$this->assertSame( $improve_sizes, $new_sizes );
}
}

0 comments on commit 0a1ef58

Please sign in to comment.