@@ -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}
351415add_action ( 'delete_attachment ' , 'webp_uploads_remove_sources_files ' , 10 , 1 );
352416
0 commit comments