ShortPixel Image Optimizer - Version 1.4.0

Version Description

  • Bulk image processing improved so it can optimize all the images in background while admin page is open
  • small changes in readme.txt descrption
Download this release

Release Info

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

Code changes from version 1.3.5 to 1.4.0

Files changed (3) hide show
  1. readme.txt +8 -3
  2. shortpixel_api.php +5 -1
  3. wp-shortpixel.php +96 -48
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.3.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -31,7 +31,7 @@ On the https://ShortPixel.com website, we offer free access to the ShrtPixel API
31
  Let's get ShortPixel plugin running on your WordPress website:
32
 
33
 
34
- 1. Sign up using your email at https://shortpixel.com/free-sign-up
35
  2. You will receive your personal API key in a confirmation email, to the address you provided.
36
  3. Upload the ShortPixel plugin to the /wp-content/plugins/ directory
37
  4. Use your unique API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress.
@@ -56,7 +56,7 @@ You can also use the API in your own applications, the <a href="https://shortpix
56
 
57
  = Where do I get my API key? =
58
 
59
- To get your API key, you must <a href="https://shortpixel.com/free-sign-up">Sign up to ShortPixel</a>. You will receive your personal API key in a confirmation email to the address you provided. Use your API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress.
60
 
61
  = Where do I use my API key? =
62
 
@@ -94,6 +94,11 @@ Your images are automatically stored in a backup folder, on your hosting server.
94
 
95
  == Changelog ==
96
 
 
 
 
 
 
97
  = 1.3.5 =
98
 
99
  * fixed broken link in settings page
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.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
31
  Let's get ShortPixel plugin running on your WordPress website:
32
 
33
 
34
+ 1. Sign up using your email at https://shortpixel.com/wp-apikey
35
  2. You will receive your personal API key in a confirmation email, to the address you provided.
36
  3. Upload the ShortPixel plugin to the /wp-content/plugins/ directory
37
  4. Use your unique API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress.
56
 
57
  = Where do I get my API key? =
58
 
59
+ To get your API key, you must <a href="https://shortpixel.com/wp-apikey">Sign up to ShortPixel</a>. You will receive your personal API key in a confirmation email to the address you provided. Use your API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress.
60
 
61
  = Where do I use my API key? =
62
 
94
 
95
  == Changelog ==
96
 
97
+ = 1.4.0 =
98
+
99
+ * Bulk image processing improved so it can optimize all the images in background while admin page is open
100
+ * small changes in readme.txt descrption
101
+
102
  = 1.3.5 =
103
 
104
  * fixed broken link in settings page
shortpixel_api.php CHANGED
@@ -72,6 +72,7 @@ class shortpixel_api {
72
  if(!$response) return $response;
73
 
74
  if($response['response']['code'] != 200) {
 
75
  printf('Web service did not respond. Please try again later.');
76
  return false;
77
  //error
@@ -104,7 +105,7 @@ class shortpixel_api {
104
 
105
  public function handleSuccess($callData, $url, $filePath, $ID) {
106
 
107
- if(property_exists($callData, 'LossySize')) {
108
  //lossy
109
  $correctFileSize = $callData->LossySize;
110
  $tempFile = download_url(str_replace('https://','http://',urldecode($callData->LossyURL)));
@@ -117,15 +118,18 @@ class shortpixel_api {
117
  if ( is_wp_error( $tempFile ) ) {
118
  @unlink($tempFile);
119
  return printf("Error downloading file (%s)", $tempFile->get_error_message());
 
120
  }
121
 
122
  //check response so that download is OK
123
  if(filesize($tempFile) != $correctFileSize) {
124
  return printf("Error downloading file - incorrect file size");
 
125
  }
126
 
127
  if (!file_exists($tempFile)) {
128
  return printf("Unable to locate downloaded file (%s)", $tempFile);
 
129
  }
130
 
131
  //if backup is enabled
72
  if(!$response) return $response;
73
 
74
  if($response['response']['code'] != 200) {
75
+ WPShortPixel::log("Response 200 OK");
76
  printf('Web service did not respond. Please try again later.');
77
  return false;
78
  //error
105
 
106
  public function handleSuccess($callData, $url, $filePath, $ID) {
107
 
108
+ if($this->_compressionType) {
109
  //lossy
110
  $correctFileSize = $callData->LossySize;
111
  $tempFile = download_url(str_replace('https://','http://',urldecode($callData->LossyURL)));
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
 
135
  //if backup is enabled
wp-shortpixel.php CHANGED
@@ -2,8 +2,8 @@
2
  /**
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/free-sign-up" 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.3.5
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
@@ -16,7 +16,8 @@ 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
-
 
20
 
21
  class WPShortPixel {
22
 
@@ -63,6 +64,8 @@ class WPShortPixel {
63
 
64
  if(get_option('wp-short-pixel-apiKey') != false) {
65
  $this->_apiKey = get_option('wp-short-pixel-apiKey');
 
 
66
  }
67
 
68
  if(get_option('wp-short-pixel-verifiedKey') != false) {
@@ -98,17 +101,27 @@ class WPShortPixel {
98
  }
99
  }
100
 
 
 
 
 
 
 
101
  function my_action_javascript() { ?>
102
  <script type="text/javascript" >
103
- jQuery(document).ready(function($) {
104
- var data = {
105
- 'action': 'my_action'
106
- };
107
  // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
108
- $.post(ajaxurl, data, function(response) {
109
- console.log('Server response: ' + response);
 
 
 
 
 
110
  });
111
- });
112
  </script> <?php
113
  }
114
 
@@ -132,9 +145,12 @@ class WPShortPixel {
132
 
133
  public function handleImageUpload($meta, $ID = null) {
134
  if(MUST_HAVE_KEY && $this->_verifiedKey) {
 
135
  $url = wp_get_attachment_url($ID);
136
  $path = get_attached_file($ID);
137
  $this->_apiInterface->doRequests($url, $path, $ID);
 
 
138
  }
139
  $meta['ShortPixel']['WaitingProcessing'] = true;
140
  return $meta;
@@ -148,53 +164,57 @@ class WPShortPixel {
148
 
149
  //query database for first found entry that needs processing
150
  global $wpdb;
151
- $qry = "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%' LIMIT 1";
152
- $ID = $wpdb->get_var($qry);
153
 
154
- if(empty($ID)) { die; }
155
 
156
- $imageURL = wp_get_attachment_url($ID);
157
- $imagePath = get_attached_file($ID);
158
- $meta = wp_get_attachment_metadata($ID);
 
 
159
 
160
- $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
161
 
162
- if(is_string($result)) {
163
- $meta['ShortPixelImprovement'] = $result;
164
- die;
165
- }
166
 
167
- $processThumbnails = get_option('wp-short-process_thumbnails');
168
 
169
- //handle the rest of the thumbnails generated by WP
170
- if($processThumbnails && $result && !empty($meta['sizes'])) {
171
- foreach($meta['sizes'] as $thumbnailInfo) {
172
- $thumbURL = str_replace(basename($imagePath), $thumbnailInfo['file'], $imageURL);
173
- $thumbPath = str_replace(basename($imagePath), $thumbnailInfo['file'], $imagePath);
174
- $this->_apiInterface->processImage($thumbURL, $thumbPath);
 
175
  }
176
- }
177
 
178
- unset($meta['ShortPixel']['WaitingProcessing']);
179
- if(isset($meta['ShortPixel']['BulkProcessing'])) {
180
- unset($meta['ShortPixel']['BulkProcessing']);
181
- }
182
 
183
- //check bulk processing
184
- $bulkLog = get_option('bulkProcessingLog');
185
- if(isset($bulkLog['toDo'])) {
186
- if(array_key_exists($ID, $bulkLog['toDo'])) {
187
- unset($bulkLog['toDo'][$ID]);
 
188
  }
189
- }
190
 
191
- if(empty($bulkLog['toDo'])) { delete_option('bulkProcessingLog'); }
192
- else { update_option('bulkProcessingLog', $bulkLog); }
193
 
194
- $meta['ShortPixelImprovement'] = $result->PercentImprovement;
 
 
 
 
195
 
196
- wp_update_attachment_metadata($ID, $meta);
197
- echo "Processing done succesfully for image #{$ID}";
198
  die();
199
  }
200
 
@@ -282,6 +302,21 @@ class WPShortPixel {
282
  public function bulkProcesss() {
283
  echo '<h1>ShortPixel Bulk Processing</h1>';
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  if(MUST_HAVE_KEY && $this->_verifiedKey == false) {
286
  echo "<p>You do not have an API Key set. Bulk processing cannot be used. </p>";
287
  echo "<p>Don’t have an API Key? <a href=\"https://shortpixel.com/wp-apikey\" target=\"_blank\">Sign up, it’s free.</a> </p>";
@@ -315,7 +350,16 @@ class WPShortPixel {
315
  $currentBulkProcessingStatus = get_option('bulkProcessingLog');
316
 
317
  if($currentBulkProcessingStatus && $currentBulkProcessingStatus['running']) {
318
- echo "<p>Bulk processing started and it may take a while to be completed.</p>";
 
 
 
 
 
 
 
 
 
319
 
320
  $imagesLeft = count($currentBulkProcessingStatus["toDo"]);
321
  $totalImages = $currentBulkProcessingStatus['total'];
@@ -323,11 +367,15 @@ class WPShortPixel {
323
  echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
324
 
325
  echo '
326
- <a class="button button-secondary" href="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk">Check Status<a/>
327
  <a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
328
  ';
329
  } else {
330
  echo $this->getBulkProcessingForm(count($attachments));
 
 
 
 
 
331
  }
332
  }
333
 
@@ -451,7 +499,7 @@ for(var i = 0; i < rad.length; i++) {
451
  if(this !== prev) {
452
  prev = this;
453
  }
454
- alert('Select Media/Bulk Short Pixel to reprocess all the images');
455
  };
456
  }
457
  </script>
@@ -628,7 +676,7 @@ HTML;
628
  }
629
 
630
  public function columns( $defaults ) {
631
- $defaults['wp-shortPixel'] = 'Short Pixel Compression';
632
  return $defaults;
633
  }
634
 
2
  /**
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.0
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
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
 
64
 
65
  if(get_option('wp-short-pixel-apiKey') != false) {
66
  $this->_apiKey = get_option('wp-short-pixel-apiKey');
67
+ } else {
68
+ add_option( 'wp-short-pixel-apiKey', '', '', 'yes' );
69
  }
70
 
71
  if(get_option('wp-short-pixel-verifiedKey') != false) {
101
  }
102
  }
103
 
104
+ static function log($message) {
105
+ if(SP_DEBUG) {
106
+ echo "{$message}</br>";
107
+ }
108
+ }
109
+
110
  function my_action_javascript() { ?>
111
  <script type="text/javascript" >
112
+ jQuery(document).ready(sendRequest());
113
+ function sendRequest() {
114
+ var data = { 'action': 'my_action' };
 
115
  // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
116
+ jQuery.post(ajaxurl, data, function(response) {
117
+ if(response == 'empty queue') {
118
+ console.log('Queue is empty');
119
+ } else {
120
+ console.log('Server response: ' + response);
121
+ sendRequest();
122
+ }
123
  });
124
+ }
125
  </script> <?php
126
  }
127
 
145
 
146
  public function handleImageUpload($meta, $ID = null) {
147
  if(MUST_HAVE_KEY && $this->_verifiedKey) {
148
+ self::log("Processing image id {$ID}");
149
  $url = wp_get_attachment_url($ID);
150
  $path = get_attached_file($ID);
151
  $this->_apiInterface->doRequests($url, $path, $ID);
152
+ } else {
153
+
154
  }
155
  $meta['ShortPixel']['WaitingProcessing'] = true;
156
  return $meta;
164
 
165
  //query database for first found entry that needs processing
166
  global $wpdb;
167
+ $qry = "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%' LIMIT " . BATCH_SIZE;
168
+ $idList = $wpdb->get_results($qry);
169
 
170
+ if(empty($idList)) { echo 'empty queue'; die; }
171
 
172
+ foreach($idList as $post) {
173
+ $ID = $post->post_id;
174
+ $imageURL = wp_get_attachment_url($ID);
175
+ $imagePath = get_attached_file($ID);
176
+ $meta = wp_get_attachment_metadata($ID);
177
 
178
+ $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
179
 
180
+ if(is_string($result)) {
181
+ $meta['ShortPixelImprovement'] = $result;
182
+ die;
183
+ }
184
 
185
+ $processThumbnails = get_option('wp-short-process_thumbnails');
186
 
187
+ //handle the rest of the thumbnails generated by WP
188
+ if($processThumbnails && $result && !empty($meta['sizes'])) {
189
+ foreach($meta['sizes'] as $thumbnailInfo) {
190
+ $thumbURL = str_replace(basename($imagePath), $thumbnailInfo['file'], $imageURL);
191
+ $thumbPath = str_replace(basename($imagePath), $thumbnailInfo['file'], $imagePath);
192
+ $this->_apiInterface->processImage($thumbURL, $thumbPath);
193
+ }
194
  }
 
195
 
196
+ unset($meta['ShortPixel']['WaitingProcessing']);
197
+ if(isset($meta['ShortPixel']['BulkProcessing'])) {
198
+ unset($meta['ShortPixel']['BulkProcessing']);
199
+ }
200
 
201
+ //check bulk processing
202
+ $bulkLog = get_option('bulkProcessingLog');
203
+ if(isset($bulkLog['toDo'])) {
204
+ if(array_key_exists($ID, $bulkLog['toDo'])) {
205
+ unset($bulkLog['toDo'][$ID]);
206
+ }
207
  }
 
208
 
209
+ if(empty($bulkLog['toDo'])) { delete_option('bulkProcessingLog'); }
210
+ else { update_option('bulkProcessingLog', $bulkLog); }
211
 
212
+ $meta['ShortPixelImprovement'] = $result->PercentImprovement;
213
+
214
+ wp_update_attachment_metadata($ID, $meta);
215
+ echo "Processing done succesfully for image #{$ID}";
216
+ }
217
 
 
 
218
  die();
219
  }
220
 
302
  public function bulkProcesss() {
303
  echo '<h1>ShortPixel Bulk Processing</h1>';
304
 
305
+ echo '
306
+ <script type="text/javascript" >
307
+ jQuery(document).ready(function() {
308
+ if(bulkProcessingRunning) {
309
+ console.log("Bulk processing running");
310
+ setTimeout(function(){
311
+ window.location = window.location.href;
312
+ }, 30000);
313
+ } else {
314
+ console.log("No bulk processing is currently running");
315
+ }
316
+ });
317
+ </script>
318
+ ';
319
+
320
  if(MUST_HAVE_KEY && $this->_verifiedKey == false) {
321
  echo "<p>You do not have an API Key set. Bulk processing cannot be used. </p>";
322
  echo "<p>Don’t have an API Key? <a href=\"https://shortpixel.com/wp-apikey\" target=\"_blank\">Sign up, it’s free.</a> </p>";
350
  $currentBulkProcessingStatus = get_option('bulkProcessingLog');
351
 
352
  if($currentBulkProcessingStatus && $currentBulkProcessingStatus['running']) {
353
+ echo "<p>
354
+ Bulk processing started and it may take a while we process your images in the background. </br>
355
+ This page will refresh every 30 seconds in order to display the latest status of the processing. </br>
356
+ In the mean time you can continue using the admin as usually.
357
+ </p>";
358
+ echo '
359
+ <script type="text/javascript" >
360
+ var bulkProcessingRunning = true;
361
+ </script>
362
+ ';
363
 
364
  $imagesLeft = count($currentBulkProcessingStatus["toDo"]);
365
  $totalImages = $currentBulkProcessingStatus['total'];
367
  echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
368
 
369
  echo '
 
370
  <a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
371
  ';
372
  } else {
373
  echo $this->getBulkProcessingForm(count($attachments));
374
+ echo '
375
+ <script type="text/javascript" >
376
+ var bulkProcessingRunning = false;
377
+ </script>
378
+ ';
379
  }
380
  }
381
 
499
  if(this !== prev) {
500
  prev = this;
501
  }
502
+ alert('Select Media/Bulk ShortPixel to reprocess all the images');
503
  };
504
  }
505
  </script>
676
  }
677
 
678
  public function columns( $defaults ) {
679
+ $defaults['wp-shortPixel'] = 'ShortPixel Compression';
680
  return $defaults;
681
  }
682