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

Yoast / wordpress-seo / e69c2e79c97a57e26aa5c15ba3ca2a387f561b7d

02 Mar 2026 03:20PM UTC coverage: 53.204% (-0.7%) from 53.915%
e69c2e79c97a57e26aa5c15ba3ca2a387f561b7d

Pull #23031

github

web-flow
Merge 25ffa8e80 into 4ff1bc18e
Pull Request #23031: Refactor ai organization

5938 of 10669 branches covered (55.66%)

Branch coverage included in aggregate %.

718 of 759 new or added lines in 38 files covered. (94.6%)

718 existing lines in 33 files now uncovered.

21852 of 41564 relevant lines covered (52.57%)

71980.72 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

8
/**
9
 * Class API_Client
10
 * Handles the API requests to the AI Generator API.
11
 *
12
 * @makePublic
13
 */
14
class API_Client implements API_Client_Interface {
15

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

23
        /**
24
         * Performs a request to the API.
25
         *
26
         * @param string        $action_path The action path for the request.
27
         * @param array<string> $body        The body of the request.
28
         * @param array<string> $headers     The headers for the request.
29
         * @param bool          $is_post     Whether the request is a POST request.
30
         *
31
         * @return array<int|string|array<string>> The response from the API.
32
         *
33
         * @throws WP_Request_Exception When the wp_remote_post() returns an error.
34
         */
UNCOV
35
        public function perform_request( string $action_path, $body, $headers, bool $is_post ): array {
×
36
                // Our API expects JSON.
37
                // The request times out after 30 seconds.
UNCOV
38
                $headers   = \array_merge( $headers, [ 'Content-Type' => 'application/json' ] );
×
UNCOV
39
                $arguments = [
×
UNCOV
40
                        'timeout' => $this->get_request_timeout(),
×
UNCOV
41
                        'headers' => $headers,
×
UNCOV
42
                ];
×
43

UNCOV
44
                if ( $is_post ) {
×
45
                        // phpcs:ignore Yoast.Yoast.JsonEncodeAlternative.Found -- Reason: We don't want the debug/pretty possibility.
UNCOV
46
                        $arguments['body'] = WPSEO_Utils::format_json_encode( $body );
×
47
                }
48

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

UNCOV
59
                if ( \is_wp_error( $response ) ) {
×
60
                        // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
UNCOV
61
                        throw new WP_Request_Exception( $response->get_error_message() );
×
62
                }
63

UNCOV
64
                return $response;
×
65
        }
66

67
        /**
68
         * Gets the timeout of the requests in seconds.
69
         *
70
         * @return int The timeout of the suggestion requests in seconds.
71
         */
UNCOV
72
        public function get_request_timeout(): int {
×
73
                /**
74
                 * Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes.
75
                 *
76
                 * @since 22.7
77
                 * @internal
78
                 *
79
                 * @param int $timeout The default timeout in seconds.
80
                 */
UNCOV
81
                return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 );
×
82
        }
83
}
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