diff --git a/Tests/Integration/inc/functions/imagifyGetMimeTypes.php b/Tests/Integration/inc/functions/imagifyGetMimeTypes.php new file mode 100644 index 000000000..edd6608ee --- /dev/null +++ b/Tests/Integration/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() + ); + } + + 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 ); + } + + 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 0bf6f3242..f5fa22e6c 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -26,7 +26,23 @@ 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. + */ + $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 + ); } /**