Skip to content

Commit bbbbf2b

Browse files
Merge pull request #1642 from WordPress/fix/1635-follow-up
Follow-up: Introduce `webp_uploads_get_file_mime_type` helper function
2 parents a11c384 + b367d7e commit bbbbf2b

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

‎plugins/webp-uploads/helper.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,27 @@ function webp_uploads_get_mime_type_image( int $attachment_id, string $src, stri
468468

469469
return null;
470470
}
471+
472+
/**
473+
* Retrieves the MIME type of a file, checking the file directly if possible,
474+
* and falling back to the attachment's MIME type if needed.
475+
*
476+
* The function attempts to determine the MIME type directly from the file.
477+
* If that information is unavailable, it uses the MIME type from the attachment metadata.
478+
* If neither is available, it defaults to an empty string.
479+
*
480+
* @since n.e.x.t
481+
*
482+
* @param string $file The path to the file.
483+
* @param int $attachment_id The attachment ID.
484+
* @return string The MIME type of the file, or an empty string if not found.
485+
*/
486+
function webp_uploads_get_file_mime_type( string $file, int $attachment_id ): string {
487+
/*
488+
* We need to get the MIME type ideally from the file, as WordPress Core may have already altered it.
489+
* The post MIME type is typically not updated during that process.
490+
*/
491+
$filetype = wp_check_filetype( $file );
492+
$mime_type = $filetype['type'] ?? get_post_mime_type( $attachment_id );
493+
return is_string( $mime_type ) ? $mime_type : '';
494+
}

‎plugins/webp-uploads/hooks.php‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,8 @@ function webp_uploads_create_sources_property( array $metadata, int $attachment_
5858
return $metadata;
5959
}
6060

61-
/*
62-
* We need to get the MIME type ideally from the file, as WordPress Core may have already altered it.
63-
* The post MIME type is typically not updated during that process.
64-
*/
65-
$filetype = wp_check_filetype( $file );
66-
$mime_type = $filetype['type'] ?? get_post_mime_type( $attachment_id );
67-
if ( ! is_string( $mime_type ) ) {
61+
$mime_type = webp_uploads_get_file_mime_type( $file, $attachment_id );
62+
if ( '' === $mime_type ) {
6863
return $metadata;
6964
}
7065

‎plugins/webp-uploads/picture-element.php‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,21 @@ function webp_uploads_wrap_image_in_picture( string $image, string $context, int
2222
if ( 'the_content' !== $context ) {
2323
return $image;
2424
}
25-
$image_meta = wp_get_attachment_metadata( $attachment_id );
26-
$original_file_mime_type = get_post_mime_type( $attachment_id );
27-
if ( false === $original_file_mime_type || ! isset( $image_meta['sizes'] ) ) {
25+
26+
$file = get_attached_file( $attachment_id, true );
27+
// File does not exist.
28+
if ( false === $file || ! file_exists( $file ) ) {
29+
return $image;
30+
}
31+
32+
$original_file_mime_type = webp_uploads_get_file_mime_type( $file, $attachment_id );
33+
if ( '' === $original_file_mime_type ) {
34+
return $image;
35+
}
36+
37+
$image_meta = wp_get_attachment_metadata( $attachment_id );
38+
39+
if ( ! isset( $image_meta['sizes'] ) ) {
2840
return $image;
2941
}
3042

0 commit comments

Comments
 (0)