PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / ai-http-request / infrastructure / api-client.php

api-client.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/ai-http-request/infrastructure/api-client.php

84 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\AI_HTTP_Request\Infrastructure;
4
5 use WPSEO_Utils;
6 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\WP_Request_Exception;
7
8 /**
9 * Class API_Client
10 * Handles the API requests to the AI Generator API.
11 *
12 * @makePublic
13 */
14 class API_Client implements API_Client_Interface {
15
16 /**
17 * The base URL for the API.
18 *
19 * @var string
20 */
21 private $base_url = 'https://ai.yoa.st/api/v1';
22
23 /**
24 * Performs a request to the API.
25 *
26 * @param string $action_path The action path for the request.
27 * @param array<string> $body The body of the request.
28 * @param array<string> $headers The headers for the request.
29 * @param bool $is_post Whether the request is a POST request.
30 *
31 * @return array<int|string|array<string>> The response from the API.
32 *
33 * @throws WP_Request_Exception When the wp_remote_post() returns an error.
34 */
35 public function perform_request( string $action_path, $body, $headers, bool $is_post ): array {
36 // Our API expects JSON.
37 // The request times out after 30 seconds.
38 $headers = \array_merge( $headers, [ 'Content-Type' => 'application/json' ] );
39 $arguments = [
40 'timeout' => $this->get_request_timeout(),
41 'headers' => $headers,
42 ];
43
44 if ( $is_post ) {
45 // phpcs:ignore Yoast.Yoast.JsonEncodeAlternative.Found -- Reason: We don't want the debug/pretty possibility.
46 $arguments['body'] = WPSEO_Utils::format_json_encode( $body );
47 }
48
49 /**
50 * Filter: 'Yoast\WP\SEO\ai_api_url' - Replaces the default URL for the AI API with a custom one.
51 *
52 * @internal
53 *
54 * @param string $url The default URL for the AI API.
55 */
56 $url = \apply_filters( 'Yoast\WP\SEO\ai_api_url', $this->base_url );
57 $response = ( $is_post ) ? \wp_remote_post( $url . $action_path, $arguments ) : \wp_remote_get( $url . $action_path, $arguments );
58
59 if ( \is_wp_error( $response ) ) {
60 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
61 throw new WP_Request_Exception( $response->get_error_message() );
62 }
63
64 return $response;
65 }
66
67 /**
68 * Gets the timeout of the requests in seconds.
69 *
70 * @return int The timeout of the suggestion requests in seconds.
71 */
72 public function get_request_timeout(): int {
73 /**
74 * Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes.
75 *
76 * @since 22.7
77 * @internal
78 *
79 * @param int $timeout The default timeout in seconds.
80 */
81 return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 );
82 }
83 }
84