ShortPixel Image Optimizer - Version 1.0.6

Version Description

  • bulk processing runs in background now.
Download this release

Release Info

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

Code changes from version 1.0.4 to 1.0.6

Files changed (3) hide show
  1. readme.txt +9 -1
  2. shortpixel_api.php +18 -4
  3. wp-shortpixel.php +150 -116
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
5
  Requires at least: 3.0.0 or higher
6
  Tested up to: 4.0
7
- Stable tag: 1.0.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -92,6 +92,14 @@ ShortPixel team is here to help. <a href="https://shortpixel.com/contact">Contac
92
 
93
  == Changelog ==
94
 
 
 
 
 
 
 
 
 
95
  = 1.0.4 =
96
 
97
  * corrections and additions to readme.txt and wp-shortpixel.php
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
5
  Requires at least: 3.0.0 or higher
6
  Tested up to: 4.0
7
+ Stable tag: 1.0.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
92
 
93
  == Changelog ==
94
 
95
+ = 1.0.6 =
96
+
97
+ * bulk processing runs in background now.
98
+
99
+ = 1.0.5 =
100
+
101
+ * extra check for the converted images to be safely copied from ShortPixel
102
+
103
  = 1.0.4 =
104
 
105
  * corrections and additions to readme.txt and wp-shortpixel.php
shortpixel_api.php CHANGED
@@ -8,6 +8,7 @@ class shortpixel_api {
8
  private $_apiKey = '';
9
  private $_compressionType = '';
10
  private $_maxAttempts = 10;
 
11
 
12
  public function setCompressionType($compressionType)
13
  {
@@ -42,8 +43,7 @@ class shortpixel_api {
42
  }
43
 
44
  public function doRequests($url, $filePath, $ID = null, $time = 0) {
45
- $requestURL = 'https://api.shortpixel.com/reducer.php?key='.$this->_apiKey.'&lossy='.$this->_compressionType.'&url=';
46
-
47
  $requestURL = $requestURL . urlencode($url);
48
 
49
  $args = array('timeout'=> SP_MAX_TIMEOUT);
@@ -103,13 +103,27 @@ class shortpixel_api {
103
 
104
 
105
  public function handleSuccess($callData, $url, $filePath, $ID) {
106
- $tempFile = download_url(str_replace('https://','http://',urldecode($callData->DownloadURL)));
 
 
 
 
 
 
 
 
 
107
 
108
  if ( is_wp_error( $tempFile ) ) {
109
  @unlink($tempFile);
110
  return printf("Error downloading file (%s)", $tempFile->get_error_message());
111
  }
112
 
 
 
 
 
 
113
  if (!file_exists($tempFile)) {
114
  return printf("Unable to locate downloaded file (%s)", $tempFile);
115
  }
@@ -127,7 +141,7 @@ class shortpixel_api {
127
  if(isset($callData->LossySize)) {
128
  $savedSpace = $callData->OriginalSize - $callData->LossySize;
129
  } else {
130
- $savedSpace = $callData->OriginalSize - $callData->SPSize;
131
  }
132
 
133
  update_option(
8
  private $_apiKey = '';
9
  private $_compressionType = '';
10
  private $_maxAttempts = 10;
11
+ private $_apiEndPoint = 'https://api.shortpixel.com/v1/reducer.php';
12
 
13
  public function setCompressionType($compressionType)
14
  {
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);
103
 
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)));
111
+ } else {
112
+ //lossless
113
+ $correctFileSize = $callData->LoselessSize;
114
+ $tempFile = download_url(str_replace('https://','http://',urldecode($callData->LosslessURL)));
115
+ }
116
 
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
  }
141
  if(isset($callData->LossySize)) {
142
  $savedSpace = $callData->OriginalSize - $callData->LossySize;
143
  } else {
144
+ $savedSpace = $callData->OriginalSize - $callData->LoselessSize;
145
  }
146
 
147
  update_option(
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">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.0.4
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
@@ -17,6 +17,7 @@ class WPShortPixel {
17
  private $_apiKey = '';
18
  private $_compressionType = '';
19
  private $_processThumbnails = 1;
 
20
 
21
  public function __construct() {
22
  define('SP_DEBUG', false);
@@ -32,15 +33,17 @@ class WPShortPixel {
32
  add_filter( 'manage_media_columns', array( &$this, 'columns' ) );
33
  add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'generatePluginLinks'));
34
 
 
35
  add_action( 'manage_media_custom_column', array( &$this, 'generateCustomColumn' ), 10, 2 );
36
- add_action( 'handleBulkProcessing', array( &$this, 'handleBulkProcessing' ) );
37
-
38
 
39
  //add settings page
40
  add_action( 'admin_menu', array( &$this, 'registerSettingsPage' ) );
41
  add_action( 'admin_menu', array( &$this, 'registerAdminPage' ) );
42
  add_action( 'admin_notices', array( &$this, 'displayNotice' ) );
43
 
 
 
 
44
  }
45
 
46
  public function populateOptions() {
@@ -49,6 +52,10 @@ class WPShortPixel {
49
  $this->_apiKey = get_option('wp-short-pixel-apiKey');
50
  }
51
 
 
 
 
 
52
  if(get_option('wp-short-pixel-compression') != false) {
53
  $this->_compressionType = get_option('wp-short-pixel-compression');
54
  }
@@ -72,36 +79,79 @@ class WPShortPixel {
72
  }
73
  }
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  //handling older
76
  public function WPShortPixel() {
77
  $this->__construct();
78
  }
79
 
80
  public function handleImageUpload($meta, $ID = null) {
 
 
 
 
 
 
 
 
 
 
 
 
81
  $imageURL = wp_get_attachment_url($ID);
82
  $imagePath = get_attached_file($ID);
 
83
 
84
  $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
85
 
86
  if(is_string($result)) {
87
  $meta['ShortPixelImprovement'] = $result;
88
- return $meta;
89
- } else {
90
- $processThumbnails = get_option('wp-short-process_thumbnails');
91
-
92
- //handle the rest of the thumbnails generated by WP
93
- if($processThumbnails && $result && !empty($meta['sizes'])) {
94
- foreach($meta['sizes'] as $thumbnailInfo) {
95
- $thumbURL = str_replace(basename($imagePath), $thumbnailInfo['file'], $imageURL);
96
- $thumbPath = str_replace(basename($imagePath), $thumbnailInfo['file'], $imagePath);
97
- $this->_apiInterface->processImage($thumbURL, $thumbPath);
98
- }
99
  }
100
  }
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  $meta['ShortPixelImprovement'] = $result->PercentImprovement;
103
 
104
- return $meta;
 
 
105
  }
106
 
107
  public function registerSettingsPage() {
@@ -115,13 +165,6 @@ class WPShortPixel {
115
  public function bulkProcesss() {
116
  echo '<h1>ShortPixel Bulk Processing</h1>';
117
 
118
- if ( function_exists( 'apache_setenv' ) ) {
119
- @apache_setenv('no-gzip', 1);
120
- }
121
- @ini_set('output_buffering','on');
122
- @ini_set('zlib.output_compression', 0);
123
- @ini_set('implicit_flush', 1);
124
-
125
  $attachments = null;
126
  $attachments = get_posts( array(
127
  'numberposts' => -1,
@@ -129,94 +172,43 @@ class WPShortPixel {
129
  'post_mime_type' => 'image'
130
  ));
131
 
132
- $bulkProcessingLog = get_option('bulkProcessingLog');
133
- $bulkProcessingRunning = false;
134
-
135
- if(
136
- is_null($bulkProcessingLog['endDate']) &&
137
- ( $bulkProcessingLog['lastUpdate'] < $bulkProcessingLog['lastUpdate'] + 60 ) &&
138
- ( $bulkProcessingLog['startTime'] < $bulkProcessingLog['startTime'] + (60 * 15))
139
- ) {
140
- $bulkProcessingRunning = true;
141
- }
142
-
143
- if(isset($_POST['bulkProcess'])) {
144
-
145
- $scheduleResult = wp_schedule_single_event(time()+1, 'handleBulkProcessing');
146
- if(is_null($scheduleResult)) {
147
- $bulkProcessingLog = array(
148
- 'startTime' => time(),
149
- 'totalImageCount' => count($attachments),
150
- 'doneCount' => 0,
151
- 'log' => '',
152
- 'lastUpdate' => time(),
153
- 'endDate' => null
154
- );
155
- update_option('bulkProcessingLog', $bulkProcessingLog);
156
- echo "Bulk processing started. Please visite the Bulk processing page to view the status. </br>";
157
- }
158
-
159
- if($scheduleResult === false) {
160
- echo "There was a problem scheduling the bulk processing. Please try again later.";
161
- }
162
-
163
- } else {
164
-
165
- if ($bulkProcessingRunning) {
166
- echo "{$bulkProcessingLog['doneCount']}/{$bulkProcessingLog['totalImageCount']} images processed</br></br>Processing log:</br>";
167
- echo $bulkProcessingLog['log'];
168
- } else {
169
-
170
- if(count($attachments)) {
171
- if($bulkProcessingLog) { echo "Last succesful bulk processing done on {$bulkProcessingLog['totalImageCount']} images (" . date("d-m-Y H:i:s", $bulkProcessingLog['endDate']) . ").</br>"; }
172
- echo $this->getBulkProcessingForm(count($attachments));
173
- } else {
174
- echo "It appear that you have no images uploaded yet.</br>";
175
- }
176
  }
 
 
 
 
 
177
  }
178
 
179
- }
180
 
181
- public function handleBulkProcessing() {
182
- //get all images
183
- $attachments = null;
184
- $attachments = get_posts( array(
185
- 'numberposts' => -1,
186
- 'post_type' => 'attachment',
187
- 'post_mime_type' => 'image'
188
- ));
189
- //set up logging in db
190
- $bulkProcessingLog = get_option('bulkProcessingLog');
191
- //for each image
192
- foreach( $attachments as $attachment ) {
193
- //handle image processing
194
- $imageURL = wp_get_attachment_url($attachment->ID);
195
- $imagePath = get_attached_file($attachment->ID);
196
-
197
- $processingResult = $this->_apiInterface->processImage($imageURL, $imagePath, $attachment->ID);
198
-
199
- if(!is_object($processingResult)) {
200
- $bulkProcessingLog['log'] .= "Error! Image " . basename($imagePath) . " could not be processed<br/>";
201
- }
202
 
203
- if($processingResult->Status->Code == 1) {
204
- $bulkProcessingLog['log'] .= "Image " . basename($imagePath) . " scheduled for processing.<br/>";
205
- } elseif($processingResult->Status->Code == 2) {
206
- $bulkProcessingLog['log'] .= "Image " . basename($imagePath) . " processed succesfully.<br/>";
207
- } else {
208
- $bulkProcessingLog['log'] .= "Error! Image " . basename($imagePath) . " could not be processed<br/>";
209
- }
210
 
211
- $bulkProcessingLog['doneCount'] = $bulkProcessingLog['doneCount'] + 1;
212
- $bulkProcessingLog['lastUpdate'] = time();
213
- update_option('bulkProcessingLog', $bulkProcessingLog);
214
 
 
 
 
 
 
 
215
  }
216
 
217
- $bulkProcessingLog['endDate'] = time();
218
 
219
- update_option('bulkProcessingLog', $bulkProcessingLog);
 
220
 
221
  }
222
 
@@ -225,32 +217,56 @@ class WPShortPixel {
225
  wp_die('You do not have sufficient permissions to access this page.');
226
  }
227
 
228
- if(isset($_POST['submit'])) {
229
- //handle save options
230
- update_option('wp-short-pixel-apiKey', $_POST['key']);
231
  $this->_apiKey = $_POST['key'];
232
  $this->_apiInterface->setApiKey($this->_apiKey);
233
- update_option('wp-short-pixel-compression', $_POST['compressionType']);
234
- $this->_compressionType = $_POST['compressionType'];
235
- $this->_apiInterface->setCompressionType($this->_compressionType);
236
- if(isset($_POST['thumbnails'])) { $this->_processThumbnails = 1; } else { $this->_processThumbnails = 0; }
237
- update_option('wp-short-process_thumbnails', $this->_processThumbnails);
 
 
 
 
 
 
 
 
 
 
 
238
  }
239
 
240
  $checked = '';
241
  if($this->_processThumbnails) { $checked = 'checked'; }
242
 
243
  echo '<h1>ShortPixel Image Optimiser Settings</h1>';
244
- echo '<p>ShortPixel improves website performance by reducing the images’ size.<BR>Configure ShortPixel plugin to compress both past and new past images and optimise your website.</p>';
 
 
 
 
 
245
 
246
  $formHTML = <<< HTML
247
  <form name='wp_shortpixel_options' action='' method='post' id='wp_shortpixel_options'>
248
  <table class="form-table">
249
  <tbody><tr>
250
  <th scope="row"><label for="key">API Key:</label></th>
251
- <td><input name="key" type="text" id="key" value="{$this->_apiKey}" class="regular-text"></td>
 
 
252
  </tr>
253
- <tr><td style="padding-left: 0px;" colspan="2">Don’t have an API Key? <a href="https://shortpixel.com/wp-apikey" target="_blank">Sign up, it’s free.</a></td></tr>
 
 
 
 
 
 
254
  <tr><th scope="row">
255
  <label for="compressionType">Compression type: <span title="
256
  Lossy compression: lossy has a better compression rate than lossless compression. The resulting image is not 100% identical with the original. Works well for photos taken with your camera.
@@ -258,6 +274,7 @@ Lossless compression: the shrunk image will be identical with the original and s
258
  ">?</span></label>
259
  </th><td>
260
  HTML;
 
261
  if($this->_compressionType == 'lossless') {
262
  $formHTML .= '<input type="radio" name="compressionType" value="lossy" >Lossy</br></br>';
263
  $formHTML .= '<input type="radio" name="compressionType" value="lossless" checked>Lossless';
@@ -274,7 +291,10 @@ HTML;
274
  <td><input name="thumbnails" type="checkbox" id="thumbnails" {$checked}></td>
275
  </tr>
276
  </tbody></table>
277
- <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"></p>
 
 
 
278
  </form>
279
  <script>
280
  var rad = document.wp_shortpixel_options.compressionType;
@@ -406,9 +426,9 @@ HTML;
406
  if($data->Status->Code == '-401') { return $defaultData; }
407
 
408
  return array(
409
- "APIKeyValid" => true,
410
- "APICallsMade" => number_format($data->APICallsMade) . ' images',
411
- "APICallsQuota" => number_format($data->APICallsQuota) . ' images'
412
  );
413
 
414
 
@@ -417,12 +437,26 @@ HTML;
417
  public function generateCustomColumn( $column_name, $id ) {
418
  if( 'wp-shortPixel' == $column_name ) {
419
  $data = wp_get_attachment_metadata($id);
 
420
  if ( isset( $data['ShortPixelImprovement'] ) ) {
 
 
 
 
 
 
421
  print $data['ShortPixelImprovement'];
422
  if(is_numeric($data['ShortPixelImprovement'])) print '%';
 
 
 
 
 
 
423
  } else {
424
  if ( wp_attachment_is_image( $id ) ) {
425
  print 'Image not processed';
 
426
  }
427
  }
428
  }
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.0.6
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
17
  private $_apiKey = '';
18
  private $_compressionType = '';
19
  private $_processThumbnails = 1;
20
+ private $_verifiedKey = false;
21
 
22
  public function __construct() {
23
  define('SP_DEBUG', false);
33
  add_filter( 'manage_media_columns', array( &$this, 'columns' ) );
34
  add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'generatePluginLinks'));
35
 
36
+ //add_action( 'admin_footer', array(&$this, 'handleImageProcessing'));
37
  add_action( 'manage_media_custom_column', array( &$this, 'generateCustomColumn' ), 10, 2 );
 
 
38
 
39
  //add settings page
40
  add_action( 'admin_menu', array( &$this, 'registerSettingsPage' ) );
41
  add_action( 'admin_menu', array( &$this, 'registerAdminPage' ) );
42
  add_action( 'admin_notices', array( &$this, 'displayNotice' ) );
43
 
44
+
45
+ add_action( 'admin_footer', array( &$this, 'my_action_javascript') );
46
+ add_action( 'wp_ajax_my_action', array( &$this, 'handleImageProcessing') );
47
  }
48
 
49
  public function populateOptions() {
52
  $this->_apiKey = get_option('wp-short-pixel-apiKey');
53
  }
54
 
55
+ if(get_option('wp-short-pixel-verifiedKey') != false) {
56
+ $this->_verifiedKey = get_option('wp-short-pixel-verifiedKey');
57
+ }
58
+
59
  if(get_option('wp-short-pixel-compression') != false) {
60
  $this->_compressionType = get_option('wp-short-pixel-compression');
61
  }
79
  }
80
  }
81
 
82
+ function my_action_javascript() { ?>
83
+ <script type="text/javascript" >
84
+ jQuery(document).ready(function($) {
85
+ var data = {
86
+ 'action': 'my_action',
87
+ 'whatever': 1234
88
+ };
89
+ // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
90
+ $.post(ajaxurl, data, function(response) {
91
+ console.log('Server response: ' + response);
92
+ });
93
+ });
94
+ </script> <?php
95
+ }
96
+
97
  //handling older
98
  public function WPShortPixel() {
99
  $this->__construct();
100
  }
101
 
102
  public function handleImageUpload($meta, $ID = null) {
103
+ $meta['ShortPixel']['WaitingProcessing'] = true;
104
+ return $meta;
105
+ }
106
+
107
+ public function handleImageProcessing($ID = null) {
108
+ //query database for first found entry that needs processing
109
+ global $wpdb;
110
+ $qry = "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%' LIMIT 1";
111
+ $ID = $wpdb->get_var($qry);
112
+
113
+ if(empty($ID)) { die; }
114
+
115
  $imageURL = wp_get_attachment_url($ID);
116
  $imagePath = get_attached_file($ID);
117
+ $meta = wp_get_attachment_metadata($ID);
118
 
119
  $result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
120
 
121
  if(is_string($result)) {
122
  $meta['ShortPixelImprovement'] = $result;
123
+ die;
124
+ }
125
+
126
+ $processThumbnails = get_option('wp-short-process_thumbnails');
127
+
128
+ //handle the rest of the thumbnails generated by WP
129
+ if($processThumbnails && $result && !empty($meta['sizes'])) {
130
+ foreach($meta['sizes'] as $thumbnailInfo) {
131
+ $thumbURL = str_replace(basename($imagePath), $thumbnailInfo['file'], $imageURL);
132
+ $thumbPath = str_replace(basename($imagePath), $thumbnailInfo['file'], $imagePath);
133
+ $this->_apiInterface->processImage($thumbURL, $thumbPath);
134
  }
135
  }
136
 
137
+ unset($meta['ShortPixel']['WaitingProcessing']);
138
+
139
+ //check bulk processing
140
+ $bulkLog = get_option('bulkProcessingLog');
141
+ if(isset($bulkLog)) {
142
+ if(array_key_exists($ID, $bulkLog['toDo'])) {
143
+ unset($bulkLog['toDo'][$ID]);
144
+ }
145
+ }
146
+
147
+ if(empty($bulkLog['toDo'])) { delete_option('bulkProcessingLog'); }
148
+ else { update_option('bulkProcessingLog', $bulkLog); }
149
+
150
  $meta['ShortPixelImprovement'] = $result->PercentImprovement;
151
 
152
+ wp_update_attachment_metadata($ID, $meta);
153
+ echo "Processing done succesfully for image #{$ID}";
154
+ die();
155
  }
156
 
157
  public function registerSettingsPage() {
165
  public function bulkProcesss() {
166
  echo '<h1>ShortPixel Bulk Processing</h1>';
167
 
 
 
 
 
 
 
 
168
  $attachments = null;
169
  $attachments = get_posts( array(
170
  'numberposts' => -1,
172
  'post_mime_type' => 'image'
173
  ));
174
 
175
+ if($_POST["bulkProcess"]) {
176
+ $imageLog = array();
177
+ //remove all ShortPixel data from metadata
178
+ foreach($attachments as $attachment) {
179
+ $meta = wp_get_attachment_metadata($attachment->ID);
180
+ $meta['ShortPixel']['WaitingProcessing'] = true;
181
+ wp_update_attachment_metadata($attachment->ID, $meta);
182
+ $imageLog[$attachment->ID] = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  }
184
+ $bulkLog = array();
185
+ $bulkLog['running'] = true;
186
+ $bulkLog['toDo'] = $imageLog;
187
+ $bulkLog['total'] = count($imageLog);
188
+ update_option('bulkProcessingLog', $bulkLog);
189
  }
190
 
191
+ $currentBulkProcessingStatus = get_option('bulkProcessingLog');
192
 
193
+ if($currentBulkProcessingStatus && $currentBulkProcessingStatus['running']) {
194
+ echo "<p>Bulk processing started and it may take a while to be completed.</p>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
+ $imagesLeft = count($currentBulkProcessingStatus["toDo"]);
197
+ $totalImages = $currentBulkProcessingStatus['total'];
 
 
 
 
 
198
 
199
+ echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
 
 
200
 
201
+ echo '
202
+ <a class="button button-secondary" href="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk">Check Status<a/>
203
+ <a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
204
+ ';
205
+ } else {
206
+ echo $this->getBulkProcessingForm(count($attachments));
207
  }
208
 
 
209
 
210
+
211
+ //TO DO: find a way to track when bulk processing is running and update a log for customer
212
 
213
  }
214
 
217
  wp_die('You do not have sufficient permissions to access this page.');
218
  }
219
 
220
+ if(isset($_POST['submit']) || isset($_POST['validate'])) {
221
+ //handle API Key - common for submit and validate
222
+ $validityData = $this->getQuotaInformation($_POST['key']);
223
  $this->_apiKey = $_POST['key'];
224
  $this->_apiInterface->setApiKey($this->_apiKey);
225
+ update_option('wp-short-pixel-apiKey', $_POST['key']);
226
+ if($validityData['APIKeyValid']) {
227
+ update_option('wp-short-pixel-verifiedKey', true);
228
+ $this->_verifiedKey = true;
229
+ } else {
230
+ update_option('wp-short-pixel-verifiedKey', false);
231
+ $this->_verifiedKey = false;
232
+ }
233
+ //if save button - we process the rest of the form elements
234
+ if(isset($_POST['submit'])) {
235
+ update_option('wp-short-pixel-compression', $_POST['compressionType']);
236
+ $this->_compressionType = $_POST['compressionType'];
237
+ $this->_apiInterface->setCompressionType($this->_compressionType);
238
+ if(isset($_POST['thumbnails'])) { $this->_processThumbnails = 1; } else { $this->_processThumbnails = 0; }
239
+ update_option('wp-short-process_thumbnails', $this->_processThumbnails);
240
+ }
241
  }
242
 
243
  $checked = '';
244
  if($this->_processThumbnails) { $checked = 'checked'; }
245
 
246
  echo '<h1>ShortPixel Image Optimiser Settings</h1>';
247
+ echo '<p>
248
+ <a href="https://shortpixel.com">ShortPixel.com</a> |
249
+ <a href="https://wordpress.org/plugins/shortpixel-image-optimiser/installation/">Installation </a> |
250
+ <a href="https://wordpress.org/support/plugin/shortpixel-image-optimiser">Support </a>
251
+ </p>';
252
+ echo '<p>New images uploaded to the Media Library will be optimized automatically.<br/>If you have existing images you would like to optimize, you can use the <a href="/upload.php?page=wp-short-pixel-bulk">Bulk Optimize tool</a>.</p>';
253
 
254
  $formHTML = <<< HTML
255
  <form name='wp_shortpixel_options' action='' method='post' id='wp_shortpixel_options'>
256
  <table class="form-table">
257
  <tbody><tr>
258
  <th scope="row"><label for="key">API Key:</label></th>
259
+ <td><input name="key" type="text" id="key" value="{$this->_apiKey}" class="regular-text">
260
+ <input type="submit" name="validate" id="validate" class="button button-primary" title="Validate the provided API key" value="Validate">
261
+ </td>
262
  </tr>
263
+ HTML;
264
+
265
+ if(!$this->_verifiedKey) {
266
+ $formHTML .= '<tr><td style="padding-left: 0px;" colspan="2">Don’t have an API Key? <a href="https://shortpixel.com/wp-apikey" target="_blank">Sign up, it’s free.</a></td></tr>';
267
+ }
268
+
269
+ $formHTML .= <<< HTML
270
  <tr><th scope="row">
271
  <label for="compressionType">Compression type: <span title="
272
  Lossy compression: lossy has a better compression rate than lossless compression. The resulting image is not 100% identical with the original. Works well for photos taken with your camera.
274
  ">?</span></label>
275
  </th><td>
276
  HTML;
277
+
278
  if($this->_compressionType == 'lossless') {
279
  $formHTML .= '<input type="radio" name="compressionType" value="lossy" >Lossy</br></br>';
280
  $formHTML .= '<input type="radio" name="compressionType" value="lossless" checked>Lossless';
291
  <td><input name="thumbnails" type="checkbox" id="thumbnails" {$checked}></td>
292
  </tr>
293
  </tbody></table>
294
+ <p class="submit">
295
+ <input type="submit" name="submit" id="submit" class="button button-primary" title="Save Changes" value="Save Changes">
296
+ <input type="submit" name="bulk-process" id="bulk-process" class="button button-primary" title="Process all the images in your Media Library" value="Bulk Process">
297
+ </p>
298
  </form>
299
  <script>
300
  var rad = document.wp_shortpixel_options.compressionType;
426
  if($data->Status->Code == '-401') { return $defaultData; }
427
 
428
  return array(
429
+ "APIKeyValid" => true,
430
+ "APICallsMade" => number_format($data->APICallsMade) . ' images',
431
+ "APICallsQuota" => number_format($data->APICallsQuota) . ' images'
432
  );
433
 
434
 
437
  public function generateCustomColumn( $column_name, $id ) {
438
  if( 'wp-shortPixel' == $column_name ) {
439
  $data = wp_get_attachment_metadata($id);
440
+
441
  if ( isset( $data['ShortPixelImprovement'] ) ) {
442
+
443
+ if(isset($data['ShortPixel']['WaitingProcessing']) && $data['ShortPixel']['WaitingProcessing']) {
444
+ print 'Waiting for bulk processing';
445
+ return;
446
+ }
447
+
448
  print $data['ShortPixelImprovement'];
449
  if(is_numeric($data['ShortPixelImprovement'])) print '%';
450
+ return;
451
+ }
452
+
453
+ if(isset($data['ShortPixel']['WaitingProcessing']) && $data['ShortPixel']['WaitingProcessing']) {
454
+ print 'Image waiting to be processed';
455
+ return;
456
  } else {
457
  if ( wp_attachment_is_image( $id ) ) {
458
  print 'Image not processed';
459
+ return;
460
  }
461
  }
462
  }