Last active
September 24, 2020 20:47
-
-
Save wpmudev-sls/fc8d94c347ef9f7d975647c97ab175a2 to your computer and use it in GitHub Desktop.
[Smush] - Bulk Smush Page. Creates a custom page for Bulk Smushing
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 Smush Page | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Creates a custom page for Bulk Smushing | |
* Task: SLS-777 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
if ( ! class_exists( 'WPMUDEV_Smush_Bulk_Smush_Page' ) ) { | |
class WPMUDEV_Smush_Bulk_Smush_Page { | |
private static $_instance = null; | |
private static $smush_core = null; | |
private static $smush_instance = null; | |
private $is_admin_screen = false; | |
private static $per_page = 5; | |
private static $current_step = 0; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_Smush_Bulk_Smush_Page(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
if ( ! defined( 'WP_SMUSH_DISABLE_STATS' ) || | |
! WP_SMUSH_DISABLE_STATS || | |
( ! class_exists( 'WP_Smush' ) && ! class_exists( 'Smush\\WP_Smush' ) ) | |
) { | |
return; | |
} | |
if ( is_null( self::$smush_core ) ) { | |
if ( version_compare( WP_SMUSH_VERSION, '3.5.0' ) >= 0 ) { | |
self::$smush_instance = WP_Smush::get_instance(); | |
self::$smush_core = self::$smush_instance->core(); | |
} else { | |
self::$smush_instance = Smush\WP_Smush::get_instance(); | |
self::$smush_core = self::$smush_instance->core(); | |
} | |
} | |
add_filter( 'admin_body_class', array( $this, 'smush_body_classes' ) ); | |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); | |
add_action( 'admin_footer', array( $this, 'footer_js' ) ); | |
add_action( 'wp_ajax_wpmudev_bulk_smush_images', array( $this, 'bulk_smush_ajax' ) ); | |
} | |
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_smush_images_nonce' ) ) { | |
$return = array( | |
'success' => false, | |
'message' => 'AJAX nonce is wrong', | |
); | |
wp_send_json( $return ); | |
} | |
$response = $this->bulk_smush(); | |
$message = isset( $response['message'] ) ? $response['message'] : ''; | |
$flag = isset( $response['flag'] ) ? $response['flag'] : ''; | |
if ( 'COMPLETED' === $flag ) { | |
self::$current_step = false; | |
} | |
$return = array( | |
'success' => true, | |
'message' => $message, | |
'flag' => $flag, | |
'current_step' => self::$current_step, | |
); | |
wp_send_json( $return ); | |
} | |
public function bulk_smush() { | |
$images = $this->get_images(); | |
$message = null; | |
$flag = 'IN_PROGRESS'; | |
if ( ! $images || empty( $images ) ) { | |
$flag = 'COMPLETED'; | |
$message = '<div style="vertical-align: top; margin-bottom: 20px; background-color: #f4f4f4;padding-left: 20px;"> | |
<h3>All images have been Smushed</h3> | |
</div>'; | |
return array( | |
'message' => $message, | |
'flag' => $flag, | |
); | |
} | |
foreach ( $images as $key => $attachment ) { | |
try { | |
if ( ! property_exists( self::$smush_core, 'mime_types' ) ) { | |
throw new Exception( 'Unknown property mime_types' ); | |
} | |
if ( ! is_callable( array( self::$smush_core->mod->smush, 'smush_single' ) ) ) { | |
throw new Exception( 'Unknown method smush_single' ); | |
} | |
$mime = get_post_mime_type( $attachment->ID ); | |
if ( ! in_array( $mime, self::$smush_core::$mime_types, true ) ) { | |
$message .= " | |
<div style=\"vertical-align: top; margin-bottom: 20px; background-color: #d9adad; padding: 20px;\"> | |
<table> | |
<tr> | |
<td style=\"vertical-align: middle;\"><h3>{$attachment->post_title}</h3></td> | |
<td style=\"vertical-align: middle;\">File {$attachment->ID} Filetype not supported</td> | |
</tr> | |
</table> | |
</div> | |
"; | |
continue; | |
} | |
if ( ! wp_attachment_is_image( $attachment ) ) { | |
$message .= " | |
<div style=\"vertical-align: top; margin-bottom: 20px; background-color: #d9adad; padding: 20px;\"> | |
<table> | |
<tr> | |
<td style=\"vertical-align: middle;\"><h3>{$attachment->post_title}</h3></td> | |
<td style=\"vertical-align: middle;\">File {$attachment->ID} not an image</td> | |
</tr> | |
</table> | |
</div> | |
"; | |
continue; | |
} | |
self::$current_step ++; | |
$image_edit_link = admin_url( "upload.php?item={$attachment->ID}" ); | |
$image = wp_get_attachment_image( $attachment->ID, 'thumbnail' ); | |
if ( $this->smushed_already( $attachment->ID ) ) { | |
$message .= " | |
<div style=\"vertical-align: top; margin-bottom: 20px; background-color: #ddd; padding: 20px;\"> | |
<table> | |
<tr> | |
<td style=\"vertical-align: middle;\">{$image}</td> | |
<td style=\"vertical-align: middle;\"> | |
<h3>{$attachment->post_title}</h3> | |
<div> | |
<a href=\"{$image_edit_link}\">Image {$attachment->ID}</a> already smushed | |
</div> | |
</td> | |
</tr> | |
</table> | |
</div> | |
"; | |
continue; | |
} | |
if ( $response = self::$smush_core->mod->smush->smush_single( $attachment->ID, true ) ) { | |
$message .= " | |
<div style=\"vertical-align: top; margin-bottom: 20px; background-color: #73b387; padding: 20px;\"> | |
<table> | |
<tr> | |
<td style=\"vertical-align: middle;\">{$image}</td> | |
<td style=\"vertical-align: middle;\"> | |
<h3>{$attachment->post_title}</h3> | |
<div> | |
<a href=\"{$image_edit_link}\">Image {$attachment->ID}</a> was smushed successfully | |
</div> | |
</td> | |
</tr> | |
</table> | |
</div> | |
"; | |
} else { | |
$message .= " | |
<div style=\"vertical-align: top; margin-bottom: 20px; background-color: #d9adad; padding: 20px;\"> | |
<table> | |
<tr> | |
<td style=\"vertical-align: middle;\">{$image}</td> | |
<td style=\"vertical-align: middle;\"> | |
<h3>{$attachment->post_title}</h3> | |
<div> | |
<a href=\"{$image_edit_link}\">Image {$attachment->ID}</a> could not be smushed | |
</div> | |
</td> | |
</tr> | |
</table> | |
</div> | |
"; | |
} | |
} // End try. | |
catch ( Exception $e ) { | |
$flag = 'COMPLETED'; | |
$exception = $e->getMessage(); | |
$message = " | |
<div style=\"vertical-align: top; margin-bottom: 20px;background-color: #d9adad;padding: 20px;\"> | |
<h3>Something Went Wrong</h3> | |
<div style=\"display: inner-block;vertical-align: middle;\"> | |
{$exception} | |
</div> | |
</div> | |
"; | |
} // End catch. | |
} // End loop. | |
return array( | |
'message' => $message, | |
'flag' => $flag, | |
); | |
} | |
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::$per_page, | |
'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; | |
} | |
public function smushed_already( $attachment_id ) { | |
return ! ! get_post_meta( $attachment_id, 'wp-smpro-smush-data', false ); | |
} | |
public function footer_js() { | |
?> | |
<script type="text/javascript"> | |
($=>{ | |
WPMUDEV_Smush_Bulk_Smush_Images = { | |
button : $( '#smush-bulk-start' ), | |
log : $( '#wpmudev-bulk-smush-log' ), | |
responseholder : $( '#smush-box-bulk-smush .sui-box-body' ), | |
header : $( '#wp-smush-page-header' ), | |
current_step : 0, | |
init : function(){ | |
this.button.on( 'click', this.run ); | |
}, | |
run : async function(){ | |
let _self = WPMUDEV_Smush_Bulk_Smush_Images, | |
body = { | |
step : _self.current_step, | |
nonce : '<?php echo wp_create_nonce( 'wpmudev_smush_bulk_smush_images_nonce' ); ?>', | |
}; | |
_self.button.hide( 300, function(){ $(this).remove(); }); | |
_self.header.append( '<h4>Please wait while Smushing 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_bulk_smush_images`, { | |
method: 'POST', // or 'PUT' | |
body: JSON.stringify( body ) | |
}); | |
json = await response.json(); | |
if ( json.success ) { | |
_self.responseholder.prepend( json.message ); | |
if ( '' === json.message || null === json.message || ! json.current_step ) { | |
_self.responseholder.prepend( '<h3>All images have been Smushed</h3>' ); | |
return; | |
} | |
_self.current_step = json.current_step; | |
_self.run(); | |
} | |
} | |
}; | |
$(document).ready( WPMUDEV_Smush_Bulk_Smush_Images.init() ); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
public function admin_menu() { | |
add_submenu_page( | |
'smush', | |
'Bulk Smush', | |
'Bulk Smush', | |
'manage_options', | |
'bulk-smush-page', | |
array( $this, 'admin_page' ) | |
); | |
} | |
public function admin_page() { | |
?> | |
<div class="sui-wrap"> | |
<div class="sui-header_ wp-smush-page-header" id="#wp-smush-page-header"> | |
<h1 class="sui-header-title">Bulk Smush Board</h1> | |
<small>Useful when Stats are deactivated on sites with a huge number of images</small> | |
</div> | |
<div id="smush-box-bulk-smush" class="sui-bulk sui-box bulk-smush-wrapper wp-smush-pro-install"> | |
<div class="sui-box_header"> | |
<h3 class="sui-box-title_">Bulk Smushed</h3> | |
<hr /> | |
<div class="sui-box-body"> | |
</div> | |
<div class="sui-box-footer"> | |
<span class="wp-smush-upload-images sui-no-padding-bottom tc"> | |
<a class="sui-button sui-button-blue tc" id="smush-bulk-start"> | |
Start Bulk Smush | |
</a> | |
</span> | |
</div> | |
</div> | |
</div> | |
</div> | |
<style type="text/css"> | |
.sui-box_header h3{ | |
padding: 20px 0 0 20px !important; | |
} | |
</style> | |
<?php | |
} | |
public function admin_enqueue_scripts( $classes ) { | |
if ( ! $this->is_admin_screen() ) { | |
return $classes; | |
} | |
wp_enqueue_style( 'smush-admin', WP_SMUSH_URL . 'app/assets/css/smush-admin.min.css', array(), WP_SMUSH_VERSION ); | |
} | |
public function smush_body_classes( $classes ) { | |
if ( ! $this->is_admin_screen() ) { | |
return $classes; | |
} | |
// Remove old wpmud class from body of smush page to avoid style conflict. | |
$classes = str_replace( 'wpmud ', '', $classes ); | |
$classes .= ' ' . WP_SHARED_UI_VERSION; | |
return $classes; | |
} | |
public function is_admin_screen() { | |
if ( $this->is_admin_screen ) { | |
return true; | |
} | |
$this->is_admin_screen = ( function_exists( 'get_current_screen' ) && 'smush-pro_page_bulk-smush-page' === get_current_screen()->id ); | |
return $this->is_admin_screen; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_smush_bulk_smush_page' ) ) { | |
function wpmudev_smush_bulk_smush_page() { | |
return WPMUDEV_Smush_Bulk_Smush_Page::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_smush_bulk_smush_page', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment