← All changes
|
assets/src/apps/js/admin/pages/tools/database/upgrade.js
+300
-300
4.3.9
→
4.4.9
View file →
| @@ -1,300 +1,300 @@ | ||
| 1 | -import lpModalOverlay from '../../../../utils/lp-modal-overlay'; | |
| 2 | -import handleAjax from '../../../../utils/handle-ajax-api'; | |
| 3 | - | |
| 4 | -const $ = jQuery; | |
| 5 | - | |
| 6 | -const elToolUpgradeDB = $( '#lp-tool-upgrade-db' ); | |
| 7 | - | |
| 8 | -const upgradeDB = () => { | |
| 9 | - let isUpgrading = 0; | |
| 10 | - const elWrapperTermsUpgrade = elToolUpgradeDB.find( '.wrapper-terms-upgrade' ); | |
| 11 | - const elStatusUpgrade = elToolUpgradeDB.find( '.wrapper-lp-status-upgrade' ); | |
| 12 | - const elWrapperUpgradeMessage = elToolUpgradeDB.find( '.wrapper-lp-upgrade-message' ); | |
| 13 | - let checkValidBeforeUpgrade = null; | |
| 14 | - const elMessageUpgrading = $( 'input[name=message-when-upgrading]' ).val(); | |
| 15 | - | |
| 16 | - if ( elWrapperTermsUpgrade.length ) { // Show Terms Upgrade. | |
| 17 | - lpModalOverlay.setContentModal( elWrapperTermsUpgrade.html() ); | |
| 18 | - | |
| 19 | - const elTermUpdate = lpModalOverlay.elLPOverlay.find( '.terms-upgrade' ); | |
| 20 | - const elLPAgreeTerm = elTermUpdate.find( 'input[name=lp-agree-term]' ); | |
| 21 | - const elTermMessage = elTermUpdate.find( '.error' ); | |
| 22 | - | |
| 23 | - checkValidBeforeUpgrade = function() { | |
| 24 | - elTermMessage.hide(); | |
| 25 | - elTermMessage.removeClass( 'learn-press-message' ); | |
| 26 | - | |
| 27 | - if ( elLPAgreeTerm.is( ':checked' ) ) { | |
| 28 | - handleAjax( '/lp/v1/database/agree_terms', { agree_terms: 1 }, {} ); | |
| 29 | - | |
| 30 | - lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); | |
| 31 | - lpModalOverlay.setContentModal( elStatusUpgrade.html() ); | |
| 32 | - | |
| 33 | - return true; | |
| 34 | - } | |
| 35 | - | |
| 36 | - elTermMessage.show(); | |
| 37 | - elTermMessage.addClass( 'learn-press-message' ); | |
| 38 | - lpModalOverlay.elMainContent.animate( { | |
| 39 | - scrollTop: elTermMessage.offset().top, | |
| 40 | - } ); | |
| 41 | - | |
| 42 | - return false; | |
| 43 | - }; | |
| 44 | - } else { // Show Steps Upgrade. | |
| 45 | - lpModalOverlay.setContentModal( elStatusUpgrade.html() ); | |
| 46 | - checkValidBeforeUpgrade = function() { | |
| 47 | - return true; | |
| 48 | - }; | |
| 49 | - } | |
| 50 | - | |
| 51 | - lpModalOverlay.setTitleModal( elToolUpgradeDB.find( 'h2' ).html() ); | |
| 52 | - lpModalOverlay.elBtnYes.text( 'Upgrade' ); | |
| 53 | - lpModalOverlay.elBtnYes.show(); | |
| 54 | - lpModalOverlay.elBtnNo.text( 'Cancel' ); | |
| 55 | - lpModalOverlay.callBackYes = function( e ) { | |
| 56 | - if ( ! checkValidBeforeUpgrade() ) { | |
| 57 | - return; | |
| 58 | - } | |
| 59 | - | |
| 60 | - const target = e.target; | |
| 61 | - // Show message note when upgrading. | |
| 62 | - if ( target.innerText === 'Upgrade' ) { | |
| 63 | - lpModalOverlay.elFooter.prepend( '<span class="learn-press-notice">' + elMessageUpgrading + '</span>' ); | |
| 64 | - } | |
| 65 | - | |
| 66 | - isUpgrading = 1; | |
| 67 | - | |
| 68 | - lpModalOverlay.elBtnYes.hide(); | |
| 69 | - lpModalOverlay.elBtnNo.hide(); | |
| 70 | - | |
| 71 | - const urlHandle = '/lp/v1/database/upgrade'; | |
| 72 | - const elGroupStep = lpModalOverlay.elLPOverlay.find( '.lp-group-step' ); | |
| 73 | - const elItemSteps = elToolUpgradeDB.find( '.lp-item-step' ); | |
| 74 | - | |
| 75 | - // Get params. | |
| 76 | - const steps = []; | |
| 77 | - | |
| 78 | - $.each( elItemSteps, function( i, el ) { | |
| 79 | - const elItemStepsTmp = $( el ); | |
| 80 | - | |
| 81 | - if ( ! elItemStepsTmp.hasClass( 'completed' ) ) { | |
| 82 | - const step = elItemStepsTmp.find( 'input' ).val(); | |
| 83 | - steps.push( step ); | |
| 84 | - } | |
| 85 | - } ); | |
| 86 | - | |
| 87 | - const params = { | |
| 88 | - steps, | |
| 89 | - step: steps[ 0 ], | |
| 90 | - }; | |
| 91 | - | |
| 92 | - let elItemStepCurrent = null; | |
| 93 | - | |
| 94 | - // Show progress when upgrading. | |
| 95 | - const showProgress = ( stepCurrent, percent ) => { | |
| 96 | - elItemStepCurrent = elGroupStep.find( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); | |
| 97 | - elItemStepCurrent.addClass( 'running' ); | |
| 98 | - | |
| 99 | - if ( 100 === percent ) { | |
| 100 | - elItemStepCurrent.removeClass( 'running' ).addClass( 'completed' ); | |
| 101 | - } | |
| 102 | - | |
| 103 | - elItemStepCurrent.find( '.progress-bar' ).css( 'width', percent + '%' ); | |
| 104 | - elItemStepCurrent.find( '.percent' ).text( percent + '%' ); | |
| 105 | - }; | |
| 106 | - | |
| 107 | - // Scroll to step current. | |
| 108 | - const scrollToStepCurrent = ( stepCurrent ) => { | |
| 109 | - elItemStepCurrent = elGroupStep.find( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); | |
| 110 | - | |
| 111 | - if ( ! elItemStepCurrent.length ) { | |
| 112 | - return; | |
| 113 | - } | |
| 114 | - | |
| 115 | - const offset = elItemStepCurrent.offset().top - lpModalOverlay.elMainContent.offset().top + | |
| 116 | - lpModalOverlay.elMainContent.scrollTop(); | |
| 117 | - | |
| 118 | - lpModalOverlay.elMainContent.stop().animate( { | |
| 119 | - scrollTop: offset, | |
| 120 | - }, 600 ); | |
| 121 | - }; | |
| 122 | - | |
| 123 | - showProgress( steps[ 0 ], 0.1 ); | |
| 124 | - | |
| 125 | - const funcCallBack = { | |
| 126 | - success: ( res ) => { | |
| 127 | - showProgress( params.step, res.percent ); | |
| 128 | - | |
| 129 | - if ( params.step !== res.name ) { | |
| 130 | - showProgress( res.name, 0.1 ); | |
| 131 | - } | |
| 132 | - | |
| 133 | - scrollToStepCurrent( params.step ); | |
| 134 | - | |
| 135 | - if ( 'success' === res.status ) { | |
| 136 | - params.step = res.name; | |
| 137 | - params.data = res.data; | |
| 138 | - | |
| 139 | - setTimeout( () => { | |
| 140 | - handleAjax( urlHandle, params, funcCallBack ); | |
| 141 | - }, 800 ); | |
| 142 | - } else if ( 'finished' === res.status ) { | |
| 143 | - isUpgrading = 0; | |
| 144 | - elItemStepCurrent.removeClass( 'running' ).addClass( 'completed' ); | |
| 145 | - setTimeout( () => { | |
| 146 | - lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html() ); | |
| 147 | - }, 1000 ); | |
| 148 | - lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); | |
| 149 | - lpModalOverlay.elBtnNo.text( 'Close' ); | |
| 150 | - lpModalOverlay.elBtnNo.show(); | |
| 151 | - lpModalOverlay.elBtnNo.on( 'click', () => { | |
| 152 | - window.location.reload(); | |
| 153 | - } ); | |
| 154 | - } else { | |
| 155 | - isUpgrading = 0; | |
| 156 | - lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); | |
| 157 | - elItemStepCurrent.removeClass( 'running' ).addClass( 'error' ); | |
| 158 | - lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html(), function() { | |
| 159 | - lpModalOverlay.elBtnYes.text( 'Retry' ).show(); | |
| 160 | - lpModalOverlay.callBackYes = () => { | |
| 161 | - window.location.href = lpGlobalSettings.siteurl + '/wp-admin/admin.php?page=learn-press-tools&tab=database&action=upgrade-db'; | |
| 162 | - }; | |
| 163 | - lpModalOverlay.elBtnNo.show(); | |
| 164 | - | |
| 165 | - if ( ! res.message ) { | |
| 166 | - res.message = 'Upgrade not success! Please clear cache, restart sever then retry or contact to LP to help'; | |
| 167 | - } | |
| 168 | - | |
| 169 | - lpModalOverlay.elMainContent.find( '.learn-press-message' ).addClass( 'error' ).html( res.message ); | |
| 170 | - } ); | |
| 171 | - } | |
| 172 | - }, | |
| 173 | - error: ( err ) => { | |
| 174 | - isUpgrading = 0; | |
| 175 | - lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html(), function() { | |
| 176 | - lpModalOverlay.elBtnYes.text( 'Retry' ).show(); | |
| 177 | - lpModalOverlay.callBackYes = () => { | |
| 178 | - window.location.location = 'wp-admin/admin.php?page=learn-press-tools&tab=database&action=upgrade-db'; | |
| 179 | - }; | |
| 180 | - lpModalOverlay.elBtnNo.show(); | |
| 181 | - | |
| 182 | - if ( ! err.message ) { | |
| 183 | - err.message = 'Upgrade not success! Something wrong. Please clear cache, restart sever then retry or contact to LP to help'; | |
| 184 | - } | |
| 185 | - | |
| 186 | - lpModalOverlay.elMainContent.find( '.learn-press-message' ).addClass( 'error' ).html( err.message ); | |
| 187 | - } ); | |
| 188 | - }, | |
| 189 | - completed: () => { | |
| 190 | - | |
| 191 | - }, | |
| 192 | - }; | |
| 193 | - | |
| 194 | - handleAjax( urlHandle, params, funcCallBack ); | |
| 195 | - }; | |
| 196 | - | |
| 197 | - // Show confirm if, within upgrading, the user reload the page. | |
| 198 | - window.onbeforeunload = function() { | |
| 199 | - if ( isUpgrading ) { | |
| 200 | - return 'LP is upgrading Database. Are you want to reload page?'; | |
| 201 | - } | |
| 202 | - }; | |
| 203 | - | |
| 204 | - // Show confirm if, within upgrading, the user close the page. | |
| 205 | - window.onclose = function() { | |
| 206 | - if ( isUpgrading ) { | |
| 207 | - return 'LP is upgrading Database. Are you want to close page?'; | |
| 208 | - } | |
| 209 | - }; | |
| 210 | -}; | |
| 211 | - | |
| 212 | -const getStepsUpgradeStatus = () => { | |
| 213 | - if ( ! elToolUpgradeDB.length ) { | |
| 214 | - return; | |
| 215 | - } | |
| 216 | - | |
| 217 | - // initial LP Modal Overlay | |
| 218 | - if ( ! lpModalOverlay.init() ) { | |
| 219 | - return; | |
| 220 | - } | |
| 221 | - | |
| 222 | - const elWrapperStatusUpgrade = $( '.wrapper-lp-status-upgrade' ); | |
| 223 | - const urlHandle = '/lp/v1/database/get_steps'; | |
| 224 | - | |
| 225 | - // Show dialog upgrade database. | |
| 226 | - const queryString = window.location.search; | |
| 227 | - const urlParams = new URLSearchParams( queryString ); | |
| 228 | - const action = urlParams.get( 'action' ); | |
| 229 | - | |
| 230 | - if ( 'upgrade-db' === action ) { | |
| 231 | - lpModalOverlay.elLPOverlay.show(); | |
| 232 | - lpModalOverlay.setTitleModal( elToolUpgradeDB.find( 'h2' ).html() ); | |
| 233 | - lpModalOverlay.setContentModal( $( '.wrapper-lp-loading' ).html() ); | |
| 234 | - } | |
| 235 | - | |
| 236 | - const funcCallBack = { | |
| 237 | - success: ( res ) => { | |
| 238 | - const { steps_completed, steps_default } = res; | |
| 239 | - | |
| 240 | - if ( undefined === steps_completed || undefined === steps_default ) { | |
| 241 | - console.log( 'invalid steps_completed and steps_default' ); | |
| 242 | - return false; | |
| 243 | - } | |
| 244 | - | |
| 245 | - // Render show Steps. | |
| 246 | - let htmlStep = ''; | |
| 247 | - for ( const k_gr_steps in steps_default ) { | |
| 248 | - const step_group = steps_default[ k_gr_steps ]; | |
| 249 | - const steps = step_group.steps; | |
| 250 | - | |
| 251 | - htmlStep = '<div class="lp-group-step">'; | |
| 252 | - htmlStep += '<h3>' + step_group.label + '</h3>'; | |
| 253 | - | |
| 254 | - for ( const k_step in steps ) { | |
| 255 | - const step = steps[ k_step ]; | |
| 256 | - let completed = ''; | |
| 257 | - | |
| 258 | - if ( undefined !== steps_completed[ k_step ] ) { | |
| 259 | - completed = 'completed'; | |
| 260 | - } | |
| 261 | - | |
| 262 | - htmlStep += '<div class="lp-item-step ' + completed + '">'; | |
| 263 | - htmlStep += '<div class="lp-item-step-left"><input type="hidden" name="lp_steps_upgrade_db[]" value="' + step.name + '" /></div>'; | |
| 264 | - htmlStep += '<div class="lp-item-step-right">'; | |
| 265 | - htmlStep += '<label for=""><strong></strong>' + step.label + '</label>'; | |
| 266 | - htmlStep += '<div class="description">' + step.description + '</div>'; | |
| 267 | - htmlStep += '<div class="percent"></div>'; | |
| 268 | - htmlStep += '<span class="progress-bar"></span>'; | |
| 269 | - htmlStep += '</div>'; | |
| 270 | - htmlStep += '</div>'; | |
| 271 | - } | |
| 272 | - | |
| 273 | - htmlStep += '</div>'; | |
| 274 | - | |
| 275 | - elWrapperStatusUpgrade.append( htmlStep ); | |
| 276 | - | |
| 277 | - const elBtnUpgradeDB = $( '.lp-btn-upgrade-db' ); | |
| 278 | - | |
| 279 | - if ( 'upgrade-db' === action ) { | |
| 280 | - upgradeDB(); | |
| 281 | - } | |
| 282 | - | |
| 283 | - elBtnUpgradeDB.on( 'click', function() { | |
| 284 | - lpModalOverlay.elLPOverlay.show(); | |
| 285 | - upgradeDB(); | |
| 286 | - } ); | |
| 287 | - } | |
| 288 | - }, | |
| 289 | - error: ( err ) => { | |
| 290 | - | |
| 291 | - }, | |
| 292 | - completed: () => { | |
| 293 | - | |
| 294 | - }, | |
| 295 | - }; | |
| 296 | - | |
| 297 | - handleAjax( urlHandle, {}, funcCallBack ); | |
| 298 | -}; | |
| 299 | - | |
| 300 | -export default getStepsUpgradeStatus; | |
| 1 | +import lpModalOverlay from '../../../../utils/lp-modal-overlay'; | |
| 2 | +import handleAjax from '../../../../utils/handle-ajax-api'; | |
| 3 | + | |
| 4 | +const $ = jQuery; | |
| 5 | + | |
| 6 | +const elToolUpgradeDB = $( '#lp-tool-upgrade-db' ); | |
| 7 | + | |
| 8 | +const upgradeDB = () => { | |
| 9 | + let isUpgrading = 0; | |
| 10 | + const elWrapperTermsUpgrade = elToolUpgradeDB.find( '.wrapper-terms-upgrade' ); | |
| 11 | + const elStatusUpgrade = elToolUpgradeDB.find( '.wrapper-lp-status-upgrade' ); | |
| 12 | + const elWrapperUpgradeMessage = elToolUpgradeDB.find( '.wrapper-lp-upgrade-message' ); | |
| 13 | + let checkValidBeforeUpgrade = null; | |
| 14 | + const elMessageUpgrading = $( 'input[name=message-when-upgrading]' ).val(); | |
| 15 | + | |
| 16 | + if ( elWrapperTermsUpgrade.length ) { // Show Terms Upgrade. | |
| 17 | + lpModalOverlay.setContentModal( elWrapperTermsUpgrade.html() ); | |
| 18 | + | |
| 19 | + const elTermUpdate = lpModalOverlay.elLPOverlay.find( '.terms-upgrade' ); | |
| 20 | + const elLPAgreeTerm = elTermUpdate.find( 'input[name=lp-agree-term]' ); | |
| 21 | + const elTermMessage = elTermUpdate.find( '.error' ); | |
| 22 | + | |
| 23 | + checkValidBeforeUpgrade = function() { | |
| 24 | + elTermMessage.hide(); | |
| 25 | + elTermMessage.removeClass( 'learn-press-message' ); | |
| 26 | + | |
| 27 | + if ( elLPAgreeTerm.is( ':checked' ) ) { | |
| 28 | + handleAjax( '/lp/v1/database/agree_terms', { agree_terms: 1 }, {} ); | |
| 29 | + | |
| 30 | + lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); | |
| 31 | + lpModalOverlay.setContentModal( elStatusUpgrade.html() ); | |
| 32 | + | |
| 33 | + return true; | |
| 34 | + } | |
| 35 | + | |
| 36 | + elTermMessage.show(); | |
| 37 | + elTermMessage.addClass( 'learn-press-message' ); | |
| 38 | + lpModalOverlay.elMainContent.animate( { | |
| 39 | + scrollTop: elTermMessage.offset().top, | |
| 40 | + } ); | |
| 41 | + | |
| 42 | + return false; | |
| 43 | + }; | |
| 44 | + } else { // Show Steps Upgrade. | |
| 45 | + lpModalOverlay.setContentModal( elStatusUpgrade.html() ); | |
| 46 | + checkValidBeforeUpgrade = function() { | |
| 47 | + return true; | |
| 48 | + }; | |
| 49 | + } | |
| 50 | + | |
| 51 | + lpModalOverlay.setTitleModal( elToolUpgradeDB.find( 'h2' ).html() ); | |
| 52 | + lpModalOverlay.elBtnYes.text( 'Upgrade' ); | |
| 53 | + lpModalOverlay.elBtnYes.show(); | |
| 54 | + lpModalOverlay.elBtnNo.text( 'Cancel' ); | |
| 55 | + lpModalOverlay.callBackYes = function( e ) { | |
| 56 | + if ( ! checkValidBeforeUpgrade() ) { | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + const target = e.target; | |
| 61 | + // Show message note when upgrading. | |
| 62 | + if ( target.innerText === 'Upgrade' ) { | |
| 63 | + lpModalOverlay.elFooter.prepend( '<span class="learn-press-notice">' + elMessageUpgrading + '</span>' ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + isUpgrading = 1; | |
| 67 | + | |
| 68 | + lpModalOverlay.elBtnYes.hide(); | |
| 69 | + lpModalOverlay.elBtnNo.hide(); | |
| 70 | + | |
| 71 | + const urlHandle = '/lp/v1/database/upgrade'; | |
| 72 | + const elGroupStep = lpModalOverlay.elLPOverlay.find( '.lp-group-step' ); | |
| 73 | + const elItemSteps = elToolUpgradeDB.find( '.lp-item-step' ); | |
| 74 | + | |
| 75 | + // Get params. | |
| 76 | + const steps = []; | |
| 77 | + | |
| 78 | + $.each( elItemSteps, function( i, el ) { | |
| 79 | + const elItemStepsTmp = $( el ); | |
| 80 | + | |
| 81 | + if ( ! elItemStepsTmp.hasClass( 'completed' ) ) { | |
| 82 | + const step = elItemStepsTmp.find( 'input' ).val(); | |
| 83 | + steps.push( step ); | |
| 84 | + } | |
| 85 | + } ); | |
| 86 | + | |
| 87 | + const params = { | |
| 88 | + steps, | |
| 89 | + step: steps[ 0 ], | |
| 90 | + }; | |
| 91 | + | |
| 92 | + let elItemStepCurrent = null; | |
| 93 | + | |
| 94 | + // Show progress when upgrading. | |
| 95 | + const showProgress = ( stepCurrent, percent ) => { | |
| 96 | + elItemStepCurrent = elGroupStep.find( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); | |
| 97 | + elItemStepCurrent.addClass( 'running' ); | |
| 98 | + | |
| 99 | + if ( 100 === percent ) { | |
| 100 | + elItemStepCurrent.removeClass( 'running' ).addClass( 'completed' ); | |
| 101 | + } | |
| 102 | + | |
| 103 | + elItemStepCurrent.find( '.progress-bar' ).css( 'width', percent + '%' ); | |
| 104 | + elItemStepCurrent.find( '.percent' ).text( percent + '%' ); | |
| 105 | + }; | |
| 106 | + | |
| 107 | + // Scroll to step current. | |
| 108 | + const scrollToStepCurrent = ( stepCurrent ) => { | |
| 109 | + elItemStepCurrent = elGroupStep.find( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); | |
| 110 | + | |
| 111 | + if ( ! elItemStepCurrent.length ) { | |
| 112 | + return; | |
| 113 | + } | |
| 114 | + | |
| 115 | + const offset = elItemStepCurrent.offset().top - lpModalOverlay.elMainContent.offset().top + | |
| 116 | + lpModalOverlay.elMainContent.scrollTop(); | |
| 117 | + | |
| 118 | + lpModalOverlay.elMainContent.stop().animate( { | |
| 119 | + scrollTop: offset, | |
| 120 | + }, 600 ); | |
| 121 | + }; | |
| 122 | + | |
| 123 | + showProgress( steps[ 0 ], 0.1 ); | |
| 124 | + | |
| 125 | + const funcCallBack = { | |
| 126 | + success: ( res ) => { | |
| 127 | + showProgress( params.step, res.percent ); | |
| 128 | + | |
| 129 | + if ( params.step !== res.name ) { | |
| 130 | + showProgress( res.name, 0.1 ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + scrollToStepCurrent( params.step ); | |
| 134 | + | |
| 135 | + if ( 'success' === res.status ) { | |
| 136 | + params.step = res.name; | |
| 137 | + params.data = res.data; | |
| 138 | + | |
| 139 | + setTimeout( () => { | |
| 140 | + handleAjax( urlHandle, params, funcCallBack ); | |
| 141 | + }, 800 ); | |
| 142 | + } else if ( 'finished' === res.status ) { | |
| 143 | + isUpgrading = 0; | |
| 144 | + elItemStepCurrent.removeClass( 'running' ).addClass( 'completed' ); | |
| 145 | + setTimeout( () => { | |
| 146 | + lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html() ); | |
| 147 | + }, 1000 ); | |
| 148 | + lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); | |
| 149 | + lpModalOverlay.elBtnNo.text( 'Close' ); | |
| 150 | + lpModalOverlay.elBtnNo.show(); | |
| 151 | + lpModalOverlay.elBtnNo.on( 'click', () => { | |
| 152 | + window.location.reload(); | |
| 153 | + } ); | |
| 154 | + } else { | |
| 155 | + isUpgrading = 0; | |
| 156 | + lpModalOverlay.elFooter.find( '.learn-press-notice' ).remove(); | |
| 157 | + elItemStepCurrent.removeClass( 'running' ).addClass( 'error' ); | |
| 158 | + lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html(), function() { | |
| 159 | + lpModalOverlay.elBtnYes.text( 'Retry' ).show(); | |
| 160 | + lpModalOverlay.callBackYes = () => { | |
| 161 | + window.location.href = lpGlobalSettings.siteurl + '/wp-admin/admin.php?page=learn-press-tools&tab=database&action=upgrade-db'; | |
| 162 | + }; | |
| 163 | + lpModalOverlay.elBtnNo.show(); | |
| 164 | + | |
| 165 | + if ( ! res.message ) { | |
| 166 | + res.message = 'Upgrade not success! Please clear cache, restart sever then retry or contact to LP to help'; | |
| 167 | + } | |
| 168 | + | |
| 169 | + lpModalOverlay.elMainContent.find( '.learn-press-message' ).addClass( 'error' ).html( res.message ); | |
| 170 | + } ); | |
| 171 | + } | |
| 172 | + }, | |
| 173 | + error: ( err ) => { | |
| 174 | + isUpgrading = 0; | |
| 175 | + lpModalOverlay.setContentModal( elWrapperUpgradeMessage.html(), function() { | |
| 176 | + lpModalOverlay.elBtnYes.text( 'Retry' ).show(); | |
| 177 | + lpModalOverlay.callBackYes = () => { | |
| 178 | + window.location.location = 'wp-admin/admin.php?page=learn-press-tools&tab=database&action=upgrade-db'; | |
| 179 | + }; | |
| 180 | + lpModalOverlay.elBtnNo.show(); | |
| 181 | + | |
| 182 | + if ( ! err.message ) { | |
| 183 | + err.message = 'Upgrade not success! Something wrong. Please clear cache, restart sever then retry or contact to LP to help'; | |
| 184 | + } | |
| 185 | + | |
| 186 | + lpModalOverlay.elMainContent.find( '.learn-press-message' ).addClass( 'error' ).html( err.message ); | |
| 187 | + } ); | |
| 188 | + }, | |
| 189 | + completed: () => { | |
| 190 | + | |
| 191 | + }, | |
| 192 | + }; | |
| 193 | + | |
| 194 | + handleAjax( urlHandle, params, funcCallBack ); | |
| 195 | + }; | |
| 196 | + | |
| 197 | + // Show confirm if, within upgrading, the user reload the page. | |
| 198 | + window.onbeforeunload = function() { | |
| 199 | + if ( isUpgrading ) { | |
| 200 | + return 'LP is upgrading Database. Are you want to reload page?'; | |
| 201 | + } | |
| 202 | + }; | |
| 203 | + | |
| 204 | + // Show confirm if, within upgrading, the user close the page. | |
| 205 | + window.onclose = function() { | |
| 206 | + if ( isUpgrading ) { | |
| 207 | + return 'LP is upgrading Database. Are you want to close page?'; | |
| 208 | + } | |
| 209 | + }; | |
| 210 | +}; | |
| 211 | + | |
| 212 | +const getStepsUpgradeStatus = () => { | |
| 213 | + if ( ! elToolUpgradeDB.length ) { | |
| 214 | + return; | |
| 215 | + } | |
| 216 | + | |
| 217 | + // initial LP Modal Overlay | |
| 218 | + if ( ! lpModalOverlay.init() ) { | |
| 219 | + return; | |
| 220 | + } | |
| 221 | + | |
| 222 | + const elWrapperStatusUpgrade = $( '.wrapper-lp-status-upgrade' ); | |
| 223 | + const urlHandle = '/lp/v1/database/get_steps'; | |
| 224 | + | |
| 225 | + // Show dialog upgrade database. | |
| 226 | + const queryString = window.location.search; | |
| 227 | + const urlParams = new URLSearchParams( queryString ); | |
| 228 | + const action = urlParams.get( 'action' ); | |
| 229 | + | |
| 230 | + if ( 'upgrade-db' === action ) { | |
| 231 | + lpModalOverlay.elLPOverlay.show(); | |
| 232 | + lpModalOverlay.setTitleModal( elToolUpgradeDB.find( 'h2' ).html() ); | |
| 233 | + lpModalOverlay.setContentModal( $( '.wrapper-lp-loading' ).html() ); | |
| 234 | + } | |
| 235 | + | |
| 236 | + const funcCallBack = { | |
| 237 | + success: ( res ) => { | |
| 238 | + const { steps_completed, steps_default } = res; | |
| 239 | + | |
| 240 | + if ( undefined === steps_completed || undefined === steps_default ) { | |
| 241 | + console.log( 'invalid steps_completed and steps_default' ); | |
| 242 | + return false; | |
| 243 | + } | |
| 244 | + | |
| 245 | + // Render show Steps. | |
| 246 | + let htmlStep = ''; | |
| 247 | + for ( const k_gr_steps in steps_default ) { | |
| 248 | + const step_group = steps_default[ k_gr_steps ]; | |
| 249 | + const steps = step_group.steps; | |
| 250 | + | |
| 251 | + htmlStep = '<div class="lp-group-step">'; | |
| 252 | + htmlStep += '<h3>' + step_group.label + '</h3>'; | |
| 253 | + | |
| 254 | + for ( const k_step in steps ) { | |
| 255 | + const step = steps[ k_step ]; | |
| 256 | + let completed = ''; | |
| 257 | + | |
| 258 | + if ( undefined !== steps_completed[ k_step ] ) { | |
| 259 | + completed = 'completed'; | |
| 260 | + } | |
| 261 | + | |
| 262 | + htmlStep += '<div class="lp-item-step ' + completed + '">'; | |
| 263 | + htmlStep += '<div class="lp-item-step-left"><input type="hidden" name="lp_steps_upgrade_db[]" value="' + step.name + '" /></div>'; | |
| 264 | + htmlStep += '<div class="lp-item-step-right">'; | |
| 265 | + htmlStep += '<label for=""><strong></strong>' + step.label + '</label>'; | |
| 266 | + htmlStep += '<div class="description">' + step.description + '</div>'; | |
| 267 | + htmlStep += '<div class="percent"></div>'; | |
| 268 | + htmlStep += '<span class="progress-bar"></span>'; | |
| 269 | + htmlStep += '</div>'; | |
| 270 | + htmlStep += '</div>'; | |
| 271 | + } | |
| 272 | + | |
| 273 | + htmlStep += '</div>'; | |
| 274 | + | |
| 275 | + elWrapperStatusUpgrade.append( htmlStep ); | |
| 276 | + | |
| 277 | + const elBtnUpgradeDB = $( '.lp-btn-upgrade-db' ); | |
| 278 | + | |
| 279 | + if ( 'upgrade-db' === action ) { | |
| 280 | + upgradeDB(); | |
| 281 | + } | |
| 282 | + | |
| 283 | + elBtnUpgradeDB.on( 'click', function() { | |
| 284 | + lpModalOverlay.elLPOverlay.show(); | |
| 285 | + upgradeDB(); | |
| 286 | + } ); | |
| 287 | + } | |
| 288 | + }, | |
| 289 | + error: ( err ) => { | |
| 290 | + | |
| 291 | + }, | |
| 292 | + completed: () => { | |
| 293 | + | |
| 294 | + }, | |
| 295 | + }; | |
| 296 | + | |
| 297 | + handleAjax( urlHandle, {}, funcCallBack ); | |
| 298 | +}; | |
| 299 | + | |
| 300 | +export default getStepsUpgradeStatus; | |