Skip to content

Commit 2d1a64b

Browse files
committed
AI: Validate filtered default request timeout in WP_AI_Client_Prompt_Builder.
This checks that the return value of the `wp_ai_client_default_request_timeout` filter is a non-negative number before passing it to `RequestOptions`. If the filtered value is invalid, it is discarded in favor of the original default of `30.0` and a `_doing_it_wrong()` notice is issued. Without this check, a fatal error would ensue from the exception thrown in `\WordPress\AiClient\Providers\Http\DTO\RequestOptions::validateTimeout()`. The following static analysis issues are addressed: * Use `float` instead of `int` for the `wp_ai_client_default_request_timeout` filter parameter. * Add missing PHP imports for `Message` and `MessagePart` in the PHPDoc for `wp_ai_client_prompt()`. * Add PHP return type hints for `wp_ai_client_prompt()` and `WP_AI_Client_Cache::getMultiple()`. * Use native property type hints in `WP_AI_Client_HTTP_Client`. Developed in WordPress/wordpress-develop#11596 Reviewed by peterwilsoncc. Merges r62255 to the 7.0 branch. Props westonruter, justlevine, flixos90, khushdoms, darshitrajyaguru97, adrmf25, jarodortegaaraya, tusharaddweb, gaurangsondagar. Fixes #65094. Built from https://develop.svn.wordpress.org/branches/7.0@62266 git-svn-id: http://core.svn.wordpress.org/branches/7.0@61546 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent cd652b9 commit 2d1a64b

5 files changed

Lines changed: 24 additions & 9 deletions

File tree

wp-includes/ai-client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99

1010
use WordPress\AiClient\AiClient;
11+
use WordPress\AiClient\Messages\DTO\Message;
12+
use WordPress\AiClient\Messages\DTO\MessagePart;
1113

1214
/**
1315
* Returns whether AI features are supported in the current environment.
@@ -55,6 +57,6 @@ function wp_supports_ai(): bool {
5557
* conversations. Default null.
5658
* @return WP_AI_Client_Prompt_Builder The prompt builder instance.
5759
*/
58-
function wp_ai_client_prompt( $prompt = null ) {
60+
function wp_ai_client_prompt( $prompt = null ): WP_AI_Client_Prompt_Builder {
5961
return new WP_AI_Client_Prompt_Builder( AiClient::defaultRegistry(), $prompt );
6062
}

wp-includes/ai-client/adapters/class-wp-ai-client-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function clear(): bool {
104104
* @param mixed $default_value Default value to return for keys that do not exist.
105105
* @return array<string, mixed> A list of key => value pairs.
106106
*/
107-
public function getMultiple( $keys, $default_value = null ) {
107+
public function getMultiple( $keys, $default_value = null ): array {
108108
/**
109109
* Keys array.
110110
*

wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ class WP_AI_Client_HTTP_Client implements ClientInterface, ClientWithOptionsInte
3232
* Response factory instance.
3333
*
3434
* @since 7.0.0
35-
* @var ResponseFactoryInterface
3635
*/
37-
private $response_factory;
36+
private ResponseFactoryInterface $response_factory;
3837

3938
/**
4039
* Stream factory instance.
4140
*
4241
* @since 7.0.0
43-
* @var StreamFactoryInterface
4442
*/
45-
private $stream_factory;
43+
private StreamFactoryInterface $stream_factory;
4644

4745
/**
4846
* Constructor.

wp-includes/ai-client/class-wp-ai-client-prompt-builder.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,29 @@ public function __construct( ProviderRegistry $registry, $prompt = null ) {
190190
$this->error = $this->exception_to_wp_error( $e );
191191
}
192192

193+
$default_timeout = 30.0;
194+
193195
/**
194196
* Filters the default request timeout in seconds for AI Client HTTP requests.
195197
*
196198
* @since 7.0.0
197199
*
198-
* @param int $default_timeout The default timeout in seconds.
200+
* @param float $default_timeout The default timeout in seconds.
199201
*/
200-
$default_timeout = (int) apply_filters( 'wp_ai_client_default_request_timeout', 30 );
202+
$filtered_default_timeout = apply_filters( 'wp_ai_client_default_request_timeout', $default_timeout );
203+
if ( is_numeric( $filtered_default_timeout ) && (float) $filtered_default_timeout >= 0.0 ) {
204+
$default_timeout = (float) $filtered_default_timeout;
205+
} else {
206+
_doing_it_wrong(
207+
__METHOD__,
208+
sprintf(
209+
/* translators: %s: wp_ai_client_default_request_timeout */
210+
__( 'The %s filter must return a non-negative number.' ),
211+
'<code>wp_ai_client_default_request_timeout</code>'
212+
),
213+
'7.0.0'
214+
);
215+
}
201216

202217
$this->builder->usingRequestOptions(
203218
RequestOptions::fromArray(

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '7.0-RC2-62265';
19+
$wp_version = '7.0-RC2-62266';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)