PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.3
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-re-smush-it-task.php

class-re-smush-it-task.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.3, at includes/class-re-smush-it-task.php

210 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A sample implementation using the Resmush.it API and our tasks library
4 */
5
6 if (!defined('ABSPATH')) die('Access denied.');
7
8 if (!class_exists('Re_Smush_It_Task')) :
9
10 class Re_Smush_It_Task extends Updraft_Smush_Task {
11
12 /**
13 * Task name
14 *
15 * @var string
16 */
17 public $label = 're-smush-it';
18
19 /**
20 * Maximum filesize
21 */
22 const MAX_FILESIZE = 5242880;
23
24 /**
25 * API URL
26 */
27 const API_URL = 'https://api.resmush.it/';
28
29 /**
30 * Checks if the server is online
31 *
32 * @return boolean - true if yes, false otherwise
33 */
34 public static function is_server_online() {
35
36 global $wp_version;
37 $test_image = WPO_PLUGIN_MAIN_PATH . 'images/icon/wpo.png';
38 $boundary = wp_generate_password();
39 $file_name = basename($test_image);
40
41 $body = "--$boundary";
42 $body .= "\r\n";
43 $body .= "Content-Disposition: form-data; name=\"files\"; filename=\"$file_name\"\r\n";
44 $body .= "\r\n";
45 $body .= file_get_contents($test_image);
46 $body .= "\r\n";
47 $body .= "--$boundary";
48
49 $request = array(
50 'headers' => array( "content-type" => "multipart/form-data; boundary=$boundary" ),
51 'user-agent' => "WordPress $wp_version/WP-Optimize ".WPO_VERSION.' - anonymous', // Anonymous until Resmushit has a clear privacy statement that we can link to
52 'timeout' => 10,
53 'body' => $body,
54 );
55
56 $response = wp_remote_post(self::API_URL, $request);
57
58 if (is_wp_error($response)) {
59 update_option(__CLASS__, $response->get_error_message());
60 return false;
61 }
62
63 $data = json_decode(wp_remote_retrieve_body($response));
64
65
66 if (empty($data)) {
67 update_option(__CLASS__, "Empty data returned by server");
68 return false;
69 }
70
71 if (isset($data->error)) {
72 update_option(__CLASS__, $data->error_long);
73 return false;
74 }
75
76 return true;
77 }
78
79 /**
80 * Prepares the image as part of the post data for the specific implementation
81 *
82 * @param string $local_file - The image to e optimised
83 * @param array $options - Eventual options
84 *
85 * @return array
86 */
87 public function prepare_post_request($local_file, $options) {
88 global $wp_version;
89
90 $boundary = wp_generate_password();
91 $headers = array( "content-type" => "multipart/form-data; boundary=$boundary" );
92
93 $lossy = $this->get_option('lossy_compression');
94
95 if ($lossy) {
96 $quality = $this->get_option('image_quality');
97 } else {
98 $quality = 92;
99 }
100
101 if (isset($options['quality']) && is_int($options['quality']) && 0 < $options['quality']) $quality = $options['quality'];
102
103 $post_fields = array(
104 'qlty' => $quality,
105 'exif' => $this->get_option('preserve_exif', false)
106 );
107 $payload = '';
108 $file_name = basename($local_file);
109
110 foreach ($post_fields as $name => $value) {
111 $payload .= "--$boundary";
112 $payload .= "\r\n";
113 $payload .= "Content-Disposition: form-data; name='$name' \r\n\r\n $value";
114 $payload .= "\r\n";
115 }
116
117 $payload .= "--$boundary";
118 $payload .= "\r\n";
119 $payload .= "Content-Disposition: form-data; name=\"files\"; filename=\"$file_name\"\r\n";
120 $payload .= "\r\n";
121 $payload .= file_get_contents($local_file);
122 $payload .= "\r\n";
123 $payload .= "--$boundary";
124
125 return array(
126 'headers' => $headers,
127 'timeout' => $this->get_option('request_timeout'),
128 'user-agent' => "WordPress $wp_version/WP-Optimize ".WPO_VERSION.' - anonymous', // Anonymous until Resmushit has a clear privacy statement that we can link to
129 'body' => $payload,
130 );
131 }
132
133 /**
134 * Processes the response received from the remote server
135 *
136 * @param mixed $response - The response object
137 */
138 public function process_server_response($response) {
139
140 $response = parent::process_server_response($response);
141 $data = json_decode(wp_remote_retrieve_body($response));
142
143 if (!$data) {
144 $this->log("Cannot establish connection with reSmush.it webservice. Please try later");
145 return false;
146 }
147
148 if (isset($data->error)) {
149 $this->fail($data->error, $data->error_long);
150 return false;
151 }
152
153 if (!property_exists($data, 'dest')) {
154 $this->fail("invalid_response", "The response does not contain the compressed file URL");
155 $this->log("data: ".wp_json_encode($data));
156 return false;
157 }
158
159 $compressed_image_response = wp_remote_get($data->dest);
160
161 if (!is_wp_error($compressed_image_response)) {
162 $image_contents = wp_remote_retrieve_body($compressed_image_response);
163 if ($this->is_downloaded_image_buffer_mime_type_valid($image_contents)) {
164 return $image_contents;
165 } else {
166 $this->log("The downloaded resource does not have a matching mime type.");
167 return false;
168 }
169 } else {
170 $this->fail("invalid_response", "The compression apparently succeeded, but WP-Optimize could not retrieve the compressed image from the remote server.");
171 $this->log("data: ".wp_json_encode($data));
172 $this->log("error: ".$compressed_image_response->get_error_message());
173 return false;
174 }
175 }
176
177 /**
178 * Retrieve features for this service
179 *
180 * @return array - an array of options
181 */
182 public static function get_features() {
183 return array(
184 'max_filesize' => self::MAX_FILESIZE,
185 'lossy_compression' => false,
186 'preserve_exif' => true,
187 );
188 }
189
190 /**
191 * Retrieve default options for this task type.
192 *
193 * @return array - an array of options
194 */
195 public function get_default_options() {
196 return array(
197 'allowed_file_types' => array('gif', 'png', 'jpg', 'tif', 'jpeg'),
198 'request_timeout' => 30,
199 'keep_original' => true,
200 'preserve_exif' => false,
201 'image_quality' => 92,
202 'api_endpoint' => self::API_URL,
203 'max_filesize' => self::MAX_FILESIZE,
204 'version' => '0.1.13',
205 'backup_prefix' => '-updraft-pre-smush-original.'
206 );
207 }
208 }
209 endif;
210