reSmush.it Image Optimizer - Version 0.1.15

Version Description

  • Log rotate if file too big
Download this release

Release Info

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

Code changes from version 0.1.14 to 0.1.15

Files changed (4) hide show
  1. readme.txt +4 -1
  2. resmushit.inc.php +76 -25
  3. resmushit.php +246 -246
  4. resmushit.settings.php +10 -9
readme.txt CHANGED
@@ -3,7 +3,7 @@ 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.14
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -66,6 +66,9 @@ Yes ! Absolutely free, the only restriction is to send images below 5MB.
66
 
67
  == Changelog ==
68
 
 
 
 
69
  = 0.1.14 =
70
  * Tested up to Wordpress 4.9.5
71
  * New contributor (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
 
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
74
  * New contributor (resmushit)
resmushit.inc.php CHANGED
@@ -1,25 +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
- $str = "[".date('d-m-Y H:i:s')."] " . $str;
21
- $str = print_r($str, true) . "\n";
22
- $fp = fopen('../' . RESMUSHIT_LOGS_PATH, 'a+');
23
- fwrite($fp, $str);
24
- fclose($fp);
25
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.14
14
- * Timestamp: 2018.04.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'))
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.15
14
+ * Timestamp: 2018.04.16
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' );
resmushit.settings.php CHANGED
@@ -1,10 +1,11 @@
1
- <?php
2
-
3
-
4
- define('RESMUSHIT_ENDPOINT', 'http://api.resmush.it/');
5
- define('RESMUSHIT_VERSION', '0.1.14');
6
- define('RESMUSHIT_DEFAULT_QLTY', '92');
7
- define('RESMUSHIT_TIMEOUT', '5');
8
- define('RESMUSHIT_LOGS_PATH', 'resmushit.log');
9
- define('RESMUSHIT_NEWSFEED', 'http://feed.resmush.it/');
 
10
  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.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__ ));