| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Ai; |
| 4 |
|
| 5 |
defined('ABSPATH') or die; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handling AI Api. |
| 9 |
* @since 6.0.0 |
| 10 |
*/ |
| 11 |
class FluentFormAIAPI |
| 12 |
{ |
| 13 |
private $url = 'https://ai.fluentforms.com/'; |
| 14 |
|
| 15 |
public function makeRequest($requestData = []) |
| 16 |
{ |
| 17 |
$headers = [ |
| 18 |
'Content-Type' => 'application/json', |
| 19 |
]; |
| 20 |
|
| 21 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- http_request_timeout is a WordPress core hook |
| 22 |
$originalTimeout = apply_filters('http_request_timeout', 5); |
| 23 |
|
| 24 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- http_request_timeout is a WordPress core hook |
| 25 |
add_filter('http_request_timeout', function ($timeout) { |
| 26 |
return 60; |
| 27 |
}); |
| 28 |
|
| 29 |
$response = wp_remote_post($this->url, [ |
| 30 |
'headers' => $headers, |
| 31 |
'body' => json_encode($requestData), |
| 32 |
]); |
| 33 |
|
| 34 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- http_request_timeout is a WordPress core hook |
| 35 |
add_filter('http_request_timeout', function ($timeout) use ($originalTimeout) { |
| 36 |
return $originalTimeout; |
| 37 |
}); |
| 38 |
|
| 39 |
if (is_wp_error($response)) { |
| 40 |
return new \WP_Error(423, $response->get_error_message()); |
| 41 |
} |
| 42 |
|
| 43 |
$body = json_decode(wp_remote_retrieve_body($response), true); |
| 44 |
$code = wp_remote_retrieve_response_code($response); |
| 45 |
|
| 46 |
$error_message = __('Something went wrong.', 'fluentform'); |
| 47 |
if (isset($body['error']['message'])) { |
| 48 |
$error_message = $body['error']['message']; |
| 49 |
} |
| 50 |
|
| 51 |
if ($code !== 200) { |
| 52 |
return new \WP_Error(423, $error_message); |
| 53 |
} |
| 54 |
|
| 55 |
return $body; |
| 56 |
} |
| 57 |
} |
| 58 |
|