File: //srv/wordpress/mu-plugins/edge-cache/js/edge-cache.js
jQuery(document).ready(function ($) {
const toggle_timeout_ms = 5000;
const purge_timeout_ms = 60000;
function disable_actions(flag, action) {
if (undefined === action) {
$('.ec-action').each(function() {
$(this).prop('disabled', flag).show();
$(this).next().removeAttr('class').html('');
});
} else {
btn = $('.ec-action[data-action="'+action+'"]');
btn.prop('disabled', flag).show();
btn.next().removeAttr('class').html('');
}
}
function fatal_error(action, message) {
if (undefined === message) {
message = 'Failed to process your request.<br/>'
+ 'Try refreshing this page in a few minutes. '
+ 'If this error persists, please contact Technical Support.';
}
btn = $('.ec-action[data-action="'+action+'"]');
msg = $(btn.next());
msg.addClass('notice notice-error');
msg.html('<p>' + message + '</p>');
setTimeout(disable_actions, purge_timeout_ms, false);
}
function toggle_ec_status() {
ec_form = $('#ec_form');
ec_status = ec_form.data('status');
ecs = $('.dashicons-cloud');
btn = $('.ec-action[data-action="toggle"]');
msg = $(btn.next());
msg.addClass('notice notice-info');
if (1 === ec_status) {
new_status = 0;
btn.html('Enable Edge Cache');
btn.removeClass('button-link-delete').addClass('is-primary');
ecs.removeClass('ec-status-1').addClass('ec-status-0');
msg.html('Edge Cache has been <b>disabled</b>.');
disable_actions(true, 'purge');
} else if (0 === ec_status) {
new_status = 1;
btn.html('Disable Edge Cache');
btn.removeClass('is-primary').addClass('button-link-delete');
ecs.removeClass('ec-status-0').addClass('ec-status-1');
msg.html('Edge Cache has been <b>enabled</b>.');
$('.ec-action[data-action="purge"]').prop('disabled', false);
} else {
fatal_error( 'toggle', 'Edge Cache cannot be modifed at the moment.');
return;
}
ec_form.data('status', new_status);
setTimeout(disable_actions, toggle_timeout_ms, false, 'toggle');
}
$('.ec-action').click(function (e) {
e.preventDefault();
disable_actions(true);
ec_form = $('#ec_form');
ec_status = ec_form.data('status');
btn = $(e.target);
action = btn.data('action');
msg = $(btn.next());
spinner = $(msg.next());
spinner.html('<img src="/wp-admin/images/loading.gif" />');
if ('toggle' === action) {
msg.html( ( ec_status ? 'Disabling ' : 'Enabling ' ) + 'Edge Cache ...' );
} else {
msg.html('Clearing Edge Cache ...');
}
jQuery.ajax({
url: ec_form.prop('action'),
type: 'POST',
data: { "action": "edge_cache", "opt": action, "ec_status": ec_status },
}).always(function() {
btn.hide();
spinner.html('');
}).fail(function() {
return fatal_error(action);
}).done(function(result) {
if ( typeof result !== 'object' ) {
try {
result = JSON.parse(result);
} catch (e) {
return fatal_error(action);
}
}
if (false === result.hasOwnProperty('success')) {
return fatal_error(action);
}
if (false === result.success) {
if ( 'purge' === action ) {
$('.ec-action[data-action="toggle"]').prop('disabled', false);
}
if ( result.data ) {
msg.addClass('notice notice-error');
msg.html(result.data);
setTimeout(disable_actions, purge_timeout_ms, false);
return;
}
return fatal_error(action);
}
if ('toggle' === action) {
toggle_ec_status();
} else {
msg.addClass('notice notice-info');
msg.html('Edge Cache has been cleared.');
$('.ec-action[data-action="toggle"]').prop('disabled', false);
setTimeout(disable_actions, purge_timeout_ms, false);
}
})
});
});