Skip to content
Merged
66 changes: 16 additions & 50 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,6 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
return $image;
}

// TODO: Add a filterable option to determine image extensions, see https://github.com/WordPress/performance/issues/187 for more details.
$target_image_extensions = array(
'jpg',
'jpeg',
);

// Creates a regular extension to find all the URLS with the provided extension for img tag.
preg_match_all( '/[^\s"]+\.(?:' . implode( '|', $target_image_extensions ) . ')/i', $image, $matches );
if ( empty( $matches ) ) {
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.
Expand All @@ -623,45 +611,23 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
return $image;
}

$basename = wp_basename( $metadata['file'] );
$urls = $matches[0];
foreach ( $urls as $url ) {
$src_filename = wp_basename( $url );

// Replace the full size image if present.
if ( isset( $metadata['sources'][ $target_mime ]['file'] ) && strpos( $url, $basename ) !== false ) {
$image = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image );
continue;
}

if ( empty( $metadata['sizes'] ) ) {
continue;
}

$extension = wp_check_filetype( $src_filename );
// Extension was not set properly no action possible or extension is already in the expected mime.
if ( empty( $extension['type'] ) || $extension['type'] === $target_mime ) {
continue;
}

// Find the appropriate size for the provided URL.
foreach ( $metadata['sizes'] as $name => $size_data ) {
// Not the size we are looking for.
if ( empty( $size_data['file'] ) || $src_filename !== $size_data['file'] ) {
continue;
}

if ( empty( $size_data['sources'][ $target_mime ]['file'] ) ) {
continue;
}

// This is the same as the file we want to replace nothing to do here.
if ( $size_data['sources'][ $target_mime ]['file'] === $src_filename ) {
continue;
}
// Replace the full size image if present.
if ( isset( $metadata['sources'][ $target_mime ]['file'] ) ) {
$basename = wp_basename( $metadata['file'] );
$image = str_replace( $basename, $metadata['sources'][ $target_mime ]['file'], $image );
Copy link
Member

Choose a reason for hiding this comment

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

These values could be the same. Just thinking out loud here, in terms of PHP performance (I know it's very minor but still worth thinking about) not sure whether that's okay or whether it would be better to use an if check around them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a check to avoid replacing the same images.

}

$image = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image );
break;
// Replace sub sizes for the image if present.
foreach ( $metadata['sizes'] as $name => $size_data ) {
if (
! empty( $size_data['file'] ) &&
! empty( $size_data['sources'][ $target_mime ]['file'] )
) {
$image = str_replace(
$size_data['file'],
$size_data['sources'][ $target_mime ]['file'],
$image
);
}
}

Expand Down