optimize();
if ( ! defined( 'DOING_AJAX' ) ) {
wp_safe_redirect( wp_get_referer() );
die();
}
// Return the optimization statistics
$output = get_imagify_attachment_optimization_text( $attachment_id );
wp_send_json_success( $output );
}
/**
* Process a manual upload by overriding the optimization level.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_manual_override_upload', '_do_admin_post_imagify_manual_override_upload' );
add_action( 'admin_post_imagify_manual_override_upload', '_do_admin_post_imagify_manual_override_upload' );
function _do_admin_post_imagify_manual_override_upload() {
if ( defined( 'DOING_AJAX' ) ) {
check_ajax_referer( 'imagify-manual-override-upload' );
} else {
check_admin_referer( 'imagify-manual-override-upload' );
}
if ( ! isset( $_GET['attachment_id'] ) || ! current_user_can( 'upload_files' ) ) {
if ( defined( 'DOING_AJAX' ) ) {
wp_send_json_error();
} else {
wp_nonce_ays( '' );
}
}
$attachment = new Imagify_Attachment( $_GET['attachment_id'] );
// Restore the backup file
$attachment->restore();
// Optimize it!!!!!
$attachment->optimize( (int) $_GET['optimization_level'] );
if ( ! defined( 'DOING_AJAX' ) ) {
wp_safe_redirect( wp_get_referer() );
die();
}
// Return the optimization statistics
$output = get_imagify_attachment_optimization_text( $attachment->id );
wp_send_json_success( $output );
}
/**
* Process a restoration to the original attachment.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_restore_upload', '_do_admin_post_imagify_restore_upload' );
add_action( 'admin_post_imagify_restore_upload', '_do_admin_post_imagify_restore_upload' );
function _do_admin_post_imagify_restore_upload() {
if ( defined( 'DOING_AJAX' ) ) {
check_ajax_referer( 'imagify-restore-upload' );
} else {
check_admin_referer( 'imagify-restore-upload' );
}
if ( ! isset( $_GET['attachment_id'] ) || ! current_user_can( 'upload_files' ) ) {
if ( defined( 'DOING_AJAX' ) ) {
wp_send_json_error();
} else {
wp_nonce_ays( '' );
}
}
$attachment = new Imagify_Attachment( $_GET['attachment_id'] );
// Restore the backup file
$attachment->restore();
if ( ! defined( 'DOING_AJAX' ) ) {
wp_safe_redirect( wp_get_referer() );
die();
}
// Return the optimization button
$output = '' . __( 'Optimize', 'imagify' ) . '';
wp_send_json_success( $output );
}
/**
* Get all unoptimized attachment ids.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_get_unoptimized_attachment_ids', '_do_wp_ajax_imagify_get_unoptimized_attachment_ids' );
function _do_wp_ajax_imagify_get_unoptimized_attachment_ids() {
check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
if ( ! current_user_can( 'upload_files' ) ) {
wp_send_json_error();
}
$user = new Imagify_User();
if ( $user->is_over_quota() ) {
wp_send_json_error( array( 'message' => 'over-quota' ) );
}
@set_time_limit( 0 );
$optimization_level = $_GET['optimization_level'];
$optimization_level = ( -1 != $optimization_level ) ? $optimization_level : get_imagify_option( 'optimization_level', 1 );
$optimization_level = (int) $optimization_level;
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_imagify_optimization_level',
'value' => $optimization_level,
'compare' => '!='
),
array(
'key' => '_imagify_optimization_level',
'compare' => 'NOT EXISTS'
),
array(
'key' => '_imagify_status',
'value' => 'error',
'compare' => '='
),
);
/**
* Filter the unoptimized attachments limit query
*
* @since 1.4.4
*
* @param int The limit (-1 for unlimited)
*/
$unoptimized_attachment_limit = apply_filters( 'imagify_unoptimized_attachment_limit', 10000 );
$args = array(
'fields' => 'ids',
'post_type' => 'attachment',
'post_status' => 'any',
'post_mime_type' => get_imagify_mime_type(),
'meta_query' => $meta_query,
'posts_per_page' => $unoptimized_attachment_limit,
'orderby' => 'ID',
'order' => 'DESC',
'no_found_rows' => true,
'update_post_term_cache' => false,
);
global $wpdb;
$data = array();
$attachments = new WP_Query( $args );
$ids = $attachments->posts;
$ids = array_filter( (array) $ids );
$sql_ids = implode( ',', $ids );
if ( empty( $sql_ids ) ) {
wp_send_json_error( array( 'message' => 'no-images' ) );
}
// Get attachments filename
$attachments_filename = $wpdb->get_results(
"SELECT pm.post_id as id, pm.meta_value as value
FROM $wpdb->postmeta as pm
WHERE pm.meta_key= '_wp_attached_file'
AND pm.post_id IN ($sql_ids)
ORDER BY pm.post_id DESC"
, ARRAY_A
);
$attachments_filename = imagify_query_results_combine( $ids, $attachments_filename );
// Get attachments data
$attachments_data = $wpdb->get_results(
"SELECT pm.post_id as id, pm.meta_value as value
FROM $wpdb->postmeta as pm
WHERE pm.meta_key= '_imagify_data'
AND pm.post_id IN ($sql_ids)
ORDER BY pm.post_id DESC"
, ARRAY_A
);
$attachments_data = imagify_query_results_combine( $ids, $attachments_data );
$attachments_data = array_map( 'maybe_unserialize', $attachments_data );
// Get attachments optimization level
$attachments_optimization_level = $wpdb->get_results(
"SELECT pm.post_id as id, pm.meta_value as value
FROM $wpdb->postmeta as pm
WHERE pm.meta_key= '_imagify_optimization_level'
AND pm.post_id IN ($sql_ids)
ORDER BY pm.post_id DESC"
, ARRAY_A
);
$attachments_optimization_level = imagify_query_results_combine( $ids, $attachments_optimization_level );
// Get attachments status
$attachments_status = $wpdb->get_results(
"SELECT pm.post_id as id, pm.meta_value as value
FROM $wpdb->postmeta as pm
WHERE pm.meta_key= '_imagify_status'
AND pm.post_id IN ($sql_ids)
ORDER BY pm.post_id DESC"
, ARRAY_A
);
$attachments_status = imagify_query_results_combine( $ids, $attachments_status );
// Save the optimization level in a transient to retrieve it later during the process
set_transient( 'imagify_bulk_optimization_level', $optimization_level );
foreach( $ids as $id ) {
/** This filter is documented in inc/functions/process.php */
$file_path = apply_filters( 'imagify_file_path', get_imagify_attached_file( $attachments_filename[ $id ] ) );
if ( file_exists( $file_path ) ) {
$attachment_data = ( isset( $attachments_data[ $id ] ) ) ? $attachments_data[ $id ] : false;
$attachment_error = '';
if ( isset( $attachment_data['sizes']['full']['error'] ) ) {
$attachment_error = $attachment_data['sizes']['full']['error'];
}
$attachment_error = trim( $attachment_error );
$attachment_status = ( isset( $attachments_status[ $id ] ) ) ? $attachments_status[ $id ] : false;
$attachment_optimization_level = ( isset( $attachments_optimization_level[ $id ] ) ) ? $attachments_optimization_level[ $id ] : false;
$attachment_backup_path = get_imagify_attachment_backup_path( $file_path );
// Don't try to re-optimize if the optimization level is still the same
if ( $optimization_level === $attachment_optimization_level && is_string( $attachment_error ) ) {
continue;
}
// Don't try to re-optimize if there is no backup file
if ( $optimization_level !== $attachment_optimization_level && ! file_exists( $attachment_backup_path ) && $attachment_status == 'success' ) {
continue;
}
// Don't try to re-optimize images already compressed
if ( $attachment_optimization_level >= $optimization_level && $attachment_status == 'already_optimized' ) {
continue;
}
// Don't try to re-optimize images with an empty error message
if ( $attachment_status == 'error' && empty( $attachment_error ) ) {
continue;
}
$data[ '_' . $id ] = get_imagify_attachment_url( $attachments_filename[ $id ] );
}
}
if ( (bool) $data ) {
wp_send_json_success( $data );
}
wp_send_json_error( array( 'message' => 'no-images' ) );
}
/**
* Process all thumbnails of a specific image with Imagify with the bulk method.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_bulk_upload', '_do_wp_ajax_imagify_bulk_upload' );
function _do_wp_ajax_imagify_bulk_upload() {
check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
if ( ! isset( $_POST['image'] ) || ! current_user_can( 'upload_files' ) ) {
wp_send_json_error();
}
$attachment_id = (int) $_POST['image'];
$attachment = new Imagify_Attachment( $_POST['image'] );
$optimization_level = get_transient( 'imagify_bulk_optimization_level' );
// Restore it if the optimization level is updated
if ( $optimization_level !== $attachment->get_optimization_level() ) {
$attachment->restore();
}
// Optimize it!!!!!
$attachment->optimize( $optimization_level );
// Return the optimization statistics
$fullsize_data = $attachment->get_size_data();
$stats_data = $attachment->get_stats_data();
$user = new Imagify_User();
$data = array();
if ( ! $attachment->is_optimized() ) {
$data['success'] = false;
$data['error'] = $fullsize_data['error'];
wp_send_json_error( $data );
}
$data['success'] = true;
$data['original_size'] = $fullsize_data['original_size'];
$data['new_size'] = $fullsize_data['optimized_size'];
$data['percent'] = $fullsize_data['percent'];
$data['overall_saving'] = $stats_data['original_size'] - $stats_data['optimized_size'];
$data['original_overall_size'] = $stats_data['original_size'];
$data['new_overall_size'] = $stats_data['optimized_size'];
$data['thumbnails'] = $attachment->get_optimized_sizes_count();
wp_send_json_success( $data );
}
/**
* Create a new Imagify account.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_signup', '_do_wp_ajax_imagify_signup' );
function _do_wp_ajax_imagify_signup() {
check_ajax_referer( 'imagify-signup', 'imagifysignupnonce' );
if ( ! isset( $_GET['email'] ) ) {
wp_send_json_error();
}
$data = array(
'email' => $_GET['email'],
'password' => wp_generate_password( 12, false ),
'lang' => get_locale()
);
$response = add_imagify_user( $data );
if ( is_wp_error( $response ) ) {
wp_send_json_error( $response->get_error_message() );
}
wp_send_json_success();
}
/**
* Process an API key check validity.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_check_api_key_validity', '_do_wp_ajax_imagify_check_api_key_validity' );
function _do_wp_ajax_imagify_check_api_key_validity() {
check_ajax_referer( 'imagify-check-api-key', 'imagifycheckapikeynonce' );
if ( ! isset( $_GET['api_key'] ) ) {
wp_send_json_error();
}
$response = get_imagify_status( $_GET['api_key'] );
if ( is_wp_error( $response ) ) {
wp_send_json_error( $response->get_error_message() );
}
$options = get_site_option( IMAGIFY_SETTINGS_SLUG );
$options['api_key'] = sanitize_key( $_GET['api_key'] );
update_site_option( IMAGIFY_SETTINGS_SLUG, $options );
wp_send_json_success();
}
/**
* Process a dismissed notice.
*
* @since 1.0
*/
add_action( 'wp_ajax_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
add_action( 'admin_post_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
function _do_admin_post_imagify_dismiss_notice() {
if ( defined( 'DOING_AJAX' ) ) {
check_ajax_referer( 'imagify-dismiss-notice' );
} else {
check_admin_referer( 'imagify-dismiss-notice' );
}
if ( ! isset( $_GET['notice'] ) || ! current_user_can( 'manage_options' ) ) {
if ( defined( 'DOING_AJAX' ) ) {
wp_send_json_error();
} else {
wp_nonce_ays( '' );
}
}
$notice = $_GET['notice'];
imagify_dismiss_notice( $notice );
/**
* Fires when a notice is dismissed.
*
* @since 1.4.2
*
* @param int $notice The notice slug
*/
do_action( 'imagify_dismiss_notice', $notice );
if ( ! defined( 'DOING_AJAX' ) ) {
wp_safe_redirect( wp_get_referer() );
die();
}
wp_send_json_success();
}
/**
* Disable a plugin which can be in conflict with Imagify
*
* @since 1.2
*/
add_action( 'admin_post_imagify_deactivate_plugin', '_imagify_deactivate_plugin' );
function _imagify_deactivate_plugin() {
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'imagifydeactivatepluginnonce' ) ) {
wp_nonce_ays( '' );
}
deactivate_plugins( $_GET['plugin'] );
wp_safe_redirect( wp_get_referer() );
die();
}
/**
* Get admin bar profile output
*
* @since 1.2.3
*/
add_action( 'wp_ajax_imagify_get_admin_bar_profile', '_do_wp_ajax_imagify_get_admin_bar_profile' );
function _do_wp_ajax_imagify_get_admin_bar_profile() {
check_ajax_referer( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' );
$user = new Imagify_User();
$unconsumed_quota = $user->get_percent_unconsumed_quota();
$meteo_icon = '';
$bar_class = 'positive';
$message = '';
if ( $unconsumed_quota >= 21 && $unconsumed_quota <= 50 ) {
$bar_class = 'neutral';
$meteo_icon = '
';
}
elseif ( $unconsumed_quota <= 20 ) {
$bar_class = 'negative';
$meteo_icon = '
';
}
if ( $unconsumed_quota <= 20 && $unconsumed_quota > 0 ) {
$message = '
' . __( 'Oops, It\'s almost over!', 'imagify' ) . '
' . sprintf( __( 'You have almost used all your credit.%sDon\'t forget to upgrade your subscription to continue optimizing your images.', 'imagify' ), '
' ) . '
' . __( 'Oops, It\'s Over!', 'imagify' ) . '
' . sprintf( __( 'You have consumed all your credit for this month. You will have %s back on %s.', 'imagify' ), size_format( $user->quota * 1048576 ), date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) ) ) . '