PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.6
28.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 All 130 releases
wordpress-seo / src / deprecated / src / ai-http-request / infrastructure / api-client.php

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

114 lines 3.7 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 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Request;
8
9 /**
10 * Class API_Client
11 * Handles the API requests to the AI Generator API.
12 *
13 * @deprecated 28.4
14 * @codeCoverageIgnore
15 *
16 * @makePublic
17 */
18 class API_Client implements API_Client_Interface {
19
20 /**
21 * The base URL for the API.
22 *
23 * @var string
24 */
25 private $base_url = 'https://ai.yoa.st/api/v1';
26
27 /**
28 * Performs a request to the API.
29 *
30 * @deprecated 28.4
31 * @codeCoverageIgnore
32 *
33 * @param string $action_path The action path for the request.
34 * @param array<string>|null $body The body of the request, or null/empty to send no body.
35 * @param array<string> $headers The headers for the request.
36 * @param string $http_method The HTTP method for the request. One of `Request::METHOD_*`.
37 *
38 * @return array<int|string|array<string>> The response from the API.
39 *
40 * @throws WP_Request_Exception When the underlying WordPress HTTP call returns an error, or the HTTP method is not supported.
41 */
42 public function perform_request( string $action_path, $body, $headers, string $http_method ): array {
43 \_deprecated_function( __METHOD__, 'Yoast SEO 28.4' );
44 // Our API expects JSON.
45 $headers = \array_merge( $headers, [ 'Content-Type' => 'application/json' ] );
46 $arguments = [
47 'timeout' => $this->get_request_timeout(),
48 'headers' => $headers,
49 ];
50
51 // Only POST sends a body to the AI API today; GET and DELETE endpoints do not. An empty body is
52 // omitted entirely: an empty array is ambiguous once JSON-encoded (`[]` vs `{}`) and the AI
53 // service rejects it, so a bodyless POST is sent instead.
54 if ( $http_method === Request::METHOD_POST && ! empty( $body ) ) {
55 // phpcs:ignore Yoast.Yoast.JsonEncodeAlternative.Found -- Reason: We don't want the debug/pretty possibility.
56 $arguments['body'] = WPSEO_Utils::format_json_encode( $body );
57 }
58
59 /**
60 * Filter: 'Yoast\WP\SEO\ai_api_url' - Replaces the default URL for the AI API with a custom one.
61 *
62 * @internal
63 *
64 * @param string $url The default URL for the AI API.
65 */
66 $url = \apply_filters( 'Yoast\WP\SEO\ai_api_url', $this->base_url );
67
68 switch ( $http_method ) {
69 case Request::METHOD_POST:
70 $response = \wp_remote_post( $url . $action_path, $arguments );
71 break;
72 case Request::METHOD_GET:
73 $response = \wp_remote_get( $url . $action_path, $arguments );
74 break;
75 case Request::METHOD_DELETE:
76 $response = \wp_remote_request( $url . $action_path, \array_merge( $arguments, [ 'method' => 'DELETE' ] ) );
77 break;
78 default:
79 // Defensive: the Request constructor already validates the method, so we should never reach this branch.
80 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
81 throw new WP_Request_Exception( "Unsupported HTTP method: $http_method" );
82 }
83
84 if ( \is_wp_error( $response ) ) {
85 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
86 throw new WP_Request_Exception( $response->get_error_message() );
87 }
88
89 return $response;
90 }
91
92 /**
93 * Gets the timeout of the requests in seconds.
94 *
95 * @deprecated 28.4
96 * @codeCoverageIgnore
97 *
98 * @return int The timeout of the suggestion requests in seconds.
99 */
100 public function get_request_timeout(): int {
101 \_deprecated_function( __METHOD__, 'Yoast SEO 28.4' );
102
103 /**
104 * Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes.
105 *
106 * @since 22.7
107 * @internal
108 *
109 * @param int $timeout The default timeout in seconds.
110 */
111 return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 );
112 }
113 }
114