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

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