ShortPixel Image Optimizer - Version 2.0.4

Version Description

  • fixed recursive backup directory size counter
  • added backup with subdirectory structure to handle many files
  • empty backup can handle subdirectories & sets the right flag for backup restore
  • latest images are optimized first
  • check for missing images on disk but still linked in DB
Download this release

Release Info

Developer ShortPixel
Plugin Icon 128x128 ShortPixel Image Optimizer
Version 2.0.4
Comparing to
See all releases

Code changes from version 2.0.3 to 2.0.4

Files changed (3) hide show
  1. readme.txt +10 -2
  2. shortpixel_api.php +31 -4
  3. wp-shortpixel.php +64 -22
readme.txt CHANGED
@@ -3,8 +3,8 @@
3
  Contributors: AlexSP
4
  Tags: picture, optimization, image editor, pngout, upload speed, shortpixel, compression, jpegmini, webp, lossless, cwebp, media, tinypng, jpegtran,image, image optimisation, shrink, picture, photo, optimize photos, compress, performance, tinypng, crunch, pngquant, attachment, optimize, pictures,fast, images, image files, image quality, lossy, upload, kraken, resize, seo, smushit, optipng, kraken image optimizer, ewww, photo optimization, gifsicle, image optimizer, images, krakenio, png, gmagick, image optimize, pdf, pdf optimisation, optimise pdf, shrink pdf, jpg, jpeg, jpg optimisation, optimise jpg, shrink jpg, gif, animated gif, optimise gif
5
  Requires at least: 3.0.0 or higher
6
- Tested up to: 4.1
7
- Stable tag: 2.0.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -107,6 +107,14 @@ The ShortPixel team is here to help. <a href="https://shortpixel.com/contact">Co
107
 
108
  == Changelog ==
109
 
 
 
 
 
 
 
 
 
110
  = 2.0.3 =
111
 
112
  * added extra check for bad server responses
3
  Contributors: AlexSP
4
  Tags: picture, optimization, image editor, pngout, upload speed, shortpixel, compression, jpegmini, webp, lossless, cwebp, media, tinypng, jpegtran,image, image optimisation, shrink, picture, photo, optimize photos, compress, performance, tinypng, crunch, pngquant, attachment, optimize, pictures,fast, images, image files, image quality, lossy, upload, kraken, resize, seo, smushit, optipng, kraken image optimizer, ewww, photo optimization, gifsicle, image optimizer, images, krakenio, png, gmagick, image optimize, pdf, pdf optimisation, optimise pdf, shrink pdf, jpg, jpeg, jpg optimisation, optimise jpg, shrink jpg, gif, animated gif, optimise gif
5
  Requires at least: 3.0.0 or higher
6
+ Tested up to: 4.1.1
7
+ Stable tag: 2.0.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
107
 
108
  == Changelog ==
109
 
110
+ = 2.0.4 =
111
+
112
+ * fixed recursive backup directory size counter
113
+ * added backup with subdirectory structure to handle many files
114
+ * empty backup can handle subdirectories & sets the right flag for backup restore
115
+ * latest images are optimized first
116
+ * check for missing images on disk but still linked in DB
117
+
118
  = 2.0.3 =
119
 
120
  * added extra check for bad server responses
shortpixel_api.php CHANGED
@@ -169,22 +169,49 @@ class shortpixel_api {
169
  //if backup is enabled
170
  if(get_option('wp-short-backup_images')) {
171
 
 
 
 
172
  if(!file_exists(SP_BACKUP_FOLDER) && !mkdir(SP_BACKUP_FOLDER, 0777, true)) {
173
  return sprintf("Backup folder does not exist and it could not be created");
174
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
- $source = $filePath;
177
- $destination = SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($source);
178
 
179
  if(is_writable(SP_BACKUP_FOLDER)) {
180
- if(!file_exists($destination)) {
181
- @copy($source, $destination);
 
 
 
 
 
 
182
  }
183
  } else {
184
  return sprintf("Backup folder exists but is not writable");
185
  }
186
  }
187
 
 
 
 
 
 
188
  @unlink( $filePath );
189
  $success = @rename( $tempFile, $filePath );
190
 
169
  //if backup is enabled
170
  if(get_option('wp-short-backup_images')) {
171
 
172
+ $imageIndex = 0;
173
+ $uploadDir = wp_upload_dir();
174
+
175
  if(!file_exists(SP_BACKUP_FOLDER) && !mkdir(SP_BACKUP_FOLDER, 0777, true)) {
176
  return sprintf("Backup folder does not exist and it could not be created");
177
  }
178
+ $meta = wp_get_attachment_metadata($ID);
179
+ $source[$imageIndex] = $filePath;
180
+
181
+ //create destination dir if it isn't already created
182
+ @mkdir( SP_BACKUP_FOLDER . $uploadDir['subdir'], 0777, true);
183
+
184
+ $destination[$imageIndex] = SP_BACKUP_FOLDER . $uploadDir['subdir'] . DIRECTORY_SEPARATOR . basename($source[$imageIndex]);
185
+
186
+ foreach ( $meta['sizes'] as $pictureDetails )
187
+ {
188
+ $imageIndex++;
189
+ $source[$imageIndex] = $uploadDir['path'] . DIRECTORY_SEPARATOR . $pictureDetails['file'];
190
+ $destination[$imageIndex] = SP_BACKUP_FOLDER . $uploadDir['subdir'] . DIRECTORY_SEPARATOR . basename($source[$imageIndex]);
191
+
192
+ }
193
 
 
 
194
 
195
  if(is_writable(SP_BACKUP_FOLDER)) {
196
+ if(!file_exists($destination[0]))
197
+ {
198
+ foreach ( $source as $imageIndex => $fileSource )
199
+ {
200
+ $fileDestination = $destination[$imageIndex];
201
+ @copy($fileSource, $fileDestination);
202
+ }
203
+
204
  }
205
  } else {
206
  return sprintf("Backup folder exists but is not writable");
207
  }
208
  }
209
 
210
+
211
+
212
+ /////////////////////////
213
+
214
+
215
  @unlink( $filePath );
216
  $success = @rename( $tempFile, $filePath );
217
 
wp-shortpixel.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: ShortPixel Image Optimiser
4
  * Plugin URI: https://shortpixel.com/
5
  * Description: ShortPixel is an image compression tool that helps improve your website performance. The plugin optimises images automatically using both lossy and lossless compression. Resulting, smaller, images are no different in quality from the original. To install: 1) Click the "Activate" link to the left of this description. 2) <a href="https://shortpixel.com/wp-apikey" target="_blank">Free Sign up</a> for your unique API Key . 3) Check your email for your API key. 4) Use your API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress. 5) Done!
6
- * Version: 2.0.3
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
@@ -35,14 +35,14 @@ class WPShortPixel {
35
 
36
  //add hook for image upload processing
37
  add_filter( 'wp_generate_attachment_metadata', array( &$this, 'handleImageUpload' ), 10, 2 );
38
- add_filter( 'manage_media_columns', array( &$this, 'columns' ) );
39
- add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'generatePluginLinks'));
40
 
41
  //add_action( 'admin_footer', array(&$this, 'handleImageProcessing'));
42
- add_action( 'manage_media_custom_column', array( &$this, 'generateCustomColumn' ), 10, 2 );
43
 
44
  //add settings page
45
- add_action( 'admin_menu', array( &$this, 'registerSettingsPage' ) );
46
  add_action( 'admin_menu', array( &$this, 'registerAdminPage' ) );
47
  add_action( 'delete_attachment', array( &$this, 'handleDeleteAttachmentInBackup' ) );
48
 
@@ -175,13 +175,14 @@ class WPShortPixel {
175
  echo "Missing API Key";
176
  die();
177
  }
178
-
179
  //query database for first found entry that needs processing
180
  global $wpdb;
181
  $qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
182
- WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%'
183
- OR meta_value LIKE '%\"BulkProcessing\";b:1;%'
184
- LIMIT " . BATCH_SIZE;
 
 
185
  $idList = $wpdb->get_results($qry);
186
 
187
  if(empty($idList)) { echo 'Empty queue'; die; }
@@ -192,16 +193,18 @@ class WPShortPixel {
192
  $imagePath = get_attached_file($ID);
193
  $meta = wp_get_attachment_metadata($ID);
194
 
195
- //check if image is public
196
- if(wp_remote_retrieve_response_code($imageURL) > 400) {
197
  if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
198
- if(isset($met['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
 
199
  wp_update_attachment_metadata($ID, $meta);
200
  die;
201
  }
202
 
203
  $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
204
 
 
205
  if(is_string($result)) {
206
  if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
207
  if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
@@ -273,19 +276,21 @@ class WPShortPixel {
273
  $uploadFilePath = get_attached_file($attachmentID);
274
  $meta = wp_get_attachment_metadata($attachmentID);
275
  $pathInfo = pathinfo($uploadFilePath);
 
276
 
277
  try {
278
  //main file
279
- @rename(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($uploadFilePath), $uploadFilePath);
 
280
  //overwriting thumbnails
281
  if(is_array($meta["sizes"])) {
282
  foreach($meta["sizes"] as $size => $imageData) {
283
- $source = SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $imageData['file'];
284
  $destination = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $imageData['file'];
285
  @rename($source, $destination);
286
  }
287
  }
288
-
289
  unset($meta["ShortPixelImprovement"]);
290
  wp_update_attachment_metadata($attachmentID, $meta);
291
 
@@ -535,15 +540,32 @@ class WPShortPixel {
535
  }
536
  }
537
 
 
 
538
  if(isset($_POST['emptyBackup'])) {
539
  if(file_exists(SP_BACKUP_FOLDER)) {
540
- $files = scandir(SP_BACKUP_FOLDER);
541
- $cleanPath = rtrim(SP_BACKUP_FOLDER, '/'). '/';
542
- foreach($files as $t) {
543
- if ( $t != "." && $t != "..") {
544
- unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $t);
545
- }
 
 
 
 
 
 
 
 
 
 
 
 
546
  }
 
 
 
547
  }
548
  }
549
 
@@ -777,14 +799,19 @@ HTML;
777
  }
778
 
779
  print $data['ShortPixelImprovement'];
780
- if(is_numeric($data['ShortPixelImprovement'])) {
781
  print '%';
782
  print " | <a href=\"admin.php?action=shortpixel_restore_backup&amp;attachment_ID={$id}\">Restore backup</a>";
783
  return;
784
  }
 
 
785
  } elseif(isset($data['ShortPixel']['WaitingProcessing'])) {
786
  print 'Image waiting to be processed';
787
  return;
 
 
 
788
  } else {
789
  if ( wp_attachment_is_image( $id ) ) {
790
  print 'Image not processed';
@@ -848,6 +875,21 @@ HTML;
848
 
849
  }
850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
  static public function folderSize($path) {
852
  $total_size = 0;
853
  if(file_exists($path)) {
3
  * Plugin Name: ShortPixel Image Optimiser
4
  * Plugin URI: https://shortpixel.com/
5
  * Description: ShortPixel is an image compression tool that helps improve your website performance. The plugin optimises images automatically using both lossy and lossless compression. Resulting, smaller, images are no different in quality from the original. To install: 1) Click the "Activate" link to the left of this description. 2) <a href="https://shortpixel.com/wp-apikey" target="_blank">Free Sign up</a> for your unique API Key . 3) Check your email for your API key. 4) Use your API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress. 5) Done!
6
+ * Version: 2.0.4
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
35
 
36
  //add hook for image upload processing
37
  add_filter( 'wp_generate_attachment_metadata', array( &$this, 'handleImageUpload' ), 10, 2 );
38
+ add_filter( 'manage_media_columns', array( &$this, 'columns' ) );//add media library column header
39
+ add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'generatePluginLinks'));//for plugin settings page
40
 
41
  //add_action( 'admin_footer', array(&$this, 'handleImageProcessing'));
42
+ add_action( 'manage_media_custom_column', array( &$this, 'generateCustomColumn' ), 10, 2 );//generate the media library column
43
 
44
  //add settings page
45
+ add_action( 'admin_menu', array( &$this, 'registerSettingsPage' ) );//display SP in Settings menu
46
  add_action( 'admin_menu', array( &$this, 'registerAdminPage' ) );
47
  add_action( 'delete_attachment', array( &$this, 'handleDeleteAttachmentInBackup' ) );
48
 
175
  echo "Missing API Key";
176
  die();
177
  }
 
178
  //query database for first found entry that needs processing
179
  global $wpdb;
180
  $qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
181
+ WHERE (
182
+ meta_value LIKE '%\"WaitingProcessing\";b:1;%'
183
+ OR meta_value LIKE '%\"BulkProcessing\";b:1;%' )
184
+ ORDER BY post_id DESC
185
+ LIMIT " . BATCH_SIZE; //add also meta_key='_wp_attachment_metadata' AND ?
186
  $idList = $wpdb->get_results($qry);
187
 
188
  if(empty($idList)) { echo 'Empty queue'; die; }
193
  $imagePath = get_attached_file($ID);
194
  $meta = wp_get_attachment_metadata($ID);
195
 
196
+ //check if the image file exists on disk, if not set the right params
197
+ if(!file_exists($imagePath)) {
198
  if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
199
+ if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
200
+ $meta['ShortPixel']['NoFileOnDisk'] = true;
201
  wp_update_attachment_metadata($ID, $meta);
202
  die;
203
  }
204
 
205
  $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
206
 
207
+
208
  if(is_string($result)) {
209
  if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
210
  if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
276
  $uploadFilePath = get_attached_file($attachmentID);
277
  $meta = wp_get_attachment_metadata($attachmentID);
278
  $pathInfo = pathinfo($uploadFilePath);
279
+ $SubDirs = substr($meta['file'],0,strrpos($meta['file'],"/")+1);
280
 
281
  try {
282
  //main file
283
+ @rename(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $SubDirs . basename($uploadFilePath), $uploadFilePath);
284
+
285
  //overwriting thumbnails
286
  if(is_array($meta["sizes"])) {
287
  foreach($meta["sizes"] as $size => $imageData) {
288
+ $source = SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $SubDirs . $imageData['file'];
289
  $destination = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $imageData['file'];
290
  @rename($source, $destination);
291
  }
292
  }
293
+
294
  unset($meta["ShortPixelImprovement"]);
295
  wp_update_attachment_metadata($attachmentID, $meta);
296
 
540
  }
541
  }
542
 
543
+
544
+ //empty backup
545
  if(isset($_POST['emptyBackup'])) {
546
  if(file_exists(SP_BACKUP_FOLDER)) {
547
+
548
+ //extract all images from DB in an array. of course
549
+ $attachments = null;
550
+ $attachments = get_posts( array(
551
+ 'numberposts' => -1,
552
+ 'post_type' => 'attachment',
553
+ 'post_mime_type' => 'image'
554
+ ));
555
+
556
+
557
+ //parse all images and set the right flag that the image has no backup
558
+ foreach($attachments as $attachment)
559
+ {
560
+ if(self::isProcesable(get_attached_file($attachment->ID)) == false) continue;
561
+
562
+ $meta = wp_get_attachment_metadata($attachment->ID);
563
+ $meta['ShortPixel']['NoBackup'] = true;
564
+ wp_update_attachment_metadata($attachment->ID, $meta);
565
  }
566
+
567
+ //delete the actual files on disk
568
+ $this->deleteDir(SP_BACKUP_FOLDER);//call a recursive function to empty files and sub-dirs in backup dir
569
  }
570
  }
571
 
799
  }
800
 
801
  print $data['ShortPixelImprovement'];
802
+ if( is_numeric($data['ShortPixelImprovement']) && !$data['ShortPixel']['NoBackup'] ) {
803
  print '%';
804
  print " | <a href=\"admin.php?action=shortpixel_restore_backup&amp;attachment_ID={$id}\">Restore backup</a>";
805
  return;
806
  }
807
+ else
808
+ print '%';
809
  } elseif(isset($data['ShortPixel']['WaitingProcessing'])) {
810
  print 'Image waiting to be processed';
811
  return;
812
+ } elseif(isset($data['ShortPixel']['NoFileOnDisk'])) {
813
+ print 'Image does not exist';
814
+ return;
815
  } else {
816
  if ( wp_attachment_is_image( $id ) ) {
817
  print 'Image not processed';
875
 
876
  }
877
 
878
+ public static function deleteDir($dirPath) {
879
+ if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
880
+ $dirPath .= '/';
881
+ }
882
+ $files = glob($dirPath . '*', GLOB_MARK);
883
+ foreach ($files as $file) {
884
+ if (is_dir($file)) {
885
+ self::deleteDir($file);
886
+ @rmdir($file);//remove empty dir
887
+ } else {
888
+ @unlink($file);//remove file
889
+ }
890
+ }
891
+ }
892
+
893
  static public function folderSize($path) {
894
  $total_size = 0;
895
  if(file_exists($path)) {