bridge = $bridge; } public function connect(string $token): array { $clean = strtoupper(trim($token)); if (!$clean) { return ['success' => false, 'error' => 'missing_token']; } $response = $this->bridge->api_post('/api/widget/connect', [ 'token' => $clean, 'site_url' => home_url('/'), 'wp_version' => get_bloginfo('version'), 'php_version' => phpversion(), ]); if (empty($response['success'])) { return [ 'success' => false, 'error' => $response['error'] ?? 'connect_failed', 'message' => $response['message'] ?? '', ]; } update_option('sitebirds_config', [ 'connected' => true, 'business_id' => $response['business_id'] ?? '', 'site_key' => $response['site_key'] ?? '', 'admin_code' => $response['admin_code'] ?? '', 'api_base' => $response['api_base'] ?? 'https://sitebirds.com', 'plan' => $response['plan'] ?? 'trial', 'features' => $response['features'] ?? [], 'connected_at' => gmdate('c'), ], false); $this->bridge->sync_config(); return ['success' => true]; } public function get_error_message(array $result): string { $error = (string) ($result['error'] ?? ''); $message = trim((string) ($result['message'] ?? '')); if ($message !== '') { return $message; } switch ($error) { case 'missing_token': return 'Paste a connect code from SiteBirds first.'; case 'token_invalid': return 'This connect code is not valid. Generate a new code in SiteBirds and try again.'; case 'token_expired': return 'This connect code has expired. Generate a new code in SiteBirds and try again.'; case 'token_used': return 'This connect code has already been used. Generate a new code in SiteBirds and try again.'; case 'network_error': return 'WordPress could not reach SiteBirds. Check outgoing requests, firewall rules, or hosting restrictions and try again.'; case 'missing_api_base': return 'SiteBirds is missing the API address. Disconnect the plugin and try connecting again.'; default: return 'Could not connect this WordPress site. Check the connect code and try again.'; } } public function disconnect(): void { update_option('sitebirds_config', [ 'connected' => false, 'business_id' => '', 'site_key' => '', 'admin_code' => '', 'api_base' => 'https://sitebirds.com', 'plan' => 'free', 'features' => [], ], false); delete_option('sitebirds_cloud_config'); } }