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

519 lines 16.6 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 );
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 imagify_dismiss_notice( $_GET['notice'] );
363
364 if ( ! defined( 'DOING_AJAX' ) ) {
365 wp_safe_redirect( wp_get_referer() );
366 die();
367 }
368
369 wp_send_json_success();
370 }
371
372 /**
373 * Disable a plugin which can be in conflict with Imagify
374 *
375 * @since 1.2
376 */
377 add_action( 'admin_post_imagify_deactivate_plugin', '_imagify_deactivate_plugin' );
378 function _imagify_deactivate_plugin() {
379 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'imagifydeactivatepluginnonce' ) ) {
380 wp_nonce_ays( '' );
381 }
382
383 deactivate_plugins( $_GET['plugin'] );
384
385 wp_safe_redirect( wp_get_referer() );
386 die();
387 }
388
389 /**
390 * Get admin bar profile output
391 *
392 * @since 1.2.3
393 */
394 add_action( 'wp_ajax_imagify_get_admin_bar_profile', '_do_wp_ajax_imagify_get_admin_bar_profile' );
395 function _do_wp_ajax_imagify_get_admin_bar_profile() {
396 check_ajax_referer( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' );
397
398 $user = new Imagify_User();
399 $unconsumed_quota = $user->get_percent_unconsumed_quota();
400 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'sun.svg" width="37" height="38" alt="" />';
401 $bar_class = 'positive';
402 $message = '';
403
404 if ( $unconsumed_quota >= 21 && $unconsumed_quota <= 50 ) {
405 $bar_class = 'neutral';
406 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'cloudy-sun.svg" width="37" height="38" alt="" />';
407 }
408 elseif ( $unconsumed_quota <= 20 ) {
409 $bar_class = 'negative';
410 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'stormy.svg" width="38" height="36" alt="" />';
411 }
412
413 if ( $unconsumed_quota <= 20 && $unconsumed_quota > 0 ) {
414 $message = '
415 <div class="imagify-error">
416 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s almost over!', 'imagify' ) . '</strong></p>
417 <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>
418 <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>
419 </div>
420 ';
421 }
422
423 if ( $unconsumed_quota === 0 ) {
424 $message = '
425 <div class="imagify-error">
426 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s Over!', 'imagify' ) . '</strong></p>
427 <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>
428 <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>
429 </div>
430 ';
431 }
432
433 // custom HTML
434 $quota_section = '
435 <div class="imagify-admin-bar-quota">
436 <div class="imagify-abq-row">';
437
438 if ( 1 === $user->plan_id ) {
439 $quota_section .= '
440 <div class="imagify-meteo-icon">
441 ' . $meteo_icon . '
442 </div>';
443 }
444
445 $quota_section .= '
446 <div class="imagify-account">
447 <p class="imagify-meteo-title">' . __( 'Account status', 'imagify' ) . '</p>
448 <p class="imagify-meteo-subs">' . __( 'Your subscription:', 'imagify' ) . '&nbsp;<strong class="imagify-user-plan">' . $user->plan_label . '</strong></p>
449 </div>
450 </div>';
451
452 if ( 1 === $user->plan_id ) {
453 $quota_section .= '
454 <div class="imagify-abq-row">
455 <div class="imagify-space-left">
456 <p>' . sprintf( __( 'You have %s space credit left', 'imagify'), '<span class="imagify-unconsumed-percent">' . $unconsumed_quota . '%</span>' ) . '</p>
457 <div class="imagify-bar-' . $bar_class . '">
458 <div style="width: ' . $unconsumed_quota . '%;" class="imagify-unconsumed-bar imagify-progress"></div>
459 </div>
460 </div>
461 </div>';
462 }
463
464 $quota_section .= '
465 <p class="imagify-abq-row">
466 <a class="imagify-account-link" href="' . IMAGIFY_APP_MAIN . '/#/subscription" target="_blank">
467 <span class="dashicons dashicons-admin-users"></span>
468 <span class="button-text">' . __( 'View my subscription', 'imagify' ) . '</span>
469 </a>
470 </p>
471 </div>
472 ' . $message;
473
474 wp_send_json_success( $quota_section );
475 }
476
477 /**
478 * Optimize image on picture editing with async request
479 *
480 * @since 1.4
481 **/
482 add_action( 'wp_ajax_imagify_async_optimize_save_image_editor_file', '_do_admin_post_async_optimize_save_image_editor_file' );
483 function _do_admin_post_async_optimize_save_image_editor_file() {
484 if ( isset( $_POST['do'], $_POST['postid'] )
485 && check_ajax_referer( 'image_editor-' . $_POST['postid'] )
486 && get_post_meta( $_POST['postid'], '_imagify_data', true )
487 ) {
488
489 $attachment_id = $_POST['postid'];
490 $optimization_level = get_post_meta( $attachment_id, '_imagify_optimization_level', true );
491 $attachment = new Imagify_Attachment( $attachment_id );
492 $metadata = wp_get_attachment_metadata( $attachment_id );
493
494 // Remove old optimization data
495 delete_post_meta( $attachment_id, '_imagify_data' );
496 delete_post_meta( $attachment_id, '_imagify_status' );
497 delete_post_meta( $attachment_id, '_imagify_optimization_level' );
498
499 if ( 'restore' === $_POST['do'] ) {
500 // Restore the backup file
501 $attachment->restore();
502
503 // Get old metadata to regenerate all thumbnails
504 $metadata = array( 'sizes' => array() );
505 $backup_sizes = (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
506
507 foreach ( $backup_sizes as $size_key => $size_data ) {
508 $size_key = str_replace( '-origin', '' , $size_key );
509 $metadata['sizes'][ $size_key ] = $size_data;
510 }
511 }
512
513 // Optimize it!!!!!
514 $attachment->optimize( $optimization_level, $metadata );
515
516 die( 1 );
517 }
518
519 }