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

Yoast / wordpress-seo / 8902429d51b3f7824505257cd9f756866d74786b

29 May 2025 07:09AM UTC coverage: 51.845% (-5.5%) from 57.298%
8902429d51b3f7824505257cd9f756866d74786b

push

github

web-flow
Merge pull request #22264 from Yoast/move-and-refactor-ai-generator-helper

Move and refactor ai generator helper

8230 of 14678 branches covered (56.07%)

Branch coverage included in aggregate %.

0 of 741 new or added lines in 38 files covered. (0.0%)

29750 of 58579 relevant lines covered (50.79%)

40756.97 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
class API_Client implements API_Client_Interface {
13

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

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

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

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

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

NEW
62
                return $response;
×
63
        }
64

65
        /**
66
         * Gets the timeout of the requests in seconds.
67
         *
68
         * @return int The timeout of the suggestion requests in seconds.
69
         */
NEW
70
        public function get_request_timeout(): int {
×
71
                /**
72
                 * Filter: 'Yoast\WP\SEO\ai_suggestions_timeout' - Replaces the default timeout with a custom one, for testing purposes.
73
                 *
74
                 * @since 22.7
75
                 * @internal
76
                 *
77
                 * @param int $timeout The default timeout in seconds.
78
                 */
NEW
79
                return (int) \apply_filters( 'Yoast\WP\SEO\ai_suggestions_timeout', 60 );
×
80
        }
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc