reSmush.it Image Optimizer - Version 0.3.0

Version Description

  • Add Backup deletion option
  • Add script to delete old backups
  • Changed JS inclusion
Download this release

Release Info

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

Code changes from version 0.2.5 to 0.3.0

classes/resmushit.class.php CHANGED
@@ -95,7 +95,7 @@ Class reSmushit {
95
  curl_close($ch);
96
  }
97
  if ($data) {
98
- if($is_original){
99
  $originalFile = pathinfo($file_path);
100
  $newPath = $originalFile['dirname'] . '/' . $originalFile['filename'] . '-unsmushed.' . $originalFile['extension'];
101
  copy($file_path, $newPath);
@@ -166,7 +166,24 @@ Class reSmushit {
166
  unlink($originalFile);
167
  }
168
 
169
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  /**
171
  *
172
  * Return optimization statistics
95
  curl_close($ch);
96
  }
97
  if ($data) {
98
+ if($is_original && get_option('resmushit_remove_unsmushed') == 0){
99
  $originalFile = pathinfo($file_path);
100
  $newPath = $originalFile['dirname'] . '/' . $originalFile['filename'] . '-unsmushed.' . $originalFile['extension'];
101
  copy($file_path, $newPath);
166
  unlink($originalFile);
167
  }
168
 
169
+ /**
170
+ *
171
+ * Detect if optimization process was already launched one time
172
+ *
173
+ * @return boolean
174
+ */
175
+ public static function hasAlreadyRunOnce(){
176
+ global $wpdb;
177
+ $query = $wpdb->prepare(
178
+ "select
179
+ count($wpdb->posts.ID) as count
180
+ from $wpdb->posts
181
+ inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s $extraSQL
182
+ limit 1",
183
+ array('resmushed_cumulated_original_sizes')
184
+ );
185
+ return (boolean)$wpdb->get_var($query);
186
+ }
187
  /**
188
  *
189
  * Return optimization statistics
classes/resmushitUI.class.php CHANGED
@@ -101,6 +101,7 @@ Class reSmushitUI {
101
  . self::addSetting("checkbox", __("Enable logs", 'resmushit-image-optimizer'), __("Enable file logging (for developers)", 'resmushit-image-optimizer'), "resmushit_logs")
102
  . self::addSetting("checkbox", $new_label . __("Process optimize on CRON", 'resmushit-image-optimizer'), __("Will perform image optimization process through CRON tasks", 'resmushit-image-optimizer'), "resmushit_cron")
103
  . self::addSetting("checkbox", $new_label . __("Preserve EXIF", 'resmushit-image-optimizer'), __("Will preserve EXIF data during optimization", 'resmushit-image-optimizer'), "resmushit_preserve_exif")
 
104
  . '</table>';
105
  submit_button();
106
  echo '</form></div>';
@@ -341,36 +342,63 @@ Class reSmushitUI {
341
  * @return none
342
  */
343
  public static function alertPanel() {
344
- if (resmushit_get_cron_status() == 'DISABLED' || resmushit_get_cron_status() == 'OK') {
 
 
 
345
  return TRUE;
346
  }
347
 
348
  self::fullWidthPanelWrapper(__('Important informations', 'resmushit-image-optimizer'), null, 'red');
349
- echo "<div class='rsmt-alert'>";
350
-
351
-
352
- echo "<h3 class='icon_message warning'>"
353
- . __('Cronjobs seems incorrectly configured', 'resmushit-image-optimizer')
354
- . "</h3>";
355
-
356
- if (resmushit_get_cron_status() == 'MISCONFIGURED') {
357
- echo "<p>"
358
- . __('Cronjobs are not correctly configured. The variable <em>DISABLE_WP_CRON</em> must be set to <em>TRUE</em> in <em>wp-config.php</em>. Please install them by reading the following <a href="https://resmush.it/wordpress/howto-configure-cronjobs" target="_blank">instruction page</a>.', 'resmushit-image-optimizer')
359
- . "</p><p>"
360
- . __('We advice to disable Remush.it option "Process optimize on CRON" as long as Cron jobs are incorrectly set up.', 'resmushit-image-optimizer')
361
- . "</p>";
362
- } else if (resmushit_get_cron_status() == 'NEVER_RUN') {
363
- echo "<p>"
364
- . __('Cronjobs seems to have never been launched. Please install them by reading the following <a href="https://resmush.it/wordpress/howto-configure-cronjobs" target="_blank">instruction page</a>.', 'resmushit-image-optimizer')
365
- . "</p>";
366
- } else if (resmushit_get_cron_status() == 'NO_LATELY_RUN') {
367
- echo "<p>"
368
- . __('Cronjobs seems not to have run lately. Please read the following <a href="https://resmush.it/wordpress/howto-configure-cronjobs" target="_blank">instruction page</a> to install them correctly.', 'resmushit-image-optimizer')
369
- . "<ul><li><em>" . __('Expected Frequency :', 'resmushit-image-optimizer') . "</em> " . __('Every', 'resmushit-image-optimizer') . " " . time_elapsed_string(RESMUSHIT_CRON_FREQUENCY) . "</li>"
370
- . "<ul><li><em>" . __('Last run :', 'resmushit-image-optimizer') . "</em> " . time_elapsed_string(time() - get_option('resmushit_cron_lastrun')) . " " . __('ago', 'resmushit-image-optimizer') . "</li>"
371
- . "</p>";
 
 
 
372
  }
373
- echo "</div>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
 
375
  self::fullWidthPanelEndWrapper();
376
  }
101
  . self::addSetting("checkbox", __("Enable logs", 'resmushit-image-optimizer'), __("Enable file logging (for developers)", 'resmushit-image-optimizer'), "resmushit_logs")
102
  . self::addSetting("checkbox", $new_label . __("Process optimize on CRON", 'resmushit-image-optimizer'), __("Will perform image optimization process through CRON tasks", 'resmushit-image-optimizer'), "resmushit_cron")
103
  . self::addSetting("checkbox", $new_label . __("Preserve EXIF", 'resmushit-image-optimizer'), __("Will preserve EXIF data during optimization", 'resmushit-image-optimizer'), "resmushit_preserve_exif")
104
+ . self::addSetting("checkbox", $new_label . __("Do not preserve backups", 'resmushit-image-optimizer'), sprintf(__("Will not preserve a backup of the original file (save space). <a href='%s' title='Should I remove backups?' target='_blank'>Read instructions</a> carefully before enabling.", 'resmushit-image-optimizer'), 'https://resmush.it/wordpress/why-keeping-backup-files'), "resmushit_remove_unsmushed")
105
  . '</table>';
106
  submit_button();
107
  echo '</form></div>';
342
  * @return none
343
  */
344
  public static function alertPanel() {
345
+ if (
346
+ ( get_option('resmushit_remove_unsmushed') == 0
347
+ || (get_option('resmushit_remove_unsmushed') == 1 && get_option('resmushit_has_no_backup_files') == 1))
348
+ && (resmushit_get_cron_status() == 'DISABLED' || resmushit_get_cron_status() == 'OK')) {
349
  return TRUE;
350
  }
351
 
352
  self::fullWidthPanelWrapper(__('Important informations', 'resmushit-image-optimizer'), null, 'red');
353
+
354
+ if(resmushit_get_cron_status() != 'DISABLED' && resmushit_get_cron_status() != 'OK') {
355
+
356
+ echo "<div class='rsmt-alert'>";
357
+ echo "<h3 class='icon_message warning'>"
358
+ . __('Cronjobs seems incorrectly configured', 'resmushit-image-optimizer')
359
+ . "</h3>";
360
+
361
+ if (resmushit_get_cron_status() == 'MISCONFIGURED') {
362
+ echo "<p>"
363
+ . __('Cronjobs are not correctly configured. The variable <em>DISABLE_WP_CRON</em> must be set to <em>TRUE</em> in <em>wp-config.php</em>. Please install them by reading the following <a href="https://resmush.it/wordpress/howto-configure-cronjobs" target="_blank">instruction page</a>.', 'resmushit-image-optimizer')
364
+ . "</p><p>"
365
+ . __('We advice to disable Remush.it option "Process optimize on CRON" as long as Cron jobs are incorrectly set up.', 'resmushit-image-optimizer')
366
+ . "</p>";
367
+ } else if (resmushit_get_cron_status() == 'NEVER_RUN') {
368
+ echo "<p>"
369
+ . __('Cronjobs seems to have never been launched. Please install them by reading the following <a href="https://resmush.it/wordpress/howto-configure-cronjobs" target="_blank">instruction page</a>.', 'resmushit-image-optimizer')
370
+ . "</p>";
371
+ } else if (resmushit_get_cron_status() == 'NO_LATELY_RUN') {
372
+ echo "<p>"
373
+ . __('Cronjobs seems not to have run lately. Please read the following <a href="https://resmush.it/wordpress/howto-configure-cronjobs" target="_blank">instruction page</a> to install them correctly.', 'resmushit-image-optimizer')
374
+ . "<ul><li><em>" . __('Expected Frequency :', 'resmushit-image-optimizer') . "</em> " . __('Every', 'resmushit-image-optimizer') . " " . time_elapsed_string(RESMUSHIT_CRON_FREQUENCY) . "</li>"
375
+ . "<li><em>" . __('Last run :', 'resmushit-image-optimizer') . "</em> " . time_elapsed_string(time() - get_option('resmushit_cron_lastrun')) . " " . __('ago', 'resmushit-image-optimizer') . "</li></ul>"
376
+ . "</p>";
377
+ }
378
+ echo "</div>";
379
  }
380
+ if(get_option('resmushit_remove_unsmushed') == 1 && get_option('resmushit_has_no_backup_files') == 0) {
381
+ $files_to_delete = count(detect_unsmushed_files());
382
+
383
+ if($files_to_delete) {
384
+ echo "<div class='rsmt-alert'>";
385
+ echo "<h3 class='icon_message warning'>"
386
+ . __('Backup files can be removed.', 'resmushit-image-optimizer')
387
+ . "</h3>";
388
+
389
+ echo
390
+ '<p>'
391
+ . sprintf(__('Keep these files and turn off "Do not preserve backups" option if you want to restore your unoptimized files in the future. Please <a href="%s" title="Should I remove backups? target="_blank">read instructions</a> before clicking.', 'resmushit-image-optimizer'), 'https://resmush.it/wordpress/why-keeping-backup-files')
392
+ . '</p><p>'
393
+
394
+ . sprintf( __( 'We have found %s files ready to be removed', 'resmushit-image-optimizer' ), count(detect_unsmushed_files()) )
395
+ . '</p><p>'
396
+ . '<input type="button" value="'. __('Remove backup files', 'resmushit-image-optimizer') .'" class="rsmt-trigger--remove-backup-files button media-button select-mode-toggle-button" name="resmushit" class="button wp-smush-send" />';
397
+
398
+ echo "</div>";
399
+ }
400
+ }
401
+
402
 
403
  self::fullWidthPanelEndWrapper();
404
  }
js/script.js CHANGED
@@ -21,20 +21,20 @@ jQuery("#rsmt-options-form").submit(function(){
21
  });
22
 
23
 
24
- jQuery( ".list-accordion h4" ).on('click', function(){
25
- if(jQuery(this).parent().hasClass('opened')){
26
- jQuery(".list-accordion ul").slideUp();
27
- jQuery('.list-accordion').removeClass('opened');
28
-
29
- } else {
30
- jQuery(".list-accordion ul").slideDown();
31
- jQuery('.list-accordion').addClass('opened');
32
- }
33
- });
34
 
35
  updateDisabledState();
36
  optimizeSingleAttachment();
37
-
38
 
39
 
40
  /**
@@ -209,4 +209,30 @@ function optimizeSingleAttachment() {
209
  }
210
  );
211
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  }
21
  });
22
 
23
 
24
+ jQuery( ".list-accordion h4" ).on('click', function(){
25
+ if(jQuery(this).parent().hasClass('opened')){
26
+ jQuery(".list-accordion ul").slideUp();
27
+ jQuery('.list-accordion').removeClass('opened');
28
+
29
+ } else {
30
+ jQuery(".list-accordion ul").slideDown();
31
+ jQuery('.list-accordion').addClass('opened');
32
+ }
33
+ });
34
 
35
  updateDisabledState();
36
  optimizeSingleAttachment();
37
+ removeBackupFiles();
38
 
39
 
40
  /**
209
  }
210
  );
211
  });
212
+ }
213
+
214
+
215
+ /**
216
+ * ajax to Optimize a single picture
217
+ */
218
+ function removeBackupFiles() {
219
+ jQuery(document).delegate(".rsmt-trigger--remove-backup-files","mouseup",function(e){
220
+ if ( confirm( "You're about to delete your image backup files. Are you sure to perform this operation ?" ) ) {
221
+
222
+ e.preventDefault();
223
+ var current = this;
224
+ jQuery(current).val('Removing backups...');
225
+ jQuery(current).prop('disabled', true);
226
+ jQuery.post(
227
+ ajaxurl, {
228
+ action: 'resmushit_remove_backup_files'
229
+ },
230
+ function(response) {
231
+ var data = jQuery.parseJSON(response);
232
+ jQuery(current).val(data.success + ' backup files successfully removed');
233
+ setTimeout(function(){ jQuery(current).parent().parent().slideUp() }, 3000);
234
+ }
235
+ );
236
+ }
237
+ });
238
  }
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: 5.3.2
6
- Stable tag: 0.2.5
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -70,6 +70,11 @@ Yes ! Absolutely free, the only restriction is to send images below 5MB.
70
 
71
  == Changelog ==
72
 
 
 
 
 
 
73
  = 0.2.5 =
74
  * Add Preserve Exif Feature
75
 
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: 5.3.2
6
+ Stable tag: 0.3.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
70
 
71
  == Changelog ==
72
 
73
+ = 0.3.0 =
74
+ * Add Backup deletion option
75
+ * Add script to delete old backups
76
+ * Changed JS inclusion
77
+
78
  = 0.2.5 =
79
  * Add Preserve Exif Feature
80
 
resmushit.admin.php CHANGED
@@ -28,6 +28,7 @@ function resmushit_settings_declare() {
28
  register_setting( 'resmushit-settings', 'resmushit_logs' );
29
  register_setting( 'resmushit-settings', 'resmushit_cron' );
30
  register_setting( 'resmushit-settings', 'resmushit_preserve_exif' );
 
31
  }
32
  add_action( 'admin_init', 'resmushit_settings_declare' );
33
 
@@ -154,7 +155,21 @@ function resmushit_register_plugin_assets(){
154
  wp_enqueue_style( 'resmushit-css' );
155
  wp_enqueue_style( 'prefix-style', esc_url_raw( 'https://fonts.googleapis.com/css?family=Roboto+Slab:700' ), array(), null );
156
 
157
- wp_register_script( 'resmushit-js', plugins_url( 'js/script.js', __FILE__ ) );
158
  wp_enqueue_script( 'resmushit-js' );
159
  }
160
  add_action( 'admin_head', 'resmushit_register_plugin_assets' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  register_setting( 'resmushit-settings', 'resmushit_logs' );
29
  register_setting( 'resmushit-settings', 'resmushit_cron' );
30
  register_setting( 'resmushit-settings', 'resmushit_preserve_exif' );
31
+ register_setting( 'resmushit-settings', 'resmushit_remove_unsmushed' );
32
  }
33
  add_action( 'admin_init', 'resmushit_settings_declare' );
34
 
155
  wp_enqueue_style( 'resmushit-css' );
156
  wp_enqueue_style( 'prefix-style', esc_url_raw( 'https://fonts.googleapis.com/css?family=Roboto+Slab:700' ), array(), null );
157
 
158
+ wp_register_script( 'resmushit-js', plugins_url( 'js/script.js?' . hash_file('crc32', dirname(__FILE__) . '/js/script.js'), __FILE__ ) );
159
  wp_enqueue_script( 'resmushit-js' );
160
  }
161
  add_action( 'admin_head', 'resmushit_register_plugin_assets' );
162
+
163
+
164
+
165
+ /**
166
+ *
167
+ * Detect unsmushed files by browsing the library directory
168
+ *
169
+ * @param none
170
+ * @return none
171
+ */
172
+ function detect_unsmushed_files() {
173
+ $wp_upload_dir=wp_upload_dir();
174
+ return glob_recursive($wp_upload_dir['basedir'] . '/*-unsmushed.*');
175
+ }
resmushit.inc.php CHANGED
@@ -134,4 +134,26 @@ function time_elapsed_string($duration, $full = false) {
134
 
135
  if (!$full) $string = array_slice($string, 0, 1);
136
  return $string ? implode(', ', $string) : __('just now', 'resmushit-image-optimizer');
137
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  if (!$full) $string = array_slice($string, 0, 1);
136
  return $string ? implode(', ', $string) : __('just now', 'resmushit-image-optimizer');
137
+ }
138
+
139
+
140
+ /**
141
+ *
142
+ * Find recursively files based on pattern
143
+ *
144
+ * @param string $pattern file search
145
+ * @param boolean $flags
146
+ * @return array
147
+ * @author Mike
148
+ * @link https://www.php.net/manual/en/function.glob.php#106595
149
+ */
150
+ function glob_recursive($pattern, $flags = 0) {
151
+ $files = glob($pattern, $flags);
152
+
153
+ foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
154
+ {
155
+ $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
156
+ }
157
+
158
+ return $files;
159
+ }
resmushit.php CHANGED
@@ -10,8 +10,8 @@
10
  * Plugin Name: reSmush.it Image Optimizer
11
  * Plugin URI: https://wordpress.org/plugins/resmushit-image-optimizer/
12
  * Description: Image Optimization API. Provides image size optimization
13
- * Version: 0.2.5
14
- * Timestamp: 2020.02.15
15
  * Author: reSmush.it
16
  * Author URI: https://resmush.it
17
  * Author: Charles Bourgeaux
@@ -65,6 +65,10 @@ function resmushit_activate() {
65
  update_option( 'resmushit_cron_firstactivation', 0 );
66
  if(!get_option('resmushit_preserve_exif'))
67
  update_option( 'resmushit_preserve_exif', 0 );
 
 
 
 
68
  }
69
  }
70
  register_activation_hook( __FILE__, 'resmushit_activate' );
@@ -370,4 +374,57 @@ function resmushit_get_cron_status() {
370
  return 'NO_LATELY_RUN';
371
  }
372
  return 'OK';
373
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  * Plugin Name: reSmush.it Image Optimizer
11
  * Plugin URI: https://wordpress.org/plugins/resmushit-image-optimizer/
12
  * Description: Image Optimization API. Provides image size optimization
13
+ * Version: 0.3.0
14
+ * Timestamp: 2020.02.16
15
  * Author: reSmush.it
16
  * Author URI: https://resmush.it
17
  * Author: Charles Bourgeaux
65
  update_option( 'resmushit_cron_firstactivation', 0 );
66
  if(!get_option('resmushit_preserve_exif'))
67
  update_option( 'resmushit_preserve_exif', 0 );
68
+ if(!get_option('resmushit_remove_unsmushed'))
69
+ update_option( 'resmushit_remove_unsmushed', 0 );
70
+ if(!get_option('resmushit_has_no_backup_files'))
71
+ update_option( 'resmushit_has_no_backup_files', 0 );
72
  }
73
  }
74
  register_activation_hook( __FILE__, 'resmushit_activate' );
374
  return 'NO_LATELY_RUN';
375
  }
376
  return 'OK';
377
+ }
378
+
379
+
380
+ /**
381
+ * Trigger when the cron are activated for the first time
382
+ * @param mixed old value for cron_activation option
383
+ * @param mixed new value for cron_activation option
384
+ */
385
+
386
+ function resmushit_on_remove_unsmushed_change($old_value, $value) {
387
+ $old_value = (boolean)$old_value;
388
+ $value = (boolean)$value;
389
+ if($old_value == $value) {
390
+ return TRUE;
391
+ } else {
392
+ //if remove backup is activated
393
+ if($value === TRUE) {
394
+ if(!resmushit::hasAlreadyRunOnce()) {
395
+ update_option( 'resmushit_has_no_backup_files', 1);
396
+ } else {
397
+ update_option( 'resmushit_has_no_backup_files', 0);
398
+ }
399
+ } else {
400
+ update_option( 'resmushit_has_no_backup_files', 0);
401
+ }
402
+ }
403
+ }
404
+ add_action('update_option_resmushit_remove_unsmushed', 'resmushit_on_remove_unsmushed_change', 100, 2);
405
+
406
+
407
+
408
+
409
+ /**
410
+ *
411
+ * add Ajax action to remove backups (-unsmushed) of the filesystem
412
+ *
413
+ * @param none
414
+ * @return json object
415
+ */
416
+ function resmushit_remove_backup_files() {
417
+ $files=detect_unsmushed_files();
418
+ $return = array('success' => 0);
419
+
420
+ foreach($files as $f) {
421
+ if(unlink($f)) {
422
+ $return['success']++;
423
+ }
424
+ }
425
+ echo json_encode($return);
426
+ update_option( 'resmushit_has_no_backup_files', 1);
427
+
428
+ die();
429
+ }
430
+ add_action( 'wp_ajax_resmushit_remove_backup_files', 'resmushit_remove_backup_files' );
resmushit.settings.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
 
3
  define('RESMUSHIT_ENDPOINT', 'http://api.resmush.it/');
4
- define('RESMUSHIT_VERSION', '0.2.5');
5
  define('RESMUSHIT_DEFAULT_QLTY', '92');
6
  define('RESMUSHIT_TIMEOUT', '10');
7
  define('RESMUSHIT_LOGS_PATH', 'resmushit.log');
1
  <?php
2
 
3
  define('RESMUSHIT_ENDPOINT', 'http://api.resmush.it/');
4
+ define('RESMUSHIT_VERSION', '0.3.0');
5
  define('RESMUSHIT_DEFAULT_QLTY', '92');
6
  define('RESMUSHIT_TIMEOUT', '10');
7
  define('RESMUSHIT_LOGS_PATH', 'resmushit.log');