*/ public static function defaults(): array { return array( // Critical high — aligned with 2024 ICR consensus (Umpierrez et al.). 'ketoacidosis_threshold' => 11.1, // Type 1 — NICE NG17 2022. 'type1_fasting_min' => 4.0, 'type1_fasting_max' => 7.0, 'type1_post_meal_min' => 5.0, 'type1_post_meal_max' => 9.0, // Type 2 — ADA 2024 (upper); universal hypo floor (lower). 'type2_fasting_min' => 4.0, 'type2_fasting_max' => 7.2, 'type2_post_meal_min' => 5.0, 'type2_post_meal_max' => 10.0, // Gestational — NICE NG3 / ADA 2024 (stored; not yet used by Calculator widget). 'gestational_fasting_min' => 4.0, 'gestational_fasting_max' => 5.3, 'gestational_post_meal_min' => 4.0, 'gestational_post_meal_max' => 7.8, 'default_diabetes_type' => 'type1', 'default_meal_timing' => 'fasting', ); } /** * Return the active thresholds, merged over defaults. * * @return array */ public static function get(): array { $saved = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } return array_merge( self::defaults(), $saved ); } /** * Sanitise raw POST data into a clean thresholds array with range clamping. * * @param array $data Raw input data to sanitise. * @return array */ public static function sanitise( array $data ): array { $defaults = self::defaults(); $clean = array(); $float_keys = array( 'ketoacidosis_threshold', 'type1_fasting_min', 'type1_fasting_max', 'type1_post_meal_min', 'type1_post_meal_max', 'type2_fasting_min', 'type2_fasting_max', 'type2_post_meal_min', 'type2_post_meal_max', 'gestational_fasting_min', 'gestational_fasting_max', 'gestational_post_meal_min', 'gestational_post_meal_max', ); foreach ( $float_keys as $key ) { $val = (float) ( $data[ $key ] ?? $defaults[ $key ] ); $clean[ $key ] = max( 0.1, min( 50.0, $val ) ); } $clean['default_diabetes_type'] = in_array( $data['default_diabetes_type'] ?? '', array( 'type1', 'type2' ), true ) ? (string) $data['default_diabetes_type'] : (string) $defaults['default_diabetes_type']; $clean['default_meal_timing'] = in_array( $data['default_meal_timing'] ?? '', array( 'fasting', 'postMeal' ), true ) ? (string) $data['default_meal_timing'] : (string) $defaults['default_meal_timing']; return $clean; } /** * Sanitise and persist thresholds to the database. * * @param array $data Raw input data to sanitise and save. */ public static function save( array $data ): void { update_option( self::OPTION_KEY, self::sanitise( $data ) ); } }