v1.2.0
This commit is contained in:
parent
02cd01e862
commit
1ccc13da01
14 changed files with 845 additions and 197 deletions
|
|
@ -22,6 +22,52 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
|
|||
*/
|
||||
class AI_Translator_Translator {
|
||||
|
||||
/**
|
||||
* Settings handler.
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @var AI_Translator_Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* Timeout value (seconds) enforced during an active translate() call.
|
||||
* Null when no call is in progress.
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @var int|null
|
||||
*/
|
||||
private $active_timeout = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*
|
||||
* @param AI_Translator_Settings $settings Settings handler.
|
||||
*/
|
||||
public function __construct( AI_Translator_Settings $settings ) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the http_request_timeout value while an AI translation call is
|
||||
* in progress. Registered and de-registered by translate() around each call.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*
|
||||
* @param mixed $timeout Current timeout value passed by WordPress.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function filter_timeout( $timeout ) {
|
||||
if ( null !== $this->active_timeout ) {
|
||||
return $this->active_timeout;
|
||||
}
|
||||
|
||||
return is_numeric( $timeout ) ? (int) $timeout : 30;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether AI features are enabled for this WordPress installation.
|
||||
*
|
||||
|
|
@ -66,6 +112,9 @@ 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.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $text Source text. Plain text or HTML; preserved as-is.
|
||||
|
|
@ -109,16 +158,24 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
|
|||
$language_name
|
||||
);
|
||||
|
||||
// Apply the configured request timeout around the AI call.
|
||||
$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 );
|
||||
|
||||
try {
|
||||
$result = wp_ai_client_prompt( $text )
|
||||
->using_system_instruction( $system_instruction )
|
||||
->generate_text();
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'ai_translator_exception', $e->getMessage() );
|
||||
$result = new WP_Error( 'ai_translator_exception', $e->getMessage() );
|
||||
} catch ( Throwable $e ) {
|
||||
return new WP_Error( 'ai_translator_exception', $e->getMessage() );
|
||||
$result = new WP_Error( 'ai_translator_exception', $e->getMessage() );
|
||||
}
|
||||
|
||||
remove_filter( 'http_request_timeout', array( $this, 'filter_timeout' ), PHP_INT_MAX );
|
||||
$this->active_timeout = null;
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue