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

471 lines 15.0 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 = new Imagify_Attachment( $_GET['attachment_id'] );
27
28 // Optimize it!!!!!
29 $attachment->optimize();
30
31 if ( ! defined( 'DOING_AJAX' ) ) {
32 wp_safe_redirect( wp_get_referer() );
33 die();
34 }
35
36 // Return the optimization statistics
37 $output = get_imagify_attachment_optimization_text( $attachment->id );
38 wp_send_json_success( $output );
39 }
40
41 /**
42 * Process a manual upload by overriding the optimization level.
43 *
44 * @since 1.0
45 */
46 add_action( 'wp_ajax_imagify_manual_override_upload', '_do_admin_post_imagify_manual_override_upload' );
47 add_action( 'admin_post_imagify_manual_override_upload', '_do_admin_post_imagify_manual_override_upload' );
48 function _do_admin_post_imagify_manual_override_upload() {
49 if ( defined( 'DOING_AJAX' ) ) {
50 check_ajax_referer( 'imagify-manual-override-upload' );
51 } else {
52 check_admin_referer( 'imagify-manual-override-upload' );
53 }
54
55 if ( ! isset( $_GET['attachment_id'] ) || ! current_user_can( 'upload_files' ) ) {
56 if ( defined( 'DOING_AJAX' ) ) {
57 wp_send_json_error();
58 } else {
59 wp_nonce_ays( '' );
60 }
61 }
62
63 $attachment = new Imagify_Attachment( $_GET['attachment_id'] );
64
65 // Restore the backup file
66 $attachment->restore();
67
68 // Optimize it!!!!!
69 $attachment->optimize( (int) $_GET['optimization_level'] );
70
71 if ( ! defined( 'DOING_AJAX' ) ) {
72 wp_safe_redirect( wp_get_referer() );
73 die();
74 }
75
76 // Return the optimization statistics
77 $output = get_imagify_attachment_optimization_text( $attachment->id );
78 wp_send_json_success( $output );
79 }
80
81 /**
82 * Process a restoration to the original attachment.
83 *
84 * @since 1.0
85 */
86 add_action( 'wp_ajax_imagify_restore_upload', '_do_admin_post_imagify_restore_upload' );
87 add_action( 'admin_post_imagify_restore_upload', '_do_admin_post_imagify_restore_upload' );
88 function _do_admin_post_imagify_restore_upload() {
89 if ( defined( 'DOING_AJAX' ) ) {
90 check_ajax_referer( 'imagify-restore-upload' );
91 } else {
92 check_admin_referer( 'imagify-restore-upload' );
93 }
94
95 if ( ! isset( $_GET['attachment_id'] ) || ! current_user_can( 'upload_files' ) ) {
96 if ( defined( 'DOING_AJAX' ) ) {
97 wp_send_json_error();
98 } else {
99 wp_nonce_ays( '' );
100 }
101 }
102
103 $attachment = new Imagify_Attachment( $_GET['attachment_id'] );
104
105 // Restore the backup file
106 $attachment->restore();
107
108 if ( ! defined( 'DOING_AJAX' ) ) {
109 wp_safe_redirect( wp_get_referer() );
110 die();
111 }
112
113 // Return the optimization button
114 $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>';
115 wp_send_json_success( $output );
116 }
117
118 /**
119 * Get all unoptimized attachment ids.
120 *
121 * @since 1.0
122 */
123 add_action( 'wp_ajax_imagify_get_unoptimized_attachment_ids', '_do_wp_ajax_imagify_get_unoptimized_attachment_ids' );
124 function _do_wp_ajax_imagify_get_unoptimized_attachment_ids() {
125 check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
126
127 if ( ! current_user_can( 'upload_files' ) ) {
128 wp_send_json_error();
129 }
130
131 $user = new Imagify_User();
132
133 if ( $user->is_over_quota() ) {
134 wp_send_json_error( array( 'message' => 'over-quota' ) );
135 }
136
137 @set_time_limit( 0 );
138
139 $optimization_level = $_GET['optimization_level'];
140 $optimization_level = ( -1 != $optimization_level ) ? $optimization_level : get_imagify_option( 'optimization_level', 1 );
141 $optimization_level = (int) $optimization_level;
142
143 $meta_query = array(
144 'relation' => 'OR',
145 array(
146 'key' => '_imagify_optimization_level',
147 'value' => $optimization_level,
148 'compare' => '!='
149 ),
150 array(
151 'key' => '_imagify_optimization_level',
152 'compare' => 'NOT EXISTS'
153 ),
154 array(
155 'key' => '_imagify_status',
156 'value' => 'error',
157 'compare' => '='
158 ),
159 );
160
161 $args = array(
162 'fields' => 'ids',
163 'post_type' => 'attachment',
164 'post_status' => 'any',
165 'post_mime_type' => get_imagify_mime_type(),
166 'meta_query' => $meta_query,
167 'posts_per_page' => -1,
168 'no_found_rows' => true,
169 'update_post_term_cache' => false,
170 );
171
172 $data = array();
173 $query = new WP_Query( $args );
174 $ids = $query->posts;
175
176 // Save the optimization level in a transient to retrieve it later during the process
177 set_transient( 'imagify_bulk_optimization_level', $optimization_level );
178
179 foreach( $ids as $id ) {
180 /** This filter is documented in inc/functions/process.php */
181 $file_path = apply_filters( 'imagify_file_path', get_attached_file( $id ) );
182
183 if ( file_exists( $file_path ) ) {
184 $attachment = new Imagify_Attachment( $id );
185 $attachment_error = $attachment->get_optimized_error();
186 $attachment_error = trim( $attachment_error );
187 $attachment_status = $attachment->get_status();
188
189 // Don't try to re-optimize if the optimization level is still the same
190 if ( $optimization_level === $attachment->get_optimization_level() && ! $attachment->has_error() ) {
191 continue;
192 }
193
194 // Don't try to re-optimize if there is no backup file
195 if ( $optimization_level !== $attachment->get_optimization_level() && ! $attachment->has_backup() && $attachment->is_optimized() ) {
196 continue;
197 }
198
199 // Don't try to re-optimize images already compressed
200 if ( $attachment->get_optimization_level() >= $optimization_level && $attachment_status == 'already_optimized' ) {
201 continue;
202 }
203
204 // Don't try to re-optimize images with an empty error message
205 if ( $attachment_status == 'error' && empty( $attachment_error ) ) {
206 continue;
207 }
208
209 $data[ '_' . $id ] = wp_get_attachment_url( $id );
210 }
211 }
212
213 if ( (bool) $data ) {
214 wp_send_json_success( $data );
215 }
216
217 wp_send_json_error( array( 'message' => 'no-images' ) );
218 }
219
220 /**
221 * Process all thumbnails of a specific image with Imagify with the bulk method.
222 *
223 * @since 1.0
224 */
225 add_action( 'wp_ajax_imagify_bulk_upload', '_do_wp_ajax_imagify_bulk_upload' );
226 function _do_wp_ajax_imagify_bulk_upload() {
227 check_ajax_referer( 'imagify-bulk-upload', 'imagifybulkuploadnonce' );
228
229 if ( ! isset( $_POST['image'] ) || ! current_user_can( 'upload_files' ) ) {
230 wp_send_json_error();
231 }
232
233 $attachment_id = (int) $_POST['image'];
234 $attachment = new Imagify_Attachment( $_POST['image'] );
235 $optimization_level = get_transient( 'imagify_bulk_optimization_level' );
236
237 // Restore it if the optimization level is updated
238 if ( $optimization_level !== $attachment->get_optimization_level() ) {
239 $attachment->restore();
240 }
241
242 // Optimize it!!!!!
243 $attachment->optimize( $optimization_level );
244
245 // Return the optimization statistics
246 $fullsize_data = $attachment->get_size_data();
247 $stats_data = $attachment->get_stats_data();
248 $saving_data = imagify_count_saving_data();
249 $user = new Imagify_User();
250 $data = array(
251 'global_already_optimized_attachments' => $saving_data['count'],
252 'global_optimized_attachments' => imagify_count_optimized_attachments(),
253 'global_unoptimized_attachments' => imagify_count_unoptimized_attachments(),
254 'global_errors_attachments' => imagify_count_error_attachments(),
255 'global_optimized_attachments_percent' => imagify_percent_optimized_attachments(),
256 'global_optimized_percent' => $saving_data['percent'],
257 'global_original_human' => size_format( $saving_data['original_size'], 1 ),
258 'global_optimized_human' => size_format( $saving_data['optimized_size'], 1 ),
259 'global_unconsumed_quota' => $user->get_percent_unconsumed_quota(),
260 );
261
262 if ( ! $attachment->is_optimized() ) {
263 $data['success'] = false;
264 $data['error'] = $fullsize_data['error'];
265
266 wp_send_json_error( $data );
267 }
268
269 $data['success'] = true;
270 $data['original_size'] = $fullsize_data['original_size'];
271 $data['new_size'] = $fullsize_data['optimized_size'];
272 $data['percent'] = $fullsize_data['percent'];
273 $data['overall_saving'] = $stats_data['original_size'] - $stats_data['optimized_size'];
274 $data['original_overall_size'] = $stats_data['original_size'];
275 $data['new_overall_size'] = $stats_data['optimized_size'];
276 $data['thumbnails'] = $attachment->get_optimized_sizes_count();
277
278 wp_send_json_success( $data );
279 }
280
281 /**
282 * Create a new Imagify account.
283 *
284 * @since 1.0
285 */
286 add_action( 'wp_ajax_imagify_signup', '_do_wp_ajax_imagify_signup' );
287 function _do_wp_ajax_imagify_signup() {
288 check_ajax_referer( 'imagify-signup', 'imagifysignupnonce' );
289
290 if ( ! isset( $_GET['email'] ) ) {
291 wp_send_json_error();
292 }
293
294 $data = array(
295 'email' => $_GET['email'],
296 'password' => wp_generate_password( 12, false ),
297 'lang' => get_locale()
298 );
299
300 $response = add_imagify_user( $data );
301
302 if ( is_wp_error( $response ) ) {
303 wp_send_json_error( $response->get_error_message() );
304 }
305
306 wp_send_json_success();
307 }
308
309 /**
310 * Process an API key check validity.
311 *
312 * @since 1.0
313 */
314 add_action( 'wp_ajax_imagify_check_api_key_validity', '_do_wp_ajax_imagify_check_api_key_validity' );
315 function _do_wp_ajax_imagify_check_api_key_validity() {
316 check_ajax_referer( 'imagify-check-api-key', 'imagifycheckapikeynonce' );
317
318 if ( ! isset( $_GET['api_key'] ) ) {
319 wp_send_json_error();
320 }
321
322 $response = get_imagify_status( $_GET['api_key'] );
323
324 if ( is_wp_error( $response ) ) {
325 wp_send_json_error( $response->get_error_message() );
326 }
327
328 $options = get_site_option( IMAGIFY_SETTINGS_SLUG );
329 $options['api_key'] = sanitize_key( $_GET['api_key'] );
330
331 update_site_option( IMAGIFY_SETTINGS_SLUG, $options );
332
333 wp_send_json_success();
334 }
335
336 /**
337 * Process a dismissed notice.
338 *
339 * @since 1.0
340 */
341 add_action( 'wp_ajax_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
342 add_action( 'admin_post_imagify_dismiss_notice', '_do_admin_post_imagify_dismiss_notice' );
343 function _do_admin_post_imagify_dismiss_notice() {
344 if ( defined( 'DOING_AJAX' ) ) {
345 check_ajax_referer( 'imagify-dismiss-notice' );
346 } else {
347 check_admin_referer( 'imagify-dismiss-notice' );
348 }
349
350 if ( ! isset( $_GET['notice'] ) || ! current_user_can( 'manage_options' ) ) {
351 if ( defined( 'DOING_AJAX' ) ) {
352 wp_send_json_error();
353 } else {
354 wp_nonce_ays( '' );
355 }
356 }
357
358 imagify_dismiss_notice( $_GET['notice'] );
359
360 if ( ! defined( 'DOING_AJAX' ) ) {
361 wp_safe_redirect( wp_get_referer() );
362 die();
363 }
364
365 wp_send_json_success();
366 }
367
368 /**
369 * Disable a plugin which can be in conflict with Imagify
370 *
371 * @since 1.2
372 */
373 add_action( 'admin_post_imagify_deactivate_plugin', '_imagify_deactivate_plugin' );
374 function _imagify_deactivate_plugin() {
375 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'imagifydeactivatepluginnonce' ) ) {
376 wp_nonce_ays( '' );
377 }
378
379 deactivate_plugins( $_GET['plugin'] );
380
381 wp_safe_redirect( wp_get_referer() );
382 die();
383 }
384
385 /**
386 * Get admin bar profile output
387 *
388 * @since 1.2.3
389 */
390 add_action( 'wp_ajax_imagify_get_admin_bar_profile', '_do_wp_ajax_imagify_get_admin_bar_profile' );
391 function _do_wp_ajax_imagify_get_admin_bar_profile() {
392 check_ajax_referer( 'imagify-get-admin-bar-profile', 'imagifygetadminbarprofilenonce' );
393
394 $user = new Imagify_User();
395 $unconsumed_quota = $user->get_percent_unconsumed_quota();
396 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'sun.svg" width="37" height="38" alt="" />';
397 $bar_class = 'positive';
398 $message = '';
399
400 if ( $unconsumed_quota >= 21 && $unconsumed_quota <= 50 ) {
401 $bar_class = 'neutral';
402 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'cloudy-sun.svg" width="37" height="38" alt="" />';
403 }
404 elseif ( $unconsumed_quota <= 20 ) {
405 $bar_class = 'negative';
406 $meteo_icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'stormy.svg" width="38" height="36" alt="" />';
407 }
408
409 if ( $unconsumed_quota <= 20 && $unconsumed_quota > 0 ) {
410 $message = '
411 <div class="imagify-error">
412 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s almost over!', 'imagify' ) . '</strong></p>
413 <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>
414 <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>
415 </div>
416 ';
417 }
418
419 if ( $unconsumed_quota === 0 ) {
420 $message = '
421 <div class="imagify-error">
422 <p><i class="dashicons dashicons-warning" aria-hidden="true"></i><strong>' . __( 'Oops, It\'s Over!', 'imagify' ) . '</strong></p>
423 <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>
424 <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>
425 </div>
426 ';
427 }
428
429 // custom HTML
430 $quota_section = '
431 <div class="imagify-admin-bar-quota">
432 <div class="imagify-abq-row">';
433
434 if ( 1 === $user->plan_id ) {
435 $quota_section .= '
436 <div class="imagify-meteo-icon">
437 ' . $meteo_icon . '
438 </div>';
439 }
440
441 $quota_section .= '
442 <div class="imagify-account">
443 <p class="imagify-meteo-title">' . __( 'Account status', 'imagify' ) . '</p>
444 <p class="imagify-meteo-subs">' . __( 'Your subscription:', 'imagify' ) . '&nbsp;<strong class="imagify-user-plan">' . $user->plan_label . '</strong></p>
445 </div>
446 </div>';
447
448 if ( 1 === $user->plan_id ) {
449 $quota_section .= '
450 <div class="imagify-abq-row">
451 <div class="imagify-space-left">
452 <p>' . sprintf( __( 'You have %s space credit left', 'imagify'), '<span class="imagify-unconsumed-percent">' . $unconsumed_quota . '%</span>' ) . '</p>
453 <div class="imagify-bar-' . $bar_class . '">
454 <div style="width: ' . $unconsumed_quota . '%;" class="imagify-unconsumed-bar imagify-progress"></div>
455 </div>
456 </div>
457 </div>';
458 }
459
460 $quota_section .= '
461 <p class="imagify-abq-row">
462 <a class="imagify-account-link" href="' . IMAGIFY_APP_MAIN . '/#/subscription" target="_blank">
463 <span class="dashicons dashicons-admin-users"></span>
464 <span class="button-text">' . __( 'View my subscription', 'imagify' ) . '</span>
465 </a>
466 </p>
467 </div>
468 ' . $message;
469
470 wp_send_json_success( $quota_section );
471 }