Skip to content

Commit

Permalink
Merge pull request #1662 from WordPress/follow-up-1642
Browse files Browse the repository at this point in the history
Rename `webp_uploads_get_file_mime_type` to `webp_uploads_get_attachment_file_mime_type` to clarify scope
  • Loading branch information
felixarntz authored Nov 18, 2024
2 parents 89e4080 + 8579b36 commit e3b36f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
17 changes: 13 additions & 4 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,29 @@ 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.
* If neither is available, it defaults to an empty string.
*
* @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 ) {
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.
Expand Down
2 changes: 1 addition & 1 deletion plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 1 addition & 7 deletions plugins/webp-uploads/picture-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e3b36f6

Please sign in to comment.