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/clone/wp-conte.../plugins/wp-smush.../_src/js/common
File: media-library-scanner.js
/* global WP_Smush */
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Abstract Media Library Scanner.
[3] Fix | Delete
*
[4] Fix | Delete
*/
[5] Fix | Delete
import Fetcher from '../utils/fetcher';
[6] Fix | Delete
import { scanProgressBar } from './progressbar';
[7] Fix | Delete
import { GlobalStats } from './globalStats';
[8] Fix | Delete
import loopbackTester from '../loopback-tester';
[9] Fix | Delete
const { __ } = wp.i18n;
[10] Fix | Delete
export default class MediaLibraryScanner {
[11] Fix | Delete
constructor() {
[12] Fix | Delete
this.autoSyncDuration = 1500;
[13] Fix | Delete
this.progressTimeoutId = 0;
[14] Fix | Delete
this.scanProgress = scanProgressBar( this.autoSyncDuration );
[15] Fix | Delete
}
[16] Fix | Delete
[17] Fix | Delete
startScan( optimizeOnScanCompleted = false ) {
[18] Fix | Delete
this.onStart();
[19] Fix | Delete
const processType = optimizeOnScanCompleted ? 'smush' : 'scan';
[20] Fix | Delete
loopbackTester.performTest().then( ( res ) => {
[21] Fix | Delete
const isLoopbackHealthy = res?.loopback;
[22] Fix | Delete
if ( isLoopbackHealthy ) {
[23] Fix | Delete
Fetcher.scanMediaLibrary.start( optimizeOnScanCompleted ).then( ( response ) => {
[24] Fix | Delete
if ( ! response?.success ) {
[25] Fix | Delete
this.showFailureNotice( response );
[26] Fix | Delete
this.onStartFailure( response );
[27] Fix | Delete
return;
[28] Fix | Delete
}
[29] Fix | Delete
this.showProgressBar().autoSyncStatus();
[30] Fix | Delete
} );
[31] Fix | Delete
} else {
[32] Fix | Delete
this.showLoopbackErrorModal( processType );
[33] Fix | Delete
this.onStartFailure( res );
[34] Fix | Delete
}
[35] Fix | Delete
} ).catch( ( error ) => {
[36] Fix | Delete
console.error( 'Error:', error );
[37] Fix | Delete
this.showLoopbackErrorModal( processType );
[38] Fix | Delete
this.onStartFailure( error );
[39] Fix | Delete
} );
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
onStart() {
[43] Fix | Delete
// Do nothing at the moment.
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
onStartFailure( response ) {
[47] Fix | Delete
// Do nothing at the moment.
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
showFailureNotice( response ) {
[51] Fix | Delete
WP_Smush.helpers.showNotice( response, {
[52] Fix | Delete
showdismiss: true,
[53] Fix | Delete
autoclose: false,
[54] Fix | Delete
} );
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
showLoopbackErrorModal( processType ) {
[58] Fix | Delete
const loopbackErrorModal = document.getElementById( 'smush-loopback-error-dialog' );
[59] Fix | Delete
if ( ! loopbackErrorModal || ! window.SUI ) {
[60] Fix | Delete
return;
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
// Cache current process type.
[64] Fix | Delete
loopbackErrorModal.dataset.processType = processType || 'scan';
[65] Fix | Delete
[66] Fix | Delete
WP_Smush.helpers.showModal( loopbackErrorModal.id );
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
showProgressBar() {
[70] Fix | Delete
this.onShowProgressBar();
[71] Fix | Delete
this.scanProgress.reset().setOnCancelCallback( this.showStopScanningModal.bind( this ) ).open();
[72] Fix | Delete
return this;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
onShowProgressBar() {
[76] Fix | Delete
// Do nothing at the moment.
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
showStopScanningModal() {
[80] Fix | Delete
if ( ! window.SUI ) {
[81] Fix | Delete
return;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
this.onShowStopScanningModal();
[85] Fix | Delete
[86] Fix | Delete
window.SUI.openModal(
[87] Fix | Delete
'smush-stop-scanning-dialog',
[88] Fix | Delete
'wpbody-content',
[89] Fix | Delete
undefined,
[90] Fix | Delete
false
[91] Fix | Delete
);
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
onShowStopScanningModal() {
[95] Fix | Delete
this.registerCancelProcessEvent();
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
registerCancelProcessEvent() {
[99] Fix | Delete
const stopScanButton = document.querySelector( '.smush-stop-scanning-dialog-button' );
[100] Fix | Delete
if ( ! stopScanButton ) {
[101] Fix | Delete
return;
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
stopScanButton.addEventListener( 'click', this.cancelProgress.bind( this ), { once: true } );
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
closeStopScanningModal() {
[108] Fix | Delete
if ( ! window.SUI ) {
[109] Fix | Delete
return;
[110] Fix | Delete
}
[111] Fix | Delete
const stopScanningElement = document.querySelector( '#smush-stop-scanning-dialog' );
[112] Fix | Delete
const isModalClosed = ( ! stopScanningElement ) || ! stopScanningElement.classList.contains( 'sui-content-fade-in' );
[113] Fix | Delete
if ( isModalClosed ) {
[114] Fix | Delete
return;
[115] Fix | Delete
}
[116] Fix | Delete
window.SUI.closeModal( 'smush-stop-scanning-dialog' );
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
closeProgressBar() {
[120] Fix | Delete
this.onCloseProgressBar();
[121] Fix | Delete
this.scanProgress.close();
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
onCloseProgressBar() {
[125] Fix | Delete
// Do nothing at the moment.
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
updateProgress( stats ) {
[129] Fix | Delete
const totalItems = this.getTotalItems( stats );
[130] Fix | Delete
const processedItems = this.getProcessedItems( stats );
[131] Fix | Delete
[132] Fix | Delete
return this.scanProgress.update( processedItems, totalItems );
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
getProcessedItems( stats ) {
[136] Fix | Delete
return stats?.processed_items || 0;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
getTotalItems( stats ) {
[140] Fix | Delete
return stats?.total_items || 0;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
cancelProgress() {
[144] Fix | Delete
this.scanProgress.setCancelButtonOnCancelling();
[145] Fix | Delete
return Fetcher.scanMediaLibrary.cancel().then( ( response ) => {
[146] Fix | Delete
if ( ! response?.success ) {
[147] Fix | Delete
this.onCancelFailure( response );
[148] Fix | Delete
return;
[149] Fix | Delete
}
[150] Fix | Delete
this.onCancelled( response.data );
[151] Fix | Delete
} );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
onCancelFailure( response ) {
[155] Fix | Delete
WP_Smush.helpers.showNotice( response, {
[156] Fix | Delete
showdismiss: true,
[157] Fix | Delete
autoclose: false,
[158] Fix | Delete
} );
[159] Fix | Delete
this.scanProgress.resetCancelButtonOnFailure();
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
onDead( stats ) {
[163] Fix | Delete
this.clearProgressTimeout();
[164] Fix | Delete
this.closeProgressBar();
[165] Fix | Delete
this.closeStopScanningModal();
[166] Fix | Delete
this.showRetryScanModal();
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
showRetryScanModal() {
[170] Fix | Delete
const retryScanModalElement = document.getElementById( 'smush-retry-scan-notice' );
[171] Fix | Delete
if ( ! window.SUI || ! retryScanModalElement ) {
[172] Fix | Delete
return;
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
retryScanModalElement.querySelector( '.smush-retry-scan-notice-button' ).addEventListener( 'click', ( e ) => {
[176] Fix | Delete
window.SUI.closeModal( 'smush-retry-scan-notice' );
[177] Fix | Delete
const recheckImagesBtn = document.querySelector( '.wp-smush-scan' );
[178] Fix | Delete
if ( ! recheckImagesBtn ) {
[179] Fix | Delete
return;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
e.preventDefault();
[183] Fix | Delete
recheckImagesBtn.click();
[184] Fix | Delete
}, { once: true } );
[185] Fix | Delete
[186] Fix | Delete
window.SUI.openModal(
[187] Fix | Delete
'smush-retry-scan-notice',
[188] Fix | Delete
'wpbody-content',
[189] Fix | Delete
undefined,
[190] Fix | Delete
false
[191] Fix | Delete
);
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
onCompleted( stats ) {
[195] Fix | Delete
this.onFinish( stats );
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
onCancelled( stats ) {
[199] Fix | Delete
this.onFinish( stats );
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
onFinish( stats ) {
[203] Fix | Delete
this.clearProgressTimeout();
[204] Fix | Delete
const globalStats = stats?.global_stats;
[205] Fix | Delete
this.updateGlobalStatsAndBulkContent( globalStats );
[206] Fix | Delete
this.closeProgressBar();
[207] Fix | Delete
this.closeStopScanningModal();
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
clearProgressTimeout() {
[211] Fix | Delete
if ( this.progressTimeoutId ) {
[212] Fix | Delete
clearTimeout( this.progressTimeoutId );
[213] Fix | Delete
}
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
updateGlobalStatsAndBulkContent( globalStats ) {
[217] Fix | Delete
if ( ! globalStats ) {
[218] Fix | Delete
return;
[219] Fix | Delete
}
[220] Fix | Delete
GlobalStats.updateGlobalStatsFromSmushScriptData( globalStats );
[221] Fix | Delete
GlobalStats.renderStats();
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
getStatus() {
[225] Fix | Delete
return Fetcher.scanMediaLibrary.getScanStatus();
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
autoSyncStatus() {
[229] Fix | Delete
const startTime = new Date().getTime();
[230] Fix | Delete
this.getStatus().then( ( response ) => {
[231] Fix | Delete
if ( ! response?.success ) {
[232] Fix | Delete
return;
[233] Fix | Delete
}
[234] Fix | Delete
const stats = response.data;
[235] Fix | Delete
[236] Fix | Delete
if ( stats.is_dead ) {
[237] Fix | Delete
this.onDead( response.data );
[238] Fix | Delete
return;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
this.beforeUpdateStatus( stats );
[242] Fix | Delete
[243] Fix | Delete
this.updateProgress( stats ).then( () => {
[244] Fix | Delete
this.scanProgress.increaseDurationToHaveChangeOnProgress( new Date().getTime() - startTime );
[245] Fix | Delete
[246] Fix | Delete
const isCompleted = stats?.is_completed;
[247] Fix | Delete
if ( isCompleted ) {
[248] Fix | Delete
this.onCompleted( stats );
[249] Fix | Delete
return;
[250] Fix | Delete
}
[251] Fix | Delete
const isCancelled = stats?.is_cancelled;
[252] Fix | Delete
if ( isCancelled ) {
[253] Fix | Delete
this.onCancelled( stats );
[254] Fix | Delete
return;
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
this.progressTimeoutId = setTimeout( () => this.autoSyncStatus(), this.autoSyncDuration );
[258] Fix | Delete
} );
[259] Fix | Delete
} );
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
beforeUpdateStatus() {
[263] Fix | Delete
// Do nothing at the moment.
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
setInnerText( element, newText ) {
[267] Fix | Delete
if ( ! element ) {
[268] Fix | Delete
return;
[269] Fix | Delete
}
[270] Fix | Delete
element.dataset.originalText = element.dataset.originalText || element.innerText.trim();
[271] Fix | Delete
element.innerText = newText;
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
revertInnerText( element ) {
[275] Fix | Delete
if ( ! element || ! element.dataset.originalText ) {
[276] Fix | Delete
return;
[277] Fix | Delete
}
[278] Fix | Delete
element.innerText = element.dataset.originalText.trim();
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
hideAnElement( element ) {
[282] Fix | Delete
if ( element ) {
[283] Fix | Delete
element.classList.add( 'sui-hidden' );
[284] Fix | Delete
}
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
showAnElement( element ) {
[288] Fix | Delete
if ( element ) {
[289] Fix | Delete
element.classList.remove( 'sui-hidden' );
[290] Fix | Delete
}
[291] Fix | Delete
}
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function