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

539 lines 17.1 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 'no_found_rows' => true,
182 'update_post_term_cache' => false,
183 );
184
185 $data = array();
186 $query = new WP_Query( $args );
187 $ids = $query->posts;
188
189 // Save the optimization level in a transient to retrieve it later during the process
190 set_transient( 'imagify_bulk_optimization_level', $optimization_level );
191
192 foreach( $ids as $id ) {
193 /** This filter is documented in inc/functions/process.php */
194 $file_path = apply_filters( 'imagify_file_path', get_attached_file( $id ) );
195
196 if ( file_exists( $file_path ) ) {
197 $attachment = new Imagify_Attachment( $id );
198 $attachment_error = $attachment->get_optimized_error();
199 $attachment_error = trim( $attachment_error );
200 $attachment_status = $attachment->get_status();
201
202 // Don't try to re-optimize if the optimization level is still the same
203 if ( $optimization_level === $attachment->get_optimization_level() && ! $attachment->has_error() ) {
204 continue;
205 }
206
207 // Don't try to re-optimize if there is no backup file
208 if ( $optimization_level !== $attachment->get_optimization_level() && ! $attachment->has_backup() && $attachment->is_optimized() ) {
209 continue;
210 }
211
212 // Don't try to re-optimize images already compressed
213 if ( $attachment->get_optimization_level() >= $optimization_level && $attachment_status == 'already_optimized' ) {
214 continue;
215 }
216
217 // Don't try to re-optimize images with an empty error message
218 if ( $attachment_status == 'error' && empty( $attachment_error ) ) {
219 continue;
220 }
221
222 $data[ '_' . $id ] = wp_get_attachment_url( $id );
223 }
224 }
225
226 if ( (bool) $data ) {
227 wp_send_json_success( $data );
228 }
229
230 wp_send_json_error( array( 'message' => 'no-images' ) );
231 }
232
233 /**
234 * Process all thumbnails of a specific image with Imagify with the bulk method.
235 *
236 * @since 1.0
237 */
238 add_action( 'wp_ajax_imagify_bulk_upload', '_do_wp_ajax_imagify_bulk_upload' );
239 function _do_wp_ajax_imagify_bulk_upload() {
240 check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
241
242 if ( ! isset( $_POST['image'] ) || ! current_user_can( 'upload_files' ) ) {
243 wp_send_json_error();
244 }
245
246 $attachment_id = (int) $_POST['image'];
247 $attachment = new Imagify_Attachment( $_POST['image'] );
248 $optimization_level = get_transient( 'imagify_bulk_optimization_level' );
249
250 // Restore it if the optimization level is updated
251 if ( $optimization_level !== $attachment->get_optimization_level() ) {
252 $attachment->restore();
253 }
254
255 // Optimize it!!!!!
256 $attachment->optimize( $optimization_level );
257
258 // Return the optimization statistics
259 $fullsize_data = $attachment->get_size_data();
260 $stats_data = $attachment->get_stats_data();
261 $saving_data = imagify_count_saving_data();
262 $user = new Imagify_User();
263 $data = array(
264 'global_already_optimized_attachments' => $saving_data['count'],
265 'global_optimized_attachments' => imagify_count_optimized_attachments(),
266 'global_unoptimized_attachments' => imagify_count_unoptimized_attachments(),
267 'global_errors_attachments' => imagify_count_error_attachments(),
268 'global_optimized_attachments_percent' => imagify_percent_optimized_attachments(),
269 'global_optimized_percent' => $saving_data['percent'],
270 'global_original_human' => size_format( $saving_data['original_size'], 1 ),
271 'global_optimized_human' => size_format( $saving_data['optimized_size'], 1 ),
272 'global_unconsumed_quota' => $user->get_percent_unconsumed_quota(),
273 );
274
275 if ( ! $attachment->is_optimized() ) {
276 $data['success'] = false;
277 $data['error'] = $fullsize_data['error'];
278
279 wp_send_json_error( $data );
280 }
281
282 $data['success'] = true;
283 $data['original_size'] = $fullsize_data['original_size'];
284 $data['new_size'] = $fullsize_data['optimized_size'];
285 $data['percent'] = $fullsize_data['percent'];
286 $data['overall_saving'] = $stats_data['original_size'] - $stats_data['optimized_size'];
287 $data['original_overall_size'] = $stats_data['original_size'];
288 $data['new_overall_size'] = $stats_data['optimized_size'];
289 $data['thumbnails'] = $attachment->get_optimized_sizes_count();
290
291 wp_send_json_success( $data );
292 }
293
294 /**
295 * Create a new Imagify account.
296 *
297 * @since 1.0
298 */
299 add_action( 'wp_ajax_imagify_signup', '_do_wp_ajax_imagify_signup' );
300 function _do_wp_ajax_imagify_signup() {
301 check_ajax_referer( 'imagify-signup', 'imagifysignupnonce' );
302
303 if ( ! isset( $_GET['email'] ) ) {
304 wp_send_json_error();
305 }
306
307 $data = array(
308 'email' => $_GET['email'],
309 'password' => wp_generate_password( 12, false ),
310 'lang' => get_locale()
311 );
312
313 $response = add_imagify_user( $data );
314
315 if ( is_wp_error( $response ) ) {
316 wp_send_json_error( $response->get_error_message() );
317 }
318
319 wp_send_json_success();
320 }
321
322 /**
323 * Process an API key check validity.
324 *
325 * @since 1.0
326 */
327 add_action( 'wp_ajax_imagify_check_api_key_validity', '_do_wp_ajax_imagify_check_api_key_validity' );
328 function _do_wp_ajax_imagify_check_api_key_validity() {
329 check_ajax_referer( 'imagify-check-api-key', 'imagifycheckapikeynonce' );
330
331 if ( ! isset( $_GET['api_key'] ) ) {
332 wp_send_json_error();
333 }
334
335 $response = get_imagify_status( $_GET['api_key'] );
336
337 if ( is_wp_error( $response ) ) {
338 wp_send_json_error( $response->get_error_message() );
339 }
340
341 $options = get_site_option( IMAGIFY_SETTINGS_SLUG );
342 $options['api_key'] = sanitize_key( $_GET['api_key'] );
343
344 update_site_option( IMAGIFY_SETTINGS_SLUG, $options );
345
346 wp_send_json_success();
347 }
348
349 /**
350 * Process a dismissed notice.
351 *
352 * @since 1.0
353 */
354 add_action( 'wp_ajax_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
355 add_action( 'admin_post_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
356 function _do_admin_post_imagify_dismiss_notice() {
357 if ( defined( 'DOING_AJAX' ) ) {
358 check_ajax_referer( 'imagify-dismiss-notice' );
359 } else {
360 check_admin_referer( 'imagify-dismiss-notice' );
361 }
362
363 if ( ! isset( $_GET['notice'] ) || ! current_user_can( 'manage_options' ) ) {
364 if ( defined( 'DOING_AJAX' ) ) {
365 wp_send_json_error();
366 } else {
367 wp_nonce_ays( '' );
368 }
369 }
370
371 $notice = $_GET['notice'];
372
373 imagify_dismiss_notice( $notice );
374
375 /**
376 * Fires when a notice is dismissed.
377 *
378 * @since 1.4.2
379 *
380 * @param int $notice The notice slug
381 */
382 do_action( 'imagify_dismiss_notice', $notice );
383
384 if ( ! defined( 'DOING_AJAX' ) ) {
385 wp_safe_redirect( wp_get_referer() );
386 die();
387 }
388
389 wp_send_json_success();
390 }
391
392 /**
393 * Disable a plugin which can be in conflict with Imagify
394 *
395 * @since 1.2
396 */
397 add_action( 'admin_post_imagify_deactivate_plugin', '_imagify_deactivate_plugin' );
398 function _imagify_deactivate_plugin() {
399 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'imagifydeactivatepluginnonce' ) ) {
400 wp_nonce_ays( '' );
401 }
402
403 deactivate_plugins( $_GET['plugin'] );
404
405 wp_safe_redirect( wp_get_referer() );
406 die();
407 }
408
409 /**
410 * Get admin bar profile output
411 *
412 * @since 1.2.3
413 */
414 add_action( 'wp_ajax_imagify_get_admin_bar_profile', '_do_wp_ajax_imagify_get_admin_bar_profile' );
415 function _do_wp_ajax_imagify_get_admin_bar_profile() {
416 check_ajax_referer( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' );
417
418 $user = new Imagify_User();
419 $unconsumed_quota = $user->get_percent_unconsumed_quota();
420 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'sun.svg" width="37" height="38" alt="" />';
421 $bar_class = 'positive';
422 $message = '';
423
424 if ( $unconsumed_quota >= 21 && $unconsumed_quota <= 50 ) {
425 $bar_class = 'neutral';
426 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'cloudy-sun.svg" width="37" height="38" alt="" />';
427 }
428 elseif ( $unconsumed_quota <= 20 ) {
429 $bar_class = 'negative';
430 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'stormy.svg" width="38" height="36" alt="" />';
431 }
432
433 if ( $unconsumed_quota <= 20 && $unconsumed_quota > 0 ) {
434 $message = '
435 <div class="imagify-error">
436 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s almost over!', 'imagify' ) . '</strong></p>
437 <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>
438 <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>
439 </div>
440 ';
441 }
442
443 if ( $unconsumed_quota === 0 ) {
444 $message = '
445 <div class="imagify-error">
446 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s Over!', 'imagify' ) . '</strong></p>
447 <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>
448 <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>
449 </div>
450 ';
451 }
452
453 // custom HTML
454 $quota_section = '
455 <div class="imagify-admin-bar-quota">
456 <div class="imagify-abq-row">';
457
458 if ( 1 === $user->plan_id ) {
459 $quota_section .= '
460 <div class="imagify-meteo-icon">
461 ' . $meteo_icon . '
462 </div>';
463 }
464
465 $quota_section .= '
466 <div class="imagify-account">
467 <p class="imagify-meteo-title">' . __( 'Account status', 'imagify' ) . '</p>
468 <p class="imagify-meteo-subs">' . __( 'Your subscription:', 'imagify' ) . '&nbsp;<strong class="imagify-user-plan">' . $user->plan_label . '</strong></p>
469 </div>
470 </div>';
471
472 if ( 1 === $user->plan_id ) {
473 $quota_section .= '
474 <div class="imagify-abq-row">
475 <div class="imagify-space-left">
476 <p>' . sprintf( __( 'You have %s space credit left', 'imagify'), '<span class="imagify-unconsumed-percent">' . $unconsumed_quota . '%</span>' ) . '</p>
477 <div class="imagify-bar-' . $bar_class . '">
478 <div style="width: ' . $unconsumed_quota . '%;" class="imagify-unconsumed-bar imagify-progress"></div>
479 </div>
480 </div>
481 </div>';
482 }
483
484 $quota_section .= '
485 <p class="imagify-abq-row">
486 <a class="imagify-account-link" href="' . IMAGIFY_APP_MAIN . '/#/subscription" target="_blank">
487 <span class="dashicons dashicons-admin-users"></span>
488 <span class="button-text">' . __( 'View my subscription', 'imagify' ) . '</span>
489 </a>
490 </p>
491 </div>
492 ' . $message;
493
494 wp_send_json_success( $quota_section );
495 }
496
497 /**
498 * Optimize image on picture editing with async request
499 *
500 * @since 1.4
501 **/
502 add_action( 'wp_ajax_imagify_async_optimize_save_image_editor_file', '_do_admin_post_async_optimize_save_image_editor_file' );
503 function _do_admin_post_async_optimize_save_image_editor_file() {
504 if ( isset( $_POST['do'], $_POST['postid'] )
505 && check_ajax_referer( 'image_editor-' . $_POST['postid'] )
506 && get_post_meta( $_POST['postid'], '_imagify_data', true )
507 ) {
508
509 $attachment_id = $_POST['postid'];
510 $optimization_level = get_post_meta( $attachment_id, '_imagify_optimization_level', true );
511 $attachment = new Imagify_Attachment( $attachment_id );
512 $metadata = wp_get_attachment_metadata( $attachment_id );
513
514 // Remove old optimization data
515 delete_post_meta( $attachment_id, '_imagify_data' );
516 delete_post_meta( $attachment_id, '_imagify_status' );
517 delete_post_meta( $attachment_id, '_imagify_optimization_level' );
518
519 if ( 'restore' === $_POST['do'] ) {
520 // Restore the backup file
521 $attachment->restore();
522
523 // Get old metadata to regenerate all thumbnails
524 $metadata = array( 'sizes' => array() );
525 $backup_sizes = (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
526
527 foreach ( $backup_sizes as $size_key => $size_data ) {
528 $size_key = str_replace( '-origin', '' , $size_key );
529 $metadata['sizes'][ $size_key ] = $size_data;
530 }
531 }
532
533 // Optimize it!!!!!
534 $attachment->optimize( $optimization_level, $metadata );
535
536 die( 1 );
537 }
538
539 }