132 lines
3.3 KiB
PHP
132 lines
3.3 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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolve the interval in whole days represented by a frequency.
|
|
*
|
|
* Used to align the "Remember this browser" trust duration with the user's
|
|
* profile verification frequency. Returns 0 for session-based frequency.
|
|
*
|
|
* @since 1.5.3
|
|
*
|
|
* @param string $frequency Frequency identifier.
|
|
*
|
|
* @return int
|
|
*/
|
|
public static function get_interval_days( string $frequency ): int {
|
|
$seconds = self::get_interval_seconds( $frequency );
|
|
|
|
if ( $seconds <= 0 ) {
|
|
return 0;
|
|
}
|
|
|
|
return intdiv( $seconds, DAY_IN_SECONDS );
|
|
}
|
|
}
|