Skip to content
Merged
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
64 changes: 64 additions & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,70 @@ function webp_uploads_remove_sources_files( $attachment_id ) {
}
wp_delete_file_from_directory( $full_size_file, $intermediate_dir );
}

$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
$backup_sizes = is_array( $backup_sizes ) ? $backup_sizes : array();

foreach ( $backup_sizes as $backup_size ) {
if ( ! isset( $backup_size['sources'] ) || ! is_array( $backup_size['sources'] ) ) {
continue;
}

$original_backup_size_mime = empty( $backup_size['mime-type'] ) ? '' : $backup_size['mime-type'];

foreach ( $backup_size['sources'] as $backup_mime => $backup_properties ) {
/**
* When we face the same mime type as the original image, we ignore this file as this file
* would be removed when the size is removed by WordPress itself. The meta information as well
* would be deleted as soon as the image is removed.
*
* @see wp_delete_attachment
*/
if ( $original_backup_size_mime === $backup_mime ) {
continue;
}

if ( ! is_array( $backup_properties ) || empty( $backup_properties['file'] ) ) {
continue;
}

$backup_intermediate_file = str_replace( $basename, $backup_properties['file'], $file );
if ( empty( $backup_intermediate_file ) ) {
continue;
}

$backup_intermediate_file = path_join( $upload_path['basedir'], $backup_intermediate_file );
if ( ! file_exists( $backup_intermediate_file ) ) {
continue;
}

wp_delete_file_from_directory( $backup_intermediate_file, $intermediate_dir );
}
}

$backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true );
$backup_sources = is_array( $backup_sources ) ? $backup_sources : array();

// Delete full sizes backup mime types.
foreach ( $backup_sources as $backup_mimes ) {

foreach ( $backup_mimes as $backup_mime_properties ) {
if ( ! is_array( $backup_mime_properties ) || empty( $backup_mime_properties['file'] ) ) {
continue;
}

$full_size = str_replace( $basename, $backup_mime_properties['file'], $file );
if ( empty( $full_size ) ) {
continue;
}

$full_size_file = path_join( $upload_path['basedir'], $full_size );
if ( ! file_exists( $full_size_file ) ) {
continue;
}
wp_delete_file_from_directory( $full_size_file, $intermediate_dir );
}
}
}
add_action( 'delete_attachment', 'webp_uploads_remove_sources_files', 10, 1 );

Expand Down
33 changes: 33 additions & 0 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,39 @@ public function it_should_remove_full_size_images_when_no_size_image_exists() {
$this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) );
}

/**
* Remove the attached WebP version if the attachment is force deleted after edit.
*
* @test
*/
public function it_should_remove_the_backup_sizes_and_sources_if_the_attachment_is_deleted_after_edit() {
$attachment_id = $this->factory->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'
);

$file = get_attached_file( $attachment_id, true );
$dirname = pathinfo( $file, PATHINFO_DIRNAME );

$this->assertIsString( $file );
$this->assertFileExists( $file );

$editor = new WP_Image_Edit( $attachment_id );
$editor->rotate_right()->save();

$backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true );
$this->assertNotEmpty( $backup_sources );
$this->assertIsArray( $backup_sources );

$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
$this->assertNotEmpty( $backup_sizes );
$this->assertIsArray( $backup_sizes );

wp_delete_attachment( $attachment_id, true );

$this->assertFileDoesNotExist( path_join( $dirname, $backup_sources['full-orig']['image/webp']['file'] ) );
$this->assertFileDoesNotExist( path_join( $dirname, $backup_sizes['thumbnail-orig']['sources']['image/webp']['file'] ) );
}

/**
* Avoid the change of URLs of images that are not part of the media library
*
Expand Down