class-first-time-wizard-steps.php
11 months ago
class-grace-period-notifications.php
11 months ago
class-passord-reset-2fa.php
11 months ago
class-re-login-2fa.php
11 months ago
class-wizard-steps.php
11 months ago
index.php
11 months ago
class-grace-period-notifications.php
144 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Roles and main settings grace period notifications class. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage views |
| 7 | * |
| 8 | * @copyright 2025 Melapress |
| 9 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 10 | * |
| 11 | * @see https://wordpress.org/plugins/wp-2fa/ |
| 12 | */ |
| 13 | |
| 14 | declare(strict_types=1); |
| 15 | |
| 16 | namespace WP2FA\Admin\Views; |
| 17 | |
| 18 | use WP2FA\Utils\Settings_Utils; |
| 19 | use WP2FA\Admin\Helpers\User_Helper; |
| 20 | |
| 21 | if ( ! class_exists( '\WP2FA\Admin\Views\Grace_Period_Notifications' ) ) { |
| 22 | /** |
| 23 | * Grace_Period_Notifications - Class for rendering the grace period notification settings. |
| 24 | * |
| 25 | * @since 2.5.0 |
| 26 | */ |
| 27 | class Grace_Period_Notifications { |
| 28 | public const GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME = 'grace-policy-notification-show'; |
| 29 | |
| 30 | /** |
| 31 | * Inits all the class related hooks. |
| 32 | * |
| 33 | * @return void |
| 34 | * |
| 35 | * @since 2.5.0 |
| 36 | */ |
| 37 | public static function init() { |
| 38 | |
| 39 | if ( is_admin() ) { |
| 40 | |
| 41 | \add_filter( WP_2FA_PREFIX . 'after_grace_period', array( __CLASS__, 'grace_period_notification_settings' ), 11, 5 ); |
| 42 | \add_filter( WP_2FA_PREFIX . 'loop_settings', array( __CLASS__, 'add_setting_value' ) ); |
| 43 | } |
| 44 | \add_filter( WP_2FA_PREFIX . 'default_settings', array( __CLASS__, 'add_default_settings' ) ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Shows the settings for the grace period notifications behavior. |
| 49 | * |
| 50 | * @param string $content - HTML content. |
| 51 | * @param string $role - The name of the role. |
| 52 | * @param string $name_prefix - Name prefix for the input name, includes the role name if provided. |
| 53 | * @param string $data_role - Data attribute - used by the JS. |
| 54 | * @param string $role_id - The role name, used to identify the inputs. |
| 55 | * |
| 56 | * @return string |
| 57 | * |
| 58 | * @since 2.5.0 |
| 59 | */ |
| 60 | public static function grace_period_notification_settings( string $content, string $role = '', string $name_prefix = '', string $data_role = '', string $role_id = '' ) { |
| 61 | ob_start(); |
| 62 | |
| 63 | $expire_action = Settings_Utils::get_setting_role( $role, self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME, true ); |
| 64 | |
| 65 | ?> |
| 66 | <div class="sub-setting-indent"> |
| 67 | <p class="description" style="margin-top: 15px; margin-bottom: 8px;"> |
| 68 | <?php echo \esc_html__( 'How do you want users to be informed they are enforced to setup 2FA?', 'wp-2fa' ); ?> |
| 69 | </p> |
| 70 | <fieldset> |
| 71 | <label for="dashboard-notification<?php echo \esc_attr( $role_id ); ?>" style="margin-bottom: 10px; display: inline-block;"> |
| 72 | <input type="radio" name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME ); ?>]" |
| 73 | id="dashboard-notification<?php echo \esc_attr( $role_id ); ?>" |
| 74 | <?php echo $data_role; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 75 | value="dashboard-notification" <?php checked( $expire_action, 'dashboard-notification' ); ?> class="js-nested"> |
| 76 | <span><?php echo \esc_html__( 'Show an admin notice in the dashboard', 'wp-2fa' ); ?></span> |
| 77 | </label> |
| 78 | |
| 79 | <br> |
| 80 | <div style="clear:both"> |
| 81 | <label for="after-login-notification<?php echo \esc_attr( $role_id ); ?>"> |
| 82 | <input type="radio" name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME ); ?>]" <?php checked( $expire_action, 'after-login-notification' ); ?> |
| 83 | id="after-login-notification<?php echo \esc_attr( $role_id ); ?>" |
| 84 | <?php echo $data_role; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 85 | value="after-login-notification" <?php checked( $expire_action, 'after-login-notification' ); ?> class="js-nested"> |
| 86 | <span><?php echo \esc_html__( 'Show a notification on a page on its own after the user authenticates and before accessing the dashboard', 'wp-2fa' ); ?></span> |
| 87 | </label> |
| 88 | </div> |
| 89 | </fieldset> |
| 90 | </div> |
| 91 | <?php |
| 92 | $html_content = ob_get_clean(); |
| 93 | |
| 94 | return $content . $html_content; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Adds global plugin setting options. |
| 99 | * |
| 100 | * @param array $loop_settings - Array with current plugin settings. |
| 101 | * |
| 102 | * @return array |
| 103 | * |
| 104 | * @since 2.5.0 |
| 105 | */ |
| 106 | public static function add_setting_value( array $loop_settings ) { |
| 107 | $loop_settings[] = self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME; |
| 108 | |
| 109 | return $loop_settings; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Checks the grace policy setting for the given user. |
| 114 | * |
| 115 | * @param \WP_User $user - The user for which we have to check the settings. |
| 116 | * |
| 117 | * @return bool |
| 118 | * |
| 119 | * @since 2.5.0 |
| 120 | */ |
| 121 | public static function notify_using_dashboard( \WP_User $user ) { |
| 122 | if ( 'dashboard-notification' !== Settings_Utils::get_setting_role( User_Helper::get_user_role( $user ), self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME ) ) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Adds the extension default settings to the main plugin settings |
| 131 | * |
| 132 | * @param array $default_settings - array with plugin default settings. |
| 133 | * |
| 134 | * @return array |
| 135 | * |
| 136 | * @since 2.5.0 |
| 137 | */ |
| 138 | public static function add_default_settings( array $default_settings ) { |
| 139 | $default_settings[ self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME ] = 'after-login-notification'; |
| 140 | return $default_settings; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 |