' . esc_html__('Einstellungen', 'deskhub-tickets') . ''); return $links; } public static function register(): void { register_setting(self::GROUP, 'deskhub_tickets_options', [ 'type' => 'array', 'sanitize_callback' => [__CLASS__, 'sanitize'], 'default' => [], ]); } /** * Prüft und bereinigt die abgeschickten Einstellungen. Der API-Schlüssel bleibt erhalten, wenn das Feld leer ist * (er wird nie wieder in die Seite geschrieben). */ public static function sanitize($input): array { $input = is_array($input) ? $input : []; $old = get_option('deskhub_tickets_options', []); $old = array_merge(deskhub_tickets_defaults(), is_array($old) ? $old : []); $out = deskhub_tickets_defaults(); $url = trim((string) ($input['server_url'] ?? '')); if ($url === '') { $out['server_url'] = ''; } else { $norm = Deskhub_Tickets_Client::normalize_url($url); if (is_wp_error($norm)) { add_settings_error('deskhub_tickets_options', 'deskhub_url', $norm->get_error_message()); $out['server_url'] = (string) $old['server_url']; } else { $out['server_url'] = $norm; } } if (!empty($input['clear_key'])) { $out['api_key'] = ''; } else { $key = trim((string) ($input['api_key'] ?? '')); $key = preg_replace('/[^\x21-\x7E]/', '', $key); $out['api_key'] = $key !== '' ? $key : (string) $old['api_key']; } foreach (['department_id', 'topic_id', 'priority_id'] as $f) { $out[$f] = absint($input[$f] ?? 0); } $out['consent_text'] = wp_kses((string) ($input['consent_text'] ?? ''), [ 'a' => ['href' => [], 'target' => [], 'rel' => []], 'strong' => [], 'em' => [], ]); $out['attachments'] = !empty($input['attachments']) ? 1 : 0; $accent = sanitize_hex_color((string) ($input['accent_color'] ?? '')); $out['accent_color'] = empty($input['accent_auto']) && $accent ? $accent : ''; $out['success_text'] = mb_substr(sanitize_text_field((string) ($input['success_text'] ?? '')), 0, 500); $out['status_page_url'] = esc_url_raw((string) ($input['status_page_url'] ?? '')); $out['min_seconds'] = max(0, min(60, absint($input['min_seconds'] ?? 3))); $out['rate_limit_per_hour'] = max(0, min(100, absint($input['rate_limit_per_hour'] ?? 5))); $out['turnstile_site'] = sanitize_text_field((string) ($input['turnstile_site'] ?? '')); $secret = trim(sanitize_text_field((string) ($input['turnstile_secret'] ?? ''))); $out['turnstile_secret'] = !empty($input['clear_turnstile']) ? '' : ($secret !== '' ? $secret : (string) $old['turnstile_secret']); // „Speichern und Verbindung testen“: prüft gleich die Werte, die gerade gespeichert werden (nicht erst die schon gespeicherten) if (!empty($_POST['dht_test_after']) && current_user_can('manage_options')) { $key = deskhub_tickets_key_from_config() ? (string) DESKHUB_TICKETS_API_KEY : (string) $out['api_key']; $url = defined('DESKHUB_TICKETS_SERVER') && DESKHUB_TICKETS_SERVER !== '' ? (string) DESKHUB_TICKETS_SERVER : (string) $out['server_url']; $norm = Deskhub_Tickets_Client::normalize_url($url); $result = (is_wp_error($norm) || trim($key) === '') ? ['ok' => false, 'message' => __('Bitte Server-Adresse und API-Schlüssel eintragen.', 'deskhub-tickets'), 'canRead' => false] : (new Deskhub_Tickets_Client($norm, trim($key)))->test_connection(); set_transient('deskhub_tickets_test_' . get_current_user_id(), $result, 120); } return $out; } /** „Formulare neu laden“: leert den Zwischenspeicher der Kundenformulare aus DeskHub. */ public static function handle_clear_cache(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('Dafür fehlt die Berechtigung.', 'deskhub-tickets'), '', ['response' => 403]); } check_admin_referer('deskhub_tickets_clear_cache'); Deskhub_Tickets_Client::clear_form_cache(); set_transient('deskhub_tickets_test_' . get_current_user_id(), ['ok' => true, 'message' => __('Die Formulare werden beim nächsten Aufruf neu aus DeskHub geladen.', 'deskhub-tickets'), 'canRead' => true], 120); wp_safe_redirect(admin_url('options-general.php?page=' . self::PAGE . '&dht_tested=1')); exit; } public static function render(): void { if (!current_user_can('manage_options')) { return; } $o = deskhub_tickets_options(); $name = 'deskhub_tickets_options'; $keyFromConfig = deskhub_tickets_key_from_config(); $hasKey = trim((string) $o['api_key']) !== ''; $hasSecret = trim((string) $o['turnstile_secret']) !== ''; $topicList = []; $cl = Deskhub_Tickets_Client::from_settings(); if ($cl) { $tl = $cl->get_topics(); $topicList = is_array($tl) ? $tl : []; } $tk = 'deskhub_tickets_test_' . get_current_user_id(); $test = get_transient($tk); // Ergebnis von „Speichern und Verbindung testen“ bzw. „Formulare neu laden“ (wird nur einmal gezeigt) delete_transient($tk); ?>