Skip to content

Commit

Permalink
Merge pull request #1547 from WordPress/fix/1531-update-auto-sizes-logic
Browse files Browse the repository at this point in the history
Update auto sizes logic in Enhanced Responsive Images plugin to no longer load if already in Core
  • Loading branch information
westonruter authored Sep 23, 2024
2 parents 63ff958 + 791c5a2 commit d86cf87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions plugins/auto-sizes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function auto_sizes_update_image_attributes( $attr ): array {

return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'auto_sizes_update_image_attributes' );

/**
* Adds auto to the sizes attribute to the image, if applicable.
Expand Down Expand Up @@ -85,7 +84,12 @@ function auto_sizes_update_content_img_tag( $html ): string {
$processor->set_attribute( 'sizes', "auto, $sizes" );
return $processor->get_updated_html();
}
add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );

// Skip loading plugin filters if WordPress Core already loaded the functionality.
if ( ! function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ) {
add_filter( 'wp_get_attachment_image_attributes', 'auto_sizes_update_image_attributes' );
add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );
}

/**
* Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list.
Expand Down
4 changes: 2 additions & 2 deletions plugins/auto-sizes/tests/test-auto-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function get_image_tag( int $attachment_id ): string {
}

public function test_hooks(): void {
$this->assertSame( 10, has_filter( 'wp_get_attachment_image_attributes', 'auto_sizes_update_image_attributes' ) );
$this->assertSame( 10, has_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' ) );
$this->assertSame( function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ? false : 10, has_filter( 'wp_get_attachment_image_attributes', 'auto_sizes_update_image_attributes' ) );
$this->assertSame( function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ? false : 10, has_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' ) );
$this->assertSame( 10, has_action( 'wp_head', 'auto_sizes_render_generator' ) );
}

Expand Down

0 comments on commit d86cf87

Please sign in to comment.