get_metasync_page_slug(); if (!$page_slug) { return; } // Add settings section for Google Index add_settings_section( self::SECTION_GOOGLE_INDEX, '', // Empty title - we'll use dashboard card styling function(){}, // Empty callback $page_slug . '_general' ); // Add the main settings field add_settings_field( 'google_index_direct_config', 'Google Index API', array($this, 'render_settings_field'), $page_slug . '_general', self::SECTION_GOOGLE_INDEX ); } /** * Get MetaSync page slug */ private function get_metasync_page_slug() { if (class_exists('Metasync_Admin') && property_exists('Metasync_Admin', 'page_slug')) { return Metasync_Admin::$page_slug; } return 'searchatlas'; // fallback } /** * Render the Google Index settings field */ public function render_settings_field() { // Load Google Index functionality if (!function_exists('google_index_direct')) { require_once plugin_dir_path(__FILE__) . 'google-index-init.php'; } // Get current service account info (safe - doesn't expose private key) $google_index = google_index_direct(); $service_info = $google_index->get_service_account_info(); $is_configured = !isset($service_info['error']); // Include the settings field view include plugin_dir_path(__FILE__) . '../views/metasync-google-index-api-settings.php'; } /** * Process Google Index settings during MetaSync AJAX save * This runs early in the AJAX processing chain (priority 5) */ public function process_google_index_settings() { // Only process if our fields are present in the request if (!isset($_POST['google_index_service_account_json']) && !isset($_POST['google_index_clear_config']) && !isset($_FILES['google_index_service_account_file'])) { return; // No Google Index data to process } // Load Google Index functionality if (!function_exists('google_index_save_service_account')) { require_once plugin_dir_path(__FILE__) . 'google-index-init.php'; } $service_account_json = ''; // Get JSON from textarea if (isset($_POST['google_index_service_account_json'])) { $service_account_json = sanitize_textarea_field(wp_unslash($_POST['google_index_service_account_json'])); } // Override with file upload if provided if (isset($_FILES['google_index_service_account_file']) && !empty($_FILES['google_index_service_account_file']['tmp_name']) && file_exists($_FILES['google_index_service_account_file']['tmp_name'])) { $uploaded_json = file_get_contents($_FILES['google_index_service_account_file']['tmp_name']); if ($uploaded_json !== false) { $service_account_json = $uploaded_json; // Removed unnecessary wp_unslash } } // Handle clear configuration if (isset($_POST['google_index_clear_config'])) { delete_option('google_index_service_account'); // Success messages shown after redirect (to prevent interfering with MetaSync flow) $this->add_settings_notice('Service account configuration cleared successfully!', 'success'); } // Process service account JSON if provided elseif (!empty($service_account_json) && trim($service_account_json) !== '') { // Skip if it's just the placeholder text if (strpos($service_account_json, 'Service account configured') !== false) { return; // Don't process placeholder text } // Decode and validate JSON $service_account_data = json_decode($service_account_json, true); if (json_last_error() !== JSON_ERROR_NONE) { // JSON parsing error - send immediate error response $json_error_message = 'Invalid JSON format'; switch (json_last_error()) { case JSON_ERROR_SYNTAX: $json_error_message .= ' - Syntax error in JSON'; break; case JSON_ERROR_UTF8: $json_error_message .= ' - Invalid UTF-8 encoding'; break; default: $json_error_message .= ' - ' . json_last_error_msg(); break; } wp_send_json_error([ 'errors' => ['Google Index: ' . $json_error_message . '. Please check your service account JSON.'] ]); } if (!is_array($service_account_data)) { wp_send_json_error([ 'errors' => ['Google Index: JSON must be an object/array. Please check your service account JSON format.'] ]); } // Validate required fields $required_fields = ['type', 'project_id', 'private_key_id', 'private_key', 'client_email', 'client_id', 'auth_uri', 'token_uri']; $missing_fields = []; foreach ($required_fields as $field) { if (!isset($service_account_data[$field]) || empty($service_account_data[$field])) { $missing_fields[] = $field; } } if (!empty($missing_fields)) { wp_send_json_error([ 'errors' => ['Google Index: Missing required fields - ' . implode(', ', $missing_fields) . '. Please ensure you have a complete service account JSON.'] ]); } // Save using Google Index function $result = google_index_save_service_account($service_account_data); if ($result) { $this->add_settings_notice('Google Index service account configured successfully!', 'success'); } else { // Send immediate error response using MetaSync's expected format wp_send_json_error([ 'errors' => ['Google Index: Failed to save service account configuration. Please try again or check your JSON format.'] ]); } } } /** * Add a settings notice to be displayed after AJAX save * * @param string $message Notice message * @param string $type Notice type: 'success', 'error', 'warning', 'info' */ private function add_settings_notice($message, $type = 'info') { $notices = get_transient('google_index_admin_notices') ?: []; $notices[] = [ 'message' => $message, 'type' => $type, 'time' => time() ]; // Store notices for 30 seconds (enough time for page redirect) set_transient('google_index_admin_notices', $notices, 30); } /** * Display admin notices for Google Index settings */ public function display_admin_notices() { // Only show notices on MetaSync settings pages $page_slug = $this->get_metasync_page_slug(); $current_screen = get_current_screen(); if (!$current_screen || strpos($current_screen->id, $page_slug) === false) { return; } // Get and display notices $notices = get_transient('google_index_admin_notices'); if (!empty($notices)) { foreach ($notices as $notice) { $class = 'notice notice-' . esc_attr($notice['type']) . ' is-dismissible'; echo '
Google Index: ' . esc_html($notice['message']) . '
'; echo 'Testing connection...
โ Connection Test Results:
'; html += 'โ Test Failed: ' + response.data.message + '
โ Connection Error: Unable to perform test.