Version Description
- Use Ajax for Bulk Smush to avoid timeouts and internal server error
- Other Notice and bug fixes
- Settings moved under Media > WP Smush.it
- Added debug log file
Download this release
Release Info
Developer | WPMUDEV |
Plugin | Smush Image Compression and Optimization |
Version | 1.7 |
Comparing to | |
See all releases |
Code changes from version 1.6.5.4 to 1.7
- assets/css/wp-smushit-admin.css +226 -0
- assets/js/wp-smushit-admin.js +176 -0
- languages/wp_smushit-default.po +1 -1
- languages/wp_smushit-default.pot +1 -1
- lib/class-wp-smushit-admin.php +420 -0
- lib/class-wp-smushit-bulk.php +40 -0
- lib/error/class-wp-smushit-errorlog.php +80 -0
- lib/error/class-wp-smushit-errorregistry.php +37 -0
- lib/error_log.php +98 -0
- readme.txt +40 -33
- screenshot-1.jpg +0 -0
- uninstall.php +39 -0
- wp-smushit.php +377 -526
assets/css/wp-smushit-admin.css
ADDED
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
Smush button transformation
|
3 |
+
*/
|
4 |
+
.button.wp-smushit-started{
|
5 |
+
padding:0 10px 1px 0;
|
6 |
+
}
|
7 |
+
.button.wp-smushit-started .floatingCirclesG{
|
8 |
+
float:left;
|
9 |
+
}
|
10 |
+
|
11 |
+
.button.wp-smushit-finished,.button.wp-smushit-finished:disabled,.button.wp-smushit-finished[disabled]{
|
12 |
+
color:#fff!important;
|
13 |
+
background: #00cf21!important;
|
14 |
+
border-color: #619f6b!important;
|
15 |
+
-webkit-box-shadow: inset 0 1px 0 rgba(0,207,33,.5),0 1px 0 rgba(0,0,0,.15)!important;
|
16 |
+
box-shadow: inset 0 1px 0 rgba(0,207,33,.5),0 1px 0 rgba(0,0,0,.15)!important;
|
17 |
+
}
|
18 |
+
/*
|
19 |
+
Loader
|
20 |
+
*/
|
21 |
+
#wp-smushit-loader-wrap{
|
22 |
+
display:none;
|
23 |
+
}
|
24 |
+
.floatingCirclesG{
|
25 |
+
position:relative;
|
26 |
+
width:28px;
|
27 |
+
height:28px;
|
28 |
+
-moz-transform:scale(0.6);
|
29 |
+
-webkit-transform:scale(0.6);
|
30 |
+
-ms-transform:scale(0.6);
|
31 |
+
-o-transform:scale(0.6);
|
32 |
+
transform:scale(0.6);
|
33 |
+
}
|
34 |
+
|
35 |
+
.f_circleG{
|
36 |
+
position:absolute;
|
37 |
+
background-color:#FFFFFF;
|
38 |
+
height:5px;
|
39 |
+
width:5px;
|
40 |
+
-moz-border-radius:3px;
|
41 |
+
-moz-animation-name:f_fadeG;
|
42 |
+
-moz-animation-duration:0.8s;
|
43 |
+
-moz-animation-iteration-count:infinite;
|
44 |
+
-moz-animation-direction:linear;
|
45 |
+
-webkit-border-radius:3px;
|
46 |
+
-webkit-animation-name:f_fadeG;
|
47 |
+
-webkit-animation-duration:0.8s;
|
48 |
+
-webkit-animation-iteration-count:infinite;
|
49 |
+
-webkit-animation-direction:linear;
|
50 |
+
-ms-border-radius:3px;
|
51 |
+
-ms-animation-name:f_fadeG;
|
52 |
+
-ms-animation-duration:0.8s;
|
53 |
+
-ms-animation-iteration-count:infinite;
|
54 |
+
-ms-animation-direction:linear;
|
55 |
+
-o-border-radius:3px;
|
56 |
+
-o-animation-name:f_fadeG;
|
57 |
+
-o-animation-duration:0.8s;
|
58 |
+
-o-animation-iteration-count:infinite;
|
59 |
+
-o-animation-direction:linear;
|
60 |
+
border-radius:3px;
|
61 |
+
animation-name:f_fadeG;
|
62 |
+
animation-duration:0.8s;
|
63 |
+
animation-iteration-count:infinite;
|
64 |
+
animation-direction:linear;
|
65 |
+
}
|
66 |
+
|
67 |
+
#frotateG_01{
|
68 |
+
left:0;
|
69 |
+
top:11px;
|
70 |
+
-moz-animation-delay:0.3s;
|
71 |
+
-webkit-animation-delay:0.3s;
|
72 |
+
-ms-animation-delay:0.3s;
|
73 |
+
-o-animation-delay:0.3s;
|
74 |
+
animation-delay:0.3s;
|
75 |
+
}
|
76 |
+
|
77 |
+
#frotateG_02{
|
78 |
+
left:3px;
|
79 |
+
top:3px;
|
80 |
+
-moz-animation-delay:0.4s;
|
81 |
+
-webkit-animation-delay:0.4s;
|
82 |
+
-ms-animation-delay:0.4s;
|
83 |
+
-o-animation-delay:0.4s;
|
84 |
+
animation-delay:0.4s;
|
85 |
+
}
|
86 |
+
|
87 |
+
#frotateG_03{
|
88 |
+
left:11px;
|
89 |
+
top:0;
|
90 |
+
-moz-animation-delay:0.5s;
|
91 |
+
-webkit-animation-delay:0.5s;
|
92 |
+
-ms-animation-delay:0.5s;
|
93 |
+
-o-animation-delay:0.5s;
|
94 |
+
animation-delay:0.5s;
|
95 |
+
}
|
96 |
+
|
97 |
+
#frotateG_04{
|
98 |
+
right:3px;
|
99 |
+
top:3px;
|
100 |
+
-moz-animation-delay:0.6s;
|
101 |
+
-webkit-animation-delay:0.6s;
|
102 |
+
-ms-animation-delay:0.6s;
|
103 |
+
-o-animation-delay:0.6s;
|
104 |
+
animation-delay:0.6s;
|
105 |
+
}
|
106 |
+
|
107 |
+
#frotateG_05{
|
108 |
+
right:0;
|
109 |
+
top:11px;
|
110 |
+
-moz-animation-delay:0.7s;
|
111 |
+
-webkit-animation-delay:0.7s;
|
112 |
+
-ms-animation-delay:0.7s;
|
113 |
+
-o-animation-delay:0.7s;
|
114 |
+
animation-delay:0.7s;
|
115 |
+
}
|
116 |
+
|
117 |
+
#frotateG_06{
|
118 |
+
right:3px;
|
119 |
+
bottom:3px;
|
120 |
+
-moz-animation-delay:0.8s;
|
121 |
+
-webkit-animation-delay:0.8s;
|
122 |
+
-ms-animation-delay:0.8s;
|
123 |
+
-o-animation-delay:0.8s;
|
124 |
+
animation-delay:0.8s;
|
125 |
+
}
|
126 |
+
|
127 |
+
#frotateG_07{
|
128 |
+
left:11px;
|
129 |
+
bottom:0;
|
130 |
+
-moz-animation-delay:0.9s;
|
131 |
+
-webkit-animation-delay:0.9s;
|
132 |
+
-ms-animation-delay:0.9s;
|
133 |
+
-o-animation-delay:0.9s;
|
134 |
+
animation-delay:0.9s;
|
135 |
+
}
|
136 |
+
|
137 |
+
#frotateG_08{
|
138 |
+
left:3px;
|
139 |
+
bottom:3px;
|
140 |
+
-moz-animation-delay:1s;
|
141 |
+
-webkit-animation-delay:1s;
|
142 |
+
-ms-animation-delay:1s;
|
143 |
+
-o-animation-delay:1s;
|
144 |
+
animation-delay:1s;
|
145 |
+
}
|
146 |
+
|
147 |
+
@-moz-keyframes f_fadeG{
|
148 |
+
0%{
|
149 |
+
background-color:#000000}
|
150 |
+
|
151 |
+
100%{
|
152 |
+
background-color:#FFFFFF}
|
153 |
+
|
154 |
+
}
|
155 |
+
|
156 |
+
@-webkit-keyframes f_fadeG{
|
157 |
+
0%{
|
158 |
+
background-color:#000000}
|
159 |
+
|
160 |
+
100%{
|
161 |
+
background-color:#FFFFFF}
|
162 |
+
|
163 |
+
}
|
164 |
+
|
165 |
+
@-ms-keyframes f_fadeG{
|
166 |
+
0%{
|
167 |
+
background-color:#000000}
|
168 |
+
|
169 |
+
100%{
|
170 |
+
background-color:#FFFFFF}
|
171 |
+
|
172 |
+
}
|
173 |
+
|
174 |
+
@-o-keyframes f_fadeG{
|
175 |
+
0%{
|
176 |
+
background-color:#000000}
|
177 |
+
|
178 |
+
100%{
|
179 |
+
background-color:#FFFFFF}
|
180 |
+
|
181 |
+
}
|
182 |
+
|
183 |
+
@keyframes f_fadeG{
|
184 |
+
0%{
|
185 |
+
background-color:#000000}
|
186 |
+
|
187 |
+
100%{
|
188 |
+
background-color:#FFFFFF}
|
189 |
+
|
190 |
+
}
|
191 |
+
|
192 |
+
/*
|
193 |
+
Progressbar ui
|
194 |
+
*/
|
195 |
+
#progress-ui{
|
196 |
+
width: 80%;
|
197 |
+
margin-bottom: 20px;
|
198 |
+
display:none;
|
199 |
+
}
|
200 |
+
|
201 |
+
#progress-ui #wp-smushit-progress-wrap{
|
202 |
+
height: 16px;
|
203 |
+
border: 1px solid #0074a2;
|
204 |
+
border-radius: 7px;
|
205 |
+
overflow: hidden;
|
206 |
+
}
|
207 |
+
#progress-ui #wp-smushit-progress-wrap .wp-smushit-progressbar{
|
208 |
+
height:16px;
|
209 |
+
float:left;
|
210 |
+
width:100%;
|
211 |
+
}
|
212 |
+
#progress-ui #wp-smushit-progress-wrap .wp-smushit-progressbar div{
|
213 |
+
height:16px;
|
214 |
+
}
|
215 |
+
#progress-ui #wp-smushit-progress-wrap #wp-smushit-smush-progress div{
|
216 |
+
border-radius: 5px 0 0 5px;
|
217 |
+
background-color: #2ea2cc;
|
218 |
+
}
|
219 |
+
#progress-ui #wp-smushit-progress-wrap #wp-smushit-check-progress div{
|
220 |
+
border-radius: 0 5px 5px 0;
|
221 |
+
background-color: #0074a2;
|
222 |
+
}
|
223 |
+
|
224 |
+
#progress-ui p#check-status{
|
225 |
+
display:none;
|
226 |
+
}
|
assets/js/wp-smushit-admin.js
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Processes bulk smushing
|
3 |
+
*
|
4 |
+
* @author Saurabh Shukla <saurabh@incsub.com>
|
5 |
+
*
|
6 |
+
*/
|
7 |
+
jQuery('document').ready(function () {
|
8 |
+
|
9 |
+
// url for smushing
|
10 |
+
$send_url = ajaxurl + '?action=wp_smushit_bulk';
|
11 |
+
$remaining = '';
|
12 |
+
$smush_done = 1;
|
13 |
+
|
14 |
+
/**
|
15 |
+
* Checks for the specified param in URL
|
16 |
+
* @param sParam
|
17 |
+
* @returns {*}
|
18 |
+
*/
|
19 |
+
function geturlparam ( arg ) {
|
20 |
+
$sPageURL = window.location.search.substring(1);
|
21 |
+
$sURLVariables = $sPageURL.split('&');
|
22 |
+
|
23 |
+
for (var i = 0; i < $sURLVariables.length; i++) {
|
24 |
+
$sParameterName = $sURLVariables[i].split('=');
|
25 |
+
if ($sParameterName[0] == arg) {
|
26 |
+
return $sParameterName[1];
|
27 |
+
}
|
28 |
+
}
|
29 |
+
}
|
30 |
+
|
31 |
+
/**
|
32 |
+
* Show progress of smushing
|
33 |
+
*/
|
34 |
+
function smushit_progress() {
|
35 |
+
|
36 |
+
$total = $smush_done + $remaining;
|
37 |
+
|
38 |
+
// calculate %
|
39 |
+
if ($remaining != 0) {
|
40 |
+
|
41 |
+
// increase progress count
|
42 |
+
$smush_done++;
|
43 |
+
|
44 |
+
$progress = ( $smush_done / $total) * 100;
|
45 |
+
}else{
|
46 |
+
jQuery('#smush-status').html(wp_smushit_msgs['done']);
|
47 |
+
}
|
48 |
+
jQuery('#smushing-total').html($total);
|
49 |
+
|
50 |
+
// increase the progress bar
|
51 |
+
wp_smushit_change_progress_status($smush_done, $progress);
|
52 |
+
|
53 |
+
}
|
54 |
+
|
55 |
+
/**
|
56 |
+
* Send ajax request for smushing
|
57 |
+
*
|
58 |
+
* @param {type} $id
|
59 |
+
* @param {type} $getnxt
|
60 |
+
* @returns {unresolved}
|
61 |
+
*/
|
62 |
+
function smushitRequest($id, $getnxt) {
|
63 |
+
// make request
|
64 |
+
return jQuery.ajax({
|
65 |
+
type: "GET",
|
66 |
+
data: {attachment_id: $id, get_next: $getnxt},
|
67 |
+
url: $send_url,
|
68 |
+
timeout: 60000,
|
69 |
+
dataType: 'json'
|
70 |
+
}).done(function (response) {
|
71 |
+
|
72 |
+
// increase progressbar
|
73 |
+
smushit_progress();
|
74 |
+
});
|
75 |
+
}
|
76 |
+
|
77 |
+
/**
|
78 |
+
* Change the button status on bulk smush completion
|
79 |
+
*
|
80 |
+
* @returns {undefined}
|
81 |
+
*/
|
82 |
+
function wp_smushit_all_done() {
|
83 |
+
$button = jQuery('.wp-smushit-bulk-wrap #wp-smushit-begin');
|
84 |
+
|
85 |
+
// copy the loader into an object
|
86 |
+
$loader = $button.find('.floatingCirclesG');
|
87 |
+
|
88 |
+
// remove the loader
|
89 |
+
$loader.remove();
|
90 |
+
|
91 |
+
// empty the current text
|
92 |
+
$button.find('span').html('');
|
93 |
+
|
94 |
+
// add new class for css adjustment
|
95 |
+
$button.removeClass('wp-smushit-started');
|
96 |
+
$button.addClass('wp-smushit-finished');
|
97 |
+
|
98 |
+
// add the progress text
|
99 |
+
$button.find('span').html(wp_smushit_msgs.done);
|
100 |
+
|
101 |
+
return;
|
102 |
+
}
|
103 |
+
|
104 |
+
/**
|
105 |
+
* Change progress bar and status
|
106 |
+
*
|
107 |
+
* @param {type} $count
|
108 |
+
* @param {type} $width
|
109 |
+
* @returns {undefined}
|
110 |
+
*/
|
111 |
+
function wp_smushit_change_progress_status($count, $width) {
|
112 |
+
// get the progress bar
|
113 |
+
$progress_bar = jQuery('#wp-smushit-progress-wrap #wp-smushit-smush-progress div');
|
114 |
+
if ($progress_bar.length < 1) {
|
115 |
+
return;
|
116 |
+
}
|
117 |
+
jQuery('#smushed-count').html($count);
|
118 |
+
// increase progress
|
119 |
+
$progress_bar.css('width', $width + '%');
|
120 |
+
|
121 |
+
}
|
122 |
+
|
123 |
+
/**
|
124 |
+
* Send for bulk smushing
|
125 |
+
*
|
126 |
+
* @returns {undefined}
|
127 |
+
*/
|
128 |
+
function wp_smushit_bulk_smush() {
|
129 |
+
// instantiate our deferred object for piping
|
130 |
+
var startingpoint = jQuery.Deferred();
|
131 |
+
startingpoint.resolve();
|
132 |
+
|
133 |
+
//Show progress bar
|
134 |
+
jQuery('#wp-smushit-progress-wrap #wp-smushit-smush-progress div').css('width', 0);
|
135 |
+
jQuery('#progress-ui').show();
|
136 |
+
|
137 |
+
// if we have a definite number of ids
|
138 |
+
if (wp_smushit_ids.length > 0) {
|
139 |
+
|
140 |
+
$remaining = wp_smushit_ids.length;
|
141 |
+
|
142 |
+
// loop and pipe into deferred object
|
143 |
+
jQuery.each(wp_smushit_ids, function (ix, $id) {
|
144 |
+
startingpoint = startingpoint.then(function () {
|
145 |
+
$remaining = $remaining - 1;
|
146 |
+
|
147 |
+
// call the ajax requestor
|
148 |
+
return smushitRequest($id, 0);
|
149 |
+
});
|
150 |
+
});
|
151 |
+
}
|
152 |
+
|
153 |
+
}
|
154 |
+
//If ids are set in url, click over bulk smush button
|
155 |
+
$ids = geturlparam('ids');
|
156 |
+
if ($ids && $ids != '') {
|
157 |
+
wp_smushit_bulk_smush();
|
158 |
+
|
159 |
+
return;
|
160 |
+
}
|
161 |
+
|
162 |
+
/**
|
163 |
+
* Handle the start button click
|
164 |
+
*/
|
165 |
+
jQuery('button[name="smush-all"]').on('click', function (e) {
|
166 |
+
// prevent the default action
|
167 |
+
e.preventDefault();
|
168 |
+
|
169 |
+
jQuery(this).attr('disabled', 'disabled');
|
170 |
+
|
171 |
+
wp_smushit_bulk_smush();
|
172 |
+
|
173 |
+
return;
|
174 |
+
|
175 |
+
});
|
176 |
+
});
|
languages/wp_smushit-default.po
CHANGED
@@ -74,7 +74,7 @@ msgid "The image MUST be accessible via a non-https URL. The Yahoo! Smush.it ser
|
|
74 |
msgstr ""
|
75 |
|
76 |
#: wp-smushit.php:211
|
77 |
-
msgid "The image MUST publicly accessible server. As the Yahoo! Smush.it service needs to download the image via a URL the image needs to be on a public server and not a local
|
78 |
msgstr ""
|
79 |
|
80 |
#: wp-smushit.php:212
|
74 |
msgstr ""
|
75 |
|
76 |
#: wp-smushit.php:211
|
77 |
+
msgid "The image MUST publicly accessible server. As the Yahoo! Smush.it service needs to download the image via a URL the image needs to be on a public server and not a local development system. This is a limit of the Yahoo! service not this plugin."
|
78 |
msgstr ""
|
79 |
|
80 |
#: wp-smushit.php:212
|
languages/wp_smushit-default.pot
CHANGED
@@ -92,7 +92,7 @@ msgstr ""
|
|
92 |
msgid ""
|
93 |
"The image MUST publicly accessible server. As the Yahoo! Smush.it service "
|
94 |
"needs to download the image via a URL the image needs to be on a public "
|
95 |
-
"server and not a local
|
96 |
"Yahoo! service not this plugin."
|
97 |
msgstr ""
|
98 |
|
92 |
msgid ""
|
93 |
"The image MUST publicly accessible server. As the Yahoo! Smush.it service "
|
94 |
"needs to download the image via a URL the image needs to be on a public "
|
95 |
+
"server and not a local development system. This is a limit of the "
|
96 |
"Yahoo! service not this plugin."
|
97 |
msgstr ""
|
98 |
|
lib/class-wp-smushit-admin.php
ADDED
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* @package WP SmushIt
|
4 |
+
* @subpackage Admin
|
5 |
+
* @version 1.0
|
6 |
+
*
|
7 |
+
* @author Saurabh Shukla <saurabh@incsub.com>
|
8 |
+
* @author Umesh Kumar <umesh@incsub.com>
|
9 |
+
*
|
10 |
+
* @copyright (c) 2014, Incsub (http://incsub.com)
|
11 |
+
*/
|
12 |
+
if ( ! class_exists( 'WpSmushitAdmin' ) ) {
|
13 |
+
/**
|
14 |
+
* Show settings in Media settings and add column to media library
|
15 |
+
*
|
16 |
+
*/
|
17 |
+
class WpSmushitAdmin {
|
18 |
+
|
19 |
+
/**
|
20 |
+
*
|
21 |
+
* @var array Settings
|
22 |
+
*/
|
23 |
+
public $settings;
|
24 |
+
|
25 |
+
public $bulk;
|
26 |
+
|
27 |
+
/**
|
28 |
+
* Constructor
|
29 |
+
*/
|
30 |
+
public function __construct() {
|
31 |
+
|
32 |
+
// hook scripts and styles
|
33 |
+
add_action( 'admin_init', array( $this, 'register' ) );
|
34 |
+
|
35 |
+
// hook custom screen
|
36 |
+
add_action( 'admin_menu', array( $this, 'screen' ) );
|
37 |
+
|
38 |
+
add_action( 'admin_footer-upload.php', array( $this, 'print_loader' ) );
|
39 |
+
|
40 |
+
//Handle Smush Ajax
|
41 |
+
add_action( 'wp_ajax_wp_smushit_bulk', array( $this, 'process_smush_request' ) );
|
42 |
+
}
|
43 |
+
|
44 |
+
/**
|
45 |
+
* Add Bulk option settings page
|
46 |
+
*/
|
47 |
+
function screen() {
|
48 |
+
$admin_page_suffix = add_media_page( 'Bulk WP Smush.it', 'WP Smush.it', 'edit_others_posts', 'wp-smushit-bulk', array(
|
49 |
+
$this,
|
50 |
+
'ui'
|
51 |
+
) );
|
52 |
+
//Register Debug page only if WP_SMPRO_DEBUG is defined and true
|
53 |
+
if ( defined( 'WP_SMUSHIT_DEBUG' ) && WP_SMUSHIT_DEBUG ) {
|
54 |
+
add_media_page( 'WP Smush.it Error Log', 'Error Log', 'edit_others_posts', 'wp-smushit-errorlog', array(
|
55 |
+
$this,
|
56 |
+
'create_admin_error_log_page'
|
57 |
+
) );
|
58 |
+
}
|
59 |
+
// enqueue js only on this screen
|
60 |
+
add_action( 'admin_print_scripts-' . $admin_page_suffix, array( $this, 'enqueue' ) );
|
61 |
+
}
|
62 |
+
|
63 |
+
/**
|
64 |
+
* Register js and css
|
65 |
+
*/
|
66 |
+
function register() {
|
67 |
+
global $WpSmushit;
|
68 |
+
/* Register our script. */
|
69 |
+
wp_register_script( 'wp-smushit-admin-js', WP_SMUSHIT_URL . 'assets/js/wp-smushit-admin.js', array( 'jquery' ), $WpSmushit->version );
|
70 |
+
|
71 |
+
/* Register Style. */
|
72 |
+
wp_register_style( 'wp-smushit-admin-css', WP_SMUSHIT_URL . 'assets/css/wp-smushit-admin.css', array(), $WpSmushit->version );
|
73 |
+
|
74 |
+
// localize translatable strings for js
|
75 |
+
$this->localize();
|
76 |
+
}
|
77 |
+
|
78 |
+
/**
|
79 |
+
* enqueue js and css
|
80 |
+
*/
|
81 |
+
function enqueue() {
|
82 |
+
wp_enqueue_script( 'wp-smushit-admin-js' );
|
83 |
+
wp_enqueue_style( 'wp-smushit-admin-css' );
|
84 |
+
}
|
85 |
+
|
86 |
+
function localize() {
|
87 |
+
$bulk = new WpSmushitBulk();
|
88 |
+
$handle = 'wp-smushit-admin-js';
|
89 |
+
|
90 |
+
$wp_smushit_msgs = array(
|
91 |
+
'progress' => __( 'Smushing in Progress', WP_SMUSHIT_DOMAIN ),
|
92 |
+
'done' => __( 'All done!', WP_SMUSHIT_DOMAIN )
|
93 |
+
);
|
94 |
+
|
95 |
+
wp_localize_script( $handle, 'wp_smushit_msgs', $wp_smushit_msgs );
|
96 |
+
|
97 |
+
//Localize smushit_ids variable, if there are fix number of ids
|
98 |
+
$ids = ! empty( $_REQUEST['ids'] ) ? explode( ',', $_REQUEST['ids'] ) : $bulk->get_attachments();
|
99 |
+
wp_localize_script( 'wp-smushit-admin-js', 'wp_smushit_ids', $ids );
|
100 |
+
|
101 |
+
}
|
102 |
+
|
103 |
+
/**
|
104 |
+
* Display the ui
|
105 |
+
*/
|
106 |
+
function ui() {
|
107 |
+
?>
|
108 |
+
<div class="wrap">
|
109 |
+
<div id="icon-upload" class="icon32"><br/></div>
|
110 |
+
|
111 |
+
<h2>
|
112 |
+
<?php _e( 'WP Smush.it', WP_SMUSHIT_DOMAIN ) ?>
|
113 |
+
</h2>
|
114 |
+
|
115 |
+
<div class="wp-smpushit-container">
|
116 |
+
<h3>
|
117 |
+
<?php _e( 'Settings', WP_SMUSHIT_DOMAIN ) ?>
|
118 |
+
</h3>
|
119 |
+
<?php
|
120 |
+
// display the options
|
121 |
+
$this->options_ui();
|
122 |
+
|
123 |
+
//Bulk Smushing
|
124 |
+
$this->bulk_preview();
|
125 |
+
?>
|
126 |
+
</div>
|
127 |
+
</div>
|
128 |
+
<?php
|
129 |
+
}
|
130 |
+
|
131 |
+
/**
|
132 |
+
* Process and display the options form
|
133 |
+
*/
|
134 |
+
function options_ui() {
|
135 |
+
|
136 |
+
// Save settings, if needed
|
137 |
+
$this->process_options();
|
138 |
+
|
139 |
+
?>
|
140 |
+
<form action="" method="post"><?php
|
141 |
+
|
142 |
+
//Auto Smushing
|
143 |
+
$auto = 'wp_smushit_smushit_auto';
|
144 |
+
$auto_val = intval( get_option( $auto, WP_SMUSHIT_AUTO_OK ) );
|
145 |
+
$disabled = sprintf( __( 'Temporarily disabled until %s', WP_SMUSHIT_DOMAIN ), date( 'M j, Y \a\t H:i', $auto_val ) );
|
146 |
+
|
147 |
+
//Timeout
|
148 |
+
$timeout = 'wp_smushit_smushit_timeout';
|
149 |
+
$timeout_val = intval( get_option( $timeout, WP_SMUSHIT_AUTO_OK ) );
|
150 |
+
|
151 |
+
//Enforce Same URL
|
152 |
+
$enforce_same_url = 'wp_smushit_smushit_enforce_same_url';
|
153 |
+
$enforce_same_url_val = get_option( $enforce_same_url, WP_SMUSHIT_ENFORCE_SAME_URL );
|
154 |
+
|
155 |
+
//Debug
|
156 |
+
$smushit_debug = 'wp_smushit_smushit_debug';
|
157 |
+
$smushit_debug_val = get_option( $smushit_debug, WP_SMUSHIT_DEBUG );
|
158 |
+
?>
|
159 |
+
<table class="form-table">
|
160 |
+
<tbody>
|
161 |
+
<tr>
|
162 |
+
<th><label><?php echo __( 'Smush images on upload', WP_SMUSHIT_DOMAIN ); ?></label></th>
|
163 |
+
<td>
|
164 |
+
<select name='<?php echo $auto; ?>' id='<?php echo $auto; ?>'>
|
165 |
+
<option value='<?php echo WP_SMUSHIT_AUTO_OK; ?>' <?php selected( WP_SMUSHIT_AUTO_OK, $auto_val ); ?>><?php echo __( 'Automatically process on upload', WP_SMUSHIT_DOMAIN ); ?></option>
|
166 |
+
<option value='<?php echo WP_SMUSHIT_AUTO_NEVER; ?>' <?php selected( WP_SMUSHIT_AUTO_NEVER, $auto_val ); ?>><?php echo __( 'Do not process on upload', WP_SMUSHIT_DOMAIN ); ?></option> <?php
|
167 |
+
|
168 |
+
if ( $auto_val > 0 ) {
|
169 |
+
?>
|
170 |
+
<option value="<?php echo $auto_val; ?>" selected="selected"><?php echo $disabled; ?></option><?php
|
171 |
+
} ?>
|
172 |
+
</select>
|
173 |
+
</td>
|
174 |
+
</tr>
|
175 |
+
<tr>
|
176 |
+
<th><?php _e( 'API Timeout', WP_SMUSHIT_DOMAIN ); ?></th>
|
177 |
+
<td>
|
178 |
+
<input type='text' name='<?php echo esc_attr( $timeout ); ?>' id='<?php echo esc_attr( $timeout ); ?>' value='<?php echo intval( get_option( $timeout, 60 ) ); ?>' size="2">
|
179 |
+
</td>
|
180 |
+
</tr>
|
181 |
+
<tr>
|
182 |
+
<th><?php _e( 'Enforce home URL', WP_SMUSHIT_DOMAIN ); ?></th>
|
183 |
+
<td>
|
184 |
+
<input type="checkbox" name="<?php echo $enforce_same_url ?>" <?php echo checked( $enforce_same_url_val, 'on' ); ?>/> <?php
|
185 |
+
echo '<strong>' . get_option( 'home' ) . '</strong><br />' . __( 'By default the plugin will enforce that the image URL is the same domain as the home. If you are using a sub-domain pointed to this same host or an external Content Delivery Network (CDN) you want to unset this option.', WP_SMUSHIT_DOMAIN ); ?>
|
186 |
+
</td>
|
187 |
+
</tr>
|
188 |
+
<tr>
|
189 |
+
<th><?php _e( 'Smushit Debug', WP_SMUSHIT_DOMAIN ); ?></th>
|
190 |
+
<td>
|
191 |
+
<input type="checkbox" name="<?php echo $smushit_debug ?>" <?php echo checked( $smushit_debug_val, 'on' ); ?>/>
|
192 |
+
<?php _e( 'If you are having trouble with the plugin enable this option can reveal some information about your system needed for support.', WP_SMUSHIT_DOMAIN ); ?>
|
193 |
+
</td>
|
194 |
+
</tr>
|
195 |
+
</tbody>
|
196 |
+
</table><?php
|
197 |
+
// nonce
|
198 |
+
wp_nonce_field( 'save_wp_smushit_options', 'wp_smushit_options_nonce' );
|
199 |
+
?>
|
200 |
+
<input type="submit" id="wp-smushit-save-settings" class="button button-primary" value="<?php _e( 'Save Changes', WP_SMUSHIT_DOMAIN ); ?>">
|
201 |
+
</form>
|
202 |
+
<?php
|
203 |
+
}
|
204 |
+
|
205 |
+
/**
|
206 |
+
* Check if form is submitted and process it
|
207 |
+
*
|
208 |
+
* @return null
|
209 |
+
*/
|
210 |
+
function process_options() {
|
211 |
+
// we aren't saving options
|
212 |
+
if ( ! isset( $_POST['wp_smushit_options_nonce'] ) ) {
|
213 |
+
return;
|
214 |
+
}
|
215 |
+
echo "here";
|
216 |
+
// the nonce doesn't pan out
|
217 |
+
if ( ! wp_verify_nonce( $_POST['wp_smushit_options_nonce'], 'save_wp_smushit_options' ) ) {
|
218 |
+
return;
|
219 |
+
}
|
220 |
+
echo "there";
|
221 |
+
|
222 |
+
//Array of options
|
223 |
+
$smushit_settings = array(
|
224 |
+
'wp_smushit_smushit_auto',
|
225 |
+
'wp_smushit_smushit_timeout',
|
226 |
+
'wp_smushit_smushit_enforce_same_url',
|
227 |
+
'wp_smushit_smushit_debug'
|
228 |
+
);
|
229 |
+
//Save All the options
|
230 |
+
foreach ( $smushit_settings as $setting ) {
|
231 |
+
if ( empty( $_POST[ $setting ] ) ) {
|
232 |
+
update_option( $setting, '' );
|
233 |
+
continue;
|
234 |
+
}
|
235 |
+
update_option( $setting, $_POST[ $setting ] );
|
236 |
+
}
|
237 |
+
|
238 |
+
}
|
239 |
+
|
240 |
+
/**
|
241 |
+
* Bulk Smushing UI
|
242 |
+
*/
|
243 |
+
function bulk_preview() {
|
244 |
+
|
245 |
+
$bulk = new WpSmushitBulk();
|
246 |
+
if ( function_exists( 'apache_setenv' ) ) {
|
247 |
+
@apache_setenv( 'no-gzip', 1 );
|
248 |
+
}
|
249 |
+
@ini_set( 'output_buffering', 'on' );
|
250 |
+
@ini_set( 'zlib.output_compression', 0 );
|
251 |
+
@ini_set( 'implicit_flush', 1 );
|
252 |
+
|
253 |
+
$attachments = null;
|
254 |
+
$auto_start = false;
|
255 |
+
|
256 |
+
$attachments = $bulk->get_attachments();
|
257 |
+
$count = 0;
|
258 |
+
//Check images bigger than 1Mb, used to display the count of images that can't be smushed
|
259 |
+
foreach ( $attachments as $attachment ) {
|
260 |
+
if ( file_exists( get_attached_file( $attachment ) ) ) {
|
261 |
+
$size = filesize( get_attached_file( $attachment ) );
|
262 |
+
}
|
263 |
+
if ( empty( $size ) || ! ( ( $size / 1048576 ) > 1 ) ) {
|
264 |
+
continue;
|
265 |
+
}
|
266 |
+
$count ++;
|
267 |
+
}
|
268 |
+
$exceed_mb = '';
|
269 |
+
$text = $count > 1 ? 'are' : 'is';
|
270 |
+
if ( $count ) {
|
271 |
+
$exceed_mb = sprintf( __( " %d of those images %s <b>over 1Mb</b> and <b>can not be compressed using the free version of the plugin.</b>", WP_SMUSHIT_DOMAIN ), $count, $text );
|
272 |
+
}
|
273 |
+
$media_lib = get_admin_url( '', 'upload.php' );
|
274 |
+
?>
|
275 |
+
<div class="wrap">
|
276 |
+
<div id="icon-upload" class="icon32"><br/></div>
|
277 |
+
<h3><?php _e( 'Smush in Bulk', WP_SMUSHIT_DOMAIN ) ?></h3>
|
278 |
+
<?php
|
279 |
+
|
280 |
+
if ( sizeof( $attachments ) < 1 ) {
|
281 |
+
_e( "<p>You don't appear to have uploaded any images yet.</p>", WP_SMUSHIT_DOMAIN );
|
282 |
+
} else {
|
283 |
+
if ( ! isset( $_POST['smush-all'] ) && ! $auto_start ) { // instructions page
|
284 |
+
$upgrade_link = "<a href='http://premium.wpmudev.org/project/wp-smush-pro'>" . __( "Upgrade to WP Smush PRO", WP_SMUSHIT_DOMAIN ) . "</a>";
|
285 |
+
_e( "<h4>WP Smush.it uses Yahoo! Smush.it API. As such, there are a few restrictions:</h4>", WP_SMUSHIT_DOMAIN );
|
286 |
+
?>
|
287 |
+
<ol>
|
288 |
+
<li><?php printf( __( "Each image MUST be less than 1Mb in size. %s and use our servers for images upto 5Mb.", WP_SMUSHIT_DOMAIN ), $upgrade_link ); ?></li>
|
289 |
+
<li><?php printf( __( "Images MUST be accessible via a non-https URL. The Yahoo! Smush.it service will not handle https:// image URLs. %s to allow https URLs", WP_SMUSHIT_DOMAIN ), $upgrade_link ); ?></li>
|
290 |
+
<li><?php printf( __( "Smushing images in bulk can sometimes cause time-outs. %s and use our reliable server to prevent time-outs.", WP_SMUSHIT_DOMAIN ), $upgrade_link ); ?></li>
|
291 |
+
</ol>
|
292 |
+
<div class="smushit-pro-update-link" style="background: white; float: left; font-size: 18px; line-height: 1.4; margin: 0 0 10px; padding: 13px;">
|
293 |
+
<?php _e( "<strong>WP Smush PRO</strong> allows you to smush images up to 5Mb.<br /> Fast, reliable & time-out free. <a href='http://premium.wpmudev.org/projects/wp-smush-pro'>Find Out more »</a>", WP_SMUSHIT_DOMAIN ); ?>
|
294 |
+
</div>
|
295 |
+
|
296 |
+
<hr style="clear: left;"/>
|
297 |
+
|
298 |
+
<style type="text/css">
|
299 |
+
.smush-instructions p {
|
300 |
+
line-height: 1.2;
|
301 |
+
margin: 0 0 4px;
|
302 |
+
}
|
303 |
+
</style>
|
304 |
+
<div class="smush-instructions" style="line-height: 1;">
|
305 |
+
<?php printf( __( "<p>We found %d images in your media library. %s </p>", WP_SMUSHIT_DOMAIN ), sizeof( $attachments ), $exceed_mb ); ?>
|
306 |
+
|
307 |
+
<?php _e( "<p><b style='color: red;'>Please beware</b>, <b>smushing a large number of images can take a long time.</b></p>", WP_SMUSHIT_DOMAIN ); ?>
|
308 |
+
|
309 |
+
<?php _e( "<p><b>You can not leave this page, until all images have been received back, and you see a success message.</b></p>", WP_SMUSHIT_DOMAIN ); ?>
|
310 |
+
<br/>
|
311 |
+
<?php printf( __( "Click below to smush all your images. Alternatively, you can smush your images individually or as a bulk action from your <a href='%s'>Media Library</a>", WP_SMUSHIT_DOMAIN ), $media_lib ); ?>
|
312 |
+
</div>
|
313 |
+
|
314 |
+
<!-- Bulk Smushing-->
|
315 |
+
<?php wp_nonce_field( 'wp-smushit-bulk', '_wpnonce' ); ?>
|
316 |
+
<br/>
|
317 |
+
<?php $this->progress_ui(); ?>
|
318 |
+
<button type="submit" class="button-primary action" name="smush-all"><?php _e( 'Bulk Smush all my images', WP_SMUSHIT_DOMAIN ) ?></button>
|
319 |
+
<?php _e( "<p><em>N.B. If your server <tt>gzip</tt>s content you may not see the progress updates as your files are processed.</em></p>", WP_SMUSHIT_DOMAIN ); ?>
|
320 |
+
<?php
|
321 |
+
if ( WP_SMUSHIT_DEBUG ) {
|
322 |
+
_e( "<p>DEBUG mode is currently enabled. To disable uncheck the smushit debug option.</p>", WP_SMUSHIT_DOMAIN );
|
323 |
+
}
|
324 |
+
}
|
325 |
+
}
|
326 |
+
?>
|
327 |
+
</div>
|
328 |
+
<?php
|
329 |
+
}
|
330 |
+
|
331 |
+
function print_loader() {
|
332 |
+
?>
|
333 |
+
<div id="wp-smushit-loader-wrap">
|
334 |
+
<div class="floatingCirclesG">
|
335 |
+
<div class="f_circleG" id="frotateG_01">
|
336 |
+
</div>
|
337 |
+
<div class="f_circleG" id="frotateG_02">
|
338 |
+
</div>
|
339 |
+
<div class="f_circleG" id="frotateG_03">
|
340 |
+
</div>
|
341 |
+
<div class="f_circleG" id="frotateG_04">
|
342 |
+
</div>
|
343 |
+
<div class="f_circleG" id="frotateG_05">
|
344 |
+
</div>
|
345 |
+
<div class="f_circleG" id="frotateG_06">
|
346 |
+
</div>
|
347 |
+
<div class="f_circleG" id="frotateG_07">
|
348 |
+
</div>
|
349 |
+
<div class="f_circleG" id="frotateG_08">
|
350 |
+
</div>
|
351 |
+
</div>
|
352 |
+
</div>
|
353 |
+
<?php
|
354 |
+
}
|
355 |
+
|
356 |
+
function progress_ui() {
|
357 |
+
$bulk = new WpSmushitBulk;
|
358 |
+
$total = count( $bulk->get_attachments() );
|
359 |
+
$total = $total ? $total : 1; ?>
|
360 |
+
|
361 |
+
<div id="progress-ui">
|
362 |
+
<div id="smush-status" style="margin: 0 0 5px;"><?php printf( __( 'Smushing <span id="smushed-count">1</span> of <span id="smushing-total">%d</span>', WP_SMUSHIT_DOMAIN ), $total ); ?></div>
|
363 |
+
<div id="wp-smushit-progress-wrap">
|
364 |
+
<div id="wp-smushit-smush-progress" class="wp-smushit-progressbar">
|
365 |
+
<div></div>
|
366 |
+
</div>
|
367 |
+
</div>
|
368 |
+
</div> <?php
|
369 |
+
}
|
370 |
+
|
371 |
+
/**
|
372 |
+
* Processes the Smush request and sends back the next id for smushing
|
373 |
+
*/
|
374 |
+
function process_smush_request() {
|
375 |
+
|
376 |
+
global $WpSmushit;
|
377 |
+
|
378 |
+
if ( empty( $_REQUEST['attachment_id'] ) ) {
|
379 |
+
wp_send_json_error( 'missing id' );
|
380 |
+
}
|
381 |
+
|
382 |
+
$attachment_id = $_REQUEST['attachment_id'];
|
383 |
+
|
384 |
+
$original_meta = wp_get_attachment_metadata( $attachment_id, true );
|
385 |
+
|
386 |
+
$meta = $WpSmushit->resize_from_meta_data( $original_meta, $attachment_id, false );
|
387 |
+
|
388 |
+
wp_update_attachment_metadata( $attachment_id, $meta );
|
389 |
+
|
390 |
+
wp_send_json_success( 'processed' );
|
391 |
+
}
|
392 |
+
|
393 |
+
/**
|
394 |
+
* Creates Admin Error Log info page.
|
395 |
+
*
|
396 |
+
* @access private.
|
397 |
+
*/
|
398 |
+
function create_admin_error_log_page() {
|
399 |
+
global $log;
|
400 |
+
if ( ! empty( $_GET['action'] ) && 'purge' == @$_GET['action'] ) {
|
401 |
+
//Check Nonce
|
402 |
+
if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'purge_log' ) ) {
|
403 |
+
echo '<div class="error"><p>' . __( 'Nonce verification failed', WP_SMUSHIT_DOMAIN ) . '</p></div>';
|
404 |
+
} else {
|
405 |
+
$log->purge_errors();
|
406 |
+
$log->purge_notices();
|
407 |
+
}
|
408 |
+
}
|
409 |
+
$errors = $log->get_all_errors();
|
410 |
+
$notices = $log->get_all_notices();
|
411 |
+
/**
|
412 |
+
* Error Log Form
|
413 |
+
*/
|
414 |
+
require_once( WP_SMUSHIT_DIR . '/lib/error_log.php' );
|
415 |
+
}
|
416 |
+
}
|
417 |
+
|
418 |
+
//Add js variables for smushing
|
419 |
+
$wpsmushit_admin = new WpSmushitAdmin();
|
420 |
+
}
|
lib/class-wp-smushit-bulk.php
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* @package WP SmushIt
|
5 |
+
* @subpackage Admin
|
6 |
+
* @version 1.0
|
7 |
+
*
|
8 |
+
* @author Saurabh Shukla <saurabh@incsub.com>
|
9 |
+
* @author Umesh Kumar <umesh@incsub.com>
|
10 |
+
*
|
11 |
+
* @copyright (c) 2014, Incsub (http://incsub.com)
|
12 |
+
*/
|
13 |
+
if ( ! class_exists( 'WpSmushitBulk' ) ) {
|
14 |
+
|
15 |
+
/**
|
16 |
+
* Methods for bulk processing
|
17 |
+
*/
|
18 |
+
class WpSmushitBulk {
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Fetch all the attachments
|
22 |
+
* @return array $attachments
|
23 |
+
*/
|
24 |
+
function get_attachments() {
|
25 |
+
if ( ! isset( $_REQUEST['ids'] ) ) {
|
26 |
+
$attachments = get_posts( array(
|
27 |
+
'numberposts' => - 1,
|
28 |
+
'post_type' => 'attachment',
|
29 |
+
'post_mime_type' => 'image',
|
30 |
+
'fields' => 'ids'
|
31 |
+
) );
|
32 |
+
} else {
|
33 |
+
return explode( ',', $_REQUEST['ids'] );
|
34 |
+
}
|
35 |
+
|
36 |
+
return $attachments;
|
37 |
+
}
|
38 |
+
|
39 |
+
}
|
40 |
+
}
|
lib/error/class-wp-smushit-errorlog.php
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
class WpSmushitErrorLog {
|
4 |
+
var $_limit = 100;
|
5 |
+
|
6 |
+
function get_all_errors() {
|
7 |
+
$errors = get_option( 'wp-smushit_error_log' );
|
8 |
+
|
9 |
+
return $errors ? $errors : array();
|
10 |
+
}
|
11 |
+
|
12 |
+
function get_all_notices() {
|
13 |
+
$notices = get_option( 'wp-smushit_notice_log' );
|
14 |
+
|
15 |
+
return $notices ? $notices : array();
|
16 |
+
}
|
17 |
+
|
18 |
+
function purge_errors() {
|
19 |
+
update_option( 'wp-smushit_error_log', array() );
|
20 |
+
}
|
21 |
+
|
22 |
+
function purge_notices() {
|
23 |
+
update_option( 'wp-smushit_notice_log', array() );
|
24 |
+
}
|
25 |
+
|
26 |
+
/**
|
27 |
+
* Store the error in db
|
28 |
+
* @param $function, Function name where error occured
|
29 |
+
* @param $image, Details of attachment
|
30 |
+
* @param $exception, Details about error
|
31 |
+
*/
|
32 |
+
function error( $function, $image, $exception ) {
|
33 |
+
WpSmushitErrorRegistry::store( $exception );
|
34 |
+
$info = is_object( $exception ) && method_exists( $exception, 'getMessage' )
|
35 |
+
? $exception->getMessage()
|
36 |
+
: $exception;
|
37 |
+
|
38 |
+
//If image variable is an array, display the details per line, otherwise display it directly
|
39 |
+
$image = is_array( $image ) ? implode( "<br/>", $image ) : $image;
|
40 |
+
$this->_update_error_queue( array(
|
41 |
+
'date' => current_time( 'timestamp' ),
|
42 |
+
'area' => $function,
|
43 |
+
'image' => $image,
|
44 |
+
'info' => $info
|
45 |
+
) );
|
46 |
+
}
|
47 |
+
|
48 |
+
/**
|
49 |
+
* Store notice in db
|
50 |
+
* @param $msg
|
51 |
+
*/
|
52 |
+
function notice( $msg ) {
|
53 |
+
$this->_update_notice_queue( array(
|
54 |
+
'date' => current_time( 'timestamp' ),
|
55 |
+
'user_id' => get_current_user_id(),
|
56 |
+
'message' => $msg
|
57 |
+
) );
|
58 |
+
}
|
59 |
+
|
60 |
+
function _update_error_queue( $error ) {
|
61 |
+
$errors = $this->get_all_errors();
|
62 |
+
if ( count( $errors ) >= $this->_limit ) {
|
63 |
+
$errors = array_slice( $errors, ( ( $this->_limit * - 1 ) - 1 ), $this->_limit - 1 );
|
64 |
+
}
|
65 |
+
$errors[] = $error;
|
66 |
+
update_option( 'wp-smushit_error_log', $errors );
|
67 |
+
}
|
68 |
+
|
69 |
+
function _update_notice_queue( $notice ) {
|
70 |
+
$notices = $this->get_all_notices();
|
71 |
+
if ( count( $notices ) >= $this->_limit ) {
|
72 |
+
$notices = array_slice( $notices, - $this->_limit );
|
73 |
+
} // * -1)), $this->_limit-1);
|
74 |
+
$notices[] = $notice;
|
75 |
+
update_option( 'wp-smushit_notice_log', $notices );
|
76 |
+
}
|
77 |
+
}
|
78 |
+
|
79 |
+
global $log;
|
80 |
+
$log = new WpSmushitErrorLog();
|
lib/error/class-wp-smushit-errorregistry.php
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* Error registry class for exception transport.
|
5 |
+
*/
|
6 |
+
class WpSmushitErrorRegistry {
|
7 |
+
private static $_errors = array();
|
8 |
+
|
9 |
+
private function __construct() {
|
10 |
+
}
|
11 |
+
|
12 |
+
public static function store( $exception ) {
|
13 |
+
self::$_errors[] = $exception;
|
14 |
+
}
|
15 |
+
|
16 |
+
public static function clear() {
|
17 |
+
self::$_errors = array();
|
18 |
+
}
|
19 |
+
|
20 |
+
public static function get_errors() {
|
21 |
+
return self::$_errors;
|
22 |
+
}
|
23 |
+
|
24 |
+
public static function get_last_error() {
|
25 |
+
return end( self::$_errors );
|
26 |
+
}
|
27 |
+
|
28 |
+
public static function get_last_error_message() {
|
29 |
+
$e = self::get_last_error();
|
30 |
+
|
31 |
+
return ( $e && is_object( $e ) && $e instanceof Exception )
|
32 |
+
? $e->getMessage()
|
33 |
+
: false;
|
34 |
+
}
|
35 |
+
}
|
36 |
+
|
37 |
+
?>
|
lib/error_log.php
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
$date_fmt = get_option( 'date_format' );
|
3 |
+
$date_fmt = $date_fmt ? $date_fmt : 'Y-m-d';
|
4 |
+
$time_fmt = get_option( 'time_format' );
|
5 |
+
$time_fmt = $time_fmt ? $time_fmt : 'H:i:s';
|
6 |
+
$datetime_format = "{$date_fmt} {$time_fmt}";
|
7 |
+
?>
|
8 |
+
<div class="wrap">
|
9 |
+
<h2><?php _e( 'Error Log', WP_SMUSHIT_DOMAIN ); ?></h2>
|
10 |
+
|
11 |
+
<h3>Errors</h3>
|
12 |
+
<?php if ( $errors ) { ?>
|
13 |
+
<a href="<?php echo wp_nonce_url( admin_url( 'admin.php?page=wp-smushit-errorlog&action=purge' ), 'purge_log' ); ?>"><?php _e( 'Purge log', WP_SMUSHIT_DOMAIN ); ?></a>
|
14 |
+
<table class="widefat">
|
15 |
+
<thead>
|
16 |
+
<tr>
|
17 |
+
<th><?php _e( 'Date', WP_SMUSHIT_DOMAIN ) ?></th>
|
18 |
+
<th><?php _e( 'Function', WP_SMUSHIT_DOMAIN ) ?></th>
|
19 |
+
<th><?php _e( 'Image', WP_SMUSHIT_DOMAIN ) ?></th>
|
20 |
+
<th><?php _e( 'Info', WP_SMUSHIT_DOMAIN ) ?></th>
|
21 |
+
</tr>
|
22 |
+
</thead>
|
23 |
+
<tfoot>
|
24 |
+
<tr>
|
25 |
+
<th><?php _e( 'Date', WP_SMUSHIT_DOMAIN ) ?></th>
|
26 |
+
<th><?php _e( 'Function', WP_SMUSHIT_DOMAIN ) ?></th>
|
27 |
+
<th><?php _e( 'Image', WP_SMUSHIT_DOMAIN ) ?></th>
|
28 |
+
<th><?php _e( 'Info', WP_SMUSHIT_DOMAIN ) ?></th>
|
29 |
+
</tr>
|
30 |
+
</tfoot>
|
31 |
+
<tbody>
|
32 |
+
<?php foreach ( $errors as $error ) { ?>
|
33 |
+
<?php $user = get_userdata( @$error['user_id'] ); ?>
|
34 |
+
<tr>
|
35 |
+
<td><?php echo date( $datetime_format, $error['date'] ); ?></td>
|
36 |
+
<td><?php echo $error['area']; ?></td>
|
37 |
+
<td><?php echo $error['image']; ?></td>
|
38 |
+
<td><?php echo urldecode( $error['info'] ); ?></td>
|
39 |
+
</tr>
|
40 |
+
<?php } ?>
|
41 |
+
</tbody>
|
42 |
+
</table>
|
43 |
+
<?php } else { ?>
|
44 |
+
<p><i>Your error log is empty.</i></p>
|
45 |
+
<?php } ?>
|
46 |
+
|
47 |
+
<?php if ( current_user_can( 'manage_network_options' ) ) { ?>
|
48 |
+
<p><a href="#notices" class="wp_smpro_toggle_notices">Show/Hide notices</a></p>
|
49 |
+
<div id="wp_smpro_notices" style="display:none">
|
50 |
+
<h3>Notices</h3>
|
51 |
+
<?php if ( $notices ) { ?>
|
52 |
+
<table class="widefat">
|
53 |
+
<thead>
|
54 |
+
<tr>
|
55 |
+
<th><?php _e( 'Date', WP_SMUSHIT_DOMAIN ) ?></th>
|
56 |
+
<th><?php _e( 'User', WP_SMUSHIT_DOMAIN ) ?></th>
|
57 |
+
<th><?php _e( 'Message', WP_SMUSHIT_DOMAIN ) ?></th>
|
58 |
+
</tr>
|
59 |
+
</thead>
|
60 |
+
<tfoot>
|
61 |
+
<tr>
|
62 |
+
<th><?php _e( 'Date', WP_SMUSHIT_DOMAIN ) ?></th>
|
63 |
+
<th><?php _e( 'User', WP_SMUSHIT_DOMAIN ) ?></th>
|
64 |
+
<th><?php _e( 'Message', WP_SMUSHIT_DOMAIN ) ?></th>
|
65 |
+
</tr>
|
66 |
+
</tfoot>
|
67 |
+
<tbody>
|
68 |
+
<?php foreach ( $notices as $notice ) { ?>
|
69 |
+
<?php $user = get_userdata( @$notice['user_id'] ); ?>
|
70 |
+
<tr>
|
71 |
+
<td><?php echo date( $datetime_format, $notice['date'] ); ?></td>
|
72 |
+
<td><?php echo( ( isset( $user->user_login ) && $user->user_login ) ? $user->user_login : __( 'Unknown', WP_SMUSHIT_DOMAIN ) ); ?></td>
|
73 |
+
<td><?php echo $notice['message']; ?></td>
|
74 |
+
</tr>
|
75 |
+
<?php } ?>
|
76 |
+
</tbody>
|
77 |
+
</table>
|
78 |
+
<?php } else { ?>
|
79 |
+
<p><i>No notices.</i></p>
|
80 |
+
<?php } ?>
|
81 |
+
</div>
|
82 |
+
|
83 |
+
<script type="text/javascript">
|
84 |
+
(function ($) {
|
85 |
+
$(function () {
|
86 |
+
|
87 |
+
$(".wp_smpro_toggle_notices").click(function () {
|
88 |
+
if ($("#wp_smpro_notices").is(":visible")) $("#wp_smpro_notices").hide();
|
89 |
+
else $("#wp_smpro_notices").show();
|
90 |
+
return false;
|
91 |
+
});
|
92 |
+
|
93 |
+
});
|
94 |
+
})(jQuery);
|
95 |
+
</script>
|
96 |
+
<?php } ?>
|
97 |
+
|
98 |
+
</div>
|
readme.txt
CHANGED
@@ -1,54 +1,55 @@
|
|
1 |
=== WP Smush.it ===
|
2 |
Plugin Name: WP Smush.it
|
3 |
-
Version: 1.
|
4 |
Author: WPMU DEV
|
5 |
Author URI: http://premium.wpmudev.org
|
6 |
Contributors: WPMUDEV, alexdunae
|
7 |
-
Tags: Compress,
|
8 |
Requires at least: 3.5
|
9 |
-
Tested up to: 4.
|
10 |
-
Stable tag: 1.
|
11 |
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
12 |
|
13 |
-
|
14 |
|
15 |
== Description ==
|
16 |
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
* optimizing JPEG compression
|
21 |
-
* converting certain GIFs to indexed PNGs
|
22 |
-
* stripping the un-used colours from indexed images
|
23 |
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
Every image you add to a page or post will be automatically run through Smush.it behind the scenes. You don’t have to do anything different.
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
= About Us =
|
47 |
-
WPMU DEV is a premium supplier of quality WordPress plugins and themes. For premium support with any WordPress related issues you can join us here:
|
48 |
-
<a href="http://premium.wpmudev.org/join/">http://premium.wpmudev.org/join/</a>
|
49 |
-
|
50 |
-
Don't forget to stay up to date on everything WordPress from the Internet's number one resource:
|
51 |
-
<a href="http://wpmu.org/">http://wpmu.org</a>
|
52 |
|
53 |
|
54 |
== Screenshots ==
|
@@ -72,6 +73,12 @@ This will give us a good foundation to start adding some new features!
|
|
72 |
|
73 |
== Changelog ==
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
= 1.6.5.4 =
|
76 |
* Added settings option to disable check for home url.
|
77 |
* for PHP 5.4.x reordered class WpSmushit contructors to prevent Strict Standards Exception
|
1 |
=== WP Smush.it ===
|
2 |
Plugin Name: WP Smush.it
|
3 |
+
Version: 1.7
|
4 |
Author: WPMU DEV
|
5 |
Author URI: http://premium.wpmudev.org
|
6 |
Contributors: WPMUDEV, alexdunae
|
7 |
+
Tags: Compress,Images,Compression,Optimise,Optimize,Photo,Photos,Pictures,Smush,Smush.it,Upload,Yahoo,Yahoo Smush.it
|
8 |
Requires at least: 3.5
|
9 |
+
Tested up to: 4.1
|
10 |
+
Stable tag: 1.7
|
11 |
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
12 |
|
13 |
+
Improve performance and get faster load times by optimizing image files with <a href="http://smush.it/">Smush.it</a> for WordPress – “It's the best plugin of its kind.”
|
14 |
|
15 |
== Description ==
|
16 |
|
17 |
+
WP Smush.it strips hidden, bulky information from your images, reducing the file size without losing quality. The faster your site loads, the more Google, Bing, Yahoo and other search engines will like it.
|
18 |
|
19 |
+
[youtube https://www.youtube.com/watch?v=_74QFoRb230]
|
|
|
|
|
|
|
20 |
|
21 |
+
Heavy image files may be slowing down your site without you even knowing it. WP Smush.it meticulously scans every image you upload – or have already added to your site – and cuts all the unnecessary data for you.
|
22 |
|
23 |
+
★★★★★ <br>
|
24 |
+
“I had no idea that my page load time was being dragged down by the images. The plugin nearly halved the time it took.” - <a href="http://profiles.wordpress.org/karlcw">karlcw</a>
|
25 |
|
26 |
+
Install WP Smush.it and find out why it's the most popular image optimization plugin for WordPress available today with over 1 million downloads.
|
|
|
27 |
|
28 |
+
<blockquote>
|
29 |
+
<h4>If you like WP Smush.it, you'll love WP Smush Pro</h4>
|
30 |
+
<br>
|
31 |
+
<a href="http://premium.wpmudev.org/project/wp-smush-pro/">WP Smush Pro</a> gives you everything you'll find in WP Smush.it and more:
|
32 |
+
<ul>
|
33 |
+
<li>Access better quality, higher performing WPMU DEV dedicated smushing servers</li>
|
34 |
+
<li>Smush images up to 5MB (WP Smush.it is limited to 1MB)</li>
|
35 |
+
<li>No more batch smushing dropouts or timeouts (you have your own dedicated servers)</li>
|
36 |
+
<li>Works with HTTPS images (WP Smush.it won’t work with HTTPS)</li>
|
37 |
+
<li>24/7/365 support from <a href="http://premium.wpmudev.org/support/">the best WordPress support team on the planet</a></li>
|
38 |
+
<li><a href="http://premium.wpmudev.org/join/">400+ other premium plugins and themes</a> included in your membership</li>
|
39 |
+
</ul>
|
40 |
|
41 |
+
Upgrade to <a href="http://premium.wpmudev.org/project/wp-smush-pro/">WP Smush Pro</a> and optimize more and larger image files faster and with greater reliability to increase your site’s performance.
|
42 |
+
</blockquote>
|
43 |
|
44 |
+
Features available to both WP Smush.it and pro users include:
|
45 |
+
<ul>
|
46 |
+
<li>Optimize your images using lossless compression techniques</li>
|
47 |
+
<li>Process JPEG, GIF and PNG image files</li>
|
48 |
+
<li>Compress files 1 MB or smaller</li>
|
49 |
+
<li>Strip unused colours from indexed images</li>
|
50 |
+
<li>Both versions are fully internationalized</li>
|
51 |
+
</ul>
|
52 |
+
Discover for yourself why WP Smush.it is the most popular free image optimization plugin with more than a million downloads.
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
|
55 |
== Screenshots ==
|
73 |
|
74 |
== Changelog ==
|
75 |
|
76 |
+
= 1.7 =
|
77 |
+
* Use Ajax for Bulk Smush to avoid timeouts and internal server error
|
78 |
+
* Other Notice and bug fixes
|
79 |
+
* Settings moved under Media > WP Smush.it
|
80 |
+
* Added debug log file
|
81 |
+
|
82 |
= 1.6.5.4 =
|
83 |
* Added settings option to disable check for home url.
|
84 |
* for PHP 5.4.x reordered class WpSmushit contructors to prevent Strict Standards Exception
|
screenshot-1.jpg
CHANGED
Binary file
|
uninstall.php
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Remove plugin settings data
|
4 |
+
*
|
5 |
+
* @since 1.7
|
6 |
+
*
|
7 |
+
*/
|
8 |
+
|
9 |
+
//if uninstall not called from WordPress exit
|
10 |
+
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
11 |
+
exit();
|
12 |
+
}
|
13 |
+
global $wpdb;
|
14 |
+
|
15 |
+
$smushit_keys = array(
|
16 |
+
'smushit_auto',
|
17 |
+
'smushit_timeout',
|
18 |
+
'smushit_enforce_same_url',
|
19 |
+
'smushit_debug',
|
20 |
+
'error_log',
|
21 |
+
'notice_log'
|
22 |
+
);
|
23 |
+
foreach ( $smushit_keys as $key ) {
|
24 |
+
$key = 'wp_smushit_' . $key;
|
25 |
+
if ( is_multisite() ) {
|
26 |
+
$blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A );
|
27 |
+
if ( $blogs ) {
|
28 |
+
foreach ( $blogs as $blog ) {
|
29 |
+
switch_to_blog( $blog['blog_id'] );
|
30 |
+
delete_option( $key );
|
31 |
+
delete_site_option( $key );
|
32 |
+
}
|
33 |
+
restore_current_blog();
|
34 |
+
}
|
35 |
+
} else {
|
36 |
+
delete_option( $key );
|
37 |
+
}
|
38 |
+
}
|
39 |
+
?>
|
wp-smushit.php
CHANGED
@@ -4,7 +4,7 @@ Plugin Name: WP Smush.it
|
|
4 |
Plugin URI: http://wordpress.org/extend/plugins/wp-smushit/
|
5 |
Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
|
6 |
Author: WPMU DEV
|
7 |
-
Version: 1.
|
8 |
Author URI: http://premium.wpmudev.org/
|
9 |
Textdomain: wp_smushit
|
10 |
*/
|
@@ -31,631 +31,482 @@ along with this program; if not, write to the Free Software
|
|
31 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
32 |
*/
|
33 |
|
34 |
-
if ( !function_exists( 'download_url' ) ) {
|
35 |
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
36 |
}
|
37 |
|
38 |
-
if ( !class_exists( 'WpSmushit' ) ) {
|
39 |
|
40 |
-
class WpSmushit {
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
/**
|
45 |
-
* Constructor
|
46 |
-
*/
|
47 |
-
function __construct( ) {
|
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 |
-
define( '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
-
|
75 |
-
/*
|
76 |
-
Each service has a setting specifying whether it should be used automatically on upload.
|
77 |
-
Values are:
|
78 |
-
-1 Don't use (until manually enabled via Media > Settings)
|
79 |
-
0 Use automatically
|
80 |
-
n Any other number is a Unix timestamp indicating when the service can be used again
|
81 |
-
*/
|
82 |
-
|
83 |
-
define('WP_SMUSHIT_AUTO_OK', 0);
|
84 |
-
define('WP_SMUSHIT_AUTO_NEVER', -1);
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
*/
|
89 |
-
if ( WP_SMUSHIT_AUTO == WP_SMUSHIT_AUTO_OK ) {
|
90 |
-
add_filter( 'wp_generate_attachment_metadata', array( &$this, 'resize_from_meta_data' ), 10, 2 );
|
91 |
}
|
92 |
-
add_filter( 'manage_media_columns', array( &$this, 'columns' ) );
|
93 |
-
add_action( 'manage_media_custom_column', array( &$this, 'custom_column' ), 10, 2 );
|
94 |
-
add_action( 'admin_init', array( &$this, 'admin_init' ) );
|
95 |
-
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
|
96 |
-
add_action( 'admin_action_wp_smushit_manual', array( &$this, 'smushit_manual' ) );
|
97 |
-
add_action( 'admin_head-upload.php', array( &$this, 'add_bulk_actions_via_javascript' ) );
|
98 |
-
add_action( 'admin_action_bulk_smushit', array( &$this, 'bulk_action_handler' ) );
|
99 |
-
add_action( 'admin_init', array( &$this, 'register_settings' ) );
|
100 |
-
}
|
101 |
-
|
102 |
-
function WpSmushit( ) {
|
103 |
-
$this->__construct( );
|
104 |
-
}
|
105 |
-
|
106 |
-
/**
|
107 |
-
* Plugin setting functions
|
108 |
-
*/
|
109 |
-
function register_settings( ) {
|
110 |
-
add_settings_section( 'wp_smushit_settings', 'WP Smush.it', array( &$this, 'settings_cb' ), 'media' );
|
111 |
|
112 |
-
|
113 |
-
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
117 |
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
-
|
122 |
-
|
|
|
123 |
|
124 |
-
|
125 |
-
register_setting( 'media', 'wp_smushit_smushit_timeout' );
|
126 |
-
register_setting( 'media', 'wp_smushit_smushit_enforce_same_url' );
|
127 |
-
register_setting( 'media', 'wp_smushit_smushit_debug' );
|
128 |
-
}
|
129 |
|
130 |
-
|
131 |
-
}
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
$val = intval( get_option( $key, WP_SMUSHIT_AUTO_OK ) );
|
136 |
-
printf( "<select name='%1\$s' id='%1\$s'>", esc_attr( $key ) );
|
137 |
-
echo '<option value=' . WP_SMUSHIT_AUTO_OK . ' ' . selected( WP_SMUSHIT_AUTO_OK, $val ) . '>'. __( 'Automatically process on upload', WP_SMUSHIT_DOMAIN ) . '</option>';
|
138 |
-
echo '<option value=' . WP_SMUSHIT_AUTO_NEVER . ' ' . selected( WP_SMUSHIT_AUTO_NEVER, $val ) . '>'. __( 'Do not process on upload', WP_SMUSHIT_DOMAIN ) . '</option>';
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
printf( __( 'Temporarily disabled until %s', WP_SMUSHIT_DOMAIN ), date( 'M j, Y \a\t H:i', $val ) ).'</option>';
|
143 |
}
|
144 |
-
echo '</select>';
|
145 |
-
}
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
?><input type="checkbox" name="<?php echo $key ?>" <?php if ($val == 'on') { echo ' checked="checked" '; } ?>/> <?php
|
157 |
-
echo '<strong>'. get_option('home'). '</strong><br />'.__( 'By default the plugin will enforce that the image URL is the same domain as the home. If you are using a sub-domain pointed to this same host or an external Content Delivery Network (CDN) you want to unset this option.', WP_SMUSHIT_DOMAIN );
|
158 |
-
}
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
?><input type="checkbox" name="<?php echo $key ?>" <?php if ($val) { echo ' checked="checked" '; } ?>/> <?php _e( 'If you are having trouble with the plugin enable this option can reveal some information about your system needed for support.', WP_SMUSHIT_DOMAIN );
|
164 |
-
}
|
165 |
|
166 |
-
|
167 |
-
function temporarily_disable( $seconds = 21600 ) {
|
168 |
-
update_option( 'wp_smushit_smushit_auto', time() + $seconds );
|
169 |
-
}
|
170 |
-
|
171 |
-
function admin_init( ) {
|
172 |
-
load_plugin_textdomain(WP_SMUSHIT_DOMAIN, false, dirname(plugin_basename(__FILE__)).'/languages/');
|
173 |
-
wp_enqueue_script( 'common' );
|
174 |
-
}
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
@ini_set('output_buffering','on');
|
185 |
-
@ini_set('zlib.output_compression', 0);
|
186 |
-
@ini_set('implicit_flush', 1);
|
187 |
-
|
188 |
-
$attachments = null;
|
189 |
-
$auto_start = false;
|
190 |
-
|
191 |
-
if ( isset($_REQUEST['ids'] ) ) {
|
192 |
-
$attachments = get_posts( array(
|
193 |
-
'numberposts' => -1,
|
194 |
-
'include' => explode(',', $_REQUEST['ids']),
|
195 |
-
'post_type' => 'attachment',
|
196 |
-
'post_mime_type' => 'image'
|
197 |
-
));
|
198 |
-
$auto_start = true;
|
199 |
-
} else {
|
200 |
-
$attachments = get_posts( array(
|
201 |
-
'numberposts' => -1,
|
202 |
-
'post_type' => 'attachment',
|
203 |
-
'post_mime_type' => 'image'
|
204 |
-
));
|
205 |
-
}
|
206 |
-
?>
|
207 |
-
<div class="wrap">
|
208 |
-
<div id="icon-upload" class="icon32"><br /></div><h2><?php _e( 'Bulk WP Smush.it', WP_SMUSHIT_DOMAIN ) ?></h2>
|
209 |
-
<?php
|
210 |
-
|
211 |
-
if ( sizeof($attachments) < 1 ) {
|
212 |
-
_e( "<p>You don't appear to have uploaded any images yet.</p>", WP_SMUSHIT_DOMAIN );
|
213 |
-
} else {
|
214 |
-
if ( empty($_POST) && !$auto_start ){ // instructions page
|
215 |
-
|
216 |
-
_e("<p>This tool will run all of the images in your media library through the WP Smush.it web service. Any image already processed will not be reprocessed. Any new images or unsuccessful attempts will be processed.</p>", WP_SMUSHIT_DOMAIN );
|
217 |
-
_e("<p>As part of the Yahoo! Smush.it API this plugin wil provide a URL to each of your images to be processed. The Yahoo! service will download the image via the URL. The Yahoo Smush.it service will then return a URL to this plugin of the new version of the image. This image will be downloaded and replace the original image on your server.</p>", WP_SMUSHIT_DOMAIN );
|
218 |
-
|
219 |
-
_e('<p>Limitations of using the Yahoo Smush.it API</p>', WP_SMUSHIT_DOMAIN);
|
220 |
-
?>
|
221 |
-
<ol>
|
222 |
-
<li><?php _e('The image MUST be less than 1 megabyte in size. This is a limit of the Yahoo! service not this plugin.', WP_SMUSHIT_DOMAIN); ?></li>
|
223 |
-
<li><?php _e('The image MUST be accessible via a non-https URL. The Yahoo! Smush.it service will not handle https:// image URLs. This is a limit of the Yahoo! service not this plugin.', WP_SMUSHIT_DOMAIN); ?></li>
|
224 |
-
<li><?php _e('The image MUST publicly accessible server. As the Yahoo! Smush.it service needs to download the image via a URL the image needs to be on a public server and not a local local development system. This is a limit of the Yahoo! service not this plugin.', WP_SMUSHIT_DOMAIN); ?></li>
|
225 |
-
<li><?php _e('The image MUST be local to the site. This plugin cannot update images stored on Content Delivery Networks (CDN)', WP_SMUSHIT_DOMAIN); ?></li>
|
226 |
-
</ol>
|
227 |
-
<?php
|
228 |
-
printf( __( "<p><strong>This is an experimental feature.</strong> Please post any feedback to the %s.</p>", WP_SMUSHIT_DOMAIN ), '<a href="http://wordpress.org/tags/wp-smushit">'. __( 'WordPress WP Smush.it forums', WP_SMUSHIT_DOMAIN ). '</a>' );
|
229 |
-
|
230 |
-
?>
|
231 |
-
<hr />
|
232 |
-
<?php printf( __( "<p>We found %d images in your media library. Be forewarned, <strong>it will take <em>at least</em> %d minutes</strong> to process all these images if they have never been smushed before.</p>", WP_SMUSHIT_DOMAIN ), sizeof($attachments), sizeof($attachments) * 3 / 60 ); ?>
|
233 |
-
<form method="post" action="">
|
234 |
-
<?php wp_nonce_field( 'wp-smushit-bulk', '_wpnonce'); ?>
|
235 |
-
<button type="submit" class="button-secondary action"><?php _e( 'Run all my images through WP Smush.it right now', WP_SMUSHIT_DOMAIN ) ?></button>
|
236 |
-
<?php _e( "<p><em>N.B. If your server <tt>gzip</tt>s content you may not see the progress updates as your files are processed.</em></p>", WP_SMUSHIT_DOMAIN ); ?>
|
237 |
-
<?php
|
238 |
-
if (WP_SMUSHIT_DEBUG) {
|
239 |
-
_e( "<p>DEBUG mode is currently enabled. To disable see the Settings > Media page.</p>", WP_SMUSHIT_DOMAIN );
|
240 |
-
}
|
241 |
-
?>
|
242 |
-
</form>
|
243 |
-
<?php
|
244 |
-
} else { // run the script
|
245 |
|
246 |
-
|
247 |
-
|
248 |
-
|
|
|
249 |
|
|
|
|
|
|
|
|
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
$
|
258 |
-
|
259 |
-
|
260 |
-
if (
|
261 |
-
|
262 |
-
&& (stripos( $meta['wp_smushit'], 'Smush.it error' ) === false ) ) {
|
263 |
-
if ((stripos( $meta['wp_smushit'], '<a' ) === false)
|
264 |
-
&& (stripos( $meta['wp_smushit'], __('No savings', WP_SMUSHIT_DOMAIN )) === false))
|
265 |
-
echo $meta['wp_smushit'] .' '. __('<strong>already smushed</strong>', WP_SMUSHIT_DOMAIN);
|
266 |
-
else
|
267 |
-
echo $meta['wp_smushit'];
|
268 |
-
} else {
|
269 |
-
echo $meta['wp_smushit'];
|
270 |
}
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
foreach( $meta['sizes'] as $size_name => $size ) {
|
275 |
-
printf( "— [%s] %d x %d: ", $size_name, intval($size['width']), intval($size['height']) );
|
276 |
-
if ( $original_meta['sizes'][$size_name]['wp_smushit'] == $size['wp_smushit'] && stripos( $meta['sizes'][$size_name]['wp_smushit'], 'Smush.it error' ) === false ) {
|
277 |
-
echo $size['wp_smushit'] .' '. __('<strong>already smushed</strong>', WP_SMUSHIT_DOMAIN);
|
278 |
-
} else {
|
279 |
-
echo $size['wp_smushit'];
|
280 |
-
}
|
281 |
-
echo '<br />';
|
282 |
}
|
283 |
}
|
284 |
-
|
|
|
|
|
285 |
|
286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
flush();
|
292 |
}
|
293 |
-
_e('<hr /></p>Smush.it finished processing.</p>', WP_SMUSHIT_DOMAIN);
|
294 |
}
|
295 |
-
}
|
296 |
-
?>
|
297 |
-
</div>
|
298 |
-
<?php
|
299 |
-
}
|
300 |
|
301 |
-
|
302 |
-
|
303 |
-
*/
|
304 |
-
function smushit_manual( ) {
|
305 |
-
if ( !current_user_can('upload_files') ) {
|
306 |
-
wp_die( __( "You don't have permission to work with uploaded files.", WP_SMUSHIT_DOMAIN ) );
|
307 |
-
}
|
308 |
|
309 |
-
|
310 |
-
wp_die( __( 'No attachment ID was provided.', WP_SMUSHIT_DOMAIN ) );
|
311 |
-
}
|
312 |
|
313 |
-
|
|
|
314 |
|
315 |
-
|
|
|
316 |
|
317 |
-
|
318 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
319 |
|
320 |
-
|
321 |
-
exit();
|
322 |
-
}
|
323 |
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
* Returns an array of the $file $results.
|
328 |
-
*
|
329 |
-
* @param string $file Full absolute path to the image file
|
330 |
-
* @param string $file_url Optional full URL to the image file
|
331 |
-
* @returns array
|
332 |
-
*/
|
333 |
-
function do_smushit( $file_path = '', $file_url = '' ) {
|
334 |
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
static $error_count = 0;
|
344 |
|
345 |
-
|
346 |
-
|
347 |
-
}
|
348 |
|
349 |
-
|
350 |
-
if ( !file_exists( $file_path ) || !is_file( $file_path ) ) {
|
351 |
-
return sprintf( __( "ERROR: Could not find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN ), $file_path );
|
352 |
-
}
|
353 |
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
|
|
358 |
|
359 |
-
|
360 |
-
if ( $file_size > WP_SMUSHIT_MAX_BYTES ) {
|
361 |
-
return sprintf(__('ERROR: <span style="color:#FF0000;">Skipped (%s) Unable to Smush due to Yahoo 1mb size limits. See <a href="http://developer.yahoo.com/yslow/smushit/faq.html#faq_restrict">FAQ</a></span>', WP_SMUSHIT_DOMAIN), $this->format_bytes($file_size));
|
362 |
-
}
|
363 |
|
364 |
-
|
365 |
-
|
366 |
-
// from being processed.
|
367 |
-
// check that the file is within the WP_CONTENT_DIR
|
368 |
-
// But first convert the slashes to be uniform. Really with WP would already do this!
|
369 |
-
/*
|
370 |
-
$file_path_tmp = str_replace('\\', '/', $file_path);
|
371 |
-
$WP_CONTENT_DIR_tmp = str_replace('\\', '/', WP_CONTENT_DIR);
|
372 |
-
|
373 |
-
if (WP_SMUSHIT_DEBUG) {
|
374 |
-
echo "DEBUG: file_path [". $file_path_tmp ."] WP_CONTENT_DIR [". $WP_CONTENT_DIR_tmp ."]<br />";
|
375 |
-
}
|
376 |
-
if (stripos( $file_path_tmp, $WP_CONTENT_DIR_tmp) !== 0) {
|
377 |
-
return sprintf( __( "ERROR: <span class='code'>%s</span> must be within the website content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN ),
|
378 |
-
htmlentities( $file_path_tmp ), $WP_CONTENT_DIR_tmp);
|
379 |
-
}
|
380 |
-
}
|
381 |
-
*/
|
382 |
-
// The Yahoo! Smush.it service does not working with https images.
|
383 |
-
$file_url = str_replace('https://', 'http://', $file_url);
|
384 |
|
385 |
-
|
386 |
-
// to this same host or may be an external CDN. In either case the image would be the same. So we let the Yahoo! Smush.it service use the image URL with
|
387 |
-
// the assumption the remote image and the local file are the same. Also with the assumption that the CDN service will somehow be updated when the image
|
388 |
-
// is changed.
|
389 |
-
if ((defined('WP_SMUSHIT_ENFORCE_SAME_URL')) && (WP_SMUSHIT_ENFORCE_SAME_URL == 'on')) {
|
390 |
-
$home_url = str_replace('https://', 'http://', get_option('home'));
|
391 |
-
if (WP_SMUSHIT_DEBUG) {
|
392 |
-
echo "DEBUG: file_url [". $file_url ."] home_url [". $home_url ."]<br />";
|
393 |
}
|
394 |
|
395 |
-
if (
|
396 |
-
return sprintf( __( "
|
397 |
-
htmlentities( $file_url ), $home_url);
|
398 |
}
|
399 |
-
}
|
400 |
-
//echo "calling _post(". $file_url .")<br />";
|
401 |
-
$data = $this->_post( $file_url );
|
402 |
-
|
403 |
-
//echo "returned from _post data<pre>"; print_r($data); echo "</pre>";
|
404 |
-
|
405 |
-
if ( false === $data ) {
|
406 |
-
$error_count++;
|
407 |
-
return __( 'ERROR: posting to Smush.it', WP_SMUSHIT_DOMAIN );
|
408 |
-
}
|
409 |
-
|
410 |
-
// make sure the response looks like JSON -- added 2008-12-19 when
|
411 |
-
// Smush.it was returning PHP warnings before the JSON output
|
412 |
-
if ( strpos( trim($data), '{' ) != 0 ) {
|
413 |
-
return __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN );
|
414 |
-
}
|
415 |
-
|
416 |
-
// read the JSON response
|
417 |
-
if ( function_exists('json_decode') ) {
|
418 |
-
$data = json_decode( $data );
|
419 |
-
} else {
|
420 |
-
require_once( 'JSON/JSON.php' );
|
421 |
-
$json = new Services_JSON( );
|
422 |
-
$data = $json->decode( $data );
|
423 |
-
}
|
424 |
-
if (WP_SMUSHIT_DEBUG) {
|
425 |
-
echo "DEBUG: return from API: data<pre>"; print_r($data); echo "</pre>";
|
426 |
-
}
|
427 |
|
428 |
-
|
|
|
|
|
|
|
|
|
|
|
429 |
|
430 |
-
|
431 |
-
|
|
|
432 |
|
433 |
-
|
434 |
-
|
|
|
435 |
|
436 |
-
|
437 |
-
$err = ( $data->error ? __( 'Smush.it error: ', WP_SMUSHIT_DOMAIN ) . $data->error : __( 'unknown error', WP_SMUSHIT_DOMAIN ) );
|
438 |
-
$err .= sprintf( __( " while processing <span class='code'>%s</span> (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN ), $file_url, $file_path);
|
439 |
-
return $err ;
|
440 |
}
|
441 |
|
442 |
-
$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
|
444 |
-
|
445 |
-
|
446 |
-
if ( 0 !== stripos($processed_url, 'http://') ) {
|
447 |
-
$processed_url = SMUSHIT_BASE_URL . $processed_url;
|
448 |
}
|
449 |
|
450 |
-
$temp_file = download_url( $processed_url );
|
451 |
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
456 |
|
457 |
-
|
458 |
-
|
459 |
-
}
|
460 |
-
|
461 |
-
@unlink( $file_path );
|
462 |
-
$success = @rename( $temp_file, $file_path );
|
463 |
-
if (!$success) {
|
464 |
-
copy($temp_file, $file_path);
|
465 |
-
unlink($temp_file);
|
466 |
-
}
|
467 |
-
|
468 |
-
$savings = intval( $data->src_size ) - intval( $data->dest_size );
|
469 |
-
$savings_str = $this->format_bytes( $savings, 1 );
|
470 |
-
$savings_str = str_replace( ' ', ' ', $savings_str );
|
471 |
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
|
476 |
-
|
477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
}
|
483 |
|
484 |
-
|
485 |
-
return false;
|
486 |
-
}
|
487 |
|
488 |
-
|
489 |
-
return true;
|
490 |
-
}
|
491 |
|
|
|
|
|
492 |
|
493 |
-
|
494 |
-
* Read the image paths from an attachment's meta data and process each image
|
495 |
-
* with wp_smushit().
|
496 |
-
*
|
497 |
-
* This method also adds a `wp_smushit` meta key for use in the media library.
|
498 |
-
*
|
499 |
-
* Called after `wp_generate_attachment_metadata` is completed.
|
500 |
-
*/
|
501 |
-
function resize_from_meta_data( $meta, $ID = null, $force_resmush = true ) {
|
502 |
-
if ( $ID && wp_attachment_is_image( $ID ) === false ) {
|
503 |
return $meta;
|
504 |
}
|
505 |
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
514 |
|
515 |
-
|
516 |
-
$meta['wp_smushit'] = $this->do_smushit($attachment_file_path, $attachment_file_url);
|
517 |
}
|
518 |
|
519 |
-
// no resized versions, so we can exit
|
520 |
-
if ( !isset( $meta['sizes'] ) )
|
521 |
-
return $meta;
|
522 |
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
// path. So just get the dirname and rpelace the filename.
|
530 |
-
$attachment_file_path_size = trailingslashit(dirname($attachment_file_path)) . $size_data['file'];
|
531 |
-
if (WP_SMUSHIT_DEBUG) {
|
532 |
-
echo "DEBUG: attachment_file_path_size=[". $attachment_file_path_size ."]<br />";
|
533 |
-
}
|
534 |
-
|
535 |
-
$attachment_file_url_size = trailingslashit(dirname($attachment_file_url)) . $size_data['file'];
|
536 |
-
if (WP_SMUSHIT_DEBUG) {
|
537 |
-
echo "DEBUG: attachment_file_url_size=[". $attachment_file_url_size ."]<br />";
|
538 |
-
}
|
539 |
|
540 |
-
|
541 |
-
|
542 |
-
//echo "size_key[". $size_key ."] wp_smushit<pre>"; print_r($meta['sizes'][$size_key]['wp_smushit']); echo "</pre>";
|
543 |
}
|
544 |
-
|
545 |
-
//echo "meta<pre>"; print_r($meta); echo "</pre>";
|
546 |
-
return $meta;
|
547 |
-
}
|
548 |
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
|
|
|
|
557 |
|
558 |
-
|
559 |
-
if (WP_SMUSHIT_DEBUG) {
|
560 |
-
echo "DEBUG: Calling API: [". $req."]<br />";
|
561 |
}
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
568 |
}
|
569 |
-
} else {
|
570 |
-
wp_die( __('WP Smush.it requires WordPress 2.8 or greater', WP_SMUSHIT_DOMAIN) );
|
571 |
}
|
572 |
|
573 |
-
return $data;
|
574 |
-
}
|
575 |
-
|
576 |
-
|
577 |
-
/**
|
578 |
-
* Print column header for Smush.it results in the media library using
|
579 |
-
* the `manage_media_columns` hook.
|
580 |
-
*/
|
581 |
-
function columns( $defaults ) {
|
582 |
-
$defaults['smushit'] = 'Smush.it';
|
583 |
-
return $defaults;
|
584 |
-
}
|
585 |
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
$bytes /= pow(1024, $pow);
|
596 |
-
return round($bytes, $precision) . ' ' . $units[$pow];
|
597 |
-
}
|
598 |
-
|
599 |
-
/**
|
600 |
-
* Print column data for Smush.it results in the media library using
|
601 |
-
* the `manage_media_custom_column` hook.
|
602 |
-
*/
|
603 |
-
function custom_column( $column_name, $id ) {
|
604 |
-
if( 'smushit' == $column_name ) {
|
605 |
-
$data = wp_get_attachment_metadata($id);
|
606 |
-
if ( isset( $data['wp_smushit'] ) && !empty( $data['wp_smushit'] ) ) {
|
607 |
-
print $data['wp_smushit'];
|
608 |
-
printf( "<br><a href=\"admin.php?action=wp_smushit_manual&attachment_ID=%d\">%s</a>",
|
609 |
-
$id,
|
610 |
-
__( 'Re-smush', WP_SMUSHIT_DOMAIN ) );
|
611 |
-
} else {
|
612 |
-
if ( wp_attachment_is_image( $id ) ) {
|
613 |
-
print __( 'Not processed', WP_SMUSHIT_DOMAIN );
|
614 |
-
printf( "<br><a href=\"admin.php?action=wp_smushit_manual&attachment_ID=%d\">%s</a>",
|
615 |
-
$id,
|
616 |
-
__('Smush.it now!', WP_SMUSHIT_DOMAIN));
|
617 |
-
}
|
618 |
-
}
|
619 |
}
|
620 |
-
}
|
621 |
|
622 |
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
$('select[name^="action"] option:last-child').before('<option value="bulk_smushit">Bulk Smush.it</option>');
|
628 |
-
});
|
629 |
-
</script>
|
630 |
-
<?php }
|
631 |
|
|
|
|
|
|
|
632 |
|
633 |
-
|
634 |
-
// Borrowed from http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
|
635 |
-
function bulk_action_handler() {
|
636 |
-
check_admin_referer( 'bulk-media' );
|
637 |
|
638 |
-
|
639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
640 |
|
641 |
-
|
|
|
|
|
|
|
642 |
|
643 |
-
// Can't use wp_nonce_url() as it escapes HTML entities
|
644 |
-
wp_redirect( add_query_arg( '_wpnonce', wp_create_nonce( 'wp-smushit-bulk' ), admin_url( 'upload.php?page=wp-smushit-bulk&goback=1&ids=' . $ids ) ) );
|
645 |
-
exit();
|
646 |
}
|
647 |
-
|
648 |
-
}
|
649 |
|
650 |
-
$WpSmushit = new WpSmushit();
|
651 |
-
global $WpSmushit;
|
652 |
|
653 |
}
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
659 |
function wp_basename( $path, $suffix = '' ) {
|
660 |
return urldecode( basename( str_replace( '%2F', '/', urlencode( $path ) ), $suffix ) );
|
661 |
}
|
4 |
Plugin URI: http://wordpress.org/extend/plugins/wp-smushit/
|
5 |
Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
|
6 |
Author: WPMU DEV
|
7 |
+
Version: 1.7
|
8 |
Author URI: http://premium.wpmudev.org/
|
9 |
Textdomain: wp_smushit
|
10 |
*/
|
31 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
32 |
*/
|
33 |
|
34 |
+
if ( ! function_exists( 'download_url' ) ) {
|
35 |
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
36 |
}
|
37 |
|
38 |
+
if ( ! class_exists( 'WpSmushit' ) ) {
|
39 |
|
40 |
+
class WpSmushit {
|
41 |
|
42 |
+
var $version = "1.6.6";
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
/**
|
45 |
+
* Constructor
|
46 |
*/
|
47 |
+
function __construct() {
|
48 |
+
|
49 |
+
/**
|
50 |
+
* Constants
|
51 |
+
*/
|
52 |
+
define( 'SMUSHIT_REQ_URL', 'http://www.smushit.com/ysmush.it/ws.php?img=%s' );
|
53 |
+
define( 'SMUSHIT_BASE_URL', 'http://www.smushit.com/' );
|
54 |
+
|
55 |
+
define( 'WP_SMUSHIT_DOMAIN', 'wp_smushit' );
|
56 |
+
define( 'WP_SMUSHIT_UA', "WP Smush.it/{$this->version} (+http://wordpress.org/extend/plugins/wp-smushit/)" );
|
57 |
+
define( 'WP_SMUSHIT_DIR', plugin_dir_path( __FILE__ ) );
|
58 |
+
define( 'WP_SMUSHIT_URL', plugin_dir_url( __FILE__ ) );
|
59 |
+
define( 'WP_SMUSHIT_MAX_BYTES', 1048576 );
|
60 |
+
|
61 |
+
// The number of images (including generated sizes) that can return errors before abandoning all hope.
|
62 |
+
// N.B. this doesn't work with the bulk uploader, since it creates a new HTTP request
|
63 |
+
// for each image. It does work with the bulk smusher, though.
|
64 |
+
define( 'WP_SMUSHIT_ERRORS_BEFORE_QUITTING', 3 * count( get_intermediate_image_sizes() ) );
|
65 |
+
|
66 |
+
define( 'WP_SMUSHIT_AUTO', intval( get_option( 'wp_smushit_smushit_auto', 0 ) ) );
|
67 |
+
define( 'WP_SMUSHIT_TIMEOUT', intval( get_option( 'wp_smushit_smushit_timeout', 60 ) ) );
|
68 |
+
define( 'WP_SMUSHIT_ENFORCE_SAME_URL', get_option( 'wp_smushit_smushit_enforce_same_url', 'on' ) );
|
69 |
+
|
70 |
+
define( 'WP_SMUSHIT_DEBUG', get_option( 'wp_smushit_smushit_debug', '' ) );
|
71 |
+
|
72 |
+
/*
|
73 |
+
Each service has a setting specifying whether it should be used automatically on upload.
|
74 |
+
Values are:
|
75 |
+
-1 Don't use (until manually enabled via Media > Settings)
|
76 |
+
0 Use automatically
|
77 |
+
n Any other number is a Unix timestamp indicating when the service can be used again
|
78 |
+
*/
|
79 |
+
|
80 |
+
define( 'WP_SMUSHIT_AUTO_OK', 0 );
|
81 |
+
define( 'WP_SMUSHIT_AUTO_NEVER', - 1 );
|
82 |
+
|
83 |
+
/**
|
84 |
+
* Hooks
|
85 |
+
*/
|
86 |
+
if ( WP_SMUSHIT_AUTO == WP_SMUSHIT_AUTO_OK ) {
|
87 |
+
add_filter( 'wp_generate_attachment_metadata', array( &$this, 'resize_from_meta_data' ), 10, 2 );
|
88 |
+
}
|
89 |
+
add_filter( 'manage_media_columns', array( &$this, 'columns' ) );
|
90 |
+
add_action( 'manage_media_custom_column', array( &$this, 'custom_column' ), 10, 2 );
|
91 |
+
add_action( 'admin_init', array( &$this, 'admin_init' ) );
|
92 |
+
add_action( 'admin_action_wp_smushit_manual', array( &$this, 'smushit_manual' ) );
|
93 |
+
add_action( 'admin_head-upload.php', array( &$this, 'add_bulk_actions_via_javascript' ) );
|
94 |
+
add_action( 'admin_action_bulk_smushit', array( &$this, 'bulk_action_handler' ) );
|
95 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
function WpSmushit() {
|
98 |
+
$this->__construct();
|
|
|
|
|
|
|
99 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
function settings_cb() {
|
102 |
+
}
|
103 |
|
104 |
+
function admin_init() {
|
105 |
+
load_plugin_textdomain( WP_SMUSHIT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
|
106 |
+
wp_enqueue_script( 'common' );
|
107 |
+
}
|
108 |
|
109 |
+
/**
|
110 |
+
* Manually process an image from the Media Library
|
111 |
+
*/
|
112 |
+
function smushit_manual() {
|
113 |
+
if ( ! current_user_can( 'upload_files' ) ) {
|
114 |
+
wp_die( __( "You don't have permission to work with uploaded files.", WP_SMUSHIT_DOMAIN ) );
|
115 |
+
}
|
116 |
|
117 |
+
if ( ! isset( $_GET['attachment_ID'] ) ) {
|
118 |
+
wp_die( __( 'No attachment ID was provided.', WP_SMUSHIT_DOMAIN ) );
|
119 |
+
}
|
120 |
|
121 |
+
$attachment_ID = intval( $_GET['attachment_ID'] );
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
$original_meta = wp_get_attachment_metadata( $attachment_ID );
|
|
|
124 |
|
125 |
+
$new_meta = $this->resize_from_meta_data( $original_meta, $attachment_ID );
|
126 |
+
wp_update_attachment_metadata( $attachment_ID, $new_meta );
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
wp_redirect( preg_replace( '|[^a-z0-9-~+_.?#=&;,/:]|i', '', wp_get_referer() ) );
|
129 |
+
exit();
|
|
|
130 |
}
|
|
|
|
|
131 |
|
132 |
+
/**
|
133 |
+
* Process an image with Smush.it.
|
134 |
+
*
|
135 |
+
* Returns an array of the $file $results.
|
136 |
+
*
|
137 |
+
* @param string $file Full absolute path to the image file
|
138 |
+
* @param string $file_url Optional full URL to the image file
|
139 |
+
*
|
140 |
+
* @returns array
|
141 |
+
*/
|
142 |
+
function do_smushit( $attachment_id, $file_path = '', $file_url = '' ) {
|
143 |
+
global $log;
|
144 |
|
145 |
+
if ( empty( $file_path ) ) {
|
146 |
+
return __( "File path is empty", WP_SMUSHIT_DOMAIN );
|
147 |
+
}
|
|
|
|
|
|
|
148 |
|
149 |
+
if ( empty( $file_url ) ) {
|
150 |
+
return __( "File URL is empty", WP_SMUSHIT_DOMAIN );
|
151 |
+
}
|
|
|
|
|
152 |
|
153 |
+
static $error_count = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
+
if ( $error_count >= WP_SMUSHIT_ERRORS_BEFORE_QUITTING ) {
|
156 |
+
return __( "Did not Smush.it due to previous errors", WP_SMUSHIT_DOMAIN );
|
157 |
+
}
|
158 |
|
159 |
+
// check that the file exists
|
160 |
+
if ( ! file_exists( $file_path ) || ! is_file( $file_path ) ) {
|
161 |
+
return sprintf( __( "ERROR: Could not find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN ), $file_path );
|
162 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
// check that the file is writable
|
165 |
+
if ( ! is_writable( dirname( $file_path ) ) ) {
|
166 |
+
return sprintf( __( "ERROR: <span class='code'>%s</span> is not writable", WP_SMUSHIT_DOMAIN ), dirname( $file_path ) );
|
167 |
+
}
|
168 |
|
169 |
+
$file_size = filesize( $file_path );
|
170 |
+
if ( $file_size > WP_SMUSHIT_MAX_BYTES ) {
|
171 |
+
return sprintf( __( 'ERROR: <span style="color:#FF0000;">Skipped (%s) Unable to Smush due to Yahoo 1mb size limits. See <a href="http://developer.yahoo.com/yslow/smushit/faq.html#faq_restrict">FAQ</a></span>', WP_SMUSHIT_DOMAIN ), $this->format_bytes( $file_size ) );
|
172 |
+
}
|
173 |
|
174 |
+
// local file check disabled 2013-09-05 - I don't see a point in checking this. We only use the URL of the file to Yahoo! and there is no gaurentee
|
175 |
+
// the file path is the SAME as the image from the URL. We can only really make sure the image URL starts with the home URL. This should prevent CDN URLs
|
176 |
+
// from being processed.
|
177 |
+
// check that the file is within the WP_CONTENT_DIR
|
178 |
+
// But first convert the slashes to be uniform. Really with WP would already do this!
|
179 |
+
/*
|
180 |
+
$file_path_tmp = str_replace('\\', '/', $file_path);
|
181 |
+
$WP_CONTENT_DIR_tmp = str_replace('\\', '/', WP_CONTENT_DIR);
|
182 |
+
|
183 |
+
if (WP_SMUSHIT_DEBUG) {
|
184 |
+
echo "DEBUG: file_path [". $file_path_tmp ."] WP_CONTENT_DIR [". $WP_CONTENT_DIR_tmp ."]<br />";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
}
|
186 |
+
if (stripos( $file_path_tmp, $WP_CONTENT_DIR_tmp) !== 0) {
|
187 |
+
return sprintf( __( "ERROR: <span class='code'>%s</span> must be within the website content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN ),
|
188 |
+
htmlentities( $file_path_tmp ), $WP_CONTENT_DIR_tmp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
}
|
190 |
}
|
191 |
+
*/
|
192 |
+
// The Yahoo! Smush.it service does not working with https images.
|
193 |
+
$file_url = str_replace( 'https://', 'http://', $file_url );
|
194 |
|
195 |
+
// File URL check disabled 2013-10-11 - The assumption here is the URL may not be the local site URL. The image may be served via a sub-domain pointed
|
196 |
+
// to this same host or may be an external CDN. In either case the image would be the same. So we let the Yahoo! Smush.it service use the image URL with
|
197 |
+
// the assumption the remote image and the local file are the same. Also with the assumption that the CDN service will somehow be updated when the image
|
198 |
+
// is changed.
|
199 |
+
if ( ( defined( 'WP_SMUSHIT_ENFORCE_SAME_URL' ) ) && ( WP_SMUSHIT_ENFORCE_SAME_URL == 'on' ) ) {
|
200 |
+
$home_url = str_replace( 'https://', 'http://', get_option( 'home' ) );
|
201 |
+
$error_message = "<b>Home URL: </b>" . $home_url . " <br />";
|
202 |
|
203 |
+
if ( stripos( $file_url, $home_url ) !== 0 ) {
|
204 |
+
return sprintf( __( "ERROR: <span class='code'>%s</span> must be within the website home URL (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN ),
|
205 |
+
htmlentities( $file_url ), $home_url );
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
|
|
|
|
|
|
|
|
208 |
|
209 |
+
//echo "calling _post(". $file_url .")<br />";
|
210 |
+
$data = $this->_post( $attachment_id, $file_url );
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
+
//echo "returned from _post data<pre>"; print_r($data); echo "</pre>";
|
|
|
|
|
213 |
|
214 |
+
if ( false === $data ) {
|
215 |
+
$error_count ++;
|
216 |
|
217 |
+
return __( 'ERROR: posting to Smush.it', WP_SMUSHIT_DOMAIN );
|
218 |
+
}
|
219 |
|
220 |
+
// make sure the response looks like JSON -- added 2008-12-19 when
|
221 |
+
// Smush.it was returning PHP warnings before the JSON output
|
222 |
+
if ( strpos( trim( $data ), '{' ) != 0 ) {
|
223 |
+
return __( 'Bad response from Smush.it', WP_SMUSHIT_DOMAIN );
|
224 |
+
}
|
225 |
+
$string_data = http_build_query( json_decode( $data, true ), '', '<br />' );
|
226 |
+
$string_data = urldecode( $string_data );
|
227 |
+
|
228 |
+
//Log data for debugging purpose
|
229 |
+
$error_message .= "<b>Return from API:</b> <br />$string_data";
|
230 |
+
if ( WP_SMUSHIT_DEBUG ) {
|
231 |
+
$log->error(
|
232 |
+
"do_smushit",
|
233 |
+
array(
|
234 |
+
"Attachment id" => "<b>Attachment id: </b>" . $attachment_id,
|
235 |
+
"File URL" => "<b>File URL: </b>" . $file_url,
|
236 |
+
"File Path" => "<b>File Path: </b>" . $file_path
|
237 |
+
),
|
238 |
+
$error_message );
|
239 |
+
}
|
240 |
+
// read the JSON response
|
241 |
+
if ( function_exists( 'json_decode' ) ) {
|
242 |
+
$data = json_decode( $data );
|
243 |
+
} else {
|
244 |
+
require_once( 'JSON/JSON.php' );
|
245 |
+
$json = new Services_JSON();
|
246 |
+
$data = $json->decode( $data );
|
247 |
+
}
|
248 |
|
249 |
+
//echo "returned from _post data<pre>"; print_r($data); echo "</pre>";
|
|
|
|
|
250 |
|
251 |
+
if ( ! isset( $data->dest_size ) ) {
|
252 |
+
return __( 'Bad response from Smush.it', WP_SMUSHIT_DOMAIN );
|
253 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
255 |
+
if ( - 1 === intval( $data->dest_size ) ) {
|
256 |
+
return __( 'No savings', WP_SMUSHIT_DOMAIN );
|
257 |
+
}
|
258 |
|
259 |
+
if ( ! isset( $data->dest ) ) {
|
260 |
+
$err = ( $data->error ? __( 'Smush.it error: ', WP_SMUSHIT_DOMAIN ) . $data->error : __( 'unknown error', WP_SMUSHIT_DOMAIN ) );
|
261 |
+
$err .= sprintf( __( " while processing <span class='code'>%s</span> (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN ), $file_url, $file_path );
|
|
|
|
|
262 |
|
263 |
+
return $err;
|
264 |
+
}
|
|
|
265 |
|
266 |
+
$processed_url = $data->dest;
|
|
|
|
|
|
|
267 |
|
268 |
+
// The smush.it web service does not append the domain;
|
269 |
+
// smushit.com web service does
|
270 |
+
if ( 0 !== stripos( $processed_url, 'http://' ) ) {
|
271 |
+
$processed_url = SMUSHIT_BASE_URL . $processed_url;
|
272 |
+
}
|
273 |
|
274 |
+
$temp_file = download_url( $processed_url );
|
|
|
|
|
|
|
275 |
|
276 |
+
if ( is_wp_error( $temp_file ) ) {
|
277 |
+
@unlink( $temp_file );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
|
279 |
+
return sprintf( __( "Error downloading file (%s)", WP_SMUSHIT_DOMAIN ), $temp_file->get_error_message() );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
}
|
281 |
|
282 |
+
if ( ! file_exists( $temp_file ) ) {
|
283 |
+
return sprintf( __( "Unable to locate Smushed file (%s)", WP_SMUSHIT_DOMAIN ), $temp_file );
|
|
|
284 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
|
286 |
+
@unlink( $file_path );
|
287 |
+
$success = @rename( $temp_file, $file_path );
|
288 |
+
if ( ! $success ) {
|
289 |
+
copy( $temp_file, $file_path );
|
290 |
+
unlink( $temp_file );
|
291 |
+
}
|
292 |
|
293 |
+
$savings = intval( $data->src_size ) - intval( $data->dest_size );
|
294 |
+
$savings_str = $this->format_bytes( $savings, 1 );
|
295 |
+
$savings_str = str_replace( ' ', ' ', $savings_str );
|
296 |
|
297 |
+
$results_msg = sprintf( __( "Reduced by %01.1f%% (%s)", WP_SMUSHIT_DOMAIN ),
|
298 |
+
$data->percent,
|
299 |
+
$savings_str );
|
300 |
|
301 |
+
return $results_msg;
|
|
|
|
|
|
|
302 |
}
|
303 |
|
304 |
+
function should_resmush( $previous_status ) {
|
305 |
+
if ( ! $previous_status || empty( $previous_status ) ) {
|
306 |
+
return true;
|
307 |
+
}
|
308 |
+
|
309 |
+
if ( stripos( $previous_status, 'no savings' ) !== false || stripos( $previous_status, 'reduced' ) !== false ) {
|
310 |
+
return false;
|
311 |
+
}
|
312 |
|
313 |
+
// otherwise an error
|
314 |
+
return true;
|
|
|
|
|
315 |
}
|
316 |
|
|
|
317 |
|
318 |
+
/**
|
319 |
+
* Read the image paths from an attachment's meta data and process each image
|
320 |
+
* with wp_smushit().
|
321 |
+
*
|
322 |
+
* This method also adds a `wp_smushit` meta key for use in the media library.
|
323 |
+
*
|
324 |
+
* Called after `wp_generate_attachment_metadata` is completed.
|
325 |
+
*/
|
326 |
+
function resize_from_meta_data( $meta, $ID = null, $force_resmush = true ) {
|
327 |
+
global $log;
|
328 |
+
if ( $ID && wp_attachment_is_image( $ID ) === false ) {
|
329 |
+
return $meta;
|
330 |
+
}
|
331 |
|
332 |
+
$attachment_file_path = get_attached_file( $ID );
|
333 |
+
$attachment_file_url = wp_get_attachment_url( $ID );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
|
335 |
+
if ( $force_resmush || $this->should_resmush( @$meta['wp_smushit'] ) ) {
|
336 |
+
$meta['wp_smushit'] = $this->do_smushit( $ID, $attachment_file_path, $attachment_file_url );
|
337 |
+
}
|
338 |
|
339 |
+
// no resized versions, so we can exit
|
340 |
+
if ( ! isset( $meta['sizes'] ) ) {
|
341 |
+
return $meta;
|
342 |
+
}
|
343 |
+
foreach ( $meta['sizes'] as $size_key => $size_data ) {
|
344 |
+
if ( ! $force_resmush && $this->should_resmush( @$meta['sizes'][ $size_key ]['wp_smushit'] ) === false ) {
|
345 |
+
continue;
|
346 |
+
}
|
347 |
|
348 |
+
// We take the original image. The 'sizes' will all match the same URL and
|
349 |
+
// path. So just get the dirname and rpelace the filename.
|
350 |
+
$attachment_file_path_size = trailingslashit( dirname( $attachment_file_path ) ) . $size_data['file'];
|
|
|
351 |
|
352 |
+
$attachment_file_url_size = trailingslashit( dirname( $attachment_file_url ) ) . $size_data['file'];
|
|
|
|
|
353 |
|
354 |
+
$meta['sizes'][ $size_key ]['wp_smushit'] = $this->do_smushit( $ID, $attachment_file_path_size, $attachment_file_url_size );
|
|
|
|
|
355 |
|
356 |
+
//echo "size_key[". $size_key ."] wp_smushit<pre>"; print_r($meta['sizes'][$size_key]['wp_smushit']); echo "</pre>";
|
357 |
+
}
|
358 |
|
359 |
+
//echo "meta<pre>"; print_r($meta); echo "</pre>";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
360 |
return $meta;
|
361 |
}
|
362 |
|
363 |
+
/**
|
364 |
+
* Post an image to Smush.it.
|
365 |
+
*
|
366 |
+
* @param string $file_url URL of the file to send to Smush.it
|
367 |
+
*
|
368 |
+
* @return string|boolean Returns the JSON response on success or else false
|
369 |
+
*/
|
370 |
+
function _post( $id, $file_url ) {
|
371 |
+
global $log;
|
372 |
+
$req = sprintf( SMUSHIT_REQ_URL, urlencode( $file_url ) );
|
373 |
+
|
374 |
+
$data = false;
|
375 |
+
if ( WP_SMUSHIT_DEBUG ) {
|
376 |
+
$debug = "<b>Calling API: </b>[" . $req . "]<br />";
|
377 |
+
$log->error( '_post', array(
|
378 |
+
' attachment_id' => "<b>Attachment id: </b>" . $id,
|
379 |
+
'file_url' => "<b>File URL: </b>" . $file_url
|
380 |
+
), $debug );
|
381 |
+
}
|
382 |
+
if ( function_exists( 'wp_remote_get' ) ) {
|
383 |
+
$response = wp_remote_get( $req, array(
|
384 |
+
'user-agent' => WP_SMUSHIT_UA,
|
385 |
+
'timeout' => WP_SMUSHIT_TIMEOUT
|
386 |
+
) );
|
387 |
+
if ( ! $response || is_wp_error( $response ) ) {
|
388 |
+
$data = false;
|
389 |
+
} else {
|
390 |
+
$data = wp_remote_retrieve_body( $response );
|
391 |
+
}
|
392 |
+
} else {
|
393 |
+
wp_die( __( 'WP Smush.it requires WordPress 2.8 or greater', WP_SMUSHIT_DOMAIN ) );
|
394 |
+
}
|
395 |
|
396 |
+
return $data;
|
|
|
397 |
}
|
398 |
|
|
|
|
|
|
|
399 |
|
400 |
+
/**
|
401 |
+
* Print column header for Smush.it results in the media library using
|
402 |
+
* the `manage_media_columns` hook.
|
403 |
+
*/
|
404 |
+
function columns( $defaults ) {
|
405 |
+
$defaults['smushit'] = 'Smush.it';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
406 |
|
407 |
+
return $defaults;
|
|
|
|
|
408 |
}
|
|
|
|
|
|
|
|
|
409 |
|
410 |
+
/**
|
411 |
+
* Return the filesize in a humanly readable format.
|
412 |
+
* Taken from http://www.php.net/manual/en/function.filesize.php#91477
|
413 |
+
*/
|
414 |
+
function format_bytes( $bytes, $precision = 2 ) {
|
415 |
+
$units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
|
416 |
+
$bytes = max( $bytes, 0 );
|
417 |
+
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
|
418 |
+
$pow = min( $pow, count( $units ) - 1 );
|
419 |
+
$bytes /= pow( 1024, $pow );
|
420 |
|
421 |
+
return round( $bytes, $precision ) . ' ' . $units[ $pow ];
|
|
|
|
|
422 |
}
|
423 |
+
|
424 |
+
/**
|
425 |
+
* Print column data for Smush.it results in the media library using
|
426 |
+
* the `manage_media_custom_column` hook.
|
427 |
+
*/
|
428 |
+
function custom_column( $column_name, $id ) {
|
429 |
+
if ( 'smushit' == $column_name ) {
|
430 |
+
$data = wp_get_attachment_metadata( $id );
|
431 |
+
if ( isset( $data['wp_smushit'] ) && ! empty( $data['wp_smushit'] ) ) {
|
432 |
+
print $data['wp_smushit'];
|
433 |
+
printf( "<br><a href=\"admin.php?action=wp_smushit_manual&attachment_ID=%d\">%s</a>",
|
434 |
+
$id,
|
435 |
+
__( 'Re-smush', WP_SMUSHIT_DOMAIN ) );
|
436 |
+
} else {
|
437 |
+
if ( wp_attachment_is_image( $id ) ) {
|
438 |
+
print __( 'Not processed', WP_SMUSHIT_DOMAIN );
|
439 |
+
printf( "<br><a href=\"admin.php?action=wp_smushit_manual&attachment_ID=%d\">%s</a>",
|
440 |
+
$id,
|
441 |
+
__( 'Smush.it now!', WP_SMUSHIT_DOMAIN ) );
|
442 |
+
}
|
443 |
+
}
|
444 |
}
|
|
|
|
|
445 |
}
|
446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
447 |
|
448 |
+
// Borrowed from http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
|
449 |
+
function add_bulk_actions_via_javascript() {
|
450 |
+
?>
|
451 |
+
<script type="text/javascript">
|
452 |
+
jQuery(document).ready(function ($) {
|
453 |
+
$('select[name^="action"] option:last-child').before('<option value="bulk_smushit">Bulk Smush.it</option>');
|
454 |
+
});
|
455 |
+
</script>
|
456 |
+
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
457 |
}
|
|
|
458 |
|
459 |
|
460 |
+
// Handles the bulk actions POST
|
461 |
+
// Borrowed from http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
|
462 |
+
function bulk_action_handler() {
|
463 |
+
check_admin_referer( 'bulk-media' );
|
|
|
|
|
|
|
|
|
464 |
|
465 |
+
if ( empty( $_REQUEST['media'] ) || ! is_array( $_REQUEST['media'] ) ) {
|
466 |
+
return;
|
467 |
+
}
|
468 |
|
469 |
+
$ids = implode( ',', array_map( 'intval', $_REQUEST['media'] ) );
|
|
|
|
|
|
|
470 |
|
471 |
+
// Can't use wp_nonce_url() as it escapes HTML entities
|
472 |
+
$url = admin_url( 'upload.php' );
|
473 |
+
$url = add_query_arg(
|
474 |
+
array(
|
475 |
+
'page' => 'wp-smushit-bulk',
|
476 |
+
'goback' => 1,
|
477 |
+
'ids' => $ids,
|
478 |
+
'_wpnonce' => wp_create_nonce( 'wp-smushit-bulk' )
|
479 |
+
),
|
480 |
+
$url
|
481 |
+
);
|
482 |
+
wp_redirect( $url );
|
483 |
+
exit();
|
484 |
+
}
|
485 |
|
486 |
+
// default is 6hrs
|
487 |
+
function temporarily_disable( $seconds = 21600 ) {
|
488 |
+
update_option( 'wp_smushit_smushit_auto', time() + $seconds );
|
489 |
+
}
|
490 |
|
|
|
|
|
|
|
491 |
}
|
|
|
|
|
492 |
|
493 |
+
$WpSmushit = new WpSmushit();
|
494 |
+
global $WpSmushit;
|
495 |
|
496 |
}
|
497 |
+
//Include Admin class
|
498 |
+
require_once( WP_SMUSHIT_DIR . '/lib/class-wp-smushit-bulk.php' );
|
499 |
+
require_once( WP_SMUSHIT_DIR . '/lib/class-wp-smushit-admin.php' );
|
500 |
+
/**
|
501 |
+
* Error Log
|
502 |
+
*/
|
503 |
+
require_once( WP_SMUSHIT_DIR . '/lib/error/class-wp-smushit-errorlog.php' );
|
504 |
+
require_once( WP_SMUSHIT_DIR . '/lib/error/class-wp-smushit-errorregistry.php' );
|
505 |
+
|
506 |
+
if ( ! function_exists( 'wp_basename' ) ) {
|
507 |
+
/**
|
508 |
+
* Introduced in WP 3.1... this is copied verbatim from wp-includes/formatting.php.
|
509 |
+
*/
|
510 |
function wp_basename( $path, $suffix = '' ) {
|
511 |
return urldecode( basename( str_replace( '%2F', '/', urlencode( $path ) ), $suffix ) );
|
512 |
}
|