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

Introduce webp_uploads_get_content_image_mimes() to get content image MIME replacement rules #420

Merged
merged 7 commits into from
Aug 10, 2022
30 changes: 30 additions & 0 deletions modules/images/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,36 @@ function webp_uploads_get_attachment_sources( $attachment_id, $size = 'thumbnail
return array();
}

/**
* Returns mime types that should be used for an image in the specific context.
*
* @since n.e.x.t
*
* @param int $attachment_id The attachment ID.
* @param string $context The current context.
* @return array Mime types to use for the image.
*/
function webp_uploads_get_content_image_mimes( $attachment_id, $context ) {
$target_mimes = array( 'image/webp', 'image/jpeg' );

/**
* Filters mime types that should be used to update all images in the content. The order of
* mime types matters. The first mime type in the list will be used if it is supported by an image.
*
* @since 1.0.0
*
* @param array $target_mimes The list of mime types that can be used to update images in the content.
* @param int $attachment_id The attachment ID.
* @param string $context The current context.
*/
$target_mimes = apply_filters( 'webp_uploads_content_image_mimes', $target_mimes, $attachment_id, $context );
if ( ! is_array( $target_mimes ) ) {
$target_mimes = array();
}

return $target_mimes;
}

/**
* Check whether the additional image is larger than the original image.
*
Expand Down
17 changes: 1 addition & 16 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,23 +507,10 @@ function webp_uploads_img_tag_update_mime_type( $original_image, $context, $atta
return $image;
}

/**
* Filters mime types that should be used to update all images in the content. The order of
* mime types matters. The first mime type in the list will be used if it is supported by an image.
*
* @since 1.0.0
*
* @param array $target_mimes The list of mime types that can be used to update images in the content.
* @param int $attachment_id The attachment ID.
* @param string $context The current context.
*/
$target_mimes = apply_filters( 'webp_uploads_content_image_mimes', array( 'image/webp', 'image/jpeg' ), $attachment_id, $context );

// Get the original mime type for comparison.
$original_mime = get_post_mime_type( $attachment_id );
$target_mimes = webp_uploads_get_content_image_mimes( $attachment_id, $context );

foreach ( $target_mimes as $target_mime ) {

if ( $target_mime === $original_mime ) {
continue;
}
Expand Down Expand Up @@ -568,7 +555,6 @@ function webp_uploads_img_tag_update_mime_type( $original_image, $context, $atta
}

foreach ( $target_mimes as $target_mime ) {

if ( $target_mime === $original_mime ) {
continue;
}
Expand Down Expand Up @@ -598,7 +584,6 @@ function webp_uploads_img_tag_update_mime_type( $original_image, $context, $atta
}

foreach ( $target_mimes as $target_mime ) {

if ( $target_mime === $original_mime ) {
continue;
}
Expand Down