This commit is contained in:
Javier Casares 2026-11-04 17:34:22 +00:00
commit 8716024cd2

View file

@ -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'] : '' ),
);
}
}