$chatbot_id, 'embed_token' => $embed_token, ); } /** * Credentials + site_url for every studio call. * * @return array|WP_Error */ public static function auth_params() { $chatbot_id = ''; $embed_token = ''; if ( is_array( self::$request_creds ) ) { $chatbot_id = (string) ( self::$request_creds['chatbot_id'] ?? '' ); $embed_token = (string) ( self::$request_creds['embed_token'] ?? '' ); } if ( '' === $chatbot_id || '' === $embed_token ) { $stored = Aanya_Connection::get_credentials(); $chatbot_id = $stored['chatbot_id']; $embed_token = $stored['embed_token']; } if ( '' === $chatbot_id || '' === $embed_token ) { return new WP_Error( 'aanya_not_connected', __( 'Not connected to AANYA. Click Uninstall, then Authorize with AANYA again.', 'aanya-ai-chatbot' ) ); } // Heal options table if AJAX had creds but storage was empty/stale. if ( ! Aanya_Connection::is_connected() ) { Aanya_Connection::save( $chatbot_id, $embed_token ); } return array( 'chatbot_id' => $chatbot_id, 'embed_token' => $embed_token, 'site_url' => Aanya_Connection::site_url(), ); } /** * Perform a JSON API request. * * @param string $method HTTP method. * @param string $path Path under studio base (no leading slash required). * @param array $body JSON/query body (auth merged automatically). * @param array $args Extra wp_remote_* args. * @return array|WP_Error Decoded JSON body. */ public static function request( $method, $path = '', $body = array(), $args = array() ) { $auth = self::auth_params(); if ( is_wp_error( $auth ) ) { return $auth; } $method = strtoupper( (string) $method ); $path = ltrim( (string) $path, '/' ); $url = self::studio_base(); if ( '' !== $path ) { $url = trailingslashit( $url ) . $path; } $payload = array_merge( $auth, is_array( $body ) ? $body : array() ); $headers = array( 'Accept' => 'application/json', 'X-Aanya-Embed-Token' => $auth['embed_token'], ); $request_args = array( 'method' => $method, 'timeout' => isset( $args['timeout'] ) ? (int) $args['timeout'] : 45, 'headers' => $headers, 'sslverify' => true, 'user-agent'=> 'AanyaWordPress/' . ( defined( 'AANYA_CHATBOT_VERSION' ) ? AANYA_CHATBOT_VERSION : '1.0' ) . '; ' . home_url( '/' ), ); if ( 'GET' === $method || 'DELETE' === $method ) { $url = add_query_arg( $payload, $url ); } else { $headers['Content-Type'] = 'application/json'; $request_args['headers'] = $headers; $request_args['body'] = wp_json_encode( $payload ); } if ( ! empty( $args['headers'] ) && is_array( $args['headers'] ) ) { $request_args['headers'] = array_merge( $request_args['headers'], $args['headers'] ); } $response = wp_remote_request( $url, $request_args ); return self::parse_response( $response ); } /** * Multipart upload (documents / logo). * * @param string $path Studio path. * @param array $fields Extra fields. * @param string $file_key Form field name. * @param string $file_path Absolute file path. * @param string $filename Original filename. * @param string $mime Mime type. * @return array|WP_Error */ public static function upload( $path, $fields, $file_key, $file_path, $filename, $mime = 'application/octet-stream' ) { $auth = self::auth_params(); if ( is_wp_error( $auth ) ) { return $auth; } if ( ! is_readable( $file_path ) ) { return new WP_Error( 'aanya_upload_missing', __( 'Upload file is not readable.', 'aanya-ai-chatbot' ) ); } $boundary = wp_generate_password( 24, false, false ); $body = ''; $payload = array_merge( $auth, is_array( $fields ) ? $fields : array() ); foreach ( $payload as $key => $value ) { if ( is_array( $value ) ) { foreach ( $value as $sub ) { $body .= '--' . $boundary . "\r\n"; $body .= 'Content-Disposition: form-data; name="' . $key . "[]\"\r\n\r\n"; $body .= (string) $sub . "\r\n"; } continue; } $body .= '--' . $boundary . "\r\n"; $body .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n"; $body .= (string) $value . "\r\n"; } $file_contents = file_get_contents( $file_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( false === $file_contents ) { return new WP_Error( 'aanya_upload_read', __( 'Could not read upload file.', 'aanya-ai-chatbot' ) ); } $body .= '--' . $boundary . "\r\n"; $body .= 'Content-Disposition: form-data; name="' . $file_key . '"; filename="' . basename( $filename ) . "\"\r\n"; $body .= 'Content-Type: ' . $mime . "\r\n\r\n"; $body .= $file_contents . "\r\n"; $body .= '--' . $boundary . "--\r\n"; $url = trailingslashit( self::studio_base() ) . ltrim( (string) $path, '/' ); $response = wp_remote_post( $url, array( 'timeout' => 90, 'sslverify' => true, 'user-agent' => 'AanyaWordPress/' . ( defined( 'AANYA_CHATBOT_VERSION' ) ? AANYA_CHATBOT_VERSION : '1.0' ) . '; ' . home_url( '/' ), 'headers' => array( 'Accept' => 'application/json', 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, 'X-Aanya-Embed-Token' => $auth['embed_token'], ), 'body' => $body, ) ); return self::parse_response( $response ); } /** * @param array|WP_Error $response Remote response. * @return array|WP_Error */ private static function parse_response( $response ) { if ( is_wp_error( $response ) ) { $message = $response->get_error_message(); return new WP_Error( 'aanya_api_http', sprintf( /* translators: %s: transport error from WordPress HTTP API */ __( 'Could not reach AANYA (%s). Check that this server can make outbound HTTPS requests to www.aanya.ai.', 'aanya-ai-chatbot' ), $message ) ); } $code = (int) wp_remote_retrieve_response_code( $response ); $raw = wp_remote_retrieve_body( $response ); $data = json_decode( $raw, true ); if ( ! is_array( $data ) ) { $data = array(); } if ( $code < 200 || $code >= 300 ) { $message = isset( $data['error'] ) ? (string) $data['error'] : __( 'AANYA API request failed.', 'aanya-ai-chatbot' ); if ( isset( $data['message'] ) && is_string( $data['message'] ) && '' !== $data['message'] ) { $message = $data['message']; } if ( 401 === $code || 403 === $code ) { $message .= ' ' . __( 'Try Uninstall, then Authorize with AANYA again.', 'aanya-ai-chatbot' ); } return new WP_Error( 'aanya_api_http', $message, array( 'status' => $code, 'body' => $data ) ); } return $data; } }