'GET', 'timeout' => 15, 'headers' => [], ]; $args = array_merge( $defaults, $args ); $args['headers'] = array_merge( [ 'Authorization' => 'Bearer ' . $settings['api_key'], 'Accept' => 'application/json', 'User-Agent' => 'AgentsiaWP/' . AGENTSIA_AGENTS_VERSION . ' (' . home_url() . ')', ], (array) $args['headers'] ); $url = self::build_url( $path ); $res = wp_remote_request( $url, $args ); if ( is_wp_error( $res ) ) { return $res; } $code = wp_remote_retrieve_response_code( $res ); $body = wp_remote_retrieve_body( $res ); $data = json_decode( $body, true ); if ( $code >= 400 ) { if ( is_array( $data ) && isset( $data['error'] ) ) { $message = (string) $data['error']; } else { /* translators: %d: HTTP status code */ $message = sprintf( __( 'Erreur HTTP %d', 'agentsia-agents' ), $code ); } return new \WP_Error( 'agentsia_api_error', $message, [ 'status' => $code ] ); } return is_array( $data ) ? $data : []; } public static function verify_key(): array { $result = self::request( '/api/public/wp/me' ); if ( is_wp_error( $result ) ) { return [ 'ok' => false, 'message' => $result->get_error_message(), ]; } return [ 'ok' => true, 'company' => $result['company'] ?? null, 'scopes' => $result['scopes'] ?? [], ]; } public static function list_chatbots( bool $fresh = false ): array { return self::cached_list( 'chatbots', '/api/public/wp/chatbots', 'chatbots', $fresh ); } public static function list_booking_calendars( bool $fresh = false ): array { return self::cached_list( 'booking_calendars', '/api/public/wp/booking-calendars', 'calendars', $fresh ); } private static function cached_list( string $cache_key, string $path, string $response_key, bool $fresh ): array { $transient_key = self::CACHE_TRANSIENT_PREFIX . $cache_key; if ( ! $fresh ) { $cached = get_transient( $transient_key ); if ( is_array( $cached ) ) { return $cached; } } $result = self::request( $path ); if ( is_wp_error( $result ) ) { return []; } $list = isset( $result[ $response_key ] ) && is_array( $result[ $response_key ] ) ? $result[ $response_key ] : []; set_transient( $transient_key, $list, self::CACHE_TTL ); return $list; } public static function clear_cache(): void { delete_transient( self::CACHE_TRANSIENT_PREFIX . 'chatbots' ); delete_transient( self::CACHE_TRANSIENT_PREFIX . 'booking_calendars' ); } }