PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.5.0
WP 2FA – Two-factor authentication for WordPress v2.5.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 / App / grace-period / class-grace-period.php
wp-2fa / includes / classes / App / grace-period Last commit date
class-grace-period.php 2 years ago index.php 2 years ago
class-grace-period.php
187 lines
1 <?php
2 /**
3 * Main file of the grace period settings extension class.
4 *
5 * @package wp2fa
6 * @subpackage grace-period
7 * @since 2.0.0
8 * @copyright %%YEAR%% Melapress
9 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
10 * @link https://wordpress.org/plugins/wp-2fa/
11 */
12
13 namespace WP2FA\App;
14
15 use WP2FA\Admin\Helpers\User_Helper;
16 use WP2FA\Admin\Controllers\Settings;
17 use WP2FA\Extensions\RoleSettings\Role_Settings_Controller;
18
19 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
20
21 /**
22 * Grace period class
23 */
24 if ( ! class_exists( '\WP2FA\App\Grace_Period' ) ) {
25
26 /**
27 * Responsible for users which grace period has expired
28 *
29 * Gives the administrator the ability to select what action the plugin should take:
30 * - Lock the user
31 * - Force the user to set up their 2FA immediately
32 *
33 * @since 2.0.0
34 */
35 class Grace_Period {
36
37 /**
38 * Inits all the hooks
39 *
40 * @return void
41 *
42 * @since 2.0.0
43 */
44 public static function init() {
45 \add_filter( WP_2FA_PREFIX . 'after_grace_period', array( __CLASS__, 'grace_period_options' ), 10, 5 );
46 \add_filter( WP_2FA_PREFIX . 'loop_settings', array( __CLASS__, 'add_setting_value' ) );
47 \add_filter( WP_2FA_PREFIX . 'default_settings', array( __CLASS__, 'add_default_settings' ) );
48 \add_filter( WP_2FA_PREFIX . 'should_account_be_locked_on_grace_period_expiration', array( __CLASS__, 'maybe_prevent_account_lock' ), 10, 2 );
49 }
50
51 /**
52 * Prevent account locking if allowed, depending on the plugin settings.
53 *
54 * @param boolean $state - Current state of the checking.
55 * @param \WP_User $user - The User class.
56 *
57 * @return bool
58 *
59 * @since 2.0.0
60 */
61 public static function maybe_prevent_account_lock( bool $state, \WP_User $user ) {
62 if ( 'configure-right-away' === Settings::get_role_or_default_setting( 'grace-policy-after-expire-action', $user ) ) {
63 User_Helper::set_user_enforced_instantly( true, $user );
64 return false;
65 }
66
67 return $state;
68 }
69
70 /**
71 * Collects the options for the main plugin settings page and returns them
72 *
73 * @param string $content - HTML content.
74 * @param string $role - The name of the role.
75 * @param string $name_prefix - Name prefix for the input name, includes the role name if provided.
76 * @param string $data_role - Data attribute - used by the JS.
77 * @param string $role_id - The role name, used to identify the inputs.
78 *
79 * @return string
80 *
81 * @since 2.0.0
82 */
83 public static function grace_period_options( string $content, string $role = '', string $name_prefix = '', string $data_role = '', string $role_id = '' ) {
84 return $content . self::grace_options( $role, $name_prefix, $data_role, $role_id );
85 }
86
87 /**
88 * Adds global plugin setting options
89 *
90 * @param array $loop_settings - Array with current plugin settings.
91 *
92 * @return array
93 *
94 * @since 2.0.0
95 */
96 public static function add_setting_value( array $loop_settings ) {
97 $loop_settings[] = 'grace-policy-after-expire-action';
98
99 return $loop_settings;
100 }
101
102 /**
103 * Checks the grace policy setting for the given user
104 *
105 * @param \WP_User $user - The user for which we have to check the settings.
106 *
107 * @return boolean
108 *
109 * @since 2.0.0
110 */
111 public static function is_set_up_immediately_set( \WP_User $user ) {
112 if ( 'configure-right-away' === Settings::get_role_or_default_setting( 'grace-policy-after-expire-action', $user ) ) {
113 return true;
114 }
115
116 return false;
117 }
118
119 /**
120 * Adds the extension default settings to the main plugin settings
121 *
122 * @param array $default_settings - array with plugin default settings.
123 *
124 * @return array
125 *
126 * @since 2.0.0
127 */
128 public static function add_default_settings( array $default_settings ) {
129 $default_settings['grace-policy-after-expire-action'] = 'configure-right-away';
130 return $default_settings;
131 }
132
133 /**
134 * Adds options to the settings page
135 *
136 * @param string $role - The name of the role.
137 * @param string $name_prefix - Name prefix for the input name, includes the role name if provided.
138 * @param string $data_role - Data attribute - used by the JS.
139 * @param string $role_id - The role name, used to identify the inputs.
140 *
141 * @return string
142 *
143 * @since 2.0.0
144 */
145 private static function grace_options( string $role = '', string $name_prefix = '', string $data_role = '', string $role_id = '' ): string {
146 ob_start();
147
148 if ( class_exists( 'WP2FA\Extensions\RoleSettings\Role_Settings_Controller' ) ) {
149 $expire_action = Role_Settings_Controller::get_setting( $role, 'grace-policy-after-expire-action', true );
150 } else {
151 $expire_action = Settings::get_role_or_default_setting( 'grace-policy-after-expire-action', null, null, true );
152 }
153 ?>
154 <div class="sub-setting-indent">
155 <p class="description" style="margin-top: 15px; margin-bottom: 8px;">
156 <?php echo \esc_html__( 'What should the plugin do with users who do not configure 2FA within the grace period?', 'wp-2fa' ); ?>
157 </p>
158 <fieldset>
159 <label for="configure-right-away<?php echo \esc_attr( $role_id ); ?>" style="margin-bottom: 10px; display: inline-block;">
160 <input type="radio" name="<?php echo \esc_attr( $name_prefix ); ?>[grace-policy-after-expire-action]"
161 id="configure-right-away<?php echo \esc_attr( $role_id ); ?>"
162 <?php echo $data_role; // phpcs:ignore?>
163 value="configure-right-away" <?php checked( $expire_action, 'configure-right-away' ); ?> class="js-nested">
164 <span><?php echo \esc_html__( 'Do not let them access the dashboard / user page once they log in until they configure 2FA', 'wp-2fa' ); ?></span>
165 </label>
166
167 <br>
168 <div style="clear:both">
169 <label for="manual-block<?php echo \esc_attr( $role_id ); ?>">
170 <input type="radio" name="<?php echo \esc_attr( $name_prefix ); ?>[grace-policy-after-expire-action]" <?php checked( $expire_action, 'manual-block' ); ?>
171 id="manual-block<?php echo \esc_attr( $role_id ); ?>"
172 <?php echo $data_role; // phpcs:ignore?>
173 value="manual-block" class="js-nested">
174 <span><?php echo \esc_html__( 'Block the user (administrators have to manually unblock them)', 'wp-2fa' ); ?></span>
175 </label>
176 </div>
177 </fieldset>
178 </div>
179 <?php
180 $html_content = ob_get_contents();
181 ob_end_clean();
182
183 return $html_content;
184 }
185 }
186 }
187