Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename webp_uploads_get_file_mime_type to webp_uploads_get_attachment_file_mime_type to clarify scope #1662

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the above, the file_exists() check was never needed here anyway, so this is okay to be removed.

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