• 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

0.0
/src/ai-http-request/infrastructure/api-client.php
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
 * @makePublic
14
 */
15
class API_Client implements API_Client_Interface {
16

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

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

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

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

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

75
                if ( \is_wp_error( $response ) ) {
×
76
                        // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
77
                        throw new WP_Request_Exception( $response->get_error_message() );
×
78
                }
79

80
                return $response;
×
81
        }
82

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