/* global metasyncHostBlockingData, jQuery */ /** * MetaSync Host Blocking Test * * Extracted for Phase 5, #887. * Handles GET/POST/BOTH host blocking tests on the settings page * and the dashboard "Test Both" variant. * * Localized object: metasyncHostBlockingData * - ajaxUrl (string) * * @since Phase 5 */ (function () { // Wait for jQuery to be available function initHostBlockingTest() { if (typeof jQuery === 'undefined') { setTimeout(initHostBlockingTest, 100); return; } jQuery(document).ready(function ($) { // Ensure ajax URL is available in this scope var ajaxUrl = (typeof window.ajaxurl !== 'undefined' && window.ajaxurl) ? window.ajaxurl : metasyncHostBlockingData.ajaxUrl; // --- Settings page buttons (GET / POST / BOTH) --- $(document).on('click', '#test-get-request', function (e) { e.preventDefault(); e.stopPropagation(); runHostTest('GET'); return false; }); $(document).on('click', '#test-post-request', function (e) { e.preventDefault(); e.stopPropagation(); runHostTest('POST'); return false; }); // --- Shared "Test Both" button (settings + dashboard) --- var $btn = $('#test-both-requests'); $btn.on('click', function (e) { e.preventDefault(); e.stopPropagation(); runHostTest('BOTH'); return false; }); $(document).on('click', '#test-both-requests', function (e) { e.preventDefault(); e.stopPropagation(); runHostTest('BOTH'); return false; }); // Expose function globally for debugging window.runHostBlockingTest = function () { runHostTest('BOTH'); }; function runHostTest(method) { var buttonId = (method === 'BOTH' ? 'test-both-requests' : 'test-' + method.toLowerCase() + '-request'); var $button = $('#' + buttonId); if ($button.length === 0) { alert('Error: Test button not found. Please refresh the page.'); return; } var originalText = $button.text(); // Disable button and show loading $button.prop('disabled', true); $button.text('\uD83D\uDD04 Testing...'); // Prepare results area var $resultsDiv = $('#host-test-results'); var $resultsContent = $('#test-results-content'); $resultsDiv.show(); $resultsContent.html('
Running ' + (method === 'BOTH' ? 'GET and POST' : method) + ' test(s)...
');
$('').text('HTTP Status Code: ').appendTo($p);
$('').addClass('status-code').text(String(result.status_code)).appendTo($p);
$details.append($p);
}
if (result.error) {
var $pe = $(' ');
$('').text('Error: ').appendTo($pe);
$('').addClass('error-message').text(result.error).appendTo($pe);
$details.append($pe);
}
if (result.body) {
$details.append($(' ').append($('').text('Response Body:')));
$details.append(makePre('response-body', result.body));
}
if (result.headers && Object.keys(result.headers).length > 0) {
$details.append($(' ').append($('').text('Response Headers:')));
var headersText = '';
Object.keys(result.headers).forEach(function (key) {
headersText += key + ': ' + String(result.headers[key]) + '\n';
});
$details.append(makePre('response-headers', headersText));
}
if (result.sent_data) {
$details.append($(' ').append($('').text('Sent Data:')));
$details.append(makePre('sent-data', JSON.stringify(result.sent_data, null, 2)));
}
if (result.parsed_response) {
$details.append($(' ').append($('').text('Parsed Response:')));
$details.append(makePre('parsed-response', JSON.stringify(result.parsed_response, null, 2)));
}
$details.append(makeP('Details: ', result.details));
$item.append($details);
$container.append($item);
});
$('#host-test-results').show();
}
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function (m) {
return map[m];
});
}
// Verify button exists on page load
setTimeout(function () {
var btnBoth = $('#test-both-requests');
}, 500);
});
}
// Start initialization
initHostBlockingTest();
})();