ShortPixel Image Optimizer - Version 2.1.6

Version Description

  • improved login procedure upon quota is exceeded
  • when quota is exceeded user can more easily increase it
  • extra warning regarding the number of thumbs available additionally to main images
  • improved counting of images
  • check if https works if not use http for communcations with the API
  • better handling of error messages when API service cannot be contacted
Download this release

Release Info

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

Code changes from version 2.1.5 to 2.1.6

Files changed (3) hide show
  1. readme.txt +11 -2
  2. shortpixel_api.php +17 -6
  3. wp-shortpixel.php +115 -22
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, pdf, pdf optimisation, pdf optimization, optimize pdf, optimise pdf, shrink pdf, jpg, jpeg, jpg optimisation, jpg optimization, optimize jpg, optimise jpg, shrink jpg, gif, animated gif, optimize gif, optimise gif
5
  Requires at least: 3.0.0 or higher
6
  Tested up to: 4.2
7
- Stable tag: 2.1.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -112,10 +112,19 @@ The ShortPixel team is here to help. <a href="https://shortpixel.com/contact">Co
112
 
113
  == Changelog ==
114
 
 
 
 
 
 
 
 
 
 
115
  = 2.1.5 =
116
 
117
  * visiting Settings/ShortPixel now resets the Queue flag
118
- * when plgin's API Key cannot be validated the JS script that calls admin-ajas.php is stopped
119
  * return meaningful message on Media Library listing when API Key is not valid
120
  * extra check for API Key validation
121
 
4
  Tags: picture, optimization, image editor, pngout, upload speed, shortpixel, compression, jpegmini, webp, lossless, cwebp, media, tinypng, jpegtran,image, image optimisation, shrink, picture, photo, optimize photos, compress, performance, tinypng, crunch, pngquant, attachment, optimize, pictures,fast, images, image files, image quality, lossy, upload, kraken, resize, seo, smushit, optipng, kraken image optimizer, ewww, photo optimization, gifsicle, image optimizer, images, krakenio, png, gmagick, image optimize, pdf, pdf optimisation, pdf optimization, optimize pdf, optimise pdf, shrink pdf, jpg, jpeg, jpg optimisation, jpg optimization, optimize jpg, optimise jpg, shrink jpg, gif, animated gif, optimize gif, optimise gif
5
  Requires at least: 3.0.0 or higher
6
  Tested up to: 4.2
7
+ Stable tag: 2.1.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
112
 
113
  == Changelog ==
114
 
115
+ = 2.1.6 =
116
+
117
+ * improved login procedure upon quota is exceeded
118
+ * when quota is exceeded user can more easily increase it
119
+ * extra warning regarding the number of thumbs available additionally to main images
120
+ * improved counting of images
121
+ * check if https works if not use http for communcations with the API
122
+ * better handling of error messages when API service cannot be contacted
123
+
124
  = 2.1.5 =
125
 
126
  * visiting Settings/ShortPixel now resets the Queue flag
127
+ * when plgin's API Key cannot be validated the JS script that calls admin-ajax.php is stopped
128
  * return meaningful message on Media Library listing when API Key is not valid
129
  * extra check for API Key validation
130
 
shortpixel_api.php CHANGED
@@ -8,7 +8,7 @@ class shortpixel_api {
8
  private $_apiKey = '';
9
  private $_compressionType = '';
10
  private $_maxAttempts = 10;
11
- private $_apiEndPoint = 'https://api.shortpixel.com/v2/reducer.php';
12
 
13
  public function setCompressionType($compressionType) {
14
  $this->_compressionType = $compressionType;
@@ -44,7 +44,10 @@ class shortpixel_api {
44
  $response = $this->doBulkRequest($urls, true);
45
 
46
  if(is_object($response) && get_class($response) == 'WP_Error') {
47
- return false;
 
 
 
48
  }
49
 
50
  return $response;
@@ -59,8 +62,8 @@ class shortpixel_api {
59
  'lossy' => $this->_compressionType,
60
  'urllist' => $imageList
61
  );
62
-
63
- $response = wp_remote_post($this->_apiEndPoint, array(
64
  'method' => 'POST',
65
  'timeout' => 45,
66
  'redirection' => 3,
@@ -70,7 +73,12 @@ class shortpixel_api {
70
  'headers' => array(),
71
  'body' => json_encode($requestParameters),
72
  'cookies' => array()
73
- ));
 
 
 
 
 
74
 
75
  return $response;
76
  }
@@ -106,7 +114,10 @@ class shortpixel_api {
106
  }
107
 
108
  $response = $this->doRequests($url, $filePaths, $ID);//send requests to API
109
- if(!$response) return $response;
 
 
 
110
 
111
  if($response['response']['code'] != 200) {//response <> 200 -> there was an error apparently?
112
  printf('ShortPixel API service accesibility error. Please try again later.');
8
  private $_apiKey = '';
9
  private $_compressionType = '';
10
  private $_maxAttempts = 10;
11
+ private $_apiEndPoint = 'https://api.shortpixel.com/v2/reducer.php';//fai
12
 
13
  public function setCompressionType($compressionType) {
14
  $this->_compressionType = $compressionType;
44
  $response = $this->doBulkRequest($urls, true);
45
 
46
  if(is_object($response) && get_class($response) == 'WP_Error') {
47
+ if ( isset($response->errors['http_request_failed'][0]) )
48
+ return $response->errors['http_request_failed'][0] . ". If problem persists please <a href='https://shortpixel.com/contact'>contact us</a>";
49
+ else
50
+ return "There was an error. If persists please <a href='https://shortpixel.com/contact'>contact us</a>";
51
  }
52
 
53
  return $response;
62
  'lossy' => $this->_compressionType,
63
  'urllist' => $imageList
64
  );
65
+
66
+ $arguments = array(
67
  'method' => 'POST',
68
  'timeout' => 45,
69
  'redirection' => 3,
73
  'headers' => array(),
74
  'body' => json_encode($requestParameters),
75
  'cookies' => array()
76
+ );
77
+
78
+ $response = wp_remote_post($this->_apiEndPoint, $arguments );
79
+
80
+ if(is_wp_error( $response ))
81
+ $response = wp_remote_post(str_replace('https://', 'http://', $this->_apiEndPoint), $arguments );
82
 
83
  return $response;
84
  }
114
  }
115
 
116
  $response = $this->doRequests($url, $filePaths, $ID);//send requests to API
117
+
118
+ if(! is_array($response)) {
119
+ return $response;
120
+ }
121
 
122
  if($response['response']['code'] != 200) {//response <> 200 -> there was an error apparently?
123
  printf('ShortPixel API service accesibility error. Please try again later.');
wp-shortpixel.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: ShortPixel Image Optimizer
4
  * Plugin URI: https://shortpixel.com/
5
  * Description: ShortPixel is an image compression tool that helps improve your website performance. The plugin optimizes images automatically using both lossy and lossless compression. Resulting, smaller, images are no different in quality from the original. To install: 1) Click the "Activate" link to the left of this description. 2) <a href="https://shortpixel.com/wp-apikey" target="_blank">Free Sign up</a> for your unique API Key . 3) Check your email for your API key. 4) Use your API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress. 5) Done!
6
- * Version: 2.1.5
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
@@ -15,14 +15,13 @@ if ( !is_plugin_active( 'wpmandrill/wpmandrill.php' ) ) {
15
  require_once( ABSPATH . 'wp-includes/pluggable.php' );//to avoid conflict with wpmandrill plugin
16
  }
17
 
18
- define('PLUGIN_VERSION', "2.1.5");
19
  define('SP_DEBUG', false);
20
  define('SP_LOG', false);
21
  define('SP_MAX_TIMEOUT', 10);
22
  define('SP_BACKUP_FOLDER', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'ShortpixelBackups');
23
  define('MUST_HAVE_KEY', true);
24
  define('MAX_API_RETRIES', 5);
25
- define('QUOTA_EXCEEDED', "Quota Exceeded. <a href='https://shortpixel.com/pricing' target='_blank'>Learn more</a>");
26
  $MAX_EXECUTION_TIME = ini_get('max_execution_time');
27
  if ( is_numeric($MAX_EXECUTION_TIME) )
28
  define('MAX_EXECUTION_TIME', $MAX_EXECUTION_TIME - 3 ); //in seconds
@@ -42,6 +41,8 @@ class WPShortPixel {
42
  public function __construct() {
43
 
44
  $this->populateOptions();
 
 
45
  $this->setDefaultViewModeList();//set default mode as list. only @ first run
46
 
47
  $this->_apiInterface = new shortpixel_api($this->_apiKey, $this->_compressionType);
@@ -133,6 +134,10 @@ class WPShortPixel {
133
  if(get_option('wp-short-pixel-quota-exceeded') === false) {//min ID used for postmeta queries
134
  add_option( 'wp-short-pixel-quota-exceeded', 0, '', 'yes' );
135
  }
 
 
 
 
136
 
137
  }
138
 
@@ -143,7 +148,7 @@ class WPShortPixel {
143
  delete_option('bulkProcessingStatus');
144
  delete_option( 'wp-short-pixel-cancel-pointer');
145
  update_option( 'wp-short-pixel-query-id-stop', $endQueryID );
146
- update_option( 'wp-short-pixel-query-id-start', $startQueryID );
147
  }
148
 
149
  public function shortPixelDeactivatePlugin()//reset some params to avoid troubles for plugins that were activated/deactivated/activated
@@ -612,6 +617,7 @@ class WPShortPixel {
612
 
613
  public function bulkProcess() {
614
  global $wpdb,$startQueryID,$endQueryID;
 
615
  echo '<h1>Bulk Image Optimization by ShortPixel</h1>';
616
 
617
  if(MUST_HAVE_KEY && $this->_verifiedKey == false) {//invalid API Key
@@ -620,6 +626,16 @@ class WPShortPixel {
620
  return;
621
  }
622
 
 
 
 
 
 
 
 
 
 
 
623
  if(isset($_GET['cancel']))
624
  {//cancel an ongoing bulk processing, it might be needed sometimes
625
  $this->cancelProcessing();
@@ -661,7 +677,7 @@ class WPShortPixel {
661
  {
662
  $noticeHTML = "<br/><div style=\"background-color: #fff; border-left: 4px solid %s; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1); padding: 1px 12px;\"><p>%s</p></div>";
663
  $quotaData = $this->getQuotaInformation();
664
-
665
  //maybe in the meantime the user added credits to their account?
666
 
667
  if ( $quotaData['APICallsQuotaNumeric'] > $quotaData['APICallsMadeNumeric'] )
@@ -670,8 +686,16 @@ class WPShortPixel {
670
  }
671
  else
672
  {
673
- printf($noticeHTML, '#ff0000', "The plugin has optimized " . number_format($quotaData['APICallsMadeNumeric']) . " images and stopped because it reached the monthly limit which is " . number_format($quotaData['APICallsQuotaNumeric']) . ".<BR> See the other <a href='https://shortpixel.com/pricing' target='_blank'>options availbe</a> and <a href='https://shortpixel.com/login' target='_blank'>log into your account</a> to change your type of subscription.");
 
 
 
 
 
674
  return;
 
 
 
675
  }
676
  }
677
 
@@ -733,7 +757,7 @@ class WPShortPixel {
733
  }
734
 
735
  delete_option('bulkProcessingStatus');
736
- echo $this->getBulkProcessingForm($allFiles[0]->FilesToBeProcessed);
737
  echo '
738
  <script type="text/javascript" >
739
  var bulkProcessingRunning = false;
@@ -1023,14 +1047,49 @@ HTML;
1023
  }
1024
  }
1025
 
1026
- public function getBulkProcessingForm($imageCount) {
1027
 
1028
- $message = "</br>
1029
- Currently, you have {$imageCount} images in your library. </br>
1030
- </br>
1031
- <form action='' method='POST' >
1032
- <input type='submit' name='bulkProcess' id='bulkProcess' class='button button-primary' value='Compress all your images'>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1033
 
 
1034
  if ( get_option( 'wp-short-pixel-cancel-pointer') )//add also the resume bulk processing option
1035
  $message .= "&nbsp;&nbsp;&nbsp;<input type='submit' name='bulkProcessResume' id='bulkProcessResume' class='button button-primary' value='Resume cancelled process'>";
1036
 
@@ -1042,7 +1101,7 @@ Currently, you have {$imageCount} images in your library. </br>
1042
 
1043
 
1044
  public function getQuotaInformation($apiKey = null, $appendUserAgent = false) {
1045
-
1046
  if(is_null($apiKey)) { $apiKey = $this->_apiKey; }
1047
 
1048
  $requestURL = 'https://api.shortpixel.com/v2/api-status.php';
@@ -1054,21 +1113,17 @@ Currently, you have {$imageCount} images in your library. </br>
1054
  if($appendUserAgent) {
1055
  $args['body']['useragent'] = "Agent" . urlencode($_SERVER['HTTP_USER_AGENT']);
1056
  }
1057
-
1058
  $response = wp_remote_post($requestURL, $args);
1059
 
1060
  if(is_wp_error( $response )) //some hosting providers won't allow https:// POST connections so we try http:// as well
1061
  $response = wp_remote_post(str_replace('https://', 'http://', $requestURL), $args);
1062
-
1063
- if(is_wp_error( $response )) {
1064
  $response = wp_remote_get(str_replace('https://', 'http://', $requestURL), $args);
1065
- }
1066
-
1067
-
1068
 
1069
  $defaultData = array(
1070
  "APIKeyValid" => false,
1071
- "Message" => 'API Key could not be validated. Could not connect to Shortpixel service.',
1072
  "APICallsMade" => 'Information unavailable. Please check your API key.',
1073
  "APICallsQuota" => 'Information unavailable. Please check your API key.');
1074
 
@@ -1337,8 +1392,46 @@ Currently, you have {$imageCount} images in your library. </br>
1337
  $resultQuery = $wpdb->get_results($queryMax);
1338
  $startQueryID = $resultQuery[0]->startQueryID;
1339
  $endQueryID = $startQueryID;
1340
-
1341
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1342
 
1343
  public function migrateBackupFolder() {
1344
  $oldBackupFolder = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ShortpixelBackups';
3
  * Plugin Name: ShortPixel Image Optimizer
4
  * Plugin URI: https://shortpixel.com/
5
  * Description: ShortPixel is an image compression tool that helps improve your website performance. The plugin optimizes images automatically using both lossy and lossless compression. Resulting, smaller, images are no different in quality from the original. To install: 1) Click the "Activate" link to the left of this description. 2) <a href="https://shortpixel.com/wp-apikey" target="_blank">Free Sign up</a> for your unique API Key . 3) Check your email for your API key. 4) Use your API key to activate ShortPixel plugin in the 'Plugins' menu in WordPress. 5) Done!
6
+ * Version: 2.1.6
7
  * Author: ShortPixel
8
  * Author URI: https://shortpixel.com
9
  */
15
  require_once( ABSPATH . 'wp-includes/pluggable.php' );//to avoid conflict with wpmandrill plugin
16
  }
17
 
18
+ define('PLUGIN_VERSION', "2.1.6");
19
  define('SP_DEBUG', false);
20
  define('SP_LOG', false);
21
  define('SP_MAX_TIMEOUT', 10);
22
  define('SP_BACKUP_FOLDER', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'ShortpixelBackups');
23
  define('MUST_HAVE_KEY', true);
24
  define('MAX_API_RETRIES', 5);
 
25
  $MAX_EXECUTION_TIME = ini_get('max_execution_time');
26
  if ( is_numeric($MAX_EXECUTION_TIME) )
27
  define('MAX_EXECUTION_TIME', $MAX_EXECUTION_TIME - 3 ); //in seconds
41
  public function __construct() {
42
 
43
  $this->populateOptions();
44
+ define('QUOTA_EXCEEDED', "Quota Exceeded. <a href='https://shortpixel.com/login/".$this->_apiKey."' target='_blank'>Extend Quota</a>");
45
+
46
  $this->setDefaultViewModeList();//set default mode as list. only @ first run
47
 
48
  $this->_apiInterface = new shortpixel_api($this->_apiKey, $this->_compressionType);
134
  if(get_option('wp-short-pixel-quota-exceeded') === false) {//min ID used for postmeta queries
135
  add_option( 'wp-short-pixel-quota-exceeded', 0, '', 'yes' );
136
  }
137
+
138
+ if(get_option('wp-short-pixel-protocol') === false) {//min ID used for postmeta queries
139
+ add_option( 'wp-short-pixel-protocol', 'https', '', 'yes' );
140
+ }
141
 
142
  }
143
 
148
  delete_option('bulkProcessingStatus');
149
  delete_option( 'wp-short-pixel-cancel-pointer');
150
  update_option( 'wp-short-pixel-query-id-stop', $endQueryID );
151
+ update_option( 'wp-short-pixel-query-id-start', $startQueryID );
152
  }
153
 
154
  public function shortPixelDeactivatePlugin()//reset some params to avoid troubles for plugins that were activated/deactivated/activated
617
 
618
  public function bulkProcess() {
619
  global $wpdb,$startQueryID,$endQueryID;
620
+
621
  echo '<h1>Bulk Image Optimization by ShortPixel</h1>';
622
 
623
  if(MUST_HAVE_KEY && $this->_verifiedKey == false) {//invalid API Key
626
  return;
627
  }
628
 
629
+
630
+ if ( isset($_POST['bulkProcess']) )
631
+ {//set the thumbnails option
632
+ if ( isset($_POST['thumbnails']) )
633
+ update_option('wp-short-process_thumbnails', 1);
634
+ else
635
+ update_option('wp-short-process_thumbnails', 0);
636
+ }
637
+
638
+
639
  if(isset($_GET['cancel']))
640
  {//cancel an ongoing bulk processing, it might be needed sometimes
641
  $this->cancelProcessing();
677
  {
678
  $noticeHTML = "<br/><div style=\"background-color: #fff; border-left: 4px solid %s; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1); padding: 1px 12px;\"><p>%s</p></div>";
679
  $quotaData = $this->getQuotaInformation();
680
+
681
  //maybe in the meantime the user added credits to their account?
682
 
683
  if ( $quotaData['APICallsQuotaNumeric'] > $quotaData['APICallsMadeNumeric'] )
686
  }
687
  else
688
  {
689
+ printf($noticeHTML, '#ff0000', "The plugin has stopped because it reached the monthly limit. You can upgrade your ShortPixel plan by <a href='https://shortpixel.com/login/".$this->_apiKey."' target='_blank'>logging into your account</a> - see the <a href='https://shortpixel.com/pricing' target='_blank'>options available</a><BR>The plugin successfully optimized " . number_format($quotaData['APICallsMadeNumeric']) . " images, with a " . round(get_option('wp-short-pixel-averageCompression'),2) . "% average compression rate. See your own stats by <a href='https://shortpixel.com/login/".$this->_apiKey."' target='_blank'>checking your optimization reports</a>. <BR>" );
690
+ //fai
691
+
692
+
693
+ //~ see the . number_format($quotaData['APICallsMadeNumeric']) . " images and stopped because it reached the monthly limit which is " . number_format($quotaData['APICallsQuotaNumeric']) . ".<BR> See the other <a href='https://shortpixel.com/pricing' target='_blank'>options availbe</a> and <a href='https://shortpixel.com/login/".$this->_apiKey."' target='_blank'>log into your account</a> to change your type of subscription.");
694
+ //~
695
  return;
696
+
697
+ //The plugin has stopped because it reached the monthly limit. You can upgrade your ShortPixel plan by logging into your account - see the options available
698
+ //fai stop bulk
699
  }
700
  }
701
 
757
  }
758
 
759
  delete_option('bulkProcessingStatus');
760
+ echo $this->getBulkProcessingForm();
761
  echo '
762
  <script type="text/javascript" >
763
  var bulkProcessingRunning = false;
1047
  }
1048
  }
1049
 
1050
+ public function getBulkProcessingForm() {
1051
 
1052
+ //image count
1053
+ $imageCount = $this->countAllProcessableFiles();
1054
+ $imageOnlyThumbs = $imageCount['totalFiles'] - $imageCount['mainFiles'];
1055
+ //average compression
1056
+ $averageCompression = round(get_option('wp-short-pixel-averageCompression'),2);
1057
+
1058
+
1059
+ if ( $averageCompression == 0 )
1060
+ {
1061
+ $message = "</br>
1062
+ There are {$imageCount['mainFiles']} images in your library. </br>
1063
+ The plugin will replace the original images with the smaller, compressed ones.<BR>
1064
+ ";
1065
+
1066
+ if ( $this->_backupImages )
1067
+ $message .= "A backup folder stores the originals in a separate place on your server.<BR>";
1068
+
1069
+ $message .= "
1070
+ Start the compression by clicking the button below.<BR>
1071
+ </br>
1072
+ <form action='' method='POST' >
1073
+ <input type='submit' name='bulkProcess' id='bulkProcess' class='button button-primary' value='Compress all your images'>";
1074
+ }
1075
+ else
1076
+ {
1077
+ $message = "<BR>
1078
+ <p>Before you start the image processing, please take into consideration the extra thumbnails (for each image in your Media Library, Wordress automatically creates smaller images for different screen resolutions, mobile, etc).</p>
1079
+ <p>You now have {$imageCount['mainFiles']} images in your Media Library and {$imageOnlyThumbs} smaller thumbnails. Unless you uncheck the box below, ShortPixel will process a total of {$imageCount['totalFiles']} images.</p>
1080
+ <p>Optimizing thumbnails is important for your mobile website speed. However, if you don't want to optimize thumbs, please uncheck the box below</p>";
1081
+
1082
+ $message .= "<form action='' method='POST' >";
1083
+ if ( $this->_processThumbnails )
1084
+ $message .= "<input type='checkbox' name='thumbnails' checked> Include thumbnails";
1085
+ else
1086
+ $message .= "<input type='checkbox' name='thumbnails'> Include thumbnails";
1087
+
1088
+ $message .= "<BR><BR><input type='submit' name='bulkProcess' id='bulkProcess' class='button button-primary' value='Compress all your images'>";
1089
+
1090
+ }
1091
 
1092
+
1093
  if ( get_option( 'wp-short-pixel-cancel-pointer') )//add also the resume bulk processing option
1094
  $message .= "&nbsp;&nbsp;&nbsp;<input type='submit' name='bulkProcessResume' id='bulkProcessResume' class='button button-primary' value='Resume cancelled process'>";
1095
 
1101
 
1102
 
1103
  public function getQuotaInformation($apiKey = null, $appendUserAgent = false) {
1104
+
1105
  if(is_null($apiKey)) { $apiKey = $this->_apiKey; }
1106
 
1107
  $requestURL = 'https://api.shortpixel.com/v2/api-status.php';
1113
  if($appendUserAgent) {
1114
  $args['body']['useragent'] = "Agent" . urlencode($_SERVER['HTTP_USER_AGENT']);
1115
  }
 
1116
  $response = wp_remote_post($requestURL, $args);
1117
 
1118
  if(is_wp_error( $response )) //some hosting providers won't allow https:// POST connections so we try http:// as well
1119
  $response = wp_remote_post(str_replace('https://', 'http://', $requestURL), $args);
1120
+
1121
+ if(is_wp_error( $response ))
1122
  $response = wp_remote_get(str_replace('https://', 'http://', $requestURL), $args);
 
 
 
1123
 
1124
  $defaultData = array(
1125
  "APIKeyValid" => false,
1126
+ "Message" => 'API Key could not be validated due to a connectivity error.<BR>It is likely that your hosting provider doesn\'t allow connections to our servers via <i>wp_remote_post</i> command.<BR> If you need further assistance from us please <a href="https://shortpixel.com/contact" target="_blank">contact us</a>',
1127
  "APICallsMade" => 'Information unavailable. Please check your API key.',
1128
  "APICallsQuota" => 'Information unavailable. Please check your API key.');
1129
 
1392
  $resultQuery = $wpdb->get_results($queryMax);
1393
  $startQueryID = $resultQuery[0]->startQueryID;
1394
  $endQueryID = $startQueryID;
 
1395
  }
1396
+
1397
+ //count all the processable files in media library (while limiting the results to max 10000)
1398
+ public function countAllProcessableFiles(){
1399
+ global $wpdb;
1400
+
1401
+ $totalFiles = 0;
1402
+ $mainFiles = 0;
1403
+ $filesList= $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "postmeta
1404
+ WHERE ( meta_key = '_wp_attached_file' OR meta_key = '_wp_attachment_metadata' ) LIMIT 10000");
1405
+
1406
+
1407
+ foreach ( $filesList as $file )
1408
+ {
1409
+ if ( $file->meta_key == "_wp_attached_file" )
1410
+ {//count pdf files only
1411
+ $extension = substr($file->meta_value, strrpos($file->meta_value,".") + 1 );
1412
+ if ( $extension == "pdf" )
1413
+ {
1414
+ $totalFiles++;
1415
+ $mainFiles++;
1416
+ }
1417
+ }
1418
+ else
1419
+ {
1420
+ $attachment = unserialize($file->meta_value);
1421
+ if ( isset($attachment['sizes']) )
1422
+ $totalFiles += count($attachment['sizes']);
1423
+
1424
+ if ( isset($attachment['file']) )
1425
+ {
1426
+ $totalFiles++;
1427
+ $mainFiles++;
1428
+ }
1429
+ }
1430
+ }
1431
+
1432
+ return array("totalFiles" => $totalFiles, "mainFiles" => $mainFiles);
1433
+ }
1434
+
1435
 
1436
  public function migrateBackupFolder() {
1437
  $oldBackupFolder = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ShortpixelBackups';