| 1 |
/* global metasyncConnectData, jQuery */ |
| 2 |
/** |
| 3 |
* MetaSync Connect — Whitelabel Validation Modal & Lock Section |
| 4 |
* |
| 5 |
* Extracted for Phase 5, #887 — Part B JS extraction. |
| 6 |
* Combines the whitelabel validation modal (password/recovery-email guards, |
| 7 |
* hide-settings checkbox logic, export button) with the lock-section handler. |
| 8 |
* |
| 9 |
* Localized data object: metasyncConnectData |
| 10 |
* - optionKey (string) — plugin option key for form field names |
| 11 |
* - storedPassword (string) — current stored settings password |
| 12 |
* - adminPostUrl (string) — admin-post.php URL |
| 13 |
* - exportNonce (string) — nonce for metasync_export_whitelabel action |
| 14 |
* - ajaxUrl (string) — admin-ajax.php URL |
| 15 |
* - logoutNonceField (string) — full HTML nonce input for whitelabel logout |
| 16 |
* |
| 17 |
* @since Phase 5 |
| 18 |
* @see wp_localize_script() call in class-metasync-admin.php |
| 19 |
*/ |
| 20 |
|
| 21 |
jQuery(document).ready(function($) { |
| 22 |
var isShowingModal = false; |
| 23 |
|
| 24 |
// Function to show custom modal |
| 25 |
function showModal(icon, title, message) { |
| 26 |
isShowingModal = true; |
| 27 |
$('#modal-icon').text(icon); |
| 28 |
$('#modal-title').text(title); |
| 29 |
$('#modal-message').html(message); |
| 30 |
$('#metasync-custom-modal').fadeIn(200); |
| 31 |
|
| 32 |
// Reset flag after modal animation |
| 33 |
setTimeout(function() { |
| 34 |
isShowingModal = false; |
| 35 |
}, 300); |
| 36 |
} |
| 37 |
|
| 38 |
// Close modal |
| 39 |
$('#modal-close-btn').on('click', function(e) { |
| 40 |
e.preventDefault(); |
| 41 |
e.stopPropagation(); |
| 42 |
$('#metasync-custom-modal').fadeOut(200); |
| 43 |
return false; |
| 44 |
}); |
| 45 |
|
| 46 |
// Close modal when clicking backdrop |
| 47 |
$('#metasync-custom-modal').on('click', function(e) { |
| 48 |
if (e.target === this) { |
| 49 |
$(this).fadeOut(200); |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
// Function to check if password is set |
| 54 |
function hasPassword() { |
| 55 |
var passwordField = $('input[name="' + metasyncConnectData.optionKey + '[whitelabel][settings_password]"]'); |
| 56 |
var passwordValue = passwordField.val(); |
| 57 |
return passwordValue && passwordValue.length > 0; |
| 58 |
} |
| 59 |
|
| 60 |
// Function to check if recovery email is set |
| 61 |
function hasRecoveryEmail() { |
| 62 |
var recoveryEmailField = $('input[name="' + metasyncConnectData.optionKey + '[whitelabel][recovery_email]"]'); |
| 63 |
var emailValue = recoveryEmailField.val(); |
| 64 |
return emailValue && emailValue.length > 0 && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailValue); |
| 65 |
} |
| 66 |
|
| 67 |
// Validate recovery email when password is being set |
| 68 |
var passwordField = $('input[name="' + metasyncConnectData.optionKey + '[whitelabel][settings_password]"]'); |
| 69 |
var recoveryEmailField = $('input[name="' + metasyncConnectData.optionKey + '[whitelabel][recovery_email]"]'); |
| 70 |
|
| 71 |
// Real-time validation for recovery email |
| 72 |
passwordField.on('blur', function() { |
| 73 |
if (hasPassword() && !hasRecoveryEmail()) { |
| 74 |
showModal( |
| 75 |
'\uD83D\uDCE7', |
| 76 |
'Recovery Email Required', |
| 77 |
'You must set a <strong>Recovery Email</strong> when setting a password.<br><br>Please enter a valid email address in the <strong>Recovery Email</strong> field.' |
| 78 |
); |
| 79 |
recoveryEmailField.focus(); |
| 80 |
} |
| 81 |
}); |
| 82 |
|
| 83 |
// Mark recovery email as required when password is set |
| 84 |
passwordField.on('input', function() { |
| 85 |
if (hasPassword()) { |
| 86 |
recoveryEmailField.attr('required', true); |
| 87 |
recoveryEmailField.closest('tr').find('th').addClass('required-field'); |
| 88 |
} else { |
| 89 |
recoveryEmailField.removeAttr('required'); |
| 90 |
recoveryEmailField.closest('tr').find('th').removeClass('required-field'); |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
// Trigger initial check |
| 95 |
passwordField.trigger('input'); |
| 96 |
|
| 97 |
// Store original checkbox state |
| 98 |
var hideSettingsCheckbox = $('#checkbox_hide_settings'); |
| 99 |
var originalCheckboxState = hideSettingsCheckbox.is(':checked'); |
| 100 |
|
| 101 |
// Handle Hide Settings checkbox with click event (runs before change) |
| 102 |
hideSettingsCheckbox.on('click', function(e) { |
| 103 |
if (!originalCheckboxState && !hasPassword()) { |
| 104 |
// Prevent the click from checking the box |
| 105 |
e.preventDefault(); |
| 106 |
e.stopImmediatePropagation(); |
| 107 |
|
| 108 |
// Show modal asking to set password |
| 109 |
showModal( |
| 110 |
'\uD83D\uDD10', |
| 111 |
'Password Required', |
| 112 |
'You must set a <strong>Settings Password</strong> before enabling "Hide Settings".<br><br>Please scroll up to the <strong>Branding</strong> section and set a password first.' |
| 113 |
); |
| 114 |
|
| 115 |
// Focus on password field when modal closes |
| 116 |
setTimeout(function() { |
| 117 |
$('input[name="' + metasyncConnectData.optionKey + '[whitelabel][settings_password]"]').focus(); |
| 118 |
}, 300); |
| 119 |
|
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
// Update original state when valid change happens |
| 124 |
setTimeout(function() { |
| 125 |
originalCheckboxState = hideSettingsCheckbox.is(':checked'); |
| 126 |
}, 0); |
| 127 |
}); |
| 128 |
|
| 129 |
// Prevent clearing password when Hide Settings is enabled |
| 130 |
var storedPassword = metasyncConnectData.storedPassword; |
| 131 |
|
| 132 |
passwordField.on('keydown', function(e) { |
| 133 |
var currentValue = $(this).val(); |
| 134 |
|
| 135 |
// If Hide Settings is checked and user tries to clear password (backspace/delete on empty or last char) |
| 136 |
if (hideSettingsCheckbox.is(':checked') && |
| 137 |
(currentValue.length <= 1 || !currentValue) && |
| 138 |
(e.keyCode === 8 || e.keyCode === 46)) { // Backspace or Delete |
| 139 |
|
| 140 |
e.preventDefault(); |
| 141 |
e.stopImmediatePropagation(); |
| 142 |
|
| 143 |
// Show warning modal only once |
| 144 |
if (!isShowingModal) { |
| 145 |
showModal( |
| 146 |
'\uD83D\uDEAB', |
| 147 |
'Cannot Remove Password', |
| 148 |
'You cannot remove the <strong>White Label Settings Password</strong> while "Hide Settings" is enabled.<br><br>Please uncheck <strong>"Hide Settings"</strong> first if you want to remove the password.' |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
// Listen for successful AJAX save to update password status |
| 157 |
$(document).on('metasync_settings_saved', function() { |
| 158 |
// Password status will be checked dynamically via hasPassword() function |
| 159 |
}); |
| 160 |
|
| 161 |
// Close modal with Escape key |
| 162 |
$(document).on('keydown', function(e) { |
| 163 |
if (e.key === 'Escape') { |
| 164 |
$('#metasync-custom-modal').fadeOut(200); |
| 165 |
} |
| 166 |
}); |
| 167 |
|
| 168 |
// Handle export whitelabel settings button |
| 169 |
$('#metasync-export-whitelabel-btn').on('click', function(e) { |
| 170 |
e.preventDefault(); |
| 171 |
|
| 172 |
var $button = $(this); |
| 173 |
var originalText = $button.html(); |
| 174 |
|
| 175 |
// Disable button and show loading state |
| 176 |
$button.prop('disabled', true).html('\u23F3 Exporting...'); |
| 177 |
|
| 178 |
// Create a form to submit the export request |
| 179 |
var form = $('<form>', { |
| 180 |
'method': 'POST', |
| 181 |
'action': metasyncConnectData.adminPostUrl, |
| 182 |
'target': '_blank' |
| 183 |
}); |
| 184 |
|
| 185 |
form.append($('<input>', { |
| 186 |
'type': 'hidden', |
| 187 |
'name': 'action', |
| 188 |
'value': 'metasync_export_whitelabel_settings' |
| 189 |
})); |
| 190 |
|
| 191 |
form.append($('<input>', { |
| 192 |
'type': 'hidden', |
| 193 |
'name': '_wpnonce', |
| 194 |
'value': metasyncConnectData.exportNonce |
| 195 |
})); |
| 196 |
|
| 197 |
// Append form to body, submit, then remove |
| 198 |
$('body').append(form); |
| 199 |
form.submit(); |
| 200 |
|
| 201 |
// Remove form after a short delay |
| 202 |
setTimeout(function() { |
| 203 |
form.remove(); |
| 204 |
$button.prop('disabled', false).html(originalText); |
| 205 |
}, 2000); |
| 206 |
}); |
| 207 |
|
| 208 |
/* ====================================================================== |
| 209 |
Lock Section Handler |
| 210 |
====================================================================== */ |
| 211 |
|
| 212 |
// Ensure ajax URL is available in all admin contexts |
| 213 |
var ajaxUrl = (typeof window.ajaxurl !== 'undefined' && window.ajaxurl) |
| 214 |
? window.ajaxurl |
| 215 |
: metasyncConnectData.ajaxUrl; |
| 216 |
|
| 217 |
// Handle Lock Section button clicks |
| 218 |
$('.metasync-lock-btn').on('click', function(e) { |
| 219 |
e.preventDefault(); |
| 220 |
e.stopPropagation(); |
| 221 |
|
| 222 |
var currentTab = $(this).data('tab'); |
| 223 |
|
| 224 |
// Create a hidden form to submit the logout request |
| 225 |
var logoutForm = $('<form>', { |
| 226 |
'method': 'post', |
| 227 |
'action': '' |
| 228 |
}); |
| 229 |
|
| 230 |
// Add nonce field |
| 231 |
logoutForm.append(metasyncConnectData.logoutNonceField); |
| 232 |
|
| 233 |
// Add logout field |
| 234 |
logoutForm.append($('<input>', { |
| 235 |
'type': 'hidden', |
| 236 |
'name': 'whitelabel_logout', |
| 237 |
'value': '1' |
| 238 |
})); |
| 239 |
|
| 240 |
// Append to body and submit |
| 241 |
$('body').append(logoutForm); |
| 242 |
logoutForm.submit(); |
| 243 |
|
| 244 |
return false; |
| 245 |
}); |
| 246 |
}); |
| 247 |
|