From a384b8665544f13c04966a7fb1645ef8f5ca5fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Fri, 17 Jul 2026 02:15:12 +0200 Subject: [PATCH 1/3] Add imagify_get_mime_types filter to the return of imagify_get_mime_types() Adopted from https://github.com/wp-media/imagify-plugin/pull/1177, using wpm_apply_filters_typed per project standard. Co-authored-by: mauroTabsSpaces <300527260+mauroTabsSpaces@users.noreply.github.com> --- inc/functions/attachments.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/inc/functions/attachments.php b/inc/functions/attachments.php index 0bf6f3242..7a5e89b56 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -26,7 +26,14 @@ function imagify_get_mime_types( $type = null ) { $mimes['pdf'] = 'application/pdf'; } - return $mimes; + /** + * Filter the mime types which could be optimized by Imagify. + * + * @since 2.3.1 + * + * @param array $mimes The mime types, as extension => mime type pairs. + */ + return wpm_apply_filters_typed( 'array', 'imagify_get_mime_types', $mimes ); } /** From 983916b125d124b2368aa4be7e6e10c57f3965ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Fri, 17 Jul 2026 02:27:33 +0200 Subject: [PATCH 2/3] Add integration tests for imagify_get_mime_types() Covers the default, image, and not-image type branches and the new imagify_get_mime_types filter. Co-authored-by: mauroTabsSpaces <300527260+mauroTabsSpaces@users.noreply.github.com> --- .../inc/functions/imagifyGetMimeTypes.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Tests/Integration/inc/functions/imagifyGetMimeTypes.php diff --git a/Tests/Integration/inc/functions/imagifyGetMimeTypes.php b/Tests/Integration/inc/functions/imagifyGetMimeTypes.php new file mode 100644 index 000000000..3152d3b93 --- /dev/null +++ b/Tests/Integration/inc/functions/imagifyGetMimeTypes.php @@ -0,0 +1,59 @@ +assertSame( + [ + 'jpg|jpeg|jpe' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif', + 'webp' => 'image/webp', + 'pdf' => 'application/pdf', + ], + imagify_get_mime_types() + ); + } + + public function testShouldReturnImageMimeTypesOnly() { + $this->assertSame( + [ + 'jpg|jpeg|jpe' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif', + 'webp' => 'image/webp', + ], + imagify_get_mime_types( 'image' ) + ); + } + + public function testShouldReturnNotImageMimeTypesOnly() { + $this->assertSame( + [ 'pdf' => 'application/pdf' ], + imagify_get_mime_types( 'not-image' ) + ); + } + + public function testShouldApplyMimeTypesFilter() { + $callback = function ( $mimes ) { + $mimes['avif'] = 'image/avif'; + unset( $mimes['pdf'] ); + + return $mimes; + }; + + add_filter( 'imagify_get_mime_types', $callback ); + + $mimes = imagify_get_mime_types(); + + remove_filter( 'imagify_get_mime_types', $callback ); + + $this->assertSame( 'image/avif', $mimes['avif'] ); + $this->assertArrayNotHasKey( 'pdf', $mimes ); + } +} From 325baa840c878993098cb9fbc715c7dc9512f005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Fri, 17 Jul 2026 02:34:06 +0200 Subject: [PATCH 3/3] Sanitize filtered mime types and add unit tests for coverage Codacy diff coverage only measures the unit suite (composer code-coverage runs --testsuite unit), so integration tests alone left the changed lines at 0%. Add unit tests via Brain Monkey. Also discard malformed entries returned by the imagify_get_mime_types filter: only string extension => 'type/subtype' string pairs survive, protecting downstream mime checks from bad callback returns. Co-authored-by: mauroTabsSpaces <300527260+mauroTabsSpaces@users.noreply.github.com> --- .../inc/functions/imagifyGetMimeTypes.php | 21 +++++ .../inc/functions/imagifyGetMimeTypes.php | 80 +++++++++++++++++++ inc/functions/attachments.php | 11 ++- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 Tests/Unit/inc/functions/imagifyGetMimeTypes.php diff --git a/Tests/Integration/inc/functions/imagifyGetMimeTypes.php b/Tests/Integration/inc/functions/imagifyGetMimeTypes.php index 3152d3b93..edd6608ee 100644 --- a/Tests/Integration/inc/functions/imagifyGetMimeTypes.php +++ b/Tests/Integration/inc/functions/imagifyGetMimeTypes.php @@ -56,4 +56,25 @@ public function testShouldApplyMimeTypesFilter() { $this->assertSame( 'image/avif', $mimes['avif'] ); $this->assertArrayNotHasKey( 'pdf', $mimes ); } + + public function testShouldDiscardMalformedFilteredMimeTypes() { + $callback = function ( $mimes ) { + $mimes['bad-bool'] = true; + $mimes['bad-array'] = [ 'image/png' ]; + $mimes['not-mime'] = 'image'; + + return $mimes; + }; + + add_filter( 'imagify_get_mime_types', $callback ); + + $mimes = imagify_get_mime_types(); + + remove_filter( 'imagify_get_mime_types', $callback ); + + $this->assertArrayNotHasKey( 'bad-bool', $mimes ); + $this->assertArrayNotHasKey( 'bad-array', $mimes ); + $this->assertArrayNotHasKey( 'not-mime', $mimes ); + $this->assertSame( 'image/jpeg', $mimes['jpg|jpeg|jpe'] ); + } } diff --git a/Tests/Unit/inc/functions/imagifyGetMimeTypes.php b/Tests/Unit/inc/functions/imagifyGetMimeTypes.php new file mode 100644 index 000000000..8e42edc20 --- /dev/null +++ b/Tests/Unit/inc/functions/imagifyGetMimeTypes.php @@ -0,0 +1,80 @@ +assertSame( + [ + 'jpg|jpeg|jpe' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif', + 'webp' => 'image/webp', + 'pdf' => 'application/pdf', + ], + imagify_get_mime_types() + ); + } + + /** + * Test should return only image mime types for the image type. + */ + public function testShouldReturnImageMimeTypesOnly() { + $this->assertSame( + [ + 'jpg|jpeg|jpe' => 'image/jpeg', + 'png' => 'image/png', + 'gif' => 'image/gif', + 'webp' => 'image/webp', + ], + imagify_get_mime_types( 'image' ) + ); + } + + /** + * Test should return only PDF for the not-image type. + */ + public function testShouldReturnNotImageMimeTypesOnly() { + $this->assertSame( + [ 'pdf' => 'application/pdf' ], + imagify_get_mime_types( 'not-image' ) + ); + } + + /** + * Test should pass the mime types through the imagify_get_mime_types filter. + */ + public function testShouldApplyMimeTypesFilter() { + Filters\expectApplied( 'imagify_get_mime_types' ) + ->once() + ->andReturn( [ 'avif' => 'image/avif' ] ); + + $this->assertSame( [ 'avif' => 'image/avif' ], imagify_get_mime_types() ); + } + + /** + * Test should discard malformed entries returned by the filter. + */ + public function testShouldDiscardMalformedFilteredMimeTypes() { + Filters\expectApplied( 'imagify_get_mime_types' ) + ->once() + ->andReturn( + [ + 'avif' => 'image/avif', + 'bad-bool' => true, + 'bad-array' => [ 'image/png' ], + 'not-mime' => 'image', + 0 => 'image/gif', + '' => 'image/jpeg', + ] + ); + + $this->assertSame( [ 'avif' => 'image/avif' ], imagify_get_mime_types() ); + } +} diff --git a/inc/functions/attachments.php b/inc/functions/attachments.php index 7a5e89b56..f5fa22e6c 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -33,7 +33,16 @@ function imagify_get_mime_types( $type = null ) { * * @param array $mimes The mime types, as extension => mime type pairs. */ - return wpm_apply_filters_typed( 'array', 'imagify_get_mime_types', $mimes ); + $mimes = wpm_apply_filters_typed( 'array', 'imagify_get_mime_types', $mimes ); + + // Keep only well-formed extension => mime type pairs. + return array_filter( + $mimes, + function ( $mime, $ext ) { + return is_string( $ext ) && '' !== $ext && is_string( $mime ) && preg_match( '@^[a-z-]+/[a-z0-9.+-]+$@', $mime ); + }, + ARRAY_FILTER_USE_BOTH + ); } /**