v1.3.0
This commit is contained in:
parent
44e2366e16
commit
8716024cd2
3 changed files with 322 additions and 121 deletions
|
|
@ -67,17 +67,28 @@ if ( ! class_exists( 'AI_Translator_Admin' ) ) {
|
|||
*/
|
||||
private $manager_notice;
|
||||
|
||||
/**
|
||||
* Translator handler, used to list the available AI providers.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @var AI_Translator_Translator
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @since 1.3.0 Added the $translator parameter.
|
||||
*
|
||||
* @param AI_Translator_Settings $settings Settings handler.
|
||||
* @param AI_Translator_Manager_Notice $manager_notice Manager notice handler.
|
||||
* @param AI_Translator_Translator $translator Translator handler.
|
||||
*/
|
||||
public function __construct( AI_Translator_Settings $settings, AI_Translator_Manager_Notice $manager_notice ) {
|
||||
public function __construct( AI_Translator_Settings $settings, AI_Translator_Manager_Notice $manager_notice, AI_Translator_Translator $translator ) {
|
||||
$this->settings = $settings;
|
||||
$this->manager_notice = $manager_notice;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -291,6 +302,8 @@ if ( ! class_exists( 'AI_Translator_Admin' ) ) {
|
|||
</td>
|
||||
</tr>
|
||||
|
||||
<?php $this->render_provider_field( $current, 'ai-translator' ); ?>
|
||||
|
||||
<?php if ( class_exists( '\Inpsyde\MultilingualPress\TranslationUi\Post\MetaboxAction' ) ) : ?>
|
||||
<tr>
|
||||
<th scope="row"><?php echo esc_html__( 'MultilingualPress', 'robotstxt-ai-translator' ); ?></th>
|
||||
|
|
@ -437,6 +450,8 @@ if ( ! class_exists( 'AI_Translator_Admin' ) ) {
|
|||
</td>
|
||||
</tr>
|
||||
|
||||
<?php $this->render_provider_field( $network, 'ai-translator-net' ); ?>
|
||||
|
||||
<?php if ( class_exists( '\Inpsyde\MultilingualPress\TranslationUi\Post\MetaboxAction' ) ) : ?>
|
||||
<tr>
|
||||
<th scope="row"><?php echo esc_html__( 'MultilingualPress', 'robotstxt-ai-translator' ); ?></th>
|
||||
|
|
@ -537,6 +552,68 @@ if ( ! class_exists( 'AI_Translator_Admin' ) ) {
|
|||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the AI provider selector row shared by the site and network forms.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param array<string,bool|int|string> $current Effective settings for the form being rendered.
|
||||
* @param string $id_base Unique prefix for the element IDs ('ai-translator' or 'ai-translator-net').
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function render_provider_field( array $current, $id_base ) {
|
||||
$providers = $this->translator->get_available_providers();
|
||||
$selected = isset( $current['ai_provider'] ) && is_string( $current['ai_provider'] ) ? $current['ai_provider'] : '';
|
||||
?>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="<?php echo esc_attr( $id_base ); ?>-ai-provider"><?php echo esc_html__( 'AI provider', 'robotstxt-ai-translator' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<select id="<?php echo esc_attr( $id_base ); ?>-ai-provider" name="ai_translator_settings[ai_provider]">
|
||||
<option value="" <?php selected( $selected, '' ); ?>>
|
||||
<?php echo esc_html__( 'Automatic (WordPress picks the provider)', 'robotstxt-ai-translator' ); ?>
|
||||
</option>
|
||||
<?php foreach ( $providers as $provider ) : ?>
|
||||
<option value="<?php echo esc_attr( $provider['id'] ); ?>" <?php selected( $selected, $provider['id'] ); ?>>
|
||||
<?php echo esc_html( $provider['name'] ); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<p class="description">
|
||||
<?php echo esc_html__( 'Provider used for translations. Only providers with configured credentials are listed.', 'robotstxt-ai-translator' ); ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitises the AI provider selection against the available providers.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param mixed $raw Raw value from the form.
|
||||
*
|
||||
* @return string Provider ID, or an empty string for "Automatic".
|
||||
*/
|
||||
private function sanitize_provider( $raw ) {
|
||||
if ( ! is_string( $raw ) || '' === $raw ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = strtolower( trim( $raw ) );
|
||||
|
||||
foreach ( $this->translator->get_available_providers() as $provider ) {
|
||||
if ( $provider['id'] === $value ) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the model recommendations table.
|
||||
*
|
||||
|
|
@ -658,7 +735,7 @@ if ( ! class_exists( 'AI_Translator_Admin' ) ) {
|
|||
* only the known schema keys are read, so integer
|
||||
* keys and unexpected values are ignored.
|
||||
*
|
||||
* @return array<string,bool|int>
|
||||
* @return array<string,bool|int|string>
|
||||
*/
|
||||
private function sanitize_settings( array $raw ) {
|
||||
return array(
|
||||
|
|
@ -667,6 +744,7 @@ if ( ! class_exists( 'AI_Translator_Admin' ) ) {
|
|||
'translate_excerpt' => ! empty( $raw['translate_excerpt'] ),
|
||||
'auto_translate_on_mlp_create' => ! empty( $raw['auto_translate_on_mlp_create'] ),
|
||||
'request_timeout' => max( 30, min( 300, isset( $raw['request_timeout'] ) && is_numeric( $raw['request_timeout'] ) ? (int) $raw['request_timeout'] : 60 ) ),
|
||||
'ai_provider' => $this->sanitize_provider( isset( $raw['ai_provider'] ) ? $raw['ai_provider'] : '' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,10 +51,11 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
/**
|
||||
* Default settings values.
|
||||
*
|
||||
* Boolean keys are coerced to bool on read/write; integer keys to int.
|
||||
* Boolean keys are coerced to bool on read/write; integer keys to int;
|
||||
* string keys to string.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var array<string,bool|int>
|
||||
* @var array<string,bool|int|string>
|
||||
*/
|
||||
const DEFAULTS = array(
|
||||
'translate_title' => true,
|
||||
|
|
@ -62,6 +63,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
'translate_excerpt' => true,
|
||||
'auto_translate_on_mlp_create' => false,
|
||||
'request_timeout' => 60,
|
||||
'ai_provider' => '',
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
@ -107,7 +109,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
*
|
||||
* @param int|null $blog_id Optional blog ID. Defaults to the current site.
|
||||
*
|
||||
* @return array<string,bool|int> Settings array with typed values.
|
||||
* @return array<string,bool|int|string> Settings array with typed values.
|
||||
*/
|
||||
public function get_settings( $blog_id = null ) {
|
||||
if ( ! is_multisite() ) {
|
||||
|
|
@ -142,7 +144,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array<string,bool|int>
|
||||
* @return array<string,bool|int|string>
|
||||
*/
|
||||
public function get_network_settings() {
|
||||
return $this->normalize( (array) get_site_option( self::OPTION_NETWORK, array() ) );
|
||||
|
|
@ -153,7 +155,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array<string,bool|int>
|
||||
* @return array<string,bool|int|string>
|
||||
*/
|
||||
public function get_site_settings() {
|
||||
return $this->normalize( (array) get_option( self::OPTION_SITE, array() ) );
|
||||
|
|
@ -164,7 +166,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array<string,bool|int> $settings Settings to store.
|
||||
* @param array<string,bool|int|string> $settings Settings to store.
|
||||
*
|
||||
* @return bool True on success.
|
||||
*/
|
||||
|
|
@ -181,7 +183,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array<string,bool|int> $settings Settings to store.
|
||||
* @param array<string,bool|int|string> $settings Settings to store.
|
||||
*
|
||||
* @return bool True on success.
|
||||
*/
|
||||
|
|
@ -192,15 +194,16 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
/**
|
||||
* Normalises raw settings into a typed array matching the schema.
|
||||
*
|
||||
* Boolean keys are coerced to strict bool; integer keys to strict int.
|
||||
* The DEFAULTS constant is the authoritative schema: any key not present
|
||||
* there is silently dropped, and any missing key falls back to its default.
|
||||
* Boolean keys are coerced to strict bool; integer keys to strict int;
|
||||
* string keys to trimmed, lowercased strings. The DEFAULTS constant is
|
||||
* the authoritative schema: any key not present there is silently
|
||||
* dropped, and any missing key falls back to its default.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array<int|string,mixed> $settings Raw settings.
|
||||
*
|
||||
* @return array<string,bool|int>
|
||||
* @return array<string,bool|int|string>
|
||||
*/
|
||||
private function normalize( array $settings ) {
|
||||
$normalized = array();
|
||||
|
|
@ -212,6 +215,8 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
|
|||
// Reject non-numeric input instead of casting it: garbage
|
||||
// falls back to the schema default.
|
||||
$normalized[ $key ] = is_numeric( $value ) ? (int) $value : $default;
|
||||
} elseif ( is_string( $default ) ) {
|
||||
$normalized[ $key ] = is_scalar( $settings[ $key ] ) ? strtolower( trim( (string) $settings[ $key ] ) ) : $default;
|
||||
} else {
|
||||
$normalized[ $key ] = (bool) $settings[ $key ];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,6 +134,45 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the AI providers available for translation on this site.
|
||||
*
|
||||
* Only providers registered with the WordPress AI client (core since
|
||||
* WordPress 7.0) that also have their credentials configured are
|
||||
* listed, since those are the only ones capable of serving requests.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @return array<int,array{id:string,name:string}> List of provider IDs and names.
|
||||
*/
|
||||
public function get_available_providers() {
|
||||
$registry = \WordPress\AiClient\AiClient::defaultRegistry();
|
||||
$providers = array();
|
||||
|
||||
foreach ( $registry->getRegisteredProviderIds() as $provider_id ) {
|
||||
$provider_id = (string) $provider_id;
|
||||
|
||||
try {
|
||||
if ( ! $registry->isProviderConfigured( $provider_id ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $registry->getProviderClassName( $provider_id )::metadata()->getName();
|
||||
} catch ( Throwable $e ) {
|
||||
// A misbehaving provider must not break the settings page:
|
||||
// skip it and keep enumerating the rest.
|
||||
continue;
|
||||
}
|
||||
|
||||
$providers[] = array(
|
||||
'id' => $provider_id,
|
||||
'name' => '' !== $name ? $name : $provider_id,
|
||||
);
|
||||
}
|
||||
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a single piece of text to the target locale.
|
||||
*
|
||||
|
|
@ -195,7 +234,15 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
|
|||
add_filter( 'wp_ai_client_default_request_timeout', array( $this, 'filter_ai_default_timeout' ), PHP_INT_MAX );
|
||||
|
||||
try {
|
||||
$result = wp_ai_client_prompt( $text )
|
||||
$prompt = wp_ai_client_prompt( $text );
|
||||
|
||||
// Pin the configured provider when one is selected. An empty
|
||||
// value keeps the WordPress AI client default resolution.
|
||||
if ( isset( $site_settings['ai_provider'] ) && is_string( $site_settings['ai_provider'] ) && '' !== $site_settings['ai_provider'] ) {
|
||||
$prompt = $prompt->using_provider( $site_settings['ai_provider'] );
|
||||
}
|
||||
|
||||
$result = $prompt
|
||||
->using_system_instruction( $system_instruction )
|
||||
->generate_text();
|
||||
} catch ( Exception $e ) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue