This commit is contained in:
Javier Casares 2026-05-28 07:22:36 +00:00
commit 1ccc13da01
14 changed files with 845 additions and 197 deletions

View file

@ -51,14 +51,17 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
/**
* Default settings values.
*
* Boolean keys are coerced to bool on read/write; integer keys to int.
*
* @since 1.0.0
* @var array<string,bool>
* @var array<string,bool|int>
*/
const DEFAULTS = array(
'translate_title' => true,
'translate_content' => true,
'translate_excerpt' => true,
'auto_translate_on_mlp_create' => false,
'request_timeout' => 60,
);
/**
@ -104,7 +107,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
*
* @param int|null $blog_id Optional blog ID. Defaults to the current site.
*
* @return array<string,bool> Settings array with boolean values.
* @return array<string,bool|int> Settings array with typed values.
*/
public function get_settings( $blog_id = null ) {
if ( ! is_multisite() ) {
@ -139,7 +142,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
*
* @since 1.0.0
*
* @return array<string,bool>
* @return array<string,bool|int>
*/
public function get_network_settings() {
return $this->normalize( (array) get_site_option( self::OPTION_NETWORK, array() ) );
@ -150,7 +153,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
*
* @since 1.0.0
*
* @return array<string,bool>
* @return array<string,bool|int>
*/
public function get_site_settings() {
return $this->normalize( (array) get_option( self::OPTION_SITE, array() ) );
@ -161,7 +164,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
*
* @since 1.0.0
*
* @param array<string,bool> $settings Settings to store.
* @param array<string,bool|int> $settings Settings to store.
*
* @return bool True on success.
*/
@ -178,7 +181,7 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
*
* @since 1.0.0
*
* @param array<string,bool> $settings Settings to store.
* @param array<string,bool|int> $settings Settings to store.
*
* @return bool True on success.
*/
@ -187,22 +190,30 @@ if ( ! class_exists( 'AI_Translator_Settings' ) ) {
}
/**
* Normalises raw settings into a strict array of booleans matching the schema.
* 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.
*
* @since 1.0.0
*
* @param array<int|string,mixed> $settings Raw settings.
*
* @return array<string,bool>
* @return array<string,bool|int>
*/
private function normalize( array $settings ) {
$normalized = array();
foreach ( self::DEFAULTS as $key => $default ) {
if ( array_key_exists( $key, $settings ) ) {
$normalized[ $key ] = (bool) $settings[ $key ];
if ( is_int( $default ) ) {
$normalized[ $key ] = (int) $settings[ $key ];
} else {
$normalized[ $key ] = (bool) $settings[ $key ];
}
} else {
$normalized[ $key ] = (bool) $default;
$normalized[ $key ] = $default;
}
}