• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / da4efbc5b07d9422360e8da09157e996aa6d4c8e

13 May 2026 09:45AM UTC coverage: 50.158%. First build
da4efbc5b07d9422360e8da09157e996aa6d4c8e

Pull #23265

github

web-flow
Merge c231415c9 into b17f347fb
Pull Request #23265: feat: authenticate yoast-ai requests with MyYoast OAuth tokens

129 of 246 new or added lines in 16 files covered. (52.44%)

20769 of 41407 relevant lines covered (50.16%)

4.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/ai/http-request/infrastructure/api-client.php
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 {
6✔
38
                // Our API expects JSON.
39
                // The request times out after 30 seconds.
40
                $headers   = \array_merge( $headers, [ 'Content-Type' => 'application/json' ] );
6✔
41
                $arguments = [
6✔
42
                        'timeout' => $this->get_request_timeout(),
6✔
43
                        'headers' => $headers,
6✔
44
                ];
6✔
45

46
                if ( $is_post ) {
6✔
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 );
4✔
49
                }
50

51
                $url      = $this->get_url( $action_path );
6✔
52
                $response = ( $is_post ) ? \wp_remote_post( $url, $arguments ) : \wp_remote_get( $url, $arguments );
6✔
53

54
                if ( \is_wp_error( $response ) ) {
6✔
55
                        // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
56
                        throw new WP_Request_Exception( $response->get_error_message() );
2✔
57
                }
58

59
                return $response;
4✔
60
        }
61

62
        /**
63
         * Builds the full URL for a request to the AI API, applying the same filter perform_request() uses.
64
         *
65
         * @param string $action_path The action path for the request.
66
         *
67
         * @return string The full URL for the request.
68
         */
NEW
69
        public function get_url( string $action_path ): string {
×
70
                /**
71
                 * Filter: 'Yoast\WP\SEO\ai_api_url' - Replaces the default URL for the AI API with a custom one.
72
                 *
73
                 * @internal
74
                 *
75
                 * @param string $url The default URL for the AI API.
76
                 */
NEW
77
                $url = \apply_filters( 'Yoast\WP\SEO\ai_api_url', $this->base_url );
×
78

NEW
79
                return $url . $action_path;
×
80
        }
81

82
        /**
83
         * Gets the timeout of the requests in seconds.
84
         *
85
         * @return int The timeout of the suggestion requests in seconds.
86
         */
87
        public function get_request_timeout(): int {
4✔
88
                /**
89
                 * Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes.
90
                 *
91
                 * @since 22.7
92
                 * @internal
93
                 *
94
                 * @param int $timeout The default timeout in seconds.
95
                 */
96
                return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 );
4✔
97
        }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc