| 1 |
/* global jQuery, ajaxurl */ |
| 2 |
/** |
| 3 |
* MetaSync settings page scripts. |
| 4 |
* |
| 5 |
* Combines: save button handler, clear settings confirm, |
| 6 |
* Bing API key generator, plugin access roles. |
| 7 |
* Extracted from admin/class-metasync-admin.php (Phase 5, #887). |
| 8 |
*/ |
| 9 |
jQuery(document).ready(function($) { |
| 10 |
|
| 11 |
// --- Hosting Cache save button handler --- |
| 12 |
$('#metasync-hc-save-btn').on('click', function() { |
| 13 |
var $btn = $(this); |
| 14 |
var $msg = $('#metasync-hc-save-msg'); |
| 15 |
|
| 16 |
$btn.prop('disabled', true).text('Saving…'); |
| 17 |
|
| 18 |
$.ajax({ |
| 19 |
url: ajaxurl, |
| 20 |
type: 'POST', |
| 21 |
data: { |
| 22 |
action: 'metasync_save_hosting_cache_settings', |
| 23 |
hosting_cache_nonce: $('#metasync-hc-nonce').val(), |
| 24 |
wpengine_enabled: $('#metasync-hc-wpengine').is(':checked') ? '1' : '0', |
| 25 |
kinsta_enabled: $('#metasync-hc-kinsta').is(':checked') ? '1' : '0', |
| 26 |
}, |
| 27 |
success: function(response) { |
| 28 |
if (response.success) { |
| 29 |
$msg.text('� |
| 30 |
Saved').css('color', '#22c55e').show(); |
| 31 |
} else { |
| 32 |
$msg.text('❌ ' + (response.data.message || 'Save failed')).css('color', '#ef4444').show(); |
| 33 |
} |
| 34 |
}, |
| 35 |
error: function() { |
| 36 |
$msg.text('❌ Request failed').css('color', '#ef4444').show(); |
| 37 |
}, |
| 38 |
complete: function() { |
| 39 |
$btn.prop('disabled', false).text('💾 Save Settings'); |
| 40 |
setTimeout(function() { $msg.fadeOut(); }, 4000); |
| 41 |
} |
| 42 |
}); |
| 43 |
}); |
| 44 |
|
| 45 |
// --- Clear All Settings confirmation --- |
| 46 |
$('#metasync-clear-settings-form').on('submit', function(e) { |
| 47 |
e.preventDefault(); |
| 48 |
|
| 49 |
// First confirmation |
| 50 |
var firstConfirm = confirm("⚠️ WARNING: This will permanently delete ALL plugin settings!\n\nThis action cannot be undone. Are you sure you want to continue?"); |
| 51 |
if (!firstConfirm) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
// Second confirmation with more specific warning |
| 56 |
var secondConfirm = confirm("🚨 FINAL WARNING 🚨\n\nThis will delete:\n• All API keys and authentication tokens\n• White label branding settings\n• Plugin configuration and preferences\n• Instant indexing settings\n• All cached data\n\nYou will need to reconfigure the entire plugin from scratch.\n\nType 'DELETE' in the next prompt to confirm."); |
| 57 |
if (!secondConfirm) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
// Third confirmation requiring typing "DELETE" |
| 62 |
var typeConfirm = prompt("Type 'DELETE' (in capital letters) to confirm you want to permanently clear all settings:"); |
| 63 |
if (typeConfirm !== 'DELETE') { |
| 64 |
alert("Settings reset cancelled. Type 'DELETE' exactly to confirm."); |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
// If all confirmations passed, allow form submission |
| 69 |
this.submit(); |
| 70 |
return false; |
| 71 |
}); |
| 72 |
|
| 73 |
// --- Bing API key generator --- |
| 74 |
// Generate random API key |
| 75 |
$('#generate-bing-api-key-inline').on('click', function() { |
| 76 |
const array = new Uint8Array(16); |
| 77 |
crypto.getRandomValues(array); |
| 78 |
const hexString = Array.from(array, byte => byte.toString(16).padStart(2, '0')).join(''); |
| 79 |
$('#metasync_bing_api_key_inline').val(hexString); |
| 80 |
|
| 81 |
// Auto-enable toggle when API key is generated |
| 82 |
$('#enable_binginstantindex').prop('checked', true); |
| 83 |
|
| 84 |
// Trigger change event for unsaved changes detection |
| 85 |
$('#metasync_bing_api_key_inline').trigger('change'); |
| 86 |
}); |
| 87 |
|
| 88 |
// Auto-enable toggle when API key is entered manually |
| 89 |
$('#metasync_bing_api_key_inline').on('input', function() { |
| 90 |
const apiKey = $(this).val().trim(); |
| 91 |
if (apiKey.length >= 8) { |
| 92 |
$('#enable_binginstantindex').prop('checked', true); |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
// Show/hide API configuration based on enable toggle |
| 97 |
function toggleBingConfig() { |
| 98 |
const isEnabled = $('#enable_binginstantindex').is(':checked'); |
| 99 |
const $apiConfig = $('#enable_binginstantindex').closest('tr').nextAll('tr').first(); |
| 100 |
if (isEnabled) { |
| 101 |
$apiConfig.show(); |
| 102 |
} else { |
| 103 |
$apiConfig.hide(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// Initial state |
| 108 |
toggleBingConfig(); |
| 109 |
|
| 110 |
// Toggle on change |
| 111 |
$('#enable_binginstantindex').on('change', toggleBingConfig); |
| 112 |
|
| 113 |
// --- Plugin access roles --- |
| 114 |
var $allRolesCheckbox = $('#plugin-access-all-roles'); |
| 115 |
var $individualRoles = $('.plugin-access-individual-role'); |
| 116 |
|
| 117 |
// When "All Roles" is checked, uncheck all individual roles |
| 118 |
$allRolesCheckbox.on('change', function() { |
| 119 |
if ($(this).is(':checked')) { |
| 120 |
$individualRoles.prop('checked', false); |
| 121 |
} |
| 122 |
}); |
| 123 |
|
| 124 |
// When any individual role is checked, uncheck "All Roles" |
| 125 |
$individualRoles.on('change', function() { |
| 126 |
if ($(this).is(':checked')) { |
| 127 |
$allRolesCheckbox.prop('checked', false); |
| 128 |
} |
| 129 |
}); |
| 130 |
}); |
| 131 |
|