Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Created March 10, 2017 12:21
Show Gist options
  • Save UmeshSingla/b3e7d2b08c37c11dd329b05c5270d086 to your computer and use it in GitHub Desktop.
Save UmeshSingla/b3e7d2b08c37c11dd329b05c5270d086 to your computer and use it in GitHub Desktop.
Adds a Box after settings, that allows you to restore your images from an existing backup created by Smush using Ajax
<?php
if ( ! class_exists( 'WpSmushRestore' ) ) {
class WpSmushRestore {
/**
* Constructor
*/
function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'js' ) );
add_action( 'smush_settings_ui_bottom', array( $this, 'ui' ) );
add_action( 'wp_ajax_smush_restore_bulk', array( $this, 'restore_images' ) );
}
function ui() {
global $WpSmush, $wpsmushit_admin;
$ui = new WpSmushBulkUi();
wp_nonce_field( 'smush_restore', 'smush-restore' );
//Contianer Header
$ui->container_header( 'wp-smush-restore', 'wp-smush-bulk-wrap-box', esc_html__( "BULK RESTORE", "wp-smushit" ) ); ?>
<div class="box-container"><?php
if ( empty( $wpsmushit_admin->smushed_attachments ) ) { ?>
<div class="wp-smush-no-images-content tc roboto-regular"><?php esc_html_e( "We haven’t found any smushed images in your media library that can be restored.", "wp-smushit" ); ?></div><?php
} else { ?>
<div class="wp-smush-notice wp-smush-image-total roboto-regular" style="background-color: #FFF5D5;">
<i class="dev-icon wdv-icon wdv-icon-fw wdv-icon-exclamation-sign"></i><?php printf( esc_html__( "We have found %d images in your media library that you can restore.", "wp-smushit" ), count( $wpsmushit_admin->smushed_attachments ) ); ?>
</div>
<div class="wp-smush-notice wp-smush-restore-progress hidden">
<span class="spinner is-active" style="float: left;"></span><?php esc_html_e( "Restoring Images..", "wp-smushit" ); ?>
</div>
<div class="wp-smush-notice wp-smush-restore-finished hidden" style="background-color: #FFF5D5;">
<?php esc_html_e( "WP Smush has finished restoring the images.", "wp-smushit" ); ?>
</div>
<button class="wp-smush-restore-bulk"
title="<?php esc_html_e( "Click to start Bulk Restore", "wp-smushit" ); ?>"><?php esc_html_e( "Bulk Restore", "wp-smushit" ); ?></button><?php
} ?>
</div><?php
echo "</section>";
}
function js( $hook ) {
if ( 'media_page_wp-smush-bulk' != $hook ) {
return;
}
global $wpsmushit_admin;
//Initialize stats if empty
if ( empty( $wpsmushit_admin->total_count ) ) {
$wpsmushit_admin->setup_global_stats();
}
$data = '
jQuery("document").ready(function($) {
var smushed_ids = ' . json_encode( $wpsmushit_admin->smushed_attachments ) .';
var restore = function (value) {
var param = {
"nonce": $("input#smush-restore").val(),
"action": "smush_restore_bulk",
"attachment_id": value
};
$.ajax({
type: "POST",
url: ajaxurl,
data: param,
async: false,
success: function (response) {
if (response.success) {
console.log(response);
}
}
})
};
//Handle the restore button click
$("button.wp-smush-restore-bulk").on("click", function (e) {
e.preventDefault();
//Hide the message and show restoring div
$("div.wp-smush-image-total").hide();
$("div.wp-smush-restore-progress").show();
//Disable button
$(this).attr("disabled", "disabled");
$.each( smushed_ids, function( item, value)
{
restore( value );
});
//Remove the button and show a message that all the images were processed
$("div.wp-smush-restore-progress, div.wp-smush-image-total, button.wp-smush-restore-bulk").hide();
$("div.wp-smush-restore-finished").show();
});
});';
wp_add_inline_script('wp-smushit-admin-js', $data );
}
/**
* Process ther estore request for the images
*/
function restore_images() {
//Check for
if ( empty( $_POST['attachment_id'] ) || ! wp_verify_nonce( $_POST['nonce'], 'smush_restore' ) ) {
wp_send_json_error( "Missing attachment id or Nonce verification failed" );
}
global $wpsmush_backup;
$wpsmush_backup->restore_image( intval( $_POST['attachment_id'] ) );
return;
}
}
global $wpsmush_restore;
$wpsmush_restore = new WpSmushRestore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment