ShortPixel Image Optimizer - Version 1.5.0

Version Description

  • pictures are removed from backup as well when deleted
  • restore backup warning/error fixed
  • fixed useless/bad AJAX requests that occured sometimes
  • added user agent to API Key validation for debugging purposes
  • Bulk Processing was freezing for some users, fixed this + added Cancel button
Download this release

Release Info

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

Code changes from version 1.4.1 to 1.5.0

Files changed (2) hide show
  1. readme.txt +9 -1
  2. wp-shortpixel.php +84 -52
readme.txt CHANGED
@@ -4,7 +4,7 @@ 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.4.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -101,6 +101,14 @@ ShortPixel team is here to help. <a href="https://shortpixel.com/contact">Contac
101
 
102
  == Changelog ==
103
 
 
 
 
 
 
 
 
 
104
  = 1.4.1 =
105
 
106
  * optimize again overwrote the original image, fixed
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.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
101
 
102
  == Changelog ==
103
 
104
+ = 1.5.0 =
105
+
106
+ * pictures are removed from backup as well when deleted
107
+ * restore backup warning/error fixed
108
+ * fixed useless/bad AJAX requests that occured sometimes
109
+ * added user agent to API Key validation for debugging purposes
110
+ * Bulk Processing was freezing for some users, fixed this + added Cancel button
111
+
112
  = 1.4.1 =
113
 
114
  * optimize again overwrote the original image, fixed
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.4.1
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
@@ -163,9 +163,14 @@ class WPShortPixel {
163
  die();
164
  }
165
 
 
 
166
  //query database for first found entry that needs processing
167
  global $wpdb;
168
- $qry = "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%' LIMIT " . BATCH_SIZE;
 
 
 
169
  $idList = $wpdb->get_results($qry);
170
 
171
  if(empty($idList)) { echo 'empty queue'; die; }
@@ -176,10 +181,20 @@ class WPShortPixel {
176
  $imagePath = get_attached_file($ID);
177
  $meta = wp_get_attachment_metadata($ID);
178
 
 
 
 
 
 
 
 
 
179
  $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
180
 
181
  if(is_string($result)) {
182
  $meta['ShortPixelImprovement'] = $result;
 
 
183
  die;
184
  }
185
 
@@ -195,27 +210,23 @@ class WPShortPixel {
195
  }
196
 
197
  unset($meta['ShortPixel']['WaitingProcessing']);
 
198
  if(isset($meta['ShortPixel']['BulkProcessing'])) {
199
  unset($meta['ShortPixel']['BulkProcessing']);
200
  }
201
 
202
- //check bulk processing
203
- $bulkLog = get_option('bulkProcessingLog');
204
- if(isset($bulkLog['toDo'])) {
205
- if(array_key_exists($ID, $bulkLog['toDo'])) {
206
- unset($bulkLog['toDo'][$ID]);
207
- }
208
- }
209
-
210
- if(empty($bulkLog['toDo'])) { delete_option('bulkProcessingLog'); }
211
- else { update_option('bulkProcessingLog', $bulkLog); }
212
-
213
  $meta['ShortPixelImprovement'] = $result->PercentImprovement;
214
 
215
  wp_update_attachment_metadata($ID, $meta);
216
  echo "Processing done succesfully for image #{$ID}";
217
  }
218
 
 
 
 
 
 
 
219
  die();
220
  }
221
 
@@ -285,7 +296,6 @@ class WPShortPixel {
285
  } catch(Exception $e) {
286
  //what to do, what to do?
287
  }
288
-
289
  }
290
 
291
  public function bulkOptimizeActionHandler($hook) {
@@ -296,7 +306,6 @@ class WPShortPixel {
296
  //remove all ShortPixel data from metadata
297
  foreach($_GET['media'] as $attachmentID) {
298
  $meta = wp_get_attachment_metadata($attachmentID);
299
- $meta['ShortPixel']['WaitingProcessing'] = true;
300
  $meta['ShortPixel']['BulkProcessing'] = true;
301
  unset($meta['ShortPixelImprovement']);
302
  wp_update_attachment_metadata($attachmentID, $meta);
@@ -323,21 +332,6 @@ class WPShortPixel {
323
  public function bulkProcesss() {
324
  echo '<h1>Bulk Image Optimisation by ShortPixel</h1>';
325
 
326
- echo '
327
- <script type="text/javascript" >
328
- jQuery(document).ready(function() {
329
- if(bulkProcessingRunning) {
330
- console.log("Bulk processing running");
331
- setTimeout(function(){
332
- window.location = window.location.href;
333
- }, 30000);
334
- } else {
335
- console.log("No bulk processing is currently running");
336
- }
337
- });
338
- </script>
339
- ';
340
-
341
  if(MUST_HAVE_KEY && $this->_verifiedKey == false) {
342
  echo "<p>In order to start processing your images, you need to validate your API key in the ShortPixel Settings. If you don’t have an API Key, you can get one delivered to your inbox.</p>";
343
  echo "<p>Don’t have an API Key yet? Get it now at <a href=\"https://shortpixel.com/wp-apikey\" target=\"_blank\">www.ShortPixel.com</a>, for free.</p>";
@@ -351,12 +345,20 @@ class WPShortPixel {
351
  'post_mime_type' => 'image'
352
  ));
353
 
 
 
 
 
 
 
 
 
 
354
  if($_POST["bulkProcess"]) {
355
  $imageLog = array();
356
  //remove all ShortPixel data from metadata
357
  foreach($attachments as $attachment) {
358
  $meta = wp_get_attachment_metadata($attachment->ID);
359
- $meta['ShortPixel']['WaitingProcessing'] = true;
360
  $meta['ShortPixel']['BulkProcessing'] = true;
361
  wp_update_attachment_metadata($attachment->ID, $meta);
362
  $imageLog[$attachment->ID] = false;
@@ -370,25 +372,34 @@ class WPShortPixel {
370
 
371
  $currentBulkProcessingStatus = get_option('bulkProcessingLog');
372
 
373
- if($currentBulkProcessingStatus && $currentBulkProcessingStatus['running']) {
374
- echo "<p>
375
- Bulk optimisation has started. It may take a while until we process all your images. The latest status of the processing will be displayed here every 30 seconds.
376
- In the meantime, you can continue using the admin as usual. However, <b>you musn’t close the WordPress admin</b>, or the bulk processing will stop.
377
- </p>";
378
- echo '
379
- <script type="text/javascript" >
380
- var bulkProcessingRunning = true;
381
- </script>
382
- ';
383
-
384
- $imagesLeft = count($currentBulkProcessingStatus["toDo"]);
385
- $totalImages = $currentBulkProcessingStatus['total'];
386
-
387
- echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
388
-
389
- echo '
390
- <a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
391
- ';
 
 
 
 
 
 
 
 
 
392
  } else {
393
  echo $this->getBulkProcessingForm(count($attachments));
394
  echo '
@@ -397,6 +408,21 @@ class WPShortPixel {
397
  </script>
398
  ';
399
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
  }
401
 
402
  public function renderSettingsMenu() {
@@ -406,7 +432,7 @@ class WPShortPixel {
406
 
407
  if(isset($_POST['submit']) || isset($_POST['validate'])) {
408
  //handle API Key - common for submit and validate
409
- $validityData = $this->getQuotaInformation($_POST['key']);
410
  $this->_apiKey = $_POST['key'];
411
  $this->_apiInterface->setApiKey($this->_apiKey);
412
  update_option('wp-short-pixel-apiKey', $_POST['key']);
@@ -417,6 +443,7 @@ class WPShortPixel {
417
  update_option('wp-short-pixel-verifiedKey', false);
418
  $this->_verifiedKey = false;
419
  }
 
420
  //if save button - we process the rest of the form elements
421
  if(isset($_POST['submit'])) {
422
  update_option('wp-short-pixel-compression', $_POST['compressionType']);
@@ -626,11 +653,16 @@ HTML;
626
  if($hook_suffix == 'plugins.php' && !$this->_verifiedKey) { echo $noticeInvalidKeyContent; }
627
  }
628
 
629
- public function getQuotaInformation($apiKey = null) {
630
 
631
  if(is_null($apiKey)) { $apiKey = $this->_apiKey; }
632
 
633
  $requestURL = 'https://api.shortpixel.com/api-status.php?key='.$apiKey;
 
 
 
 
 
634
  $args = array('timeout'=> SP_MAX_TIMEOUT, 'sslverify' => false);
635
  $response = wp_remote_get($requestURL, $args);
636
 
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.0
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
163
  die();
164
  }
165
 
166
+ $bulkLog = get_option('bulkProcessingLog');
167
+
168
  //query database for first found entry that needs processing
169
  global $wpdb;
170
+ $qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
171
+ WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%'
172
+ OR meta_value LIKE '%\"BulkProcessing\";b:1;%'
173
+ LIMIT " . BATCH_SIZE;
174
  $idList = $wpdb->get_results($qry);
175
 
176
  if(empty($idList)) { echo 'empty queue'; die; }
181
  $imagePath = get_attached_file($ID);
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
+ }
191
+
192
  $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
193
 
194
  if(is_string($result)) {
195
  $meta['ShortPixelImprovement'] = $result;
196
+ $bulkLog['toDo'][$ID] = false;
197
+ update_option('bulkProcessingLog', $bulkLog);
198
  die;
199
  }
200
 
210
  }
211
 
212
  unset($meta['ShortPixel']['WaitingProcessing']);
213
+
214
  if(isset($meta['ShortPixel']['BulkProcessing'])) {
215
  unset($meta['ShortPixel']['BulkProcessing']);
216
  }
217
 
 
 
 
 
 
 
 
 
 
 
 
218
  $meta['ShortPixelImprovement'] = $result->PercentImprovement;
219
 
220
  wp_update_attachment_metadata($ID, $meta);
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();
231
  }
232
 
296
  } catch(Exception $e) {
297
  //what to do, what to do?
298
  }
 
299
  }
300
 
301
  public function bulkOptimizeActionHandler($hook) {
306
  //remove all ShortPixel data from metadata
307
  foreach($_GET['media'] as $attachmentID) {
308
  $meta = wp_get_attachment_metadata($attachmentID);
 
309
  $meta['ShortPixel']['BulkProcessing'] = true;
310
  unset($meta['ShortPixelImprovement']);
311
  wp_update_attachment_metadata($attachmentID, $meta);
332
  public function bulkProcesss() {
333
  echo '<h1>Bulk Image Optimisation by ShortPixel</h1>';
334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  if(MUST_HAVE_KEY && $this->_verifiedKey == false) {
336
  echo "<p>In order to start processing your images, you need to validate your API key in the ShortPixel Settings. If you don’t have an API Key, you can get one delivered to your inbox.</p>";
337
  echo "<p>Don’t have an API Key yet? Get it now at <a href=\"https://shortpixel.com/wp-apikey\" target=\"_blank\">www.ShortPixel.com</a>, for free.</p>";
345
  'post_mime_type' => 'image'
346
  ));
347
 
348
+ if($_GET['cancel']) {
349
+ delete_option('bulkProcessingLog');
350
+ foreach($attachments as $attachment) {
351
+ $meta = wp_get_attachment_metadata($attachment->ID);
352
+ if(isset($meta['ShortPixel']['BulkProcessing'])) unset($meta['ShortPixel']['BulkProcessing']);
353
+ wp_update_attachment_metadata($attachment->ID, $meta);
354
+ }
355
+ }
356
+
357
  if($_POST["bulkProcess"]) {
358
  $imageLog = array();
359
  //remove all ShortPixel data from metadata
360
  foreach($attachments as $attachment) {
361
  $meta = wp_get_attachment_metadata($attachment->ID);
 
362
  $meta['ShortPixel']['BulkProcessing'] = true;
363
  wp_update_attachment_metadata($attachment->ID, $meta);
364
  $imageLog[$attachment->ID] = false;
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.
380
+ </p>";
381
+ echo '
382
+ <script type="text/javascript" >
383
+ var bulkProcessingRunning = true;
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
+
392
+ echo '
393
+ <a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
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));
405
  echo '
408
  </script>
409
  ';
410
  }
411
+
412
+ echo '
413
+ <script type="text/javascript" >
414
+ jQuery(document).ready(function() {
415
+ if(bulkProcessingRunning) {
416
+ console.log("Bulk processing running");
417
+ setTimeout(function(){
418
+ window.location = window.location.href;
419
+ }, 30000);
420
+ } else {
421
+ console.log("No bulk processing is currently running");
422
+ }
423
+ });
424
+ </script>
425
+ ';
426
  }
427
 
428
  public function renderSettingsMenu() {
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);
438
  update_option('wp-short-pixel-apiKey', $_POST['key']);
443
  update_option('wp-short-pixel-verifiedKey', false);
444
  $this->_verifiedKey = false;
445
  }
446
+
447
  //if save button - we process the rest of the form elements
448
  if(isset($_POST['submit'])) {
449
  update_option('wp-short-pixel-compression', $_POST['compressionType']);
653
  if($hook_suffix == 'plugins.php' && !$this->_verifiedKey) { echo $noticeInvalidKeyContent; }
654
  }
655
 
656
+ public function getQuotaInformation($apiKey = null, $appendUserAgent = false) {
657
 
658
  if(is_null($apiKey)) { $apiKey = $this->_apiKey; }
659
 
660
  $requestURL = 'https://api.shortpixel.com/api-status.php?key='.$apiKey;
661
+
662
+ if($appendUserAgent) {
663
+ $requestURL .= '&useragent=' . urlencode($_SERVER['HTTP_USER_AGENT']);
664
+ }
665
+
666
  $args = array('timeout'=> SP_MAX_TIMEOUT, 'sslverify' => false);
667
  $response = wp_remote_get($requestURL, $args);
668