PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.4.6
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.4.6
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.4.6, at inc/admin/ajax.php

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