PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.5
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.5
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / admin / ajax.php

ajax.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.5, at inc/admin/ajax.php

651 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
4 /**
5 * Process all thumbnails of a specific image with Imagify with the manual method.
6 *
7 * @since 1.0
8 * @author Jonathan Buttigieg
9 */
10 add_action( 'wp_ajax_imagify_manual_upload' , '_do_admin_post_imagify_manual_upload' );
11 add_action( 'admin_post_imagify_manual_upload' , '_do_admin_post_imagify_manual_upload' );
12 function _do_admin_post_imagify_manual_upload() {
13 if ( defined( 'DOING_AJAX' ) ) {
14 check_ajax_referer( 'imagify-manual-upload' );
15 } else {
16 check_admin_referer( 'imagify-manual-upload' );
17 }
18
19 if ( ! isset( $_GET['attachment_id'], $_GET['context'] ) || ! current_user_can( 'upload_files' ) ) {
20 if ( defined( 'DOING_AJAX' ) ) {
21 wp_send_json_error();
22 } else {
23 wp_nonce_ays( '' );
24 }
25 }
26
27 $context = $_GET['context'];
28 $attachment_id = $_GET['attachment_id'];
29 $class_name = get_imagify_attachment_class_name( $_GET['context'] );
30 $attachment = new $class_name( $attachment_id );
31
32 // Optimize it!!!!!
33 $attachment->optimize();
34
35 if ( ! defined( 'DOING_AJAX' ) ) {
36 wp_safe_redirect( wp_get_referer() );
37 die();
38 }
39
40 // Return the optimization statistics
41 $output = get_imagify_attachment_optimization_text( $attachment, $context );
42 wp_send_json_success( $output );
43 }
44
45 /**
46 * Process a manual upload by overriding the optimization level.
47 *
48 * @since 1.0
49 * @author Jonathan Buttigieg
50 */
51 add_action( 'wp_ajax_imagify_manual_override_upload', '_do_admin_post_imagify_manual_override_upload' );
52 add_action( 'admin_post_imagify_manual_override_upload', '_do_admin_post_imagify_manual_override_upload' );
53 function _do_admin_post_imagify_manual_override_upload() {
54 if ( defined( 'DOING_AJAX' ) ) {
55 check_ajax_referer( 'imagify-manual-override-upload' );
56 } else {
57 check_admin_referer( 'imagify-manual-override-upload' );
58 }
59
60 if ( ! isset( $_GET['attachment_id'], $_GET['context'] ) || ! current_user_can( 'upload_files' ) ) {
61 if ( defined( 'DOING_AJAX' ) ) {
62 wp_send_json_error();
63 } else {
64 wp_nonce_ays( '' );
65 }
66 }
67
68 $context = $_GET['context'];
69 $class_name = get_imagify_attachment_class_name( $context );
70 $attachment = new $class_name( $_GET['attachment_id'] );
71
72 // Restore the backup file
73 $attachment->restore();
74
75 // Optimize it!!!!!
76 $attachment->optimize( (int) $_GET['optimization_level'] );
77
78 if ( ! defined( 'DOING_AJAX' ) ) {
79 wp_safe_redirect( wp_get_referer() );
80 die();
81 }
82
83 // Return the optimization statistics
84 $output = get_imagify_attachment_optimization_text( $attachment, $context );
85 wp_send_json_success( $output );
86 }
87
88 /**
89 * Process a restoration to the original attachment.
90 *
91 * @since 1.0
92 * @author Jonathan Buttigieg
93 */
94 add_action( 'wp_ajax_imagify_restore_upload', '_do_admin_post_imagify_restore_upload' );
95 add_action( 'admin_post_imagify_restore_upload', '_do_admin_post_imagify_restore_upload' );
96 function _do_admin_post_imagify_restore_upload() {
97 if ( defined( 'DOING_AJAX' ) ) {
98 check_ajax_referer( 'imagify-restore-upload' );
99 } else {
100 check_admin_referer( 'imagify-restore-upload' );
101 }
102
103 if ( ! isset( $_GET['attachment_id'], $_GET['context'] ) || ! current_user_can( 'upload_files' ) ) {
104 if ( defined( 'DOING_AJAX' ) ) {
105 wp_send_json_error();
106 } else {
107 wp_nonce_ays( '' );
108 }
109 }
110
111 $class_name = get_imagify_attachment_class_name( $_GET['context'] );
112 $attachment = new $class_name( $_GET['attachment_id'] );
113
114 // Restore the backup file
115 $attachment->restore();
116
117 if ( ! defined( 'DOING_AJAX' ) ) {
118 wp_safe_redirect( wp_get_referer() );
119 die();
120 }
121
122 // Return the optimization button
123 $output = '<a id="imagify-upload-' . $attachment->id . '" href="' . get_imagify_admin_url( 'manual-upload', array( 'attachment_id' => $attachment->id ) ) . '" class="button-primary button-imagify-manual-upload" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">' . __( 'Optimize', 'imagify' ) . '</a>';
124 wp_send_json_success( $output );
125 }
126
127 /**
128 * Get all unoptimized attachment ids.
129 *
130 * @since 1.0
131 * @author Jonathan Buttigieg
132 */
133 add_action( 'wp_ajax_imagify_get_unoptimized_attachment_ids', '_do_wp_ajax_imagify_get_unoptimized_attachment_ids' );
134 function _do_wp_ajax_imagify_get_unoptimized_attachment_ids() {
135 check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
136
137 if ( ! current_user_can( 'upload_files' ) ) {
138 wp_send_json_error();
139 }
140
141 if ( ! imagify_valid_key() ) {
142 wp_send_json_error( array( 'message' => 'invalid-api-key' ) );
143 }
144
145 $user = new Imagify_User();
146
147 if ( $user->is_over_quota() ) {
148 wp_send_json_error( array( 'message' => 'over-quota' ) );
149 }
150
151 @set_time_limit( 0 );
152
153 $optimization_level = $_GET['optimization_level'];
154 $optimization_level = ( -1 != $optimization_level ) ? $optimization_level : get_imagify_option( 'optimization_level', 1 );
155 $optimization_level = (int) $optimization_level;
156
157 $meta_query = array(
158 'relation' => 'OR',
159 array(
160 'key' => '_imagify_optimization_level',
161 'value' => $optimization_level,
162 'compare' => '!='
163 ),
164 array(
165 'key' => '_imagify_optimization_level',
166 'compare' => 'NOT EXISTS'
167 ),
168 array(
169 'key' => '_imagify_status',
170 'value' => 'error',
171 'compare' => '='
172 ),
173 );
174
175 /**
176 * Filter the unoptimized attachments limit query
177 *
178 * @since 1.4.4
179 *
180 * @param int The limit (-1 for unlimited)
181 */
182 $unoptimized_attachment_limit = apply_filters( 'imagify_unoptimized_attachment_limit', 10000 );
183
184 $args = array(
185 'fields' => 'ids',
186 'post_type' => 'attachment',
187 'post_status' => 'any',
188 'post_mime_type' => get_imagify_mime_type(),
189 'meta_query' => $meta_query,
190 'posts_per_page' => $unoptimized_attachment_limit,
191 'orderby' => 'ID',
192 'order' => 'DESC',
193 'no_found_rows' => true,
194 'update_post_term_cache' => false,
195 );
196
197 global $wpdb;
198
199 $data = array();
200 $attachments = new WP_Query( $args );
201 $ids = $attachments->posts;
202 $ids = array_filter( (array) $ids );
203 $sql_ids = implode( ',', $ids );
204
205 if ( empty( $sql_ids ) ) {
206 wp_send_json_error( array( 'message' => 'no-images' ) );
207 }
208
209 // Get attachments filename
210 $attachments_filename = $wpdb->get_results(
211 "SELECT pm.post_id as id, pm.meta_value as value
212 FROM $wpdb->postmeta as pm
213 WHERE pm.meta_key= '_wp_attached_file'
214 AND pm.post_id IN ($sql_ids)
215 ORDER BY pm.post_id DESC"
216 , ARRAY_A
217 );
218
219 $attachments_filename = imagify_query_results_combine( $ids, $attachments_filename );
220
221 // Get attachments data
222 $attachments_data = $wpdb->get_results(
223 "SELECT pm.post_id as id, pm.meta_value as value
224 FROM $wpdb->postmeta as pm
225 WHERE pm.meta_key= '_imagify_data'
226 AND pm.post_id IN ($sql_ids)
227 ORDER BY pm.post_id DESC"
228 , ARRAY_A
229 );
230
231 $attachments_data = imagify_query_results_combine( $ids, $attachments_data );
232 $attachments_data = array_map( 'maybe_unserialize', $attachments_data );
233
234 // Get attachments optimization level
235 $attachments_optimization_level = $wpdb->get_results(
236 "SELECT pm.post_id as id, pm.meta_value as value
237 FROM $wpdb->postmeta as pm
238 WHERE pm.meta_key= '_imagify_optimization_level'
239 AND pm.post_id IN ($sql_ids)
240 ORDER BY pm.post_id DESC"
241 , ARRAY_A
242 );
243
244 $attachments_optimization_level = imagify_query_results_combine( $ids, $attachments_optimization_level );
245
246 // Get attachments status
247 $attachments_status = $wpdb->get_results(
248 "SELECT pm.post_id as id, pm.meta_value as value
249 FROM $wpdb->postmeta as pm
250 WHERE pm.meta_key= '_imagify_status'
251 AND pm.post_id IN ($sql_ids)
252 ORDER BY pm.post_id DESC"
253 , ARRAY_A
254 );
255
256 $attachments_status = imagify_query_results_combine( $ids, $attachments_status );
257
258 // Save the optimization level in a transient to retrieve it later during the process
259 set_transient( 'imagify_bulk_optimization_level', $optimization_level );
260
261 foreach( $ids as $id ) {
262 /** This filter is documented in inc/functions/process.php */
263 $file_path = apply_filters( 'imagify_file_path', get_imagify_attached_file( $attachments_filename[ $id ] ) );
264
265 if ( file_exists( $file_path ) ) {
266 $attachment_data = ( isset( $attachments_data[ $id ] ) ) ? $attachments_data[ $id ] : false;
267 $attachment_error = '';
268
269 if ( isset( $attachment_data['sizes']['full']['error'] ) ) {
270 $attachment_error = $attachment_data['sizes']['full']['error'];
271 }
272
273 $attachment_error = trim( $attachment_error );
274 $attachment_status = ( isset( $attachments_status[ $id ] ) ) ? $attachments_status[ $id ] : false;
275 $attachment_optimization_level = ( isset( $attachments_optimization_level[ $id ] ) ) ? $attachments_optimization_level[ $id ] : false;
276 $attachment_backup_path = get_imagify_attachment_backup_path( $file_path );
277
278 // Don't try to re-optimize if the optimization level is still the same
279 if ( $optimization_level === $attachment_optimization_level && is_string( $attachment_error ) ) {
280 continue;
281 }
282
283 // Don't try to re-optimize if there is no backup file
284 if ( $optimization_level !== $attachment_optimization_level && ! file_exists( $attachment_backup_path ) && $attachment_status == 'success' ) {
285 continue;
286 }
287
288 // Don't try to re-optimize images already compressed
289 if ( $attachment_optimization_level >= $optimization_level && $attachment_status == 'already_optimized' ) {
290 continue;
291 }
292
293 // Don't try to re-optimize images with an empty error message
294 if ( $attachment_status == 'error' && empty( $attachment_error ) ) {
295 continue;
296 }
297
298 $data[ '_' . $id ] = get_imagify_attachment_url( $attachments_filename[ $id ] );
299 }
300 }
301
302 if ( (bool) $data ) {
303 wp_send_json_success( $data );
304 }
305
306 wp_send_json_error( array( 'message' => 'no-images' ) );
307 }
308
309 /**
310 * Process all thumbnails of a specific image with Imagify with the bulk method.
311 *
312 * @since 1.0
313 * @author Jonathan Buttigieg
314 */
315 add_action( 'wp_ajax_imagify_bulk_upload', '_do_wp_ajax_imagify_bulk_upload' );
316 function _do_wp_ajax_imagify_bulk_upload() {
317 check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
318
319 if ( ! isset( $_POST['image'], $_POST['context'] ) || ! current_user_can( 'upload_files' ) ) {
320 wp_send_json_error();
321 }
322
323 $class_name = get_imagify_attachment_class_name( $_POST['context'] );
324 $attachment = new $class_name( $_POST['image'] );
325 $optimization_level = get_transient( 'imagify_bulk_optimization_level' );
326
327 // Restore it if the optimization level is updated
328 if ( $optimization_level !== $attachment->get_optimization_level() ) {
329 $attachment->restore();
330 }
331
332 // Optimize it!!!!!
333 $attachment->optimize( $optimization_level );
334
335 // Return the optimization statistics
336 $fullsize_data = $attachment->get_size_data();
337 $stats_data = $attachment->get_stats_data();
338 $user = new Imagify_User();
339 $data = array();
340
341 if ( ! $attachment->is_optimized() ) {
342 $data['success'] = false;
343 $data['error'] = $fullsize_data['error'];
344
345 wp_send_json_error( $data );
346 }
347
348 $data['success'] = true;
349 $data['original_size'] = $fullsize_data['original_size'];
350 $data['new_size'] = $fullsize_data['optimized_size'];
351 $data['percent'] = $fullsize_data['percent'];
352 $data['overall_saving'] = $stats_data['original_size'] - $stats_data['optimized_size'];
353 $data['original_overall_size'] = $stats_data['original_size'];
354 $data['new_overall_size'] = $stats_data['optimized_size'];
355 $data['thumbnails'] = $attachment->get_optimized_sizes_count();
356
357 wp_send_json_success( $data );
358 }
359
360 /**
361 * Create a new Imagify account.
362 *
363 * @since 1.0
364 * @author Jonathan Buttigieg
365 */
366 add_action( 'wp_ajax_imagify_signup', '_do_wp_ajax_imagify_signup' );
367 function _do_wp_ajax_imagify_signup() {
368 check_ajax_referer( 'imagify-signup', 'imagifysignupnonce' );
369
370 if ( ! isset( $_GET['email'] ) ) {
371 wp_send_json_error();
372 }
373
374 $data = array(
375 'email' => $_GET['email'],
376 'password' => wp_generate_password( 12, false ),
377 'lang' => get_locale()
378 );
379
380 $response = add_imagify_user( $data );
381
382 if ( is_wp_error( $response ) ) {
383 wp_send_json_error( $response->get_error_message() );
384 }
385
386 wp_send_json_success();
387 }
388
389 /**
390 * Process an API key check validity.
391 *
392 * @since 1.0
393 * @author Jonathan Buttigieg
394 */
395 add_action( 'wp_ajax_imagify_check_api_key_validity', '_do_wp_ajax_imagify_check_api_key_validity' );
396 function _do_wp_ajax_imagify_check_api_key_validity() {
397 check_ajax_referer( 'imagify-check-api-key', 'imagifycheckapikeynonce' );
398
399 if ( ! isset( $_GET['api_key'] ) ) {
400 wp_send_json_error();
401 }
402
403 $response = get_imagify_status( $_GET['api_key'] );
404
405 if ( is_wp_error( $response ) ) {
406 wp_send_json_error( $response->get_error_message() );
407 }
408
409 $options = get_site_option( IMAGIFY_SETTINGS_SLUG );
410 $options['api_key'] = sanitize_key( $_GET['api_key'] );
411
412 update_site_option( IMAGIFY_SETTINGS_SLUG, $options );
413
414 wp_send_json_success();
415 }
416
417 /**
418 * Process a dismissed notice.
419 *
420 * @since 1.0
421 * @author Jonathan Buttigieg
422 */
423 add_action( 'wp_ajax_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
424 add_action( 'admin_post_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
425 function _do_admin_post_imagify_dismiss_notice() {
426 if ( defined( 'DOING_AJAX' ) ) {
427 check_ajax_referer( 'imagify-dismiss-notice' );
428 } else {
429 check_admin_referer( 'imagify-dismiss-notice' );
430 }
431
432 if ( ! isset( $_GET['notice'] ) || ! current_user_can( 'manage_options' ) ) {
433 if ( defined( 'DOING_AJAX' ) ) {
434 wp_send_json_error();
435 } else {
436 wp_nonce_ays( '' );
437 }
438 }
439
440 $notice = $_GET['notice'];
441
442 imagify_dismiss_notice( $notice );
443
444 /**
445 * Fires when a notice is dismissed.
446 *
447 * @since 1.4.2
448 *
449 * @param int $notice The notice slug
450 */
451 do_action( 'imagify_dismiss_notice', $notice );
452
453 if ( ! defined( 'DOING_AJAX' ) ) {
454 wp_safe_redirect( wp_get_referer() );
455 die();
456 }
457
458 wp_send_json_success();
459 }
460
461 /**
462 * Disable a plugin which can be in conflict with Imagify
463 *
464 * @since 1.2
465 * @author Jonathan Buttigieg
466 */
467 add_action( 'admin_post_imagify_deactivate_plugin', '_imagify_deactivate_plugin' );
468 function _imagify_deactivate_plugin() {
469 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'imagifydeactivatepluginnonce' ) ) {
470 wp_nonce_ays( '' );
471 }
472
473 deactivate_plugins( $_GET['plugin'] );
474
475 wp_safe_redirect( wp_get_referer() );
476 die();
477 }
478
479 /**
480 * Get admin bar profile output
481 *
482 * @since 1.2.3
483 * @author Jonathan Buttigieg
484 */
485 add_action( 'wp_ajax_imagify_get_admin_bar_profile', '_do_wp_ajax_imagify_get_admin_bar_profile' );
486 function _do_wp_ajax_imagify_get_admin_bar_profile() {
487 check_ajax_referer( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' );
488
489 $user = new Imagify_User();
490 $unconsumed_quota = $user->get_percent_unconsumed_quota();
491 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'sun.svg" width="37" height="38" alt="" />';
492 $bar_class = 'positive';
493 $message = '';
494
495 if ( $unconsumed_quota >= 21 && $unconsumed_quota <= 50 ) {
496 $bar_class = 'neutral';
497 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'cloudy-sun.svg" width="37" height="38" alt="" />';
498 }
499 elseif ( $unconsumed_quota <= 20 ) {
500 $bar_class = 'negative';
501 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'stormy.svg" width="38" height="36" alt="" />';
502 }
503
504 if ( $unconsumed_quota <= 20 && $unconsumed_quota > 0 ) {
505 $message = '
506 <div class="imagify-error">
507 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s almost over!', 'imagify' ) . '</strong></p>
508 <p>' . sprintf( __( 'You have almost used all your credit.%sDon\'t forget to upgrade your subscription to continue optimizing your images.', 'imagify' ), '<br/><br/>' ) . '</p>
509 <p class="center txt-center text-center"><a class="btn btn-ghost" href="' . IMAGIFY_APP_MAIN . '/#/subscription" target="_blank">' . __( 'View My Subscription', 'imagify' ) . '</a></p>
510 </div>
511 ';
512 }
513
514 if ( $unconsumed_quota === 0 ) {
515 $message = '
516 <div class="imagify-error">
517 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s Over!', 'imagify' ) . '</strong></p>
518 <p>' . sprintf( __( 'You have consumed all your credit for this month. You will have <strong>%s back on %s</strong>.', 'imagify' ), size_format( $user->quota * 1048576 ), date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) ) ) . '</p>
519 <p class="center txt-center text-center"><a class="btn btn-ghost" href="' . IMAGIFY_APP_MAIN . '/#/subscription" target="_blank">' . __( 'Upgrade My Subscription', 'imagify' ) . '</a></p>
520 </div>
521 ';
522 }
523
524 // custom HTML
525 $quota_section = '
526 <div class="imagify-admin-bar-quota">
527 <div class="imagify-abq-row">';
528
529 if ( 1 === $user->plan_id ) {
530 $quota_section .= '
531 <div class="imagify-meteo-icon">
532 ' . $meteo_icon . '
533 </div>';
534 }
535
536 $quota_section .= '
537 <div class="imagify-account">
538 <p class="imagify-meteo-title">' . __( 'Account status', 'imagify' ) . '</p>
539 <p class="imagify-meteo-subs">' . __( 'Your subscription:', 'imagify' ) . '&nbsp;<strong class="imagify-user-plan">' . $user->plan_label . '</strong></p>
540 </div>
541 </div>';
542
543 if ( 1 === $user->plan_id ) {
544 $quota_section .= '
545 <div class="imagify-abq-row">
546 <div class="imagify-space-left">
547 <p>' . sprintf( __( 'You have %s space credit left', 'imagify'), '<span class="imagify-unconsumed-percent">' . $unconsumed_quota . '%</span>' ) . '</p>
548 <div class="imagify-bar-' . $bar_class . '">
549 <div style="width: ' . $unconsumed_quota . '%;" class="imagify-unconsumed-bar imagify-progress"></div>
550 </div>
551 </div>
552 </div>';
553 }
554
555 $quota_section .= '
556 <p class="imagify-abq-row">
557 <a class="imagify-account-link" href="' . IMAGIFY_APP_MAIN . '/#/subscription" target="_blank">
558 <span class="dashicons dashicons-admin-users"></span>
559 <span class="button-text">' . __( 'View my subscription', 'imagify' ) . '</span>
560 </a>
561 </p>
562 </div>
563 ' . $message;
564
565 wp_send_json_success( $quota_section );
566 }
567
568 /**
569 * Optimize image on picture uploading with async request
570 *
571 * @since 1.5
572 * @author Julio Potier
573 **/
574 add_action( 'wp_ajax_imagify_async_optimize_upload_new_media', '_do_admin_post_async_optimize_upload_new_media' );
575 function _do_admin_post_async_optimize_upload_new_media() {
576 if ( isset( $_POST['_ajax_nonce'], $_POST['attachment_id'], $_POST['metadata'], $_POST['context'] )
577 && check_ajax_referer( 'new_media-' . $_POST['attachment_id'] )
578 ) {
579 $class_name = get_imagify_attachment_class_name( $_POST['context'] );
580 $attachment = new $class_name( $_POST['attachment_id'] );
581
582 // Optimize it!!!!!
583 $attachment->optimize( null, $_POST['metadata'] );
584
585 die( 1 );
586 }
587 }
588 /**
589 * Optimize image on picture editing with async request
590 *
591 * @since 1.4
592 * @author Julio Potier
593 **/
594 add_action( 'wp_ajax_imagify_async_optimize_save_image_editor_file', '_do_admin_post_async_optimize_save_image_editor_file' );
595 function _do_admin_post_async_optimize_save_image_editor_file() {
596 if ( isset( $_POST['do'], $_POST['postid'] )
597 && check_ajax_referer( 'image_editor-' . $_POST['postid'] )
598 && get_post_meta( $_POST['postid'], '_imagify_data', true )
599 ) {
600
601 $attachment_id = $_POST['postid'];
602 $optimization_level = get_post_meta( $attachment_id, '_imagify_optimization_level', true );
603 $attachment = new Imagify_Attachment( $attachment_id );
604 $metadata = wp_get_attachment_metadata( $attachment_id );
605
606 // Remove old optimization data
607 delete_post_meta( $attachment_id, '_imagify_data' );
608 delete_post_meta( $attachment_id, '_imagify_status' );
609 delete_post_meta( $attachment_id, '_imagify_optimization_level' );
610
611 if ( 'restore' === $_POST['do'] ) {
612 // Restore the backup file
613 $attachment->restore();
614
615 // Get old metadata to regenerate all thumbnails
616 $metadata = array( 'sizes' => array() );
617 $backup_sizes = (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
618
619 foreach ( $backup_sizes as $size_key => $size_data ) {
620 $size_key = str_replace( '-origin', '' , $size_key );
621 $metadata['sizes'][ $size_key ] = $size_data;
622 }
623 }
624
625 // Optimize it!!!!!
626 $attachment->optimize( $optimization_level, $metadata );
627
628 die( 1 );
629 }
630 }
631
632 /**
633 * Get pricings from API for Onetime and Plans at the same time
634 *
635 * @since 1.5
636 * @author Geoffrey
637 */
638 add_action( 'wp_ajax_imagify_get_prices', '_imagify_get_prices_from_api' );
639 function _imagify_get_prices_from_api() {
640 if ( check_ajax_referer( 'imagify_get_pricing_' . get_current_user_id(), 'imagifynonce', false) ) {
641
642 $data = array();
643 $data['onetimes'] = get_imagify_packs_prices();
644 $data['monthlies'] = get_imagify_plans_prices();
645
646 wp_send_json_success( $data );
647 }
648 else {
649 wp_send_json_error( 'check_ajax_referer failed' );
650 }
651 }