From c2f9f48ad556df7ca55c5c2094e40bb5637e39aa Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 19 Sep 2024 10:14:22 +0530 Subject: [PATCH 1/6] Check WP core function --- plugins/auto-sizes/hooks.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/auto-sizes/hooks.php b/plugins/auto-sizes/hooks.php index 4d2479b74..fe22e7eb4 100644 --- a/plugins/auto-sizes/hooks.php +++ b/plugins/auto-sizes/hooks.php @@ -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. @@ -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 the WordPress core is already loaded auto sizes. +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. From 2e8dabbe4a7bc286413a105f2dbc62ebbc415898 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 19 Sep 2024 10:14:54 +0530 Subject: [PATCH 2/6] Update unit tests --- plugins/auto-sizes/tests/test-auto-sizes.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/auto-sizes/tests/test-auto-sizes.php b/plugins/auto-sizes/tests/test-auto-sizes.php index 8a25f2b05..265fffcc2 100644 --- a/plugins/auto-sizes/tests/test-auto-sizes.php +++ b/plugins/auto-sizes/tests/test-auto-sizes.php @@ -33,8 +33,11 @@ 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' ) ); + // Skip loading plugin filters if the WordPress core is already loaded auto sizes. + if ( ! function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ) { + $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( 10, has_action( 'wp_head', 'auto_sizes_render_generator' ) ); } From a7410d292a974095402b1b5e160609a7ceda8f1d Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 23 Sep 2024 10:42:05 +0530 Subject: [PATCH 3/6] Address unit tests feedbacks --- plugins/auto-sizes/tests/test-auto-sizes.php | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/plugins/auto-sizes/tests/test-auto-sizes.php b/plugins/auto-sizes/tests/test-auto-sizes.php index 265fffcc2..83630a61f 100644 --- a/plugins/auto-sizes/tests/test-auto-sizes.php +++ b/plugins/auto-sizes/tests/test-auto-sizes.php @@ -32,15 +32,6 @@ public function get_image_tag( int $attachment_id ): string { return get_image_tag( $attachment_id, '', '', '', 'large' ); } - public function test_hooks(): void { - // Skip loading plugin filters if the WordPress core is already loaded auto sizes. - if ( ! function_exists( 'wp_sizes_attribute_includes_valid_auto' ) ) { - $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( 10, has_action( 'wp_head', 'auto_sizes_render_generator' ) ); - } - /** * Test generated markup for an image with lazy loading gets auto-sizes. * @@ -209,13 +200,11 @@ public function data_image_with_existing_auto_sizes(): array { } /** - * Test printing the meta generator tag. - * - * @covers ::auto_sizes_render_generator + * Test auto-sizes meta tag present in wp_head hook. */ - public function test_auto_sizes_render_generator(): void { - $tag = get_echo( 'auto_sizes_render_generator' ); - $this->assertStringStartsWith( 'assertStringContainsString( 'assertStringContainsString( 'generator', $tag ); $this->assertStringContainsString( 'auto-sizes ' . IMAGE_AUTO_SIZES_VERSION, $tag ); } From fb5d2ff75408bb17ec0087270e4d9b09fa4ac771 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 23 Sep 2024 20:48:50 +0530 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Felix Arntz --- plugins/auto-sizes/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/auto-sizes/hooks.php b/plugins/auto-sizes/hooks.php index fe22e7eb4..b77c06903 100644 --- a/plugins/auto-sizes/hooks.php +++ b/plugins/auto-sizes/hooks.php @@ -85,7 +85,7 @@ function auto_sizes_update_content_img_tag( $html ): string { return $processor->get_updated_html(); } -// Skip loading plugin filters if the WordPress core is already loaded auto sizes. +// 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' ); From de6022ca0f6d1515556f0f9b856e308ea74081bc Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 23 Sep 2024 20:56:10 +0530 Subject: [PATCH 5/6] Re-add test_hooks test --- plugins/auto-sizes/tests/test-auto-sizes.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/auto-sizes/tests/test-auto-sizes.php b/plugins/auto-sizes/tests/test-auto-sizes.php index 83630a61f..c0130738b 100644 --- a/plugins/auto-sizes/tests/test-auto-sizes.php +++ b/plugins/auto-sizes/tests/test-auto-sizes.php @@ -32,6 +32,12 @@ public function get_image_tag( int $attachment_id ): string { return get_image_tag( $attachment_id, '', '', '', 'large' ); } + public function test_hooks(): void { + $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' ) ); + } + /** * Test generated markup for an image with lazy loading gets auto-sizes. * From 791c5a2ec412cdbb22532b9304075e494c3d1e08 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 23 Sep 2024 21:22:57 +0530 Subject: [PATCH 6/6] Revert test changes --- plugins/auto-sizes/tests/test-auto-sizes.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/auto-sizes/tests/test-auto-sizes.php b/plugins/auto-sizes/tests/test-auto-sizes.php index c0130738b..8560c3352 100644 --- a/plugins/auto-sizes/tests/test-auto-sizes.php +++ b/plugins/auto-sizes/tests/test-auto-sizes.php @@ -206,11 +206,13 @@ public function data_image_with_existing_auto_sizes(): array { } /** - * Test auto-sizes meta tag present in wp_head hook. + * Test printing the meta generator tag. + * + * @covers ::auto_sizes_render_generator */ - public function test_auto_sizes_meta_tag_present_in_wp_head_hook(): void { - $tag = get_echo( 'wp_head' ); - $this->assertStringContainsString( 'assertStringStartsWith( 'assertStringContainsString( 'generator', $tag ); $this->assertStringContainsString( 'auto-sizes ' . IMAGE_AUTO_SIZES_VERSION, $tag ); }