diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 2e812e7fd..c2ae6d2d7 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -410,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. * diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index e88489a8f..5495dcf8b 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -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 * @@ -234,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'] ) ) { + $new_formats_exist = false; + + // Check if the 'sources' property has a file with the expected extensions. + foreach ( $metadata['sources'] as $current_mime_type => $source ) { + if ( isset( $source['file'] ) ) { + $file_extension = pathinfo( $source['file'], PATHINFO_EXTENSION ); + if ( in_array( $file_extension, array( 'avif', 'webp' ), true ) ) { + $new_formats_exist = true; + break; + } + } + } + + if ( $new_formats_exist ) { + // Update the original_image metadata to point to the new primary image + // file since original image will be deleted. + $first_source = reset( $metadata['sources'] ); + if ( ! 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 ); diff --git a/plugins/webp-uploads/settings.php b/plugins/webp-uploads/settings.php index c44d6c452..e9cc270f0 100644 --- a/plugins/webp-uploads/settings.php +++ b/plugins/webp-uploads/settings.php @@ -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' ); @@ -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' ); @@ -270,6 +291,23 @@ function webp_uploads_use_picture_element_callback(): void { + +

+ +

+