<?php
/**
 * Abstract class for a simple nonce route.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Abstracts\REST;

defined( 'ABSPATH' ) || exit;

/**
 * Class Abstract_Simple_Nonce_Route
 *
 * @used Express Order
 */
abstract class Abstract_Route_Nonce extends Abstract_Route {

	public const NONCE_ACTION = 'wp_rest';

	/**
	 * {@inheritDoc}
	 */
	protected function default_args(): array {
		return array(
			'_nonce' => array(
				'description'       => 'Nonce for the request.',
				'type'              => 'string',
				'required'          => true,
				'validate_callback' => array( $this, 'verify_nonce' ),
			),
		);
	}

	/**
	 * Creates a nonce.
	 *
	 * @return string
	 */
	public static function create_nonce(): string {
		return wp_create_nonce( self::NONCE_ACTION );
	}

	/**
	 * Verifies the provided nonce.
	 *
	 * @param string $param The nonce to verify.
	 *
	 * @return bool
	 */
	public function verify_nonce( string $param ): bool {
		return (bool) wp_verify_nonce( $param, self::NONCE_ACTION );
	}
}
