'application/json' ); if ( $with_auth ) { $token = get_option( self::OPT_TOKEN, '' ); if ( empty( $token ) ) { return new WP_Error( 'airabot_no_token', __( 'This site is not connected to Airabot yet.', 'airabot-chatbot' ) ); } // The backend reads this custom header, not Authorization. $headers['X-RAGCB-AUTH'] = $token; } $response = wp_remote_post( AIRABOT_API_BASE . '/' . $endpoint, array( 'timeout' => 20, 'headers' => $headers, 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'airabot_transport', sprintf( /* translators: %s: underlying network error message. */ __( 'Could not reach Airabot: %s', 'airabot-chatbot' ), $response->get_error_message() ) ); } $status = wp_remote_retrieve_response_code( $response ); $parsed = json_decode( wp_remote_retrieve_body( $response ), true ); // The connection was revoked (from the Airabot dashboard, or because // the account changed): forget it so the UI offers to reconnect. if ( 401 === $status ) { $this->disconnect_locally(); return new WP_Error( 'airabot_unauthorized', __( 'This site is no longer connected to Airabot. Please connect again.', 'airabot-chatbot' ) ); } if ( $status < 200 || $status >= 300 ) { $message = is_array( $parsed ) && ! empty( $parsed['error'] ) ? $parsed['error'] : sprintf( /* translators: %d: HTTP status code. */ __( 'Airabot returned an unexpected response (HTTP %d).', 'airabot-chatbot' ), $status ); return new WP_Error( 'airabot_http_error', $message ); } if ( ! is_array( $parsed ) ) { return new WP_Error( 'airabot_bad_response', __( 'Airabot returned a response this plugin could not read.', 'airabot-chatbot' ) ); } return $parsed; } /** * Exchanges the one-time code from the redirect for a lasting token. * * This runs server-to-server, so the token itself never appears in a URL, * the browser history, or a referrer header — only the single-use code does. * * @param string $code Code supplied by airabot.in as ?airabot_code=… * @return true|WP_Error */ public function exchange_code( $code ) { $result = $this->request( 'ragcb-site-connect', array( 'action' => 'exchange', 'code' => $code, 'site_url' => home_url(), ), false ); if ( is_wp_error( $result ) ) { return $result; } if ( empty( $result['success'] ) || empty( $result['token'] ) ) { return new WP_Error( 'airabot_exchange_failed', ! empty( $result['error'] ) ? $result['error'] : __( 'Could not complete the connection. Please try again.', 'airabot-chatbot' ) ); } update_option( self::OPT_TOKEN, $result['token'], false ); $user = isset( $result['user'] ) && is_array( $result['user'] ) ? $result['user'] : array(); update_option( self::OPT_USER, array( 'user_id' => isset( $user['user_id'] ) ? $user['user_id'] : '', 'email' => isset( $user['email'] ) ? $user['email'] : '', 'name' => isset( $user['name'] ) ? $user['name'] : '', ), false ); update_option( self::OPT_SITE, home_url(), false ); return true; } /** * Checks the stored connection is still valid. * * @return bool */ public function verify_connection() { if ( ! $this->is_connected() ) { return false; } $result = $this->request( 'ragcb-site-connect', array( 'action' => 'verify', 'token' => get_option( self::OPT_TOKEN, '' ) ), false ); if ( is_wp_error( $result ) ) { // A rejected token has already been cleared by request(); a network // blip should not masquerade as a broken connection, so only an // explicit rejection counts as "not connected". return 'airabot_unauthorized' !== $result->get_error_code() && $this->is_connected(); } return ! empty( $result['success'] ); } /** * Fetches every chatbot on the connected account. * * The backend caps `limit` at 50 per page, so this walks the pages rather * than assuming one request covers the account. * * @return array|WP_Error List of bot rows. */ public function list_bots() { $bots = array(); $page = 1; $max_page = 1; do { $result = $this->request( 'ragcb-bots', array( 'action' => 'list', 'page' => $page, 'limit' => 50, ) ); if ( is_wp_error( $result ) ) { return $result; } if ( ! empty( $result['result'] ) && is_array( $result['result'] ) ) { $bots = array_merge( $bots, $result['result'] ); } if ( isset( $result['pages'] ) ) { $max_page = (int) $result['pages']; } $page++; // Hard stop: never let a malformed `pages` value spin this loop. } while ( $page <= $max_page && $page <= 20 ); return $bots; } /** * Disconnects this site, telling Airabot to revoke the token as well. */ public function disconnect() { $token = get_option( self::OPT_TOKEN, '' ); if ( ! empty( $token ) ) { // Best-effort: the local credentials are cleared regardless, so a // failed call here cannot leave the site stuck in a connected state. $this->request( 'ragcb-site-connect', array( 'action' => 'disconnect', 'token' => $token ), false ); } $this->disconnect_locally(); } /** * Forgets the connection on this site only. */ private function disconnect_locally() { delete_option( self::OPT_TOKEN ); delete_option( self::OPT_USER ); delete_option( self::OPT_SITE ); } /** * Whether a connection token is stored. * * @return bool */ public function is_connected() { return '' !== get_option( self::OPT_TOKEN, '' ); } /** * The connected Airabot account details, for display. * * @return array */ public function get_user() { $user = get_option( self::OPT_USER, array() ); return is_array( $user ) ? $user : array(); } }