From c0b392fafb9304c27aaf0370d0745d64998f7927 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 18 Nov 2024 19:27:58 +0530 Subject: [PATCH 1/3] Address feedback for new function --- plugins/webp-uploads/helper.php | 12 ++++++++++-- plugins/webp-uploads/hooks.php | 2 +- plugins/webp-uploads/picture-element.php | 8 +------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 6c8062664c..175d32117c 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -479,11 +479,19 @@ function webp_uploads_get_mime_type_image( int $attachment_id, string $src, stri * * @since n.e.x.t * - * @param string $file The path to the file. * @param int $attachment_id The attachment ID. + * @param string $file Optional. The path to the file. * @return string The MIME type of the file, or an empty string if not found. */ -function webp_uploads_get_file_mime_type( string $file, int $attachment_id ): string { +function webp_uploads_get_attachment_file_mime_type( int $attachment_id, string $file = '' ): string { + if ( '' === $file ) { + $file = get_attached_file( $attachment_id, true ); + // File does not exist. + if ( false === $file || ! file_exists( $file ) ) { + return ''; + } + } + /* * We need to get the MIME type ideally from the file, as WordPress Core may have already altered it. * The post MIME type is typically not updated during that process. diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index a7a9b308da..17a2305243 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -58,7 +58,7 @@ function webp_uploads_create_sources_property( array $metadata, int $attachment_ return $metadata; } - $mime_type = webp_uploads_get_file_mime_type( $file, $attachment_id ); + $mime_type = webp_uploads_get_attachment_file_mime_type( $attachment_id, $file ); if ( '' === $mime_type ) { return $metadata; } diff --git a/plugins/webp-uploads/picture-element.php b/plugins/webp-uploads/picture-element.php index 8beb11ef49..25714a2b4b 100644 --- a/plugins/webp-uploads/picture-element.php +++ b/plugins/webp-uploads/picture-element.php @@ -23,13 +23,7 @@ function webp_uploads_wrap_image_in_picture( string $image, string $context, int return $image; } - $file = get_attached_file( $attachment_id, true ); - // File does not exist. - if ( false === $file || ! file_exists( $file ) ) { - return $image; - } - - $original_file_mime_type = webp_uploads_get_file_mime_type( $file, $attachment_id ); + $original_file_mime_type = webp_uploads_get_attachment_file_mime_type( $attachment_id ); if ( '' === $original_file_mime_type ) { return $image; } From 384a841ef86e642f37866ad99a5541fb0d0a8d74 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 18 Nov 2024 08:22:37 -0800 Subject: [PATCH 2/3] Update function docs to clarify attachment specific scope. --- plugins/webp-uploads/helper.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 175d32117c..972091ae22 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -470,8 +470,9 @@ function webp_uploads_get_mime_type_image( int $attachment_id, string $src, stri } /** - * Retrieves the MIME type of a file, checking the file directly if possible, - * and falling back to the attachment's MIME type if needed. + * Retrieves the MIME type of an attachment file, checking the file directly if possible. + * + * If checking the file directly fails, the function falls back to the attachment's MIME type. * * The function attempts to determine the MIME type directly from the file. * If that information is unavailable, it uses the MIME type from the attachment metadata. From c0050121d1324544e4d0c1a8858391e4ac1a8554 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 18 Nov 2024 08:23:32 -0800 Subject: [PATCH 3/3] Remove unnecessary file_exists check. --- plugins/webp-uploads/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 972091ae22..11eada7811 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -488,7 +488,7 @@ function webp_uploads_get_attachment_file_mime_type( int $attachment_id, string if ( '' === $file ) { $file = get_attached_file( $attachment_id, true ); // File does not exist. - if ( false === $file || ! file_exists( $file ) ) { + if ( false === $file ) { return ''; } }