PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.8
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 / deprecated.php

deprecated.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.9.8, at inc/deprecated/deprecated.php

1,666 lines 51.8 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 * Get all mime types which could be optimized by Imagify.
6 *
7 * @since 1.3
8 * @since 1.7 Deprecated.
9 * @deprecated
10 *
11 * @return array $mime_type The mime type.
12 */
13 function get_imagify_mime_type() {
14 _deprecated_function( __FUNCTION__ . '()', '1.7', 'imagify_get_mime_types()' );
15
16 return imagify_get_mime_types();
17 }
18
19 /**
20 * Planning cron.
21 * If the task is not programmed, it is automatically triggered.
22 *
23 * @since 1.4.2
24 * @since 1.7 Deprecated.
25 * @deprecated
26 */
27 function _imagify_rating_scheduled() {
28 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Cron_Rating::get_instance()->schedule_event()' );
29
30 Imagify_Cron_Rating::get_instance()->schedule_event();
31 }
32
33 /**
34 * Save the user images count to display it later in a notice message to ask him to rate Imagify on WordPress.org.
35 *
36 * @since 1.4.2
37 * @since 1.7 Deprecated.
38 * @deprecated
39 */
40 function _do_imagify_rating_cron() {
41 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Cron_Rating::get_instance()->do_event()' );
42
43 Imagify_Cron_Rating::get_instance()->do_event();
44 }
45
46 /**
47 * Adds weekly interval for cron jobs.
48 *
49 * @since 1.6
50 * @since 1.7 Deprecated.
51 * @author Remy Perona
52 * @deprecated
53 *
54 * @param Array $schedules An array of intervals used by cron jobs.
55 * @return Array Updated array of intervals.
56 */
57 function imagify_purge_cron_schedule( $schedules ) {
58 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Cron_Library_Size::get_instance()->maybe_add_recurrence( $schedules )' );
59
60 return Imagify_Cron_Library_Size::get_instance()->do_event( $schedules );
61 }
62
63 /**
64 * Planning cron task to update weekly the size of the images and the size of images uploaded by month.
65 * If the task is not programmed, it is automatically triggered.
66 *
67 * @since 1.6
68 * @since 1.7 Deprecated.
69 * @author Remy Perona
70 * @deprecated
71 */
72 function _imagify_update_library_size_calculations_scheduled() {
73 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Cron_Library_Size::get_instance()->schedule_event()' );
74
75 Imagify_Cron_Library_Size::get_instance()->schedule_event();
76 }
77
78 /**
79 * Cron task to update weekly the size of the images and the size of images uploaded by month.
80 *
81 * @since 1.6
82 * @since 1.7 Deprecated.
83 * @author Remy Perona
84 * @deprecated
85 */
86 function _do_imagify_update_library_size_calculations() {
87 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Cron_Library_Size::get_instance()->do_event()' );
88
89 Imagify_Cron_Library_Size::get_instance()->do_event();
90 }
91
92 /**
93 * Set a file permissions using FS_CHMOD_FILE.
94 *
95 * @since 1.2
96 * @since 1.6.5 Use WP Filesystem.
97 * @since 1.7.1 Deprecated.
98 * @deprecated
99 *
100 * @param string $file_path Path to the file.
101 * @return bool True on success, false on failure.
102 */
103 function imagify_chmod_file( $file_path ) {
104 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'imagify_get_filesystem()->chmod_file( $file_path )' );
105
106 return imagify_get_filesystem()->chmod_file( $file_path );
107 }
108
109 /**
110 * Get a file mime type.
111 *
112 * @since 1.6.9
113 * @since 1.7 Doesn't use exif_imagetype() nor getimagesize() anymore.
114 * @since 1.7.1 Deprecated.
115 * @author Grégory Viguier
116 * @deprecated
117 *
118 * @param string $file_path A file path (prefered) or a filename.
119 * @return string|bool A mime type. False on failure: the test is limited to mime types supported by Imagify.
120 */
121 function imagify_get_mime_type_from_file( $file_path ) {
122 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'imagify_get_filesystem()->get_mime_type( $file_path )' );
123
124 return imagify_get_filesystem()->get_mime_type( $file_path );
125 }
126
127 /**
128 * Get a file modification date, formated as "mysql". Fallback to current date.
129 *
130 * @since 1.7
131 * @since 1.7.1 Deprecated.
132 * @author Grégory Viguier
133 * @deprecated
134 *
135 * @param string $file_path The file path.
136 * @return string The date.
137 */
138 function imagify_get_file_date( $file_path ) {
139 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'imagify_get_filesystem()->get_date( $file_path )' );
140
141 return imagify_get_filesystem()->get_date( $file_path );
142 }
143
144 /**
145 * Get a clean value of ABSPATH that can be used in string replacements.
146 *
147 * @since 1.6.8
148 * @since 1.7.1 Deprecated.
149 * @author Grégory Viguier
150 * @deprecated
151 *
152 * @return string The path to WordPress' root folder.
153 */
154 function imagify_get_abspath() {
155 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'imagify_get_filesystem()->get_abspath()' );
156
157 return imagify_get_filesystem()->get_abspath();
158 }
159
160 /**
161 * Make an absolute path relative to WordPress' root folder.
162 * Also works for files from registered symlinked plugins.
163 *
164 * @since 1.6.10
165 * @since 1.7 The parameter $base is added.
166 * @since 1.7.1 Deprecated.
167 * @author Grégory Viguier
168 * @deprecated
169 *
170 * @param string $file_path An absolute path.
171 * @param string $base A base path to use instead of ABSPATH.
172 * @return string|bool A relative path. Can return the absolute path or false in case of a failure.
173 */
174 function imagify_make_file_path_relative( $file_path, $base = '' ) {
175 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'imagify_get_filesystem()->make_path_relative( $file_path, $base )' );
176
177 return imagify_get_filesystem()->make_path_relative( $file_path, $base );
178 }
179
180 /**
181 * Tell if a file is symlinked.
182 *
183 * @since 1.7
184 * @since 1.7.1 Deprecated.
185 * @author Grégory Viguier
186 * @deprecated
187 *
188 * @param string $file_path An absolute path.
189 * @return bool
190 */
191 function imagify_file_is_symlinked( $file_path ) {
192 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'imagify_get_filesystem()->is_symlinked( $file_path )' );
193
194 return imagify_get_filesystem()->is_symlinked( $file_path );
195 }
196
197 /**
198 * Determine if the Imagify API key is valid.
199 *
200 * @since 1.0
201 * @since 1.7.1 Deprecated.
202 * @deprecated
203 *
204 * @return bool True if the API key is valid.
205 */
206 function imagify_valid_key() {
207 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'Imagify_Requirements::is_api_key_valid()' );
208
209 return Imagify_Requirements::is_api_key_valid();
210 }
211
212 /**
213 * Check if external requests are blocked for Imagify.
214 *
215 * @since 1.0
216 * @since 1.7.1 Deprecated.
217 * @deprecated
218 *
219 * @return bool True if Imagify API can't be called.
220 */
221 function is_imagify_blocked() {
222 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'Imagify_Requirements::is_imagify_blocked()' );
223
224 return Imagify_Requirements::is_imagify_blocked();
225 }
226
227 /**
228 * Determine if the Imagify API is available by checking the API version.
229 *
230 * @since 1.0
231 * @since 1.7.1 Deprecated.
232 * @deprecated
233 *
234 * @return bool True if the Imagify API is available.
235 */
236 function is_imagify_servers_up() {
237 _deprecated_function( __FUNCTION__ . '()', '1.7.1', 'Imagify_Requirements::is_api_up()' );
238
239 return Imagify_Requirements::is_api_up();
240 }
241
242 /**
243 * Auto-optimize when a new attachment is generated.
244 *
245 * @since 1.0
246 * @since 1.5 Async job.
247 * @since 1.8.4 Deprecated
248 * @see Imagify_Admin_Ajax_Post_Deprecated::imagify_async_optimize_upload_new_media_callback()
249 * @deprecated
250 *
251 * @param array $metadata An array of attachment meta data.
252 * @param int $attachment_id Current attachment ID.
253 * @return array
254 */
255 function _imagify_optimize_attachment( $metadata, $attachment_id ) {
256 _deprecated_function( __FUNCTION__ . '()', '1.8.4', 'Imagify_Auto_Optimization::get_instance()->store_upload_ids()' );
257
258 if ( ! Imagify_Requirements::is_api_key_valid() || ! get_imagify_option( 'auto_optimize' ) ) {
259 return $metadata;
260 }
261
262 /**
263 * Allow to prevent automatic optimization for a specific attachment.
264 *
265 * @since 1.6.12
266 * @author Grégory Viguier
267 *
268 * @param bool $optimize True to optimize, false otherwise.
269 * @param int $attachment_id Attachment ID.
270 * @param array $metadata An array of attachment meta data.
271 */
272 $optimize = apply_filters( 'imagify_auto_optimize_attachment', true, $attachment_id, $metadata );
273
274 if ( ! $optimize ) {
275 return $metadata;
276 }
277
278 $context = 'wp';
279 $action = 'imagify_async_optimize_upload_new_media';
280 $_ajax_nonce = wp_create_nonce( 'new_media-' . $attachment_id );
281
282 imagify_do_async_job( compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' ) );
283
284 return $metadata;
285 }
286
287 /**
288 * Optimize an attachment after being resized.
289 *
290 * @since 1.3.6
291 * @since 1.4 Async job.
292 * @since 1.8.4 Deprecated
293 * @deprecated
294 */
295 function _imagify_optimize_save_image_editor_file() {
296 _deprecated_function( __FUNCTION__ . '()', '1.8.4' );
297
298 if ( ! isset( $_POST['action'], $_POST['do'], $_POST['postid'] ) || 'image-editor' !== $_POST['action'] || 'open' === $_POST['do'] ) { // WPCS: CSRF ok.
299 return;
300 }
301
302 $attachment_id = absint( $_POST['postid'] );
303
304 if ( ! $attachment_id || ! Imagify_Requirements::is_api_key_valid() ) {
305 return;
306 }
307
308 check_ajax_referer( 'image_editor-' . $attachment_id );
309
310 $attachment = get_imagify_attachment( 'wp', $attachment_id, 'save_image_editor_file' );
311
312 if ( ! $attachment->get_data() ) {
313 return;
314 }
315
316 $body = $_POST;
317 $body['action'] = 'imagify_async_optimize_save_image_editor_file';
318
319 imagify_do_async_job( $body );
320 }
321
322
323 /**
324 * Display an admin notice informing that the current WP version is lower than the required one.
325 *
326 * @since 1.8.1
327 * @since 1.9 Deprecated
328 * @author Grégory Viguier
329 * @deprecated
330 */
331 function imagify_wp_version_notice() {
332 global $wp_version;
333
334 _deprecated_function( __FUNCTION__ . '()', '1.9', 'Imagify_Requirements_Check->print_notice()' );
335
336 if ( is_multisite() ) {
337 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
338 require_once ABSPATH . 'wp-admin/includes/plugin.php';
339 }
340
341 $is_active = is_plugin_active_for_network( plugin_basename( IMAGIFY_FILE ) );
342 $capacity = $is_active ? 'manage_network_options' : 'manage_options';
343 } else {
344 $capacity = 'manage_options';
345 }
346
347 if ( ! current_user_can( $capacity ) ) {
348 return;
349 }
350
351 echo '<div class="error notice"><p>';
352 echo '<strong>' . __( 'Notice:', 'imagify' ) . '</strong> ';
353 /* translators: 1 is this plugin name, 2 is the required WP version, 3 is the current WP version. */
354 printf( __( '%1$s requires WordPress %2$s minimum, your website is actually running version %3$s.', 'imagify' ), '<strong>Imagify</strong>', '<code>' . IMAGIFY_WP_MIN . '</code>', '<code>' . $wp_version . '</code>' );
355 echo '</p></div>';
356 }
357
358 /**
359 * Delete the backup file when an attachement is deleted.
360 *
361 * @since 1.0
362 * @since 1.9 Deprecated
363 * @deprecated
364 *
365 * @param int $post_id Attachment ID.
366 */
367 function _imagify_delete_backup_file( $post_id ) {
368 _deprecated_function( __FUNCTION__ . '()', '1.9', 'imagify_cleanup_after_media_deletion( $post_id )' );
369
370 get_imagify_attachment( 'wp', $post_id, 'delete_attachment' )->delete_backup();
371 }
372
373 /**
374 * Classes autoloader.
375 *
376 * @since 1.6.12
377 * @since 1.9 Deprecated
378 * @author Grégory Viguier
379 * @deprecated
380 *
381 * @param string $class Name of the class to include.
382 */
383 function imagify_autoload( $class ) {
384 static $strtolower;
385
386 _deprecated_function( __FUNCTION__ . '()', '1.9' );
387
388 if ( ! isset( $strtolower ) ) {
389 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
390 }
391
392 // Generic classes.
393 $classes = array(
394 'Imagify_Abstract_Attachment' => 1,
395 'Imagify_Abstract_Background_Process' => 1,
396 'Imagify_Abstract_Cron' => 1,
397 'Imagify_Abstract_DB' => 1,
398 'Imagify_Abstract_Options' => 1,
399 'Imagify_Admin_Ajax_Post' => 1,
400 'Imagify_Assets' => 1,
401 'Imagify_Attachment' => 1,
402 'Imagify_Auto_Optimization' => 1,
403 'Imagify_Cron_Library_Size' => 1,
404 'Imagify_Cron_Rating' => 1,
405 'Imagify_Cron_Sync_Files' => 1,
406 'Imagify_Custom_Folders' => 1,
407 'Imagify_Data' => 1,
408 'Imagify_DB' => 1,
409 'Imagify_File_Attachment' => 1,
410 'Imagify_Files_DB' => 1,
411 'Imagify_Files_Iterator' => 1,
412 'Imagify_Files_List_Table' => 1,
413 'Imagify_Files_Recursive_Iterator' => 1,
414 'Imagify_Files_Scan' => 1,
415 'Imagify_Files_Stats' => 1,
416 'Imagify_Filesystem' => 1,
417 'Imagify_Folders_DB' => 1,
418 'Imagify_Notices' => 1,
419 'Imagify_Options' => 1,
420 'Imagify_Requirements' => 1,
421 'Imagify_Settings' => 1,
422 'Imagify_User' => 1,
423 'Imagify_Views' => 1,
424 'Imagify' => 1,
425 );
426
427 if ( isset( $classes[ $class ] ) ) {
428 $class = str_replace( '_', '-', call_user_func( $strtolower, $class ) );
429 include IMAGIFY_PATH . 'inc/classes/class-' . $class . '.php';
430 return;
431 }
432
433 // Third party classes.
434 $classes = array(
435 'Imagify_AS3CF_Attachment' => 'amazon-s3-and-cloudfront',
436 'Imagify_AS3CF' => 'amazon-s3-and-cloudfront',
437 'Imagify_Enable_Media_Replace' => 'enable-media-replace',
438 'Imagify_Formidable_Pro' => 'formidable-pro',
439 'Imagify_NGG_Attachment' => 'nextgen-gallery',
440 'Imagify_NGG_DB' => 'nextgen-gallery',
441 'Imagify_NGG_Dynamic_Thumbnails_Background_Process' => 'nextgen-gallery',
442 'Imagify_NGG_Storage' => 'nextgen-gallery',
443 'Imagify_NGG' => 'nextgen-gallery',
444 'Imagify_Regenerate_Thumbnails' => 'regenerate-thumbnails',
445 'Imagify_WP_Retina_2x' => 'wp-retina-2x',
446 'Imagify_WP_Retina_2x_Core' => 'wp-retina-2x',
447 'Imagify_WP_Time_Capsule' => 'wp-time-capsule',
448 );
449
450 if ( isset( $classes[ $class ] ) ) {
451 $folder = $classes[ $class ];
452 $class = str_replace( '_', '-', call_user_func( $strtolower, $class ) );
453 include IMAGIFY_PATH . 'inc/3rd-party/' . $folder . '/inc/classes/class-' . $class . '.php';
454 }
455 }
456
457 /**
458 * Tell if the attachment has the required WP metadata.
459 *
460 * @since 1.6.12
461 * @since 1.7 Also checks that the '_wp_attached_file' meta is valid (not a URL or anything funny).
462 * @since 1.9 Deprecated
463 * @author Grégory Viguier
464 * @deprecated
465 *
466 * @param int $attachment_id The attachment ID.
467 * @return bool
468 */
469 function imagify_attachment_has_required_metadata( $attachment_id ) {
470 _deprecated_function( __FUNCTION__ . '()', '1.9', '( new Imagify\\Media\\WP( $attachment_id ) )->has_required_media_data() )' );
471
472 $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
473
474 if ( ! $file || preg_match( '@://@', $file ) || preg_match( '@^.:\\\@', $file ) ) {
475 return false;
476 }
477
478 return (bool) wp_get_attachment_metadata( $attachment_id, true );
479 }
480
481 /**
482 * Get the default Bulk Optimization buffer size.
483 *
484 * @since 1.5.10
485 * @since 1.7 Added $sizes parameter.
486 * @since 1.9 Deprecated
487 * @author Jonathan Buttigieg
488 * @deprecated
489 *
490 * @param int $sizes Number of image sizes per item (attachment).
491 * @return int The buffer size.
492 */
493 function get_imagify_bulk_buffer_size( $sizes = false ) {
494 _deprecated_function( __FUNCTION__ . '()', '1.9' );
495
496 if ( ! $sizes ) {
497 $sizes = count( get_imagify_thumbnail_sizes() );
498 }
499
500 switch ( true ) {
501 case ( $sizes >= 10 ):
502 return 1;
503
504 case ( $sizes >= 8 ):
505 return 2;
506
507 case ( $sizes >= 6 ):
508 return 3;
509
510 default:
511 return 4;
512 }
513 }
514
515 /**
516 * Get the Imagify attachment class name depending to a context.
517 *
518 * @since 1.5
519 * @since 1.6.6 $attachment_id and $identifier have been added.
520 * @since 1.9 Deprecated
521 * @author Jonathan Buttigieg
522 * @deprecated
523 *
524 * @param string $context The context to determine the class name.
525 * @param int $attachment_id The attachment ID.
526 * @param string $identifier An identifier.
527 * @return string The Imagify attachment class name.
528 */
529 function get_imagify_attachment_class_name( $context, $attachment_id, $identifier ) {
530 _deprecated_function( __FUNCTION__ . '()', '1.9', 'imagify_get_optimization_process_class_name( $context )' );
531
532 $context = $context ? $context : 'wp';
533
534 if ( 'wp' !== $context && 'wp' === strtolower( $context ) ) {
535 $context = 'wp';
536 }
537
538 /**
539 * Filter the context used for the optimization.
540 *
541 * @since 1.6.6
542 * @author Grégory Viguier
543 *
544 * @param string $context The context.
545 * @param int $attachment_id The attachment ID.
546 * @param string $identifier An identifier.
547 */
548 $context = apply_filters( 'imagify_optimize_attachment_context', $context, $attachment_id, $identifier );
549
550 return 'Imagify_' . ( 'wp' !== $context ? $context . '_' : '' ) . 'Attachment';
551 }
552
553 /**
554 * Get the Imagify attachment instance depending to a context.
555 *
556 * @since 1.6.13
557 * @since 1.9 Deprecated
558 * @author Grégory Viguier
559 * @deprecated
560 *
561 * @param string $context The context to determine the class name.
562 * @param int $attachment_id The attachment ID.
563 * @param string $identifier An identifier.
564 * @return object The Imagify attachment instance.
565 */
566 function get_imagify_attachment( $context, $attachment_id, $identifier ) {
567 _deprecated_function( __FUNCTION__ . '()', '1.9', 'imagify_get_optimization_process( $media_id, $context )' );
568
569 $class_name = get_imagify_attachment_class_name( $context, $attachment_id, $identifier );
570 return new $class_name( $attachment_id );
571 }
572
573 /**
574 * Optimize a file with Imagify.
575 *
576 * @since 1.0
577 * @since 1.9 Deprecated
578 * @deprecated
579 *
580 * @param string $file_path Absolute path to the file.
581 * @param array $args {
582 * Optional. An array of arguments.
583 *
584 * @type bool $backup Force a backup of the original file.
585 * @type int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal).
586 * @type bool $keep_exif To keep exif data or not.
587 * }
588 * @return array|WP_Error Optimized image data. A WP_Error object on error.
589 */
590 function do_imagify( $file_path, $args = array() ) {
591 _deprecated_function( __FUNCTION__ . '()', '1.9', '(new Imagify\\Optimization\\File( $file_path ))->optimize( $args )' );
592
593 $args = array_merge( array(
594 'backup' => get_imagify_option( 'backup' ),
595 'optimization_level' => get_imagify_option( 'optimization_level' ),
596 'keep_exif' => get_imagify_option( 'exif' ),
597 'context' => 'wp',
598 'resized' => false,
599 'original_size' => 0,
600 'backup_path' => null,
601 ), $args );
602
603 /**
604 * Filter the attachment path.
605 *
606 * @since 1.2
607 *
608 * @param string $file_path The attachment path.
609 */
610 $file_path = apply_filters( 'imagify_file_path', $file_path );
611
612 // Check that file path isn't empty.
613 if ( ! $file_path ) {
614 return new WP_Error( 'empty_path', __( 'File path is empty.', 'imagify' ) );
615 }
616
617 // Check if curl is available.
618 if ( ! Imagify_Requirements::supports_curl() ) {
619 return new WP_Error( 'curl', __( 'cURL is not available on the server.', 'imagify' ) );
620 }
621
622 $filesystem = imagify_get_filesystem();
623
624 // Check if imageMagick or GD is available.
625 if ( $filesystem->is_image( $file_path ) && ! Imagify_Requirements::supports_image_editor() ) {
626 return new WP_Error( 'image_editor', sprintf(
627 /* translators: %s is a "More info?" link. */
628 __( 'No php extensions are available to edit images on the server. ImageMagick or GD is required. %s', 'imagify' ),
629 '<a href="' . esc_url( imagify_get_external_url( 'documentation-imagick-gd' ) ) . '" target="_blank">' . __( 'More info?', 'imagify' ) . '</a>'
630 ) );
631 }
632
633 // Check if external HTTP requests are blocked.
634 if ( Imagify_Requirements::is_imagify_blocked() ) {
635 return new WP_Error( 'http_block_external', __( 'External HTTP requests are blocked.', 'imagify' ) );
636 }
637
638 // Check if the Imagify servers & the API are accessible.
639 if ( ! Imagify_Requirements::is_api_up() ) {
640 return new WP_Error( 'api_server_down', __( 'Sorry, our servers are temporarily unavailable. Please, try again in a couple of minutes.', 'imagify' ) );
641 }
642
643 // Check that the file exists.
644 if ( ! $filesystem->is_writable( $file_path ) || ! $filesystem->is_file( $file_path ) ) {
645 /* translators: %s is a file path. */
646 return new WP_Error( 'file_not_found', sprintf( __( 'Could not find %s.', 'imagify' ), $filesystem->make_path_relative( $file_path ) ) );
647 }
648
649 // Check that the file directory is writable.
650 if ( ! $filesystem->is_writable( $filesystem->dir_path( $file_path ) ) ) {
651 /* translators: %s is a file path. */
652 return new WP_Error( 'not_writable', sprintf( __( '%s is not writable.', 'imagify' ), $filesystem->make_path_relative( $filesystem->dir_path( $file_path ) ) ) );
653 }
654
655 /**
656 * Fires before to optimize the Image with Imagify.
657 *
658 * @since 1.0
659 *
660 * @param string $file_path Absolute path to the image file.
661 * @param bool $backup Force a backup of the original file.
662 */
663 do_action( 'before_do_imagify', $file_path, $args['backup'] );
664
665 // Create a backup file before sending to optimization (to make sure we can backup the file).
666 $do_backup = $args['backup'] && ! $args['resized'];
667
668 if ( $do_backup ) {
669 $backup_result = imagify_backup_file( $file_path, $args['backup_path'] );
670
671 if ( is_wp_error( $backup_result ) ) {
672 // Stop the process if we can't backup the file.
673 return $backup_result;
674 }
675 }
676
677 // Send image for optimization and fetch the response.
678 $response = upload_imagify_image( array(
679 'image' => $file_path,
680 'data' => wp_json_encode( array(
681 'aggressive' => ( 1 === (int) $args['optimization_level'] ),
682 'ultra' => ( 2 === (int) $args['optimization_level'] ),
683 'keep_exif' => $args['keep_exif'],
684 'context' => $args['context'],
685 'original_size' => $args['original_size'],
686 ) ),
687 ) );
688
689 // Check status code.
690 if ( is_wp_error( $response ) ) {
691 return new WP_Error( 'api_error', $response->get_error_message() );
692 }
693
694 if ( ! function_exists( 'download_url' ) ) {
695 require_once ABSPATH . 'wp-admin/includes/file.php';
696 }
697
698 $temp_file = download_url( $response->image );
699
700 if ( is_wp_error( $temp_file ) ) {
701 return new WP_Error( 'temp_file_not_found', $temp_file->get_error_message() );
702 }
703
704 $filesystem->move( $temp_file, $file_path, true );
705
706 /**
707 * Fires after to optimize the Image with Imagify.
708 *
709 * @since 1.0
710 *
711 * @param string $file_path Absolute path to the image file.
712 * @param bool $backup Force a backup of the original file.
713 */
714 do_action( 'after_do_imagify', $file_path, $args['backup'] );
715
716 return $response;
717 }
718
719 /**
720 * Backup a file.
721 *
722 * @since 1.6.8
723 * @since 1.9 Deprecated
724 * @author Grégory Viguier
725 * @deprecated
726 *
727 * @param string $file_path The file path.
728 * @param string $backup_path The backup path. This is useful for NGG for example, who doesn't store the backups in our backup folder.
729 * @return bool|object True on success. False if the backup option is not enabled. A WP_Error object on failure.
730 */
731 function imagify_backup_file( $file_path, $backup_path = null ) {
732 _deprecated_function( __FUNCTION__ . '()', '1.9', '(new Imagify\\Optimization\\File( $file_path ))->backup( $backup_path )' );
733
734 if ( ! get_imagify_option( 'backup' ) ) {
735 return false;
736 }
737
738 // Make sure the source path is not empty.
739 if ( ! $file_path ) {
740 return new WP_Error( 'empty_path', __( 'The file path is empty.', 'imagify' ) );
741 }
742
743 $filesystem = imagify_get_filesystem();
744
745 // Make sure the filesystem has no errors.
746 if ( ! empty( $filesystem->errors->errors ) ) {
747 return new WP_Error( 'filesystem_error', __( 'Filesystem error.', 'imagify' ), $filesystem->errors );
748 }
749
750 // Make sure the source file exists.
751 if ( ! $filesystem->exists( $file_path ) ) {
752 return new WP_Error( 'source_doesnt_exist', __( 'The file to backup does not exist.', 'imagify' ), array(
753 'file_path' => $filesystem->make_path_relative( $file_path ),
754 ) );
755 }
756
757 if ( ! isset( $backup_path ) ) {
758 // Make sure the backup directory is writable.
759 if ( ! Imagify_Requirements::attachments_backup_dir_is_writable() ) {
760 return new WP_Error( 'backup_dir_not_writable', __( 'The backup directory is not writable.', 'imagify' ) );
761 }
762
763 $backup_path = get_imagify_attachment_backup_path( $file_path );
764 }
765
766 // Make sure the uploads directory has no errors.
767 if ( ! $backup_path ) {
768 return new WP_Error( 'wp_upload_error', __( 'Error while retrieving the uploads directory path.', 'imagify' ) );
769 }
770
771 // Create sub-directories.
772 $filesystem->make_dir( $filesystem->dir_path( $backup_path ) );
773
774 /**
775 * Allow to overwrite the backup file if it already exists.
776 *
777 * @since 1.6.9
778 * @author Grégory Viguier
779 *
780 * @param bool $overwrite Whether to overwrite the backup file.
781 * @param string $file_path The file path.
782 * @param string $backup_path The backup path.
783 */
784 $overwrite = apply_filters( 'imagify_backup_overwrite_backup', false, $file_path, $backup_path );
785
786 // Copy the file.
787 $filesystem->copy( $file_path, $backup_path, $overwrite, FS_CHMOD_FILE );
788
789 // Make sure the backup copy exists.
790 if ( ! $filesystem->exists( $backup_path ) ) {
791 return new WP_Error( 'backup_doesnt_exist', __( 'The file could not be saved.', 'imagify' ), array(
792 'file_path' => $filesystem->make_path_relative( $file_path ),
793 'backup_path' => $filesystem->make_path_relative( $backup_path ),
794 ) );
795 }
796
797 return true;
798 }
799
800 if ( is_admin() ) :
801
802 /**
803 * Fix the capability for our capacity filter hook
804 *
805 * @since 1.0
806 * @since 1.7 Deprecated.
807 * @author Jonathan
808 * @deprecated
809 */
810 function _imagify_correct_capability_for_options_page() {
811 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->get_capability()' );
812
813 return Imagify_Settings::get_instance()->get_capability();
814 }
815
816 /**
817 * Tell to WordPress to be confident with our setting, we are clean!
818 *
819 * @since 1.0
820 * @since 1.7 Deprecated.
821 * @author Jonathan
822 * @deprecated
823 */
824 function _imagify_register_setting() {
825 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->register()' );
826
827 Imagify_Settings::get_instance()->register();
828 }
829
830 /**
831 * Filter specific options before its value is (maybe) serialized and updated.
832 *
833 * @since 1.0
834 * @since 1.7 Deprecated.
835 * @author Jonathan
836 * @deprecated
837 *
838 * @param mixed $value The new option value.
839 * @param mixed $old_value The old option value.
840 * @return array The new option value.
841 */
842 function _imagify_pre_update_option( $value, $old_value ) {
843 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->sanitize_and_validate( $value )' );
844
845 return Imagify_Settings::get_instance()->sanitize_and_validate( $value );
846 }
847
848 /**
849 * If the user clicked the "Save & Go to Bulk Optimizer" button, set a redirection to the bulk optimizer.
850 * We use this hook because it can be triggered even if the option value hasn't changed.
851 *
852 * @since 1.6.8
853 * @since 1.7 Deprecated.
854 * @author Grégory Viguier
855 * @deprecated
856 *
857 * @param mixed $value The new, unserialized option value.
858 * @param mixed $old_value The old option value.
859 * @return mixed The option value.
860 */
861 function _imagify_maybe_set_redirection_before_save_options( $value, $old_value ) {
862 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->maybe_set_redirection( $value, $old_value )' );
863
864 return Imagify_Settings::get_instance()->maybe_set_redirection( $value, $old_value );
865 }
866
867 /**
868 * Used to launch some actions after saving the network options.
869 *
870 * @since 1.6.5
871 * @since 1.7 Deprecated.
872 * @author Grégory Viguier
873 * @deprecated
874 *
875 * @param string $option Name of the network option.
876 * @param mixed $value Current value of the network option.
877 * @param mixed $old_value Old value of the network option.
878 */
879 function _imagify_after_save_network_options( $option, $value, $old_value ) {
880 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->after_save_network_options( $option, $value, $old_value )' );
881
882 Imagify_Settings::get_instance()->after_save_network_options( $option, $value, $old_value );
883 }
884
885 /**
886 * Used to launch some actions after saving the options.
887 *
888 * @since 1.0
889 * @since 1.5 Used to redirect user to Bulk Optimizer (if requested).
890 * @since 1.6.8 Not used to redirect user to Bulk Optimizer anymore: see _imagify_maybe_set_redirection_before_save_options().
891 * @since 1.7 Deprecated.
892 * @author Jonathan
893 * @deprecated
894 *
895 * @param mixed $old_value The old option value.
896 * @param mixed $value The new option value.
897 */
898 function _imagify_after_save_options( $old_value, $value ) {
899 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->after_save_options( $old_value, $value )' );
900
901 Imagify_Settings::get_instance()->after_save_options( $old_value, $value );
902 }
903
904 /**
905 * `options.php` do not handle site options. Let's use `admin-post.php` for multisite installations.
906 *
907 * @since 1.0
908 * @since 1.7 Deprecated.
909 * @deprecated
910 */
911 function _imagify_update_site_option_on_network() {
912 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Settings::get_instance()->update_site_option_on_network()' );
913
914 Imagify_Settings::get_instance()->update_site_option_on_network();
915 }
916
917 /**
918 * Display the plan chooser section.
919 *
920 * @since 1.6
921 * @since 1.7 Deprecated.
922 * @author Geoffrey
923 * @deprecated
924 *
925 * @return string HTML.
926 */
927 function get_imagify_new_to_imagify() {
928 _deprecated_function( __FUNCTION__ . '()', '1.7', 'imagify_get_template( \'part-new-to-imagify\' )' );
929
930 return imagify_get_template( 'part-new-to-imagify' );
931 }
932
933 /**
934 * Get the payment modal HTML.
935 *
936 * @since 1.6
937 * @since 1.6.3 Include discount banners.
938 * @since 1.7 Deprecated.
939 * @author Geoffrey
940 * @deprecated
941 */
942 function imagify_payment_modal() {
943 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->print_template( \'modal-payment\' )' );
944
945 Imagify_Views::get_instance()->print_template( 'modal-payment' );
946 }
947
948 /**
949 * Print the discount banner used inside Payment Modal.
950 *
951 * @since 1.6.3
952 * @since 1.7 Deprecated.
953 * @author Geoffrey Crofte
954 * @deprecated
955 */
956 function imagify_print_discount_banner() {
957 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->print_template( \'part-discount-banner\' )' );
958
959 Imagify_Views::get_instance()->print_template( 'part-discount-banner' );
960 }
961
962 /**
963 * Return the formatted price present in pricing tables.
964 *
965 * @since 1.6
966 * @since 1.7 Deprecated.
967 * @author Geoffrey
968 * @deprecated
969 *
970 * @param float $value The price value.
971 * @return string The markuped price.
972 */
973 function get_imagify_price_table_format( $value ) {
974 _deprecated_function( __FUNCTION__ . '()', '1.7' );
975
976 $v = explode( '.', (string) $value );
977
978 return '<span class="imagify-price-big">' . $v[0] . '</span> <span class="imagify-price-mini">.' . ( strlen( $v[1] ) === 1 ? $v[1] . '0' : $v[1] ) . '</span>';
979 }
980
981 /**
982 * Add submenu in menu "Settings".
983 *
984 * @since 1.0
985 * @since 1.7 Deprecated.
986 * @deprecated
987 */
988 function _imagify_settings_menu() {
989 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->add_network_menus()' );
990
991 Imagify_Views::get_instance()->add_network_menus();
992 }
993
994 /**
995 * Add submenu in menu "Media".
996 *
997 * @since 1.0
998 * @since 1.7 Deprecated.
999 * @deprecated
1000 */
1001 function _imagify_bulk_optimization_menu() {
1002 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->add_site_menus()' );
1003
1004 Imagify_Views::get_instance()->add_site_menus();
1005 }
1006
1007 /**
1008 * The main settings page.
1009 *
1010 * @since 1.0
1011 * @since 1.7 Deprecated.
1012 * @deprecated
1013 */
1014 function _imagify_display_options_page() {
1015 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->display_settings_page()' );
1016
1017 Imagify_Views::get_instance()->display_settings_page();
1018 }
1019
1020 /**
1021 * The bulk optimization page.
1022 *
1023 * @since 1.0
1024 * @since 1.7 Deprecated.
1025 * @deprecated
1026 */
1027 function _imagify_display_bulk_page() {
1028 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->display_bulk_page()' );
1029
1030 Imagify_Views::get_instance()->display_bulk_page();
1031 }
1032
1033 /**
1034 * Add link to the plugin configuration pages.
1035 *
1036 * @since 1.0
1037 * @since 1.7 Deprecated.
1038 *
1039 * @param array $actions An array of action links.
1040 * @return array
1041 */
1042 function _imagify_plugin_action_links( $actions ) {
1043 _deprecated_function( __FUNCTION__ . '()', '1.7', 'Imagify_Views::get_instance()->plugin_action_links( $actions )' );
1044
1045 return Imagify_Views::get_instance()->plugin_action_links( $actions );
1046 }
1047
1048 /**
1049 * Get stats data for a specific folder type.
1050 *
1051 * @since 1.7
1052 * @since 1.9 Deprecated
1053 * @author Grégory Viguier
1054 * @deprecated
1055 *
1056 * @param string $context A context.
1057 * @return array
1058 */
1059 function imagify_get_folder_type_data( $context ) {
1060 _deprecated_function( __FUNCTION__ . '()', '1.9', 'Imagify_Admin_Ajax_Post::get_instance()->get_bulk_instance( $context )->get_context_data()' );
1061
1062 /**
1063 * Get the data.
1064 */
1065 switch ( $context ) {
1066 case 'wp':
1067 $total_saving_data = imagify_count_saving_data();
1068 $data = array(
1069 'images-optimized' => imagify_count_optimized_attachments(),
1070 'errors' => imagify_count_error_attachments(),
1071 'optimized' => $total_saving_data['optimized_size'],
1072 'original' => $total_saving_data['original_size'],
1073 'errors_url' => get_imagify_admin_url( 'folder-errors', $context ),
1074 );
1075 break;
1076
1077 case 'custom-folders':
1078 $data = array(
1079 'images-optimized' => Imagify_Files_Stats::count_optimized_files(),
1080 'errors' => Imagify_Files_Stats::count_error_files(),
1081 'optimized' => Imagify_Files_Stats::get_optimized_size(),
1082 'original' => Imagify_Files_Stats::get_original_size(),
1083 'errors_url' => get_imagify_admin_url( 'folder-errors', $context ),
1084 );
1085 break;
1086
1087 default:
1088 /**
1089 * Provide custom folder type data.
1090 *
1091 * @since 1.7
1092 * @author Grégory Viguier
1093 *
1094 * @param array $data An array with keys corresponding to cell classes, and values formatted with HTML.
1095 * @param string $context A context.
1096 */
1097 $data = apply_filters( 'imagify_get_folder_type_data', [], $context );
1098
1099 if ( ! $data || ! is_array( $data ) ) {
1100 return [];
1101 }
1102 }
1103
1104 /**
1105 * Format the data.
1106 */
1107 /* translators: %s is a formatted number, dont use %d. */
1108 $data['images-optimized'] = sprintf( _n( '%s Media File Optimized', '%s Media Files Optimized', $data['images-optimized'], 'imagify' ), '<span>' . number_format_i18n( $data['images-optimized'] ) . '</span>' );
1109
1110 if ( $data['errors'] ) {
1111 /* translators: %s is a formatted number, dont use %d. */
1112 $data['errors'] = sprintf( _n( '%s Error', '%s Errors', $data['errors'], 'imagify' ), '<span>' . number_format_i18n( $data['errors'] ) . '</span>' );
1113 $data['errors'] .= ' <a href="' . esc_url( $data['errors_url'] ) . '">' . __( 'View Errors', 'imagify' ) . '</a>';
1114 } else {
1115 $data['errors'] = '';
1116 }
1117
1118 if ( $data['optimized'] ) {
1119 $data['optimized'] = '<span class="imagify-cell-label">' . __( 'Optimized Filesize', 'imagify' ) . '</span> ' . imagify_size_format( $data['optimized'], 2 );
1120 } else {
1121 $data['optimized'] = '';
1122 }
1123
1124 if ( $data['original'] ) {
1125 $data['original'] = '<span class="imagify-cell-label">' . __( 'Original Filesize', 'imagify' ) . '</span> ' . imagify_size_format( $data['original'], 2 );
1126 } else {
1127 $data['original'] = '';
1128 }
1129
1130 unset( $data['errors_url'] );
1131
1132 return $data;
1133 }
1134
1135 /**
1136 * Tell if the current user has the required ability to operate Imagify.
1137 *
1138 * @since 1.6.11
1139 * @since 1.9
1140 * @see imagify_get_capacity()
1141 * @author Grégory Viguier
1142 * @deprecated
1143 *
1144 * @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity.
1145 * @param int $post_id A post ID.
1146 * @return bool
1147 */
1148 function imagify_current_user_can( $describer = 'manage', $post_id = null ) {
1149 static $can_upload;
1150
1151 _deprecated_function( __FUNCTION__ . '()', '1.9', 'imagify_get_context( $context )->current_user_can( $describer, $media_id )' );
1152
1153 $post_id = $post_id ? $post_id : null;
1154 $capacity = imagify_get_capacity( $describer );
1155 $user_can = false;
1156
1157 if ( 'manage' !== $describer && 'bulk-optimize' !== $describer && 'optimize-file' !== $describer ) {
1158 // Describers that are not 'manage', 'bulk-optimize', and 'optimize-file' need an additional test for 'upload_files'.
1159 if ( ! isset( $can_upload ) ) {
1160 $can_upload = current_user_can( 'upload_files' );
1161 }
1162
1163 if ( $can_upload ) {
1164 if ( 'upload_files' === $capacity ) {
1165 // We already know it's true.
1166 $user_can = true;
1167 } else {
1168 $user_can = current_user_can( $capacity, $post_id );
1169 }
1170 }
1171 } else {
1172 $user_can = current_user_can( $capacity );
1173 }
1174
1175 /**
1176 * Filter the current user ability to operate Imagify.
1177 *
1178 * @since 1.6.11
1179 *
1180 * @param bool $user_can Tell if the current user has the required ability to operate Imagify.
1181 * @param string $capacity The user capacity.
1182 * @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity.
1183 * @param int $post_id A post ID (a gallery ID for NGG).
1184 */
1185 return apply_filters( 'imagify_current_user_can', $user_can, $capacity, $describer, $post_id );
1186 }
1187
1188 /**
1189 * Get user capacity to operate Imagify.
1190 *
1191 * @since 1.6.5
1192 * @since 1.6.11 Uses a string as describer for the first argument.
1193 * @since 1.9 Deprecated.
1194 * @author Grégory Viguier
1195 * @deprecated
1196 *
1197 * @param string $describer Capacity describer. Possible values are 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize', and 'optimize-file'.
1198 * @return string
1199 */
1200 function imagify_get_capacity( $describer = 'manage' ) {
1201 static $edit_attachment_cap;
1202
1203 _deprecated_function( __FUNCTION__ . '()', '1.9', 'imagify_get_context( $context )->get_capacity( $describer )' );
1204
1205 // Back compat.
1206 if ( ! is_string( $describer ) ) {
1207 if ( $describer || ! is_multisite() ) {
1208 $describer = 'bulk-optimize';
1209 } else {
1210 $describer = 'manage';
1211 }
1212 }
1213
1214 switch ( $describer ) {
1215 case 'manage':
1216 $capacity = imagify_is_active_for_network() ? 'manage_network_options' : 'manage_options';
1217 break;
1218
1219 case 'optimize-file':
1220 $capacity = is_multisite() ? 'manage_network_options' : 'manage_options';
1221 break;
1222
1223 case 'bulk-optimize':
1224 $capacity = 'manage_options';
1225 break;
1226
1227 case 'optimize':
1228 case 'restore':
1229 // This is a generic capacity: don't use it unless you have no other choices!
1230 if ( ! isset( $edit_attachment_cap ) ) {
1231 $edit_attachment_cap = get_post_type_object( 'attachment' );
1232 $edit_attachment_cap = $edit_attachment_cap ? $edit_attachment_cap->cap->edit_posts : 'edit_posts';
1233 }
1234
1235 $capacity = $edit_attachment_cap;
1236 break;
1237
1238 case 'manual-optimize':
1239 case 'manual-restore':
1240 // Must be used with an Attachment ID.
1241 $capacity = 'edit_post';
1242 break;
1243
1244 case 'auto-optimize':
1245 $capacity = 'upload_files';
1246 break;
1247
1248 default:
1249 $capacity = $describer;
1250 }
1251
1252 /**
1253 * Filter the user capacity used to operate Imagify.
1254 *
1255 * @since 1.0
1256 * @since 1.6.5 Added $force_mono parameter.
1257 * @since 1.6.11 Replaced $force_mono by $describer.
1258 *
1259 * @param string $capacity The user capacity.
1260 * @param string $describer Capacity describer. Possible values are 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize', and 'optimize-file'.
1261 */
1262 return apply_filters( 'imagify_capacity', $capacity, $describer );
1263 }
1264
1265 /**
1266 * Check for user capacity.
1267 *
1268 * @since 1.6.10
1269 * @since 1.6.11 Uses a capacity describer instead of a capacity itself.
1270 * @since 1.9 Deprecated.
1271 * @see imagify_get_capacity()
1272 * @author Grégory Viguier
1273 * @deprecated
1274 *
1275 * @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity.
1276 * @param int $post_id A post ID.
1277 */
1278 function imagify_check_user_capacity( $describer = 'manage', $post_id = null ) {
1279 _deprecated_function( __FUNCTION__ . '()', '1.9' );
1280
1281 if ( ! imagify_current_user_can( $describer, $post_id ) ) {
1282 imagify_die();
1283 }
1284 }
1285
1286 /**
1287 * Update the Heartbeat API settings.
1288 *
1289 * @since 1.4.5
1290 * @since 1.9.3 Deprecated.
1291 * @deprecated
1292 *
1293 * @param array $settings Heartbeat API settings.
1294 * @return array
1295 */
1296 function _imagify_heartbeat_settings( $settings ) {
1297 _deprecated_function( __FUNCTION__ . '()', '1.9.3' );
1298
1299 $settings['interval'] = 30;
1300 return $settings;
1301 }
1302
1303 /**
1304 * Prepare the data that goes back with the Imagifybeat API.
1305 *
1306 * @since 1.4.5
1307 * @since 1.9.3 Deprecated.
1308 * @deprecated
1309 *
1310 * @param array $response The Imagifybeat response.
1311 * @param array $data The $_POST data sent.
1312 * @return array
1313 */
1314 function _imagify_heartbeat_received( $response, $data ) {
1315 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->add_bulk_optimization_stats_to_response()' );
1316
1317 $heartbeat_id = 'imagify_bulk_data';
1318
1319 if ( empty( $data[ $heartbeat_id ] ) ) {
1320 return $response;
1321 }
1322
1323 $folder_types = array_flip( array_filter( $data[ $heartbeat_id ] ) );
1324
1325 $response[ $heartbeat_id ] = imagify_get_bulk_stats( $folder_types, array(
1326 'fullset' => true,
1327 ) );
1328
1329 return $response;
1330 }
1331
1332 /**
1333 * Prepare the data that goes back with the Imagifybeat API.
1334 *
1335 * @since 1.7.1
1336 * @since 1.9.3 Deprecated.
1337 * @author Grégory Viguier
1338 * @deprecated
1339 *
1340 * @param array $response The Imagifybeat response.
1341 * @param array $data The $_POST data sent.
1342 * @return array
1343 */
1344 function imagify_heartbeat_requirements_received( $response, $data ) {
1345 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->add_requirements_to_response()' );
1346
1347 $heartbeat_id = 'imagify_bulk_requirements';
1348
1349 if ( empty( $data[ $heartbeat_id ] ) ) {
1350 return $response;
1351 }
1352
1353 $response[ $heartbeat_id ] = array(
1354 'curl_missing' => ! Imagify_Requirements::supports_curl(),
1355 'editor_missing' => ! Imagify_Requirements::supports_image_editor(),
1356 'external_http_blocked' => Imagify_Requirements::is_imagify_blocked(),
1357 'api_down' => Imagify_Requirements::is_imagify_blocked() || ! Imagify_Requirements::is_api_up(),
1358 'key_is_valid' => ! Imagify_Requirements::is_imagify_blocked() && Imagify_Requirements::is_api_up() && Imagify_Requirements::is_api_key_valid(),
1359 'is_over_quota' => ! Imagify_Requirements::is_imagify_blocked() && Imagify_Requirements::is_api_up() && Imagify_Requirements::is_api_key_valid() && Imagify_Requirements::is_over_quota(),
1360 );
1361
1362 return $response;
1363 }
1364
1365 /**
1366 * Look for media where status has changed, compared to what Imagifybeat sends.
1367 * This is used in the bulk optimization page.
1368 *
1369 * @since 1.9
1370 * @since 1.9.3 Deprecated.
1371 * @author Grégory Viguier
1372 * @deprecated
1373 *
1374 * @param array $response The Imagifybeat response.
1375 * @param array $data The $_POST data sent.
1376 * @return array
1377 */
1378 function imagify_heartbeat_bulk_optimization_status_received( $response, $data ) {
1379 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->add_bulk_optimization_status_to_response()' );
1380
1381 $heartbeat_id = 'imagify_bulk_queue';
1382
1383 if ( empty( $data[ $heartbeat_id ] ) || ! is_array( $data[ $heartbeat_id ] ) ) {
1384 return $response;
1385 }
1386
1387 $statuses = [];
1388
1389 foreach ( $data[ $heartbeat_id ] as $item ) {
1390 if ( empty( $statuses[ $item['context'] ] ) ) {
1391 $statuses[ $item['context'] ] = [];
1392 }
1393
1394 $statuses[ $item['context'] ][ '_' . $item['mediaID'] ] = 1;
1395 }
1396
1397 $results = imagify_get_modified_optimization_statusses( $statuses );
1398
1399 if ( ! $results ) {
1400 return $response;
1401 }
1402
1403 $response[ $heartbeat_id ] = [];
1404
1405 // Sanitize received data and grab some other info.
1406 foreach ( $results as $context_id => $media_atts ) {
1407 $process = imagify_get_optimization_process( $media_atts['media_id'], $media_atts['context'] );
1408 $optim_data = $process->get_data();
1409
1410 if ( $optim_data->is_optimized() ) {
1411 // Successfully optimized.
1412 $full_size_data = $optim_data->get_size_data();
1413 $response[ $heartbeat_id ][] = [
1414 'mediaID' => $media_atts['media_id'],
1415 'context' => $media_atts['context'],
1416 'success' => true,
1417 'status' => 'optimized',
1418 // Raw data.
1419 'originalOverallSize' => $full_size_data['original_size'],
1420 'newOverallSize' => $full_size_data['optimized_size'],
1421 'overallSaving' => $full_size_data['original_size'] - $full_size_data['optimized_size'],
1422 'thumbnailsCount' => $optim_data->get_optimized_sizes_count(),
1423 // Human readable data.
1424 'originalSizeHuman' => imagify_size_format( $full_size_data['original_size'], 2 ),
1425 'newSizeHuman' => imagify_size_format( $full_size_data['optimized_size'], 2 ),
1426 'overallSavingHuman' => imagify_size_format( $full_size_data['original_size'] - $full_size_data['optimized_size'], 2 ),
1427 'originalOverallSizeHuman' => imagify_size_format( $full_size_data['original_size'], 2 ),
1428 'percentHuman' => $full_size_data['percent'] . '%',
1429 ];
1430 } elseif ( $optim_data->is_already_optimized() ) {
1431 // Already optimized.
1432 $response[ $heartbeat_id ][] = [
1433 'mediaID' => $media_atts['media_id'],
1434 'context' => $media_atts['context'],
1435 'success' => true,
1436 'status' => 'already-optimized',
1437 ];
1438 } else {
1439 // Error.
1440 $full_size_data = $optim_data->get_size_data();
1441 $message = ! empty( $full_size_data['error'] ) ? $full_size_data['error'] : '';
1442 $status = 'error';
1443
1444 if ( 'You\'ve consumed all your data. You have to upgrade your account to continue' === $message ) {
1445 $status = 'over-quota';
1446 }
1447
1448 $response[ $heartbeat_id ][] = [
1449 'mediaID' => $media_atts['media_id'],
1450 'context' => $media_atts['context'],
1451 'success' => false,
1452 'status' => $status,
1453 'error' => imagify_translate_api_message( $message ),
1454 ];
1455 }
1456 }
1457
1458 return $response;
1459 }
1460
1461 /**
1462 * Look for media where status has changed, compared to what Imagifybeat sends.
1463 * This is used in the settings page.
1464 *
1465 * @since 1.9
1466 * @since 1.9.3 Deprecated.
1467 * @author Grégory Viguier
1468 * @deprecated
1469 *
1470 * @param array $response The Imagifybeat response.
1471 * @param array $data The $_POST data sent.
1472 * @return array
1473 */
1474 function imagify_heartbeat_options_bulk_optimization_status_received( $response, $data ) {
1475 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->add_options_optimization_status_to_response()' );
1476
1477 $heartbeat_id = 'imagify_options_bulk_queue';
1478
1479 if ( empty( $data[ $heartbeat_id ] ) || ! is_array( $data[ $heartbeat_id ] ) ) {
1480 return $response;
1481 }
1482
1483 $statuses = [];
1484
1485 foreach ( $data[ $heartbeat_id ] as $item ) {
1486 if ( empty( $statuses[ $item['context'] ] ) ) {
1487 $statuses[ $item['context'] ] = [];
1488 }
1489
1490 $statuses[ $item['context'] ][ '_' . $item['mediaID'] ] = 1;
1491 }
1492
1493 $results = imagify_get_modified_optimization_statusses( $statuses );
1494
1495 if ( ! $results ) {
1496 return $response;
1497 }
1498
1499 $response[ $heartbeat_id ] = [];
1500
1501 foreach ( $results as $result ) {
1502 $response[ $heartbeat_id ][] = [
1503 'mediaID' => $result['media_id'],
1504 'context' => $result['context'],
1505 ];
1506 }
1507
1508 return $response;
1509 }
1510
1511 /**
1512 * Look for media where status has changed, compared to what Imagifybeat sends.
1513 * This is used in the WP Media Library.
1514 *
1515 * @since 1.9
1516 * @since 1.9.3 Deprecated.
1517 * @author Grégory Viguier
1518 * @deprecated
1519 *
1520 * @param array $response The Imagifybeat response.
1521 * @param array $data The $_POST data sent.
1522 * @return array
1523 */
1524 function imagify_heartbeat_optimization_status_received( $response, $data ) {
1525 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->add_library_optimization_status_to_response()' );
1526
1527 $heartbeat_id = get_imagify_localize_script_translations( 'media-modal' );
1528 $heartbeat_id = $heartbeat_id['heartbeatId'];
1529
1530 if ( empty( $data[ $heartbeat_id ] ) || ! is_array( $data[ $heartbeat_id ] ) ) {
1531 return $response;
1532 }
1533
1534 $response[ $heartbeat_id ] = imagify_get_modified_optimization_statusses( $data[ $heartbeat_id ] );
1535
1536 if ( ! $response[ $heartbeat_id ] ) {
1537 return $response;
1538 }
1539
1540 // Sanitize received data and grab some other info.
1541 foreach ( $response[ $heartbeat_id ] as $context_id => $media_atts ) {
1542 $process = imagify_get_optimization_process( $media_atts['media_id'], $media_atts['context'] );
1543
1544 $response[ $heartbeat_id ][ $context_id ] = get_imagify_media_column_content( $process, false );
1545 }
1546
1547 return $response;
1548 }
1549
1550 /**
1551 * Look for media where status has changed, compared to what Imagifybeat sends.
1552 * This is used in the custom folders list (the "Other Media" page).
1553 *
1554 * @since 1.9
1555 * @since 1.9.3 Deprecated.
1556 * @author Grégory Viguier
1557 * @deprecated
1558 *
1559 * @param array $response The Imagifybeat response.
1560 * @param array $data The $_POST data sent.
1561 * @return array
1562 */
1563 function imagify_heartbeat_custom_folders_optimization_status_received( $response, $data ) {
1564 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->add_custom_folders_optimization_status_to_response()' );
1565
1566 $heartbeat_id = get_imagify_localize_script_translations( 'files-list' );
1567 $heartbeat_id = $heartbeat_id['heartbeatId'];
1568
1569 if ( empty( $data[ $heartbeat_id ] ) || ! is_array( $data[ $heartbeat_id ] ) ) {
1570 return $response;
1571 }
1572
1573 $response[ $heartbeat_id ] = imagify_get_modified_optimization_statusses( $data[ $heartbeat_id ] );
1574
1575 if ( ! $response[ $heartbeat_id ] ) {
1576 return $response;
1577 }
1578
1579 $admin_ajax_post = Imagify_Admin_Ajax_Post::get_instance();
1580 $list_table = new Imagify_Files_List_Table( [
1581 'screen' => 'imagify-files',
1582 ] );
1583
1584 // Sanitize received data and grab some other info.
1585 foreach ( $response[ $heartbeat_id ] as $context_id => $media_atts ) {
1586 $process = imagify_get_optimization_process( $media_atts['media_id'], $media_atts['context'] );
1587
1588 $response[ $heartbeat_id ][ $context_id ] = $admin_ajax_post->get_media_columns( $process, $list_table );
1589 }
1590
1591 return $response;
1592 }
1593
1594 /**
1595 * Look for media where status has changed, compared to what Imagifybeat sends.
1596 *
1597 * @since 1.9
1598 * @since 1.9.3 Deprecated.
1599 * @author Grégory Viguier
1600 * @deprecated
1601 *
1602 * @param array $data The data received.
1603 * @return array
1604 */
1605 function imagify_get_modified_optimization_statusses( $data ) {
1606 _deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\Imagifybeat\\Actions::get_instance()->get_modified_optimization_statuses()' );
1607
1608 if ( ! $data ) {
1609 return [];
1610 }
1611
1612 $output = [];
1613
1614 // Sanitize received data and grab some other info.
1615 foreach ( $data as $context => $media_statuses ) {
1616 if ( ! $context || ! $media_statuses || ! is_array( $media_statuses ) ) {
1617 continue;
1618 }
1619
1620 // Sanitize the IDs: IDs come as strings, prefixed with an undescore character (to prevent JavaScript from screwing everything).
1621 $media_ids = array_keys( $media_statuses );
1622 $media_ids = array_map( function( $media_id ) {
1623 return (int) substr( $media_id, 1 );
1624 }, $media_ids );
1625 $media_ids = array_filter( $media_ids );
1626
1627 if ( ! $media_ids ) {
1628 continue;
1629 }
1630
1631 // Sanitize the context.
1632 $context_instance = imagify_get_context( $context );
1633 $context = $context_instance->get_name();
1634 $process_class_name = imagify_get_optimization_process_class_name( $context );
1635 $transient_name = sprintf( $process_class_name::LOCK_NAME, $context, '%' );
1636 $is_network_wide = $context_instance->is_network_wide();
1637
1638 Imagify_DB::cache_process_locks( $context, $media_ids );
1639
1640 // Now that everything is cached for this context, we can get the transients without hitting the DB.
1641 foreach ( $media_ids as $id ) {
1642 $is_locked = (bool) $media_statuses[ '_' . $id ];
1643 $option_name = str_replace( '%', $id, $transient_name );
1644
1645 if ( $is_network_wide ) {
1646 $in_db = (bool) get_site_transient( $option_name );
1647 } else {
1648 $in_db = (bool) get_transient( $option_name );
1649 }
1650
1651 if ( $is_locked === $in_db ) {
1652 continue;
1653 }
1654
1655 $output[ $context . '_' . $id ] = [
1656 'media_id' => $id,
1657 'context' => $context,
1658 ];
1659 }
1660 }
1661
1662 return $output;
1663 }
1664
1665 endif;
1666