100 lines
2.4 KiB
JavaScript
100 lines
2.4 KiB
JavaScript
|
|
/**
|
||
|
|
* WP BnB Admin JavaScript
|
||
|
|
*
|
||
|
|
* @package Magdev\WpBnb
|
||
|
|
*/
|
||
|
|
|
||
|
|
(function($) {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Initialize license management functionality.
|
||
|
|
*/
|
||
|
|
function initLicenseManagement() {
|
||
|
|
var $validateBtn = $('#wp-bnb-validate-license');
|
||
|
|
var $activateBtn = $('#wp-bnb-activate-license');
|
||
|
|
var $spinner = $('#wp-bnb-license-spinner');
|
||
|
|
var $message = $('#wp-bnb-license-message');
|
||
|
|
|
||
|
|
if (!$validateBtn.length) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validate license button click.
|
||
|
|
$validateBtn.on('click', function(e) {
|
||
|
|
e.preventDefault();
|
||
|
|
performLicenseAction('wp_bnb_validate_license', wpBnbAdmin.i18n.validating);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Activate license button click.
|
||
|
|
$activateBtn.on('click', function(e) {
|
||
|
|
e.preventDefault();
|
||
|
|
performLicenseAction('wp_bnb_activate_license', wpBnbAdmin.i18n.activating);
|
||
|
|
});
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Perform a license AJAX action.
|
||
|
|
*
|
||
|
|
* @param {string} action AJAX action name.
|
||
|
|
* @param {string} loadingText Loading text to display.
|
||
|
|
*/
|
||
|
|
function performLicenseAction(action, loadingText) {
|
||
|
|
// Disable buttons and show spinner.
|
||
|
|
$validateBtn.prop('disabled', true);
|
||
|
|
$activateBtn.prop('disabled', true);
|
||
|
|
$spinner.addClass('is-active');
|
||
|
|
$message.hide();
|
||
|
|
|
||
|
|
$.ajax({
|
||
|
|
url: wpBnbAdmin.ajaxUrl,
|
||
|
|
type: 'POST',
|
||
|
|
data: {
|
||
|
|
action: action,
|
||
|
|
nonce: wpBnbAdmin.nonce
|
||
|
|
},
|
||
|
|
success: function(response) {
|
||
|
|
$spinner.removeClass('is-active');
|
||
|
|
$validateBtn.prop('disabled', false);
|
||
|
|
$activateBtn.prop('disabled', false);
|
||
|
|
|
||
|
|
if (response.success) {
|
||
|
|
showMessage('success', response.data.message);
|
||
|
|
// Reload page after short delay to show updated status.
|
||
|
|
setTimeout(function() {
|
||
|
|
window.location.reload();
|
||
|
|
}, 1500);
|
||
|
|
} else {
|
||
|
|
showMessage('error', response.data.message || wpBnbAdmin.i18n.error);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
error: function() {
|
||
|
|
$spinner.removeClass('is-active');
|
||
|
|
$validateBtn.prop('disabled', false);
|
||
|
|
$activateBtn.prop('disabled', false);
|
||
|
|
showMessage('error', wpBnbAdmin.i18n.error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show a message.
|
||
|
|
*
|
||
|
|
* @param {string} type Message type (success or error).
|
||
|
|
* @param {string} message Message text.
|
||
|
|
*/
|
||
|
|
function showMessage(type, message) {
|
||
|
|
$message
|
||
|
|
.removeClass('success error')
|
||
|
|
.addClass(type)
|
||
|
|
.text(message)
|
||
|
|
.fadeIn();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Initialize on document ready.
|
||
|
|
$(document).ready(function() {
|
||
|
|
initLicenseManagement();
|
||
|
|
});
|
||
|
|
|
||
|
|
})(jQuery);
|