Last active
December 18, 2019 20:15
-
-
Save wpmudev-sls/bb9c4c3b24294980336cd075739346d2 to your computer and use it in GitHub Desktop.
[Smush] - Ajax Smush after upload. This is for a specific case where multiple operations happen on server after upload and before Smushing, eg uploading to S3. If server timeouts at that point, this snippet will try Smush with ajax - 11289012348292/1149345217224514
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] - Ajax Smush after upload | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: This is for a specific case where multiple operations happen on server after upload and before Smushing, eg uploading to S3. If server timeouts at that point, this snippet will try Smush with ajax - 11289012348292/1149345217224514 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
add_action( 'wp_ajax_wpmudev_smush_single_image', function(){ | |
global $wpdb; | |
$attachment_id = filter_input( INPUT_POST, 'attachment_id', FILTER_VALIDATE_INT ); | |
$smushed = Smush\WP_Smush::get_instance()->core()->mod->smush->smush_single( $attachment_id, true ); | |
$return = array( | |
'success' => false, | |
'content' => "Image {$attachment_id } could not be Smushed" | |
); | |
if ( $smushed ) { | |
$return = array( | |
'success' => true, | |
'content' => "Image {$attachment_id } was succesfully Smushed" | |
); | |
} | |
wp_send_json($return); | |
} ); | |
add_action( 'admin_footer', function(){ | |
if ( | |
! function_exists( 'get_current_screen' ) || | |
! isset( get_current_screen()->base ) || 'media' != get_current_screen()->base || | |
! isset( get_current_screen()->action ) || 'add' != get_current_screen()->action | |
) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(($,d)=>{ | |
if ( window.wpmudev_smush_image_on_upload ) { | |
return; | |
} | |
window.wpmudev_smush_image_on_upload = { | |
nonce: '<?php echo wp_create_nonce( "wpmudev_smush_single_image" ); ?>', | |
init: function() { | |
$(d).ajaxComplete( function( event, xhr, settings ) { | |
let attachment_id = wpmudev_smush_image_on_upload.get_query_var( 'attachment_id', settings.data ), | |
if_fetch = wpmudev_smush_image_on_upload.get_query_var( 'fetch', settings.data ); | |
if ( ! attachment_id || ! if_fetch ) { | |
return; | |
} | |
wpmudev_smush_image_on_upload.smush_single( attachment_id ); | |
}); | |
}, | |
smush_single: function( attachment_id ) { | |
let data = { | |
action : 'wpmudev_smush_single_image', | |
security : this.nonce, | |
attachment_id : attachment_id | |
} ; | |
$.post( ajaxurl, data, function( response ) { | |
if( response.success ){ | |
console.log( response.content ); | |
} | |
else{ | |
console.log( response.content ); | |
} | |
}); | |
}, | |
get_query_var: function( name, url ) { | |
if ( ! url ) { | |
return false; | |
} | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return false; | |
if (!results[2]) return false; | |
return decodeURIComponent( results[2].replace(/\+/g, " ") ); | |
} | |
}; | |
$(d).ready( function(){ | |
wpmudev_smush_image_on_upload.init(); | |
} ); | |
})(jQuery,document); | |
</script> | |
<?php | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment