<?php

/**
 * LolaCore � NonceManager
 *
 * @package LolaCore
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\Security;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class NonceManager
{
    public const ACTION_CHAT    = 'lolacore_chat';
    public const ACTION_SETTINGS = 'lolacore_settings';
    public const ACTION_SCAN    = 'lolacore_scan';

    /**
     * Creates a cryptographic token tied to a specific action.
     *
     * @param string $action Action name.
     * @return string
     */
    public static function create(string $action): string
    {
        return wp_create_nonce($action);
    }

    /**
     * Verifies that a correct nonce was used with time limit.
     *
     * @param string $nonce Nonce to verify.
     * @param string $action Action name.
     * @return bool
     */
    public static function verify(string $nonce, string $action): bool
    {
        return wp_verify_nonce($nonce, $action) !== false;
    }

    /**
     * Retrieve or display nonce hidden field for forms.
     *
     * @param string $action Action name.
     * @return string
     */
    public static function field(string $action): string
    {
        return wp_nonce_field($action, '_lolacore_nonce', true, false);
    }
}
