Skip to content

Commit d86cf87

Browse files
authored
Merge pull request #1547 from WordPress/fix/1531-update-auto-sizes-logic
Update auto sizes logic in Enhanced Responsive Images plugin to no longer load if already in Core
2 parents 63ff958 + 791c5a2 commit d86cf87

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

plugins/auto-sizes/hooks.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ function auto_sizes_update_image_attributes( $attr ): array {
4242

4343
return $attr;
4444
}
45-
add_filter( 'wp_get_attachment_image_attributes', 'auto_sizes_update_image_attributes' );
4645

4746
/**
4847
* Adds auto to the sizes attribute to the image, if applicable.
@@ -85,7 +84,12 @@ function auto_sizes_update_content_img_tag( $html ): string {
8584
$processor->set_attribute( 'sizes', "auto, $sizes" );
8685
return $processor->get_updated_html();
8786
}
88-
add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );
87+
88+
// Skip loading plugin filters if WordPress Core already loaded the functionality.
89+
if ( ! function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ) {
90+
add_filter( 'wp_get_attachment_image_attributes', 'auto_sizes_update_image_attributes' );
91+
add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );
92+
}
8993

9094
/**
9195
* Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list.

plugins/auto-sizes/tests/test-auto-sizes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function get_image_tag( int $attachment_id ): string {
3333
}
3434

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

0 commit comments

Comments
 (0)