1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
<?php /** * A sample implementation using the Resmush.it API and our tasks library */
if (!defined('ABSPATH')) die('Access denied.');
if (!class_exists('Updraft_Task_1_1')) require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-task.php');
if (!class_exists('Smush_Task')) :
abstract class Updraft_Smush_Task extends Updraft_Task_1_1 {
/** * A flag indicating if the operation was succesful * * @var bool */ protected $success = false;
/** * A text descriptor describing the stage of the task * * @var string */ protected $stage;
/** * Initialise the task * * @param Array $options - options to use */ public function initialise($options = array()) { parent::initialise($options); $this->set_current_stage('initialised'); do_action('ud_task_initialised', $this); }
/** * Runs the task * * @return bool - true if complete, false otherwise */ public function run() {
$this->set_status('active'); do_action('ud_task_started', $this);
$attachment_id = $this->get_option('attachment_id');
if (is_multisite()) { switch_to_blog($this->get_option('blog_id', 1)); $file_path = get_attached_file($attachment_id); restore_current_blog(); } else { $file_path = get_attached_file($attachment_id); }
if (!$this->validate_file($file_path)) return false;
$api_endpoint = $this->get_option('api_endpoint');
if (false === filter_var($api_endpoint, FILTER_VALIDATE_URL)) { $this->fail('invalid_api_url', "The API endpoint supplied {$api_endpoint} is invalid"); return false; }
$original_image = $file_path; $backup_original_image = $this->get_option('keep_original', true);
// add possibility to exclude certain image sizes from smush. $dont_smush_sizes = apply_filters('wpo_dont_smush_sizes', array());
$this->update_option('original_filesize', filesize($file_path));
// build list of files for smush. $files = array_merge(array('full' => $file_path), $this->get_attachment_files($attachment_id));
foreach ($files as $size => $file_path) {
if (in_array($size, $dont_smush_sizes)) continue;
if (filesize($file_path) > 5242880) { $this->update_option('request_timeout', 180); }
$this->log($this->get_description());
/** * Filters the options for a single image to compress. * Currently supports: * - 'quality': Will use the image quality set in this filter, instead of the one defined in the settings. * * @param array $options - The options (default: empty array) * @param integer $attachment_id - The attachment post ID * @param string $file_path - The path to the file being compressed * @param string $size - The size name (e.g. 'thumbnail') */ $options = apply_filters('wpo_image_compression_single_image_options', array(), $attachment_id, $file_path, $size);
$post_data = $this->prepare_post_request($file_path, $options);
$response = $this->post_to_remote_server($api_endpoint, $post_data); $optimised_image = $this->process_server_response($response);
if ($optimised_image) { $backup_image = ($original_image == $file_path) ? $backup_original_image : false; $this->save_optimised_image($file_path, $optimised_image, $backup_image); }
}
return $this->success; }
/** * Posts the supplied data to the API url and returns a response * * @param String $api_endpoint - the url to post the form to * @param String $post_data - the post data as specified by the server * @return mixed - the response */ public function post_to_remote_server($api_endpoint, $post_data) {
$this->set_current_stage('connecting'); $response = wp_remote_post($api_endpoint, $post_data); if (is_wp_error($response)) { $this->fail($response->get_error_code(), $response->get_error_message()); return false; }
return $response; }
/** * Processes the response recieved from the remote server * * @param mixed $response - the response object * @return mixed - the response */ public function process_server_response($response) { $this->set_current_stage('processing_response'); return $response; }
/** * Checks if a file is valid and capable of being smushed * * @param String $file_path - the path of the original image * @return bool - true on success, false otherwise */ public function validate_file($file_path) {
$allowed_file_types = $this->get_option('allowed_file_types');
if (!file_exists($file_path)) { $this->fail("invalid_file_path", "The linked attachment ID does not have a valid file path"); return false; }
if (filesize($file_path) > $this->get_option('max_filesize')) { $this->fail("exceeded_max_filesize", "$file_path - cannot be optimized, file size is above service provider limit"); return false; }
if (!in_array(pathinfo($file_path, PATHINFO_EXTENSION), $allowed_file_types)) { $this->fail("invalid_file_type", "$file_path - cannot be optimized, it has an invalid file type"); return false; }
return true; }
/** * Creates a backup of the original image * * @param String $file_path - the path of the original image * @return bool - true on success, false otherwise */ public function backup_original_image($file_path) { $this->set_current_stage('backup_original');
if (is_multisite()) { switch_to_blog($this->get_option('blog_id', 1)); }
$file = pathinfo($file_path); $back_up = wp_normalize_path($file['dirname'].'/'.basename($file['filename'].$this->get_option('backup_prefix').$file['extension'])); $uploads_dir = wp_upload_dir();
// Make path relative and safe for migrations $back_up_relative_path = preg_replace('#^'.wp_normalize_path($uploads_dir['basedir'].'/').'#', '', $back_up);
update_post_meta($this->get_option('attachment_id'), 'original-file', $back_up_relative_path);
if (is_multisite()) { restore_current_blog(); }
$this->log("Backing up the original image - {$back_up_relative_path}");
return copy($file_path, $back_up); }
/** * Creates a backup of the original image * * @param String $file_path - the path of the original image * @param Mixes $optimised_image - the contents of the image * @param bool $backup_original - backup original image * * @return bool - true on success, false otherwise */ private function save_optimised_image($file_path, $optimised_image, $backup_original) { $this->set_current_stage('saving_image');
if ($backup_original) $this->backup_original_image($file_path);
if (false !== file_put_contents($file_path, $optimised_image)) { $this->success = true; } else { $this->success = false; }
return $this->success; }
/** * Fires if the task succeds, any clean up code and logging goes here */ public function complete() {
$attachment_id = $this->get_option('attachment_id');
if (is_multisite()) { switch_to_blog($this->get_option('blog_id', 1)); $file_path = get_attached_file($attachment_id); restore_current_blog(); } else { $file_path = get_attached_file($attachment_id); }
$original_size = $this->get_option('original_filesize'); $this->set_current_stage('completed');
clearstatcache(true, $file_path); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.clearstatcache_clear_realpath_cacheFound,PHPCompatibility.FunctionUse.NewFunctionParameters.clearstatcache_filenameFound if (0 == $original_size) { $info = sprintf(__("The file was compressed to %s using WP-Optimize", 'wp-optimize'), WP_Optimize()->format_size(filesize($file_path))); } else { $saved = round((($original_size - filesize($file_path)) / $original_size * 100), 2); $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); }
$stats = array( 'smushed-with' => $this->label, 'original-size' => $original_size, 'smushed-size' => filesize($file_path), 'savings-percent' => $saved, );
if (is_multisite()) { switch_to_blog($this->get_option('blog_id', 1)); update_post_meta($attachment_id, 'smush-complete', true); update_post_meta($attachment_id, 'smush-info', $info); update_post_meta($attachment_id, 'smush-stats', $stats); restore_current_blog(); } else { update_post_meta($attachment_id, 'smush-complete', true); update_post_meta($attachment_id, 'smush-info', $info); update_post_meta($attachment_id, 'smush-stats', $stats); }
$this->log("Successfully optimized the image - {$file_path}." . $info); $this->set_status('complete');
return parent::complete(); }
/** * Fires if the task fails, any clean up code and logging goes here * * @param String $error_code - A code for the failure * @param String $error_message - A description for the failure */ public function fail($error_code = "Unknown", $error_message = "Unknown") {
$attachment_id = $this->get_option('attachment_id');
$info = sprintf(__("Failed with error code %s - %s", 'wp-optimize'), $error_code, $error_message);
if (is_multisite()) { switch_to_blog($this->get_option('blog_id', 1)); update_post_meta($attachment_id, 'smush-info', $info); update_post_meta($attachment_id, 'smush-complete', false); restore_current_blog(); } else { update_post_meta($attachment_id, 'smush-info', $info); update_post_meta($attachment_id, 'smush-complete', false); }
do_action('ud_smush_task_failed', $this, $error_code, $error_message);
return parent::fail($error_code, $error_message); } /** * Get all the supported task stages. * * @return array - list of task stages. */ public function get_allowed_stages() { $stages = array( 'initialised' => __('Initialised', 'wp-optimize'), 'connecting' => __('Connecting to API server', 'wp-optimize'), 'processing_response' => __('Processing response', 'wp-optimize'), 'backup_original' => __('Backing up original image', 'wp-optimize'), 'saving_image' => __('Saving optimized image', 'wp-optimize'), 'completed' => __('Successful', 'wp-optimize'), );
return apply_filters('allowed_task_stages', $stages); }
/** * Get features available with this service * * @return Array - an array of features */ public static function get_features() { return array( 'max_filesize' => self::MAX_FILESIZE, 'lossy_compression' => true, 'preserve_exif' => true, ); }
/** * Retrieve default options for this task. * This method should normally be over-ridden by the child. * * @return Array - an array of options */ public function get_default_options() {
return array( 'allowed_file_types' => array('gif', 'png', 'jpg', 'tif', 'jpeg'), 'request_timeout' => 15, 'image_quality' => 90, 'backup_prefix' => '-updraft-pre-smush-original.' ); }
/** * Sets the task stage. * * @param String $stage - the current stage of the task * @return bool - the result of the update */ public function set_current_stage($stage) { if (array_key_exists($stage, self::get_allowed_stages())) { $this->stage = $stage; return $this->update_option('current_stage', $this->stage); } return false; }
/** * Gets the task stage * * @return String $stage - the current stage of the task */ public function get_current_stage() { if (isset($this->stage)) return $this->stage; else return $this->get_option('current_stage'); }
/** * Get image paths to resized attachment images. * * @param int $attachment_id * @return array */ private function get_attachment_files($attachment_id) { $attachment_images = array(); $upload_dir = function_exists('wp_get_upload_dir') ? wp_get_upload_dir() : wp_upload_dir(null, false);
// get sizes info from attachment meta data. $meta = wp_get_attachment_metadata($attachment_id); if (!is_array($meta) || !array_key_exists('sizes', $meta)) return $attachment_images;
$image_sizes = array_keys($meta['sizes']);
// build list of resized images. foreach ($image_sizes as $size) { $image = image_get_intermediate_size($attachment_id, $size);
if (is_array($image)) { $file = trailingslashit($upload_dir['basedir']) . $image['path']; if (is_file($file) && !in_array($file, $attachment_images)) { $attachment_images[$size] = $file; } } }
return $attachment_images; }
/** * Check the mime type of a downloaded file, returns true if it is a valid image mime type. * * @param string $file_buffer The buffer string downloaded from the compression service * @return boolean */ protected function is_downloaded_image_buffer_mime_type_valid($file_buffer) { // If the required class does not exist, return true to avoid breaking the functionality if (!class_exists('finfo')) return true; $accepted_types = apply_filters('wpo_image_compression_accepted_mime_types', array('image/png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/webp')); // The ignore rule below is added because "finfo" doesn't exist in PHP5.2. $finfo = new finfo(FILEINFO_MIME_TYPE); // phpcs:ignore PHPCompatibility.Classes.NewClasses.finfoFound, PHPCompatibility.Constants.NewConstants.fileinfo_mime_typeFound $mime_type = $finfo->buffer($file_buffer); return in_array($mime_type, $accepted_types); } } endif;
|