PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
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
← All changes | includes/class-updraft-smush-manager-commands.php +178 -113 3.4.1 → 4.7.0 View file →
@@ -49,8 +49,10 @@
49 49 }
50 50
51 51 /**
52 52 * Returns a list of commands available for smush related operations
53 + *
54 + * @return array
53 55 */
54 56 public static function get_allowed_ajax_commands() {
55 57
56 58 $commands = parent::get_allowed_ajax_commands();
@@ -72,8 +74,9 @@
72 74 'reset_webp_serving_method',
73 75 'convert_to_webp_format',
74 76 'update_webp_options',
75 77 'get_smush_details',
78 + 'get_smush_settings_form',
76 79 );
77 80
78 81 return array_merge($commands, $smush_commands);
79 82 }
@@ -80,28 +83,36 @@
80 83
81 84 /**
82 85 * Process the compression of a single image
83 86 *
84 - * @param mixed $data - sent in via AJAX
87 + * @param array $data - sent in via AJAX
85 88 * @return WP_Error|array - information about the operation or WP_Error object on failure
86 89 */
87 90 public function compress_single_image($data) {
88 91
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 + $options = empty($data['smush_options']) ? $this->task_manager->get_smush_options() : $data['smush_options'];
93 + $image = isset($data['selected_image']['attachment_id']) ? absint($data['selected_image']['attachment_id']) : 0;
94 + $blog = isset($data['selected_image']['blog_id']) ? absint($data['selected_image']['blog_id']) : 0;
92 95
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')) {
96 + if (0 === $image) {
97 + return new WP_Error('invalid_image', __('Image ID is invalid', 'wp-optimize'));
98 + }
99 +
100 + if (0 === $blog && is_multisite()) {
101 + return new WP_Error('invalid_blog', __('Blog ID is invalid', 'wp-optimize'));
102 + }
103 +
104 + // A sub site administrator can only compress their own image. If the blog ID isn't theirs, return an error.
105 + if ($blog && is_multisite() && get_current_blog_id() !== $blog && !WP_Optimize()->current_user_can('manage_network_options')) {
95 106 return new WP_Error('compression_not_permitted', __('The blog ID provided does not match the current blog.', 'wp-optimize'));
96 107 }
97 108
98 - $server = sanitize_text_field($options['compression_server']);
109 + $server = isset($options['compression_server']) ? sanitize_text_field($options['compression_server']) : $this->task_manager->get_default_webservice();
99 110
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);
111 + $lossy = isset($options['lossy_compression']) ? filter_var($options['lossy_compression'], FILTER_VALIDATE_BOOLEAN) : false;
112 + $backup = isset($options['back_up_original']) ? filter_var($options['back_up_original'], FILTER_VALIDATE_BOOLEAN) : true;
113 + $exif = isset($options['preserve_exif']) ? filter_var($options['preserve_exif'], FILTER_VALIDATE_BOOLEAN) : false;
114 + $quality = isset($options['image_quality']) ? absint($options['image_quality']) : 92;
104 115
105 116 $options = array(
106 117 'attachment_id' => $image,
107 118 'blog_id' => $blog,
@@ -117,9 +128,9 @@
117 128
118 129 $success = $this->task_manager->compress_single_image($image, $options, $server);
119 130
120 131 if (!$success) {
121 - return new WP_Error('compress_failed', get_post_meta($image, 'smush-info', true));
132 + return new WP_Error('compress_failed', get_post_meta($image, '_wpo-smush-info', true));
122 133 }
123 134
124 135 $response = array();
125 136 $response['status'] = true;
@@ -127,15 +138,17 @@
127 138 $response['options'] = $options;
128 139 $response['server'] = $server;
129 140 $response['success'] = $success;
130 141 $response['restore_possible'] = $backup;
131 - $response['summary'] = get_post_meta($image, 'smush-info', true);
142 + $response['summary'] = get_post_meta($image, '_wpo-smush-info', true);
132 143
133 - $smush_stats = get_post_meta($image, 'smush-stats', true);
144 + $smush_stats = get_post_meta($image, '_wpo-smush-stats', true);
134 145 if (isset($smush_stats['sizes-info'])) {
135 146 $response['sizes-info'] = WP_Optimize()->include_template('images/smush-details.php', true, array('sizes_info' => $smush_stats['sizes-info']));
136 147 }
137 148
149 + $response['media_column_html'] = $this->get_smush_media_column_content($blog, $image);
150 +
138 151 return $response;
139 152 }
140 153
141 154 /**
@@ -140,15 +153,15 @@
140 153
141 154 /**
142 155 * Restores a single image, if backup is available
143 156 *
144 - * @param mixed $data - Sent in via AJAX
157 + * @param array $data - Sent in via AJAX
145 158 * @return WP_Error|array - information about the operation or a WP_Error object on failure
146 159 */
147 160 public function restore_single_image($data) {
148 161
149 - $blog_id = isset($data['blog_id']) ? $data['blog_id'] : false;
150 - $image_id = isset($data['selected_image']) ? $data['selected_image'] : false;
162 + $blog_id = isset($data['blog_id']) ? absint($data['blog_id']) : 0;
163 + $image_id = isset($data['selected_image']) ? absint($data['selected_image']) : 0;
151 164
152 165 $success = $this->task_manager->restore_single_image($image_id, $blog_id);
153 166
154 167 if (is_wp_error($success)) {
@@ -161,8 +174,10 @@
161 174 $response['blog_id'] = $blog_id;
162 175 $response['image'] = $image_id;
163 176 $response['success'] = $success;
164 177 $response['summary'] = __('The image was restored successfully', 'wp-optimize');
178 +
179 + $response['media_column_html'] = $this->get_smush_media_column_content($blog_id, $image_id);
165 180
166 181 return $response;
167 182 }
168 183
@@ -168,13 +183,13 @@
168 183
169 184 /**
170 185 * Process the compression of multiple images
171 186 *
172 - * @param mixed $data - Sent in via AJAX
187 + * @param array $data - Sent in via AJAX
173 188 * @return array
174 189 */
175 190 public function process_bulk_smush($data = array()) {
176 - $images = isset($data['selected_images']) ? $data['selected_images'] : array();
191 + $images = isset($data['selected_images']) && is_array($data['selected_images']) ? $this->sanitize_images($data['selected_images']) : array();
177 192
178 193 $this->images = $images;
179 194
180 195 $this->background_command = true;
@@ -191,9 +206,9 @@
191 206 *
192 207 * @return void
193 208 */
194 209 public function process_bulk_smush_shutdown() {
195 - WP_Optimize()->close_browser_connection(json_encode($this->final_response));
210 + WP_Optimize()->close_browser_connection(wp_json_encode($this->final_response));
196 211
197 212 $this->task_manager->process_bulk_smush($this->images);
198 213 exit;
199 214 }
@@ -200,13 +215,16 @@
200 215
201 216 /**
202 217 * Returns useful information for the UI and closes the connection
203 218 *
204 - * @param mixed $data - Sent in via AJAX
219 + * @param array $data - Sent in via AJAX
205 220 *
206 221 * @return array - Information for the UI
207 222 */
208 223 public function get_ui_update($data) {
224 + $use_cache = isset($data['use_cache']) ? sanitize_text_field($data['use_cache']) : 'true';
225 + $image_list = isset($data['image_list']) && is_array($data['image_list']) ? $this->sanitize_images($data['image_list']) : false;
226 +
209 227 $ui_update = array();
210 228 $ui_update['status'] = true;
211 229 $ui_update['is_multisite'] = is_multisite() ? 1 : 0;
212 230 $pending_tasks = $this->task_manager->get_pending_tasks();
@@ -211,9 +229,9 @@
211 229 $ui_update['is_multisite'] = is_multisite() ? 1 : 0;
212 230 $pending_tasks = $this->task_manager->get_pending_tasks();
213 231
214 232 $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");
233 + $ui_update['unsmushed_images'] = $this->task_manager->get_uncompressed_images($use_cache);
216 234 $ui_update['admin_urls'] = $this->task_manager->get_admin_urls();
217 235 $ui_update['completed_task_count'] = $this->task_manager->options->get_option('completed_task_count', 0);
218 236 $ui_update['bytes_saved'] = WP_Optimize()->format_size($this->task_manager->options->get_option('total_bytes_saved', 0));
219 237 $ui_update['percent_saved'] = number_format($this->task_manager->options->get_option('total_percent_saved', 1), 2).'%';
@@ -219,26 +237,31 @@
219 237 $ui_update['percent_saved'] = number_format($this->task_manager->options->get_option('total_percent_saved', 1), 2).'%';
220 238 $ui_update['failed_task_count'] = $this->task_manager->get_failed_task_count();
221 239
222 240 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']);
241 + // translators: %d: number of images compressed, %2$s: size of saved space, %3$02d: average percent saved
242 + $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 %2$s of space at an average of %3$02d percent per image.', 'wp-optimize'), $ui_update['completed_task_count'], $ui_update['bytes_saved'], $ui_update['percent_saved']);
224 243 } 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']);
244 + // translators: %d: number of images compressed, %2$s: size of saved space, %3$02d: average percent saved
245 + $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 %2$s of space at an average of %3$02d percent per image.', 'wp-optimize'), $ui_update['completed_task_count'], $ui_update['bytes_saved'], $ui_update['percent_saved']);
226 246 }
247 + // translators: %d: number of images that could not be compressed
227 248 $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');
249 + // translators: %d: number of images that were selected for compression, and pending processing
228 250 $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 251 $ui_update['smush_complete'] = $this->task_manager->is_queue_processed();
230 252
231 - if (isset($data['image_list'])) {
232 - $images = $data['image_list'];
233 - $stats = $this->task_manager->get_session_stats($images);
253 + if ($image_list) {
254 + $stats = $this->task_manager->get_session_stats($image_list);
234 255 $ui_update['session_stats'] = "";
235 256
236 257 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']);
258 + // translators: %d: number of images compressed
259 + $ui_update['session_stats'] .= sprintf(__("A total of %d image(s) were successfully compressed in this iteration.", 'wp-optimize'), $stats['success']);
238 260 }
239 261
240 262 if (!empty($stats['fail'])) {
263 + // translators: %d: number of images that could not be compressed
241 264 $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 265 }
243 266 }
244 267
@@ -248,69 +271,22 @@
248 271
249 272 /**
250 273 * Updates webp related options
251 274 *
252 - * @param mixed $data - Sent in via AJAX
275 + * @param array $data - Sent in via AJAX
253 276 * @return WP_Error|array - information about the operation or WP_Error object on failure
254 277 */
255 278 public function update_webp_options($data) {
256 279 $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 - }
280 + $result = $webp_instance->save_webp_settings($data);
281 + if (is_wp_error($result)) {
282 + return $result;
290 283 }
291 284
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 285 $response = array();
310 286 $response['status'] = true;
311 - $response['saved'] = $success;
312 - $response['summary'] = __('Webp options updated successfully.', 'wp-optimize');
287 + $response['saved'] = $result;
288 + $response['summary'] = __('WebP options updated successfully.', 'wp-optimize');
313 289
314 290 return $response;
315 291 }
316 292
@@ -316,22 +292,22 @@
316 292
317 293 /**
318 294 * Updates smush related options
319 295 *
320 - * @param mixed $data - Sent in via AJAX
296 + * @param array $data - Sent in via AJAX
321 297 * @return WP_Error|array - information about the operation or WP_Error object on failure
322 298 */
323 299 public function update_smush_options($data) {
324 300 $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';
301 + $options['compression_server'] = isset($data['compression_server']) ? sanitize_text_field($data['compression_server']) : $this->task_manager->get_default_webservice();
302 + $options['lossy_compression'] = isset($data['lossy_compression']) ? filter_var($data['lossy_compression'], FILTER_VALIDATE_BOOLEAN) : false;
303 + $options['back_up_original'] = isset($data['back_up_original']) ? filter_var($data['back_up_original'], FILTER_VALIDATE_BOOLEAN) : true;
304 + $options['back_up_delete_after'] = isset($data['back_up_delete_after']) ? filter_var($data['back_up_delete_after'], FILTER_VALIDATE_BOOLEAN) : true;
305 + $options['back_up_delete_after_days'] = isset($data['back_up_delete_after_days']) ? absint($data['back_up_delete_after_days']) : 50;
306 + $options['preserve_exif'] = isset($data['preserve_exif']) ? filter_var($data['preserve_exif'], FILTER_VALIDATE_BOOLEAN) : false;
307 + $options['autosmush'] = isset($data['autosmush']) ? filter_var($data['autosmush'], FILTER_VALIDATE_BOOLEAN) : false;
308 + $options['image_quality'] = isset($data['image_quality']) ? absint($data['image_quality']) : 92;
309 + $options['show_smush_metabox'] = isset($data['show_smush_metabox']) && filter_var($data['show_smush_metabox'], FILTER_VALIDATE_BOOLEAN) ? 'show' : 'hide';
334 310
335 311 $success = $this->task_manager->update_smush_options($options);
336 312
337 313 if (!$success) {
@@ -370,12 +346,14 @@
370 346
371 347 /**
372 348 * Checks if the selected server is online
373 349 *
374 - * @param mixed $data - Sent in via AJAX
350 + * @param array $data - Sent in via AJAX
351 + *
352 + * @return array
375 353 */
376 354 public function check_server_status($data) {
377 - $server = sanitize_text_field($data['server']);
355 + $server = isset($data['server']) ? sanitize_text_field($data['server']) : $this->task_manager->get_default_webservice();
378 356 $response = array();
379 357 $response['status'] = true;
380 358 $response['online'] = $this->task_manager->check_server_online($server);
381 359
@@ -397,12 +375,20 @@
397 375
398 376 /**
399 377 * Deletes and removes any pending tasks from queue
400 378 *
379 + * @param array $data - in 'restore_images' index passed an array with ids of images to restore
401 380 * @return WP_Error|array - information about the operation or WP_Error object on failure
402 381 */
403 - public function clear_pending_images() {
382 + public function clear_pending_images($data) {
404 383
384 + if (!empty($data['restore_images']) && is_array($data['restore_images'])) {
385 + $restore_images = $this->sanitize_images($data['restore_images']);
386 + foreach ($restore_images as $image) {
387 + $this->task_manager->restore_single_image($image['attachment_id'], $image['blog_id']);
388 + }
389 + }
390 +
405 391 $success = $this->task_manager->clear_pending_images();
406 392
407 393 if (!$success) {
408 394 return new WP_Error('error_deleting_tasks', __('Pending tasks could not be cleared', 'wp-optimize'));
@@ -425,10 +411,11 @@
425 411 $response = array();
426 412 $selected_images = array();
427 413
428 414 $unmark = isset($data['unmark']) && $data['unmark'];
415 + $image_list = isset($data['selected_images']) && is_array($data['selected_images']) ? $this->sanitize_images($data['selected_images']) : array();
429 416
430 - foreach ($data['selected_images'] as $image) {
417 + foreach ($image_list as $image) {
431 418 if (!array_key_exists($image['blog_id'], $selected_images)) $selected_images[$image['blog_id']] = array();
432 419
433 420 $selected_images[$image['blog_id']][] = $image['attachment_id'];
434 421 }
@@ -439,15 +426,15 @@
439 426 if (is_multisite()) switch_to_blog($blog_id);
440 427
441 428 foreach ($selected_images[$blog_id] as $attachment_id) {
442 429 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');
430 + delete_post_meta($attachment_id, '_wpo-smush-complete');
431 + delete_post_meta($attachment_id, '_wpo-smush-marked');
432 + delete_post_meta($attachment_id, '_wpo-smush-info');
446 433 } 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);
434 + update_post_meta($attachment_id, '_wpo-smush-complete', true);
435 + update_post_meta($attachment_id, '_wpo-smush-marked', true);
436 + update_post_meta($attachment_id, '_wpo-smush-info', $info);
450 437 }
451 438 }
452 439
453 440 if (is_multisite()) restore_current_blog();
@@ -455,15 +442,20 @@
455 442
456 443 $response['status'] = true;
457 444
458 445 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');
446 + $response['summary'] = _n('The selected image was successfully marked as uncompressed', 'The selected images were successfully marked as uncompressed', count($image_list), 'wp-optimize');
460 447 } 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');
448 + $response['summary'] = _n('The selected image was successfully marked as compressed', 'The selected images were successfully marked as compressed', count($image_list), 'wp-optimize');
462 449 }
463 450
464 451 $response['info'] = $info;
465 452
453 + if (1 === count($image_list)) {
454 + $selected_image = reset($image_list);
455 + $response['media_column_html'] = $this->get_smush_media_column_content($selected_image['blog_id'], $selected_image['attachment_id']);
456 + }
457 +
466 458 return $response;
467 459 }
468 460
469 461 /**
@@ -513,8 +505,9 @@
513 505 } else {
514 506 if ($delete_only_backups_meta) {
515 507 if ($smushed_images_total > 0) {
516 508 $response['message'] = __('All the compressed images with backup copies of their original files were successfully restored.', 'wp-optimize');
509 + // translators: %s - number of smushed images
517 510 $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 511 } else {
519 512 $response['message'] = __('All the compressed images were successfully restored.', 'wp-optimize');
520 513 }
@@ -539,9 +532,9 @@
539 532
540 533 /**
541 534 * Returns the log file
542 535 *
543 - * @return WP_Error|file - logfile or WP_Error object on failure
536 + * @return void|WP_Error - logfile or WP_Error object on failure
544 537 */
545 538 public function get_smush_logs() {
546 539
547 540 $logfile = $this->task_manager->get_logfile_path();
@@ -552,9 +545,9 @@
552 545
553 546 if (is_file($logfile)) {
554 547 if ($this->heartbeat_command) {
555 548 // 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);
549 + readfile($logfile); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Using WP_Filesystem and get contents will result in `echo` and unescaped error
557 550 } else {
558 551 // Headers are needed for the `Download logs` link, which will run this command and just prompt a file download
559 552 header('Content-Description: File Transfer');
560 553 header('Content-Type: application/octet-stream');
@@ -562,9 +555,9 @@
562 555 header('Expires: 0');
563 556 header('Cache-Control: must-revalidate');
564 557 header('Pragma: public');
565 558 header('Content-Length: ' . filesize($logfile));
566 - readfile($logfile);
559 + readfile($logfile); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Using WP_Filesystem and get contents will result in `echo` and unescaped error
567 560 exit;
568 561 }
569 562 } else {
570 563 return new WP_Error('log_file_error', __('Log file does not exist or could not be read', 'wp-optimize'));
@@ -594,13 +587,11 @@
594 587 */
595 588 public function reset_webp_serving_method() {
596 589 $webp_instance = WP_Optimize()->get_webp_instance();
597 590 //Run checks before calling reset_webp_serving_method
598 - if (!WP_Optimize_WebP::is_shell_functions_available()) {
591 + if (!$webp_instance->is_webp_conversion_enabled()) {
599 592 $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();
593 + $webp_instance->log("The WebP serving method cannot be reset because WebP conversion is currently disabled");
603 594 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 595 }
605 596
606 597 $webp_instance->reset_webp_serving_method();
@@ -615,9 +606,9 @@
615 606 * @param array $data
616 607 * @return array
617 608 */
618 609 public function convert_to_webp_format($data) {
619 - $attachment_id = isset($data['attachment_id']) ? $data['attachment_id'] : 0;
610 + $attachment_id = isset($data['attachment_id']) ? absint($data['attachment_id']) : 0;
620 611 if (0 === $attachment_id) return $this->image_not_found_response();
621 612
622 613 $images = WPO_Image_Utils::get_attachment_files($attachment_id);
623 614 if (empty($images)) return $this->image_not_found_response();
@@ -623,9 +614,11 @@
623 614 if (empty($images)) return $this->image_not_found_response();
624 615
625 616 $images['original'] = get_attached_file($attachment_id);
626 617 foreach ($images as $image) {
627 - WPO_WebP_Utils::do_webp_conversion($image);
618 + if (!is_file($image.'.webp')) {
619 + WPO_WebP_Utils::do_webp_conversion($image);
620 + }
628 621 }
629 622
630 623 return array(
631 624 'success' => __('Image is converted to WebP format.', 'wp-optimize'),
@@ -632,8 +625,53 @@
632 625 );
633 626 }
634 627
635 628 /**
629 + * Get Smush settings form
630 + *
631 + * @param array $data
632 + * @return array
633 + */
634 + public function get_smush_settings_form($data) {
635 + $attachment_id = isset($data['attachment_id']) ? absint($data['attachment_id']) : 0;
636 + if (0 === $attachment_id) return $this->image_not_found_response();
637 +
638 + $compressed = (bool) get_post_meta($attachment_id, '_wpo-smush-complete', true);
639 +
640 + $smush_options = Updraft_Smush_Manager()->get_smush_options();
641 +
642 + $extract = array(
643 + 'post_id' => $attachment_id,
644 + 'smush_options' => $smush_options,
645 + 'custom' => 90 >= $smush_options['image_quality'] && 65 <= $smush_options['image_quality'],
646 + 'smush_display' => $compressed ? "display:none;" : "display:block;",
647 + );
648 +
649 + return array(
650 + 'success' => true,
651 + 'html' => WP_Optimize()->include_template('admin-metabox-smush-settings.php', true, $extract),
652 + );
653 + }
654 +
655 + /**
656 + * Get content for Media Library column content
657 + *
658 + * @param int $blog_id
659 + * @param int $attachment_id
660 + *
661 + * @return string
662 + */
663 + private function get_smush_media_column_content($blog_id, $attachment_id) {
664 + if (is_multisite()) switch_to_blog($blog_id);
665 +
666 + $content = Updraft_Smush_Manager()->get_smush_details($attachment_id);
667 +
668 + if (is_multisite()) restore_current_blog();
669 +
670 + return $content;
671 + }
672 +
673 + /**
636 674 * Returns image not found response
637 675 *
638 676 * @return array
639 677 */
@@ -650,9 +688,9 @@
650 688 *
651 689 * @return array
652 690 */
653 691 public function get_smush_details($data) {
654 - $selected_images = isset($data['selected_images']) ? $data['selected_images'] : array();
692 + $selected_images = isset( $data['selected_images'] ) && is_array( $data['selected_images'] ) ? array_map( 'absint', $data['selected_images'] ) : array();
655 693 $smush_details = array();
656 694 foreach ($selected_images as $attachment_id) {
657 695 $smush_details[$attachment_id] = $this->task_manager->get_smush_details($attachment_id);
658 696 }
@@ -660,8 +698,35 @@
660 698 return array(
661 699 'success' => true,
662 700 'smush_details' => $smush_details,
663 701 );
702 + }
703 +
704 + /**
705 + * Sanitize array of images ensuring proper integer values for attachment_id and blog_id
706 + *
707 + * @param array $images Array of image data to sanitize
708 + * @return array
709 + */
710 + private function sanitize_images($images) {
711 + $result = array();
712 +
713 + foreach ($images as $image) {
714 + $attachment_id = isset($image['attachment_id']) ? absint($image['attachment_id']) : 0;
715 + $blog_id = isset($image['blog_id']) ? absint($image['blog_id']) : 0;
716 +
717 + // Skip entries where either value is zero
718 + if (0 === $attachment_id || 0 === $blog_id) {
719 + continue;
720 + }
721 +
722 + $result[] = array(
723 + 'attachment_id' => $attachment_id,
724 + 'blog_id' => $blog_id,
725 + );
726 + }
727 +
728 + return $result;
664 729 }
665 730 }
666 731
667 732 endif;