/** * WP FediStream - Admin Scripts * * @package WP_FediStream */ (function($) { 'use strict'; $(document).ready(function() { // License validation functionality initLicenseValidation(); }); /** * Initialize license validation AJAX handlers. */ function initLicenseValidation() { var $validateBtn = $('#fedistream-validate-license'); var $activateBtn = $('#fedistream-activate-license'); var $spinner = $('#fedistream-license-spinner'); var $message = $('#fedistream-license-message'); if (!$validateBtn.length) { return; } // Validate license button $validateBtn.on('click', function(e) { e.preventDefault(); performLicenseAction('fedistream_validate_license', 'Validating...'); }); // Activate license button $activateBtn.on('click', function(e) { e.preventDefault(); performLicenseAction('fedistream_activate_license', 'Activating...'); }); /** * Perform license AJAX action. * * @param {string} action AJAX action name. * @param {string} loadingText Loading button text. */ function performLicenseAction(action, loadingText) { var originalText = $validateBtn.text(); // Show loading state $spinner.addClass('is-active'); $validateBtn.prop('disabled', true); $activateBtn.prop('disabled', true); $message.hide(); $.ajax({ url: ajaxurl, type: 'POST', data: { action: action, nonce: fedistreamLicenseNonce }, 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 to show updated status setTimeout(function() { window.location.reload(); }, 1500); } else { showMessage('error', response.data.message || 'An error occurred.'); } }, error: function(xhr, status, error) { $spinner.removeClass('is-active'); $validateBtn.prop('disabled', false); $activateBtn.prop('disabled', false); showMessage('error', 'Request failed. Please try again.'); } }); } /** * Show a message to the user. * * @param {string} type Message type: 'success', 'error', 'warning', 'info'. * @param {string} text Message text. */ function showMessage(type, text) { var classMap = { 'success': 'notice-success', 'error': 'notice-error', 'warning': 'notice-warning', 'info': 'notice-info' }; var noticeClass = classMap[type] || 'notice-info'; $message .removeClass('notice-success notice-error notice-warning notice-info') .addClass('notice ' + noticeClass) .html('

' + escapeHtml(text) + '

') .show(); } /** * Escape HTML entities. * * @param {string} text Text to escape. * @return {string} Escaped text. */ function escapeHtml(text) { var div = document.createElement('div'); div.textContent = text; return div.innerHTML; } } })(jQuery);