PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.3.1
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.3.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-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 3.3.1, at includes/class-re-smush-it-task.php

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