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-task.php +62 -55 3.2.20 → 4.7.0 View file →
@@ -27,9 +27,9 @@
27 27
28 28 /**
29 29 * Initialise the task
30 30 *
31 - * @param Array $options - options to use
31 + * @param array $options - options to use
32 32 */
33 33 public function initialise($options = array()) {
34 34 parent::initialise($options);
35 35 $this->set_current_stage('initialised');
@@ -61,9 +61,9 @@
61 61
62 62 $api_endpoint = $this->get_option('api_endpoint');
63 63
64 64 if (false === filter_var($api_endpoint, FILTER_VALIDATE_URL)) {
65 - $this->fail('invalid_api_url', "The API endpoint supplied {$api_endpoint} is invalid");
65 + $this->fail('invalid_api_url', "The API endpoint supplied $api_endpoint is invalid");
66 66 return false;
67 67 }
68 68
69 69 $original_image = $file_path;
@@ -74,9 +74,15 @@
74 74
75 75 $this->update_option('original_filesize', filesize($file_path));
76 76
77 77 // build list of files for smush.
78 - $files = array_merge(array('full' => $file_path), WPO_Image_Utils::get_attachment_files($attachment_id));
78 + if (is_multisite()) {
79 + switch_to_blog($this->get_option('blog_id', 1));
80 + $files = array_merge(array('full' => $file_path), WPO_Image_Utils::get_attachment_files($attachment_id));
81 + restore_current_blog();
82 + } else {
83 + $files = array_merge(array('full' => $file_path), WPO_Image_Utils::get_attachment_files($attachment_id));
84 + }
79 85
80 86 $sizes_info = array();
81 87 $webp_tools_available = true;
82 88 $lossy = $this->get_option('lossy_compression');
@@ -85,9 +91,9 @@
85 91 } else {
86 92 $quality = 92;
87 93 }
88 94 $this->log($this->get_description());
89 - $this->log("File: " . basename($file_path) . ", Compression quality: {$quality}");
95 + $this->log("File: " . basename($file_path) . ", Compression quality: $quality");
90 96
91 97 foreach ($files as $size => $file_path) {
92 98
93 99 if (in_array($size, $dont_smush_sizes)) continue;
@@ -111,9 +117,9 @@
111 117
112 118
113 119 /**
114 120 * Filters the options for a single image to compress.
115 - * Currently supports:
121 + * Currently, supports:
116 122 * - 'quality': Will use the image quality set in this filter, instead of the one defined in the settings.
117 123 *
118 124 * @param array $options - The options (default: empty array)
119 125 * @param integer $attachment_id - The attachment post ID
@@ -127,9 +133,9 @@
127 133 $response = $this->post_to_remote_server($api_endpoint, $post_data);
128 134 $optimised_image = $this->process_server_response($response);
129 135
130 136 if ($optimised_image) {
131 - $backup_image = ($original_image == $file_path) ? $backup_original_image : false;
137 + $backup_image = ($original_image === $file_path) ? $backup_original_image : false;
132 138 $this->save_optimised_image($file_path, $optimised_image, $backup_image);
133 139
134 140 clearstatcache($file_path);
135 141
@@ -146,8 +152,19 @@
146 152 if (!$webp_tools_available) {
147 153 $this->log('There were no WebP conversion tools found on your server.');
148 154 }
149 155
156 + $destination = WPO_WebP_Utils::get_destination_path($file_path);
157 + if ($webp_tools_available && file_exists($destination)) {
158 + if (is_multisite()) {
159 + switch_to_blog($this->get_option('blog_id', 1));
160 + update_post_meta($attachment_id, WPO_Webp_Convert_Image_Task::WEBP_CONVERSION_META_KEY, true);
161 + restore_current_blog();
162 + } else {
163 + update_post_meta($attachment_id, WPO_Webp_Convert_Image_Task::WEBP_CONVERSION_META_KEY, true);
164 + }
165 + }
166 +
150 167 return $this->success;
151 168 }
152 169
153 170 /**
@@ -152,11 +169,11 @@
152 169
153 170 /**
154 171 * Posts the supplied data to the API url and returns a response
155 172 *
156 - * @param String $api_endpoint - the url to post the form to
157 - * @param String $post_data - the post data as specified by the server
158 - * @return mixed - the response
173 + * @param string $api_endpoint - the url to post the form to
174 + * @param array $post_data - the post data as specified by the server
175 + * @return array|false|WP_Error - the response
159 176 */
160 177 public function post_to_remote_server($api_endpoint, $post_data) {
161 178
162 179 $this->set_current_stage('connecting');
@@ -183,9 +200,9 @@
183 200
184 201 /**
185 202 * Checks if a file is valid and capable of being smushed
186 203 *
187 - * @param String $file_path - the path of the original image
204 + * @param string $file_path - the path of the original image
188 205 * @return bool - true on success, false otherwise
189 206 */
190 207 public function validate_file($file_path) {
191 208
@@ -211,9 +228,9 @@
211 228
212 229 /**
213 230 * Creates a backup of the original image
214 231 *
215 - * @param String $file_path - the path of the original image
232 + * @param string $file_path - the path of the original image
216 233 * @return bool - true on success, false otherwise
217 234 */
218 235 public function backup_original_image($file_path) {
219 236
@@ -229,15 +246,15 @@
229 246
230 247 // Make path relative and safe for migrations
231 248 $back_up_relative_path = preg_replace('#^'.wp_normalize_path($uploads_dir['basedir'].'/').'#', '', $back_up);
232 249
233 - update_post_meta($this->get_option('attachment_id'), 'original-file', $back_up_relative_path);
250 + update_post_meta($this->get_option('attachment_id'), '_wpo-original-file', $back_up_relative_path);
234 251
235 252 if (is_multisite()) {
236 253 restore_current_blog();
237 254 }
238 255
239 - $this->log("Backing up the original image - {$back_up_relative_path}");
256 + $this->log("Backing up the original image - $back_up_relative_path");
240 257
241 258 return copy($file_path, $back_up);
242 259 }
243 260
@@ -243,10 +260,10 @@
243 260
244 261 /**
245 262 * Creates a backup of the original image
246 263 *
247 - * @param String $file_path - the path of the original image
248 - * @param Mixes $optimised_image - the contents of the image
264 + * @param string $file_path - the path of the original image
265 + * @param mixed $optimised_image - the contents of the image
249 266 * @param bool $backup_original - backup original image
250 267 *
251 268 * @return bool - true on success, false otherwise
252 269 */
@@ -280,18 +297,20 @@
280 297 } else {
281 298 $file_path = get_attached_file($attachment_id);
282 299 }
283 300
284 - $original_size = $this->get_option('original_filesize');
301 + $original_size = (int) $this->get_option('original_filesize');
285 302 $this->set_current_stage('completed');
286 303
287 304 clearstatcache(true, $file_path);
288 - if (0 == $original_size) {
305 + if (0 === $original_size) {
289 306 $saved = '';
307 + // translators: %s is a file size
290 308 $info = sprintf(__("The file was compressed to %s using WP-Optimize", 'wp-optimize'), WP_Optimize()->format_size(filesize($file_path)));
291 309 } else {
292 310 $saved = round((($original_size - filesize($file_path)) / $original_size * 100), 2);
293 - $info = sprintf(__("The file was compressed from %s to %s, saving %s percent, using WP-Optimize", 'wp-optimize'), WP_Optimize()->format_size($original_size), WP_Optimize()->format_size(filesize($file_path)), $saved);
311 + // translators: %1$s is a file size, %2$s is a file size, %3$s is a percentage
312 + $info = sprintf(__('The file was compressed from %1$s to %2$s, saving %3$s percent, using WP-Optimize', 'wp-optimize'), WP_Optimize()->format_size($original_size), WP_Optimize()->format_size(filesize($file_path)), $saved);
294 313 }
295 314
296 315 $stats = array(
297 316 'smushed-with' => $this->label,
@@ -302,19 +321,19 @@
302 321 );
303 322
304 323 if (is_multisite()) {
305 324 switch_to_blog($this->get_option('blog_id', 1));
306 - update_post_meta($attachment_id, 'smush-complete', true);
307 - update_post_meta($attachment_id, 'smush-info', $info);
308 - update_post_meta($attachment_id, 'smush-stats', $stats);
325 + update_post_meta($attachment_id, '_wpo-smush-complete', true);
326 + update_post_meta($attachment_id, '_wpo-smush-info', $info);
327 + update_post_meta($attachment_id, '_wpo-smush-stats', $stats);
309 328 restore_current_blog();
310 329 } else {
311 - update_post_meta($attachment_id, 'smush-complete', true);
312 - update_post_meta($attachment_id, 'smush-info', $info);
313 - update_post_meta($attachment_id, 'smush-stats', $stats);
330 + update_post_meta($attachment_id, '_wpo-smush-complete', true);
331 + update_post_meta($attachment_id, '_wpo-smush-info', $info);
332 + update_post_meta($attachment_id, '_wpo-smush-stats', $stats);
314 333 }
315 334
316 - $this->log("Successfully optimized the image - {$file_path}." . $info);
335 + $this->log("Successfully optimized the image - $file_path." . $info);
317 336 $this->set_status('complete');
318 337
319 338 return parent::complete();
320 339 }
@@ -321,25 +340,26 @@
321 340
322 341 /**
323 342 * Fires if the task fails, any clean up code and logging goes here
324 343 *
325 - * @param String $error_code - A code for the failure
326 - * @param String $error_message - A description for the failure
344 + * @param string $error_code - A code for the failure
345 + * @param string $error_message - A description for the failure
327 346 */
328 347 public function fail($error_code = "Unknown", $error_message = "Unknown") {
329 348
330 349 $attachment_id = $this->get_option('attachment_id');
331 350
332 - $info = sprintf(__("Failed with error code %s - %s", 'wp-optimize'), $error_code, $error_message);
351 + // translators: %1$s is the error code, %2$s is the error message
352 + $info = sprintf(__('Failed with error code %1$s - %2$s', 'wp-optimize'), $error_code, $error_message);
333 353
334 354 if (is_multisite()) {
335 355 switch_to_blog($this->get_option('blog_id', 1));
336 - update_post_meta($attachment_id, 'smush-info', $info);
337 - update_post_meta($attachment_id, 'smush-complete', false);
356 + update_post_meta($attachment_id, '_wpo-smush-info', $info);
357 + update_post_meta($attachment_id, '_wpo-smush-complete', false);
338 358 restore_current_blog();
339 359 } else {
340 - update_post_meta($attachment_id, 'smush-info', $info);
341 - update_post_meta($attachment_id, 'smush-complete', false);
360 + update_post_meta($attachment_id, '_wpo-smush-info', $info);
361 + update_post_meta($attachment_id, '_wpo-smush-complete', false);
342 362 }
343 363
344 364
345 365 do_action('ud_smush_task_failed', $this, $error_code, $error_message);
@@ -362,29 +382,18 @@
362 382 'saving_image' => __('Saving optimized image', 'wp-optimize'),
363 383 'completed' => __('Successful', 'wp-optimize'),
364 384 );
365 385
366 - return apply_filters('allowed_task_stages', $stages);
386 + $filtered_stages = apply_filters('allowed_task_stages', $stages);
387 + if (!empty($filtered_stages) && is_array($filtered_stages)) return $filtered_stages;
388 + return $stages;
367 389 }
368 390
369 391 /**
370 - * Get features available with this service
371 - *
372 - * @return Array - an array of features
373 - */
374 - public static function get_features() {
375 - return array(
376 - 'max_filesize' => self::MAX_FILESIZE,
377 - 'lossy_compression' => true,
378 - 'preserve_exif' => true,
379 - );
380 - }
381 -
382 - /**
383 392 * Retrieve default options for this task.
384 393 * This method should normally be over-ridden by the child.
385 394 *
386 - * @return Array - an array of options
395 + * @return array - an array of options
387 396 */
388 397 public function get_default_options() {
389 398
390 399 return array(
@@ -397,9 +406,9 @@
397 406
398 407 /**
399 408 * Sets the task stage.
400 409 *
401 - * @param String $stage - the current stage of the task
410 + * @param string $stage - the current stage of the task
402 411 * @return bool - the result of the update
403 412 */
404 413 public function set_current_stage($stage) {
405 414
@@ -404,9 +413,9 @@
404 413 public function set_current_stage($stage) {
405 414
406 415 if (array_key_exists($stage, self::get_allowed_stages())) {
407 416 $this->stage = $stage;
408 - return $this->update_option('current_stage', $this->stage);
417 + return (bool) $this->update_option('current_stage', $this->stage);
409 418 }
410 419
411 420 return false;
412 421 }
@@ -413,18 +422,16 @@
413 422
414 423 /**
415 424 * Gets the task stage
416 425 *
417 - * @return String $stage - the current stage of the task
426 + * @return string $stage - the current stage of the task
418 427 */
419 428 public function get_current_stage() {
420 - if (isset($this->stage))
421 - return $this->stage;
422 - else return $this->get_option('current_stage');
429 + return $this->stage ?? $this->get_option('current_stage');
423 430 }
424 431
425 432 /**
426 - * Check the mime type of a downloaded file, returns true if it is a valid image mime type.
433 + * Check the mime type of downloaded file, returns true if it is a valid image mime type.
427 434 *
428 435 * @param string $file_buffer The buffer string downloaded from the compression service
429 436 * @return boolean
430 437 */
@@ -432,9 +439,9 @@
432 439 // If the required class does not exist, return true to avoid breaking the functionality
433 440 if (!class_exists('finfo')) return true;
434 441 $accepted_types = apply_filters('wpo_image_compression_accepted_mime_types', array('image/png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/webp'));
435 442 // The ignore rule below is added because "finfo" doesn't exist in PHP5.2.
436 - $finfo = new finfo(FILEINFO_MIME_TYPE); // phpcs:ignore PHPCompatibility.Classes.NewClasses.finfoFound, PHPCompatibility.Constants.NewConstants.fileinfo_mime_typeFound
443 + $finfo = new finfo(FILEINFO_MIME_TYPE);
437 444 $mime_type = $finfo->buffer($file_buffer);
438 445 return in_array($mime_type, $accepted_types);
439 446 }
440 447 }