class-abstract-migration.php
14 hours ago
class-date-time-utils.php
14 hours ago
class-debugging.php
14 hours ago
class-generate-modal.php
14 hours ago
class-migration.php
14 hours ago
class-request-utils.php
14 hours ago
class-settings-utils.php
14 hours ago
class-upgrade-guard.php
14 hours ago
class-user-utils.php
14 hours ago
class-validator.php
14 hours ago
class-white-label.php
14 hours ago
index.php
14 hours ago
class-date-time-utils.php
80 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for date / time manipulation. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage utils |
| 7 | * @copyright 2026 Melapress |
| 8 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 | * @link https://wordpress.org/plugins/wp-2fa/ |
| 10 | * @since 1.4.2 |
| 11 | */ |
| 12 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace WP2FA\Utils; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 18 | |
| 19 | use WP2FA\Admin\Helpers\User_Helper; |
| 20 | |
| 21 | if ( ! class_exists( '\WP2FA\Utils\Date_Time_Utils' ) ) { |
| 22 | /** |
| 23 | * Utility class for date and time manipulation, format conversion and so on. |
| 24 | * |
| 25 | * @package WP2FA\Utils |
| 26 | * |
| 27 | * @since 1.4.2 |
| 28 | */ |
| 29 | class Date_Time_Utils { |
| 30 | |
| 31 | /** |
| 32 | * Formats the date string |
| 33 | * |
| 34 | * @param string|null $grace_policy Grace policy value. |
| 35 | * @param int $grace_expiry Expiration time as unix based timestamp. |
| 36 | * |
| 37 | * @return string Translated grace period expiration string. |
| 38 | * |
| 39 | * @since 1.4.2 |
| 40 | */ |
| 41 | public static function format_grace_period_expiration_string( $grace_policy = null, $grace_expiry = -1 ) { |
| 42 | if ( null === $grace_policy ) { |
| 43 | $grace_policy = Settings_Utils::get_setting_role( User_Helper::get_user_role(), 'grace-policy' ); |
| 44 | } |
| 45 | |
| 46 | if ( 'no-grace-period' === $grace_policy ) { |
| 47 | return \esc_html__( 'no grace period', 'wp-2fa' ); |
| 48 | } |
| 49 | |
| 50 | if ( -1 === $grace_expiry ) { |
| 51 | if ( 'use-grace-period' === $grace_policy ) { |
| 52 | $grace_period = Settings_Utils::get_setting_role( User_Helper::get_user_role(), 'grace-period' ); |
| 53 | $grace_period_denominator = Settings_Utils::get_setting_role( User_Helper::get_user_role(), 'grace-period-denominator' ); |
| 54 | $grace_period_string = sanitize_text_field( $grace_period . ' ' . $grace_period_denominator ); |
| 55 | $grace_expiry = (int) strtotime( $grace_period_string ); |
| 56 | } else { |
| 57 | // this will probably never be reached, leaving it here for now just in case. |
| 58 | $grace_expiry = time(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $grace_expiry = \gmdate( 'Y-m-d H:i:s', $grace_expiry ); |
| 63 | |
| 64 | $expiration_date_time = implode( |
| 65 | ' ', |
| 66 | array( |
| 67 | // Purposefully not using the SettingsUtil class as we don't want this prefixed. |
| 68 | \get_date_from_gmt( $grace_expiry, get_option( 'date_format' ) ), |
| 69 | \get_date_from_gmt( $grace_expiry, get_option( 'time_format' ) ), |
| 70 | // wp_date( get_option( 'date_format' ), $grace_expiry ), |
| 71 | // wp_date( get_option( 'time_format' ), $grace_expiry ), |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | /* translators: Grace period expiration label. %s: Date and time formatted using WordPress date and time formats. */ |
| 76 | return sprintf( \esc_html__( 'before %s', 'wp-2fa' ), esc_html( $expiration_date_time ) ); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 |