helpers.js
8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* global WP_Smush */
/* global ajaxurl */
/* global wp_smush_msgs */
/**
* Helpers functions.
*
* @since 2.9.0 Moved from admin.js
*/
( function() {
'use strict';
WP_Smush.helpers = {
init: () => {},
cacheUpsellErrorCodes: [],
/**
* Convert bytes to human-readable form.
*
* @param {number} a Bytes
* @param {number} b Number of digits
* @return {*} Formatted Bytes
*/
formatBytes: ( a, b ) => {
const thresh = 1024,
units = [ 'KB', 'MB', 'GB', 'TB', 'PB' ];
if ( Math.abs( a ) < thresh ) {
return a + ' B';
}
let u = -1;
do {
a /= thresh;
++u;
} while ( Math.abs( a ) >= thresh && u < units.length - 1 );
return a.toFixed( b ) + ' ' + units[ u ];
},
/**
* Get size from a string.
*
* @param {string} formattedSize Formatter string
* @return {*} Formatted Bytes
*/
getSizeFromString: ( formattedSize ) => {
return formattedSize.replace( /[a-zA-Z]/g, '' ).trim();
},
/**
* Get type from formatted string.
*
* @param {string} formattedSize Formatted string
* @return {*} Formatted Bytes
*/
getFormatFromString: ( formattedSize ) => {
return formattedSize.replace( /[0-9.]/g, '' ).trim();
},
/**
* Stackoverflow: http://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript
*
* @param {number} num
* @param {number} decimals
* @return {number} Number
*/
precise_round: ( num, decimals ) => {
const sign = num >= 0 ? 1 : -1;
// Keep the percentage below 100.
num = num > 100 ? 100 : num;
return (
Math.round( num * Math.pow( 10, decimals ) + sign * 0.001 ) /
Math.pow( 10, decimals )
);
},
/**
* Displays a floating error message using the #wp-smush-ajax-notice container.
*
* @since 3.8.0
*
* @param {string} message
*/
showErrorNotice: ( message ) => {
if ( 'undefined' === typeof message ) {
return;
}
const noticeMessage = `<p>${ message }</p>`,
noticeOptions = {
type: 'error',
icon: 'info',
};
SUI.openNotice( 'wp-smush-ajax-notice', noticeMessage, noticeOptions );
const loadingButton = document.querySelector( '.sui-button-onload' );
if ( loadingButton ) {
loadingButton.classList.remove( 'sui-button-onload' );
}
},
/**
* Reset settings.
*
* @since 3.2.0
*/
resetSettings: () => {
const _nonce = document.getElementById( 'wp_smush_reset' );
const xhr = new XMLHttpRequest();
xhr.open( 'POST', ajaxurl + '?action=reset_settings', true );
xhr.setRequestHeader(
'Content-type',
'application/x-www-form-urlencoded'
);
xhr.onload = () => {
if ( 200 === xhr.status ) {
const res = JSON.parse( xhr.response );
if ( 'undefined' !== typeof res.success && res.success ) {
window.location.href = wp_smush_msgs.smush_url;
}
} else {
window.console.log(
'Request failed. Returned status of ' + xhr.status
);
}
};
xhr.send( '_ajax_nonce=' + _nonce.value );
},
/**
* Prepare error row. Will only allow to hide errors for WP media attachments (not nextgen).
*
* @since 1.9.0
* @since 3.12.0 Moved from Smush.
*
* @param {string} errorMsg Error message.
* @param {string} fileName File name.
* @param {string} thumbnail Thumbnail for image (if available).
* @param {number} id Image ID.
* @param {string} type Smush type: media or netxgen.
* @param {string} errorCode Error code.
*
* @return {string} Row with error.
*/
prepareBulkSmushErrorRow: (errorMsg, fileName, thumbnail, id, type, errorCode) => {
const thumbDiv =
thumbnail && 'undefined' !== typeof thumbnail ?
`<img class="attachment-thumbnail" src="${thumbnail}" />` :
'<i class="sui-icon-photo-picture" aria-hidden="true"></i>';
const editLink = window.wp_smush_msgs.edit_link.replace('{{id}}', id);
fileName =
'undefined' === fileName || 'undefined' === typeof fileName ?
'undefined' :
fileName;
let tableDiv =
`<div class="smush-bulk-error-row" data-error-code="${errorCode}">
<div class="smush-bulk-image-data">
<div class="smush-bulk-image-title">
${ thumbDiv }
<span class="smush-image-name">
<a href="${editLink}">${fileName}</a>
</span>
</div>
<div class="smush-image-error">
${errorMsg}
</div>
</div>`;
if ('media' === type) {
tableDiv +=
`<div class="smush-bulk-image-actions">
<a href="javascript:void(0)" class="sui-tooltip sui-tooltip-constrained sui-tooltip-left smush-ignore-image" data-tooltip="${window.wp_smush_msgs.error_ignore}" data-id="${id}">
${window.wp_smush_msgs.btn_ignore}
</a>
<a class="smush-link-detail" href="${editLink}">
${window.wp_smush_msgs.view_detail}
</a>
</div>`;
}
tableDiv += '</div>';
tableDiv += WP_Smush.helpers.upsellWithError( errorCode );
return tableDiv;
},
cacheUpsellErrorCode( errorCode ) {
this.cacheUpsellErrorCodes.push( errorCode );
},
/**
* Get upsell base on error code.
* @param {string} errorCode Error code.
* @returns {string}
*/
upsellWithError(errorCode) {
if (
!errorCode
|| !window.wp_smush_msgs['error_' + errorCode]
|| this.isUpsellRendered( errorCode )
) {
return '';
}
this.cacheRenderedUpsell( errorCode );
return '<div class="smush-bulk-error-row smush-error-upsell">' +
'<div class="smush-bulk-image-title">' +
'<span class="smush-image-error">' +
window.wp_smush_msgs['error_' + errorCode] +
'</span>' +
'</div></div>';
},
// Do not use arrow function to use `this`.
isUpsellRendered( errorCode ) {
return this.cacheUpsellErrorCodes.includes( errorCode );
},
// Do not use arrow function to use `this`.
cacheRenderedUpsell( errorCode ) {
this.cacheUpsellErrorCodes.push( errorCode );
},
/**
* Get error message from Ajax response or Error.
* @param {Object} resp
*/
getErrorMessage: ( resp ) => {
return resp.message || resp.data && resp.data.message ||
resp.responseJSON && resp.responseJSON.data && resp.responseJSON.data.message ||
window.wp_smush_msgs.generic_ajax_error ||
resp.status && 'Request failed. Returned status of ' + resp.status
},
/**
* Displays a floating message from response,
* using the #wp-smush-ajax-notice container.
*
* @param {Object|string} notice
* @param {Object} noticeOptions
*/
showNotice: function( notice, noticeOptions ) {
let message;
if ( 'object' === typeof notice ) {
message = this.getErrorMessage( notice );
} else {
message = notice;
}
if ( ! message ) {
return;
}
noticeOptions = noticeOptions || {};
noticeOptions = Object.assign({
showdismiss: false,
autoclose: true,
},noticeOptions);
noticeOptions = {
type: noticeOptions.type || 'error',
icon: noticeOptions.icon || ( 'success' === noticeOptions.type ? 'check-tick' : 'info' ),
dismiss: {
show: noticeOptions.showdismiss,
label: window.wp_smush_msgs.noticeDismiss,
tooltip: window.wp_smush_msgs.noticeDismissTooltip,
},
autoclose: {
show: noticeOptions.autoclose
}
};
const noticeMessage = `<p>${ message }</p>`;
SUI.openNotice( 'wp-smush-ajax-notice', noticeMessage, noticeOptions );
return Promise.resolve( '#wp-smush-ajax-notice' );
},
closeNotice() {
window.SUI.closeNotice( 'wp-smush-ajax-notice' );
},
renderActivationCDNNotice: function( noticeMessage ) {
const animatedNotice = document.getElementById('wp-smush-animated-upsell-notice');
if ( animatedNotice ) {
return;
}
const upsellHtml = `<div class="sui-notice sui-notice-info sui-margin-top" id="wp-smush-animated-upsell-notice">
<div class="sui-notice-content">
<div class="sui-notice-message">
<i class="sui-notice-icon sui-icon-info" aria-hidden="true"></i>
<p>${noticeMessage}</p>
</div>
</div>
</div>`;
document.querySelector( '#smush-box-bulk .wp-smush-bulk-wrapper' ).outerHTML += upsellHtml;
}
};
WP_Smush.helpers.init();
}() );