ShortPixel Image Optimizer - Version 1.6.0

Version Description

  • images' requests for optimization are sent for all sizes upon image upload in media gallery
  • non-image (e.g. PDF files) are ignored now @ bulk processing
  • bulk optimization improved & some bugs fixed.
  • FAQ/Description small changes
Download this release

Release Info

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

Code changes from version 1.5.3 to 1.6.0

Files changed (3) hide show
  1. readme.txt +13 -4
  2. shortpixel_api.php +20 -14
  3. wp-shortpixel.php +76 -56
readme.txt CHANGED
@@ -4,11 +4,11 @@ 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
5
  Requires at least: 3.0.0 or higher
6
  Tested up to: 4.0
7
- Stable tag: 1.5.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
- The ShortPixel plugin reduces your images's size, making your website load faster than before. Image quality is preserved using advanced compression technology.
12
 
13
  == Description ==
14
 
@@ -22,7 +22,7 @@ ShortPixel uses powerful algorithms that enable your website to load faster, use
22
  * **Up to 90% compression rate:** with lossy compression images that were 3MB can crunch to 307Kb, with no before/after differences.
23
  * **Supported formats:** JPG, PNG, GIF (including animated): optimisation applies to JPG, PNG and static GIF. NEW UPDATE: we introduced optimisation for animated GIFs.
24
  * **Backup and restore originals:** if you ever want to return to the original version, images are automatically stored in a backup folder on your hosting servers.
25
- * **Batch image optimisation:** Bulk Optimisation tool now available. Crunch your past image gallery, and downsize your website in minutes.
26
 
27
  On the https://ShortPixel.com website, we offer free access to the ShrtPixel API which you can use for further image optimisation purposes.
28
 
@@ -72,10 +72,12 @@ To get your API key, you must <a href="https://shortpixel.com/wp-apikey">Sign up
72
 
73
  You use the API key in the ShortPixel plugin Settings (don’t forget to click Save Settings). The same API key can be used on multiple websites/blogs.
74
 
75
- = What does Bulk Optimisation mean? =
76
 
77
  The Bulk option makes ShortPixel optimise all your images at once (not one by one). You can do this in the Media > Bulk ShortPixel section by clicking on the **Compress all your images** button.
78
 
 
 
79
  = Are my images safe? =
80
 
81
  Yes, privacy is guaranteed. The ShortPixel encryption process doesn't allow anyone to view your photos.
@@ -105,6 +107,13 @@ The ShortPixel team is here to help. <a href="https://shortpixel.com/contact">Co
105
 
106
  == Changelog ==
107
 
 
 
 
 
 
 
 
108
  = 1.5.1 =
109
 
110
  * readme changes
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
5
  Requires at least: 3.0.0 or higher
6
  Tested up to: 4.0
7
+ Stable tag: 1.6.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
+ The ShortPixel plugin reduces the images' size making your website load faster. Image quality is preserved using advanced compression technology.
12
 
13
  == Description ==
14
 
22
  * **Up to 90% compression rate:** with lossy compression images that were 3MB can crunch to 307Kb, with no before/after differences.
23
  * **Supported formats:** JPG, PNG, GIF (including animated): optimisation applies to JPG, PNG and static GIF. NEW UPDATE: we introduced optimisation for animated GIFs.
24
  * **Backup and restore originals:** if you ever want to return to the original version, images are automatically stored in a backup folder on your hosting servers.
25
+ * **Bulk image optimisation:** Crunch your image gallery, and downsize your website. This feature may take up to several hours, depending on the number and size of existing images.
26
 
27
  On the https://ShortPixel.com website, we offer free access to the ShrtPixel API which you can use for further image optimisation purposes.
28
 
72
 
73
  You use the API key in the ShortPixel plugin Settings (don’t forget to click Save Settings). The same API key can be used on multiple websites/blogs.
74
 
75
+ = How does Bulk Optimisation work? =
76
 
77
  The Bulk option makes ShortPixel optimise all your images at once (not one by one). You can do this in the Media > Bulk ShortPixel section by clicking on the **Compress all your images** button.
78
 
79
+ The batch optimisation may work slower, depending on your existing image gallery. Please be patient and do not close the Wordpress admin while you are rolling the Bulk Processing on your media gallery.
80
+
81
  = Are my images safe? =
82
 
83
  Yes, privacy is guaranteed. The ShortPixel encryption process doesn't allow anyone to view your photos.
107
 
108
  == Changelog ==
109
 
110
+ = 1.6.0 =
111
+
112
+ * images' requests for optimization are sent for all sizes upon image upload in media gallery
113
+ * non-image (e.g. PDF files) are ignored now @ bulk processing
114
+ * bulk optimization improved & some bugs fixed.
115
+ * FAQ/Description small changes
116
+
117
  = 1.5.1 =
118
 
119
  * readme changes
shortpixel_api.php CHANGED
@@ -12,8 +12,7 @@ class shortpixel_api {
12
 
13
  public function setCompressionType($compressionType)
14
  {
15
- if($compressionType == 'lossy') { $this->_compressionType = 1; }
16
- else { $this->_compressionType = 0; }
17
  }
18
 
19
  public function getCompressionType()
@@ -42,12 +41,11 @@ class shortpixel_api {
42
  $this->processImage($url, $filePath, $ID, $time);
43
  }
44
 
45
- public function doRequests($url, $filePath, $ID = null, $time = 0) {
46
  $requestURL = $this->_apiEndPoint . '?key=' . $this->_apiKey . '&lossy=' . $this->_compressionType . '&url=';
47
  $requestURL = $requestURL . urlencode($url);
48
 
49
  $args = array('timeout'=> SP_MAX_TIMEOUT, 'sslverify' => false);
50
-
51
  $response = wp_remote_get($requestURL, $args);
52
 
53
  if(is_object($response) && get_class($response) == 'WP_Error') {
@@ -65,9 +63,18 @@ class shortpixel_api {
65
  }
66
 
67
  //handles the processing of the image using the ShortPixel API
68
- public function processImage($url, $filePath, $ID = null, $time = 0) {
 
 
 
 
 
 
 
 
 
69
 
70
- $response = $this->doRequests($url, $filePath, $ID, $time);
71
 
72
  if(!$response) return $response;
73
 
@@ -75,7 +82,6 @@ class shortpixel_api {
75
  WPShortPixel::log("Response 200 OK");
76
  printf('Web service did not respond. Please try again later.');
77
  return false;
78
- //error
79
  }
80
 
81
  $data = $this->parseResponse($response);
@@ -84,7 +90,7 @@ class shortpixel_api {
84
  case 1:
85
  //handle image has been scheduled
86
  sleep(1);
87
- return $this->processImage($url, $filePath, $ID, $time);
88
  break;
89
  case 2:
90
  //handle image has been processed
@@ -96,7 +102,7 @@ class shortpixel_api {
96
  return 'Wrong API Key</br>';
97
  default:
98
  //handle error
99
- return 'An error occurred while processing this image. Please try uploading it again.</br>';
100
  }
101
 
102
  return $data;
@@ -117,18 +123,18 @@ class shortpixel_api {
117
 
118
  if ( is_wp_error( $tempFile ) ) {
119
  @unlink($tempFile);
120
- return printf("Error downloading file (%s)", $tempFile->get_error_message());
121
  die;
122
  }
123
 
124
  //check response so that download is OK
125
  if(filesize($tempFile) != $correctFileSize) {
126
- return printf("Error downloading file - incorrect file size");
127
  die;
128
  }
129
 
130
  if (!file_exists($tempFile)) {
131
- return printf("Unable to locate downloaded file (%s)", $tempFile);
132
  die;
133
  }
134
 
@@ -136,7 +142,7 @@ class shortpixel_api {
136
  if(get_option('wp-short-backup_images')) {
137
 
138
  if(!file_exists(SP_BACKUP_FOLDER) && !mkdir(SP_BACKUP_FOLDER, 0777, true)) {
139
- return printf("Backup folder does not exist and it could not be created");
140
  }
141
 
142
  $source = $filePath;
@@ -147,7 +153,7 @@ class shortpixel_api {
147
  @copy($source, $destination);
148
  }
149
  } else {
150
- return printf("Backup folder exists but is not writable");
151
  }
152
  }
153
 
12
 
13
  public function setCompressionType($compressionType)
14
  {
15
+ $this->_compressionType = $compressionType;
 
16
  }
17
 
18
  public function getCompressionType()
41
  $this->processImage($url, $filePath, $ID, $time);
42
  }
43
 
44
+ public function doRequests($url, $filePath, $ID = null) {
45
  $requestURL = $this->_apiEndPoint . '?key=' . $this->_apiKey . '&lossy=' . $this->_compressionType . '&url=';
46
  $requestURL = $requestURL . urlencode($url);
47
 
48
  $args = array('timeout'=> SP_MAX_TIMEOUT, 'sslverify' => false);
 
49
  $response = wp_remote_get($requestURL, $args);
50
 
51
  if(is_object($response) && get_class($response) == 'WP_Error') {
63
  }
64
 
65
  //handles the processing of the image using the ShortPixel API
66
+ public function processImage($url, $filePath, $ID = null, $startTime = 0) {
67
+
68
+ if($startTime == 0) { $startTime = time(); }
69
+ if(time() - $startTime > MAX_EXECUTION_TIME) {
70
+ $meta = wp_get_attachment_metadata($ID);
71
+ $meta['ShortPixelImprovement'] = 'Could not determine compression';
72
+ unset($meta['ShortPixel']['WaitingProcessing']);
73
+ wp_update_attachment_metadata($ID, $meta);
74
+ return 'Could not determine compression';
75
+ }
76
 
77
+ $response = $this->doRequests($url, $filePath, $ID);
78
 
79
  if(!$response) return $response;
80
 
82
  WPShortPixel::log("Response 200 OK");
83
  printf('Web service did not respond. Please try again later.');
84
  return false;
 
85
  }
86
 
87
  $data = $this->parseResponse($response);
90
  case 1:
91
  //handle image has been scheduled
92
  sleep(1);
93
+ return $this->processImage($url, $filePath, $ID, $startTime);
94
  break;
95
  case 2:
96
  //handle image has been processed
102
  return 'Wrong API Key</br>';
103
  default:
104
  //handle error
105
+ return 'An error occurred while processing this image. Please try uploading it again.</br>'.$data->Status->Code;
106
  }
107
 
108
  return $data;
123
 
124
  if ( is_wp_error( $tempFile ) ) {
125
  @unlink($tempFile);
126
+ return sprintf("Error downloading file (%s)", $tempFile->get_error_message());
127
  die;
128
  }
129
 
130
  //check response so that download is OK
131
  if(filesize($tempFile) != $correctFileSize) {
132
+ return sprintf("Error downloading file - incorrect file size");
133
  die;
134
  }
135
 
136
  if (!file_exists($tempFile)) {
137
+ return sprintf("Unable to locate downloaded file (%s)", $tempFile);
138
  die;
139
  }
140
 
142
  if(get_option('wp-short-backup_images')) {
143
 
144
  if(!file_exists(SP_BACKUP_FOLDER) && !mkdir(SP_BACKUP_FOLDER, 0777, true)) {
145
+ return sprintf("Backup folder does not exist and it could not be created");
146
  }
147
 
148
  $source = $filePath;
153
  @copy($source, $destination);
154
  }
155
  } else {
156
+ return sprintf("Backup folder exists but is not writable");
157
  }
158
  }
159
 
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: 1.5.3
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
@@ -11,13 +11,13 @@
11
  require_once('shortpixel_api.php');
12
  require_once( ABSPATH . 'wp-admin/includes/image.php' );
13
 
14
- define('SP_DEBUG', false);
15
  define('SP_LOG', false);
16
  define('SP_MAX_TIMEOUT', 10);
17
  define('SP_BACKUP_FOLDER', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ShortpixelBackups');
18
  define('MUST_HAVE_KEY', true);
19
- define('SP_DEBUG', true);
20
  define('BATCH_SIZE', 1);
 
21
 
22
  class WPShortPixel {
23
 
@@ -115,7 +115,7 @@ class WPShortPixel {
115
  var data = { 'action': 'my_action' };
116
  // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
117
  jQuery.post(ajaxurl, data, function(response) {
118
- if(response == 'empty queue') {
119
  console.log('Queue is empty');
120
  } else {
121
  console.log('Server response: ' + response);
@@ -149,7 +149,21 @@ class WPShortPixel {
149
  self::log("Processing image id {$ID}");
150
  $url = wp_get_attachment_url($ID);
151
  $path = get_attached_file($ID);
152
- $this->_apiInterface->doRequests($url, $path, $ID);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  } else {
154
 
155
  }
@@ -182,9 +196,9 @@ class WPShortPixel {
182
  $meta = wp_get_attachment_metadata($ID);
183
 
184
  //check bulk processing
185
- if(isset($bulkLog['toDo'])) {
186
- if(array_key_exists($ID, $bulkLog['toDo'])) {
187
- unset($bulkLog['toDo'][$ID]);
188
  update_option('bulkProcessingLog', $bulkLog);
189
  }
190
  }
@@ -193,9 +207,10 @@ class WPShortPixel {
193
 
194
  if(is_string($result)) {
195
  $meta['ShortPixelImprovement'] = $result;
196
- $bulkLog['toDo'][$ID] = false;
197
  update_option('bulkProcessingLog', $bulkLog);
198
- die;
 
199
  }
200
 
201
  $processThumbnails = get_option('wp-short-process_thumbnails');
@@ -221,10 +236,7 @@ class WPShortPixel {
221
  echo "Processing done succesfully for image #{$ID}";
222
  }
223
 
224
- if(empty($bulkLog['toDo'])) {
225
- $bulkLog['running'] = false;
226
- }
227
-
228
  update_option('bulkProcessingLog', $bulkLog);
229
 
230
  die();
@@ -235,8 +247,20 @@ class WPShortPixel {
235
 
236
  $url = wp_get_attachment_url($attachmentID);
237
  $filePath = get_attached_file($attachmentID);
 
238
 
239
- $this->_apiInterface->processImage($url, $filePath, $attachmentID);
 
 
 
 
 
 
 
 
 
 
 
240
 
241
  // store the referring webpage location
242
  $sendback = wp_get_referer();
@@ -285,16 +309,17 @@ class WPShortPixel {
285
  public function handleDeleteAttachmentInBackup($ID) {
286
  $uploadFilePath = get_attached_file($ID);
287
  $meta = wp_get_attachment_metadata($ID);
288
-
289
- try {
290
- //main file
291
- @unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($uploadFilePath));
292
- //overwriting thumbnails
293
- foreach($meta["sizes"] as $size => $imageData) {
294
- @unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $imageData['file']);
 
 
 
295
  }
296
- } catch(Exception $e) {
297
- //what to do, what to do?
298
  }
299
  }
300
 
@@ -311,11 +336,7 @@ class WPShortPixel {
311
  wp_update_attachment_metadata($attachmentID, $meta);
312
  $imageLog[$attachmentID] = false;
313
  }
314
- $bulkLog = array();
315
- $bulkLog['running'] = true;
316
- $bulkLog['toDo'] = $imageLog;
317
- $bulkLog['total'] = count($imageLog);
318
- update_option('bulkProcessingLog', $bulkLog);
319
  }
320
  }
321
  }
@@ -363,17 +384,14 @@ class WPShortPixel {
363
  wp_update_attachment_metadata($attachment->ID, $meta);
364
  $imageLog[$attachment->ID] = false;
365
  }
366
- $bulkLog = array();
367
- $bulkLog['running'] = true;
368
- $bulkLog['toDo'] = $imageLog;
369
- $bulkLog['total'] = count($imageLog);
370
- update_option('bulkProcessingLog', $bulkLog);
371
  }
372
 
373
- $currentBulkProcessingStatus = get_option('bulkProcessingLog');
374
 
375
- if($currentBulkProcessingStatus) {
376
- if($currentBulkProcessingStatus['running']) {
377
  echo "<p>
378
  Bulk optimisation has started. It may take a while until we process all your images. <BR>The latest status of the processing will be displayed here every 30 seconds.
379
  In the meantime, you can continue using the admin as usual.<BR> However, <b>you musn’t close the WordPress admin</b>, or the bulk processing will stop.
@@ -384,8 +402,8 @@ class WPShortPixel {
384
  </script>
385
  ';
386
 
387
- $imagesLeft = count($currentBulkProcessingStatus["toDo"]);
388
- $totalImages = $currentBulkProcessingStatus['total'];
389
 
390
  echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
391
 
@@ -394,11 +412,10 @@ class WPShortPixel {
394
  <a class="button button-secondary" href="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk&cancel=1">Cancel Processing</a>
395
  ';
396
  } else {
397
- echo "<p>
398
- Bulk optimisation has successfully finished.
399
- </p>";
400
  echo $this->getBulkProcessingForm(count($attachments));
401
- delete_option(bulkProcessingLog);
402
  }
403
  } else {
404
  echo $this->getBulkProcessingForm(count($attachments));
@@ -432,6 +449,7 @@ class WPShortPixel {
432
 
433
  if(isset($_POST['submit']) || isset($_POST['validate'])) {
434
  //handle API Key - common for submit and validate
 
435
  $validityData = $this->getQuotaInformation($_POST['key'], true);
436
  $this->_apiKey = $_POST['key'];
437
  $this->_apiInterface->setApiKey($this->_apiKey);
@@ -509,11 +527,11 @@ Lossless compression: the shrunk image will be identical with the original and s
509
  HTML;
510
 
511
  if($this->_compressionType == 'lossless') {
512
- $formHTML .= '<input type="radio" name="compressionType" value="lossy" >Lossy</br></br>';
513
- $formHTML .= '<input type="radio" name="compressionType" value="lossless" checked>Lossless';
514
  } else {
515
- $formHTML .= '<input type="radio" name="compressionType" value="lossy" checked>Lossy</br></br>';
516
- $formHTML .= '<input type="radio" name="compressionType" value="lossless" >Lossless';
517
  }
518
 
519
  $formHTML .= <<<HTML
@@ -564,6 +582,7 @@ HTML;
564
  $backupFolderSize = self::formatBytes(self::folderSize(SP_BACKUP_FOLDER));
565
 
566
  $statHTML = <<< HTML
 
567
  <h3>ShortPixlel Facts & Figures</h3>
568
  <table class="form-table">
569
  <tbody><tr>
@@ -644,6 +663,9 @@ HTML;
644
 
645
  if($hook_suffix == 'settings_page_wp-shortpixel' && !empty($_POST)) {
646
  $keyCheckData = $this->getQuotaInformation($_POST['key']);
 
 
 
647
  if($keyCheckData['APIKeyValid']) {
648
  echo $divHeader . $noticeCorrectAPIKeyContent . $divFooter;
649
  } else {
@@ -697,24 +719,22 @@ HTML;
697
 
698
  public function generateCustomColumn( $column_name, $id ) {
699
  if( 'wp-shortPixel' == $column_name ) {
700
-
701
  $data = wp_get_attachment_metadata($id);
702
 
703
  if ( isset( $data['ShortPixelImprovement'] ) ) {
704
-
705
  if(isset($meta['ShortPixel']['BulkProcessing'])) {
706
  print 'Waiting for bulk processing';
707
  return;
708
  }
709
 
710
  print $data['ShortPixelImprovement'];
711
- if(is_numeric($data['ShortPixelImprovement'])) print '%';
712
- print " | <a href=\"admin.php?action=shortpixel_manual_optimize&amp;attachment_ID={$id}\">Optimize now</a>";
713
- print " | <a href=\"admin.php?action=shortpixel_restore_backup&amp;attachment_ID={$id}\">Restore backup</a>";
714
- return;
715
- }
716
-
717
- if(isset($data['ShortPixel']['WaitingProcessing'])) {
718
  print 'Image waiting to be processed';
719
  return;
720
  } else {
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: 1.6.0
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
11
  require_once('shortpixel_api.php');
12
  require_once( ABSPATH . 'wp-admin/includes/image.php' );
13
 
14
+ define('SP_DEBUG', true);
15
  define('SP_LOG', false);
16
  define('SP_MAX_TIMEOUT', 10);
17
  define('SP_BACKUP_FOLDER', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ShortpixelBackups');
18
  define('MUST_HAVE_KEY', true);
 
19
  define('BATCH_SIZE', 1);
20
+ define('MAX_EXECUTION_TIME', 30); //in seconds
21
 
22
  class WPShortPixel {
23
 
115
  var data = { 'action': 'my_action' };
116
  // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
117
  jQuery.post(ajaxurl, data, function(response) {
118
+ if(response == 'empty queue' || response == 'error processing image') {
119
  console.log('Queue is empty');
120
  } else {
121
  console.log('Server response: ' + response);
149
  self::log("Processing image id {$ID}");
150
  $url = wp_get_attachment_url($ID);
151
  $path = get_attached_file($ID);
152
+ if(exif_imagetype($path) != false) {
153
+ $this->_apiInterface->doRequests($url, $path, $ID);
154
+
155
+ //send request for thumbs as well
156
+ if($this->_processThumbnails && !empty($meta['sizes'])) {
157
+ foreach($meta['sizes'] as $thumbnailInfo) {
158
+ $thumbURL = str_replace(basename($url), $thumbnailInfo['file'], $url);
159
+ $thumbPath = str_replace(basename($path), $thumbnailInfo['file'], $path);
160
+ $this->_apiInterface->doRequests($thumbURL, $thumbPath);
161
+ }
162
+ }
163
+ } else {
164
+ $meta['ShortPixelImprovement'] = 'File is not an image';
165
+ return $meta;
166
+ }
167
  } else {
168
 
169
  }
196
  $meta = wp_get_attachment_metadata($ID);
197
 
198
  //check bulk processing
199
+ if(isset($bulkLog) && is_array($bulkLog)) {
200
+ if(array_key_exists($ID, $bulkLog)) {
201
+ unset($bulkLog[$ID]);
202
  update_option('bulkProcessingLog', $bulkLog);
203
  }
204
  }
207
 
208
  if(is_string($result)) {
209
  $meta['ShortPixelImprovement'] = $result;
210
+ $bulkLog[$ID] = false;
211
  update_option('bulkProcessingLog', $bulkLog);
212
+ echo "error processing image";
213
+ die();
214
  }
215
 
216
  $processThumbnails = get_option('wp-short-process_thumbnails');
236
  echo "Processing done succesfully for image #{$ID}";
237
  }
238
 
239
+ if(empty($bulkLog)) { $bulkLog = time(); }
 
 
 
240
  update_option('bulkProcessingLog', $bulkLog);
241
 
242
  die();
247
 
248
  $url = wp_get_attachment_url($attachmentID);
249
  $filePath = get_attached_file($attachmentID);
250
+ $meta = wp_get_attachment_metadata($attachmentID);
251
 
252
+ $result = $this->_apiInterface->processImage($url, $filePath, $attachmentID);
253
+
254
+ $processThumbnails = get_option('wp-short-process_thumbnails');
255
+
256
+ //handle the rest of the thumbnails generated by WP
257
+ if($processThumbnails && $result && !empty($meta['sizes'])) {
258
+ foreach($meta['sizes'] as $thumbnailInfo) {
259
+ $thumbURL = str_replace(basename($filePath), $thumbnailInfo['file'], $filePath);
260
+ $thumbPath = str_replace(basename($filePath), $thumbnailInfo['file'], $filePath);
261
+ $this->_apiInterface->processImage($thumbURL, $thumbPath);
262
+ }
263
+ }
264
 
265
  // store the referring webpage location
266
  $sendback = wp_get_referer();
309
  public function handleDeleteAttachmentInBackup($ID) {
310
  $uploadFilePath = get_attached_file($ID);
311
  $meta = wp_get_attachment_metadata($ID);
312
+ if(exif_imagetype($uploadFilePath) != false) {
313
+ try {
314
+ //main file
315
+ @unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($uploadFilePath));
316
+ //overwriting thumbnails
317
+ foreach($meta["sizes"] as $size => $imageData) {
318
+ @unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $imageData['file']);
319
+ }
320
+ } catch(Exception $e) {
321
+ //what to do, what to do?
322
  }
 
 
323
  }
324
  }
325
 
336
  wp_update_attachment_metadata($attachmentID, $meta);
337
  $imageLog[$attachmentID] = false;
338
  }
339
+ update_option('bulkProcessingLog', $imageLog);
 
 
 
 
340
  }
341
  }
342
  }
384
  wp_update_attachment_metadata($attachment->ID, $meta);
385
  $imageLog[$attachment->ID] = false;
386
  }
387
+
388
+ update_option('bulkProcessingLog', $imageLog);
 
 
 
389
  }
390
 
391
+ $bulkProcessingLog = get_option('bulkProcessingLog');
392
 
393
+ if(!empty($bulkProcessingLog)) {
394
+ if(is_array($bulkProcessingLog)) {
395
  echo "<p>
396
  Bulk optimisation has started. It may take a while until we process all your images. <BR>The latest status of the processing will be displayed here every 30 seconds.
397
  In the meantime, you can continue using the admin as usual.<BR> However, <b>you musn’t close the WordPress admin</b>, or the bulk processing will stop.
402
  </script>
403
  ';
404
 
405
+ $imagesLeft = count($bulkProcessingLog);
406
+ $totalImages = count($attachments);
407
 
408
  echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
409
 
412
  <a class="button button-secondary" href="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk&cancel=1">Cancel Processing</a>
413
  ';
414
  } else {
415
+ echo "<p>Bulk optimisation was successful. ShortPixel has finished optimising all your images.</p>
416
+ <p>Go to the ShortPixel <a href='" . get_admin_url() . "options-general.php?page=wp-shortpixel#facts'>Facts &amp; Figures</a> and see your website's optimised stats (in Settings > ShortPixel). </p>";
 
417
  echo $this->getBulkProcessingForm(count($attachments));
418
+ //delete_option('bulkProcessingLog');
419
  }
420
  } else {
421
  echo $this->getBulkProcessingForm(count($attachments));
449
 
450
  if(isset($_POST['submit']) || isset($_POST['validate'])) {
451
  //handle API Key - common for submit and validate
452
+ $_POST['key'] = trim($_POST['key']);
453
  $validityData = $this->getQuotaInformation($_POST['key'], true);
454
  $this->_apiKey = $_POST['key'];
455
  $this->_apiInterface->setApiKey($this->_apiKey);
527
  HTML;
528
 
529
  if($this->_compressionType == 'lossless') {
530
+ $formHTML .= '<input type="radio" name="compressionType" value="1" >Lossy</br></br>';
531
+ $formHTML .= '<input type="radio" name="compressionType" value="0" checked>Lossless';
532
  } else {
533
+ $formHTML .= '<input type="radio" name="compressionType" value="1" checked>Lossy</br></br>';
534
+ $formHTML .= '<input type="radio" name="compressionType" value="0" >Lossless';
535
  }
536
 
537
  $formHTML .= <<<HTML
582
  $backupFolderSize = self::formatBytes(self::folderSize(SP_BACKUP_FOLDER));
583
 
584
  $statHTML = <<< HTML
585
+ <a id="facts"></a>
586
  <h3>ShortPixlel Facts & Figures</h3>
587
  <table class="form-table">
588
  <tbody><tr>
663
 
664
  if($hook_suffix == 'settings_page_wp-shortpixel' && !empty($_POST)) {
665
  $keyCheckData = $this->getQuotaInformation($_POST['key']);
666
+
667
+ if(!isset($_POST['validate'])) return;
668
+
669
  if($keyCheckData['APIKeyValid']) {
670
  echo $divHeader . $noticeCorrectAPIKeyContent . $divFooter;
671
  } else {
719
 
720
  public function generateCustomColumn( $column_name, $id ) {
721
  if( 'wp-shortPixel' == $column_name ) {
 
722
  $data = wp_get_attachment_metadata($id);
723
 
724
  if ( isset( $data['ShortPixelImprovement'] ) ) {
 
725
  if(isset($meta['ShortPixel']['BulkProcessing'])) {
726
  print 'Waiting for bulk processing';
727
  return;
728
  }
729
 
730
  print $data['ShortPixelImprovement'];
731
+ if(is_numeric($data['ShortPixelImprovement'])) {
732
+ print '%';
733
+ print " | <a href=\"admin.php?action=shortpixel_manual_optimize&amp;attachment_ID={$id}\">Optimize now</a>";
734
+ print " | <a href=\"admin.php?action=shortpixel_restore_backup&amp;attachment_ID={$id}\">Restore backup</a>";
735
+ return;
736
+ }
737
+ } elseif(isset($data['ShortPixel']['WaitingProcessing'])) {
738
  print 'Image waiting to be processed';
739
  return;
740
  } else {