This commit is contained in:
Javier Casares 2026-11-04 17:33:03 +00:00
commit 44e2366e16

View file

@ -68,6 +68,31 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
return is_numeric( $timeout ) ? (int) $timeout : 30;
}
/**
* Filters the wp_ai_client_default_request_timeout value while an AI
* translation call is in progress. Registered and de-registered by
* translate() around each call.
*
* Since WordPress 7.2 the AI client builders pass an explicit request
* timeout to the WordPress HTTP API (default 30 s). Explicit arguments
* bypass the http_request_timeout filter, so the configured plugin
* timeout must be injected through this filter to stay effective on
* WordPress 7.0 through 7.2+ (the filter exists since 7.0).
*
* @since 1.2.4
*
* @param int|float|string|null $default_timeout Default timeout in seconds.
*
* @return int
*/
public function filter_ai_default_timeout( $default_timeout ) {
if ( null !== $this->active_timeout ) {
return $this->active_timeout;
}
return is_numeric( $default_timeout ) ? (int) $default_timeout : 30;
}
/**
* Indicates whether AI features are enabled for this WordPress installation.
*
@ -113,7 +138,10 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
* Translates a single piece of text to the target locale.
*
* Applies the configured request_timeout via the http_request_timeout filter
* for the duration of the AI call, then restores the previous timeout.
* and, since 1.2.4, via the wp_ai_client_default_request_timeout filter
* (required on WP 7.2+, where the AI client passes an explicit timeout that
* bypasses http_request_timeout) for the duration of the AI call, then
* restores the previous timeout.
*
* @since 1.0.0
*
@ -158,10 +186,13 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
$language_name
);
// Apply the configured request timeout around the AI call.
// Apply the configured request timeout around the AI call. Both filters
// are needed: WP 7.2+ AI client requests carry an explicit timeout that
// bypasses http_request_timeout (see filter_ai_default_timeout()).
$site_settings = $this->settings->get_settings();
$this->active_timeout = max( 30, (int) $site_settings['request_timeout'] );
add_filter( 'http_request_timeout', array( $this, 'filter_timeout' ), PHP_INT_MAX );
add_filter( 'wp_ai_client_default_request_timeout', array( $this, 'filter_ai_default_timeout' ), PHP_INT_MAX );
try {
$result = wp_ai_client_prompt( $text )
@ -174,6 +205,7 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
}
remove_filter( 'http_request_timeout', array( $this, 'filter_timeout' ), PHP_INT_MAX );
remove_filter( 'wp_ai_client_default_request_timeout', array( $this, 'filter_ai_default_timeout' ), PHP_INT_MAX );
$this->active_timeout = null;
if ( is_wp_error( $result ) ) {