| 1 |
/* global jQuery, PoweredCache, ajaxurl */ |
| 2 |
const { __ } = wp.i18n; |
| 3 |
|
| 4 |
(function ($) { |
| 5 |
// Use strict mode |
| 6 |
|
| 7 |
// Define global PoweredCache object if it does not exist |
| 8 |
if (typeof window.PoweredCache !== 'object') { |
| 9 |
window.PoweredCache = {}; |
| 10 |
} |
| 11 |
|
| 12 |
PoweredCache.pageModals = function () { |
| 13 |
const body = $('body'); |
| 14 |
if (!body.hasClass('toplevel_page_powered-cache')) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Modal: diagnosticModal Modal |
| 20 |
* |
| 21 |
* Run demo for performance test and close modal at the end. |
| 22 |
*/ |
| 23 |
function diagnosticModal() { |
| 24 |
const activate = $('#pcmodal--powered-cache-diagnostic-test'); |
| 25 |
|
| 26 |
activate.on('click', function () { |
| 27 |
const button = $(this); |
| 28 |
const suiBox = button.closest('.sui-box'); |
| 29 |
const boxTitle = suiBox.find('.sui-box-title'); |
| 30 |
const boxSkip = suiBox.find('.sui-box-header button'); |
| 31 |
const diagnosticItems = suiBox.find('#powered-cache-diagnostic-items'); |
| 32 |
const nonce = $('#powered_cache_run_diagnostic').val(); |
| 33 |
const oldTitle = __('Diagnostic', 'powered-cache'); |
| 34 |
|
| 35 |
jQuery |
| 36 |
.ajax({ |
| 37 |
url: ajaxurl, |
| 38 |
method: 'post', |
| 39 |
beforeSend() { |
| 40 |
// Assign new title. |
| 41 |
boxTitle.text(__('Running Diagnostic Tests…', 'powered-cache')); |
| 42 |
// Disable "skip" button. |
| 43 |
boxSkip.attr('disabled', true); |
| 44 |
// clear previous message exists |
| 45 |
diagnosticItems.empty(); |
| 46 |
}, |
| 47 |
data: { |
| 48 |
nonce, |
| 49 |
action: 'powered_cache_run_diagnostic', |
| 50 |
}, |
| 51 |
success(response) { |
| 52 |
if (response.success && response.data.length) { |
| 53 |
response.data.forEach((item) => { |
| 54 |
let icon = null; |
| 55 |
if (item.status) { |
| 56 |
icon = |
| 57 |
'<span class="sui-icon-check" aria-hidden="true"></span>'; |
| 58 |
} else { |
| 59 |
icon = ( |
| 60 |
<span className="sui-icon-close" aria-hidden="true" /> |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
diagnosticItems.append(`<li>${icon} ${item.description}</li>`); |
| 65 |
}); |
| 66 |
} |
| 67 |
}, |
| 68 |
}) |
| 69 |
.done(function () { |
| 70 |
// Assign old title. |
| 71 |
boxTitle.text(oldTitle); |
| 72 |
// Enable "skip" button. |
| 73 |
boxSkip.removeAttr('disabled'); |
| 74 |
}); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
function init() { |
| 79 |
diagnosticModal(); |
| 80 |
} |
| 81 |
|
| 82 |
init(); |
| 83 |
}; |
| 84 |
|
| 85 |
$('body').ready(function () { |
| 86 |
PoweredCache.pageModals('modals'); |
| 87 |
}); |
| 88 |
})(jQuery); |
| 89 |
|