namespace WPForms\Integrations\AI\API\Http;
use WPForms\Integrations\AI\Helpers;
use WPForms\Integrations\LiteConnect\LiteConnect;
use WPForms\Integrations\LiteConnect\Integration;
private const URL = 'https://wpformsapi.com/api/v1';
private const TIMEOUT = 60;
* @param string $endpoint Endpoint to request.
* @param array $args Request arguments.
* @return Response Response from the API.
public function post( string $endpoint, array $args = [] ): Response {
return $this->request( 'POST', $endpoint, $args );
* Make a request to the API.
* @param string $method Request method.
* @param string $endpoint Endpoint to request.
* @param array $args Arguments to send.
* @return Response Response from the API.
* @noinspection PhpSameParameterValueInspection
private function request( string $method, string $endpoint, array $args ): Response {
// Once mark AI features as used when making a first request.
// Add domain to the request.
$args['domain'] = preg_replace( '/(https?:\/\/)?(www\.)?(.*)\/?/', '$3', home_url() );
$args = $this->maybe_add_lite_connect_credentials( $args );
'headers' => $this->get_headers(),
'timeout' => $this->get_timeout(),
'body' => wp_json_encode( $args ),
$url = $this->get_request_url( $endpoint );
return new Response( wp_safe_remote_request( $url, $options ) );
* Get AI API request URL.
* @param string $endpoint Endpoint to request.
private function get_request_url( string $endpoint ): string {
* Filter AI API request URL.
* @param string $url API request URL.
* @param string $endpoint Endpoint to request.
return (string) apply_filters( 'wpforms_integrations_aiapi_http_request_url', self::URL . $endpoint, $endpoint );
* Maybe add Lite Connect credentials to the request.
* @param array $args Arguments to send.
private function maybe_add_lite_connect_credentials( array $args ): array {
if ( wpforms()->is_pro() ) {
if ( ! LiteConnect::is_allowed() || ! LiteConnect::is_enabled() ) {
return array_merge( $args, Integration::get_site_credentials() );
* Retrieve request headers.
private function get_headers(): array {
'Content-Type' => 'application/json',
if ( wpforms()->is_pro() ) {
$headers['x-wpforms-licensekey'] = wpforms_get_license_key();
* Retrieve request timeout.
private function get_timeout(): int {
* Filter the API request timeout.
* @param int $timeout Request timeout.
return (int) apply_filters( 'wpforms_integrations_ai_api_http_request_timeout', self::TIMEOUT ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName