Files
wp-fedistream/assets/js/admin.js
magdev f3cd19efe0 feat: Add license management and tabbed settings (v0.3.0)
- Implement license management using magdev/wc-licensed-product-client
- Reorganize settings page into License, Default Settings, Integrations tabs
- Add license validation and activation via AJAX
- Frontend features require valid license (admin works always)
- Update translations with German (de_CH) for license strings

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 12:03:05 +01:00

123 lines
3.8 KiB
JavaScript

/**
* 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('<p>' + escapeHtml(text) + '</p>')
.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);