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

View file

@ -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 ];
}