Edit File by line

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