PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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.6, at src/ai/http-request/infrastructure/api-client.php

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