PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | src/ai/http-request/domain/request.php +48 -13 28.028.5 View file →
@@ -3,8 +3,10 @@
3 3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 4
5 5 namespace Yoast\WP\SEO\AI\HTTP_Request\Domain;
6 6
7 +use InvalidArgumentException;
8 +
7 9 /**
8 10 * Class Request
9 11 * Represents a request to the AI Generator API.
10 12 */
@@ -9,8 +11,14 @@
9 11 * Represents a request to the AI Generator API.
10 12 */
11 13 class Request {
12 14
15 + public const METHOD_GET = 'GET';
16 + public const METHOD_POST = 'POST';
17 + public const METHOD_DELETE = 'DELETE';
18 +
19 + private const ALLOWED_METHODS = [ self::METHOD_GET, self::METHOD_POST, self::METHOD_DELETE ];
20 +
13 21 /**
14 22 * The action path for the request.
15 23 *
16 24 * @var string
@@ -31,13 +39,13 @@
31 39 */
32 40 private $headers;
33 41
34 42 /**
35 - * Whether the request is a POST request.
43 + * The HTTP method for the request.
36 44 *
37 - * @var bool
45 + * @var string
38 46 */
39 - private $is_post;
47 + private $http_method;
40 48
41 49 /**
42 50 * Constructor for the Request class.
43 51 *
@@ -43,15 +51,22 @@
43 51 *
44 52 * @param string $action_path The action path for the request.
45 53 * @param array<string> $body The body of the request.
46 54 * @param array<string> $headers The headers for the request.
47 - * @param bool $is_post Whether the request is a POST request. Default is true.
55 + * @param string $http_method The HTTP method for the request. One of the METHOD_* constants. Defaults to POST.
56 + *
57 + * @throws InvalidArgumentException When $http_method is not one of the supported METHOD_* constants.
48 58 */
49 - public function __construct( string $action_path, array $body = [], array $headers = [], bool $is_post = true ) {
59 + public function __construct( string $action_path, array $body = [], array $headers = [], string $http_method = self::METHOD_POST ) {
60 + if ( ! \in_array( $http_method, self::ALLOWED_METHODS, true ) ) {
61 + // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
62 + throw new InvalidArgumentException( "Unsupported HTTP method: $http_method" );
63 + }
64 +
50 65 $this->action_path = $action_path;
51 66 $this->body = $body;
52 67 $this->headers = $headers;
53 - $this->is_post = $is_post;
68 + $this->http_method = $http_method;
54 69 }
55 70
56 71 /**
57 72 * Get the action path for the request.
@@ -64,12 +79,16 @@
64 79
65 80 /**
66 81 * Get the body of the request.
67 82 *
68 - * @return array<string> The body of the request.
83 + * Returns null for an empty body: an empty PHP array is ambiguous once JSON-encoded (`[]` vs `{}`),
84 + * so an empty body is omitted from the request entirely rather than sent as an empty array, which
85 + * the AI service rejects.
86 + *
87 + * @return array<string>|null The body of the request, or null when there is no body to send.
69 88 */
70 - public function get_body(): array {
71 - return $this->body;
89 + public function get_body(): ?array {
90 + return ( $this->body === [] ) ? null : $this->body;
72 91 }
73 92
74 93 /**
75 94 * Get the headers for the request.
@@ -80,12 +99,28 @@
80 99 return $this->headers;
81 100 }
82 101
83 102 /**
84 - * Whether the request is a POST request.
103 + * Get the HTTP method for the request.
85 104 *
86 - * @return bool True if the request is a POST request, false otherwise.
105 + * @return string One of the METHOD_* constants.
87 106 */
88 - public function is_post(): bool {
89 - return $this->is_post;
107 + public function get_http_method(): string {
108 + return $this->http_method;
109 + }
110 +
111 + /**
112 + * Returns a copy of the request with the given headers merged in.
113 + *
114 + * @param array<string> $headers The headers to add.
115 + *
116 + * @return self The new request.
117 + */
118 + public function with_added_headers( array $headers ): self {
119 + return new self(
120 + $this->action_path,
121 + $this->body,
122 + \array_merge( $this->headers, $headers ),
123 + $this->http_method,
124 + );
90 125 }
91 126 }