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

Add option to delete the original file on upload #1606

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri
$editor->resize( $width, $height, $crop );

if ( null === $destination_file_name ) {
$ext = pathinfo( $image_path, PATHINFO_EXTENSION );
$suffix = $editor->get_suffix();
$suffix .= "-{$ext}";
$suffix = false !== $suffix ? $suffix : '';
$extension = explode( '|', $allowed_mimes[ $mime ] );
$destination_file_name = $editor->generate_filename( $suffix, null, $extension[0] );
}
Expand Down Expand Up @@ -411,6 +410,17 @@ function webp_uploads_is_fallback_enabled(): bool {
return (bool) get_option( 'perflab_generate_webp_and_jpeg' );
}

/**
* Checks if the `webp_uploads_delete_original` option is enabled.
*
* @since 2.2.0
*
* @return bool True if the option is enabled, false otherwise.
*/
function webp_uploads_is_delete_original_enabled(): bool {
return (bool) get_option( 'webp_uploads_delete_original' );
}

/**
* Retrieves the image URL for a specified MIME type from the attachment metadata.
*
Expand Down
46 changes: 37 additions & 9 deletions plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

/**
* Hook called by `wp_generate_attachment_metadata` to create the `sources` property for every image
* size, the sources' property would create a new image size with all the mime types specified in
* `webp_uploads_get_upload_image_mime_transforms`. If the original image is one of the mimes from
* `webp_uploads_get_upload_image_mime_transforms` the image is just added to the `sources` property and not
* created again. If the uploaded attachment is not a supported mime by this function, the hook does not alter the
* metadata of the attachment. In addition to every single size the `sources` property is added at the
* top level of the image metadata to store the references for all the mime types for the `full` size image of the
* attachment.
* size, and delete the original JPEG/PNG file if the option is enabled. The sources' property would
* create a new image size with all the mime types specified in `webp_uploads_get_upload_image_mime_transforms`.
* If the original image is one of the mimes from `webp_uploads_get_upload_image_mime_transforms` the image is just
* added to the `sources` property and not created again. If the uploaded attachment is not a supported mime by this
* function, the hook does not alter the metadata of the attachment. In addition to every single size the `sources`
* property is added at the top level of the image metadata to store the references for all the mime types for the
* `full` size image of the attachment.
*
* @since 1.0.0
*
Expand Down Expand Up @@ -88,7 +88,6 @@ function webp_uploads_create_sources_property( array $metadata, int $attachment_

$original_directory = pathinfo( $file, PATHINFO_DIRNAME );
$filename = pathinfo( $file, PATHINFO_FILENAME );
$ext = pathinfo( $file, PATHINFO_EXTENSION );
$allowed_mimes = array_flip( wp_get_mime_types() );

// Create the sources for the full sized image.
Expand All @@ -104,7 +103,7 @@ function webp_uploads_create_sources_property( array $metadata, int $attachment_
}

$extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
$destination = trailingslashit( $original_directory ) . "{$filename}-{$ext}.{$extension[0]}";
$destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}";
$image = webp_uploads_generate_additional_image_source( $attachment_id, 'full', $original_size_data, $targeted_mime, $destination );

if ( is_wp_error( $image ) ) {
Expand Down Expand Up @@ -235,6 +234,35 @@ function webp_uploads_create_sources_property( array $metadata, int $attachment_
$metadata['sizes'][ $size_name ] = $properties;
}

// Delete the original file if the option is enabled and new formats exist.
if ( webp_uploads_is_delete_original_enabled() && ! empty( $metadata['sources'] ) ) {
$supported_modern_image_formats = array();

if ( webp_uploads_mime_type_supported( 'image/avif' ) ) {
$supported_modern_image_formats[] = 'avif';
}
if ( webp_uploads_mime_type_supported( 'image/webp' ) ) {
$supported_modern_image_formats[] = 'webp';
}

$original_file_extension = pathinfo( $file, PATHINFO_EXTENSION );
$generated_file_extension = ! empty( $metadata['file'] ) ? pathinfo( $metadata['file'], PATHINFO_EXTENSION ) : '';
$delete_original_file = ! in_array( $original_file_extension, $supported_modern_image_formats, true ) && in_array( $generated_file_extension, $supported_modern_image_formats, true );

if ( $delete_original_file ) {
// Update the original_image metadata value to point to the new primary image
// file since original image will be deleted.
$first_source = reset( $metadata['sources'] );
if ( isset( $metadata['original_image'] ) && ! empty( $first_source['file'] ) ) {
$metadata['original_image'] = $first_source['file'];
wp_update_attachment_metadata( $attachment_id, $metadata );
}

// Delete the original file.
wp_delete_file( $file );
}
}

return $metadata;
}
add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 );
Expand Down
38 changes: 38 additions & 0 deletions plugins/webp-uploads/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ function webp_uploads_register_media_settings_field(): void {
'show_in_rest' => false,
)
);

// Add a setting to delete the original image file after conversion.
register_setting(
'media',
'webp_uploads_delete_original',
array(
'type' => 'boolean',
'default' => false,
'show_in_rest' => false,
)
);
}
add_action( 'init', 'webp_uploads_register_media_settings_field' );

Expand Down Expand Up @@ -105,6 +116,16 @@ function webp_uploads_add_media_settings_fields(): void {
'perflab_modern_image_format_settings',
array( 'class' => 'webp-uploads-use-picture-element' )
);

// Add delete original image setting field.
add_settings_field(
'webp_uploads_delete_original',
__( 'Delete Original Image', 'webp-uploads' ),
'webp_uploads_delete_original_setting_callback',
'media',
'perflab_modern_image_format_settings',
array( 'class' => 'webp-uploads-delete-original' )
);
}
add_action( 'admin_init', 'webp_uploads_add_media_settings_fields' );

Expand Down Expand Up @@ -270,6 +291,23 @@ function webp_uploads_use_picture_element_callback(): void {
<?php
}

/**
* Renders the settings field for the 'webp_uploads_delete_original' setting.
*
* @since 2.2.0
*/
function webp_uploads_delete_original_setting_callback(): void {
?>
<label for="webp_uploads_delete_original">
<input name="webp_uploads_delete_original" type="checkbox" id="webp_uploads_delete_original" value="1" <?php checked( '1', get_option( 'webp_uploads_delete_original' ) ); ?> />
<?php esc_html_e( 'Delete the original JPEG/PNG file after conversion', 'webp-uploads' ); ?>
</label>
<p class="description" id="webp_uploads_delete_original_description">
<?php esc_html_e( 'If enabled, the original image file will be deleted after conversion to modern formats. This will reduce filesystem storage but may cause incompatibility in certain cases.', 'webp-uploads' ); ?>
</p>
<?php
}

/**
* Adds a settings link to the plugin's action links.
*
Expand Down
8 changes: 4 additions & 4 deletions plugins/webp-uploads/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public function test_it_should_create_an_image_with_the_default_suffix_in_the_sa
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( '300x300-jpeg.webp', $result['file'] );
$this->assertFileExists( "{$directory}{$name}-300x300-jpeg.webp" );
$this->assertStringEndsWith( '300x300.webp', $result['file'] );
$this->assertFileExists( "{$directory}{$name}-300x300.webp" );
}

/**
Expand Down Expand Up @@ -575,8 +575,8 @@ public function test_it_should_add_original_image_extension_to_the_webp_file_nam

$this->assertIsArray( $jpeg_image_result );
$this->assertIsArray( $jpg_image_result );
$this->assertStringEndsWith( '300x300-jpeg.webp', $jpeg_image_result['file'] );
$this->assertStringEndsWith( '300x300-jpg.webp', $jpg_image_result['file'] );
$this->assertStringEndsWith( '300x300.webp', $jpeg_image_result['file'] );
$this->assertStringEndsWith( '300x300.webp', $jpg_image_result['file'] );
$this->assertNotSame( $jpeg_image_result['file'], $jpg_image_result['file'] );
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/webp-uploads/tests/test-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ static function () {
$this->assertStringEndsWith( '-scaled.jpg', get_attached_file( $attachment_id ) );
$this->assertImageHasSizeSource( $attachment_id, 'medium', 'image/webp' );
$this->assertStringEndsNotWith( '-scaled.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
$this->assertStringEndsWith( '-300x200-jpg.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
$this->assertStringEndsWith( '-300x200.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion plugins/webp-uploads/tests/test-picture-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function test_maybe_wrap_images_in_picture_element( bool $fallback_jpeg,
if ( is_array( $image_src ) ) {
$img_src = $image_src[0];
}
$webp_srcset = str_replace( '.jpg', '-jpg.webp', $image_srcset );
$webp_srcset = str_replace( '.jpg', '.webp', $image_srcset );

// Prepare the expected HTML by replacing placeholders with expected values.
$replacements = array(
Expand Down
1 change: 1 addition & 0 deletions plugins/webp-uploads/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
*/
function webp_uploads_delete_plugin_option(): void {
delete_option( 'perflab_generate_webp_and_jpeg' );
delete_option( 'webp_uploads_delete_original' );
}
Loading