Version Description
- improved logic for processed files to be downloaded every time
- sometimes the processing script stopped before finishing all the files to be optimized, fixed
- improved processing speed for bulk processing
Download this release
Release Info
Developer | ShortPixel |
Plugin | ShortPixel Image Optimizer |
Version | 2.0.8 |
Comparing to | |
See all releases |
Code changes from version 2.0.7 to 2.0.8
- readme.txt +7 -1
- shortpixel_api.php +42 -14
- wp-shortpixel.php +102 -79
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, optimise pdf, shrink pdf, jpg, jpeg, jpg optimisation, optimise jpg, shrink jpg, gif, animated gif, optimise gif
|
5 |
Requires at least: 3.0.0 or higher
|
6 |
Tested up to: 4.1.1
|
7 |
-
Stable tag: 2.0.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
@@ -107,6 +107,12 @@ The ShortPixel team is here to help. <a href="https://shortpixel.com/contact">Co
|
|
107 |
|
108 |
== Changelog ==
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
= 2.0.7 =
|
111 |
|
112 |
* fixed issue with "missing" images
|
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, optimise pdf, shrink pdf, jpg, jpeg, jpg optimisation, optimise jpg, shrink jpg, gif, animated gif, optimise gif
|
5 |
Requires at least: 3.0.0 or higher
|
6 |
Tested up to: 4.1.1
|
7 |
+
Stable tag: 2.0.8
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
107 |
|
108 |
== Changelog ==
|
109 |
|
110 |
+
= 2.0.8 =
|
111 |
+
|
112 |
+
* improved logic for processed files to be downloaded every time
|
113 |
+
* sometimes the processing script stopped before finishing all the files to be optimized, fixed
|
114 |
+
* improved processing speed for bulk processing
|
115 |
+
|
116 |
= 2.0.7 =
|
117 |
|
118 |
* fixed issue with "missing" images
|
shortpixel_api.php
CHANGED
@@ -38,7 +38,6 @@ class shortpixel_api {
|
|
38 |
}
|
39 |
|
40 |
public function doRequests($urls, $filePath, $ID = null) {
|
41 |
-
|
42 |
if ( !is_array($urls) )
|
43 |
$response = $this->doBulkRequest(array($urls), true);
|
44 |
else
|
@@ -78,7 +77,6 @@ class shortpixel_api {
|
|
78 |
|
79 |
public function parseResponse($response) {
|
80 |
$data = $response['body'];
|
81 |
-
$data = str_replace('Warning: Division by zero in /usr/local/important/web/api.shortpixel.com/lib/functions.php on line 33', '', $data);
|
82 |
$data = $this->parseJSON($data);
|
83 |
return $data;
|
84 |
}
|
@@ -87,12 +85,23 @@ class shortpixel_api {
|
|
87 |
public function processImage($url, $filePaths, $ID = null, $startTime = 0) {
|
88 |
|
89 |
if($startTime == 0) { $startTime = time(); }
|
|
|
90 |
if(time() - $startTime > MAX_EXECUTION_TIME) {//keeps track of time
|
91 |
-
$
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
}
|
97 |
|
98 |
$response = $this->doRequests($url, $filePaths, $ID);//send requests to API
|
@@ -111,7 +120,18 @@ class shortpixel_api {
|
|
111 |
}
|
112 |
|
113 |
$firstImage = $data[0];//extract as object first image
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
switch($firstImage->Status->Code) {
|
116 |
case 1:
|
117 |
//handle image has been scheduled
|
@@ -124,10 +144,13 @@ class shortpixel_api {
|
|
124 |
break;
|
125 |
case -403:
|
126 |
return 'Quota exceeded</br>';
|
|
|
127 |
case -401:
|
128 |
return 'Wrong API Key</br>';
|
|
|
129 |
case -302:
|
130 |
return 'Images does not exists</br>';
|
|
|
131 |
default:
|
132 |
//handle error
|
133 |
if ( isset($data[0]->Status->Message) )
|
@@ -155,7 +178,7 @@ class shortpixel_api {
|
|
155 |
|
156 |
foreach ( $callData as $fileData )//download each file from array and process it
|
157 |
{
|
158 |
-
|
159 |
if ( $counter == 0 )//save percent improvement for main file
|
160 |
$percentImprovement = $fileData->PercentImprovement;
|
161 |
|
@@ -163,8 +186,11 @@ class shortpixel_api {
|
|
163 |
$tempFiles[$counter] = download_url(urldecode($fileData->$fileType));
|
164 |
|
165 |
if(is_wp_error( $tempFiles[$counter] )) //also tries with http instead of https
|
|
|
|
|
166 |
$tempFiles[$counter] = download_url(str_replace('https://', 'http://', urldecode($fileData->$fileType)));
|
167 |
-
|
|
|
168 |
if ( is_wp_error( $tempFiles[$counter] ) ) {
|
169 |
@unlink($tempFiles[$counter]);
|
170 |
return sprintf("Error downloading file (%s)", $tempFiles[$counter]->get_error_message());
|
@@ -254,7 +280,7 @@ class shortpixel_api {
|
|
254 |
$SubDir = trim(substr($meta['file'],0,strrpos($meta['file'],"/")+1));
|
255 |
|
256 |
|
257 |
-
foreach ( $tempFiles as $tempFile )
|
258 |
{
|
259 |
|
260 |
$sourceFile = $tempFile;
|
@@ -294,9 +320,11 @@ class shortpixel_api {
|
|
294 |
$meta['ShortPixelImprovement'] = $percentImprovement;
|
295 |
wp_update_attachment_metadata($ID, $meta);
|
296 |
}
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
|
|
300 |
|
301 |
public function parseJSON($data) {
|
302 |
if ( function_exists('json_decode') ) {
|
38 |
}
|
39 |
|
40 |
public function doRequests($urls, $filePath, $ID = null) {
|
|
|
41 |
if ( !is_array($urls) )
|
42 |
$response = $this->doBulkRequest(array($urls), true);
|
43 |
else
|
77 |
|
78 |
public function parseResponse($response) {
|
79 |
$data = $response['body'];
|
|
|
80 |
$data = $this->parseJSON($data);
|
81 |
return $data;
|
82 |
}
|
85 |
public function processImage($url, $filePaths, $ID = null, $startTime = 0) {
|
86 |
|
87 |
if($startTime == 0) { $startTime = time(); }
|
88 |
+
$apiRetries = get_option('wp-short-pixel-api-retries');
|
89 |
if(time() - $startTime > MAX_EXECUTION_TIME) {//keeps track of time
|
90 |
+
if ( $apiRetries > MAX_API_RETRIES )//we tried to process this time too many times, giving up...
|
91 |
+
{
|
92 |
+
$meta = wp_get_attachment_metadata($ID);
|
93 |
+
$meta['ShortPixelImprovement'] = 'Timed out while processing.';
|
94 |
+
unset($meta['ShortPixel']['WaitingProcessing']);
|
95 |
+
wp_update_attachment_metadata($ID, $meta);
|
96 |
+
}
|
97 |
+
else
|
98 |
+
{//we'll try again next time user visits a page on admin panel
|
99 |
+
$apiRetries++;
|
100 |
+
update_option('wp-short-pixel-api-retries', $apiRetries);
|
101 |
+
exit('Timed out while processing. (pass '.$apiRetries.')');
|
102 |
+
}
|
103 |
+
|
104 |
+
|
105 |
}
|
106 |
|
107 |
$response = $this->doRequests($url, $filePaths, $ID);//send requests to API
|
120 |
}
|
121 |
|
122 |
$firstImage = $data[0];//extract as object first image
|
123 |
+
|
124 |
+
//this part makes sure that all the sizes were processed and ready to be downloaded
|
125 |
+
foreach ( $data as $imageObject )
|
126 |
+
{
|
127 |
+
if ( $imageObject->Status->Code <> 2 )
|
128 |
+
{
|
129 |
+
sleep(2);
|
130 |
+
//echo "\n not ready($apiRetries):" . $imageObject->OriginalURL;//fai
|
131 |
+
return $this->processImage($url, $filePaths, $ID, $startTime);
|
132 |
+
}
|
133 |
+
}
|
134 |
+
|
135 |
switch($firstImage->Status->Code) {
|
136 |
case 1:
|
137 |
//handle image has been scheduled
|
144 |
break;
|
145 |
case -403:
|
146 |
return 'Quota exceeded</br>';
|
147 |
+
break;
|
148 |
case -401:
|
149 |
return 'Wrong API Key</br>';
|
150 |
+
break;
|
151 |
case -302:
|
152 |
return 'Images does not exists</br>';
|
153 |
+
break;
|
154 |
default:
|
155 |
//handle error
|
156 |
if ( isset($data[0]->Status->Message) )
|
178 |
|
179 |
foreach ( $callData as $fileData )//download each file from array and process it
|
180 |
{
|
181 |
+
|
182 |
if ( $counter == 0 )//save percent improvement for main file
|
183 |
$percentImprovement = $fileData->PercentImprovement;
|
184 |
|
186 |
$tempFiles[$counter] = download_url(urldecode($fileData->$fileType));
|
187 |
|
188 |
if(is_wp_error( $tempFiles[$counter] )) //also tries with http instead of https
|
189 |
+
{
|
190 |
+
sleep(2);
|
191 |
$tempFiles[$counter] = download_url(str_replace('https://', 'http://', urldecode($fileData->$fileType)));
|
192 |
+
}
|
193 |
+
|
194 |
if ( is_wp_error( $tempFiles[$counter] ) ) {
|
195 |
@unlink($tempFiles[$counter]);
|
196 |
return sprintf("Error downloading file (%s)", $tempFiles[$counter]->get_error_message());
|
280 |
$SubDir = trim(substr($meta['file'],0,strrpos($meta['file'],"/")+1));
|
281 |
|
282 |
|
283 |
+
foreach ( $tempFiles as $tempFile )//overwrite the original files with the optimized ones
|
284 |
{
|
285 |
|
286 |
$sourceFile = $tempFile;
|
320 |
$meta['ShortPixelImprovement'] = $percentImprovement;
|
321 |
wp_update_attachment_metadata($ID, $meta);
|
322 |
}
|
323 |
+
|
324 |
+
//we reset the retry counter in case of success
|
325 |
+
update_option('wp-short-pixel-api-retries', 0);
|
326 |
+
|
327 |
+
}//end handleSuccess
|
328 |
|
329 |
public function parseJSON($data) {
|
330 |
if ( function_exists('json_decode') ) {
|
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: 2.0.
|
7 |
* Author: ShortPixel
|
8 |
* Author URI: https://shortpixel.com
|
9 |
*/
|
@@ -12,13 +12,13 @@ require_once('shortpixel_api.php');
|
|
12 |
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
13 |
require_once( ABSPATH . 'wp-includes/pluggable.php' );
|
14 |
|
15 |
-
define('PLUGIN_VERSION', "2.0.
|
16 |
define('SP_DEBUG', false);
|
17 |
define('SP_LOG', false);
|
18 |
define('SP_MAX_TIMEOUT', 10);
|
19 |
define('SP_BACKUP_FOLDER', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'ShortpixelBackups');
|
20 |
define('MUST_HAVE_KEY', true);
|
21 |
-
define('
|
22 |
$MAX_EXECUTION_TIME = ini_get('max_execution_time');
|
23 |
if ( is_numeric($MAX_EXECUTION_TIME) )
|
24 |
define('MAX_EXECUTION_TIME', $MAX_EXECUTION_TIME - 1 ); //in seconds
|
@@ -105,8 +105,14 @@ class WPShortPixel {
|
|
105 |
if(get_option('wp-short-pixel-averageCompression') === false) {
|
106 |
add_option( 'wp-short-pixel-averageCompression', 0, '', 'yes' );
|
107 |
}
|
|
|
|
|
|
|
|
|
|
|
108 |
}
|
109 |
|
|
|
110 |
public function setDefaultViewModeList()
|
111 |
{
|
112 |
if(get_option('wp-short-pixel-view-mode') === false)
|
@@ -202,9 +208,8 @@ class WPShortPixel {
|
|
202 |
$meta['ShortPixelImprovement'] = 'File is not an image';
|
203 |
return $meta;
|
204 |
}
|
205 |
-
}
|
206 |
-
|
207 |
-
}
|
208 |
$meta['ShortPixel']['WaitingProcessing'] = true;
|
209 |
return $meta;
|
210 |
}
|
@@ -221,82 +226,28 @@ class WPShortPixel {
|
|
221 |
meta_value LIKE '%\"WaitingProcessing\";b:1;%'
|
222 |
OR meta_value LIKE '%\"BulkProcessing\";b:1;%' )
|
223 |
ORDER BY post_id DESC
|
224 |
-
LIMIT "
|
225 |
$idList = $wpdb->get_results($qry);
|
226 |
|
227 |
if(empty($idList)) { echo 'Empty queue'; die; }
|
228 |
|
229 |
-
|
230 |
-
$
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
if ( !isset($meta['file']) )//this could be a PDF file
|
240 |
-
{
|
241 |
-
$qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
|
242 |
-
WHERE (
|
243 |
-
post_id = $ID AND
|
244 |
-
meta_key = '_wp_attached_file'
|
245 |
-
)";
|
246 |
-
$idList = $wpdb->get_results($qry);
|
247 |
-
$idList = $idList[0];
|
248 |
-
$uploadDir = wp_upload_dir();
|
249 |
-
$filePath = $uploadDir['path'] . DIRECTORY_SEPARATOR . basename($idList->meta_value);
|
250 |
-
|
251 |
-
//check if the image file exists on disk, if not set the right params
|
252 |
-
if(!file_exists($filePath)) {
|
253 |
-
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
254 |
-
if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
|
255 |
-
$meta['ShortPixel']['NoFileOnDisk'] = true;
|
256 |
-
wp_update_attachment_metadata($ID, $meta);
|
257 |
-
die;
|
258 |
-
}
|
259 |
-
|
260 |
-
$imageURLs[] = $uploadDir['url'] . DIRECTORY_SEPARATOR . basename($idList->meta_value);//URL to PDF file
|
261 |
-
$imagePaths[] = $filePath;
|
262 |
-
}
|
263 |
-
else
|
264 |
-
{//process images
|
265 |
-
//check if the image file exists on disk, if not set the right params
|
266 |
-
if(!file_exists($imagePath)) {
|
267 |
-
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
268 |
-
if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
|
269 |
-
$meta['ShortPixel']['NoFileOnDisk'] = true;
|
270 |
-
wp_update_attachment_metadata($ID, $meta);
|
271 |
-
die;
|
272 |
-
}
|
273 |
-
|
274 |
-
//figure out the base URL
|
275 |
-
$SubDir = substr($meta['file'],0,strrpos($meta['file'],"/")+1);
|
276 |
-
$uploadDir = wp_upload_dir();
|
277 |
-
$filesPath = $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $SubDir;//base upload path
|
278 |
-
$filesURL = $uploadDir['baseurl'] . DIRECTORY_SEPARATOR . $SubDir;//base upload url
|
279 |
-
|
280 |
-
//create the images' URL list
|
281 |
-
$imageURLs[$imageIndex] = $filesURL . basename($meta['file']);//main image first
|
282 |
-
$imagePaths[$imageIndex] = $filesPath . basename($meta['file']);
|
283 |
-
$processThumbnails = get_option('wp-short-process_thumbnails');
|
284 |
-
//handle the rest of the thumbnails generated by WP
|
285 |
-
if ( $processThumbnails )
|
286 |
-
{
|
287 |
-
foreach ( $meta['sizes'] as $pictureDetails )
|
288 |
-
{
|
289 |
-
$imageIndex++;
|
290 |
-
$imageURLs[$imageIndex] = $filesURL . $pictureDetails['file'];
|
291 |
-
$imagePaths[$imageIndex] = $filesPath . $pictureDetails['file'];
|
292 |
-
}
|
293 |
-
}
|
294 |
-
}
|
295 |
-
|
296 |
-
$fileCounter++;
|
297 |
}
|
298 |
-
|
299 |
-
|
|
|
|
|
|
|
|
|
300 |
|
301 |
if(is_string($result)) {
|
302 |
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
@@ -317,10 +268,82 @@ class WPShortPixel {
|
|
317 |
|
318 |
$meta['ShortPixelImprovement'] = $result[0]->PercentImprovement;
|
319 |
wp_update_attachment_metadata($ID, $meta);
|
320 |
-
echo "
|
321 |
|
322 |
die();
|
323 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
|
325 |
public function handleManualOptimization() {
|
326 |
$attachmentID = intval($_GET['attachment_ID']);
|
@@ -938,7 +961,7 @@ HTML;
|
|
938 |
return;
|
939 |
}
|
940 |
else
|
941 |
-
print '
|
942 |
} elseif(isset($data['ShortPixel']['WaitingProcessing'])) {
|
943 |
print 'Image waiting to be processed';
|
944 |
return;
|
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: 2.0.8
|
7 |
* Author: ShortPixel
|
8 |
* Author URI: https://shortpixel.com
|
9 |
*/
|
12 |
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
13 |
require_once( ABSPATH . 'wp-includes/pluggable.php' );
|
14 |
|
15 |
+
define('PLUGIN_VERSION', "2.0.8");
|
16 |
define('SP_DEBUG', false);
|
17 |
define('SP_LOG', false);
|
18 |
define('SP_MAX_TIMEOUT', 10);
|
19 |
define('SP_BACKUP_FOLDER', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'ShortpixelBackups');
|
20 |
define('MUST_HAVE_KEY', true);
|
21 |
+
define('MAX_API_RETRIES', 5);
|
22 |
$MAX_EXECUTION_TIME = ini_get('max_execution_time');
|
23 |
if ( is_numeric($MAX_EXECUTION_TIME) )
|
24 |
define('MAX_EXECUTION_TIME', $MAX_EXECUTION_TIME - 1 ); //in seconds
|
105 |
if(get_option('wp-short-pixel-averageCompression') === false) {
|
106 |
add_option( 'wp-short-pixel-averageCompression', 0, '', 'yes' );
|
107 |
}
|
108 |
+
|
109 |
+
if(get_option('wp-short-pixel-api-retries') === false) {//sometimes we need to retry processing/downloading a file multiple times
|
110 |
+
add_option( 'wp-short-pixel-api-retries', 0, '', 'yes' );
|
111 |
+
}
|
112 |
+
|
113 |
}
|
114 |
|
115 |
+
//set default move as "list". only set once, it won't try to set the default mode again.
|
116 |
public function setDefaultViewModeList()
|
117 |
{
|
118 |
if(get_option('wp-short-pixel-view-mode') === false)
|
208 |
$meta['ShortPixelImprovement'] = 'File is not an image';
|
209 |
return $meta;
|
210 |
}
|
211 |
+
}
|
212 |
+
|
|
|
213 |
$meta['ShortPixel']['WaitingProcessing'] = true;
|
214 |
return $meta;
|
215 |
}
|
226 |
meta_value LIKE '%\"WaitingProcessing\";b:1;%'
|
227 |
OR meta_value LIKE '%\"BulkProcessing\";b:1;%' )
|
228 |
ORDER BY post_id DESC
|
229 |
+
LIMIT 0,3";
|
230 |
$idList = $wpdb->get_results($qry);
|
231 |
|
232 |
if(empty($idList)) { echo 'Empty queue'; die; }
|
233 |
|
234 |
+
//send a couple of pre-process requests (if available/needed)
|
235 |
+
if ( isset($idList[1]) )
|
236 |
+
{
|
237 |
+
$itemDetails = $this->returnURLsAndPaths($idList[1]);
|
238 |
+
$this->_apiInterface->doRequests($itemDetails['imageURLs'], $itemDetails['imagePaths']);
|
239 |
+
}
|
240 |
+
if ( isset($idList[2]) )
|
241 |
+
{
|
242 |
+
$itemDetails = $this->returnURLsAndPaths($idList[1]);
|
243 |
+
$this->_apiInterface->doRequests($itemDetails['imageURLs'], $itemDetails['imagePaths']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
}
|
245 |
+
|
246 |
+
//send a request for the latest item
|
247 |
+
$itemDetails = $this->returnURLsAndPaths($idList[0]);
|
248 |
+
$meta = $itemDetails['meta'];
|
249 |
+
$ID = $itemDetails['ID'];
|
250 |
+
$result = $this->_apiInterface->processImage($itemDetails['imageURLs'], $itemDetails['imagePaths'], $ID);//use the API connection to send processing requests for these files.
|
251 |
|
252 |
if(is_string($result)) {
|
253 |
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
268 |
|
269 |
$meta['ShortPixelImprovement'] = $result[0]->PercentImprovement;
|
270 |
wp_update_attachment_metadata($ID, $meta);
|
271 |
+
echo "\nProcessing done succesfully for image #{$ID}";
|
272 |
|
273 |
die();
|
274 |
}
|
275 |
+
|
276 |
+
|
277 |
+
//return urls and paths to be used but other functions
|
278 |
+
public function returnURLsAndPaths($itemDetails)
|
279 |
+
{
|
280 |
+
$imageIndex=0;
|
281 |
+
$ID = $itemDetails->post_id;
|
282 |
+
$imageURL = wp_get_attachment_url($ID);
|
283 |
+
$imagePath = get_attached_file($ID);
|
284 |
+
$meta = wp_get_attachment_metadata($ID);
|
285 |
+
|
286 |
+
if ( !isset($meta['file']) )//this could be a PDF file
|
287 |
+
{
|
288 |
+
$qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
|
289 |
+
WHERE (
|
290 |
+
post_id = $ID AND
|
291 |
+
meta_key = '_wp_attached_file'
|
292 |
+
)";
|
293 |
+
$idList = $wpdb->get_results($qry);
|
294 |
+
$idList = $idList[0];
|
295 |
+
$uploadDir = wp_upload_dir();
|
296 |
+
$filePath = $uploadDir['path'] . DIRECTORY_SEPARATOR . basename($idList->meta_value);
|
297 |
+
|
298 |
+
//check if the image file exists on disk, if not set the right params
|
299 |
+
if(!file_exists($filePath)) {
|
300 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
301 |
+
if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
|
302 |
+
$meta['ShortPixel']['NoFileOnDisk'] = true;
|
303 |
+
wp_update_attachment_metadata($ID, $meta);
|
304 |
+
die;
|
305 |
+
}
|
306 |
+
|
307 |
+
$imageURLs[] = $uploadDir['url'] . DIRECTORY_SEPARATOR . basename($idList->meta_value);//URL to PDF file
|
308 |
+
$imagePaths[] = $filePath;
|
309 |
+
}
|
310 |
+
else
|
311 |
+
{//process images
|
312 |
+
//check if the image file exists on disk, if not set the right params
|
313 |
+
if(!file_exists($imagePath)) {
|
314 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
315 |
+
if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
|
316 |
+
$meta['ShortPixel']['NoFileOnDisk'] = true;
|
317 |
+
wp_update_attachment_metadata($ID, $meta);
|
318 |
+
die;
|
319 |
+
}
|
320 |
+
|
321 |
+
//figure out the base URL
|
322 |
+
$SubDir = substr($meta['file'],0,strrpos($meta['file'],"/")+1);
|
323 |
+
$uploadDir = wp_upload_dir();
|
324 |
+
$filesPath = $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $SubDir;//base upload path
|
325 |
+
$filesURL = $uploadDir['baseurl'] . DIRECTORY_SEPARATOR . $SubDir;//base upload url
|
326 |
+
|
327 |
+
//create the images' URL list
|
328 |
+
$imageURLs[$imageIndex] = $filesURL . basename($meta['file']);//main image first
|
329 |
+
$imagePaths[$imageIndex] = $filesPath . basename($meta['file']);
|
330 |
+
$processThumbnails = get_option('wp-short-process_thumbnails');
|
331 |
+
//handle the rest of the thumbnails generated by WP
|
332 |
+
if ( $processThumbnails )
|
333 |
+
{
|
334 |
+
foreach ( $meta['sizes'] as $pictureDetails )
|
335 |
+
{
|
336 |
+
$imageIndex++;
|
337 |
+
$imageURLs[$imageIndex] = $filesURL . $pictureDetails['file'];
|
338 |
+
$imagePaths[$imageIndex] = $filesPath . $pictureDetails['file'];
|
339 |
+
}
|
340 |
+
}
|
341 |
+
}
|
342 |
+
|
343 |
+
|
344 |
+
return array("imageURLs" => $imageURLs, "imagePaths" => $imagePaths, "meta" => $meta, "ID" => $ID);
|
345 |
+
}
|
346 |
+
|
347 |
|
348 |
public function handleManualOptimization() {
|
349 |
$attachmentID = intval($_GET['attachment_ID']);
|
961 |
return;
|
962 |
}
|
963 |
else
|
964 |
+
print '';
|
965 |
} elseif(isset($data['ShortPixel']['WaitingProcessing'])) {
|
966 |
print 'Image waiting to be processed';
|
967 |
return;
|