Version Description
- some improvements to bulk processing
- PDF files are also optimized now
- fixed a thumb processing bug that caused extra API requests
Download this release
Release Info
Developer | ShortPixel |
Plugin | ShortPixel Image Optimizer |
Version | 2.0.1 |
Comparing to | |
See all releases |
Code changes from version 2.0.0 to 2.0.1
- readme.txt +9 -3
- shortpixel_api.php +222 -222
- wp-shortpixel.php +738 -714
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.1
|
7 |
-
Stable tag: 2.0.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
@@ -20,7 +20,7 @@ ShortPixel uses powerful algorithms that enable your website to load faster, use
|
|
20 |
|
21 |
* **Both lossy and lossless optimisation:** you can choose between the two types of compression. Lossy for photographs. Lossless for technical drawings, clip art and comics.
|
22 |
* **Up to 90% compression rate:** with lossy compression images that were 3MB can crunch to 307Kb, with no before/after differences.
|
23 |
-
* **Supported formats:** JPG, PNG,
|
24 |
* **Backup and restore originals:** if you ever want to return to the original version, images are automatically stored in a backup folder on your hosting servers.
|
25 |
* **Bulk image optimisation:** Crunch your image gallery, and downsize your website. This feature may take up to several hours, depending on the number and size of existing images.
|
26 |
|
@@ -88,7 +88,7 @@ Your images are automatically stored in a backup folder, on your hosting server.
|
|
88 |
|
89 |
= What types of formats can be optimised? =
|
90 |
|
91 |
-
For now, ShortPixel supports JPEG, PNG and GIF formats. Animated GIFs and thumbnails are also optimised. Additional formats are scheduled for optimisation in the future.
|
92 |
|
93 |
= I’m stuck. What do I do? =
|
94 |
|
@@ -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.0 =
|
111 |
|
112 |
* SP plugin uses API v2 and the processing speed is significantly improved
|
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.1
|
7 |
+
Stable tag: 2.0.1
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
20 |
|
21 |
* **Both lossy and lossless optimisation:** you can choose between the two types of compression. Lossy for photographs. Lossless for technical drawings, clip art and comics.
|
22 |
* **Up to 90% compression rate:** with lossy compression images that were 3MB can crunch to 307Kb, with no before/after differences.
|
23 |
+
* **Supported formats:** JPG, PNG, PDF, both static and animated GIFS. NEW UPDATE: we recently introduced optimisation for PDFs.
|
24 |
* **Backup and restore originals:** if you ever want to return to the original version, images are automatically stored in a backup folder on your hosting servers.
|
25 |
* **Bulk image optimisation:** Crunch your image gallery, and downsize your website. This feature may take up to several hours, depending on the number and size of existing images.
|
26 |
|
88 |
|
89 |
= What types of formats can be optimised? =
|
90 |
|
91 |
+
For now, ShortPixel supports JPEG, PNG, PDF and GIF formats. Animated GIFs and thumbnails are also optimised. Additional formats are scheduled for optimisation in the future.
|
92 |
|
93 |
= I’m stuck. What do I do? =
|
94 |
|
107 |
|
108 |
== Changelog ==
|
109 |
|
110 |
+
= 2.0.1 =
|
111 |
+
|
112 |
+
* some improvements to bulk processing
|
113 |
+
* PDF files are also optimized now
|
114 |
+
* fixed a thumb processing bug that caused extra API requests
|
115 |
+
|
116 |
= 2.0.0 =
|
117 |
|
118 |
* SP plugin uses API v2 and the processing speed is significantly improved
|
shortpixel_api.php
CHANGED
@@ -1,229 +1,229 @@
|
|
1 |
<?php
|
2 |
if ( !function_exists( 'download_url' ) ) {
|
3 |
-
|
4 |
}
|
5 |
|
6 |
class shortpixel_api {
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
}
|
1 |
<?php
|
2 |
if ( !function_exists( 'download_url' ) ) {
|
3 |
+
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
4 |
}
|
5 |
|
6 |
class shortpixel_api {
|
7 |
|
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;
|
15 |
+
}
|
16 |
+
|
17 |
+
public function getCompressionType() {
|
18 |
+
return $this->_compressionType;
|
19 |
+
}
|
20 |
+
|
21 |
+
public function setApiKey($apiKey) {
|
22 |
+
$this->_apiKey = $apiKey;
|
23 |
+
}
|
24 |
+
|
25 |
+
public function getApiKey() {
|
26 |
+
return $this->_apiKey;
|
27 |
+
}
|
28 |
+
|
29 |
+
public function __construct($apiKey, $compressionType) {
|
30 |
+
$this->_apiKey = $apiKey;
|
31 |
+
$this->setCompressionType($compressionType);
|
32 |
+
|
33 |
+
add_action('processImageAction', array(&$this, 'processImageAction'), 10, 4);
|
34 |
+
}
|
35 |
+
|
36 |
+
public function processImageAction($url, $filePath, $ID, $time) {
|
37 |
+
$this->processImage($url, $filePath, $ID, $time);
|
38 |
+
}
|
39 |
+
|
40 |
+
public function doRequests($url, $filePath, $ID = null) {
|
41 |
+
$response = $this->doBulkRequest(array($url), true);
|
42 |
+
|
43 |
+
if(is_object($response) && get_class($response) == 'WP_Error') {
|
44 |
+
return false;
|
45 |
+
}
|
46 |
+
|
47 |
+
return $response;
|
48 |
+
}
|
49 |
+
|
50 |
+
public function doBulkRequest($imageList = array(), $blocking = false) {
|
51 |
+
if(!is_array($imageList)) return false;
|
52 |
+
|
53 |
+
$requestParameters = array(
|
54 |
+
'key' => $this->_apiKey,
|
55 |
+
'lossy' => $this->_compressionType,
|
56 |
+
'urllist' => $imageList
|
57 |
+
);
|
58 |
+
|
59 |
+
$response = wp_remote_post($this->_apiEndPoint, array(
|
60 |
+
'method' => 'POST',
|
61 |
+
'timeout' => 45,
|
62 |
+
'redirection' => 5,
|
63 |
+
'httpversion' => '1.0',
|
64 |
+
'blocking' => $blocking,
|
65 |
+
'headers' => array(),
|
66 |
+
'body' => json_encode($requestParameters),
|
67 |
+
'cookies' => array()
|
68 |
+
));
|
69 |
+
|
70 |
+
return $response;
|
71 |
+
|
72 |
+
}
|
73 |
+
|
74 |
+
public function parseResponse($response) {
|
75 |
+
$data = $response['body'];
|
76 |
+
$data = str_replace('Warning: Division by zero in /usr/local/important/web/api.shortpixel.com/lib/functions.php on line 33', '', $data);
|
77 |
+
$data = $this->parseJSON($data);
|
78 |
+
return $data;
|
79 |
+
}
|
80 |
+
|
81 |
+
//handles the processing of the image using the ShortPixel API
|
82 |
+
public function processImage($url, $filePath, $ID = null, $startTime = 0) {
|
83 |
+
if($startTime == 0) { $startTime = time(); }
|
84 |
+
if(time() - $startTime > MAX_EXECUTION_TIME) {
|
85 |
+
$meta = wp_get_attachment_metadata($ID);
|
86 |
+
$meta['ShortPixelImprovement'] = 'Could not determine compression';
|
87 |
+
unset($meta['ShortPixel']['WaitingProcessing']);
|
88 |
+
wp_update_attachment_metadata($ID, $meta);
|
89 |
+
return 'Could not determine compression';
|
90 |
+
}
|
91 |
+
|
92 |
+
$response = $this->doRequests($url, $filePath, $ID);
|
93 |
+
|
94 |
+
if(!$response) return $response;
|
95 |
+
|
96 |
+
if($response['response']['code'] != 200) {
|
97 |
+
WPShortPixel::log("Response 200 OK");
|
98 |
+
printf('Web service did not respond. Please try again later.');
|
99 |
+
return false;
|
100 |
+
}
|
101 |
+
|
102 |
+
$data = $this->parseResponse($response);
|
103 |
+
$data = $data[0];
|
104 |
+
|
105 |
+
switch($data->Status->Code) {
|
106 |
+
case 1:
|
107 |
+
//handle image has been scheduled
|
108 |
+
sleep(1);
|
109 |
+
return $this->processImage($url, $filePath, $ID, $startTime);
|
110 |
+
break;
|
111 |
+
case 2:
|
112 |
+
//handle image has been processed
|
113 |
+
$this->handleSuccess($data, $url, $filePath, $ID);
|
114 |
+
break;
|
115 |
+
case -16:
|
116 |
+
return 'Quota exceeded</br>';
|
117 |
+
case -17:
|
118 |
+
return 'Wrong API Key</br>';
|
119 |
+
case -302:
|
120 |
+
return 'Images does not exists</br>';
|
121 |
+
default:
|
122 |
+
//handle error
|
123 |
+
return $data->Status->Message;
|
124 |
+
}
|
125 |
+
|
126 |
+
return $data;
|
127 |
+
}
|
128 |
+
|
129 |
+
|
130 |
+
public function handleSuccess($callData, $url, $filePath, $ID) {
|
131 |
+
|
132 |
+
if($this->_compressionType) {
|
133 |
+
//lossy
|
134 |
+
$correctFileSize = $callData->LossySize;
|
135 |
+
$tempFile = download_url(urldecode($callData->LossyURL));
|
136 |
+
if(is_wp_error( $tempFile )) {
|
137 |
+
$tempFile = download_url(str_replace('https://', 'http://', urldecode($callData->LossyURL)));
|
138 |
+
}
|
139 |
+
} else {
|
140 |
+
//lossless
|
141 |
+
$correctFileSize = $callData->LoselessSize;
|
142 |
+
$tempFile = download_url(urldecode($callData->LosslessURL));
|
143 |
+
if(is_wp_error( $tempFile )) {
|
144 |
+
$tempFile = download_url(str_replace('https://', 'http://', urldecode($callData->LosslessURL)));
|
145 |
+
}
|
146 |
+
}
|
147 |
+
|
148 |
+
if ( is_wp_error( $tempFile ) ) {
|
149 |
+
@unlink($tempFile);
|
150 |
+
return sprintf("Error downloading file (%s)", $tempFile->get_error_message());
|
151 |
+
die;
|
152 |
+
}
|
153 |
+
|
154 |
+
//check response so that download is OK
|
155 |
+
if(filesize($tempFile) != $correctFileSize) {
|
156 |
+
return sprintf("Error downloading file - incorrect file size");
|
157 |
+
die;
|
158 |
+
}
|
159 |
+
|
160 |
+
if (!file_exists($tempFile)) {
|
161 |
+
return sprintf("Unable to locate downloaded file (%s)", $tempFile);
|
162 |
+
die;
|
163 |
+
}
|
164 |
+
|
165 |
+
//if backup is enabled
|
166 |
+
if(get_option('wp-short-backup_images')) {
|
167 |
+
|
168 |
+
if(!file_exists(SP_BACKUP_FOLDER) && !mkdir(SP_BACKUP_FOLDER, 0777, true)) {
|
169 |
+
return sprintf("Backup folder does not exist and it could not be created");
|
170 |
+
}
|
171 |
+
|
172 |
+
$source = $filePath;
|
173 |
+
$destination = SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($source);
|
174 |
+
|
175 |
+
if(is_writable(SP_BACKUP_FOLDER)) {
|
176 |
+
if(!file_exists($destination)) {
|
177 |
+
@copy($source, $destination);
|
178 |
+
}
|
179 |
+
} else {
|
180 |
+
return sprintf("Backup folder exists but is not writable");
|
181 |
+
}
|
182 |
+
}
|
183 |
+
|
184 |
+
@unlink( $filePath );
|
185 |
+
$success = @rename( $tempFile, $filePath );
|
186 |
+
|
187 |
+
if (!$success) {
|
188 |
+
$copySuccess = copy($tempFile, $filePath);
|
189 |
+
unlink($tempFile);
|
190 |
+
}
|
191 |
+
|
192 |
+
if($success || $copySuccess) {
|
193 |
+
//update statistics
|
194 |
+
if(isset($callData->LossySize)) {
|
195 |
+
$savedSpace = $callData->OriginalSize - $callData->LossySize;
|
196 |
+
} else {
|
197 |
+
$savedSpace = $callData->OriginalSize - $callData->LoselessSize;
|
198 |
+
}
|
199 |
+
|
200 |
+
update_option(
|
201 |
+
'wp-short-pixel-savedSpace',
|
202 |
+
get_option('wp-short-pixel-savedSpace') + $savedSpace
|
203 |
+
);
|
204 |
+
$averageCompression = get_option('wp-short-pixel-averageCompression') * get_option('wp-short-pixel-fileCount');
|
205 |
+
$averageCompression += $callData->PercentImprovement;
|
206 |
+
$averageCompression = $averageCompression / (get_option('wp-short-pixel-fileCount') + 1);
|
207 |
+
update_option('wp-short-pixel-averageCompression', $averageCompression);
|
208 |
+
update_option('wp-short-pixel-fileCount', get_option('wp-short-pixel-fileCount')+1);
|
209 |
+
|
210 |
+
//update metadata
|
211 |
+
if(isset($ID)) {
|
212 |
+
$meta = wp_get_attachment_metadata($ID);
|
213 |
+
$meta['ShortPixelImprovement'] = $callData->PercentImprovement;
|
214 |
+
wp_update_attachment_metadata($ID, $meta);
|
215 |
+
}
|
216 |
+
}
|
217 |
+
}
|
218 |
+
|
219 |
+
public function parseJSON($data) {
|
220 |
+
if ( function_exists('json_decode') ) {
|
221 |
+
$data = json_decode( $data );
|
222 |
+
} else {
|
223 |
+
require_once( 'JSON/JSON.php' );
|
224 |
+
$json = new Services_JSON( );
|
225 |
+
$data = $json->decode( $data );
|
226 |
+
}
|
227 |
+
return $data;
|
228 |
+
}
|
229 |
}
|
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 |
*/
|
@@ -21,406 +21,413 @@ define('MAX_EXECUTION_TIME', 30); //in seconds
|
|
21 |
|
22 |
class WPShortPixel {
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%'
|
183 |
OR meta_value LIKE '%\"BulkProcessing\";b:1;%'
|
184 |
LIMIT " . BATCH_SIZE;
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
WHERE meta_value LIKE '%\"BulkProcessing\";b:1;%'";
|
419 |
-
|
420 |
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
Bulk optimisation has started. This process will take some time, depending on the number of images in your library. <BR>Do not worry about the slow speed, it is a necessary measure in order not to interfere with the normal functioning of your site.<BR><BR>
|
425 |
This is a brief estimation of the bulk processing times:<BR>
|
426 |
1 to 100 images < 20 min <BR>
|
@@ -432,38 +439,38 @@ class WPShortPixel {
|
|
432 |
In the meantime, you can continue using the admin as usual.<BR>
|
433 |
However, <b>you musn’t close the WordPress admin</b>, or the bulk processing will stop.
|
434 |
</p>";
|
435 |
-
|
436 |
<script type="text/javascript" >
|
437 |
var bulkProcessingRunning = true;
|
438 |
</script>
|
439 |
';
|
440 |
|
441 |
-
|
442 |
-
|
443 |
|
444 |
-
|
445 |
|
446 |
-
|
447 |
<a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
|
448 |
<a class="button button-secondary" href="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk&cancel=1">Cancel Processing</a>
|
449 |
';
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
<p>Go to the ShortPixel <a href='" . get_admin_url() . "options-general.php?page=wp-shortpixel#facts'>
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
<script type="text/javascript" >
|
461 |
var bulkProcessingRunning = false;
|
462 |
</script>
|
463 |
';
|
464 |
-
|
465 |
|
466 |
-
|
467 |
<script type="text/javascript" >
|
468 |
jQuery(document).ready(function() {
|
469 |
if(bulkProcessingRunning) {
|
@@ -477,77 +484,77 @@ class WPShortPixel {
|
|
477 |
});
|
478 |
</script>
|
479 |
';
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
<a href="https://shortpixel.com">ShortPixel.com</a> |
|
489 |
<a href="https://wordpress.org/plugins/shortpixel-image-optimiser/installation/">Installation </a> |
|
490 |
<a href="https://wordpress.org/support/plugin/shortpixel-image-optimiser">Support </a>
|
491 |
</p>';
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
<form name='wp_shortpixel_options' action='' method='post' id='wp_shortpixel_options'>
|
552 |
<table class="form-table">
|
553 |
<tbody><tr>
|
@@ -558,48 +565,52 @@ class WPShortPixel {
|
|
558 |
</tr>
|
559 |
HTML;
|
560 |
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
<tr><th scope="row">
|
569 |
-
<label for="compressionType">Compression type
|
570 |
-
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.
|
571 |
-
Lossless compression: the shrunk image will be identical with the original and smaller in size. Use this when you do not want to loose any of the original image's details. Works best for technical drawings, clip art and comics.
|
572 |
-
">?</span></label>
|
573 |
</th><td>
|
574 |
HTML;
|
575 |
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
|
584 |
-
|
585 |
</td>
|
586 |
</tr>
|
587 |
-
|
588 |
-
<
|
589 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
590 |
</tr>
|
591 |
<tr>
|
592 |
-
<th scope="row"><label for="backupImages">
|
593 |
-
<span title="If selected all images will be backed up in the ShortpixelBackups folder located in your wp-content folder">?</span>
|
594 |
-
</label></th>
|
595 |
<td>
|
596 |
-
<input name="backupImages" type="checkbox" id="backupImages" {$checkedBackupImages}>
|
597 |
</td>
|
598 |
</tr>
|
599 |
</tbody></table>
|
600 |
<p class="submit">
|
601 |
<input type="submit" name="submit" id="submit" class="button button-primary" title="Save Changes" value="Save Changes">
|
602 |
-
|
603 |
</p>
|
604 |
</form>
|
605 |
<script>
|
@@ -616,52 +627,62 @@ for(var i = 0; i < rad.length; i++) {
|
|
616 |
}
|
617 |
</script>
|
618 |
HTML;
|
619 |
-
|
620 |
|
621 |
-
|
622 |
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
|
|
|
|
630 |
|
631 |
-
|
632 |
<a id="facts"></a>
|
633 |
-
<h3>ShortPixel
|
634 |
<table class="form-table">
|
635 |
<tbody><tr>
|
636 |
-
<th scope="row"><label for="totalFiles">
|
637 |
<td>$fileCount</td>
|
638 |
</tr>
|
639 |
<tr>
|
640 |
-
<th scope="row"><label for="savedSpace">Saved space by ShortPixel</label></th>
|
641 |
<td>$savedSpace</td>
|
642 |
</tr>
|
643 |
<tr>
|
644 |
<th scope="row"><label for="savedBandwidth">Bandwith* saved with ShortPixel:</label></th>
|
645 |
<td>$savedBandwidth</td>
|
646 |
</tr>
|
647 |
-
|
|
|
|
|
|
|
|
|
648 |
<th scope="row"><label for="apiQuota">Your ShortPixel plan</label></th>
|
649 |
-
<td>{$quotaData['APICallsQuota']}</td>
|
650 |
</tr>
|
651 |
<tr>
|
652 |
-
<th scope="row"><label for="usedQUota">
|
653 |
<td>{$quotaData['APICallsMade']}</td>
|
654 |
</tr>
|
655 |
<tr>
|
656 |
-
<th scope="row"><label for="
|
|
|
|
|
|
|
|
|
657 |
<td>$averageCompression%</td>
|
658 |
</tr>
|
659 |
HTML;
|
660 |
-
|
661 |
-
|
662 |
<form action="" method="POST">
|
663 |
<tr>
|
664 |
-
<th scope="row"><label for="sizeBackup">
|
665 |
<td>
|
666 |
{$backupFolderSize}
|
667 |
<input type="submit" style="margin-left: 15px; vertical-align: middle;" class="button button-secondary" name="emptyBackup" value="Empty backups"/>
|
@@ -669,18 +690,17 @@ HTML;
|
|
669 |
</tr>
|
670 |
</form>
|
671 |
HTML;
|
672 |
-
|
673 |
|
674 |
-
|
675 |
</tbody></table>
|
676 |
-
<p>* Saved bandwidth is calculated at 100,000 impressions/image</p>
|
677 |
HTML;
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
|
682 |
-
|
683 |
-
|
684 |
</br>
|
685 |
Currently, you have {$imageCount} images in your library. </br>
|
686 |
</br>
|
@@ -688,185 +708,189 @@ Currently, you have {$imageCount} images in your library. </br>
|
|
688 |
<input type="submit" name="bulkProcess" id="bulkProcess" class="button button-primary" value="Compress all your images">
|
689 |
</form>
|
690 |
HTML;
|
691 |
-
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
-
|
748 |
-
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
|
|
|
|
|
|
|
|
870 |
|
871 |
|
872 |
}
|
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.1
|
7 |
* Author: ShortPixel
|
8 |
* Author URI: https://shortpixel.com
|
9 |
*/
|
21 |
|
22 |
class WPShortPixel {
|
23 |
|
24 |
+
private $_apiInterface = null;
|
25 |
+
private $_apiKey = '';
|
26 |
+
private $_compressionType = 1;
|
27 |
+
private $_processThumbnails = 1;
|
28 |
+
private $_backupImages = 1;
|
29 |
+
private $_verifiedKey = false;
|
30 |
+
|
31 |
+
public function __construct() {
|
32 |
+
$this->populateOptions();
|
33 |
+
|
34 |
+
$this->_apiInterface = new shortpixel_api($this->_apiKey, $this->_compressionType);
|
35 |
+
|
36 |
+
//add hook for image upload processing
|
37 |
+
add_filter( 'wp_generate_attachment_metadata', array( &$this, 'handleImageUpload' ), 10, 2 );
|
38 |
+
add_filter( 'manage_media_columns', array( &$this, 'columns' ) );
|
39 |
+
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'generatePluginLinks'));
|
40 |
+
|
41 |
+
//add_action( 'admin_footer', array(&$this, 'handleImageProcessing'));
|
42 |
+
add_action( 'manage_media_custom_column', array( &$this, 'generateCustomColumn' ), 10, 2 );
|
43 |
+
|
44 |
+
//add settings page
|
45 |
+
add_action( 'admin_menu', array( &$this, 'registerSettingsPage' ) );
|
46 |
+
add_action( 'admin_menu', array( &$this, 'registerAdminPage' ) );
|
47 |
+
add_action( 'delete_attachment', array( &$this, 'handleDeleteAttachmentInBackup' ) );
|
48 |
+
|
49 |
+
//automatic optimization
|
50 |
+
add_action( 'admin_footer', array( &$this, 'my_action_javascript') );
|
51 |
+
add_action( 'wp_ajax_my_action', array( &$this, 'handleImageProcessing') );
|
52 |
+
|
53 |
+
//manual optimization
|
54 |
+
add_action('admin_action_shortpixel_manual_optimize', array(&$this, 'handleManualOptimization'));
|
55 |
+
//backup restore
|
56 |
+
add_action('admin_action_shortpixel_restore_backup', array(&$this, 'handleRestoreBackup'));
|
57 |
+
|
58 |
+
$this->migrateBackupFolder();
|
59 |
+
}
|
60 |
+
|
61 |
+
public function populateOptions() {
|
62 |
+
|
63 |
+
if(get_option('wp-short-pixel-apiKey') !== false) {
|
64 |
+
$this->_apiKey = get_option('wp-short-pixel-apiKey');
|
65 |
+
} else {
|
66 |
+
add_option( 'wp-short-pixel-apiKey', '', '', 'yes' );
|
67 |
+
}
|
68 |
+
|
69 |
+
if(get_option('wp-short-pixel-verifiedKey') !== false) {
|
70 |
+
$this->_verifiedKey = get_option('wp-short-pixel-verifiedKey');
|
71 |
+
}
|
72 |
+
|
73 |
+
if(get_option('wp-short-pixel-compression') !== false) {
|
74 |
+
$this->_compressionType = get_option('wp-short-pixel-compression');
|
75 |
+
} else {
|
76 |
+
add_option('wp-short-pixel-compression', $this->_compressionType, '', 'yes');
|
77 |
+
}
|
78 |
+
|
79 |
+
if(get_option('wp-short-process_thumbnails') !== false) {
|
80 |
+
$this->_processThumbnails = get_option('wp-short-process_thumbnails');
|
81 |
+
} else {
|
82 |
+
add_option('wp-short-process_thumbnails', $this->_processThumbnails, '', 'yes' );
|
83 |
+
}
|
84 |
+
|
85 |
+
if(get_option('wp-short-backup_images') !== false) {
|
86 |
+
$this->_backupImages = get_option('wp-short-backup_images');
|
87 |
+
} else {
|
88 |
+
add_option('wp-short-backup_images', $this->_backupImages, '', 'yes' );
|
89 |
+
}
|
90 |
+
|
91 |
+
if(get_option('wp-short-pixel-fileCount') === false) {
|
92 |
+
add_option( 'wp-short-pixel-fileCount', 0, '', 'yes' );
|
93 |
+
}
|
94 |
+
|
95 |
+
if(get_option('wp-short-pixel-savedSpace') === false) {
|
96 |
+
add_option( 'wp-short-pixel-savedSpace', 0, '', 'yes' );
|
97 |
+
}
|
98 |
+
|
99 |
+
if(get_option('wp-short-pixel-averageCompression') === false) {
|
100 |
+
add_option( 'wp-short-pixel-averageCompression', 0, '', 'yes' );
|
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.search('Empty queue') >= 0 || response.search('Error processing image') >= 0) {
|
118 |
+
console.log('Queue is empty');
|
119 |
+
} else {
|
120 |
+
console.log('Server response: ' + response);
|
121 |
+
sendRequest();
|
122 |
+
}
|
123 |
+
});
|
124 |
+
}
|
125 |
+
</script> <?php
|
126 |
+
}
|
127 |
+
|
128 |
+
function wp_load_admin_js() {
|
129 |
+
add_action('admin_print_footer_scripts', array(&$this, 'add_bulk_actions_via_javascript'));
|
130 |
+
}
|
131 |
+
|
132 |
+
function add_bulk_actions_via_javascript() {
|
133 |
+
?>
|
134 |
+
<script type="text/javascript">
|
135 |
+
jQuery(document).ready(function($){
|
136 |
+
$('select[name^="action"] option:last-child').before('<option value="2">Bulk Optimize</option>');
|
137 |
+
});
|
138 |
+
</script>
|
139 |
+
<?php }
|
140 |
+
|
141 |
+
//handling older
|
142 |
+
public function WPShortPixel() {
|
143 |
+
$this->__construct();
|
144 |
+
}
|
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 |
+
if(self::isProcesable($path) != false) {
|
152 |
+
$this->_apiInterface->doRequests($url, $path, $ID);
|
153 |
+
|
154 |
+
//send request for thumbs as well
|
155 |
+
if($this->_processThumbnails && !empty($meta['sizes'])) {
|
156 |
+
foreach($meta['sizes'] as $thumbnailInfo) {
|
157 |
+
$thumbURL = str_replace(basename($url), $thumbnailInfo['file'], $url);
|
158 |
+
$thumbPath = str_replace(basename($path), $thumbnailInfo['file'], $path);
|
159 |
+
$this->_apiInterface->doRequests($thumbURL, $thumbPath);
|
160 |
+
}
|
161 |
+
}
|
162 |
+
} else {
|
163 |
+
$meta['ShortPixelImprovement'] = 'File is not an image';
|
164 |
+
return $meta;
|
165 |
+
}
|
166 |
+
} else {
|
167 |
+
|
168 |
+
}
|
169 |
+
$meta['ShortPixel']['WaitingProcessing'] = true;
|
170 |
+
return $meta;
|
171 |
+
}
|
172 |
+
|
173 |
+
public function handleImageProcessing($ID = null) {
|
174 |
+
if(MUST_HAVE_KEY && $this->_verifiedKey == false) {
|
175 |
+
echo "Missing API Key";
|
176 |
+
die();
|
177 |
+
}
|
178 |
+
|
179 |
+
//query database for first found entry that needs processing
|
180 |
+
global $wpdb;
|
181 |
+
$qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
|
182 |
WHERE meta_value LIKE '%\"WaitingProcessing\";b:1;%'
|
183 |
OR meta_value LIKE '%\"BulkProcessing\";b:1;%'
|
184 |
LIMIT " . BATCH_SIZE;
|
185 |
+
$idList = $wpdb->get_results($qry);
|
186 |
+
|
187 |
+
if(empty($idList)) { echo 'Empty queue'; die; }
|
188 |
+
|
189 |
+
foreach($idList as $post) {
|
190 |
+
$ID = $post->post_id;
|
191 |
+
$imageURL = wp_get_attachment_url($ID);
|
192 |
+
$imagePath = get_attached_file($ID);
|
193 |
+
$meta = wp_get_attachment_metadata($ID);
|
194 |
+
|
195 |
+
//check if image is public
|
196 |
+
if(wp_remote_retrieve_response_code($imageURL) > 400) {
|
197 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
198 |
+
if(isset($met['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
|
199 |
+
wp_update_attachment_metadata($ID, $meta);
|
200 |
+
die;
|
201 |
+
}
|
202 |
+
|
203 |
+
$result = $this->_apiInterface->processImage($imageURL, $imagePath, $ID);
|
204 |
+
|
205 |
+
if(is_string($result)) {
|
206 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) { unset($meta['ShortPixel']['BulkProcessing']); }
|
207 |
+
if(isset($meta['ShortPixel']['WaitingProcessing'])) { unset($meta['ShortPixel']['WaitingProcessing']); }
|
208 |
+
$meta['ShortPixelImprovement'] = $result;
|
209 |
+
wp_update_attachment_metadata($ID, $meta);
|
210 |
+
echo "Error processing image: " . $result;
|
211 |
+
die;
|
212 |
+
}
|
213 |
+
|
214 |
+
$processThumbnails = get_option('wp-short-process_thumbnails');
|
215 |
+
|
216 |
+
//handle the rest of the thumbnails generated by WP
|
217 |
+
if($processThumbnails && $result && !empty($meta['sizes'])) {
|
218 |
+
foreach($meta['sizes'] as $thumbnailInfo) {
|
219 |
+
$thumbURL = str_replace(basename($imagePath), $thumbnailInfo['file'], $imageURL);
|
220 |
+
$thumbPath = str_replace(basename($imagePath), $thumbnailInfo['file'], $imagePath);
|
221 |
+
$this->_apiInterface->processImage($thumbURL, $thumbPath);
|
222 |
+
}
|
223 |
+
}
|
224 |
+
|
225 |
+
unset($meta['ShortPixel']['WaitingProcessing']);
|
226 |
+
|
227 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) {
|
228 |
+
unset($meta['ShortPixel']['BulkProcessing']);
|
229 |
+
}
|
230 |
+
|
231 |
+
$meta['ShortPixelImprovement'] = $result->PercentImprovement;
|
232 |
+
|
233 |
+
wp_update_attachment_metadata($ID, $meta);
|
234 |
+
|
235 |
+
echo "Processing done succesfully for image #{$ID}";
|
236 |
+
}
|
237 |
+
|
238 |
+
die();
|
239 |
+
}
|
240 |
+
|
241 |
+
public function handleManualOptimization() {
|
242 |
+
$attachmentID = intval($_GET['attachment_ID']);
|
243 |
+
|
244 |
+
$url = wp_get_attachment_url($attachmentID);
|
245 |
+
$filePath = get_attached_file($attachmentID);
|
246 |
+
$meta = wp_get_attachment_metadata($attachmentID);
|
247 |
+
|
248 |
+
$result = $this->_apiInterface->processImage($url, $filePath, $attachmentID);
|
249 |
+
|
250 |
+
$processThumbnails = get_option('wp-short-process_thumbnails');
|
251 |
+
|
252 |
+
//handle the rest of the thumbnails generated by WP
|
253 |
+
if($processThumbnails && $result && !empty($meta['sizes'])) {
|
254 |
+
foreach($meta['sizes'] as $thumbnailInfo) {
|
255 |
+
$thumbURL = str_replace(basename($filePath), $thumbnailInfo['file'], $filePath);
|
256 |
+
$thumbPath = str_replace(basename($filePath), $thumbnailInfo['file'], $filePath);
|
257 |
+
$this->_apiInterface->processImage($thumbURL, $thumbPath);
|
258 |
+
}
|
259 |
+
}
|
260 |
+
|
261 |
+
// store the referring webpage location
|
262 |
+
$sendback = wp_get_referer();
|
263 |
+
// sanitize the referring webpage location
|
264 |
+
$sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
|
265 |
+
// send the user back where they came from
|
266 |
+
wp_redirect($sendback);
|
267 |
+
// we are done,
|
268 |
+
}
|
269 |
+
|
270 |
+
public function handleRestoreBackup() {
|
271 |
+
$attachmentID = intval($_GET['attachment_ID']);
|
272 |
+
|
273 |
+
$uploadFilePath = get_attached_file($attachmentID);
|
274 |
+
$meta = wp_get_attachment_metadata($attachmentID);
|
275 |
+
$pathInfo = pathinfo($uploadFilePath);
|
276 |
+
|
277 |
+
try {
|
278 |
+
//main file
|
279 |
+
@rename(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($uploadFilePath), $uploadFilePath);
|
280 |
+
//overwriting thumbnails
|
281 |
+
if(is_array($meta["sizes"])) {
|
282 |
+
foreach($meta["sizes"] as $size => $imageData) {
|
283 |
+
$source = SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $imageData['file'];
|
284 |
+
$destination = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $imageData['file'];
|
285 |
+
@rename($source, $destination);
|
286 |
+
}
|
287 |
+
}
|
288 |
+
|
289 |
+
unset($meta["ShortPixelImprovement"]);
|
290 |
+
wp_update_attachment_metadata($attachmentID, $meta);
|
291 |
+
|
292 |
+
} catch(Exception $e) {
|
293 |
+
//what to do, what to do?
|
294 |
+
}
|
295 |
+
// store the referring webpage location
|
296 |
+
$sendback = wp_get_referer();
|
297 |
+
// sanitize the referring webpage location
|
298 |
+
$sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
|
299 |
+
// send the user back where they came from
|
300 |
+
wp_redirect($sendback);
|
301 |
+
// we are done
|
302 |
+
}
|
303 |
+
|
304 |
+
|
305 |
+
public function handleDeleteAttachmentInBackup($ID) {
|
306 |
+
$uploadFilePath = get_attached_file($ID);
|
307 |
+
$meta = wp_get_attachment_metadata($ID);
|
308 |
+
if(self::isProcesable($uploadFilePath) != false) {
|
309 |
+
try {
|
310 |
+
//main file
|
311 |
+
@unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . basename($uploadFilePath));
|
312 |
+
//overwriting thumbnails
|
313 |
+
if(isset($meta["sizes"])) {
|
314 |
+
foreach($meta["sizes"] as $size => $imageData) {
|
315 |
+
@unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $imageData['file']);
|
316 |
+
}
|
317 |
+
}
|
318 |
+
} catch(Exception $e) {
|
319 |
+
//what to do, what to do?
|
320 |
+
}
|
321 |
+
}
|
322 |
+
}
|
323 |
+
|
324 |
+
public function bulkOptimizeActionHandler($hook) {
|
325 |
+
if($hook == 'upload.php') {
|
326 |
+
if($_GET['action'] == 2) {
|
327 |
+
if(!empty($_GET['media'])) {
|
328 |
+
$imageLog = array();
|
329 |
+
//remove all ShortPixel data from metadata
|
330 |
+
foreach($_GET['media'] as $attachmentID) {
|
331 |
+
$meta = wp_get_attachment_metadata($attachmentID);
|
332 |
+
$meta['ShortPixel']['BulkProcessing'] = true;
|
333 |
+
unset($meta['ShortPixelImprovement']);
|
334 |
+
wp_update_attachment_metadata($attachmentID, $meta);
|
335 |
+
$imageLog[$attachmentID] = false;
|
336 |
+
}
|
337 |
+
update_option('bulkProcessingLog', $imageLog);
|
338 |
+
}
|
339 |
+
}
|
340 |
+
}
|
341 |
+
}
|
342 |
+
|
343 |
+
public function registerSettingsPage() {
|
344 |
+
add_options_page( 'ShortPixel Settings', 'ShortPixel', 'manage_options', 'wp-shortpixel', array($this, 'renderSettingsMenu'));
|
345 |
+
}
|
346 |
+
|
347 |
+
function registerAdminPage( ) {
|
348 |
+
add_media_page( 'ShortPixel Bulk Process', 'Bulk ShortPixel', 'edit_others_posts', 'wp-short-pixel-bulk', array( &$this, 'bulkProcesss' ) );
|
349 |
+
}
|
350 |
+
|
351 |
+
public function bulkProcesss() {
|
352 |
+
echo '<h1>Bulk Image Optimisation by ShortPixel</h1>';
|
353 |
+
|
354 |
+
if(MUST_HAVE_KEY && $this->_verifiedKey == false) {
|
355 |
+
echo "<p>In order to start processing your images, you need to validate your API key in the ShortPixel Settings. If you don’t have an API Key, you can get one delivered to your inbox.</p>";
|
356 |
+
echo "<p>Don’t have an API Key yet? Get it now at <a href=\"https://shortpixel.com/wp-apikey\" target=\"_blank\">www.ShortPixel.com</a>, for free.</p>";
|
357 |
+
return;
|
358 |
+
}
|
359 |
+
|
360 |
+
$attachments = null;
|
361 |
+
$attachments = get_posts( array(
|
362 |
+
'numberposts' => -1,
|
363 |
+
'post_type' => 'attachment',
|
364 |
+
'post_mime_type' => 'image'
|
365 |
+
));
|
366 |
+
|
367 |
+
if($_GET['cancel']) {
|
368 |
+
foreach($attachments as $attachment) {
|
369 |
+
$meta = wp_get_attachment_metadata($attachment->ID);
|
370 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) unset($meta['ShortPixel']['BulkProcessing']);
|
371 |
+
wp_update_attachment_metadata($attachment->ID, $meta);
|
372 |
+
}
|
373 |
+
}
|
374 |
+
|
375 |
+
if($_POST["bulkProcess"]) {
|
376 |
+
//remove all ShortPixel data from metadata
|
377 |
+
$imageList = array();
|
378 |
+
$processThumbnails = get_option('wp-short-process_thumbnails');
|
379 |
+
|
380 |
+
foreach($attachments as $attachment) {
|
381 |
+
if(self::isProcesable(get_attached_file($attachment->ID)) == false) continue;
|
382 |
+
|
383 |
+
//prepare bulk call for processing
|
384 |
+
$imagePath = wp_get_attachment_url($attachment->ID);
|
385 |
+
$imageList[] = $imagePath;
|
386 |
+
|
387 |
+
$meta = wp_get_attachment_metadata($attachment->ID);
|
388 |
+
|
389 |
+
|
390 |
+
if($processThumbnails && isset($meta['sizes'])) {
|
391 |
+
foreach($meta['sizes'] as $thumbnailData) {
|
392 |
+
$thumbPath = substr($imagePath, 0, strrpos($imagePath, '/')) . '/' . $thumbnailData["file"];
|
393 |
+
$imageList[] = $thumbPath;
|
394 |
+
}
|
395 |
+
}
|
396 |
+
|
397 |
+
$meta['ShortPixel']['BulkProcessing'] = true;
|
398 |
+
wp_update_attachment_metadata($attachment->ID, $meta);
|
399 |
+
}
|
400 |
+
|
401 |
+
if(count($imageList) > 100) {
|
402 |
+
$batchList = array();
|
403 |
+
foreach($imageList as $image) {
|
404 |
+
$batchList[] = $image;
|
405 |
+
|
406 |
+
if(count($batchList) == 100) {
|
407 |
+
$this->_apiInterface->doBulkRequest($batchList);
|
408 |
+
$batchList = array();
|
409 |
+
}
|
410 |
+
}
|
411 |
+
//send the rest of the images in the list
|
412 |
+
if(count($batchList) > 0) { $this->_apiInterface->doBulkRequest($batchList); }
|
413 |
+
|
414 |
+
} else {
|
415 |
+
$this->_apiInterface->doBulkRequest($imageList);
|
416 |
+
}
|
417 |
+
|
418 |
+
|
419 |
+
|
420 |
+
update_option('bulkProcessingStatus', 'running');
|
421 |
+
}
|
422 |
+
|
423 |
+
global $wpdb;
|
424 |
+
$qry = "SELECT * FROM " . $wpdb->prefix . "postmeta
|
425 |
WHERE meta_value LIKE '%\"BulkProcessing\";b:1;%'";
|
426 |
+
$idList = $wpdb->get_results($qry);
|
427 |
|
428 |
+
if(!empty($idList)) {
|
429 |
+
if(is_array($idList)) {
|
430 |
+
echo "<p>
|
431 |
Bulk optimisation has started. This process will take some time, depending on the number of images in your library. <BR>Do not worry about the slow speed, it is a necessary measure in order not to interfere with the normal functioning of your site.<BR><BR>
|
432 |
This is a brief estimation of the bulk processing times:<BR>
|
433 |
1 to 100 images < 20 min <BR>
|
439 |
In the meantime, you can continue using the admin as usual.<BR>
|
440 |
However, <b>you musn’t close the WordPress admin</b>, or the bulk processing will stop.
|
441 |
</p>";
|
442 |
+
echo '
|
443 |
<script type="text/javascript" >
|
444 |
var bulkProcessingRunning = true;
|
445 |
</script>
|
446 |
';
|
447 |
|
448 |
+
$imagesLeft = count($idList);
|
449 |
+
$totalImages = count($attachments);
|
450 |
|
451 |
+
echo "<p>{$imagesLeft} out of {$totalImages} images left to process.</p>";
|
452 |
|
453 |
+
echo '
|
454 |
<a class="button button-secondary" href="' . get_admin_url() . 'upload.php">Media Library</a>
|
455 |
<a class="button button-secondary" href="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk&cancel=1">Cancel Processing</a>
|
456 |
';
|
457 |
+
}
|
458 |
+
} else {
|
459 |
+
$bulkProcessingStatus = get_option('bulkProcessingStatus');
|
460 |
+
if(isset($bulkProcessingStatus) && $bulkProcessingStatus == 'running') {
|
461 |
+
echo "<p>Bulk optimisation was successful. ShortPixel has finished optimising all your images.</p>
|
462 |
+
<p>Go to the ShortPixel <a href='" . get_admin_url() . "options-general.php?page=wp-shortpixel#facts'>Stats</a> and see your website's optimised stats (in Settings > ShortPixel). </p>";
|
463 |
+
delete_option('bulkProcessingStatus');
|
464 |
+
}
|
465 |
+
echo $this->getBulkProcessingForm(count($attachments));
|
466 |
+
echo '
|
467 |
<script type="text/javascript" >
|
468 |
var bulkProcessingRunning = false;
|
469 |
</script>
|
470 |
';
|
471 |
+
}
|
472 |
|
473 |
+
echo '
|
474 |
<script type="text/javascript" >
|
475 |
jQuery(document).ready(function() {
|
476 |
if(bulkProcessingRunning) {
|
484 |
});
|
485 |
</script>
|
486 |
';
|
487 |
+
}
|
488 |
+
|
489 |
+
public function renderSettingsMenu() {
|
490 |
+
if ( !current_user_can( 'manage_options' ) ) {
|
491 |
+
wp_die('You do not have sufficient permissions to access this page.');
|
492 |
+
}
|
493 |
+
echo '<h1>ShortPixel Plugin Settings</h1>';
|
494 |
+
echo '<p>
|
495 |
<a href="https://shortpixel.com">ShortPixel.com</a> |
|
496 |
<a href="https://wordpress.org/plugins/shortpixel-image-optimiser/installation/">Installation </a> |
|
497 |
<a href="https://wordpress.org/support/plugin/shortpixel-image-optimiser">Support </a>
|
498 |
</p>';
|
499 |
+
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="' . get_admin_url() . 'upload.php?page=wp-short-pixel-bulk">Bulk Optimisation Tool</a>.</p>';
|
500 |
+
|
501 |
+
$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>";
|
502 |
+
|
503 |
+
if(isset($_POST['submit']) || isset($_POST['validate'])) {
|
504 |
+
//handle API Key - common for submit and validate
|
505 |
+
$_POST['key'] = trim($_POST['key']);
|
506 |
+
$validityData = $this->getQuotaInformation($_POST['key'], true);
|
507 |
+
|
508 |
+
$this->_apiKey = $_POST['key'];
|
509 |
+
$this->_apiInterface->setApiKey($this->_apiKey);
|
510 |
+
update_option('wp-short-pixel-apiKey', $_POST['key']);
|
511 |
+
if($validityData['APIKeyValid']) {
|
512 |
+
if(isset($_POST['validate'])) {
|
513 |
+
//display notification
|
514 |
+
printf($noticeHTML, '#7ad03a', 'API Key valid!');
|
515 |
+
}
|
516 |
+
update_option('wp-short-pixel-verifiedKey', true);
|
517 |
+
$this->_verifiedKey = true;
|
518 |
+
} else {
|
519 |
+
if(isset($_POST['validate'])) {
|
520 |
+
//display notification
|
521 |
+
printf($noticeHTML, '#dd3d36', $validityData["Message"]);
|
522 |
+
}
|
523 |
+
update_option('wp-short-pixel-verifiedKey', false);
|
524 |
+
$this->_verifiedKey = false;
|
525 |
+
}
|
526 |
+
|
527 |
+
//if save button - we process the rest of the form elements
|
528 |
+
if(isset($_POST['submit'])) {
|
529 |
+
update_option('wp-short-pixel-compression', $_POST['compressionType']);
|
530 |
+
$this->_compressionType = $_POST['compressionType'];
|
531 |
+
$this->_apiInterface->setCompressionType($this->_compressionType);
|
532 |
+
if(isset($_POST['thumbnails'])) { $this->_processThumbnails = 1; } else { $this->_processThumbnails = 0; }
|
533 |
+
if(isset($_POST['backupImages'])) { $this->_backupImages = 1; } else { $this->_backupImages = 0; }
|
534 |
+
update_option('wp-short-process_thumbnails', $this->_processThumbnails);
|
535 |
+
update_option('wp-short-backup_images', $this->_backupImages);
|
536 |
+
}
|
537 |
+
}
|
538 |
+
|
539 |
+
if(isset($_POST['emptyBackup'])) {
|
540 |
+
if(file_exists(SP_BACKUP_FOLDER)) {
|
541 |
+
$files = scandir(SP_BACKUP_FOLDER);
|
542 |
+
$cleanPath = rtrim(SP_BACKUP_FOLDER, '/'). '/';
|
543 |
+
foreach($files as $t) {
|
544 |
+
if ( $t != "." && $t != "..") {
|
545 |
+
unlink(SP_BACKUP_FOLDER . DIRECTORY_SEPARATOR . $t);
|
546 |
+
}
|
547 |
+
}
|
548 |
+
}
|
549 |
+
}
|
550 |
+
|
551 |
+
$checked = '';
|
552 |
+
if($this->_processThumbnails) { $checked = 'checked'; }
|
553 |
+
|
554 |
+
$checkedBackupImages = '';
|
555 |
+
if($this->_backupImages) { $checkedBackupImages = 'checked'; }
|
556 |
+
|
557 |
+
$formHTML = <<< HTML
|
558 |
<form name='wp_shortpixel_options' action='' method='post' id='wp_shortpixel_options'>
|
559 |
<table class="form-table">
|
560 |
<tbody><tr>
|
565 |
</tr>
|
566 |
HTML;
|
567 |
|
568 |
+
if(!$this->_verifiedKey) {
|
569 |
+
//if invalid key we display the link to the API Key
|
570 |
+
$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>';
|
571 |
+
$formHTML .= '</form>';
|
572 |
+
} else {
|
573 |
+
//if valid key we display the rest of the options
|
574 |
+
$formHTML .= <<< HTML
|
575 |
<tr><th scope="row">
|
576 |
+
<label for="compressionType">Compression type:</label>
|
|
|
|
|
|
|
577 |
</th><td>
|
578 |
HTML;
|
579 |
|
580 |
+
if($this->_compressionType == 1) {
|
581 |
+
$formHTML .= '<input type="radio" name="compressionType" value="1" checked>Lossy</br></br>';
|
582 |
+
$formHTML .= '<input type="radio" name="compressionType" value="0" >Lossless';
|
583 |
+
} else {
|
584 |
+
$formHTML .= '<input type="radio" name="compressionType" value="1">Lossy</br></br>';
|
585 |
+
$formHTML .= '<input type="radio" name="compressionType" value="0" checked>Lossless';
|
586 |
+
}
|
587 |
|
588 |
+
$formHTML .= <<<HTML
|
589 |
</td>
|
590 |
</tr>
|
591 |
+
</tbody></table>
|
592 |
+
<p style="color: #818181;">
|
593 |
+
<b>Lossy compression: </b>lossy has a better compression rate than lossless compression.</br>The resulting image
|
594 |
+
is not 100% identical with the original. Works well for photos taken with your camera.</br></br>
|
595 |
+
<b>Lossless compression: </b> the shrunk image will be identical with the original and smaller in size.</br>Use this
|
596 |
+
when you do not want to lose any of the original image's details. Works best for technical drawings,
|
597 |
+
clip art and comics.
|
598 |
+
</p>
|
599 |
+
<table class="form-table">
|
600 |
+
<tbody><tr>
|
601 |
+
<th scope="row"><label for="thumbnails">Image thumbnails:</label></th>
|
602 |
+
<td><input name="thumbnails" type="checkbox" id="thumbnails" {$checked}> Apply compression also to image thumbnails.</td>
|
603 |
</tr>
|
604 |
<tr>
|
605 |
+
<th scope="row"><label for="backupImages">Image backup</label></th>
|
|
|
|
|
606 |
<td>
|
607 |
+
<input name="backupImages" type="checkbox" id="backupImages" {$checkedBackupImages}> Save and keep a backup of your original images in a separate folder.
|
608 |
</td>
|
609 |
</tr>
|
610 |
</tbody></table>
|
611 |
<p class="submit">
|
612 |
<input type="submit" name="submit" id="submit" class="button button-primary" title="Save Changes" value="Save Changes">
|
613 |
+
<a class="button button-primary" title="Process all the images in your Media Library" href="upload.php?page=wp-short-pixel-bulk">Bulk Process</a>
|
614 |
</p>
|
615 |
</form>
|
616 |
<script>
|
627 |
}
|
628 |
</script>
|
629 |
HTML;
|
630 |
+
}
|
631 |
|
632 |
+
echo $formHTML;
|
633 |
|
634 |
+
if($this->_verifiedKey) {
|
635 |
+
$fileCount = get_option('wp-short-pixel-fileCount');
|
636 |
+
$savedSpace = self::formatBytes(get_option('wp-short-pixel-savedSpace'),2);
|
637 |
+
$averageCompression = round(get_option('wp-short-pixel-averageCompression'),2);
|
638 |
+
$savedBandwidth = self::formatBytes(get_option('wp-short-pixel-savedSpace') * 1000,2);
|
639 |
+
$quotaData = $this->getQuotaInformation();
|
640 |
+
$backupFolderSize = self::formatBytes(self::folderSize(SP_BACKUP_FOLDER));
|
641 |
+
$remainingImages = (int)str_replace(',', '', $quotaData['APICallsQuota']) - (int)str_replace(',', '', $quotaData['APICallsMade']);
|
642 |
+
$remainingImages = number_format($remainingImages);
|
643 |
|
644 |
+
$statHTML = <<< HTML
|
645 |
<a id="facts"></a>
|
646 |
+
<h3>Your ShortPixel Stats</h3>
|
647 |
<table class="form-table">
|
648 |
<tbody><tr>
|
649 |
+
<th scope="row"><label for="totalFiles">Total number of processed files:</label></th>
|
650 |
<td>$fileCount</td>
|
651 |
</tr>
|
652 |
<tr>
|
653 |
+
<th scope="row"><label for="savedSpace">Saved disk space by ShortPixel</label></th>
|
654 |
<td>$savedSpace</td>
|
655 |
</tr>
|
656 |
<tr>
|
657 |
<th scope="row"><label for="savedBandwidth">Bandwith* saved with ShortPixel:</label></th>
|
658 |
<td>$savedBandwidth</td>
|
659 |
</tr>
|
660 |
+
</tbody></table>
|
661 |
+
|
662 |
+
<p style="padding-top: 0px; color: #818181;" >* Saved bandwidth is calculated at 100,000 impressions/image</p>
|
663 |
+
<table class="form-table">
|
664 |
+
<tbody><tr>
|
665 |
<th scope="row"><label for="apiQuota">Your ShortPixel plan</label></th>
|
666 |
+
<td>{$quotaData['APICallsQuota']}/month</td>
|
667 |
</tr>
|
668 |
<tr>
|
669 |
+
<th scope="row"><label for="usedQUota">Number of images processed this month:</label></th>
|
670 |
<td>{$quotaData['APICallsMade']}</td>
|
671 |
</tr>
|
672 |
<tr>
|
673 |
+
<th scope="row"><label for="remainingImages">Remaining images in your plan: </label></th>
|
674 |
+
<td>{$remainingImages} images</td>
|
675 |
+
</tr>
|
676 |
+
<tr>
|
677 |
+
<th scope="row"><label for="averagCompression">Average compression of your files:</label></th>
|
678 |
<td>$averageCompression%</td>
|
679 |
</tr>
|
680 |
HTML;
|
681 |
+
if($this->_backupImages) {
|
682 |
+
$statHTML .= <<< HTML
|
683 |
<form action="" method="POST">
|
684 |
<tr>
|
685 |
+
<th scope="row"><label for="sizeBackup">Original images are stored in a backup folder. Your backup folder size is now:</label></th>
|
686 |
<td>
|
687 |
{$backupFolderSize}
|
688 |
<input type="submit" style="margin-left: 15px; vertical-align: middle;" class="button button-secondary" name="emptyBackup" value="Empty backups"/>
|
690 |
</tr>
|
691 |
</form>
|
692 |
HTML;
|
693 |
+
}
|
694 |
|
695 |
+
$statHTML .= <<< HTML
|
696 |
</tbody></table>
|
|
|
697 |
HTML;
|
698 |
+
echo $statHTML;
|
699 |
+
}
|
700 |
+
}
|
701 |
|
702 |
+
public function getBulkProcessingForm($imageCount) {
|
703 |
+
return <<< HTML
|
704 |
</br>
|
705 |
Currently, you have {$imageCount} images in your library. </br>
|
706 |
</br>
|
708 |
<input type="submit" name="bulkProcess" id="bulkProcess" class="button button-primary" value="Compress all your images">
|
709 |
</form>
|
710 |
HTML;
|
711 |
+
}
|
712 |
+
|
713 |
+
|
714 |
+
public function getQuotaInformation($apiKey = null, $appendUserAgent = false) {
|
715 |
+
|
716 |
+
if(is_null($apiKey)) { $apiKey = $this->_apiKey; }
|
717 |
+
|
718 |
+
$requestURL = 'https://api.shortpixel.com/v2/api-status.php';
|
719 |
+
$args = array('timeout'=> SP_MAX_TIMEOUT,
|
720 |
+
'sslverify' => false,
|
721 |
+
'body' => array('key' => $apiKey)
|
722 |
+
);
|
723 |
+
|
724 |
+
if($appendUserAgent) {
|
725 |
+
$args['body']['useragent'] = urlencode($_SERVER['HTTP_USER_AGENT']);
|
726 |
+
}
|
727 |
+
|
728 |
+
$response = wp_remote_post($requestURL, $args);
|
729 |
+
|
730 |
+
if(is_wp_error( $response )) {
|
731 |
+
$response = wp_remote_get(str_replace('https://', 'http://', $requestURL), $args);
|
732 |
+
}
|
733 |
+
|
734 |
+
$defaultData = array(
|
735 |
+
"APIKeyValid" => false,
|
736 |
+
"Message" => 'API Key could not be validated. Could not connect Shortpixel service.',
|
737 |
+
"APICallsMade" => 'Information unavailable. Please check your API key.',
|
738 |
+
"APICallsQuota" => 'Information unavailable. Please check your API key.');
|
739 |
+
|
740 |
+
if(is_object($response) && get_class($response) == 'WP_Error') {
|
741 |
+
return $defaultData;
|
742 |
+
}
|
743 |
+
|
744 |
+
if($response['response']['code'] != 200) {
|
745 |
+
return $defaultData;
|
746 |
+
}
|
747 |
+
|
748 |
+
$data = $response['body'];
|
749 |
+
$data = $this->parseJSON($data);
|
750 |
+
|
751 |
+
if(empty($data)) { return $defaultData; }
|
752 |
+
|
753 |
+
if($data->Status->Code != 2) {
|
754 |
+
$defaultData['Message'] = $data->Status->Message;
|
755 |
+
return $defaultData;
|
756 |
+
}
|
757 |
+
|
758 |
+
return array(
|
759 |
+
"APIKeyValid" => true,
|
760 |
+
"APICallsMade" => number_format($data->APICallsMade) . ' images',
|
761 |
+
"APICallsQuota" => number_format($data->APICallsQuota) . ' images'
|
762 |
+
);
|
763 |
+
|
764 |
+
|
765 |
+
}
|
766 |
+
|
767 |
+
public function generateCustomColumn( $column_name, $id ) {
|
768 |
+
if( 'wp-shortPixel' == $column_name ) {
|
769 |
+
$data = wp_get_attachment_metadata($id);
|
770 |
+
|
771 |
+
if ( isset( $data['ShortPixelImprovement'] ) ) {
|
772 |
+
if(isset($meta['ShortPixel']['BulkProcessing'])) {
|
773 |
+
print 'Waiting for bulk processing';
|
774 |
+
return;
|
775 |
+
}
|
776 |
+
|
777 |
+
print $data['ShortPixelImprovement'];
|
778 |
+
if(is_numeric($data['ShortPixelImprovement'])) {
|
779 |
+
print '%';
|
780 |
+
print " | <a href=\"admin.php?action=shortpixel_restore_backup&attachment_ID={$id}\">Restore backup</a>";
|
781 |
+
return;
|
782 |
+
}
|
783 |
+
} elseif(isset($data['ShortPixel']['WaitingProcessing'])) {
|
784 |
+
print 'Image waiting to be processed';
|
785 |
+
return;
|
786 |
+
} else {
|
787 |
+
if ( wp_attachment_is_image( $id ) ) {
|
788 |
+
print 'Image not processed';
|
789 |
+
print " | <a href=\"admin.php?action=shortpixel_manual_optimize&attachment_ID={$id}\">Optimize now</a>";
|
790 |
+
return;
|
791 |
+
}
|
792 |
+
}
|
793 |
+
}
|
794 |
+
}
|
795 |
+
|
796 |
+
public function columns( $defaults ) {
|
797 |
+
$defaults['wp-shortPixel'] = 'ShortPixel Compression';
|
798 |
+
return $defaults;
|
799 |
+
}
|
800 |
+
|
801 |
+
public function generatePluginLinks($links) {
|
802 |
+
$in = '<a href="options-general.php?page=wp-shortpixel">Settings</a>';
|
803 |
+
array_unshift($links, $in);
|
804 |
+
return $links;
|
805 |
+
}
|
806 |
+
|
807 |
+
public function parseJSON($data) {
|
808 |
+
if ( function_exists('json_decode') ) {
|
809 |
+
$data = json_decode( $data );
|
810 |
+
} else {
|
811 |
+
require_once( 'JSON/JSON.php' );
|
812 |
+
$json = new Services_JSON( );
|
813 |
+
$data = $json->decode( $data );
|
814 |
+
}
|
815 |
+
return $data;
|
816 |
+
}
|
817 |
+
|
818 |
+
|
819 |
+
static public function formatBytes($bytes, $precision = 2) {
|
820 |
+
$units = array('B', 'KB', 'MB', 'GB', 'TB');
|
821 |
+
|
822 |
+
$bytes = max($bytes, 0);
|
823 |
+
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
824 |
+
$pow = min($pow, count($units) - 1);
|
825 |
+
|
826 |
+
$bytes /= pow(1024, $pow);
|
827 |
+
|
828 |
+
return round($bytes, $precision) . ' ' . $units[$pow];
|
829 |
+
}
|
830 |
+
|
831 |
+
static public function isProcesable($path) {
|
832 |
+
$pathParts = pathinfo($path);
|
833 |
+
if($pathParts['extension'] == 'pdf') {
|
834 |
+
return true;
|
835 |
+
}
|
836 |
+
|
837 |
+
if(function_exists('exif_imagetype')) {
|
838 |
+
return exif_imagetype($path);
|
839 |
+
} else {
|
840 |
+
if(in_array($pathParts['extension'], array('jpg', 'jpeg', 'gif', 'png'))) {
|
841 |
+
return true;
|
842 |
+
} else {
|
843 |
+
return false;
|
844 |
+
}
|
845 |
+
}
|
846 |
+
|
847 |
+
}
|
848 |
+
|
849 |
+
static public function folderSize($path) {
|
850 |
+
$total_size = 0;
|
851 |
+
if(file_exists($path)) {
|
852 |
+
$files = scandir($path);
|
853 |
+
} else {
|
854 |
+
return $total_size;
|
855 |
+
}
|
856 |
+
$cleanPath = rtrim($path, '/'). '/';
|
857 |
+
foreach($files as $t) {
|
858 |
+
if ($t<>"." && $t<>"..") {
|
859 |
+
$currentFile = $cleanPath . $t;
|
860 |
+
if (is_dir($currentFile)) {
|
861 |
+
$size = foldersize($currentFile);
|
862 |
+
$total_size += $size;
|
863 |
+
}
|
864 |
+
else {
|
865 |
+
$size = filesize($currentFile);
|
866 |
+
$total_size += $size;
|
867 |
+
}
|
868 |
+
}
|
869 |
+
}
|
870 |
+
return $total_size;
|
871 |
+
}
|
872 |
+
|
873 |
+
public function migrateBackupFolder() {
|
874 |
+
$oldBackupFolder = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ShortpixelBackups';
|
875 |
+
|
876 |
+
if(!file_exists($oldBackupFolder)) return; //if old backup folder does not exist then there is nothing to do
|
877 |
+
|
878 |
+
if(!file_exists(SP_BACKUP_FOLDER)) {
|
879 |
+
//we check that the backup folder exists, if not we create it so we can copy into it
|
880 |
+
if(!mkdir(SP_BACKUP_FOLDER, 0777, true)) return;
|
881 |
+
}
|
882 |
+
|
883 |
+
$scannedDirectory = array_diff(scandir($oldBackupFolder), array('..', '.'));
|
884 |
+
foreach($scannedDirectory as $file) {
|
885 |
+
@rename($oldBackupFolder.DIRECTORY_SEPARATOR.$file, SP_BACKUP_FOLDER.DIRECTORY_SEPARATOR.$file);
|
886 |
+
}
|
887 |
+
$scannedDirectory = array_diff(scandir($oldBackupFolder), array('..', '.'));
|
888 |
+
if(empty($scannedDirectory)) {
|
889 |
+
@rmdir($oldBackupFolder);
|
890 |
+
}
|
891 |
+
|
892 |
+
return;
|
893 |
+
}
|
894 |
|
895 |
|
896 |
}
|