settings = $settings; $this->api = $api; } private function verify(): void { if (!current_user_can('manage_options')) { wp_send_json_error(array('message' => __('Unauthorized.', 'ai-integrations-chatbot')), 403); } check_ajax_referer('aiva_lite_admin', 'nonce'); } public function refresh_summary(): void { $this->verify(); $data = $this->api->summary(true); if (is_wp_error($data)) { wp_send_json_error(array('message' => $data->get_error_message()), 400); } $this->settings->apply_status($data); wp_send_json_success(array('reload' => true)); } public function test_connection(): void { $this->verify(); $data = $this->api->status(true); if (is_wp_error($data)) { wp_send_json_error(array('message' => $data->get_error_message()), 400); } $this->settings->apply_status($data); wp_send_json_success(array('reload' => true)); } public function poll_pairing(): void { $this->verify(); if (!$this->settings->get('pairing_id') || !$this->settings->get('pairing_secret')) { wp_send_json_error(array('message' => __('Start a connection first.', 'ai-integrations-chatbot')), 400); } $data = $this->api->poll_pairing(); if (is_wp_error($data)) { wp_send_json_error(array('message' => $data->get_error_message()), 400); } if (($data['status'] ?? '') === 'building' && !empty($data['launch_url'])) { if (strpos($data['launch_url'], $this->settings->service_url('autobuild') . '/wordpress/lite?') !== 0) { wp_send_json_error(array('message' => __('Invalid Autobuild response.', 'ai-integrations-chatbot')), 502); } wp_send_json_success(array('pending' => true, 'buildUrl' => $data['launch_url'])); } if (($data['status'] ?? '') !== 'approved') { wp_send_json_success(array('pending' => true)); } if (!$this->settings->mark_connected_from_payload($data)) { wp_send_json_error(array('message' => __('The connection response was incomplete. Please reconnect.', 'ai-integrations-chatbot')), 502); } // Never return the private site token or pairing secret to browser JavaScript. wp_send_json_success(array('redirectUrl' => admin_url('admin.php?page=aiva-lite-overview'))); } }