PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.7.1
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.7.1
2.7.1 2.7.2 2.7.0 2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 All 141 releases
metasync / admin / js / metasync-connect.js

metasync-connect.js in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.7.1, at admin/js/metasync-connect.js

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