self::DEFAULT_RETENTION_DAYS, 'delete_data_on_uninstall' => false, 'reconcile_minutes' => self::DEFAULT_RECONCILE_MINUTES, ); } /** * All settings, with defaults for missing keys. * * @return array{retention_days: int, delete_data_on_uninstall: bool, reconcile_minutes: int} */ public static function all(): array { $stored = get_option( self::OPTION, array() ); return self::sanitize( is_array( $stored ) ? $stored : array() ); } /** * Days to keep events. 0 means events are never deleted. */ public static function retention_days(): int { return self::all()['retention_days']; } /** * Whether uninstalling the plugin deletes its tables and options. */ public static function delete_data_on_uninstall(): bool { return self::all()['delete_data_on_uninstall']; } /** * Minutes between two checks for changes made directly in the database. * 0 means the check is off. */ public static function reconcile_minutes(): int { return self::all()['reconcile_minutes']; } /** * Sanitizes raw input into a complete settings array. * * Retention: empty or invalid input keeps the default, 0 means forever, * any other value is clamped between MIN and MAX. Reconcile interval: one * of RECONCILE_MINUTES, otherwise the default. * * @param mixed $input Raw value, usually from the settings form. * @return array{retention_days: int, delete_data_on_uninstall: bool, reconcile_minutes: int} */ public static function sanitize( mixed $input ): array { $input = is_array( $input ) ? $input : array(); $defaults = self::defaults(); $retention = $defaults['retention_days']; if ( isset( $input['retention_days'] ) && is_numeric( $input['retention_days'] ) ) { $days = (int) $input['retention_days']; $retention = 0 === $days ? 0 : max( self::MIN_RETENTION_DAYS, min( self::MAX_RETENTION_DAYS, $days ) ); } $reconcile = $defaults['reconcile_minutes']; if ( isset( $input['reconcile_minutes'] ) && is_numeric( $input['reconcile_minutes'] ) && in_array( (int) $input['reconcile_minutes'], self::RECONCILE_MINUTES, true ) ) { $reconcile = (int) $input['reconcile_minutes']; } return array( 'retention_days' => $retention, 'delete_data_on_uninstall' => ! empty( $input['delete_data_on_uninstall'] ), 'reconcile_minutes' => $reconcile, ); } }