group( $stored, 'quiet_hours' ); $database = $this->group( $stored, 'database' ); return new Settings( check_posts: $this->flag( $stored, 'check_posts', $defaults->check_posts ), check_pages: $this->flag( $stored, 'check_pages', $defaults->check_pages ), check_media: $this->flag( $stored, 'check_media', $defaults->check_media ), editor_warning: $this->flag( $stored, 'editor_warning', $defaults->editor_warning ), email_enabled: $this->flag( $stored, 'email_enabled', $defaults->email_enabled ), email_to: $this->text( $stored, 'email_to', $defaults->email_to ), skip_hosts: $this->hosts( $stored ), quiet_hours_enabled: $this->flag( $quiet, 'enabled', $defaults->quiet_hours_enabled ), quiet_hours_from: $this->text( $quiet, 'from', $defaults->quiet_hours_from ), quiet_hours_to: $this->text( $quiet, 'to', $defaults->quiet_hours_to ), pause_at_ms: $this->number( $database, 'pause_at_ms', $defaults->pause_at_ms ), batch_size: $this->number( $database, 'batch_size', $defaults->batch_size ) ); } public function save( Settings $settings ): void { update_option( self::OPTION, $settings->to_array(), false ); } /** * @param array $stored * @return array */ private function group( array $stored, string $key ): array { $group = $stored[ $key ] ?? array(); return is_array( $group ) ? $group : array(); } /** @param array $stored */ private function flag( array $stored, string $key, bool $fallback ): bool { $value = $stored[ $key ] ?? null; return is_bool( $value ) ? $value : $fallback; } /** @param array $stored */ private function text( array $stored, string $key, string $fallback ): string { $value = $stored[ $key ] ?? null; return is_string( $value ) ? $value : $fallback; } /** @param array $stored */ private function number( array $stored, string $key, int $fallback ): int { $value = $stored[ $key ] ?? null; return is_int( $value ) ? $value : $fallback; } /** * @param array $stored * @return list */ private function hosts( array $stored ): array { $hosts = $stored['skip_hosts'] ?? array(); if ( ! is_array( $hosts ) ) { return array(); } return array_values( array_filter( $hosts, 'is_string' ) ); } }