reSmush.it Image Optimizer - Version 0.1.17

Version Description

  • Fix bug (non-working optimization) on bulk upload when "Optimize on upload" isn't selected
  • New header banner for 4 billionth images optimized
Download this release

Release Info

Developer resmushit
Plugin Icon 128x128 reSmush.it Image Optimizer
Version 0.1.17
Comparing to
See all releases

Code changes from version 0.1.16 to 0.1.17

classes/resmushit.class.php CHANGED
@@ -1,418 +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
- 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
- }
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
+ }
images/header.jpg CHANGED
Binary file
readme.txt CHANGED
@@ -3,18 +3,18 @@ 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
 
10
- Use reSmush.it Image Optimizer for FREE to optimize your pictures file sizes. Improve your performances by using reSmush.it, the 3 billion images API optimizer.
11
 
12
  == Description ==
13
 
14
  reSmush.it Image Optimizer allow to use **free Image optimization** based on [reSmush.it API](http://www.resmush.it/ "Image Optimization API, developped by Charles Bourgeaux"). reSmush.it provides image size reduction based on several advanced algorithms. The API accept JPG, PNG and GIF files up to **5MB**.
15
 
16
  This plugin includes a bulk operation to optimize all your pictures in 2 clicks ! Change your image optimization level to fit your needs !
17
- This service is used by **thousands** of websites on different CMS (Drupal, Joomla, Magento, Prestashop...).
18
 
19
  The plugin includes an option to exclude some pictures of the optimizer.
20
 
@@ -66,6 +66,10 @@ Yes ! Absolutely free, the only restriction is to send images below 5MB.
66
 
67
  == Changelog ==
68
 
 
 
 
 
69
  = 0.1.16 =
70
  * Add correction for allow_url_fopen support
71
  * News feed loaded from a SSL URL
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.17
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
+ Use reSmush.it Image Optimizer for FREE to optimize your pictures file sizes. Improve your performances by using reSmush.it, the 4 billion images API optimizer.
11
 
12
  == Description ==
13
 
14
  reSmush.it Image Optimizer allow to use **free Image optimization** based on [reSmush.it API](http://www.resmush.it/ "Image Optimization API, developped by Charles Bourgeaux"). reSmush.it provides image size reduction based on several advanced algorithms. The API accept JPG, PNG and GIF files up to **5MB**.
15
 
16
  This plugin includes a bulk operation to optimize all your pictures in 2 clicks ! Change your image optimization level to fit your needs !
17
+ This service is used by more than **300,000** websites on different CMS (Drupal, Joomla, Magento, Prestashop...).
18
 
19
  The plugin includes an option to exclude some pictures of the optimizer.
20
 
66
 
67
  == Changelog ==
68
 
69
+ = 0.1.17 =
70
+ * Fix bug (non-working optimization) on bulk upload when "Optimize on upload" isn't selected
71
+ * New header banner for 4 billionth images optimized
72
+
73
  = 0.1.16 =
74
  * Add correction for allow_url_fopen support
75
  * News feed loaded from a SSL URL
resmushit.inc.php CHANGED
@@ -1,76 +1,76 @@
1
- <?php
2
-
3
- require('resmushit.settings.php');
4
- require('classes/resmushit.class.php');
5
- require('classes/resmushitUI.class.php');
6
- require('resmushit.admin.php');
7
-
8
-
9
-
10
- /**
11
- *
12
- * Embedded file log function
13
- *
14
- * @param string $str text to log in file
15
- * @return none
16
- */
17
- function rlog($str) {
18
- if(get_option('resmushit_logs') == 0)
19
- return false;
20
-
21
- // Preserve file size under a reasonable value
22
- if(file_exists('../' . RESMUSHIT_LOGS_PATH)){
23
- if(filesize('../' . RESMUSHIT_LOGS_PATH) > RESMUSHIT_LOGS_MAX_FILESIZE) {
24
- $logtailed = logtail('../' . RESMUSHIT_LOGS_PATH, 20);
25
- $fp = fopen('../' . RESMUSHIT_LOGS_PATH, 'w');
26
- fwrite($fp, $logtailed);
27
- fclose($fp);
28
- }
29
- }
30
-
31
- $str = "[".date('d-m-Y H:i:s')."] " . $str;
32
- $str = print_r($str, true) . "\n";
33
- $fp = fopen('../' . RESMUSHIT_LOGS_PATH, 'a+');
34
- fwrite($fp, $str);
35
- fclose($fp);
36
- }
37
-
38
-
39
- /**
40
- *
41
- * Tail function for files
42
- *
43
- * @param string $filepath path of the file to tail
44
- * @param string $lines number of lines to keep
45
- * @param string $adaptative will preserve line memory
46
- * @return tailed file
47
- * @author Torleif Berger, Lorenzo Stanco
48
- * @link http://stackoverflow.com/a/15025877/995958
49
- * @license http://creativecommons.org/licenses/by/3.0/
50
- */
51
- function logtail($filepath, $lines = 1, $adaptive = true) {
52
-
53
- $f = @fopen($filepath, "rb");
54
- if ($f === false) return false;
55
- if (!$adaptive) $buffer = 4096;
56
- else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
57
- fseek($f, -1, SEEK_END);
58
- if (fread($f, 1) != "\n") $lines -= 1;
59
-
60
- $output = '';
61
- $chunk = '';
62
-
63
- while (ftell($f) > 0 && $lines >= 0) {
64
- $seek = min(ftell($f), $buffer);
65
- fseek($f, -$seek, SEEK_CUR);
66
- $output = ($chunk = fread($f, $seek)) . $output;
67
- fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
68
- $lines -= substr_count($chunk, "\n");
69
- }
70
-
71
- while ($lines++ < 0) {
72
- $output = substr($output, strpos($output, "\n") + 1);
73
- }
74
- fclose($f);
75
- return trim($output);
76
- }
1
+ <?php
2
+
3
+ require('resmushit.settings.php');
4
+ require('classes/resmushit.class.php');
5
+ require('classes/resmushitUI.class.php');
6
+ require('resmushit.admin.php');
7
+
8
+
9
+
10
+ /**
11
+ *
12
+ * Embedded file log function
13
+ *
14
+ * @param string $str text to log in file
15
+ * @return none
16
+ */
17
+ function rlog($str) {
18
+ if(get_option('resmushit_logs') == 0)
19
+ return false;
20
+
21
+ // Preserve file size under a reasonable value
22
+ if(file_exists('../' . RESMUSHIT_LOGS_PATH)){
23
+ if(filesize('../' . RESMUSHIT_LOGS_PATH) > RESMUSHIT_LOGS_MAX_FILESIZE) {
24
+ $logtailed = logtail('../' . RESMUSHIT_LOGS_PATH, 20);
25
+ $fp = fopen('../' . RESMUSHIT_LOGS_PATH, 'w');
26
+ fwrite($fp, $logtailed);
27
+ fclose($fp);
28
+ }
29
+ }
30
+
31
+ $str = "[".date('d-m-Y H:i:s')."] " . $str;
32
+ $str = print_r($str, true) . "\n";
33
+ $fp = fopen('../' . RESMUSHIT_LOGS_PATH, 'a+');
34
+ fwrite($fp, $str);
35
+ fclose($fp);
36
+ }
37
+
38
+
39
+ /**
40
+ *
41
+ * Tail function for files
42
+ *
43
+ * @param string $filepath path of the file to tail
44
+ * @param string $lines number of lines to keep
45
+ * @param string $adaptative will preserve line memory
46
+ * @return tailed file
47
+ * @author Torleif Berger, Lorenzo Stanco
48
+ * @link http://stackoverflow.com/a/15025877/995958
49
+ * @license http://creativecommons.org/licenses/by/3.0/
50
+ */
51
+ function logtail($filepath, $lines = 1, $adaptive = true) {
52
+
53
+ $f = @fopen($filepath, "rb");
54
+ if ($f === false) return false;
55
+ if (!$adaptive) $buffer = 4096;
56
+ else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
57
+ fseek($f, -1, SEEK_END);
58
+ if (fread($f, 1) != "\n") $lines -= 1;
59
+
60
+ $output = '';
61
+ $chunk = '';
62
+
63
+ while (ftell($f) > 0 && $lines >= 0) {
64
+ $seek = min(ftell($f), $buffer);
65
+ fseek($f, -$seek, SEEK_CUR);
66
+ $output = ($chunk = fread($f, $seek)) . $output;
67
+ fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
68
+ $lines -= substr_count($chunk, "\n");
69
+ }
70
+
71
+ while ($lines++ < 0) {
72
+ $output = substr($output, strpos($output, "\n") + 1);
73
+ }
74
+ fclose($f);
75
+ return trim($output);
76
+ }
resmushit.php CHANGED
@@ -1,247 +1,247 @@
1
- <?php
2
- /**
3
- * @package resmushit
4
- * @author Charles Bourgeaux <hello@resmush.it>
5
- * @license GPL-2.0+
6
- * @link http://www.resmush.it
7
- * @copyright 2018 Resmush.it
8
- *
9
- * @wordpress-plugin
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
18
- * License: GPL-2.0+
19
- * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
20
- * Domain Path: /languages
21
- */
22
-
23
-
24
- require('resmushit.inc.php');
25
-
26
-
27
-
28
-
29
-
30
- /**
31
- *
32
- * Registering language plugin
33
- *
34
- * @param none
35
- * @return none
36
- */
37
- function resmushit_load_plugin_textdomain() {
38
- load_plugin_textdomain( 'resmushit', FALSE, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
39
- }
40
- add_action( 'plugins_loaded', 'resmushit_load_plugin_textdomain' );
41
-
42
-
43
-
44
-
45
-
46
- /**
47
- *
48
- * Registering settings on plugin installation
49
- *
50
- * @param none
51
- * @return none
52
- */
53
- function resmushit_activate() {
54
- if ( is_super_admin() ) {
55
- if(!get_option('resmushit_qlty'))
56
- update_option( 'resmushit_qlty', RESMUSHIT_DEFAULT_QLTY );
57
- if(!get_option('resmushit_on_upload'))
58
- update_option( 'resmushit_on_upload', '1' );
59
- if(!get_option('resmushit_statistics'))
60
- update_option( 'resmushit_statistics', '1' );
61
- if(!get_option('resmushit_total_optimized'))
62
- update_option( 'resmushit_total_optimized', '0' );
63
- }
64
- }
65
- register_activation_hook( __FILE__, 'resmushit_activate' );
66
-
67
-
68
-
69
-
70
-
71
- /**
72
- *
73
- * Call resmush.it optimization for attachments
74
- *
75
- * @param attachment object
76
- * @param boolean preserve original file
77
- * @return attachment object
78
- */
79
- function resmushit_process_images($attachments, $force_keep_original = TRUE) {
80
- global $attachment_id;
81
- $cumulated_original_sizes = 0;
82
- $cumulated_optimized_sizes = 0;
83
- $error = FALSE;
84
-
85
- if(reSmushit::getDisabledState($attachment_id))
86
- return $attachments;
87
-
88
- $basepath = dirname(get_attached_file( $attachment_id )) . '/';
89
- $basefile = basename($attachments[ 'file' ]);
90
-
91
- $statistics[] = reSmushit::optimize($basepath . $basefile, $force_keep_original );
92
-
93
- foreach($attachments['sizes'] as $image_style)
94
- $statistics[] = reSmushit::optimize($basepath . $image_style['file'], FALSE );
95
-
96
- $count = 0;
97
- foreach($statistics as $stat){
98
- if($stat && !isset($stat->error)){
99
- $cumulated_original_sizes += $stat->src_size;
100
- $cumulated_optimized_sizes += $stat->dest_size;
101
- $count++;
102
- } else
103
- $error = TRUE;
104
- }
105
- if(!$error) {
106
- $optimizations_successful_count = get_option('resmushit_total_optimized');
107
- update_option( 'resmushit_total_optimized', $optimizations_successful_count + $count );
108
-
109
- update_post_meta($attachment_id,'resmushed_quality', resmushit::getPictureQualitySetting());
110
- if(get_option('resmushit_statistics')){
111
- update_post_meta($attachment_id,'resmushed_cumulated_original_sizes', $cumulated_original_sizes);
112
- update_post_meta($attachment_id,'resmushed_cumulated_optimized_sizes', $cumulated_optimized_sizes);
113
- }
114
- }
115
- return $attachments;
116
- }
117
- //Automatically optimize images if option is checked
118
- if(get_option('resmushit_on_upload'))
119
- add_filter('wp_generate_attachment_metadata', 'resmushit_process_images');
120
-
121
-
122
-
123
-
124
-
125
-
126
- /**
127
- *
128
- * Delete also -unsmushed file (ie. Original file) when deleting an attachment
129
- *
130
- * @param int postID
131
- * @return none
132
- */
133
- function resmushit_delete_attachment($postid) {
134
- reSmushit::deleteOriginalFile($postid);
135
- }
136
- add_action( 'delete_attachment', 'resmushit_delete_attachment' );
137
-
138
-
139
-
140
-
141
-
142
- /**
143
- *
144
- * Make current attachment available
145
- *
146
- * @param attachment object
147
- * @return attachment object
148
- */
149
- function resmushit_get_meta_id($result){
150
- global $attachment_id;
151
- $attachment_id = $result;
152
- }
153
- //Automatically retrieve image attachment ID if option is checked
154
- if(get_option('resmushit_on_upload'))
155
- add_filter('add_attachment', 'resmushit_get_meta_id');
156
-
157
-
158
-
159
-
160
-
161
- /**
162
- *
163
- * add Ajax action to fetch all unsmushed pictures
164
- *
165
- * @param none
166
- * @return json object
167
- */
168
- function resmushit_bulk_get_images() {
169
- echo reSmushit::getNonOptimizedPictures();
170
- die();
171
- }
172
- add_action( 'wp_ajax_resmushit_bulk_get_images', 'resmushit_bulk_get_images' );
173
-
174
-
175
-
176
-
177
- /**
178
- *
179
- * add Ajax action to change disabled state for an attachment
180
- *
181
- * @param none
182
- * @return json object
183
- */
184
- function resmushit_update_disabled_state() {
185
- if(isset($_POST['data']['id']) && $_POST['data']['id'] != null && isset($_POST['data']['disabled'])){
186
- echo reSmushit::updateDisabledState(sanitize_text_field($_POST['data']['id']), sanitize_text_field($_POST['data']['disabled']));
187
- }
188
- die();
189
- }
190
- add_action( 'wp_ajax_resmushit_update_disabled_state', 'resmushit_update_disabled_state' );
191
-
192
-
193
-
194
-
195
-
196
- /**
197
- *
198
- * add Ajax action to optimize a single attachment in the library
199
- *
200
- * @param none
201
- * @return json object
202
- */
203
- function resmushit_optimize_single_attachment() {
204
- if(isset($_POST['data']['id']) && $_POST['data']['id'] != null){
205
- reSmushit::revert(sanitize_text_field($_POST['data']['id']));
206
- echo json_encode(reSmushit::getStatistics($_POST['data']['id']));
207
- }
208
- die();
209
- }
210
- add_action( 'wp_ajax_resmushit_optimize_single_attachment', 'resmushit_optimize_single_attachment' );
211
-
212
-
213
-
214
-
215
-
216
- /**
217
- *
218
- * add Ajax action to optimize a picture according to attachment ID
219
- *
220
- * @param none
221
- * @return boolean
222
- */
223
- function resmushit_bulk_process_image() {
224
- rlog('Bulk optimization launched for file : ' . get_attached_file( sanitize_text_field($_POST['data']['ID']) ));
225
- echo reSmushit::revert(sanitize_text_field($_POST['data']['ID']));
226
- die();
227
- }
228
- add_action( 'wp_ajax_resmushit_bulk_process_image', 'resmushit_bulk_process_image' );
229
-
230
-
231
-
232
-
233
-
234
- /**
235
- *
236
- * add Ajax action to update statistics
237
- *
238
- * @param none
239
- * @return json object
240
- */
241
- function resmushit_update_statistics() {
242
- $output = reSmushit::getStatistics();
243
- $output['total_saved_size_formatted'] = reSmushitUI::sizeFormat($output['total_saved_size']);
244
- echo json_encode($output);
245
- die();
246
- }
247
  add_action( 'wp_ajax_resmushit_update_statistics', 'resmushit_update_statistics' );
1
+ <?php
2
+ /**
3
+ * @package resmushit
4
+ * @author Charles Bourgeaux <hello@resmush.it>
5
+ * @license GPL-2.0+
6
+ * @link http://www.resmush.it
7
+ * @copyright 2018 Resmush.it
8
+ *
9
+ * @wordpress-plugin
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.17
14
+ * Timestamp: 2018.11.04
15
+ * Author: reSmush.it
16
+ * Author URI: https://resmush.it
17
+ * Author: Charles Bourgeaux
18
+ * License: GPL-2.0+
19
+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
20
+ * Domain Path: /languages
21
+ */
22
+
23
+
24
+ require('resmushit.inc.php');
25
+
26
+
27
+
28
+
29
+
30
+ /**
31
+ *
32
+ * Registering language plugin
33
+ *
34
+ * @param none
35
+ * @return none
36
+ */
37
+ function resmushit_load_plugin_textdomain() {
38
+ load_plugin_textdomain( 'resmushit', FALSE, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
39
+ }
40
+ add_action( 'plugins_loaded', 'resmushit_load_plugin_textdomain' );
41
+
42
+
43
+
44
+
45
+
46
+ /**
47
+ *
48
+ * Registering settings on plugin installation
49
+ *
50
+ * @param none
51
+ * @return none
52
+ */
53
+ function resmushit_activate() {
54
+ if ( is_super_admin() ) {
55
+ if(!get_option('resmushit_qlty'))
56
+ update_option( 'resmushit_qlty', RESMUSHIT_DEFAULT_QLTY );
57
+ if(!get_option('resmushit_on_upload'))
58
+ update_option( 'resmushit_on_upload', '1' );
59
+ if(!get_option('resmushit_statistics'))
60
+ update_option( 'resmushit_statistics', '1' );
61
+ if(!get_option('resmushit_total_optimized'))
62
+ update_option( 'resmushit_total_optimized', '0' );
63
+ }
64
+ }
65
+ register_activation_hook( __FILE__, 'resmushit_activate' );
66
+
67
+
68
+
69
+
70
+
71
+ /**
72
+ *
73
+ * Call resmush.it optimization for attachments
74
+ *
75
+ * @param attachment object
76
+ * @param boolean preserve original file
77
+ * @return attachment object
78
+ */
79
+ function resmushit_process_images($attachments, $force_keep_original = TRUE) {
80
+ global $attachment_id;
81
+ $cumulated_original_sizes = 0;
82
+ $cumulated_optimized_sizes = 0;
83
+ $error = FALSE;
84
+
85
+ if(reSmushit::getDisabledState($attachment_id))
86
+ return $attachments;
87
+
88
+ $basepath = dirname(get_attached_file( $attachment_id )) . '/';
89
+ $basefile = basename($attachments[ 'file' ]);
90
+
91
+ $statistics[] = reSmushit::optimize($basepath . $basefile, $force_keep_original );
92
+
93
+ foreach($attachments['sizes'] as $image_style)
94
+ $statistics[] = reSmushit::optimize($basepath . $image_style['file'], FALSE );
95
+
96
+ $count = 0;
97
+ foreach($statistics as $stat){
98
+ if($stat && !isset($stat->error)){
99
+ $cumulated_original_sizes += $stat->src_size;
100
+ $cumulated_optimized_sizes += $stat->dest_size;
101
+ $count++;
102
+ } else
103
+ $error = TRUE;
104
+ }
105
+ if(!$error) {
106
+ $optimizations_successful_count = get_option('resmushit_total_optimized');
107
+ update_option( 'resmushit_total_optimized', $optimizations_successful_count + $count );
108
+
109
+ update_post_meta($attachment_id,'resmushed_quality', resmushit::getPictureQualitySetting());
110
+ if(get_option('resmushit_statistics')){
111
+ update_post_meta($attachment_id,'resmushed_cumulated_original_sizes', $cumulated_original_sizes);
112
+ update_post_meta($attachment_id,'resmushed_cumulated_optimized_sizes', $cumulated_optimized_sizes);
113
+ }
114
+ }
115
+ return $attachments;
116
+ }
117
+ //Automatically optimize images if option is checked
118
+ if(get_option('resmushit_on_upload') OR ( isset($_POST['action']) AND $_POST['action'] === "resmushit_bulk_process_image" ))
119
+ add_filter('wp_generate_attachment_metadata', 'resmushit_process_images');
120
+
121
+
122
+
123
+
124
+
125
+
126
+ /**
127
+ *
128
+ * Delete also -unsmushed file (ie. Original file) when deleting an attachment
129
+ *
130
+ * @param int postID
131
+ * @return none
132
+ */
133
+ function resmushit_delete_attachment($postid) {
134
+ reSmushit::deleteOriginalFile($postid);
135
+ }
136
+ add_action( 'delete_attachment', 'resmushit_delete_attachment' );
137
+
138
+
139
+
140
+
141
+
142
+ /**
143
+ *
144
+ * Make current attachment available
145
+ *
146
+ * @param attachment object
147
+ * @return attachment object
148
+ */
149
+ function resmushit_get_meta_id($result){
150
+ global $attachment_id;
151
+ $attachment_id = $result;
152
+ }
153
+ //Automatically retrieve image attachment ID if option is checked
154
+ if(get_option('resmushit_on_upload'))
155
+ add_filter('add_attachment', 'resmushit_get_meta_id');
156
+
157
+
158
+
159
+
160
+
161
+ /**
162
+ *
163
+ * add Ajax action to fetch all unsmushed pictures
164
+ *
165
+ * @param none
166
+ * @return json object
167
+ */
168
+ function resmushit_bulk_get_images() {
169
+ echo reSmushit::getNonOptimizedPictures();
170
+ die();
171
+ }
172
+ add_action( 'wp_ajax_resmushit_bulk_get_images', 'resmushit_bulk_get_images' );
173
+
174
+
175
+
176
+
177
+ /**
178
+ *
179
+ * add Ajax action to change disabled state for an attachment
180
+ *
181
+ * @param none
182
+ * @return json object
183
+ */
184
+ function resmushit_update_disabled_state() {
185
+ if(isset($_POST['data']['id']) && $_POST['data']['id'] != null && isset($_POST['data']['disabled'])){
186
+ echo reSmushit::updateDisabledState(sanitize_text_field($_POST['data']['id']), sanitize_text_field($_POST['data']['disabled']));
187
+ }
188
+ die();
189
+ }
190
+ add_action( 'wp_ajax_resmushit_update_disabled_state', 'resmushit_update_disabled_state' );
191
+
192
+
193
+
194
+
195
+
196
+ /**
197
+ *
198
+ * add Ajax action to optimize a single attachment in the library
199
+ *
200
+ * @param none
201
+ * @return json object
202
+ */
203
+ function resmushit_optimize_single_attachment() {
204
+ if(isset($_POST['data']['id']) && $_POST['data']['id'] != null){
205
+ reSmushit::revert(sanitize_text_field($_POST['data']['id']));
206
+ echo json_encode(reSmushit::getStatistics($_POST['data']['id']));
207
+ }
208
+ die();
209
+ }
210
+ add_action( 'wp_ajax_resmushit_optimize_single_attachment', 'resmushit_optimize_single_attachment' );
211
+
212
+
213
+
214
+
215
+
216
+ /**
217
+ *
218
+ * add Ajax action to optimize a picture according to attachment ID
219
+ *
220
+ * @param none
221
+ * @return boolean
222
+ */
223
+ function resmushit_bulk_process_image() {
224
+ rlog('Bulk optimization launched for file : ' . get_attached_file( sanitize_text_field($_POST['data']['ID']) ));
225
+ echo reSmushit::revert(sanitize_text_field($_POST['data']['ID']));
226
+ die();
227
+ }
228
+ add_action( 'wp_ajax_resmushit_bulk_process_image', 'resmushit_bulk_process_image' );
229
+
230
+
231
+
232
+
233
+
234
+ /**
235
+ *
236
+ * add Ajax action to update statistics
237
+ *
238
+ * @param none
239
+ * @return json object
240
+ */
241
+ function resmushit_update_statistics() {
242
+ $output = reSmushit::getStatistics();
243
+ $output['total_saved_size_formatted'] = reSmushitUI::sizeFormat($output['total_saved_size']);
244
+ echo json_encode($output);
245
+ die();
246
+ }
247
  add_action( 'wp_ajax_resmushit_update_statistics', 'resmushit_update_statistics' );
resmushit.settings.php CHANGED
@@ -1,11 +1,11 @@
1
- <?php
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__ ));
1
+ <?php
2
+
3
+
4
+ define('RESMUSHIT_ENDPOINT', 'http://api.resmush.it/');
5
+ define('RESMUSHIT_VERSION', '0.1.17');
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__ ));