Last active
May 2, 2020 20:11
-
-
Save wpmudev-sls/667a64e835a27418eeded036087d68bc to your computer and use it in GitHub Desktop.
[Smush] - Bulk Delete Bak Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Smush] - Bulk Delete Bak Images | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Deletes all .bak images | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Smush_Delete_Bak_Images' ) ) { | |
class WPMUDEV_Smush_Delete_Bak_Images { | |
private static $_instance = null; | |
private static $smush_core = null; | |
private static $per_page = 10; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Smush_Delete_Bak_Images(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
if ( ! class_exists( 'WP_Smush' ) && ! class_exists( 'Smush\\WP_Smush' ) ) { | |
return; | |
} | |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); | |
add_action( 'wp_ajax_wpmudev_delete_bak_images', array( $this, 'ajax_delete_images' ) ); | |
} | |
public function delete_image( $bak_path, $attachment_id ) { | |
if ( unlink( $bak_path ) ) { | |
delete_post_meta( $attachment_id, '_wp_attachment_backup_sizes' ); | |
return true; | |
} | |
return false; | |
} | |
public function ajax_delete_images ( $offset = 0 ) { | |
//check_ajax_referer( 'wpmudev_delete_bak_image', 'security' ); | |
$offset = filter_input( INPUT_POST, 'offset', FILTER_DEFAULT ); | |
$args = array( | |
'post_type' => 'attachment', | |
'offset' => $offset, | |
'posts_per_page' => self::$per_page, | |
'post_status' => null | |
); | |
$attachments = get_posts( $args ); | |
$images = array(); | |
if( ! empty( $attachments ) ) { | |
foreach( $attachments as $attachment ){ | |
$fullsize_path = get_attached_file( $attachment->ID ); | |
$back_src = $this->add_bak_substension( $attachment->guid ); | |
$back_path = $this->add_bak_substension( $fullsize_path ); | |
list( $src, $width, $height ) = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ); | |
if ( ! file_exists( $back_path ) ) { | |
continue; | |
} | |
$image_info = array( | |
'attachment_id' => $attachment->ID, | |
'src' => $back_src, | |
'bak_path' => $back_path, | |
'thumb' => $src | |
); | |
$deleted = $this->delete_image( $back_path, $attachment->ID ); | |
if ( $deleted ) { | |
$image_info[ 'deleted' ] = true; | |
} else { | |
$image_info[ 'deleted' ] = false; | |
} | |
$images[] = $image_info; | |
} | |
/* | |
if ( empty( $images ) ) { | |
wp_send_json( | |
array( | |
'success' => true, | |
'completed' => true, | |
'offset' => $offset | |
) | |
); | |
} | |
*/ | |
$offset += self::$per_page; | |
$return = array( | |
'success' => true, | |
'list' => ! empty( $images ) ? $this->list_images( $images ) : '', | |
'offset' => $offset, | |
'completed' => false | |
); | |
wp_send_json($return); | |
} | |
else { | |
wp_send_json( | |
array( | |
'success' => true, | |
'completed' => true | |
) | |
); | |
} | |
} | |
private function add_bak_substension( $path ) { | |
$pathinfo = pathinfo( $path ); | |
$back_path = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.bak.' . $pathinfo['extension']; | |
return $back_path; | |
} | |
private function list_images( $images ) { | |
if ( ! is_array( $images ) ) { | |
return ''; | |
} | |
$out = ''; | |
foreach ( $images as $image ) { | |
$attachment_id = $image[ 'attachment_id' ]; | |
$bak_path = $image[ 'bak_path' ]; | |
$src = $image[ 'thumb' ]; | |
$bg_color = '#42b883'; | |
$title = 'Image deleted succesfully'; | |
if ( ! $image[ 'deleted' ] ) { | |
$bg_color = '#ff7e67'; | |
$title = 'Image could not be deleted'; | |
} | |
$out .= " | |
<div style=\"width: 60%; vertical-align: top; margin: 10px; padding:10px; background-color:{$bg_color}; \" id=\"back-item-wrap-{$attachment_id}\"> | |
<div>{$title}</div> | |
<span style=\"display:inline-block; width:80px;\"> | |
<img src=\"{$src}\" width=\"80\" /> | |
</span> | |
<span style=\"display:inline-block;vertical-align: top;\"> | |
{$bak_path} | |
</span> | |
</div>"; | |
} | |
return $out; | |
} | |
public function admin_menu() { | |
add_submenu_page( | |
'smush' , | |
'Delete bak Images', | |
'Delete bak Images', | |
'manage_options', | |
'delete-bak-images', | |
array( $this, 'print_content' ) | |
); | |
} | |
public function print_content() { | |
$count = array_sum( (array) wp_count_attachments( $mime_type = 'image' ) ); | |
?> | |
<h1>Bulk Delete .bak Images</h1> | |
<button id="smush-back-deleter">Start Deleting</button> | |
<div id="smush-back-images-list"></div> | |
<div id="smush-spinner" style="display:none;"> | |
<?php echo sprintf( __( 'Checking all %d attachments for .bak images' ), $count )?> | |
<img src='https://svn.automattic.com/wordpress/trunk/wp-admin/images/spinner.gif' /> | |
</div> | |
<script type="text/javascript"> | |
( $ => { | |
// List all bak images with Ajax | |
Smush_RM_Bak_Images = { | |
list_container : $( '#smush-back-images-list' ), | |
init : function(){ | |
$( '#smush-back-deleter' ).on( 'click', function(){ Smush_RM_Bak_Images.rm_bak( 0 ) } ); | |
}, | |
rm_bak : function( offset ){ | |
$( '#smush-spinner' ).show( 300 ); | |
var data = { | |
action: 'wpmudev_delete_bak_images', | |
security: '<?php echo wp_create_nonce( "wpmudev_delete_bak_images" ); ?>', | |
offset: offset | |
}; | |
$.post( ajaxurl, data, function( response ) { | |
if( response.success ){ | |
Smush_RM_Bak_Images.list( response.list ); | |
if ( response.offset ) { | |
Smush_RM_Bak_Images.rm_bak( response.offset ); | |
} | |
else { | |
Smush_RM_Bak_Images.deletion_done(); | |
} | |
} else { | |
alert( 'There is probably some error and images could not be deleted' ); | |
} | |
}); | |
}, | |
deletion_done : function() { | |
$( '#smush-spinner' ).hide( 300 ); | |
alert( 'All .bak images shoud be removed!' ); | |
}, | |
list : function( list ){ | |
this.list_container.append( list ); | |
}, | |
} | |
$( document ).ready( Smush_RM_Bak_Images.init() ); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_smush_delete_bak_images' ) ) { | |
function wpmudev_smush_delete_bak_images(){ | |
return WPMUDEV_Smush_Delete_Bak_Images::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_smush_delete_bak_images', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment