PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.8.0
WP 2FA – Two-factor authentication for WordPress v2.8.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Admin / Views / class-grace-period-notifications.php
wp-2fa / includes / classes / Admin / Views Last commit date
class-first-time-wizard-steps.php 2 years ago class-grace-period-notifications.php 2 years ago class-passord-reset-2fa.php 2 years ago class-re-login-2fa.php 2 years ago class-wizard-steps.php 2 years ago index.php 2 years ago
class-grace-period-notifications.php
148 lines
1 <?php
2 /**
3 * Roles and main settings grace period notifications class.
4 *
5 * @package wp2fa
6 * @subpackage views
7 *
8 * @copyright 2024 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\Admin\Controllers\Settings;
19 use WP2FA\Extensions\RoleSettings\Role_Settings_Controller;
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 if ( class_exists( 'WP2FA\Extensions\RoleSettings\Role_Settings_Controller' ) ) {
64 $expire_action = Role_Settings_Controller::get_setting( $role, self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME, true );
65 } else {
66 $expire_action = Settings::get_role_or_default_setting( self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME, null, null, true );
67 }
68 ?>
69 <div class="sub-setting-indent">
70 <p class="description" style="margin-top: 15px; margin-bottom: 8px;">
71 <?php echo \esc_html__( 'How do you want users to be informed they are enforced to setup 2FA?', 'wp-2fa' ); ?>
72 </p>
73 <fieldset>
74 <label for="dashboard-notification<?php echo \esc_attr( $role_id ); ?>" style="margin-bottom: 10px; display: inline-block;">
75 <input type="radio" name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME ); ?>]"
76 id="dashboard-notification<?php echo \esc_attr( $role_id ); ?>"
77 <?php echo $data_role; // phpcs:ignore?>
78 value="dashboard-notification" <?php checked( $expire_action, 'dashboard-notification' ); ?> class="js-nested">
79 <span><?php echo \esc_html__( 'Show an admin notice in the dashboard', 'wp-2fa' ); ?></span>
80 </label>
81
82 <br>
83 <div style="clear:both">
84 <label for="after-login-notification<?php echo \esc_attr( $role_id ); ?>">
85 <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' ); ?>
86 id="after-login-notification<?php echo \esc_attr( $role_id ); ?>"
87 <?php echo $data_role; // phpcs:ignore?>
88 value="after-login-notification" <?php checked( $expire_action, 'after-login-notification' ); ?> class="js-nested">
89 <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>
90 </label>
91 </div>
92 </fieldset>
93 </div>
94 <?php
95 $html_content = ob_get_contents();
96 ob_end_clean();
97
98 return $content . $html_content;
99 }
100
101 /**
102 * Adds global plugin setting options.
103 *
104 * @param array $loop_settings - Array with current plugin settings.
105 *
106 * @return array
107 *
108 * @since 2.5.0
109 */
110 public static function add_setting_value( array $loop_settings ) {
111 $loop_settings[] = self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME;
112
113 return $loop_settings;
114 }
115
116 /**
117 * Checks the grace policy setting for the given user.
118 *
119 * @param \WP_User $user - The user for which we have to check the settings.
120 *
121 * @return bool
122 *
123 * @since 2.5.0
124 */
125 public static function notify_using_dashboard( \WP_User $user ) {
126 if ( 'dashboard-notification' !== Settings::get_role_or_default_setting( self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME, $user ) ) {
127 return false;
128 }
129
130 return true;
131 }
132
133 /**
134 * Adds the extension default settings to the main plugin settings
135 *
136 * @param array $default_settings - array with plugin default settings.
137 *
138 * @return array
139 *
140 * @since 2.5.0
141 */
142 public static function add_default_settings( array $default_settings ) {
143 $default_settings[ self::GRACE_PERIOD_NOTIFICATION_SETTINGS_NAME ] = 'after-login-notification';
144 return $default_settings;
145 }
146 }
147 }
148