Skip to content

Commit 044a8f2

Browse files
authored
Merge pull request #411 from WordPress/remove/backup-sources-files
Remove additional image backup sources & sizes files when attachment deleted
2 parents 18976cd + 5345210 commit 044a8f2

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎modules/images/webp-uploads/load.php‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,70 @@ function webp_uploads_remove_sources_files( $attachment_id ) {
347347
}
348348
wp_delete_file_from_directory( $full_size_file, $intermediate_dir );
349349
}
350+
351+
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
352+
$backup_sizes = is_array( $backup_sizes ) ? $backup_sizes : array();
353+
354+
foreach ( $backup_sizes as $backup_size ) {
355+
if ( ! isset( $backup_size['sources'] ) || ! is_array( $backup_size['sources'] ) ) {
356+
continue;
357+
}
358+
359+
$original_backup_size_mime = empty( $backup_size['mime-type'] ) ? '' : $backup_size['mime-type'];
360+
361+
foreach ( $backup_size['sources'] as $backup_mime => $backup_properties ) {
362+
/**
363+
* When we face the same mime type as the original image, we ignore this file as this file
364+
* would be removed when the size is removed by WordPress itself. The meta information as well
365+
* would be deleted as soon as the image is removed.
366+
*
367+
* @see wp_delete_attachment
368+
*/
369+
if ( $original_backup_size_mime === $backup_mime ) {
370+
continue;
371+
}
372+
373+
if ( ! is_array( $backup_properties ) || empty( $backup_properties['file'] ) ) {
374+
continue;
375+
}
376+
377+
$backup_intermediate_file = str_replace( $basename, $backup_properties['file'], $file );
378+
if ( empty( $backup_intermediate_file ) ) {
379+
continue;
380+
}
381+
382+
$backup_intermediate_file = path_join( $upload_path['basedir'], $backup_intermediate_file );
383+
if ( ! file_exists( $backup_intermediate_file ) ) {
384+
continue;
385+
}
386+
387+
wp_delete_file_from_directory( $backup_intermediate_file, $intermediate_dir );
388+
}
389+
}
390+
391+
$backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true );
392+
$backup_sources = is_array( $backup_sources ) ? $backup_sources : array();
393+
394+
// Delete full sizes backup mime types.
395+
foreach ( $backup_sources as $backup_mimes ) {
396+
397+
foreach ( $backup_mimes as $backup_mime_properties ) {
398+
if ( ! is_array( $backup_mime_properties ) || empty( $backup_mime_properties['file'] ) ) {
399+
continue;
400+
}
401+
402+
$full_size = str_replace( $basename, $backup_mime_properties['file'], $file );
403+
if ( empty( $full_size ) ) {
404+
continue;
405+
}
406+
407+
$full_size_file = path_join( $upload_path['basedir'], $full_size );
408+
if ( ! file_exists( $full_size_file ) ) {
409+
continue;
410+
}
411+
wp_delete_file_from_directory( $full_size_file, $intermediate_dir );
412+
}
413+
}
350414
}
351415
add_action( 'delete_attachment', 'webp_uploads_remove_sources_files', 10, 1 );
352416

‎tests/modules/images/webp-uploads/load-tests.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,39 @@ public function it_should_remove_full_size_images_when_no_size_image_exists() {
282282
$this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) );
283283
}
284284

285+
/**
286+
* Remove the attached WebP version if the attachment is force deleted after edit.
287+
*
288+
* @test
289+
*/
290+
public function it_should_remove_the_backup_sizes_and_sources_if_the_attachment_is_deleted_after_edit() {
291+
$attachment_id = $this->factory->attachment->create_upload_object(
292+
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'
293+
);
294+
295+
$file = get_attached_file( $attachment_id, true );
296+
$dirname = pathinfo( $file, PATHINFO_DIRNAME );
297+
298+
$this->assertIsString( $file );
299+
$this->assertFileExists( $file );
300+
301+
$editor = new WP_Image_Edit( $attachment_id );
302+
$editor->rotate_right()->save();
303+
304+
$backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true );
305+
$this->assertNotEmpty( $backup_sources );
306+
$this->assertIsArray( $backup_sources );
307+
308+
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
309+
$this->assertNotEmpty( $backup_sizes );
310+
$this->assertIsArray( $backup_sizes );
311+
312+
wp_delete_attachment( $attachment_id, true );
313+
314+
$this->assertFileDoesNotExist( path_join( $dirname, $backup_sources['full-orig']['image/webp']['file'] ) );
315+
$this->assertFileDoesNotExist( path_join( $dirname, $backup_sizes['thumbnail-orig']['sources']['image/webp']['file'] ) );
316+
}
317+
285318
/**
286319
* Avoid the change of URLs of images that are not part of the media library
287320
*

0 commit comments

Comments
 (0)