Created
April 10, 2019 20:56
-
-
Save wpmudev-sls/fe56fc208020c81dae63f735713af366 to your computer and use it in GitHub Desktop.
[Smush] - Bulk Restore. Adds a new sub menu in Smush admin menu with option to bulk restore all 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 Restore | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds a new sub menu in Smush admin menu with option to bulk restore all images | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Smush_Bulk_Restore' ) ) { | |
class WPMUDEV_Smush_Bulk_Restore { | |
private static $_instance = null; | |
private static $step = 10; | |
private static $current_step = 0; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Smush_Bulk_Restore(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); | |
add_action( 'wp_ajax_wpmudev_smush_bulk_restore', array( $this, 'bulk_smush_ajax' ) ); | |
} | |
public function admin_menu() { | |
add_submenu_page( | |
'smush' , | |
'Bulk Restore', | |
'Bulk Restore', | |
'manage_options', | |
'smush-bulk-restore', | |
array( $this, 'admin_page' ) | |
); | |
} | |
public function bulk_smush_ajax() { | |
$_post_data = @file_get_contents( 'php://input' ); | |
$post_data = json_decode( $_post_data ); | |
self::$current_step = $post_data->step; | |
if ( ! isset( $post_data->nonce ) || ! wp_verify_nonce( $post_data->nonce , 'wpmudev_smush_bulk_restore_nonce' ) ) { | |
$return = array( | |
'success' => false, | |
'message' => 'AJAX nonce is wrong' | |
); | |
wp_send_json( $return ); | |
} | |
$message = $this->bulk_smush(); | |
$return = array( | |
'success' => true, | |
'message' => $message, | |
'current_step' => self::$current_step | |
); | |
wp_send_json( $return ); | |
} | |
public function bulk_smush() { | |
$core = WP_Smush::get_instance()->core(); | |
$images = $this->get_images(); | |
$message = null; | |
foreach ( $images as $key => $attachment ) { | |
self::$current_step ++; | |
$can_restore = $this->can_restore( $attachment->ID ); | |
$image_edit_link = admin_url( "upload.php?item={$attachment->ID}" ); | |
$image = wp_get_attachment_image( $attachment->ID ); | |
if ( is_wp_error( $can_restore ) ) { | |
$error_message = $can_restore->get_error_message(); | |
$message .= "<div style=\"vertical-align: top; margin-bottom: 20px;\"> | |
<h3>{$attachment->post_title}</h3> | |
<span style=\"display: inner-block;vertical-align: top;\">{$image}</span> | |
<div style=\"display: inner-block;vertical-align: top;\"> | |
<a href=\"{$image_edit_link}\">Image {$attachment->ID}</a> could not be restored: <span class=\"warning\">{$error_message}</span> | |
</div> | |
</div>"; | |
continue; | |
} else { | |
$core->mod->backup->restore_image( $attachment->ID, false ); | |
$message .= "<div style=\"vertical-align: top; margin-bottom: 20px;\"> | |
<h3>{$attachment->post_title}</h3> | |
<span style=\"display: inner-block;vertical-align: top;\">{$image}</span> | |
<div style=\"display: inner-block;vertical-align: top;\"> | |
<a href=\"{$image_edit_link}\">Image {$attachment->ID}</a> was restored successfully | |
</div> | |
</div>"; | |
} | |
} | |
return $message; | |
} | |
public function get_images( $ids = array(), $args = array(), $full = false ) { | |
$defaults = array( | |
'post_type' => 'attachment', | |
'post_status' => 'any', | |
//'post_mime_type' => implode( ',', self::$smush_core::$mime_types ), | |
'posts_per_page' => self::$step, | |
'offset' => self::$current_step, | |
'orderby' => 'ID', | |
'order' => 'ASC' | |
); | |
if ( is_array( $ids ) && ! empty( $ids ) ) { | |
$defaults['post__in'] = $ids; | |
} | |
$args = wp_parse_args( $args, $defaults ); | |
$images = new WP_Query( $args ); | |
if ( $full ) { | |
return $images; | |
} | |
return $images->posts; | |
} | |
private function can_restore( $image_id ) { | |
// Get the image path for all sizes. | |
$file = get_attached_file( $image_id ); | |
// Get stored backup path, if any. | |
$backup_sizes = get_post_meta( $image_id, '_wp_attachment_backup_sizes', true ); | |
// Check if we've a backup path. | |
if ( ! empty( $backup_sizes ) && ( ! empty( $backup_sizes['smush-full'] ) || ! empty( $backup_sizes['smush_png_path'] ) ) ) { | |
// Check for PNG backup. | |
$backup = ! empty( $backup_sizes['smush_png_path'] ) ? $backup_sizes['smush_png_path'] : ''; | |
// Check for original full size image backup. | |
$backup = empty( $backup ) && ! empty( $backup_sizes['smush-full'] ) ? $backup_sizes['smush-full'] : $backup; | |
$backup = ! empty( $backup['file'] ) ? $backup['file'] : ''; | |
} | |
// If we still don't have a backup path, use traditional method to get it. | |
if ( empty( $backup ) ) { | |
// Check backup for Full size. | |
$backup = WP_Smush::get_instance()->core()->mod->smush->get_image_backup_path( $file ); | |
} else { | |
// Get the full path for file backup. | |
$backup = str_replace( wp_basename( $file ), wp_basename( $backup ), $file ); | |
} | |
$file_exists = apply_filters( 'smush_backup_exists', file_exists( $backup ), $image_id, $backup ); | |
if ( $file_exists ) { | |
return true; | |
} | |
else { | |
return new WP_Error( 'no-backup', __( "No backup found" ) ); | |
} | |
// Additional Backup Check for JPEGs converted from PNG. | |
$pngjpg_savings = get_post_meta( $image_id, WP_SMUSH_PREFIX . 'pngjpg_savings', true ); | |
if ( ! empty( $pngjpg_savings ) ) { | |
// Get the original File path and check if it exists. | |
$backup = get_post_meta( $image_id, WP_SMUSH_PREFIX . 'original_file', true ); | |
$backup = WP_Smush::get_instance()->core()->mod->smush->original_file( $backup ); | |
if ( ! empty( $backup ) && is_file( $backup ) ) { | |
return true; | |
} | |
} | |
else { | |
return new WP_Error( 'no-savings', __( "No savings for this image" ) ); | |
} | |
return new WP_Error( 'general', __( "Could not be restored" ) ); | |
} | |
public function admin_page() { | |
?> | |
<h1>Smush - Bulk Restore Images</h1> | |
<div> | |
<button id="smush-bulk-restore">Bulk Restore All Smushed Images</button> | |
<?php wp_nonce_field( 'wpmudev_smush_bulk_restore_nonce', 'wpmudev_smush_bulk_restore_nonce' ); ?> | |
</div> | |
<div id="wpmudev-bulk-smush-placeholer"></div> | |
<div id="wpmudev-bulk-smush-log"></div> | |
<script type="text/javascript"> | |
($=>{ | |
WPMUDEV_Smush_Bulk_Restore = { | |
button : $( '#smush-bulk-restore' ), | |
log : $( '#wpmudev-bulk-smush-log' ), | |
placeholder : $( '#wpmudev-bulk-smush-placeholer' ), | |
current_step : 0, | |
init : function(){ | |
this.button.on( 'click', this.run ); | |
}, | |
run : async function(){ | |
let body = { | |
step : WPMUDEV_Smush_Bulk_Restore.current_step, | |
nonce : $( '#wpmudev_smush_bulk_restore_nonce' ).val() | |
}; | |
WPMUDEV_Smush_Bulk_Restore.button.hide( 300, function(){ $(this).remove(); }); | |
WPMUDEV_Smush_Bulk_Restore.placeholder.html( '<h3>Please wailt while restoring your images. It might take a while</h3> <h4>Make sure you don\'t close or update this tab as the restoration process needs this tab open in order to complete</h4>' ); | |
const response = await fetch( `${window.ajaxurl}?action=wpmudev_smush_bulk_restore`, { | |
method: 'POST', // or 'PUT' | |
body: JSON.stringify( body ) | |
}); | |
json = await response.json(); | |
if ( json.success ) { | |
if ( '' === json.message || null === json.message || ! json.current_step ) { | |
WPMUDEV_Smush_Bulk_Restore.placeholder.html( '<h3>All images have been restored</h3>' ); | |
alert( 'All images have been restored' ); | |
return; | |
} | |
WPMUDEV_Smush_Bulk_Restore.log.append( json.message ); | |
WPMUDEV_Smush_Bulk_Restore.current_step = json.current_step; | |
WPMUDEV_Smush_Bulk_Restore.run(); | |
} | |
} | |
}; | |
$(document).ready( WPMUDEV_Smush_Bulk_Restore.init() ); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_smush_bulk_restore' ) ) { | |
function wpmudev_smush_bulk_restore(){ | |
return WPMUDEV_Smush_Bulk_Restore::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_smush_bulk_restore', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment