robotstxt-2fa/includes/class-frequency-options.php
2026-06-05 14:04:51 +00:00

110 lines
2.9 KiB
PHP

<?php
/**
* Normalize and expose two-factor verification frequency options.
*
* @package Robotstxt_2FA
*/
namespace Robotstxt\TwoFA;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Utility helpers for two-factor verification frequency handling.
*/
class Frequency_Options {
/**
* Frequency key representing per-session verification.
*/
public const FREQUENCY_SESSION = 'session';
/**
* Frequency key representing at most one verification per day.
*/
public const FREQUENCY_DAILY = 'daily';
/**
* Frequency key representing at most one verification per week.
*/
public const FREQUENCY_WEEKLY = 'weekly';
/**
* Frequency key representing at most one verification per 28 days.
*/
public const FREQUENCY_MONTHLY = 'monthly';
/**
* Retrieve the available frequency options for display.
*
* @return array<string, array<string, string>>
*/
public static function get_options(): array {
return array(
self::FREQUENCY_SESSION => array(
'label' => __( 'Every login', 'robotstxt-2fa' ),
'description' => __( 'Always require a new verification code after the password step.', 'robotstxt-2fa' ),
),
self::FREQUENCY_DAILY => array(
'label' => __( 'Daily', 'robotstxt-2fa' ),
'description' => __( 'Remember successful verifications for 24 hours per device.', 'robotstxt-2fa' ),
),
self::FREQUENCY_WEEKLY => array(
'label' => __( 'Weekly', 'robotstxt-2fa' ),
'description' => __( 'Remember successful verifications for 7 days per device.', 'robotstxt-2fa' ),
),
self::FREQUENCY_MONTHLY => array(
'label' => __( 'Monthly', 'robotstxt-2fa' ),
'description' => __( 'Remember successful verifications for 28 days per device.', 'robotstxt-2fa' ),
),
);
}
/**
* Retrieve the allowed option keys.
*
* @return array<int, string>
*/
public static function get_allowed_keys(): array {
return array_keys( self::get_options() );
}
/**
* Sanitize a frequency key against the supported values.
*
* @param string $frequency Submitted frequency value.
*
* @return string
*/
public static function sanitize( string $frequency ): string {
$frequency = sanitize_key( $frequency );
if ( in_array( $frequency, self::get_allowed_keys(), true ) ) {
return $frequency;
}
return self::FREQUENCY_SESSION;
}
/**
* Resolve the interval in seconds represented by a frequency.
*
* @param string $frequency Frequency identifier.
*
* @return int
*/
public static function get_interval_seconds( string $frequency ): int {
switch ( $frequency ) {
case self::FREQUENCY_DAILY:
return DAY_IN_SECONDS;
case self::FREQUENCY_WEEKLY:
return WEEK_IN_SECONDS;
case self::FREQUENCY_MONTHLY:
return 28 * DAY_IN_SECONDS;
case self::FREQUENCY_SESSION:
default:
return 0;
}
}
}