reSmush.it Image Optimizer - Version 0.1.16

Version Description

  • Add correction for allow_url_fopen support
  • News feed loaded from a SSL URL
Download this release

Release Info

Developer resmushit
Plugin Icon 128x128 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 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
- $data = file_get_contents($json->dest);
70
- if ($data) {
71
- if($is_original){
72
- $originalFile = pathinfo($file_path);
73
- $newPath = $originalFile['dirname'] . '/' . $originalFile['filename'] . '-unsmushed.' . $originalFile['extension'];
74
- copy($file_path, $newPath);
75
- }
76
- file_put_contents($file_path, $data);
77
- rlog("Picture " . $file_path . " optimized from " . reSmushitUI::sizeFormat($json->src_size) . " to " . reSmushitUI::sizeFormat($json->dest_size));
78
- return $json;
79
- }
80
- } else {
81
- rlog("Webservice returned the following error while optimizing $file_path : Code #" . $json->error . " - " . $json->error_long);
82
- }
83
- } else {
84
- rlog("Cannot establish connection with reSmush.it webservice while optimizing $file_path (timeout of " . RESMUSHIT_TIMEOUT . "sec.)");
85
- }
86
- return false;
87
- }
88
-
89
-
90
-
91
-
92
- /**
93
- *
94
- * Revert original file and regenerates attachment thumbnails
95
- *
96
- * @param int $attachment_id ID of the attachment to revert
97
- * @return none
98
- */
99
- public static function revert($id) {
100
- global $wp_version;
101
- global $attachment_id;
102
- $attachment_id = $id;
103
-
104
- delete_post_meta($attachment_id, 'resmushed_quality');
105
- delete_post_meta($attachment_id, 'resmushed_cumulated_original_sizes');
106
- delete_post_meta($attachment_id, 'resmushed_cumulated_optimized_sizes');
107
-
108
- $basepath = dirname(get_attached_file( $attachment_id )) . '/';
109
- $fileInfo = pathinfo(get_attached_file( $attachment_id ));
110
-
111
- $originalFile = $basepath . $fileInfo['filename'] . '-unsmushed.' . $fileInfo['extension'];
112
- rlog('Revert original image for : ' . get_attached_file( $attachment_id ));
113
-
114
- if(file_exists($originalFile))
115
- copy($originalFile, get_attached_file( $attachment_id ));
116
-
117
- //Regenerate thumbnails
118
- wp_generate_attachment_metadata($attachment_id, get_attached_file( $attachment_id ));
119
-
120
- return self::wasSuccessfullyUpdated( $attachment_id );
121
- }
122
-
123
-
124
-
125
-
126
-
127
-
128
- /**
129
- *
130
- * Delete Original file (-unsmushed)
131
- *
132
- * @param int $attachment_id ID of the attachment
133
- * @return none
134
- */
135
- public static function deleteOriginalFile($attachment_id) {
136
- $basepath = dirname(get_attached_file( $attachment_id )) . '/';
137
- $fileInfo = pathinfo(get_attached_file( $attachment_id ));
138
-
139
- $originalFile = $basepath . $fileInfo['filename'] . '-unsmushed.' . $fileInfo['extension'];
140
- rlog('Delete original image for : ' . get_attached_file( $attachment_id ));
141
- if(file_exists($originalFile))
142
- unlink($originalFile);
143
- }
144
-
145
-
146
- /**
147
- *
148
- * Return optimization statistics
149
- *
150
- * @param int $attachment_id (optional)
151
- * @return array of statistics
152
- */
153
- public static function getStatistics($attachment_id = null){
154
- global $wpdb;
155
- $output = array();
156
- $extraSQL = null;
157
- if($attachment_id)
158
- $extraSQL = "where $wpdb->postmeta.post_id = ". $attachment_id;
159
-
160
- $query = $wpdb->prepare(
161
- "select
162
- $wpdb->posts.ID as ID, $wpdb->postmeta.meta_value
163
- from $wpdb->posts
164
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s $extraSQL",
165
- array('resmushed_cumulated_original_sizes')
166
- );
167
- $original_sizes = $wpdb->get_results($query);
168
- $total_original_size = 0;
169
- foreach($original_sizes as $s){
170
- $total_original_size += $s->meta_value;
171
- }
172
-
173
- $query = $wpdb->prepare(
174
- "select
175
- $wpdb->posts.ID as ID, $wpdb->postmeta.meta_value
176
- from $wpdb->posts
177
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s $extraSQL",
178
- array('resmushed_cumulated_optimized_sizes')
179
- );
180
- $optimized_sizes = $wpdb->get_results($query);
181
- $total_optimized_size = 0;
182
- foreach($optimized_sizes as $s){
183
- $total_optimized_size += $s->meta_value;
184
- }
185
-
186
-
187
- $output['total_original_size'] = $total_original_size;
188
- $output['total_optimized_size'] = $total_optimized_size;
189
- $output['total_saved_size'] = $total_original_size - $total_optimized_size;
190
-
191
- $output['total_original_size_nice'] = reSmushitUI::sizeFormat($total_original_size);
192
- $output['total_optimized_size_nice'] = reSmushitUI::sizeFormat($total_optimized_size);
193
- $output['total_saved_size_nice'] = reSmushitUI::sizeFormat($total_original_size - $total_optimized_size);
194
- if($total_original_size == 0)
195
- $output['percent_reduction'] = 0;
196
- else
197
- $output['percent_reduction'] = 100*round(($total_original_size - $total_optimized_size)/$total_original_size,4) . ' %';
198
- //number of thumbnails + original picture
199
- $output['files_optimized'] = sizeof($optimized_sizes);
200
- $output['files_optimized_with_thumbnails'] = sizeof($optimized_sizes) * (sizeof(get_intermediate_image_sizes()) + 1);
201
-
202
- if(!$attachment_id){
203
- $output['total_optimizations'] = get_option('resmushit_total_optimized');
204
- $output['total_pictures'] = self::getCountAllPictures();
205
- $output['total_pictures_with_thumbnails'] = self::getCountAllPictures() * (sizeof(get_intermediate_image_sizes()) + 1);
206
- }
207
- return $output;
208
- }
209
-
210
-
211
-
212
- /**
213
- *
214
- * Get the count of all pictures
215
- *
216
- * @param none
217
- * @return json of unsmushed pictures attachments ID
218
- */
219
- public static function getCountAllPictures(){
220
- global $wpdb;
221
-
222
- $queryAllPictures = $wpdb->prepare(
223
- "select
224
- Count($wpdb->posts.ID) as count
225
- from $wpdb->posts
226
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
227
- where $wpdb->posts.post_type = %s
228
- and $wpdb->posts.post_mime_type like %s
229
- and ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/gif' OR $wpdb->posts.post_mime_type = 'image/png')",
230
- array('_wp_attachment_metadata','attachment', 'image%')
231
- );
232
- $data = $wpdb->get_results($queryAllPictures);
233
- if(isset($data[0]))
234
- $data = $data[0];
235
-
236
- if(!isset($data->count))
237
- return 0;
238
- return $data->count;
239
- }
240
-
241
-
242
-
243
-
244
-
245
- /**
246
- *
247
- * Get a list of non optimized pictures
248
- *
249
- * @param none
250
- * @return json of unsmushed pictures attachments ID
251
- */
252
- public static function getNonOptimizedPictures(){
253
- global $wpdb;
254
- $tmp = array();
255
- $unsmushed_images = array();
256
- $files_too_big = array();
257
- $already_optimized_images_array = array();
258
- $disabled_images_array = array();
259
-
260
- $queryAllPictures = $wpdb->prepare(
261
- "select
262
- $wpdb->posts.ID as ID,
263
- $wpdb->posts.guid as guid,
264
- $wpdb->postmeta.meta_value as file_meta
265
- from $wpdb->posts
266
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
267
- where $wpdb->posts.post_type = %s
268
- and $wpdb->posts.post_mime_type like %s
269
- and ($wpdb->posts.post_mime_type = 'image/jpeg' OR $wpdb->posts.post_mime_type = 'image/gif' OR $wpdb->posts.post_mime_type = 'image/png')",
270
- array('_wp_attachment_metadata','attachment', 'image%')
271
- );
272
-
273
- $queryAlreadyOptimizedPictures = $wpdb->prepare(
274
- "select
275
- $wpdb->posts.ID as ID
276
- from $wpdb->posts
277
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
278
- where $wpdb->postmeta.meta_value = %s",
279
- array('resmushed_quality', self::getPictureQualitySetting())
280
- );
281
-
282
- $queryDisabledPictures = $wpdb->prepare(
283
- "select
284
- $wpdb->posts.ID as ID
285
- from $wpdb->posts
286
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s",
287
- array('resmushed_disabled')
288
- );
289
-
290
-
291
-
292
- // Get the images in the attachement table
293
- $all_images = $wpdb->get_results($queryAllPictures);
294
- $already_optimized_images = $wpdb->get_results($queryAlreadyOptimizedPictures);
295
- $disabled_images = $wpdb->get_results($queryDisabledPictures);
296
-
297
- foreach($already_optimized_images as $image)
298
- $already_optimized_images_array[] = $image->ID;
299
-
300
- foreach($disabled_images as $image)
301
- $disabled_images_array[] = $image->ID;
302
-
303
-
304
- foreach($all_images as $image){
305
- if(!in_array($image->ID, $already_optimized_images_array) && !in_array($image->ID, $disabled_images_array)){
306
- $tmp = array();
307
- $tmp['ID'] = $image->ID;
308
- $tmp['attachment_metadata'] = unserialize($image->file_meta);
309
-
310
-
311
- //If filesize > 5MB, we do not optimize this picture
312
- if( filesize(get_attached_file( $image->ID )) > self::MAX_FILESIZE){
313
- $files_too_big[] = $tmp;
314
- continue;
315
- }
316
-
317
- $unsmushed_images[] = $tmp;
318
- }
319
-
320
- }
321
- return json_encode(array('nonoptimized' => $unsmushed_images, 'filestoobig' => $files_too_big));
322
- }
323
-
324
-
325
- /**
326
- *
327
- * Return the number of non optimized pictures
328
- *
329
- * @param none
330
- * @return number of non optimized pictures to the current quality factor
331
- */
332
- public static function getCountNonOptimizedPictures(){
333
- $data = json_decode(self::getNonOptimizedPictures());
334
- return array('nonoptimized' => sizeof($data->nonoptimized), 'filestoobig' => sizeof($data->filestoobig));
335
- }
336
-
337
-
338
- /**
339
- *
340
- * Record in DB new status for optimization disabled state
341
- *
342
- * @param int $id ID of postID
343
- * @param string $state state of disable
344
- * @return none
345
- */
346
- public static function updateDisabledState($id, $state){
347
- //if we do not want this attachment to be resmushed.
348
- if($state == "true"){
349
- update_post_meta($id, 'resmushed_disabled', 'disabled');
350
- self::revert($id);
351
- return 'true';
352
- } else {
353
- delete_post_meta($id, 'resmushed_disabled');
354
- return 'false';
355
- }
356
- }
357
-
358
-
359
-
360
- /**
361
- *
362
- * Get Disabled State
363
- *
364
- * @param int $attachment_id Post ID
365
- * @return boolean true if attachment is disabled
366
- */
367
- public static function getDisabledState($attachment_id){
368
- if(get_post_meta($attachment_id, 'resmushed_disabled'))
369
- return true;
370
- return false;
371
- }
372
-
373
-
374
-
375
- /**
376
- *
377
- * Get Last Quality Factor attached to a picture
378
- *
379
- * @param int $attachment_id Post ID
380
- * @return int quality setting for this attachment
381
- */
382
- public static function getAttachmentQuality($attachment_id){
383
- $attachmentQuality = get_post_meta($attachment_id, 'resmushed_quality');
384
- if(isset($attachmentQuality[0]))
385
- return $attachmentQuality[0];
386
- return null;
387
- }
388
-
389
-
390
-
391
- /**
392
- *
393
- * Check if this Attachment was successfully optimized
394
- *
395
- * @param int $attachment_id Post ID
396
- * @return string $status
397
- */
398
- public static function wasSuccessfullyUpdated($attachment_id){
399
- if( self::getDisabledState( $attachment_id ))
400
- return 'disabled';
401
-
402
- if( filesize(get_attached_file( $attachment_id )) > self::MAX_FILESIZE){
403
- return 'file_too_big';
404
- }
405
-
406
- if( self::getPictureQualitySetting() != self::getAttachmentQuality( $attachment_id ))
407
- return 'failed';
408
- return 'success';
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.5
6
- Stable tag: 0.1.15
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.15
14
- * Timestamp: 2018.04.16
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.15');
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', 'http://feed.resmush.it/');
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__ ));