PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.4.1
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.4.1
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-updraft-smush-manager-commands.php

class-updraft-smush-manager-commands.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.4.1, at includes/class-updraft-smush-manager-commands.php

668 lines 23.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A Smush Task manager class
4 */
5
6 if (!defined('ABSPATH')) die('Access denied.');
7
8 if (!class_exists('Updraft_Task_Manager_Commands_1_0')) require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-task-manager-commands.php');
9
10 if (!class_exists('Updraft_Smush_Manager_Commands')) :
11
12 class Updraft_Smush_Manager_Commands extends Updraft_Task_Manager_Commands_1_0 {
13
14 /**
15 * Stores the bulk of images to be processed
16 *
17 * @var array
18 */
19 private $images = array();
20
21 /**
22 * Flag to return a valid envelope on AJAX calls
23 *
24 * @var bool
25 */
26 public $background_command = false;
27
28 /**
29 * Flag to return a valid envelope on heartbeat AJAX calls
30 *
31 * @var bool
32 */
33 public $heartbeat_command = false;
34
35 /**
36 * Store the response to be sent at shutdown
37 *
38 * @var array
39 */
40 public $final_response = array();
41
42 /**
43 * The commands constructor
44 *
45 * @param mixed $task_manager - A task manager instance
46 */
47 public function __construct($task_manager) {
48 parent::__construct($task_manager);
49 }
50
51 /**
52 * Returns a list of commands available for smush related operations
53 */
54 public static function get_allowed_ajax_commands() {
55
56 $commands = parent::get_allowed_ajax_commands();
57
58 $smush_commands = array(
59 'compress_single_image',
60 'restore_single_image',
61 'process_bulk_smush',
62 'update_smush_options',
63 'get_ui_update',
64 'process_pending_images',
65 'clear_pending_images',
66 'clear_smush_stats',
67 'check_server_status',
68 'get_smush_logs',
69 'mark_as_compressed',
70 'mark_all_as_uncompressed',
71 'clean_all_backup_images',
72 'reset_webp_serving_method',
73 'convert_to_webp_format',
74 'update_webp_options',
75 'get_smush_details',
76 );
77
78 return array_merge($commands, $smush_commands);
79 }
80
81 /**
82 * Process the compression of a single image
83 *
84 * @param mixed $data - sent in via AJAX
85 * @return WP_Error|array - information about the operation or WP_Error object on failure
86 */
87 public function compress_single_image($data) {
88
89 $options = !empty($data['smush_options']) ? $data['smush_options'] : $this->task_manager->get_smush_options();
90 $image = isset($data['selected_image']) ? filter_var($data['selected_image']['attachment_id'], FILTER_SANITIZE_NUMBER_INT) : false;
91 $blog = isset($data['selected_image']) ? filter_var($data['selected_image']['blog_id'], FILTER_SANITIZE_NUMBER_INT) : false;
92
93 // A subsite administrator can only compress their own image. If the blog ID isn't theirs, return an error.
94 if ($blog && is_multisite() && get_current_blog_id() != $blog && !current_user_can('manage_network_options')) {
95 return new WP_Error('compression_not_permitted', __('The blog ID provided does not match the current blog.', 'wp-optimize'));
96 }
97
98 $server = sanitize_text_field($options['compression_server']);
99
100 $lossy = filter_var($options['lossy_compression'], FILTER_VALIDATE_BOOLEAN) ? true : false;
101 $backup = filter_var($options['back_up_original'], FILTER_VALIDATE_BOOLEAN) ? true : false;
102 $exif = filter_var($options['preserve_exif'], FILTER_VALIDATE_BOOLEAN) ? true : false;
103 $quality = filter_var($options['image_quality'], FILTER_SANITIZE_NUMBER_INT);
104
105 $options = array(
106 'attachment_id' => $image,
107 'blog_id' => $blog,
108 'image_quality' => $quality,
109 'keep_original' => $backup,
110 'lossy_compression' => $lossy,
111 'preserve_exif' => $exif
112 );
113
114 if (filesize(get_attached_file($image)) > 5242880) {
115 $options['request_timeout'] = 180;
116 }
117
118 $success = $this->task_manager->compress_single_image($image, $options, $server);
119
120 if (!$success) {
121 return new WP_Error('compress_failed', get_post_meta($image, 'smush-info', true));
122 }
123
124 $response = array();
125 $response['status'] = true;
126 $response['operation'] = 'compress';
127 $response['options'] = $options;
128 $response['server'] = $server;
129 $response['success'] = $success;
130 $response['restore_possible'] = $backup;
131 $response['summary'] = get_post_meta($image, 'smush-info', true);
132
133 $smush_stats = get_post_meta($image, 'smush-stats', true);
134 if (isset($smush_stats['sizes-info'])) {
135 $response['sizes-info'] = WP_Optimize()->include_template('images/smush-details.php', true, array('sizes_info' => $smush_stats['sizes-info']));
136 }
137
138 return $response;
139 }
140
141 /**
142 * Restores a single image, if backup is available
143 *
144 * @param mixed $data - Sent in via AJAX
145 * @return WP_Error|array - information about the operation or a WP_Error object on failure
146 */
147 public function restore_single_image($data) {
148
149 $blog_id = isset($data['blog_id']) ? $data['blog_id'] : false;
150 $image_id = isset($data['selected_image']) ? $data['selected_image'] : false;
151
152 $success = $this->task_manager->restore_single_image($image_id, $blog_id);
153
154 if (is_wp_error($success)) {
155 return $success;
156 }
157
158 $response = array();
159 $response['status'] = true;
160 $response['operation'] = 'restore';
161 $response['blog_id'] = $blog_id;
162 $response['image'] = $image_id;
163 $response['success'] = $success;
164 $response['summary'] = __('The image was restored successfully', 'wp-optimize');
165
166 return $response;
167 }
168
169 /**
170 * Process the compression of multiple images
171 *
172 * @param mixed $data - Sent in via AJAX
173 * @return array
174 */
175 public function process_bulk_smush($data = array()) {
176 $images = isset($data['selected_images']) ? $data['selected_images'] : array();
177
178 $this->images = $images;
179
180 $this->background_command = true;
181
182 $this->final_response = $this->get_ui_update($this->images);
183
184 add_action('shutdown', array($this, 'process_bulk_smush_shutdown'));
185
186 return $this->final_response;
187 }
188
189 /**
190 * Close request connection at the `shutdown` hook and start processing the bulk
191 *
192 * @return void
193 */
194 public function process_bulk_smush_shutdown() {
195 WP_Optimize()->close_browser_connection(json_encode($this->final_response));
196
197 $this->task_manager->process_bulk_smush($this->images);
198 exit;
199 }
200
201 /**
202 * Returns useful information for the UI and closes the connection
203 *
204 * @param mixed $data - Sent in via AJAX
205 *
206 * @return array - Information for the UI
207 */
208 public function get_ui_update($data) {
209 $ui_update = array();
210 $ui_update['status'] = true;
211 $ui_update['is_multisite'] = is_multisite() ? 1 : 0;
212 $pending_tasks = $this->task_manager->get_pending_tasks();
213
214 $ui_update['pending_tasks'] = is_array($pending_tasks) ? count($this->task_manager->get_pending_tasks()) : 0;
215 $ui_update['unsmushed_images'] = $this->task_manager->get_uncompressed_images(isset($data['use_cache']) ? $data['use_cache'] : "true");
216 $ui_update['admin_urls'] = $this->task_manager->get_admin_urls();
217 $ui_update['completed_task_count'] = $this->task_manager->options->get_option('completed_task_count', 0);
218 $ui_update['bytes_saved'] = WP_Optimize()->format_size($this->task_manager->options->get_option('total_bytes_saved', 0));
219 $ui_update['percent_saved'] = number_format($this->task_manager->options->get_option('total_percent_saved', 1), 2).'%';
220 $ui_update['failed_task_count'] = $this->task_manager->get_failed_task_count();
221
222 if (is_multisite()) {
223 $ui_update['summary'] = sprintf(__('Since the last reset of compression statistics on this multisite, a total of %d image(s) were compressed across the network.', 'wp-optimize').' '.__('This saved approximately %s of space at an average of %02d percent per image.', 'wp-optimize'), $ui_update['completed_task_count'], $ui_update['bytes_saved'], $ui_update['percent_saved']);
224 } else {
225 $ui_update['summary'] = sprintf(__('Since your compression statistics were last reset, a total of %d image(s) were compressed on this site.', 'wp-optimize').' '.__('This saved approximately %s of space at an average of %02d percent per image.', 'wp-optimize'), $ui_update['completed_task_count'], $ui_update['bytes_saved'], $ui_update['percent_saved']);
226 }
227 $ui_update['failed'] = sprintf(__("%d image(s) could not be compressed.", 'wp-optimize'), $ui_update['failed_task_count']) . ' ' . __('Please see the logs for more information, or try again later.', 'wp-optimize');
228 $ui_update['pending'] = sprintf(__("%d image(s) images were selected for compressing previously, but were not all processed.", 'wp-optimize'), $ui_update['pending_tasks']) . ' ' . __('You can either complete them now or cancel and retry later.', 'wp-optimize');
229 $ui_update['smush_complete'] = $this->task_manager->is_queue_processed();
230
231 if (isset($data['image_list'])) {
232 $images = $data['image_list'];
233 $stats = $this->task_manager->get_session_stats($images);
234 $ui_update['session_stats'] = "";
235
236 if (!empty($stats['success'])) {
237 $ui_update['session_stats'] .= sprintf(__("A total of %d image(s) were successfully compressed in this iteration.", 'wp-optimize'), $stats['success']);
238 }
239
240 if (!empty($stats['fail'])) {
241 $ui_update['session_stats'] .= sprintf(__("%d selected image(s) could not be compressed.", 'wp-optimize'), $stats['fail']) . ' ' . __('Please see the logs for more information, you may try again later.', 'wp-optimize');
242 }
243 }
244
245 return $ui_update;
246
247 }
248
249 /**
250 * Updates webp related options
251 *
252 * @param mixed $data - Sent in via AJAX
253 * @return WP_Error|array - information about the operation or WP_Error object on failure
254 */
255 public function update_webp_options($data) {
256 $webp_instance = WP_Optimize()->get_webp_instance();
257 $options = array();
258 $options['webp_conversion'] = filter_var($data['webp_conversion'], FILTER_VALIDATE_BOOLEAN);
259
260 // Only run checks when trying to enable WebP
261 if ($options['webp_conversion']) {
262 //Run checks if we are enabling webp conversion
263 if (!WP_Optimize_WebP::is_shell_functions_available()) {
264 $webp_instance->disable_webp_conversion();
265 return new WP_Error('update_failed_no_shell_functions', __('Required WebP shell functions are not available on server.', 'wp-optimize'));
266 }
267
268 // Run conversion test if not already done and set necessary option value
269 if ($webp_instance->should_run_webp_conversion_test()) {
270 $converter_status = WPO_WebP_Test_Run::get_converter_status();
271
272 if (!$webp_instance->is_webp_conversion_successful()) {
273 $webp_instance->disable_webp_conversion();
274 return new WP_Error('update_failed_no_working_webp_converter', __('No working Webp converter was found on server.', 'wp-optimize'));
275 }
276
277 $options['webp_conversion_test'] = true;
278 $options['webp_converters'] = $converter_status['working_converters'];
279 }
280
281 // Run serving methods tests and set necessary option values
282 // Not possible to test alter html since test is browser based
283 $webp_instance->save_htaccess_rules();
284 if (!$webp_instance->is_webp_redirection_possible()) {
285 $webp_instance->empty_htaccess_file();
286 $options['redirection_possible'] = 'false';
287 } else {
288 $options['redirection_possible'] = 'true';
289 }
290 }
291
292 $success = $this->task_manager->update_smush_options($options);
293
294 if (!$success) {
295 $webp_instance->disable_webp_conversion();
296 return new WP_Error('update_failed', __('Webp options could not be updated.', 'wp-optimize'));
297 }
298
299 // Setup daily CRON only when enabling WebP and Delete daily CRON when disabling WebP
300 if ($options['webp_conversion']) {
301 $webp_instance->init_webp_cron_scheduler();
302 } else {
303 $webp_instance->remove_webp_cron_schedules();
304 $webp_instance->empty_htaccess_file();
305 }
306
307 do_action('wpo_save_images_settings');
308
309 $response = array();
310 $response['status'] = true;
311 $response['saved'] = $success;
312 $response['summary'] = __('Webp options updated successfully.', 'wp-optimize');
313
314 return $response;
315 }
316
317 /**
318 * Updates smush related options
319 *
320 * @param mixed $data - Sent in via AJAX
321 * @return WP_Error|array - information about the operation or WP_Error object on failure
322 */
323 public function update_smush_options($data) {
324 $options = array();
325 $options['compression_server'] = sanitize_text_field($data['compression_server']);
326 $options['lossy_compression'] = filter_var($data['lossy_compression'], FILTER_VALIDATE_BOOLEAN) ? true : false;
327 $options['back_up_original'] = filter_var($data['back_up_original'], FILTER_VALIDATE_BOOLEAN) ? true : false;
328 $options['back_up_delete_after'] = filter_var($data['back_up_delete_after'], FILTER_VALIDATE_BOOLEAN) ? true : false;
329 $options['back_up_delete_after_days'] = filter_var($data['back_up_delete_after_days'], FILTER_SANITIZE_NUMBER_INT);
330 $options['preserve_exif'] = filter_var($data['preserve_exif'], FILTER_VALIDATE_BOOLEAN) ? true : false;
331 $options['autosmush'] = filter_var($data['autosmush'], FILTER_VALIDATE_BOOLEAN) ? true : false;
332 $options['image_quality'] = filter_var($data['image_quality'], FILTER_SANITIZE_NUMBER_INT);
333 $options['show_smush_metabox'] = filter_var($data['show_smush_metabox'], FILTER_VALIDATE_BOOLEAN) ? 'show' : 'hide';
334
335 $success = $this->task_manager->update_smush_options($options);
336
337 if (!$success) {
338 return new WP_Error('update_failed', __('Smush options could not be updated', 'wp-optimize'));
339 }
340
341 do_action('wpo_save_images_settings');
342
343 $response = array();
344 $response['status'] = true;
345 $response['saved'] = $success;
346 $response['summary'] = __('Options updated successfully', 'wp-optimize');
347
348 return $response;
349 }
350
351 /**
352 * Clears any smush related stats
353 *
354 * @return WP_Error|array - information about the operation or WP_Error object on failure
355 */
356 public function clear_smush_stats() {
357
358 $success = $this->task_manager->clear_smush_stats();
359
360 if (!$success) {
361 return new WP_Error('update_failed', __('Stats could not be cleared', 'wp-optimize'));
362 }
363
364 $response = array();
365 $response['status'] = true;
366 $response['summary'] = __('Stats cleared successfully', 'wp-optimize');
367
368 return $response;
369 }
370
371 /**
372 * Checks if the selected server is online
373 *
374 * @param mixed $data - Sent in via AJAX
375 */
376 public function check_server_status($data) {
377 $server = sanitize_text_field($data['server']);
378 $response = array();
379 $response['status'] = true;
380 $response['online'] = $this->task_manager->check_server_online($server);
381
382 if (!$response['online']) {
383 $response['error'] = get_option($this->task_manager->get_associated_task($server));
384 }
385
386 return $response;
387 }
388
389 /**
390 * Completes any pending tasks
391 *
392 * @return array
393 */
394 public function process_pending_images() {
395 return $this->process_bulk_smush();
396 }
397
398 /**
399 * Deletes and removes any pending tasks from queue
400 *
401 * @return WP_Error|array - information about the operation or WP_Error object on failure
402 */
403 public function clear_pending_images() {
404
405 $success = $this->task_manager->clear_pending_images();
406
407 if (!$success) {
408 return new WP_Error('error_deleting_tasks', __('Pending tasks could not be cleared', 'wp-optimize'));
409 }
410
411 $response = array();
412 $response['status'] = true;
413 $response['summary'] = __('Pending tasks cleared successfully', 'wp-optimize');
414
415 return $response;
416 }
417
418 /**
419 * Mark selected images as already compressed.
420 *
421 * @param array $data
422 * @return array
423 */
424 public function mark_as_compressed($data) {
425 $response = array();
426 $selected_images = array();
427
428 $unmark = isset($data['unmark']) && $data['unmark'];
429
430 foreach ($data['selected_images'] as $image) {
431 if (!array_key_exists($image['blog_id'], $selected_images)) $selected_images[$image['blog_id']] = array();
432
433 $selected_images[$image['blog_id']][] = $image['attachment_id'];
434 }
435
436 $info = __('This image is marked as already compressed by another tool.', 'wp-optimize');
437
438 foreach (array_keys($selected_images) as $blog_id) {
439 if (is_multisite()) switch_to_blog($blog_id);
440
441 foreach ($selected_images[$blog_id] as $attachment_id) {
442 if ($unmark) {
443 delete_post_meta($attachment_id, 'smush-complete');
444 delete_post_meta($attachment_id, 'smush-marked');
445 delete_post_meta($attachment_id, 'smush-info');
446 } else {
447 update_post_meta($attachment_id, 'smush-complete', true);
448 update_post_meta($attachment_id, 'smush-marked', true);
449 update_post_meta($attachment_id, 'smush-info', $info);
450 }
451 }
452
453 if (is_multisite()) restore_current_blog();
454 }
455
456 $response['status'] = true;
457
458 if ($unmark) {
459 $response['summary'] = _n('The selected image was successfully marked as uncompressed', 'The selected images were successfully marked as uncompressed', count($data['selected_images']), 'wp-optimize');
460 } else {
461 $response['summary'] = _n('The selected image was successfully marked as compressed', 'The selected images were successfully marked as compressed', count($data['selected_images']), 'wp-optimize');
462 }
463
464 $response['info'] = $info;
465
466 return $response;
467 }
468
469 /**
470 * Mark all images as uncompressed and if posted restore_backup argument
471 * then try to restore images form backup.
472 *
473 * @param array $data
474 * @return array
475 */
476 public function mark_all_as_uncompressed($data) {
477
478 $restore_backup = isset($data['restore_backup']) && $data['restore_backup'];
479 $images_per_request = apply_filters('mark_all_as_uncompressed_images_per_request', 100);
480 $delete_only_backups_meta = isset($data['delete_only_backups_meta']) && $data['delete_only_backups_meta'];
481
482 if (is_multisite()) {
483 // option where we store last completed blog id
484 $option_name = 'mark_as_uncompressed_last_blog_id';
485 $smushed_images_total_option_name = 'smushed_images_total';
486 // set default value for response
487 $response = array(
488 'completed' => true,
489 'message' => __('All the compressed images were successfully restored.', 'wp-optimize'),
490 );
491
492 // get all blogs ids
493 $blogs = WP_Optimize()->get_sites();
494 $blogs_ids = wp_list_pluck($blogs, 'blog_id');
495 sort($blogs_ids);
496
497 // select the blog for processing
498 $last_completed_blog_id = $this->task_manager->options->get_option($option_name, false);
499 $index = $last_completed_blog_id ? array_search($last_completed_blog_id, $blogs_ids) + 1 : 0;
500
501 if ($index < count($blogs_ids)) {
502 $blog_id = $blogs_ids[$index];
503 $response = $this->task_manager->bulk_restore_compressed_images($restore_backup, $blog_id, $images_per_request, $delete_only_backups_meta);
504 $smushed_images_total = $this->task_manager->options->get_option($smushed_images_total_option_name, 0) + $response['smushed_images_count'];
505 // if we get completed the current blog then update last completed blog option value
506 // and if we have other blogs for processing then set complete to false as we have not
507 // processed all blogs
508 if ($response['completed']) {
509 if ($index + 1 < count($blogs_ids)) {
510 $response['completed'] = false;
511 $this->task_manager->options->update_option($option_name, $blog_id);
512 $this->task_manager->options->update_option($smushed_images_total_option_name, $smushed_images_total);
513 } else {
514 if ($delete_only_backups_meta) {
515 if ($smushed_images_total > 0) {
516 $response['message'] = __('All the compressed images with backup copies of their original files were successfully restored.', 'wp-optimize');
517 $response['message'] .= ' '.sprintf(_n('Unable to restore %s image without backup files.', 'Unable to restore %s images without backup files.', $smushed_images_total, 'wp-optimize'), $smushed_images_total);
518 } else {
519 $response['message'] = __('All the compressed images were successfully restored.', 'wp-optimize');
520 }
521 } else {
522 $response['message'] = __('All the compressed images were successfully marked as uncompressed.', 'wp-optimize');
523 }
524 }
525 }
526 }
527
528 // if we get an error or completed the work then delete option with last completed blog id.
529 if ($response['completed'] || isset($response['error'])) {
530 $this->task_manager->options->delete_option($option_name);
531 $this->task_manager->options->delete_option($smushed_images_total_option_name);
532 }
533 } else {
534 $response = $this->task_manager->bulk_restore_compressed_images($restore_backup, 0, $images_per_request, $delete_only_backups_meta);
535 }
536
537 return $response;
538 }
539
540 /**
541 * Returns the log file
542 *
543 * @return WP_Error|file - logfile or WP_Error object on failure
544 */
545 public function get_smush_logs() {
546
547 $logfile = $this->task_manager->get_logfile_path();
548
549 if (!file_exists($logfile)) {
550 $this->task_manager->write_log_header();
551 }
552
553 if (is_file($logfile)) {
554 if ($this->heartbeat_command) {
555 // The response will be inside the heartbeat response envelope, as each response of a heartbeat goes in its own unique ID key
556 readfile($logfile);
557 } else {
558 // Headers are needed for the `Download logs` link, which will run this command and just prompt a file download
559 header('Content-Description: File Transfer');
560 header('Content-Type: application/octet-stream');
561 header('Content-Disposition: attachment; filename="'.basename($logfile).'"');
562 header('Expires: 0');
563 header('Cache-Control: must-revalidate');
564 header('Pragma: public');
565 header('Content-Length: ' . filesize($logfile));
566 readfile($logfile);
567 exit;
568 }
569 } else {
570 return new WP_Error('log_file_error', __('Log file does not exist or could not be read', 'wp-optimize'));
571 }
572 }
573
574 /**
575 * Clean all backup images command.
576 *
577 * @return array
578 */
579 public function clean_all_backup_images() {
580 $upload_dir = wp_upload_dir(null, false);
581 $base_dir = $upload_dir['basedir'];
582
583 $this->task_manager->clear_backup_images_directory($base_dir, 0);
584
585 return array(
586 'status' => true,
587 );
588 }
589
590 /**
591 * Resets webp serving method
592 *
593 * @return array|WP_Error
594 */
595 public function reset_webp_serving_method() {
596 $webp_instance = WP_Optimize()->get_webp_instance();
597 //Run checks before calling reset_webp_serving_method
598 if (!WP_Optimize_WebP::is_shell_functions_available()) {
599 $webp_instance->disable_webp_conversion();
600 return new WP_Error('reset_failed_no_shell_functions', __('Required WebP shell functions are not available on server', 'wp-optimize'));
601 } elseif (!$webp_instance->is_webp_conversion_enabled()) {
602 $webp_instance->disable_webp_conversion();
603 return new WP_Error('reset_failed_webp_conversion_disabled', __('The WebP serving method cannot be reset because WebP conversion is currently disabled', 'wp-optimize'));
604 }
605
606 $webp_instance->reset_webp_serving_method();
607 return array(
608 'success' => true,
609 );
610 }
611
612 /**
613 * Convert the image to webp format
614 *
615 * @param array $data
616 * @return array
617 */
618 public function convert_to_webp_format($data) {
619 $attachment_id = isset($data['attachment_id']) ? $data['attachment_id'] : 0;
620 if (0 === $attachment_id) return $this->image_not_found_response();
621
622 $images = WPO_Image_Utils::get_attachment_files($attachment_id);
623 if (empty($images)) return $this->image_not_found_response();
624
625 $images['original'] = get_attached_file($attachment_id);
626 foreach ($images as $image) {
627 WPO_WebP_Utils::do_webp_conversion($image);
628 }
629
630 return array(
631 'success' => __('Image is converted to WebP format.', 'wp-optimize'),
632 );
633 }
634
635 /**
636 * Returns image not found response
637 *
638 * @return array
639 */
640 private function image_not_found_response() {
641 return array(
642 'error' => __('Image not found', 'wp-optimize'),
643 );
644 }
645
646 /**
647 * Get smush details of given image IDs
648 *
649 * @param array $data
650 *
651 * @return array
652 */
653 public function get_smush_details($data) {
654 $selected_images = isset($data['selected_images']) ? $data['selected_images'] : array();
655 $smush_details = array();
656 foreach ($selected_images as $attachment_id) {
657 $smush_details[$attachment_id] = $this->task_manager->get_smush_details($attachment_id);
658 }
659
660 return array(
661 'success' => true,
662 'smush_details' => $smush_details,
663 );
664 }
665 }
666
667 endif;
668