PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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 / deprecated / classes / class-imagify-admin-ajax-post-deprecated.php

class-imagify-admin-ajax-post-deprecated.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at inc/deprecated/classes/class-imagify-admin-ajax-post-deprecated.php

510 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3
4 /**
5 * Class for deprecated methods from Imagify_Admin_Ajax_Post.
6 *
7 * @since 1.8.4
8 * @author Grégory Viguier
9 * @deprecated
10 */
11 class Imagify_Admin_Ajax_Post_Deprecated {
12
13 /**
14 * Optimize image on picture uploading with async request.
15 *
16 * @since 1.6.11
17 * @since 1.8.4 Deprecated
18 * @access public
19 * @author Julio Potier
20 * @see _imagify_optimize_attachment()
21 * @deprecated
22 */
23 public function imagify_async_optimize_upload_new_media_callback() {
24 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.8.4', 'Imagify_Admin_Ajax_Post::get_instance()->imagify_auto_optimize_callback()' );
25
26 if ( empty( $_POST['_ajax_nonce'] ) || empty( $_POST['attachment_id'] ) || empty( $_POST['metadata'] ) || empty( $_POST['context'] ) ) { // WPCS: CSRF ok.
27 return;
28 }
29
30 $context = imagify_sanitize_context( $_POST['context'] );
31 $attachment_id = absint( $_POST['attachment_id'] );
32
33 imagify_check_nonce( 'new_media-' . $attachment_id );
34 imagify_check_user_capacity( 'auto-optimize' );
35
36 $attachment = get_imagify_attachment( $context, $attachment_id, 'imagify_async_optimize_upload_new_media' );
37
38 // Optimize it!!!!!
39 $attachment->optimize( null, $_POST['metadata'] );
40 die( 1 );
41 }
42
43 /**
44 * Optimize image on picture editing (resize, crop...) with async request.
45 *
46 * @since 1.6.11
47 * @since 1.8.4 Deprecated
48 * @access public
49 * @author Julio Potier
50 * @deprecated
51 */
52 public function imagify_async_optimize_save_image_editor_file_callback() {
53 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.8.4', 'Imagify_Admin_Ajax_Post::get_instance()->imagify_auto_optimize_callback()' );
54
55 $attachment_id = ! empty( $_POST['postid'] ) ? absint( $_POST['postid'] ) : 0;
56
57 if ( ! $attachment_id || empty( $_POST['do'] ) ) {
58 return;
59 }
60
61 imagify_check_nonce( 'image_editor-' . $attachment_id );
62 imagify_check_user_capacity( 'edit_post', $attachment_id );
63
64 $attachment = get_imagify_attachment( 'wp', $attachment_id, 'wp_ajax_imagify_async_optimize_save_image_editor_file' );
65
66 if ( ! $attachment->get_data() ) {
67 return;
68 }
69
70 $optimization_level = $attachment->get_optimization_level();
71 $metadata = wp_get_attachment_metadata( $attachment_id );
72
73 // Remove old optimization data.
74 $attachment->delete_imagify_data();
75
76 if ( 'restore' === $_POST['do'] ) {
77 // Restore the backup file.
78 $attachment->restore();
79
80 // Get old metadata to regenerate all thumbnails.
81 $metadata = array( 'sizes' => array() );
82 $backup_sizes = (array) get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
83
84 foreach ( $backup_sizes as $size_key => $size_data ) {
85 $size_key = str_replace( '-origin', '' , $size_key );
86 $metadata['sizes'][ $size_key ] = $size_data;
87 }
88 }
89
90 // Optimize it!!!!!
91 $attachment->optimize( $optimization_level, $metadata );
92 die( 1 );
93 }
94
95
96 /** ----------------------------------------------------------------------------------------- */
97 /** CUSTOM FOLDERS CALLBACKS ================================================================ */
98 /** ----------------------------------------------------------------------------------------- */
99
100 /**
101 * Optimize a file.
102 *
103 * @since 1.7
104 * @since 1.9 Deprecated
105 * @access public
106 * @author Grégory Viguier
107 * @deprecated
108 */
109 public function imagify_bulk_optimize_file_callback() {
110 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9', '$this->imagify_bulk_optimize_callback()' );
111
112 imagify_check_nonce( 'imagify-bulk-upload' );
113 imagify_check_user_capacity( 'optimize-file' );
114
115 $file_id = filter_input( INPUT_POST, 'image', FILTER_VALIDATE_INT );
116 $context = imagify_sanitize_context( filter_input( INPUT_POST, 'context', FILTER_SANITIZE_STRING ) );
117 $context = ! $context || 'wp' === strtolower( $context ) ? 'File' : $context;
118
119 if ( ! $file_id ) {
120 imagify_die( __( 'Invalid request', 'imagify' ) );
121 }
122
123 $file = get_imagify_attachment( $context, $file_id, 'imagify_bulk_optimize_file' );
124
125 if ( ! $file->is_valid() ) {
126 imagify_die( __( 'Invalid file ID', 'imagify' ) );
127 }
128
129 // Restore before re-optimizing.
130 if ( false !== $file->get_optimization_level() ) {
131 $file->restore();
132 }
133
134 // Optimize it.
135 $result = $file->optimize( $this->get_optimization_level() );
136
137 // Return the optimization statistics.
138 if ( ! $file->is_optimized() ) {
139 $data = array(
140 'success' => false,
141 'error_code' => '',
142 'error' => (string) $file->get_optimized_error(),
143 );
144
145 if ( ! $file->has_error() ) {
146 $data['error_code'] = 'already-optimized';
147 } else {
148 $message = 'You\'ve consumed all your data. You have to upgrade your account to continue';
149
150 if ( $data['error'] === $message ) {
151 $data['error_code'] = 'over-quota';
152 }
153 }
154
155 $data['error'] = imagify_translate_api_message( $data['error'] );
156
157 imagify_die( $data );
158 }
159
160 $data = $file->get_size_data();
161
162 wp_send_json_success( array(
163 'success' => true,
164 'original_size_human' => imagify_size_format( $data['original_size'], 2 ),
165 'new_size_human' => imagify_size_format( $data['optimized_size'], 2 ),
166 'overall_saving' => $data['original_size'] - $data['optimized_size'],
167 'overall_saving_human' => imagify_size_format( $data['original_size'] - $data['optimized_size'], 2 ),
168 'original_overall_size' => $data['original_size'],
169 'original_overall_size_human' => imagify_size_format( $data['original_size'], 2 ),
170 'new_overall_size' => $data['optimized_size'],
171 'percent_human' => $data['percent'] . '%',
172 'thumbnails' => $file->get_optimized_sizes_count(),
173 ) );
174 }
175
176
177 /** ----------------------------------------------------------------------------------------- */
178 /** AUTOMATIC OPTIMIZATION ================================================================== */
179 /** ----------------------------------------------------------------------------------------- */
180
181 /**
182 * Auto-optimize files.
183 *
184 * @since 1.8.4
185 * @since 1.9 Deprecated
186 * @access public
187 * @author Grégory Viguier
188 * @see Imagify_Auto_Optimization->do_auto_optimization()
189 * @deprecated
190 */
191 public function imagify_auto_optimize_callback() {
192 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9' );
193
194 if ( empty( $_POST['_ajax_nonce'] ) || empty( $_POST['attachment_id'] ) || empty( $_POST['context'] ) ) { // WPCS: CSRF ok.
195 imagify_die( __( 'Invalid request', 'imagify' ) );
196 }
197
198 $media_id = $this->get_media_id( 'POST' );
199
200 imagify_check_nonce( 'imagify_auto_optimize-' . $media_id );
201
202 if ( ! get_transient( 'imagify-auto-optimize-' . $media_id ) ) {
203 imagify_die();
204 }
205
206 delete_transient( 'imagify-auto-optimize-' . $media_id );
207
208 $context = $this->get_context( 'POST' );
209 $process = imagify_get_optimization_process( $media_id, $context );
210
211 if ( ! $process->is_valid() ) {
212 imagify_die( __( 'This media is not valid.', 'imagify' ) );
213 }
214
215 if ( ! $process->get_media()->is_supported() ) {
216 imagify_die( __( 'This type of file is not supported.', 'imagify' ) );
217 }
218
219 $this->check_can_optimize();
220
221 /**
222 * Let's start.
223 */
224 $is_new_upload = ! empty( $_POST['is_new_upload'] );
225
226 /**
227 * Triggered before a media is auto-optimized.
228 *
229 * @since 1.8.4
230 * @author Grégory Viguier
231 *
232 * @param int $media_id The media ID.
233 * @param bool $is_new_upload True if it's a new upload. False otherwize.
234 */
235 do_action( 'imagify_before_auto_optimization', $media_id, $is_new_upload );
236
237 if ( $is_new_upload ) {
238 /**
239 * It's a new upload.
240 */
241 // Optimize.
242 $process->optimize();
243 } else {
244 /**
245 * The media has already been optimized (or at least it has been tried).
246 */
247 $data = $process->get_data();
248
249 // Get the optimization level before deleting the optimization data.
250 $optimization_level = $data->get_optimization_level();
251
252 // Remove old optimization data.
253 $data->delete_imagify_data();
254
255 // Some specifics for the image editor.
256 if ( ! empty( $_POST['data']['do'] ) && 'restore' === $_POST['data']['do'] ) {
257 // Restore the backup file.
258 $process->restore();
259 }
260
261 // Optimize.
262 $process->optimize( $optimization_level );
263 }
264
265 /**
266 * Triggered after a media is auto-optimized.
267 *
268 * @since 1.8.4
269 * @author Grégory Viguier
270 *
271 * @param int $media_id The media ID.
272 * @param bool $is_new_upload True if it's a new upload. False otherwize.
273 */
274 do_action( 'imagify_after_auto_optimization', $media_id, $is_new_upload );
275 die( 1 );
276 }
277
278
279 /** ----------------------------------------------------------------------------------------- */
280 /** VARIOUS FOR OPTIMIZATION ================================================================ */
281 /** ----------------------------------------------------------------------------------------- */
282
283 /**
284 * Get all unoptimized attachment ids.
285 *
286 * @since 1.6.11
287 * @since 1.9 Deprecated
288 * @access public
289 * @author Jonathan Buttigieg
290 * @deprecated
291 */
292 public function imagify_get_unoptimized_attachment_ids_callback() {
293 global $wpdb;
294
295 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9', '$this->imagify_get_media_ids_callback()' );
296
297 imagify_check_nonce( 'imagify-bulk-upload' );
298 imagify_check_user_capacity( 'bulk-optimize' );
299 $this->check_can_optimize();
300
301 @set_time_limit( 0 );
302
303 // Get (ordered) IDs.
304 $optimization_level = $this->get_optimization_level();
305
306 $mime_types = Imagify_DB::get_mime_types();
307 $statuses = Imagify_DB::get_post_statuses();
308 $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
309 $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause( array(
310 'prepared' => true,
311 ) );
312 $ids = $wpdb->get_col( $wpdb->prepare( // WPCS: unprepared SQL ok.
313 "
314 SELECT p.ID
315 FROM $wpdb->posts AS p
316 $nodata_join
317 LEFT JOIN $wpdb->postmeta AS mt1
318 ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
319 LEFT JOIN $wpdb->postmeta AS mt2
320 ON ( p.ID = mt2.post_id AND mt2.meta_key = '_imagify_optimization_level' )
321 WHERE
322 p.post_mime_type IN ( $mime_types )
323 AND (
324 mt1.meta_value = 'error'
325 OR
326 mt2.meta_value != %d
327 OR
328 mt2.post_id IS NULL
329 )
330 AND p.post_type = 'attachment'
331 AND p.post_status IN ( $statuses )
332 $nodata_where
333 GROUP BY p.ID
334 ORDER BY
335 CASE mt1.meta_value
336 WHEN 'already_optimized' THEN 2
337 ELSE 1
338 END ASC,
339 p.ID DESC
340 LIMIT 0, %d",
341 $optimization_level,
342 imagify_get_unoptimized_attachment_limit()
343 ) );
344
345 $wpdb->flush();
346 unset( $mime_types );
347 $ids = array_filter( array_map( 'absint', $ids ) );
348
349 if ( ! $ids ) {
350 wp_send_json_success( array() );
351 }
352
353 $results = Imagify_DB::get_metas( array(
354 // Get attachments filename.
355 'filenames' => '_wp_attached_file',
356 // Get attachments data.
357 'data' => '_imagify_data',
358 // Get attachments optimization level.
359 'optimization_levels' => '_imagify_optimization_level',
360 // Get attachments status.
361 'statuses' => '_imagify_status',
362 ), $ids );
363
364 // First run.
365 foreach ( $ids as $i => $id ) {
366 $attachment_status = isset( $results['statuses'][ $id ] ) ? $results['statuses'][ $id ] : false;
367 $attachment_optimization_level = isset( $results['optimization_levels'][ $id ] ) ? $results['optimization_levels'][ $id ] : false;
368 $attachment_error = '';
369
370 if ( isset( $results['data'][ $id ]['sizes']['full']['error'] ) ) {
371 $attachment_error = $results['data'][ $id ]['sizes']['full']['error'];
372 }
373
374 // Don't try to re-optimize if the optimization level is still the same.
375 if ( $optimization_level === $attachment_optimization_level && is_string( $attachment_error ) ) {
376 unset( $ids[ $i ] );
377 continue;
378 }
379
380 // Don't try to re-optimize images already compressed.
381 if ( 'already_optimized' === $attachment_status && $attachment_optimization_level >= $optimization_level ) {
382 unset( $ids[ $i ] );
383 continue;
384 }
385
386 $attachment_error = trim( $attachment_error );
387
388 // Don't try to re-optimize images with an empty error message.
389 if ( 'error' === $attachment_status && empty( $attachment_error ) ) {
390 unset( $ids[ $i ] );
391 }
392 }
393
394 if ( ! $ids ) {
395 wp_send_json_success( array() );
396 }
397
398 $ids = array_values( $ids );
399
400 /**
401 * Triggered before testing for file existence.
402 *
403 * @since 1.6.7
404 * @author Grégory Viguier
405 *
406 * @param array $ids An array of attachment IDs.
407 * @param array $results An array of the data fetched from the database.
408 * @param int $optimization_level The optimization level that will be used for the optimization.
409 */
410 do_action( 'imagify_bulk_optimize_before_file_existence_tests', $ids, $results, $optimization_level );
411
412 $data = array();
413
414 foreach ( $ids as $i => $id ) {
415 if ( empty( $results['filenames'][ $id ] ) ) {
416 // Problem.
417 continue;
418 }
419
420 $file_path = get_imagify_attached_file( $results['filenames'][ $id ] );
421
422 /** This filter is documented in inc/deprecated/deprecated.php. */
423 $file_path = apply_filters( 'imagify_file_path', $file_path );
424
425 if ( ! $file_path || ! $this->filesystem->exists( $file_path ) ) {
426 continue;
427 }
428
429 $attachment_backup_path = get_imagify_attachment_backup_path( $file_path );
430 $attachment_status = isset( $results['statuses'][ $id ] ) ? $results['statuses'][ $id ] : false;
431 $attachment_optimization_level = isset( $results['optimization_levels'][ $id ] ) ? $results['optimization_levels'][ $id ] : false;
432
433 // Don't try to re-optimize if there is no backup file.
434 if ( 'success' === $attachment_status && $optimization_level !== $attachment_optimization_level && ! $this->filesystem->exists( $attachment_backup_path ) ) {
435 continue;
436 }
437
438 $data[ '_' . $id ] = get_imagify_attachment_url( $results['filenames'][ $id ] );
439 } // End foreach().
440
441 if ( ! $data ) {
442 wp_send_json_success( array() );
443 }
444
445 wp_send_json_success( $data );
446 }
447
448 /**
449 * Get all unoptimized file ids.
450 *
451 * @since 1.7
452 * @since 1.9 Deprecated
453 * @access public
454 * @author Grégory Viguier
455 * @deprecated
456 */
457 public function imagify_get_unoptimized_file_ids_callback() {
458 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9', '$this->imagify_get_media_ids_callback()' );
459
460 imagify_check_nonce( 'imagify-bulk-upload' );
461 imagify_check_user_capacity( 'optimize-file' );
462
463 $this->check_can_optimize();
464
465 @set_time_limit( 0 );
466
467 $optimization_level = $this->get_optimization_level();
468
469 /**
470 * Get the folders from DB.
471 */
472 $folders = Imagify_Custom_Folders::get_folders( array(
473 'active' => true,
474 ) );
475
476 if ( ! $folders ) {
477 wp_send_json_success( array() );
478 }
479
480 /**
481 * Triggered before getting file IDs.
482 *
483 * @since 1.7
484 * @author Grégory Viguier
485 *
486 * @param array $folders An array of folders data.
487 * @param int $optimization_level The optimization level that will be used for the optimization.
488 */
489 do_action( 'imagify_bulk_optimize_files_before_get_files', $folders, $optimization_level );
490
491 /**
492 * Get the files from DB, and from the folders.
493 */
494 $files = Imagify_Custom_Folders::get_files_from_folders( $folders, array(
495 'optimization_level' => $optimization_level,
496 ) );
497
498 if ( ! $files ) {
499 wp_send_json_success( array() );
500 }
501
502 // We need to output file URLs.
503 foreach ( $files as $k => $file ) {
504 $files[ $k ] = Imagify_Files_Scan::remove_placeholder( $file['path'], 'url' );
505 }
506
507 wp_send_json_success( $files );
508 }
509 }
510