Files
InformatiQ-Toolkit/assets/js/admin.js
Malin a8d7972ad7 feat: add Central API clients, bot rate limiting, and admin API UI
- Add ITK_HP_API and ITK_Bot_API static classes with queue/flush/cron
- Add WP-Cron (5 min) + shutdown flush for both API queues
- Bot Blocker and Honeypot now queue events to their respective APIs
- Admin: Bot Blocker tab gains Central Bot API settings panel
  (enable, URL, token, test connection, flush queue, historical sync)
- Admin: Honeypot tab gains Central Honeypot API settings panel
- Admin JS: AJAX handlers for Test Connection and Flush Now buttons
- Admin CSS: API card styles (status badge, notices, footer controls)
- Add .gitignore (excludes bot-api/ which lives in CloudHost/bot-api)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 18:32:27 +02:00

138 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* InformatiQ Toolkit Admin JS */
(function ($) {
'use strict';
/* ── Toggle switches (AJAX) ───────────────────────────────── */
$(document).on('change', '.itk-toggle-input', function () {
var $input = $(this);
var $row = $input.closest('.itk-toggle-row');
var option = $input.data('option');
var setting = $input.data('setting');
var value = $input.is(':checked') ? 1 : 0;
$row.addClass('itk-toggle-saving');
$.post(itkAdmin.ajaxUrl, {
action: 'itk_save_setting',
nonce: itkAdmin.nonce,
option: option,
setting: setting,
value: value
})
.done(function (res) {
if (res.success) {
showFeedback($row, 'itk-toggle-saved', 'Saved');
} else {
$input.prop('checked', !$input.is(':checked')); // revert
showFeedback($row, 'itk-toggle-error', 'Error saving');
}
})
.fail(function () {
$input.prop('checked', !$input.is(':checked'));
showFeedback($row, 'itk-toggle-error', 'Request failed');
})
.always(function () {
$row.removeClass('itk-toggle-saving');
});
});
function showFeedback($row, cls, msg) {
$row.find('.itk-feedback').remove();
var $fb = $('<span class="itk-feedback ' + cls + '">' + msg + '</span>');
$row.append($fb);
setTimeout(function () { $fb.fadeOut(400, function () { $(this).remove(); }); }, 2000);
}
/* ── API: Test connection ─────────────────────────────────── */
$(document).on('click', '.itk-btn-test-api', function () {
var $btn = $(this);
var api = $btn.data('api');
var $result = $btn.closest('form').find('.itk-api-ajax-result');
$btn.prop('disabled', true).text('Testing…');
$result.hide();
$.post(itkAdmin.ajaxUrl, {
action: 'itk_test_api',
nonce: itkAdmin.nonce,
api: api
})
.done(function (res) {
var ok = res && res.ok;
var msg = (res && res.message) ? res.message : (ok ? 'Connected.' : 'Test failed.');
$result.text(msg).css('color', ok ? '#00a32a' : '#b32d2e').show();
setTimeout(function () { $result.fadeOut(); }, 5000);
})
.fail(function () {
$result.text('Request failed.').css('color', '#b32d2e').show();
setTimeout(function () { $result.fadeOut(); }, 4000);
})
.always(function () {
$btn.prop('disabled', false).text('Test Connection');
});
});
/* ── API: Flush queue ─────────────────────────────────────── */
$(document).on('click', '.itk-btn-flush-api', function () {
var $btn = $(this);
var api = $btn.data('api');
var $result = $btn.siblings('.itk-api-flush-result');
$btn.prop('disabled', true).text('Flushing…');
$result.hide();
$.post(itkAdmin.ajaxUrl, {
action: 'itk_flush_api_queue',
nonce: itkAdmin.nonce,
api: api
})
.done(function (res) {
var ok = res && res.success;
var msg = ok ? 'Queue flushed.' : 'Flush failed.';
$result.text(msg).css('color', ok ? '#00a32a' : '#b32d2e').show();
setTimeout(function () { $result.fadeOut(); }, 3000);
})
.fail(function () {
$result.text('Request failed.').css('color', '#b32d2e').show();
setTimeout(function () { $result.fadeOut(); }, 3000);
})
.always(function () {
$btn.prop('disabled', false).text('Flush Now');
});
});
/* ── Config file editor (AJAX) ────────────────────────────── */
$('#itk-save-config').on('click', function (e) {
e.preventDefault();
var $btn = $(this);
var file = $btn.data('file');
var content = $('#itk-config-content').val();
var $status = $('#itk-config-status');
$btn.prop('disabled', true).text('Saving…');
$status.hide();
$.post(itkAdmin.ajaxUrl, {
action: 'itk_save_config_file',
nonce: itkAdmin.nonce,
file: file,
content: content
})
.done(function (res) {
if (res.success) {
$status.text('Saved!').css('color', '#00a32a').show();
} else {
$status.text('Error: ' + (res.data || 'unknown')).css('color', '#b32d2e').show();
}
})
.fail(function () {
$status.text('Request failed.').css('color', '#b32d2e').show();
})
.always(function () {
$btn.prop('disabled', false).text('Save File');
setTimeout(function () { $status.fadeOut(); }, 3000);
});
});
})(jQuery);