<?php
if (!defined('ABSPATH')) exit;

if (!function_exists('custausu_admin_page_notice_get_param')) {
  function custausu_admin_page_notice_get_param(string $key): string {
    $key = sanitize_key($key);

    if ($key === '') {
      return '';
    }

    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin notice query arg after redirect; no state change is performed here.
    if (!isset($_GET[$key])) {
      return '';
    }

    // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Read-only admin notice query arg; unslashed and sanitized immediately below.
    $raw = wp_unslash($_GET[$key]);

    if (is_array($raw)) {
      return '';
    }

    return sanitize_text_field(rawurldecode((string) $raw));
  }
}

if (!function_exists('custausu_admin_render_page_notices')) {
  function custausu_admin_render_page_notices(callable $T): string {
    $settings_updated = custausu_admin_page_notice_get_param('settings-updated');
    if ($settings_updated !== '') {
      add_settings_error('custausu_messages', 'custausu_message', $T('Settings saved.','Impostazioni salvate.'), 'updated');
    }

    $notice = sanitize_key(custausu_admin_page_notice_get_param('custausu_notice'));
    if ($notice === 'settings_reset') {
      add_settings_error('custausu_messages', 'custausu_settings_reset', $T('CAS settings have been reset.','Le impostazioni CAS sono state resettate.'), 'updated');
    }

    ob_start();
    settings_errors('custausu_messages');
    return (string) ob_get_clean();
  }
}

