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

201 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 = 100;
86 }
87
88 if (isset($options['quality']) && is_int($options['quality']) && 0 < $options['quality']) $quality = $options['quality'];
89
90 $this->log($quality);
91 $post_fields = array(
92 'qlty' => $quality,
93 'exif' => $this->get_option('preserve_exif', false)
94 );
95 $payload = '';
96 $file_name = basename($local_file);
97
98 foreach ($post_fields as $name => $value) {
99 $payload .= "--$boundary";
100 $payload .= "\r\n";
101 $payload .= "Content-Disposition: form-data; name='$name' \r\n\r\n $value";
102 $payload .= "\r\n";
103 }
104
105 $payload .= "--$boundary";
106 $payload .= "\r\n";
107 $payload .= "Content-Disposition: form-data; name=\"files\"; filename=\"$file_name\"\r\n";
108 $payload .= "\r\n";
109 $payload .= file_get_contents($local_file);
110 $payload .= "\r\n";
111 $payload .= "--$boundary";
112
113 return array(
114 'headers' => $headers,
115 'timeout' => $this->get_option('request_timeout'),
116 'user-agent' => "WordPress $wp_version/WP-Optimize ".WPO_VERSION.' - anonymous', // Anonymous until Resmushit has a clear privacy statement that we can link to
117 'body' => $payload,
118 );
119 }
120
121 /**
122 * Processes the response recieved from the remote server
123 *
124 * @param String $response - The response object
125 */
126 public function process_server_response($response) {
127 global $http_response_header;
128
129 $response = parent::process_server_response($response);
130 $data = json_decode(wp_remote_retrieve_body($response));
131
132 if (!$data) {
133 $this->log("Cannot establish connection with reSmush.it webservice. Please try later");
134 return false;
135 }
136
137 if (isset($data->error)) {
138 $this->fail($data->error, $data->error_long);
139 return false;
140 }
141
142 if (!property_exists($data, 'dest')) {
143 $this->fail("invalid_response", "The response does not contain the compressed file URL");
144 $this->log("data: ".json_encode($data));
145 return false;
146 }
147
148 $compressed_image_response = wp_remote_get($data->dest);
149
150 if (!is_wp_error($compressed_image_response)) {
151 $image_contents = wp_remote_retrieve_body($compressed_image_response);
152 if ($this->is_downloaded_image_buffer_mime_type_valid($image_contents)) {
153 return $image_contents;
154 } else {
155 $this->log("The downloaded resource does not have a matching mime type.");
156 return false;
157 }
158 } else {
159 $this->fail("invalid_response", "The compression apparently succeeded, but WP-Optimize could not retrieve the compressed image from the remote server.");
160 $this->log("data: ".json_encode($data));
161 if (!empty($http_response_header) && is_array($http_response_header)) {
162 $this->log("headers: ".implode("\n", $http_response_header));
163 }
164 return false;
165 }
166 }
167
168 /**
169 * Retrieve features for this service
170 *
171 * @return Array - an array of options
172 */
173 public static function get_features() {
174 return array(
175 'max_filesize' => self::MAX_FILESIZE,
176 'lossy_compression' => false,
177 'preserve_exif' => true,
178 );
179 }
180
181 /**
182 * Retrieve default options for this task type.
183 *
184 * @return Array - an array of options
185 */
186 public function get_default_options() {
187 return array(
188 'allowed_file_types' => array('gif', 'png', 'jpg', 'tif', 'jpeg'),
189 'request_timeout' => 30,
190 'keep_original' => true,
191 'preserve_exif' => false,
192 'image_quality' => 98,
193 'api_endpoint' => self::API_URL,
194 'max_filesize' => self::MAX_FILESIZE,
195 'version' => '0.1.13',
196 'backup_prefix' => '-updraft-pre-smush-original.'
197 );
198 }
199 }
200 endif;
201