false, 'source_language' => self::guess_site_language(), 'languages' => array(), 'url_mode' => 'directory', // directory|subdomain|domain|query. 'domains' => array(), 'hide_default' => true, 'auto_translate' => true, // Translate new/updated content automatically. 'auto_publish' => false, // Publish auto translations without review. 'browser_redirect' => false, 'switcher' => array( 'floating' => true, 'position' => 'bottom-right', 'style' => 'flags-names', ), 'low_credit_alert' => 10, // Percent remaining. 'post_types' => array(), // Empty = all public types. 'taxonomies' => array(), // Empty = all public taxonomies. 'custom_fields' => array(), // meta_key => translate|copy|ignore. 'translate_emails' => true, 'capture_strings' => true, 'woo_sync' => true, 'translate_slugs' => true, 'language_packs' => false, // Opt-in: download WordPress.org translations for target languages. ); } public static function schema(): array { return array( 'onboarded' => 'bool', 'source_language' => 'lang', 'languages' => 'lang_list', 'url_mode' => array( 'directory', 'subdomain', 'domain', 'query' ), 'domains' => 'domain_map', 'hide_default' => 'bool', 'auto_translate' => 'bool', 'auto_publish' => 'bool', 'browser_redirect' => 'bool', 'switcher' => 'switcher', 'low_credit_alert' => 'percent', 'post_types' => 'key_list', 'taxonomies' => 'key_list', 'custom_fields' => 'field_rules', 'translate_emails' => 'bool', 'capture_strings' => 'bool', 'woo_sync' => 'bool', 'translate_slugs' => 'bool', 'language_packs' => 'bool', ); } /** @var array|null Per-request cache; reset on update. */ private static $cache = null; public function all(): array { if ( null === self::$cache ) { $stored = get_option( self::OPTION, array() ); self::$cache = array_replace_recursive( self::defaults(), is_array( $stored ) ? $stored : array() ); } return self::$cache; } public static function flush(): void { self::$cache = null; } /** * @param string $key Setting key. * @param mixed $default Fallback. * @return mixed */ public function get( string $key, $default = null ) { $all = $this->all(); return array_key_exists( $key, $all ) ? $all[ $key ] : $default; } /** * Merge and persist a partial update. Unknown keys are dropped. */ public function update( array $changes ): array { $next = $this->all(); foreach ( self::schema() as $key => $type ) { if ( array_key_exists( $key, $changes ) ) { $next[ $key ] = self::sanitize( $type, $changes[ $key ] ); } } // The source language can never also be a target. $next['languages'] = array_values( array_diff( $next['languages'], array( $next['source_language'] ) ) ); update_option( self::OPTION, $next ); self::$cache = null; return $this->all(); } /** * @param string|array $type Schema type. * @param mixed $value Raw value. * @return mixed */ private static function sanitize( $type, $value ) { if ( is_array( $type ) ) { return in_array( $value, $type, true ) ? $value : $type[0]; } switch ( $type ) { case 'bool': return (bool) $value; case 'percent': return max( 0, min( 100, (int) $value ) ); case 'lang': return self::sanitize_lang( $value ); case 'lang_list': return array_values( array_unique( array_filter( array_map( array( __CLASS__, 'sanitize_lang' ), (array) $value ) ) ) ); case 'key_list': return array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) $value ) ) ) ); case 'field_rules': $out = array(); foreach ( (array) $value as $key => $rule ) { $key = preg_replace( '/[^A-Za-z0-9_\-\*]/', '', (string) $key ); if ( $key && in_array( $rule, array( 'translate', 'copy', 'ignore' ), true ) ) { $out[ $key ] = $rule; } } return $out; case 'domain_map': $out = array(); foreach ( (array) $value as $lang => $domain ) { $lang = self::sanitize_lang( $lang ); $host = wp_parse_url( 'http://' . preg_replace( '#^https?://#', '', (string) $domain ), PHP_URL_HOST ); if ( $lang && $host ) { $out[ $lang ] = strtolower( $host ); } } return $out; case 'switcher': $value = (array) $value; return array( 'floating' => ! empty( $value['floating'] ), 'position' => in_array( $value['position'] ?? '', array( 'bottom-right', 'bottom-left', 'top-right', 'top-left' ), true ) ? $value['position'] : 'bottom-right', 'style' => in_array( $value['style'] ?? '', array( 'flags-names', 'flags', 'names', 'codes' ), true ) ? $value['style'] : 'flags-names', ); } return null; } public static function sanitize_lang( $value ): string { // Case is preserved: the API uses codes such as "zh-CN". $value = trim( (string) $value ); return preg_match( '/^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})?$/', $value ) ? $value : ''; } /** * Site language from the raw option (get_locale() is filtered by the router, * which reads these settings, so it must not be called here). */ private static function guess_site_language(): string { $locale = (string) get_option( 'WPLANG' ); if ( '' === $locale ) { $locale = defined( 'WPLANG' ) && WPLANG ? (string) WPLANG : 'en_US'; } $map = array( 'zh_CN' => 'zh-CN', 'zh_TW' => 'zh-TW', 'zh_HK' => 'zh-TW', ); return $map[ $locale ] ?? ( strtolower( substr( $locale, 0, 2 ) ) ?: 'en' ); } }