Version Description
- Add correction for allow_url_fopen support
- News feed loaded from a SSL URL
Download this release
Release Info
Developer | resmushit |
Plugin | reSmush.it Image Optimizer |
Version | 0.1.16 |
Comparing to | |
See all releases |
Code changes from version 0.1.15 to 0.1.16
- classes/resmushit.class.php +418 -410
- readme.txt +7 -3
- resmushit.php +2 -2
- resmushit.settings.php +2 -2
classes/resmushit.class.php
CHANGED
@@ -1,410 +1,418 @@
|
|
1 |
-
<?php
|
2 |
-
|
3 |
-
/**
|
4 |
-
* ReSmushit
|
5 |
-
*
|
6 |
-
*
|
7 |
-
* @package Resmush.it
|
8 |
-
* @subpackage Controller
|
9 |
-
* @author Charles Bourgeaux <contact@resmush.it>
|
10 |
-
*/
|
11 |
-
Class reSmushit {
|
12 |
-
|
13 |
-
const MAX_FILESIZE = 5242880;
|
14 |
-
|
15 |
-
/**
|
16 |
-
*
|
17 |
-
* Optimize a picture according to a filepath.
|
18 |
-
*
|
19 |
-
* @param string $file_path the path to the file on the server
|
20 |
-
* @return bool TRUE if the resmush operation worked
|
21 |
-
*/
|
22 |
-
public static function getPictureQualitySetting() {
|
23 |
-
if(get_option( 'resmushit_qlty' ))
|
24 |
-
return get_option( 'resmushit_qlty' );
|
25 |
-
else
|
26 |
-
return RESMUSHIT_QLTY;
|
27 |
-
}
|
28 |
-
|
29 |
-
/**
|
30 |
-
*
|
31 |
-
* Optimize a picture according to a filepath.
|
32 |
-
*
|
33 |
-
* @param string $file_path the path to the file on the server
|
34 |
-
* @return bool TRUE if the resmush operation worked
|
35 |
-
*/
|
36 |
-
public static function optimize($file_path = NULL, $is_original = TRUE) {
|
37 |
-
global $wp_version;
|
38 |
-
|
39 |
-
if(filesize($file_path) > self::MAX_FILESIZE){
|
40 |
-
rlog('Error! Picture ' . $file_path . ' cannot be optimized, file size is above 5MB ('. reSmushitUI::sizeFormat(filesize($file_path)) .')');
|
41 |
-
return false;
|
42 |
-
}
|
43 |
-
|
44 |
-
$ch = curl_init();
|
45 |
-
curl_setopt($ch, CURLOPT_URL, RESMUSHIT_ENDPOINT);
|
46 |
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
47 |
-
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, RESMUSHIT_TIMEOUT);
|
48 |
-
curl_setopt($ch, CURLOPT_POST, true);
|
49 |
-
curl_setopt($ch, CURLOPT_USERAGENT, "Wordpress $wp_version/Resmush.it " . RESMUSHIT_VERSION . ' - ' . get_bloginfo('wpurl') );
|
50 |
-
|
51 |
-
if (!class_exists('CURLFile')) {
|
52 |
-
$arg = array('files' => '@' . $file_path);
|
53 |
-
} else {
|
54 |
-
$cfile = new CURLFile($file_path);
|
55 |
-
$arg = array(
|
56 |
-
'files' => $cfile,
|
57 |
-
);
|
58 |
-
}
|
59 |
-
|
60 |
-
$arg['qlty'] = self::getPictureQualitySetting();
|
61 |
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $arg);
|
62 |
-
|
63 |
-
$data = curl_exec($ch);
|
64 |
-
curl_close($ch);
|
65 |
-
|
66 |
-
$json = json_decode($data);
|
67 |
-
if($json){
|
68 |
-
if (!isset($json->error)) {
|
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 |
-
$output['
|
200 |
-
$output['
|
201 |
-
|
202 |
-
if(
|
203 |
-
$output['
|
204 |
-
|
205 |
-
$output['
|
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 |
-
$wpdb->posts.
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
$wpdb->posts
|
285 |
-
|
286 |
-
|
287 |
-
array('
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* ReSmushit
|
5 |
+
*
|
6 |
+
*
|
7 |
+
* @package Resmush.it
|
8 |
+
* @subpackage Controller
|
9 |
+
* @author Charles Bourgeaux <contact@resmush.it>
|
10 |
+
*/
|
11 |
+
Class reSmushit {
|
12 |
+
|
13 |
+
const MAX_FILESIZE = 5242880;
|
14 |
+
|
15 |
+
/**
|
16 |
+
*
|
17 |
+
* Optimize a picture according to a filepath.
|
18 |
+
*
|
19 |
+
* @param string $file_path the path to the file on the server
|
20 |
+
* @return bool TRUE if the resmush operation worked
|
21 |
+
*/
|
22 |
+
public static function getPictureQualitySetting() {
|
23 |
+
if(get_option( 'resmushit_qlty' ))
|
24 |
+
return get_option( 'resmushit_qlty' );
|
25 |
+
else
|
26 |
+
return RESMUSHIT_QLTY;
|
27 |
+
}
|
28 |
+
|
29 |
+
/**
|
30 |
+
*
|
31 |
+
* Optimize a picture according to a filepath.
|
32 |
+
*
|
33 |
+
* @param string $file_path the path to the file on the server
|
34 |
+
* @return bool TRUE if the resmush operation worked
|
35 |
+
*/
|
36 |
+
public static function optimize($file_path = NULL, $is_original = TRUE) {
|
37 |
+
global $wp_version;
|
38 |
+
|
39 |
+
if(filesize($file_path) > self::MAX_FILESIZE){
|
40 |
+
rlog('Error! Picture ' . $file_path . ' cannot be optimized, file size is above 5MB ('. reSmushitUI::sizeFormat(filesize($file_path)) .')');
|
41 |
+
return false;
|
42 |
+
}
|
43 |
+
|
44 |
+
$ch = curl_init();
|
45 |
+
curl_setopt($ch, CURLOPT_URL, RESMUSHIT_ENDPOINT);
|
46 |
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
47 |
+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, RESMUSHIT_TIMEOUT);
|
48 |
+
curl_setopt($ch, CURLOPT_POST, true);
|
49 |
+
curl_setopt($ch, CURLOPT_USERAGENT, "Wordpress $wp_version/Resmush.it " . RESMUSHIT_VERSION . ' - ' . get_bloginfo('wpurl') );
|
50 |
+
|
51 |
+
if (!class_exists('CURLFile')) {
|
52 |
+
$arg = array('files' => '@' . $file_path);
|
53 |
+
} else {
|
54 |
+
$cfile = new CURLFile($file_path);
|
55 |
+
$arg = array(
|
56 |
+
'files' => $cfile,
|
57 |
+
);
|
58 |
+
}
|
59 |
+
|
60 |
+
$arg['qlty'] = self::getPictureQualitySetting();
|
61 |
+
curl_setopt($ch, CURLOPT_POSTFIELDS, $arg);
|
62 |
+
|
63 |
+
$data = curl_exec($ch);
|
64 |
+
curl_close($ch);
|
65 |
+
|
66 |
+
$json = json_decode($data);
|
67 |
+
if($json){
|
68 |
+
if (!isset($json->error)) {
|
69 |
+
if (ini_get('allow_url_fopen')) {
|
70 |
+
$data = file_get_contents($json->dest);
|
71 |
+
} else {
|
72 |
+
$ch = curl_init();
|
73 |
+
curl_setopt($ch, CURLOPT_URL, $json->dest);
|
74 |
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
75 |
+
$data = curl_exec($ch);
|
76 |
+
curl_close($ch);
|
77 |
+
}
|
78 |
+
if ($data) {
|
79 |
+
if($is_original){
|
80 |
+
$originalFile = pathinfo($file_path);
|
81 |
+
$newPath = $originalFile['dirname'] . '/' . $originalFile['filename'] . '-unsmushed.' . $originalFile['extension'];
|
82 |
+
copy($file_path, $newPath);
|
83 |
+
}
|
84 |
+
file_put_contents($file_path, $data);
|
85 |
+
rlog("Picture " . $file_path . " optimized from " . reSmushitUI::sizeFormat($json->src_size) . " to " . reSmushitUI::sizeFormat($json->dest_size));
|
86 |
+
return $json;
|
87 |
+
}
|
88 |
+
} else {
|
89 |
+
rlog("Webservice returned the following error while optimizing $file_path : Code #" . $json->error . " - " . $json->error_long);
|
90 |
+
}
|
91 |
+
} else {
|
92 |
+
rlog("Cannot establish connection with reSmush.it webservice while optimizing $file_path (timeout of " . RESMUSHIT_TIMEOUT . "sec.)");
|
93 |
+
}
|
94 |
+
return false;
|
95 |
+
}
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
/**
|
101 |
+
*
|
102 |
+
* Revert original file and regenerates attachment thumbnails
|
103 |
+
*
|
104 |
+
* @param int $attachment_id ID of the attachment to revert
|
105 |
+
* @return none
|
106 |
+
*/
|
107 |
+
public static function revert($id) {
|
108 |
+
global $wp_version;
|
109 |
+
global $attachment_id;
|
110 |
+
$attachment_id = $id;
|
111 |
+
|
112 |
+
delete_post_meta($attachment_id, 'resmushed_quality');
|
113 |
+
delete_post_meta($attachment_id, 'resmushed_cumulated_original_sizes');
|
114 |
+
delete_post_meta($attachment_id, 'resmushed_cumulated_optimized_sizes');
|
115 |
+
|
116 |
+
$basepath = dirname(get_attached_file( $attachment_id )) . '/';
|
117 |
+
$fileInfo = pathinfo(get_attached_file( $attachment_id ));
|
118 |
+
|
119 |
+
$originalFile = $basepath . $fileInfo['filename'] . '-unsmushed.' . $fileInfo['extension'];
|
120 |
+
rlog('Revert original image for : ' . get_attached_file( $attachment_id ));
|
121 |
+
|
122 |
+
if(file_exists($originalFile))
|
123 |
+
copy($originalFile, get_attached_file( $attachment_id ));
|
124 |
+
|
125 |
+
//Regenerate thumbnails
|
126 |
+
wp_generate_attachment_metadata($attachment_id, get_attached_file( $attachment_id ));
|
127 |
+
|
128 |
+
return self::wasSuccessfullyUpdated( $attachment_id );
|
129 |
+
}
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
/**
|
137 |
+
*
|
138 |
+
* Delete Original file (-unsmushed)
|
139 |
+
*
|
140 |
+
* @param int $attachment_id ID of the attachment
|
141 |
+
* @return none
|
142 |
+
*/
|
143 |
+
public static function deleteOriginalFile($attachment_id) {
|
144 |
+
$basepath = dirname(get_attached_file( $attachment_id )) . '/';
|
145 |
+
$fileInfo = pathinfo(get_attached_file( $attachment_id ));
|
146 |
+
|
147 |
+
$originalFile = $basepath . $fileInfo['filename'] . '-unsmushed.' . $fileInfo['extension'];
|
148 |
+
rlog('Delete original image for : ' . get_attached_file( $attachment_id ));
|
149 |
+
if(file_exists($originalFile))
|
150 |
+
unlink($originalFile);
|
151 |
+
}
|
152 |
+
|
153 |
+
|
154 |
+
/**
|
155 |
+
*
|
156 |
+
* Return optimization statistics
|
157 |
+
*
|
158 |
+
* @param int $attachment_id (optional)
|
159 |
+
* @return array of statistics
|
160 |
+
*/
|
161 |
+
public static function getStatistics($attachment_id = null){
|
162 |
+
global $wpdb;
|
163 |
+
$output = array();
|
164 |
+
$extraSQL = null;
|
165 |
+
if($attachment_id)
|
166 |
+
$extraSQL = "where $wpdb->postmeta.post_id = ". $attachment_id;
|
167 |
+
|
168 |
+
$query = $wpdb->prepare(
|
169 |
+
"select
|
170 |
+
$wpdb->posts.ID as ID, $wpdb->postmeta.meta_value
|
171 |
+
from $wpdb->posts
|
172 |
+
inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s $extraSQL",
|
173 |
+
array('resmushed_cumulated_original_sizes')
|
174 |
+
);
|
175 |
+
$original_sizes = $wpdb->get_results($query);
|
176 |
+
$total_original_size = 0;
|
177 |
+
foreach($original_sizes as $s){
|
178 |
+
$total_original_size += $s->meta_value;
|
179 |
+
}
|
180 |
+
|
181 |
+
$query = $wpdb->prepare(
|
182 |
+
"select
|
183 |
+
$wpdb->posts.ID as ID, $wpdb->postmeta.meta_value
|
184 |
+
from $wpdb->posts
|
185 |
+
inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s $extraSQL",
|
186 |
+
array('resmushed_cumulated_optimized_sizes')
|
187 |
+
);
|
188 |
+
$optimized_sizes = $wpdb->get_results($query);
|
189 |
+
$total_optimized_size = 0;
|
190 |
+
foreach($optimized_sizes as $s){
|
191 |
+
$total_optimized_size += $s->meta_value;
|
192 |
+
}
|
193 |
+
|
194 |
+
|
195 |
+
$output['total_original_size'] = $total_original_size;
|
196 |
+
$output['total_optimized_size'] = $total_optimized_size;
|
197 |
+
$output['total_saved_size'] = $total_original_size - $total_optimized_size;
|
198 |
+
|
199 |
+
$output['total_original_size_nice'] = reSmushitUI::sizeFormat($total_original_size);
|
200 |
+
$output['total_optimized_size_nice'] = reSmushitUI::sizeFormat($total_optimized_size);
|
201 |
+
$output['total_saved_size_nice'] = reSmushitUI::sizeFormat($total_original_size - $total_optimized_size);
|
202 |
+
if($total_original_size == 0)
|
203 |
+
$output['percent_reduction'] = 0;
|
204 |
+
else
|
205 |
+
$output['percent_reduction'] = 100*round(($total_original_size - $total_optimized_size)/$total_original_size,4) . ' %';
|
206 |
+
//number of thumbnails + original picture
|
207 |
+
$output['files_optimized'] = sizeof($optimized_sizes);
|
208 |
+
$output['files_optimized_with_thumbnails'] = sizeof($optimized_sizes) * (sizeof(get_intermediate_image_sizes()) + 1);
|
209 |
+
|
210 |
+
if(!$attachment_id){
|
211 |
+
$output['total_optimizations'] = get_option('resmushit_total_optimized');
|
212 |
+
$output['total_pictures'] = self::getCountAllPictures();
|
213 |
+
$output['total_pictures_with_thumbnails'] = self::getCountAllPictures() * (sizeof(get_intermediate_image_sizes()) + 1);
|
214 |
+
}
|
215 |
+
return $output;
|
216 |
+
}
|
217 |
+
|
218 |
+
|
219 |
+
|
220 |
+
/**
|
221 |
+
*
|
222 |
+
* Get the count of all pictures
|
223 |
+
*
|
224 |
+
* @param none
|
225 |
+
* @return json of unsmushed pictures attachments ID
|
226 |
+
*/
|
227 |
+
public static function getCountAllPictures(){
|
228 |
+
global $wpdb;
|
229 |
+
|
230 |
+
$queryAllPictures = $wpdb->prepare(
|
231 |
+
"select
|
232 |
+
Count($wpdb->posts.ID) as count
|
233 |
+
from $wpdb->posts
|
234 |
+
inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
|
235 |
+
where $wpdb->posts.post_type = %s
|
236 |
+
and $wpdb->posts.post_mime_type like %s
|
237 |
+
and ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/gif' OR $wpdb->posts.post_mime_type = 'image/png')",
|
238 |
+
array('_wp_attachment_metadata','attachment', 'image%')
|
239 |
+
);
|
240 |
+
$data = $wpdb->get_results($queryAllPictures);
|
241 |
+
if(isset($data[0]))
|
242 |
+
$data = $data[0];
|
243 |
+
|
244 |
+
if(!isset($data->count))
|
245 |
+
return 0;
|
246 |
+
return $data->count;
|
247 |
+
}
|
248 |
+
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
+
/**
|
254 |
+
*
|
255 |
+
* Get a list of non optimized pictures
|
256 |
+
*
|
257 |
+
* @param none
|
258 |
+
* @return json of unsmushed pictures attachments ID
|
259 |
+
*/
|
260 |
+
public static function getNonOptimizedPictures(){
|
261 |
+
global $wpdb;
|
262 |
+
$tmp = array();
|
263 |
+
$unsmushed_images = array();
|
264 |
+
$files_too_big = array();
|
265 |
+
$already_optimized_images_array = array();
|
266 |
+
$disabled_images_array = array();
|
267 |
+
|
268 |
+
$queryAllPictures = $wpdb->prepare(
|
269 |
+
"select
|
270 |
+
$wpdb->posts.ID as ID,
|
271 |
+
$wpdb->posts.guid as guid,
|
272 |
+
$wpdb->postmeta.meta_value as file_meta
|
273 |
+
from $wpdb->posts
|
274 |
+
inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
|
275 |
+
where $wpdb->posts.post_type = %s
|
276 |
+
and $wpdb->posts.post_mime_type like %s
|
277 |
+
and ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/gif' OR $wpdb->posts.post_mime_type = 'image/png')",
|
278 |
+
array('_wp_attachment_metadata','attachment', 'image%')
|
279 |
+
);
|
280 |
+
|
281 |
+
$queryAlreadyOptimizedPictures = $wpdb->prepare(
|
282 |
+
"select
|
283 |
+
$wpdb->posts.ID as ID
|
284 |
+
from $wpdb->posts
|
285 |
+
inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
|
286 |
+
where $wpdb->postmeta.meta_value = %s",
|
287 |
+
array('resmushed_quality', self::getPictureQualitySetting())
|
288 |
+
);
|
289 |
+
|
290 |
+
$queryDisabledPictures = $wpdb->prepare(
|
291 |
+
"select
|
292 |
+
$wpdb->posts.ID as ID
|
293 |
+
from $wpdb->posts
|
294 |
+
inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s",
|
295 |
+
array('resmushed_disabled')
|
296 |
+
);
|
297 |
+
|
298 |
+
|
299 |
+
|
300 |
+
// Get the images in the attachement table
|
301 |
+
$all_images = $wpdb->get_results($queryAllPictures);
|
302 |
+
$already_optimized_images = $wpdb->get_results($queryAlreadyOptimizedPictures);
|
303 |
+
$disabled_images = $wpdb->get_results($queryDisabledPictures);
|
304 |
+
|
305 |
+
foreach($already_optimized_images as $image)
|
306 |
+
$already_optimized_images_array[] = $image->ID;
|
307 |
+
|
308 |
+
foreach($disabled_images as $image)
|
309 |
+
$disabled_images_array[] = $image->ID;
|
310 |
+
|
311 |
+
|
312 |
+
foreach($all_images as $image){
|
313 |
+
if(!in_array($image->ID, $already_optimized_images_array) && !in_array($image->ID, $disabled_images_array)){
|
314 |
+
$tmp = array();
|
315 |
+
$tmp['ID'] = $image->ID;
|
316 |
+
$tmp['attachment_metadata'] = unserialize($image->file_meta);
|
317 |
+
|
318 |
+
|
319 |
+
//If filesize > 5MB, we do not optimize this picture
|
320 |
+
if( filesize(get_attached_file( $image->ID )) > self::MAX_FILESIZE){
|
321 |
+
$files_too_big[] = $tmp;
|
322 |
+
continue;
|
323 |
+
}
|
324 |
+
|
325 |
+
$unsmushed_images[] = $tmp;
|
326 |
+
}
|
327 |
+
|
328 |
+
}
|
329 |
+
return json_encode(array('nonoptimized' => $unsmushed_images, 'filestoobig' => $files_too_big));
|
330 |
+
}
|
331 |
+
|
332 |
+
|
333 |
+
/**
|
334 |
+
*
|
335 |
+
* Return the number of non optimized pictures
|
336 |
+
*
|
337 |
+
* @param none
|
338 |
+
* @return number of non optimized pictures to the current quality factor
|
339 |
+
*/
|
340 |
+
public static function getCountNonOptimizedPictures(){
|
341 |
+
$data = json_decode(self::getNonOptimizedPictures());
|
342 |
+
return array('nonoptimized' => sizeof($data->nonoptimized), 'filestoobig' => sizeof($data->filestoobig));
|
343 |
+
}
|
344 |
+
|
345 |
+
|
346 |
+
/**
|
347 |
+
*
|
348 |
+
* Record in DB new status for optimization disabled state
|
349 |
+
*
|
350 |
+
* @param int $id ID of postID
|
351 |
+
* @param string $state state of disable
|
352 |
+
* @return none
|
353 |
+
*/
|
354 |
+
public static function updateDisabledState($id, $state){
|
355 |
+
//if we do not want this attachment to be resmushed.
|
356 |
+
if($state == "true"){
|
357 |
+
update_post_meta($id, 'resmushed_disabled', 'disabled');
|
358 |
+
self::revert($id);
|
359 |
+
return 'true';
|
360 |
+
} else {
|
361 |
+
delete_post_meta($id, 'resmushed_disabled');
|
362 |
+
return 'false';
|
363 |
+
}
|
364 |
+
}
|
365 |
+
|
366 |
+
|
367 |
+
|
368 |
+
/**
|
369 |
+
*
|
370 |
+
* Get Disabled State
|
371 |
+
*
|
372 |
+
* @param int $attachment_id Post ID
|
373 |
+
* @return boolean true if attachment is disabled
|
374 |
+
*/
|
375 |
+
public static function getDisabledState($attachment_id){
|
376 |
+
if(get_post_meta($attachment_id, 'resmushed_disabled'))
|
377 |
+
return true;
|
378 |
+
return false;
|
379 |
+
}
|
380 |
+
|
381 |
+
|
382 |
+
|
383 |
+
/**
|
384 |
+
*
|
385 |
+
* Get Last Quality Factor attached to a picture
|
386 |
+
*
|
387 |
+
* @param int $attachment_id Post ID
|
388 |
+
* @return int quality setting for this attachment
|
389 |
+
*/
|
390 |
+
public static function getAttachmentQuality($attachment_id){
|
391 |
+
$attachmentQuality = get_post_meta($attachment_id, 'resmushed_quality');
|
392 |
+
if(isset($attachmentQuality[0]))
|
393 |
+
return $attachmentQuality[0];
|
394 |
+
return null;
|
395 |
+
}
|
396 |
+
|
397 |
+
|
398 |
+
|
399 |
+
/**
|
400 |
+
*
|
401 |
+
* Check if this Attachment was successfully optimized
|
402 |
+
*
|
403 |
+
* @param int $attachment_id Post ID
|
404 |
+
* @return string $status
|
405 |
+
*/
|
406 |
+
public static function wasSuccessfullyUpdated($attachment_id){
|
407 |
+
if( self::getDisabledState( $attachment_id ))
|
408 |
+
return 'disabled';
|
409 |
+
|
410 |
+
if( filesize(get_attached_file( $attachment_id )) > self::MAX_FILESIZE){
|
411 |
+
return 'file_too_big';
|
412 |
+
}
|
413 |
+
|
414 |
+
if( self::getPictureQualitySetting() != self::getAttachmentQuality( $attachment_id ))
|
415 |
+
return 'failed';
|
416 |
+
return 'success';
|
417 |
+
}
|
418 |
+
}
|
readme.txt
CHANGED
@@ -2,8 +2,8 @@
|
|
2 |
Contributors: resmushit
|
3 |
Tags: image, optimizer, image optimization, resmush.it, smush, jpg, png, gif, optimization, compression, Compress, Images, Pictures, Reduce Image Size, Smush, Smush.it
|
4 |
Requires at least: 4.0.0
|
5 |
-
Tested up to: 4.9.
|
6 |
-
Stable tag: 0.1.
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
@@ -66,8 +66,12 @@ Yes ! Absolutely free, the only restriction is to send images below 5MB.
|
|
66 |
|
67 |
== Changelog ==
|
68 |
|
|
|
|
|
|
|
|
|
69 |
= 0.1.15 =
|
70 |
-
* Log rotate if file too big
|
71 |
|
72 |
= 0.1.14 =
|
73 |
* Tested up to Wordpress 4.9.5
|
2 |
Contributors: resmushit
|
3 |
Tags: image, optimizer, image optimization, resmush.it, smush, jpg, png, gif, optimization, compression, Compress, Images, Pictures, Reduce Image Size, Smush, Smush.it
|
4 |
Requires at least: 4.0.0
|
5 |
+
Tested up to: 4.9.8
|
6 |
+
Stable tag: 0.1.16
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
66 |
|
67 |
== Changelog ==
|
68 |
|
69 |
+
= 0.1.16 =
|
70 |
+
* Add correction for allow_url_fopen support
|
71 |
+
* News feed loaded from a SSL URL
|
72 |
+
|
73 |
= 0.1.15 =
|
74 |
+
* Log rotate if file too big
|
75 |
|
76 |
= 0.1.14 =
|
77 |
* Tested up to Wordpress 4.9.5
|
resmushit.php
CHANGED
@@ -10,8 +10,8 @@
|
|
10 |
* Plugin Name: reSmush.it Image Optimizer
|
11 |
* Plugin URI: https://resmush.it
|
12 |
* Description: Image Optimization API. Provides image size optimization
|
13 |
-
* Version: 0.1.
|
14 |
-
* Timestamp: 2018.
|
15 |
* Author: reSmush.it
|
16 |
* Author URI: https://resmush.it
|
17 |
* Author: Charles Bourgeaux
|
10 |
* Plugin Name: reSmush.it Image Optimizer
|
11 |
* Plugin URI: https://resmush.it
|
12 |
* Description: Image Optimization API. Provides image size optimization
|
13 |
+
* Version: 0.1.16
|
14 |
+
* Timestamp: 2018.08.13
|
15 |
* Author: reSmush.it
|
16 |
* Author URI: https://resmush.it
|
17 |
* Author: Charles Bourgeaux
|
resmushit.settings.php
CHANGED
@@ -2,10 +2,10 @@
|
|
2 |
|
3 |
|
4 |
define('RESMUSHIT_ENDPOINT', 'http://api.resmush.it/');
|
5 |
-
define('RESMUSHIT_VERSION', '0.1.
|
6 |
define('RESMUSHIT_DEFAULT_QLTY', '92');
|
7 |
define('RESMUSHIT_TIMEOUT', '5');
|
8 |
define('RESMUSHIT_LOGS_PATH', 'resmushit.log');
|
9 |
define('RESMUSHIT_LOGS_MAX_FILESIZE', '102400');
|
10 |
-
define('RESMUSHIT_NEWSFEED', '
|
11 |
define('RESMUSHIT_BASE_URL', plugin_dir_url( __FILE__ ));
|
2 |
|
3 |
|
4 |
define('RESMUSHIT_ENDPOINT', 'http://api.resmush.it/');
|
5 |
+
define('RESMUSHIT_VERSION', '0.1.16');
|
6 |
define('RESMUSHIT_DEFAULT_QLTY', '92');
|
7 |
define('RESMUSHIT_TIMEOUT', '5');
|
8 |
define('RESMUSHIT_LOGS_PATH', 'resmushit.log');
|
9 |
define('RESMUSHIT_LOGS_MAX_FILESIZE', '102400');
|
10 |
+
define('RESMUSHIT_NEWSFEED', 'https://feed.resmush.it/');
|
11 |
define('RESMUSHIT_BASE_URL', plugin_dir_url( __FILE__ ));
|