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

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