Skip to content

Commit

Permalink
Add setting option to delete original image file
Browse files Browse the repository at this point in the history
- Add helper to check option value
- Register setting to delete original image file
- Update original_image metadata before deleting
- Remove option on plugin uninstall
  • Loading branch information
AhmarZaidi committed Oct 18, 2024
1 parent 8caabd7 commit 1fae439
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
11 changes: 11 additions & 0 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
43 changes: 36 additions & 7 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 @@ -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 );
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
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' );
}

0 comments on commit 1fae439

Please sign in to comment.