PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.7.2
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.7.2
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
← All changes | google-index/class-google-index-admin.php +317 -142 2.6.25 → 2.7.2 View file →
@@ -13,16 +13,24 @@
13 13 if (!defined('ABSPATH')) {
14 14 exit;
15 15 }
16 16
17 -class Google_Index_Admin
17 +class Google_Index_Admin
18 18 {
19 19 /**
20 20 * Section ID for Google Index settings
21 21 */
22 22 private const SECTION_GOOGLE_INDEX = 'google_index_direct_settings';
23 -
23 +
24 24 /**
25 + * Placeholder the redacted display JSON uses in place of the private key.
26 + *
27 + * Its presence means the textarea still holds what the server rendered, so
28 + * the submission carries no new credential and must leave storage alone.
29 + */
30 + private const REDACTED_MARKER = '-----REDACTED-----';
31 +
32 + /**
25 33 * Initialize admin functionality
26 34 */
27 35 public function __construct()
28 36 {
@@ -28,16 +36,16 @@
28 36 {
29 37 // Hook into MetaSync admin initialization
30 38 add_action('admin_init', array($this, 'add_settings_to_metasync'), 20);
31 39 add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
32 -
40 +
33 41 // Hook into MetaSync's AJAX settings processing
34 42 add_action('wp_ajax_meta_sync_save_settings', array($this, 'process_google_index_settings'), 5);
35 43 add_action('wp_ajax_meta_sync_save_seo_controls', array($this, 'process_google_index_settings'), 5);
36 -
44 +
37 45 // Display admin notices after redirect
38 46 add_action('admin_notices', array($this, 'display_admin_notices'));
39 -
47 +
40 48 // AJAX handlers
41 49 add_action('wp_ajax_metasync_google_index_direct_test', array($this, 'ajax_test_connection'));
42 50 }
43 51
@@ -96,9 +104,9 @@
96 104 if (!function_exists('google_index_direct')) {
97 105 if (file_exists(plugin_dir_path(__FILE__) . 'google-index-init.php')) {
98 106 require_once plugin_dir_path(__FILE__) . 'google-index-init.php';
99 107 } else {
100 - error_log('MetaSync Google Index: google-index-init.php not found at ' . plugin_dir_path(__FILE__) . 'google-index-init.php');
108 + error_log('MetaSync Google Index: google-index-init.php not found at ' . plugin_dir_path(__FILE__) . 'google-index-init.php'); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets
101 109 return;
102 110 }
103 111 }
104 112
@@ -103,8 +111,12 @@
103 111 }
104 112
105 113 // Get current service account info (safe - doesn't expose private key)
106 114 $google_index = google_index_direct();
115 + if (!$google_index) {
116 + // Nothing to render against, and calling through would be fatal.
117 + return;
118 + }
107 119 $service_info = $google_index->get_service_account_info();
108 120 $is_configured = !isset($service_info['error']);
109 121
110 122 $saved_json_display = $is_configured ? $google_index->get_redacted_config_json() : '';
@@ -119,11 +131,11 @@
119 131 */
120 132 public function process_google_index_settings()
121 133 {
122 134 // Only process if our fields are present in the request
123 - if (!isset($_POST['google_index_service_account_json']) &&
124 - !isset($_POST['google_index_clear_config']) &&
125 - !isset($_FILES['google_index_service_account_file'])) {
135 + if (!isset($_POST['google_index_service_account_json']) && // phpcs:ignore WordPress.Security.NonceVerification.Missing -- early-return checks, a nonce is verified before any write
136 + !isset($_POST['google_index_clear_config']) && // phpcs:ignore WordPress.Security.NonceVerification.Missing -- early-return checks, a nonce is verified before any write
137 + !isset($_FILES['google_index_service_account_file'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- early-return checks, a nonce is verified before any write
126 138 return; // No Google Index data to process
127 139 }
128 140
129 141 if (!$this->authorize_settings_request()) {
@@ -130,106 +142,201 @@
130 142 return;
131 143 }
132 144
133 145 // Clearing does not need to load or inspect credential contents.
134 - if (isset($_POST['google_index_clear_config'])) {
135 - delete_option('google_index_service_account');
146 + if (isset($_POST['google_index_clear_config'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- authorize_settings_request() verified nonce and capability
147 + $this->clear_stored_service_account();
136 148 $this->add_settings_notice('Service account configuration cleared successfully!', 'success');
137 149 return;
138 150 }
139 -
151 +
152 + $result = $this->persist_submitted_service_account();
153 +
154 + if ($result['status'] === 'error') {
155 + // The parent save is still mid-flight; aborting here with an error
156 + // keeps a bad credential from being reported as a successful save.
157 + wp_send_json_error([
158 + 'errors' => ['Google Index: ' . $result['message']],
159 + 'message' => 'Google Index: ' . $result['message'],
160 + ]);
161 + }
162 +
163 + if ($result['status'] === 'unavailable') {
164 + // The sub-module is missing. Let the rest of the settings save
165 + // finish and surface this as a notice instead of failing the page.
166 + $this->add_settings_notice($result['message'], 'warning');
167 + return;
168 + }
169 +
170 + if ($result['status'] === 'saved') {
171 + $this->add_settings_notice('Google Index service account configured successfully!', 'success');
172 + }
173 + }
174 +
175 + /**
176 + * Read the submitted service account from $_POST/$_FILES, validate it, and
177 + * persist it when it is a genuinely new credential.
178 + *
179 + * Returns a status rather than emitting a response because the caller runs
180 + * inside another form's save: it must abort the parent save on a bad
181 + * credential, not answer for the whole request.
182 + *
183 + * @return array{status:string, message:string} status is one of:
184 + * 'saved' - a new credential was written
185 + * 'unchanged' - nothing to write (empty, or the redacted display text)
186 + * 'error' - validation or persistence failed; message explains why
187 + */
188 + private function persist_submitted_service_account()
189 + {
140 190 // Load Google Index functionality
141 - if (!function_exists('google_index_save_service_account')) {
191 + if (!$this->module_function_available('google_index_save_service_account')) {
142 192 if (file_exists(plugin_dir_path(__FILE__) . 'google-index-init.php')) {
143 193 require_once plugin_dir_path(__FILE__) . 'google-index-init.php';
144 - } else {
145 - error_log('MetaSync Google Index: google-index-init.php not found at ' . plugin_dir_path(__FILE__) . 'google-index-init.php');
146 - return;
147 194 }
195 +
196 + // A partial deploy can leave the file present but the function
197 + // undefined, so re-check rather than assuming the require worked.
198 + if (!$this->module_function_available('google_index_save_service_account')) {
199 + error_log('MetaSync Google Index: google_index_save_service_account() unavailable; expected google-index-init.php at ' . plugin_dir_path(__FILE__) . 'google-index-init.php'); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets
200 +
201 + // Deliberately NOT 'error'. This optional sub-module failing to
202 + // load must not abort the parent settings save — the embedded
203 + // caller would turn that into a wp_send_json_error() and take
204 + // the whole General Settings / Indexation Control page down.
205 + return ['status' => 'unavailable', 'message' => 'The Google Index module could not be loaded, so the service account was not saved.'];
206 + }
148 207 }
149 -
208 +
150 209 $service_account_json = '';
151 -
210 +
152 211 // Get JSON from textarea
153 - if (isset($_POST['google_index_service_account_json'])) {
154 - $service_account_json = sanitize_textarea_field(wp_unslash($_POST['google_index_service_account_json']));
212 + if (isset($_POST['google_index_service_account_json'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- private, callers verify a nonce and capability first
213 + $service_account_json = sanitize_textarea_field(wp_unslash($_POST['google_index_service_account_json'])); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- private, callers verify a nonce and capability first
155 214 }
156 -
215 +
157 216 // Override with file upload if provided
158 - if (isset($_FILES['google_index_service_account_file']) &&
159 - !empty($_FILES['google_index_service_account_file']['tmp_name']) &&
160 - file_exists($_FILES['google_index_service_account_file']['tmp_name'])) {
161 -
162 - $uploaded_json = file_get_contents($_FILES['google_index_service_account_file']['tmp_name']);
217 + if (isset($_FILES['google_index_service_account_file']) && // phpcs:ignore WordPress.Security.NonceVerification.Missing -- private, callers verify a nonce and capability first
218 + !empty($_FILES['google_index_service_account_file']['tmp_name']) && // phpcs:ignore WordPress.Security.NonceVerification.Missing -- private, callers verify a nonce and capability first
219 + file_exists($_FILES['google_index_service_account_file']['tmp_name'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- private, callers verify a nonce and capability first
220 +
221 + $uploaded_json = file_get_contents($_FILES['google_index_service_account_file']['tmp_name']); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- private, callers verify a nonce and capability first
163 222 if ($uploaded_json !== false) {
164 223 $service_account_json = $uploaded_json; // Removed unnecessary wp_unslash
165 224 }
166 225 }
167 -
168 - // Process service account JSON if provided
169 - if (!empty($service_account_json) && trim($service_account_json) !== '') {
170 -
171 - // Skip if it contains redacted private key (user didn't paste new JSON)
172 - if (strpos($service_account_json, '-----REDACTED-----') !== false) {
173 - return; // Don't process redacted display text
226 +
227 + if (empty($service_account_json) || trim($service_account_json) === '') {
228 + return ['status' => 'unchanged', 'message' => ''];
229 + }
230 +
231 + // Skip if it contains redacted private key (user didn't paste new JSON)
232 + if (strpos($service_account_json, self::REDACTED_MARKER) !== false) {
233 + return ['status' => 'unchanged', 'message' => ''];
234 + }
235 +
236 + $validation = $this->validate_service_account_json($service_account_json);
237 + if (!empty($validation['error'])) {
238 + return ['status' => 'error', 'message' => $validation['error']];
239 + }
240 +
241 + if (!google_index_save_service_account($validation['data'])) {
242 + return [
243 + 'status' => 'error',
244 + 'message' => 'Failed to save service account configuration. Please try again or check your JSON format.',
245 + ];
246 + }
247 +
248 + return ['status' => 'saved', 'message' => 'Google Index service account configured successfully!'];
249 + }
250 +
251 + /**
252 + * Whether a function the optional Google Index sub-module provides exists.
253 + *
254 + * Taking the name as a parameter keeps the check opaque to static analysis,
255 + * which otherwise treats the second call in a load-then-verify pair as dead
256 + * because it cannot see that the intervening require_once may define it.
257 + *
258 + * Genuinely impure: the same argument yields a different answer once the
259 + * sub-module has been required, which is precisely why it is called twice.
260 + *
261 + * @phpstan-impure
262 + *
263 + * @param string $name Function name to look for.
264 + * @return bool
265 + */
266 + private function module_function_available($name)
267 + {
268 + return function_exists($name);
269 + }
270 +
271 + /**
272 + * Decode and structurally validate a service account JSON string.
273 + *
274 + * @param string $json Raw JSON text.
275 + * @return array{data:array|null, error:string} error is '' when valid.
276 + */
277 + private function validate_service_account_json($json)
278 + {
279 + $data = json_decode($json, true);
280 +
281 + if (json_last_error() !== JSON_ERROR_NONE) {
282 + $message = 'Invalid JSON format';
283 + switch (json_last_error()) {
284 + case JSON_ERROR_SYNTAX:
285 + $message .= ' - Syntax error in JSON';
286 + break;
287 + case JSON_ERROR_UTF8:
288 + $message .= ' - Invalid UTF-8 encoding';
289 + break;
290 + default:
291 + $message .= ' - ' . json_last_error_msg();
292 + break;
174 293 }
175 -
176 - // Decode and validate JSON
177 - $service_account_data = json_decode($service_account_json, true);
178 -
179 - if (json_last_error() !== JSON_ERROR_NONE) {
180 - // JSON parsing error - send immediate error response
181 - $json_error_message = 'Invalid JSON format';
182 - switch (json_last_error()) {
183 - case JSON_ERROR_SYNTAX:
184 - $json_error_message .= ' - Syntax error in JSON';
185 - break;
186 - case JSON_ERROR_UTF8:
187 - $json_error_message .= ' - Invalid UTF-8 encoding';
188 - break;
189 - default:
190 - $json_error_message .= ' - ' . json_last_error_msg();
191 - break;
192 - }
193 -
194 - wp_send_json_error([
195 - 'errors' => ['Google Index: ' . $json_error_message . '. Please check your service account JSON.']
196 - ]);
294 +
295 + return ['data' => null, 'error' => $message . '. Please check your service account JSON.'];
296 + }
297 +
298 + if (!is_array($data)) {
299 + return [
300 + 'data' => null,
301 + 'error' => 'JSON must be an object/array. Please check your service account JSON format.',
302 + ];
303 + }
304 +
305 + $required_fields = ['type', 'project_id', 'private_key_id', 'private_key', 'client_email', 'client_id', 'auth_uri', 'token_uri'];
306 + $missing_fields = [];
307 +
308 + foreach ($required_fields as $field) {
309 + if (!isset($data[$field]) || empty($data[$field])) {
310 + $missing_fields[] = $field;
197 311 }
198 -
199 - if (!is_array($service_account_data)) {
200 - wp_send_json_error([
201 - 'errors' => ['Google Index: JSON must be an object/array. Please check your service account JSON format.']
202 - ]);
312 + }
313 +
314 + if (!empty($missing_fields)) {
315 + return [
316 + 'data' => null,
317 + 'error' => 'Missing required fields - ' . implode(', ', $missing_fields) . '. Please ensure you have a complete service account JSON.',
318 + ];
319 + }
320 +
321 + return ['data' => $data, 'error' => ''];
322 + }
323 +
324 + /**
325 + * Remove the stored service account and any token minted from it.
326 + *
327 + * A cached access token that outlives the credential it was issued for
328 + * would keep working until it expired.
329 + */
330 + private function clear_stored_service_account()
331 + {
332 + delete_option('google_index_service_account');
333 +
334 + if (function_exists('google_index_direct')) {
335 + $instance = google_index_direct();
336 + if ($instance) {
337 + $instance->clear_token_cache();
203 338 }
204 -
205 - // Validate required fields
206 - $required_fields = ['type', 'project_id', 'private_key_id', 'private_key', 'client_email', 'client_id', 'auth_uri', 'token_uri'];
207 - $missing_fields = [];
208 -
209 - foreach ($required_fields as $field) {
210 - if (!isset($service_account_data[$field]) || empty($service_account_data[$field])) {
211 - $missing_fields[] = $field;
212 - }
213 - }
214 -
215 - if (!empty($missing_fields)) {
216 - wp_send_json_error([
217 - 'errors' => ['Google Index: Missing required fields - ' . implode(', ', $missing_fields) . '. Please ensure you have a complete service account JSON.']
218 - ]);
219 - }
220 -
221 - // Save using Google Index function
222 - $result = google_index_save_service_account($service_account_data);
223 -
224 - if ($result) {
225 - $this->add_settings_notice('Google Index service account configured successfully!', 'success');
226 - } else {
227 - // Send immediate error response using MetaSync's expected format
228 - wp_send_json_error([
229 - 'errors' => ['Google Index: Failed to save service account configuration. Please try again or check your JSON format.']
230 - ]);
231 - }
232 339 }
233 340 }
234 341
235 342 /**
@@ -270,10 +377,10 @@
270 377 // Get and display notices
271 378 $notices = get_transient('google_index_admin_notices');
272 379 if (!empty($notices)) {
273 380 foreach ($notices as $notice) {
274 - $class = 'notice notice-' . esc_attr($notice['type']) . ' is-dismissible';
275 - echo '<div class="' . $class . '">';
381 + $class = 'notice notice-' . $notice['type'] . ' is-dismissible';
382 + echo '<div class="' . esc_attr($class) . '">';
276 383 echo '<p><strong>Google Index:</strong> ' . esc_html($notice['message']) . '</p>';
277 384 echo '</div>';
278 385 }
279 386
@@ -297,9 +404,9 @@
297 404 if (!function_exists('google_index_direct')) {
298 405 if (file_exists(plugin_dir_path(__FILE__) . 'google-index-init.php')) {
299 406 require_once plugin_dir_path(__FILE__) . 'google-index-init.php';
300 407 } else {
301 - error_log('MetaSync Google Index: google-index-init.php not found at ' . plugin_dir_path(__FILE__) . 'google-index-init.php');
408 + error_log('MetaSync Google Index: google-index-init.php not found at ' . plugin_dir_path(__FILE__) . 'google-index-init.php'); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets
302 409 return;
303 410 }
304 411 }
305 412
@@ -361,15 +468,14 @@
361 468 $page_slug = $this->get_metasync_page_slug();
362 469 if (strpos($hook, $page_slug) === false) {
363 470 return;
364 471 }
365 -
366 - // Only load on general tab (default tab when none specified)
367 - $current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'general';
368 - if ($current_tab !== 'general') {
472 +
473 + $current_tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'general'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only page and tab check to enqueue scripts
474 + if ($current_tab !== 'general' && strpos($hook, 'seo-controls') === false) {
369 475 return;
370 476 }
371 -
477 +
372 478 // Add inline JavaScript for functionality
373 479 wp_add_inline_script('jquery', $this->get_admin_javascript());
374 480 }
375 481
@@ -383,61 +489,130 @@
383 489 $nonce = wp_create_nonce('metasync_google_index_direct_test');
384 490
385 491 return "
386 492 jQuery(document).ready(function($) {
493 + // What the textarea held immediately before a file overwrote it.
494 + // Seeded from the server-rendered value and re-snapshotted on each
495 + // file pick, so 'Remove' undoes exactly the file selection — it
496 + // does not also discard a credential the user typed by hand before
497 + // mis-clicking Choose File.
498 + var originalJsonValue = $('#google_index_service_account_json').val();
499 +
500 + // Test Connection builds its results list as an HTML string. The
501 + // values come from the API/server rather than the page, so escape
502 + // them before they reach that sink.
503 + function escHtml(value) {
504 + return $('<div>').text(value === undefined || value === null ? '' : value).html();
505 + }
506 +
507 + function showFileMessage(text, type) {
508 + var box = $('#google-index-file-messages');
509 + if (!box.length) { return; }
510 + box.empty();
511 + if (!text) { return; }
512 + var notice = $('<div>').addClass('notice notice-' + type + ' inline');
513 + notice.append($('<p>').text(text));
514 + box.append(notice);
515 + }
516 +
517 + function clearSelectedFile(restoreTextarea) {
518 + var input = $('#google_index_service_account_file');
519 + if (input.length) {
520 + input.val('');
521 + }
522 + $('#google-index-selected-file').hide();
523 + $('#google-index-selected-file-name').text('');
524 + showFileMessage('', 'info');
525 +
526 + if (restoreTextarea) {
527 + var textarea = $('#google_index_service_account_json');
528 + textarea.val(originalJsonValue);
529 + textarea.trigger('input').trigger('change');
530 + $('#metaSyncGeneralSetting, #metaSyncSeoControlsForm').trigger('change');
531 + }
532 + }
533 +
387 534 // Integration with MetaSync's unsaved changes detection
388 535 function integrateWithUnsavedChangesDetection() {
389 536 // Monitor textarea for changes (avoid recursion)
390 537 $('#google_index_service_account_json').on('input change paste keyup', function(e) {
391 - console.log('Google Index: Textarea changed via', e.type);
392 538 // Trigger MetaSync's change detection (works on both Settings and Indexation Control pages)
393 539 $('#metaSyncGeneralSetting, #metaSyncSeoControlsForm').trigger('change');
394 540 });
395 -
541 +
396 542 // Handle file upload with auto-populate and change detection
397 543 $('#google_index_service_account_file').on('change', function(e) {
398 544 var file = e.target.files[0];
399 545 var textarea = $('#google_index_service_account_json');
400 -
401 - if (file && file.name.toLowerCase().endsWith('.json')) {
402 - var reader = new FileReader();
403 - reader.onload = function(e) {
404 - try {
405 - var json = JSON.parse(e.target.result);
406 - // Update textarea value
407 - var jsonString = JSON.stringify(json, null, 2);
408 - textarea.val(jsonString);
409 -
410 - // Create and dispatch native events to ensure proper detection
411 - setTimeout(function() {
412 - // Create native events
413 - var inputEvent = new Event('input', { bubbles: true, cancelable: true });
414 - var changeEvent = new Event('change', { bubbles: true, cancelable: true });
415 -
416 - // Dispatch events on the actual DOM element (not jQuery)
417 - textarea[0].dispatchEvent(inputEvent);
418 - textarea[0].dispatchEvent(changeEvent);
419 -
420 - // Also trigger jQuery events as backup
421 - textarea.trigger('input').trigger('change');
422 -
423 - console.log('Google Index: File upload triggered change detection');
424 - }, 100);
425 - } catch (err) {
426 - alert('Invalid JSON file selected.');
427 - }
428 - };
429 - reader.readAsText(file);
430 - } else {
431 - // File upload indicates change even if not JSON (works on both Settings and Indexation Control pages)
432 - $('#metaSyncGeneralSetting, #metaSyncSeoControlsForm').trigger('change');
546 +
547 + if (!file) {
548 + clearSelectedFile(true);
549 + return;
433 550 }
551 +
552 + if (!file.name.toLowerCase().endsWith('.json')) {
553 + // Inline feedback instead of the old blocking alert().
554 + showFileMessage('\"' + file.name + '\" is not a .json file. Choose the service account key file you downloaded from Google Cloud.', 'error');
555 + $('#google-index-selected-file').hide();
556 + $('#google_index_service_account_file').val('');
557 + return;
558 + }
559 +
560 + // Snapshot only when no file is currently selected, i.e. at
561 + // the START of a pick sequence. Re-snapshotting on every
562 + // pick would make Remove restore file A's contents after
563 + // the user picked A then B then removed — leaving a
564 + // credential in the box while the UI says no file is
565 + // selected.
566 + if (!$('#google-index-selected-file').is(':visible')) {
567 + originalJsonValue = textarea.val();
568 + }
569 +
570 + var reader = new FileReader();
571 + reader.onload = function(ev) {
572 + try {
573 + var json = JSON.parse(ev.target.result);
574 + var jsonString = JSON.stringify(json, null, 2);
575 + textarea.val(jsonString);
576 +
577 + $('#google-index-selected-file-name').text(file.name);
578 + $('#google-index-selected-file').show();
579 + showFileMessage('\"' + file.name + '\" loaded. Save the page to store it.', 'info');
580 +
581 + // Create and dispatch native events to ensure proper detection
582 + setTimeout(function() {
583 + var inputEvent = new Event('input', { bubbles: true, cancelable: true });
584 + var changeEvent = new Event('change', { bubbles: true, cancelable: true });
585 +
586 + textarea[0].dispatchEvent(inputEvent);
587 + textarea[0].dispatchEvent(changeEvent);
588 +
589 + textarea.trigger('input').trigger('change');
590 + }, 100);
591 + } catch (err) {
592 + showFileMessage('\"' + file.name + '\" is not valid JSON. Re-download the key file from Google Cloud and try again.', 'error');
593 + $('#google-index-selected-file').hide();
594 + $('#google_index_service_account_file').val('');
595 + }
596 + };
597 + reader.onerror = function() {
598 + showFileMessage('\"' + file.name + '\" could not be read. Try choosing it again.', 'error');
599 + $('#google-index-selected-file').hide();
600 + $('#google_index_service_account_file').val('');
601 + };
602 + reader.readAsText(file);
434 603 });
604 +
605 + // Remove/reselect
606 + $('#google-index-remove-file').on('click', function(e) {
607 + e.preventDefault();
608 + clearSelectedFile(true);
609 + });
435 610 }
436 -
611 +
437 612 // Initialize integration after a small delay to ensure MetaSync is ready
438 613 setTimeout(integrateWithUnsavedChangesDetection, 100);
439 -
614 +
440 615 // Handle test connection button
441 616 $('#google-index-test-connection').on('click', function(e) {
442 617 e.preventDefault();
443 618
@@ -466,10 +641,10 @@
466 641 html += '<li><strong>Token Cached:</strong> ' + (results.token_test.cached ? '✅ Yes' : '⚪ No') + '</li>';
467 642 }
468 643
469 644 if (results.credentials_test) {
470 - html += '<li><strong>Service Account:</strong> ' + results.credentials_test.client_email + '</li>';
471 - html += '<li><strong>Project ID:</strong> ' + results.credentials_test.project_id + '</li>';
645 + html += '<li><strong>Service Account:</strong> ' + escHtml(results.credentials_test.client_email) + '</li>';
646 + html += '<li><strong>Project ID:</strong> ' + escHtml(results.credentials_test.project_id) + '</li>';
472 647 html += '<li><strong>Private Key:</strong> ' + (results.credentials_test.has_private_key ? '✅ Present' : '❌ Missing') + '</li>';
473 648 }
474 649
475 650 if (results.homepage_test) {
@@ -475,11 +650,11 @@
475 650 if (results.homepage_test) {
476 651 if (results.homepage_test.success) {
477 652 html += '<li><strong>Homepage Status:</strong> ✅ Success</li>';
478 653 } else {
479 - html += '<li><strong>Homepage Status:</strong> ⚠️ ' + results.homepage_test.error.message + '</li>';
654 + html += '<li><strong>Homepage Status:</strong> ⚠️ ' + escHtml(results.homepage_test.error.message) + '</li>';
480 655 if (results.homepage_test.note) {
481 - html += '<li><strong>Note:</strong> ' + results.homepage_test.note + '</li>';
656 + html += '<li><strong>Note:</strong> ' + escHtml(results.homepage_test.note) + '</li>';
482 657 }
483 658 }
484 659 }
485 660
@@ -485,9 +660,9 @@
485 660
486 661 html += '</ul></div>';
487 662 resultDiv.html(html);
488 663 } else {
489 - resultDiv.html('<div class=\"notice notice-error inline\"><p><strong>❌ Test Failed:</strong> ' + response.data.message + '</p></div>');
664 + resultDiv.html('<div class=\"notice notice-error inline\"><p><strong>❌ Test Failed:</strong> ' + escHtml(response.data.message) + '</p></div>');
490 665 }
491 666 },
492 667 error: function() {
493 668 resultDiv.html('<div class=\"notice notice-error inline\"><p><strong>❌ Connection Error:</strong> Unable to perform test.</p></div>');
@@ -500,19 +675,19 @@
500 675
501 676 // Handle clear configuration button
502 677 $('#google-index-clear-config').on('click', function(e) {
503 678 e.preventDefault();
504 -
679 +
505 680 if (!confirm('Are you sure you want to clear the service account configuration?')) {
506 681 return;
507 682 }
508 -
509 - // Create hidden input to indicate clear configuration request
683 +
684 + // Embedded contexts: piggyback on the page's settings form.
510 685 var input = $('<input>')
511 686 .attr('type', 'hidden')
512 687 .attr('name', 'google_index_clear_config')
513 688 .attr('value', '1');
514 -
689 +
515 690 // Append to the correct form (Indexation Control vs General Settings) and submit via AJAX
516 691 var clrForm = $('#metaSyncSeoControlsForm').length ? $('#metaSyncSeoControlsForm') : $('#metaSyncGeneralSetting');
517 692 var clrAction = $('#metaSyncSeoControlsForm').length ? 'meta_sync_save_seo_controls' : 'meta_sync_save_settings';
518 693 clrForm.append(input);