-
-
Save wordpressvn/b26c9ab5dfe905dd82dd85ff9d9b611a to your computer and use it in GitHub Desktop.
[Smush] - Original Images. Currently has option oly to delete them
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] - Original Images | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Displays all original images so they can be deleted. Requires Smush | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Original_Images' ) ) { | |
class WPMUDEV_Original_Images { | |
private static $_instance = null; | |
private static $smush_core = null; | |
private static $per_page = 20; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Original_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_fetch_bak_images', array( $this, 'ajax_fetch_images' ) ) ; | |
add_action( 'wp_ajax_wpmudev_delete_bak_image', array( $this, 'ajax_delete_attachment' ) ); | |
} | |
public function ajax_delete_attachment() { | |
check_ajax_referer( 'wpmudev_delete_bak_image', 'security' ); | |
$bak_path = filter_input( INPUT_POST, 'bak_path', FILTER_DEFAULT ); | |
$attachment_id = filter_input( INPUT_POST, 'attachment_id', FILTER_VALIDATE_INT ); | |
if ( unlink( $bak_path ) ) { | |
delete_post_meta( $attachment_id, '_wp_attachment_backup_sizes' ); | |
} | |
return wp_send_json( | |
array( | |
'success' => true, | |
) | |
); | |
} | |
public function fetch_images ( $offset = 0 ) { | |
$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; | |
} | |
$images[] = array( | |
'attachment_id' => $attachment->ID, | |
'src' => $back_src, | |
'bak_path' => $back_path, | |
'thumb' => $src | |
); | |
} | |
if ( empty( $images ) ) { | |
return true; | |
} | |
return $images; | |
} | |
else { | |
return false; | |
} | |
} | |
public function ajax_fetch_images() { | |
check_ajax_referer( 'wpmudev_fetch_bak_images', 'security' ); | |
$offset = (int) $_POST['offset']; | |
$images = $this->fetch_images( $offset ); | |
if ( ! $images ) { | |
$return = array( | |
'success' => true, | |
'list' => '', | |
'offset' => false | |
); | |
wp_send_json($return); | |
} | |
$images_list = $this->list_images( $images ); | |
$offset += self::$per_page; | |
$return = array( | |
'success' => true, | |
'list' => $images_list, | |
'offset' => $offset | |
); | |
wp_send_json($return); | |
} | |
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' ]; | |
//$src = $image[ 'src' ]; | |
$bak_path = $image[ 'bak_path' ]; | |
$src = $image[ 'thumb' ]; | |
$out .= " | |
<div style=\"width: 100%; vertical-align: top; margin-bottom: 20px;\" id=\"back-item-wrap-{$attachment_id}\"> | |
<label> | |
<span style=\"display:inline-block;vertical-align: top;\"> | |
<input type=\"checkbox\" class=\"single-back-chkbx\" id=\"back-item-{$attachment_id}\" value=\"{$attachment_id}\" data-bak-path=\"{$bak_path}\" /> | |
</span> | |
<span style=\"display:inline-block; width:150px;\"> | |
<img src=\"{$src}\" width=\"150\" /> | |
</span> | |
<span style=\"display:inline-block;vertical-align: top;\"> | |
{$bak_path} | |
</span> | |
</label> | |
</div>"; | |
} | |
return $out; | |
} | |
public function admin_menu() { | |
add_submenu_page( | |
'smush' , | |
'Original Images', | |
'Original Images', | |
'manage_options', | |
'original-images', | |
array( $this, 'original_images' ) | |
); | |
} | |
public function original_images() { | |
?> | |
<h1>Original Images</h1> | |
<div id="smush-back-images-list"></div> | |
<div id="smush-back-images-del-button-wrap" style="display: none;" > | |
<label> | |
<input type="checkbox" id="smush-back-check-all" /> Check all | |
</label> | |
<button id="smush-back-deleter">Delete Selected</button> | |
</div> | |
<div id="smush-spinner"> <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_Orig_Images = { | |
list_container : $( '#smush-back-images-list' ), | |
init : function(){ | |
this.fetch( 0 ); | |
$( '#smush-back-deleter' ).on( 'click', this.delete_images ); | |
}, | |
fetch : function( offset ){ | |
var data = { | |
action: 'wpmudev_fetch_bak_images', | |
security: '<?php echo wp_create_nonce( "wpmudev_fetch_bak_images" ); ?>', | |
offset: offset | |
}; | |
$.post( ajaxurl, data, function(response) { | |
if( response.success ){ | |
Smush_Orig_Images.list( response.list ); | |
if ( response.offset ) { | |
Smush_Orig_Images.fetch( response.offset ); | |
} | |
else { | |
Smush_Orig_Images.fetch_done(); | |
} | |
} | |
}); | |
}, | |
fetch_done : function() { | |
$( '#smush-spinner' ).hide( 300 ); | |
$( '#smush-back-images-del-button-wrap' ).show( 300 ); | |
$( '#smush-back-check-all' ).on( 'click', this.toggle_checkboxes ); | |
}, | |
toggle_checkboxes : function() { | |
let checkboxes = $( '#smush-back-images-list .single-back-chkbx' ); | |
if ( $( this ).prop('checked') ) { | |
checkboxes.each(function(){ | |
$(this).prop('checked', true); | |
}); | |
} else { | |
checkboxes.each(function(){ | |
$(this).prop('checked', false); | |
}); | |
} | |
}, | |
list : function( list ){ | |
this.list_container.append( list ); | |
}, | |
delete_images : function() { | |
let chosen_images = $( 'input.single-back-chkbx:checked' ), | |
images_holder = []; | |
chosen_images.each(function(){ | |
images_holder.push( $( this ).val() ); | |
}); | |
Smush_Orig_Images.process_deletion( images_holder ); | |
}, | |
process_deletion : function( images ) { | |
if (typeof images !== 'undefined' && images.length > 0) { | |
let attachment_id = images[0], | |
bak_path = $( '#back-item-' + attachment_id ).data( 'bak-path' ); | |
var data = { | |
action: 'wpmudev_delete_bak_image', | |
security: '<?php echo wp_create_nonce( "wpmudev_delete_bak_image" ); ?>', | |
attachment_id: attachment_id, | |
bak_path : bak_path | |
}; | |
$.post( ajaxurl, data, function( response ) { | |
if( response.success ){ | |
images.shift(); | |
Smush_Orig_Images.process_deletion( images ); | |
} | |
}); | |
} else { | |
alert( 'Back images removed' ); | |
location.reload(); | |
} | |
} | |
} | |
$( document ).ready( Smush_Orig_Images.init() ); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_original_images' ) ) { | |
function wpmudev_original_images(){ | |
return WPMUDEV_Original_Images::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_original_images', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment