| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
if ( function_exists( 'emr_delete_current_files' ) ) : |
| 5 |
|
| 6 |
/** |
| 7 |
* Re-Optimize an attachment after replace it with Enable Media Replace. |
| 8 |
* |
| 9 |
* @since 1.0 |
| 10 |
*/ |
| 11 |
add_action( 'enable-media-replace-upload-done', '_imagify_optimize_enable_media_replace' ); |
| 12 |
function _imagify_optimize_enable_media_replace( $guid ) { |
| 13 |
global $wpdb; |
| 14 |
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $guid ) ); |
| 15 |
|
| 16 |
// Stop if the attachment wasn't optimized yet by Imagify |
| 17 |
if ( ! get_post_meta( $attachment_id, '_imagify_data', true ) ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$optimization_level = get_post_meta( $attachment_id, '_imagify_optimization_level', true ); |
| 22 |
|
| 23 |
// Remove old optimization data |
| 24 |
delete_post_meta( $attachment_id, '_imagify_data' ); |
| 25 |
delete_post_meta( $attachment_id, '_imagify_status' ); |
| 26 |
delete_post_meta( $attachment_id, '_imagify_optimization_level' ); |
| 27 |
|
| 28 |
// Optimize it!!!!! |
| 29 |
$attachment = new Imagify_Attachment( $attachment_id ); |
| 30 |
$attachment->optimize( $optimization_level ); |
| 31 |
} |
| 32 |
|
| 33 |
endif; |