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 filter webp_uploads_pre_replace_additional_image_source to short-circuit replacing additional image sources in frontend content #319

Merged
merged 15 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 49 additions & 10 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,31 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
if ( isset( $metadata['sources'][ $target_mime ]['file'] ) ) {
$basename = wp_basename( $metadata['file'] );
if ( $basename !== $metadata['sources'][ $target_mime ]['file'] ) {
$image = str_replace(
$basename,
$metadata['sources'][ $target_mime ]['file'],
$image
);

/**
* Filter to replace additional image source file, by locating the original
* mime types of the file and return correct file path in the end.
*
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
* @since n.e.xt
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $image An <img> tag where the urls would be updated.
* @param int $attachment_id The ID of the attachment being modified.
* @param string $size The size name that would be used to create this image, out of the registered subsizes.
* @param string $target_mime The target mime in which the image should be created.
* @param string $context The context where this is function is being used.
*/
$filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context );

// Check if filtered image is same as the image, replace the image otherwise not.
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
if ( $filtered_image === $image ) {
$image = str_replace(
$basename,
$metadata['sources'][ $target_mime ]['file'],
$image
);
} else {
$image = $filtered_image;
}
}
}

Expand All @@ -493,11 +513,30 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
continue;
}

$image = str_replace(
$size_data['file'],
$size_data['sources'][ $target_mime ]['file'],
$image
);
/**
* Filter to replace additional image source file, by locating the original
* mime types of the file and return correct file path in the end.
*
* @since n.e.xt
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $image An <img> tag where the urls would be updated.
* @param int $attachment_id The ID of the attachment being modified.
* @param string $name The size name that would be used to create this image, out of the registered subsizes.
* @param string $target_mime The target mime in which the image should be created.
* @param string $context The context where this is function is being used.
*/
$filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $name, $target_mime, $context );

// Check if filtered image is same as the image, replace the image otherwise not.
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
if ( $filtered_image === $image ) {
$image = str_replace(
$size_data['file'],
$size_data['sources'][ $target_mime ]['file'],
$image
);
} else {
$image = $filtered_image;
}
}

return $image;
Expand Down
21 changes: 21 additions & 0 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,25 @@ function( $data, $attachment_id ) {
$this->assertNotContains( 'image/invalid', $mime_types );
$this->assertSame( array( 'image/jpeg', 'image/webp' ), $mime_types );
}

/**
* Prevent replacing an image if image was uploaded via external source or plugin.
*
* @group webp_uploads_update_image_references
*
* @test
*/
public function it_should_prevent_replacing_an_image_uploaded_via_external_source() {
add_filter(
eugene-manuilov marked this conversation as resolved.
Show resolved Hide resolved
'webp_uploads_pre_replace_additional_image_source',
function() {
return TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg';
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
}
);

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' );

$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
$this->assertNotSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}
}