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

Yoast / wordpress-seo / ac933c658864ca7120bd502004dc6816cd9ad1cc

29 May 2026 08:54PM UTC coverage: 50.322%. First build
ac933c658864ca7120bd502004dc6816cd9ad1cc

Pull #23306

github

web-flow
Merge a2074da73 into 951901dd1
Pull Request #23306: 23302 manage user consent through yoast ai

38 of 93 new or added lines in 10 files covered. (40.86%)

20893 of 41519 relevant lines covered (50.32%)

4.19 hits per line

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

95.24
/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
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Request;
10

11
/**
12
 * Class API_Client
13
 * Handles the API requests to the AI Generator API.
14
 *
15
 * @makePublic
16
 */
17
class API_Client implements API_Client_Interface {
18

19
        /**
20
         * The base URL for the API.
21
         *
22
         * @var string
23
         */
24
        private $base_url = 'https://ai.yoa.st/api/v1';
25

26
        /**
27
         * Performs a request to the API.
28
         *
29
         * @param string        $action_path The action path for the request.
30
         * @param array<string> $body        The body of the request.
31
         * @param array<string> $headers     The headers for the request.
32
         * @param string        $http_method The HTTP method for the request. One of `Request::METHOD_*`.
33
         *
34
         * @return array<int|string|array<string>> The response from the API.
35
         *
36
         * @throws WP_Request_Exception When the underlying WordPress HTTP call returns an error, or the HTTP method is not supported.
37
         */
38
        public function perform_request( string $action_path, $body, $headers, string $http_method ): array {
8✔
39
                // Our API expects JSON.
40
                $headers   = \array_merge( $headers, [ 'Content-Type' => 'application/json' ] );
8✔
41
                $arguments = [
8✔
42
                        'timeout' => $this->get_request_timeout(),
8✔
43
                        'headers' => $headers,
8✔
44
                ];
8✔
45

46
                // Only POST sends a body to the AI API today; GET and DELETE endpoints do not.
47
                if ( $http_method === Request::METHOD_POST ) {
8✔
48
                        // phpcs:ignore Yoast.Yoast.JsonEncodeAlternative.Found -- Reason: We don't want the debug/pretty possibility.
49
                        $arguments['body'] = WPSEO_Utils::format_json_encode( $body );
4✔
50
                }
51

52
                /**
53
                 * Filter: 'Yoast\WP\SEO\ai_api_url' - Replaces the default URL for the AI API with a custom one.
54
                 *
55
                 * @internal
56
                 *
57
                 * @param string $url The default URL for the AI API.
58
                 */
59
                $url = \apply_filters( 'Yoast\WP\SEO\ai_api_url', $this->base_url );
8✔
60

61
                switch ( $http_method ) {
62
                        case Request::METHOD_POST:
63
                                $response = \wp_remote_post( $url . $action_path, $arguments );
4✔
64
                                break;
4✔
65
                        case Request::METHOD_GET:
66
                                $response = \wp_remote_get( $url . $action_path, $arguments );
2✔
67
                                break;
2✔
68
                        case Request::METHOD_DELETE:
69
                                $response = \wp_remote_request( $url . $action_path, \array_merge( $arguments, [ 'method' => 'DELETE' ] ) );
2✔
70
                                break;
2✔
71
                        default:
72
                                // Defensive: the Request constructor already validates the method, so we should never reach this branch.
73
                                // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
NEW
74
                                throw new WP_Request_Exception( "Unsupported HTTP method: $http_method" );
×
75
                }
76

77
                if ( \is_wp_error( $response ) ) {
8✔
78
                        // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
79
                        throw new WP_Request_Exception( $response->get_error_message() );
2✔
80
                }
81

82
                return $response;
6✔
83
        }
84

85
        /**
86
         * Gets the timeout of the requests in seconds.
87
         *
88
         * @return int The timeout of the suggestion requests in seconds.
89
         */
90
        public function get_request_timeout(): int {
4✔
91
                /**
92
                 * Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes.
93
                 *
94
                 * @since 22.7
95
                 * @internal
96
                 *
97
                 * @param int $timeout The default timeout in seconds.
98
                 */
99
                return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 );
4✔
100
        }
101
}
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