PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.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-updraft-resmushit-task.php

class-updraft-resmushit-task.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at includes/class-updraft-resmushit-task.php

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