PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.9.2
WP 2FA – Two-factor authentication for WordPress v2.9.2
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 / SettingsPages / class-settings-page-general.php
wp-2fa / includes / classes / Admin / SettingsPages Last commit date
class-settings-page-email.php 11 months ago class-settings-page-general.php 11 months ago class-settings-page-policies.php 11 months ago class-settings-page-render.php 11 months ago class-settings-page-white-label.php 11 months ago index.php 11 months ago
class-settings-page-general.php
372 lines
1 <?php
2 /**
3 * Generals settings class.
4 *
5 * @package wp2fa
6 * @subpackage settings-pages
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 namespace WP2FA\Admin\SettingsPages;
15
16 use WP2FA\Utils\Debugging;
17 use WP2FA\Utils\Settings_Utils;
18 use WP2FA\WP2FA;
19
20 /*
21 * General settings tab
22 */
23 if ( ! class_exists( '\WP2FA\Admin\SettingsPages\Settings_Page_General' ) ) {
24 /**
25 * Settings_Page_General - Class for handling general settings.
26 *
27 * @since 2.0.0
28 */
29 class Settings_Page_General {
30 /**
31 * Renders the settings.
32 *
33 * @return void
34 *
35 * @since 2.0.0
36 */
37 public static function render() {
38 if ( ! current_user_can( 'manage_options' ) ) {
39 return;
40 }
41 settings_fields( WP_2FA_SETTINGS_NAME );
42 self::no_method_exists();
43 self::disable_brute_force_settings();
44 self::limit_settings_access();
45 self::enable_rest();
46 self::remove_data_upon_uninstall();
47 submit_button( null, 'primary', WP_2FA_SETTINGS_NAME . '[submit]' );
48 }
49
50 /**
51 * Validate options before saving.
52 *
53 * @param array $input The settings array.
54 *
55 * @return array|void
56 *
57 * @since 3.0.0
58 */
59 public static function validate_and_sanitize( $input ) {
60 // Bail if user doesn't have permissions to be here.
61 if ( ! current_user_can( 'manage_options' ) || ! isset( $_POST['action'] ) && ! check_admin_referer( 'wp2fa-step-choose-method' ) ) {
62 return;
63 }
64
65 Debugging::log( 'The following settings will be processed (General): ' . "\n" . wp_json_encode( $input ) );
66
67 $simple_settings_we_can_loop = array(
68 'enable_destroy_session',
69 'limit_access',
70 'enable_rest',
71 'brute_force_disable',
72 'delete_data_upon_uninstall',
73 'method_invalid_setting',
74 );
75
76 /**
77 * Gives the ability to change the default general settings.
78 *
79 * @param array $general_settings - The array with the default settings.
80 *
81 * @since 2.0.0
82 */
83 $simple_settings_we_can_loop = \apply_filters( WP_2FA_PREFIX . 'loop_general_settings', $simple_settings_we_can_loop );
84
85 $settings_to_turn_into_bools = array(
86 'enable_destroy_session',
87 'limit_access',
88 'enable_rest',
89 'brute_force_disable',
90 'delete_data_upon_uninstall',
91 );
92
93 foreach ( $simple_settings_we_can_loop as $simple_setting ) {
94 if ( ! in_array( $simple_setting, $settings_to_turn_into_bools, true ) ) {
95 // Is item is not one of our possible settings we want to turn into a bool, process.
96 $output[ $simple_setting ] = ( isset( $input[ $simple_setting ] ) && ! empty( $input[ $simple_setting ] ) ) ? trim( (string) sanitize_text_field( $input[ $simple_setting ] ) ) : false;
97 } else {
98 // This item is one we treat as a bool, so process correctly.
99 $output[ $simple_setting ] = ( isset( $input[ $simple_setting ] ) && ! empty( $input[ $simple_setting ] ) ) ? true : false;
100 }
101 }
102
103 if ( isset( $input['2fa_settings_last_updated_by'] ) && ! empty( $input['2fa_settings_last_updated_by'] ) ) {
104 $policies = WP2FA::get_wp2fa_setting();
105 if ( false === $policies ) {
106 $policies = WP2FA::get_default_settings();
107 }
108 $policies['2fa_settings_last_updated_by'] = (int) get_current_user_id();
109
110 WP2FA::update_plugin_settings( $policies );
111 }
112
113 // Remove duplicates from settings errors. We do this as this sanitization callback is actually fired twice, so we end up with duplicates when saving the settings for the FIRST TIME only. The issue is not present once the settings are in the DB as the sanitization wont fire again. For details on this core issue - https://core.trac.wordpress.org/ticket/21989.
114 global $wp_settings_errors;
115 if ( isset( $wp_settings_errors ) ) {
116 $errors = array_map( 'unserialize', array_unique( array_map( 'serialize', $wp_settings_errors ) ) );
117 $wp_settings_errors = $errors; // phpcs:ignore
118 }
119
120 /**
121 * Filter the values we are about to store in the plugin settings.
122 *
123 * @param array $output - The output array with all the data we will store in the settings.
124 * @param array $input - The input array with all the data we received from the user.
125 *
126 * @since 2.0.0
127 */
128 $output = \apply_filters( WP_2FA_PREFIX . 'filter_output_content_general_settings', $output, $input );
129
130 // We have overridden any defaults by now so can clear this.
131 Settings_Utils::delete_option( WP_2FA_PREFIX . 'default_settings_applied' );
132
133 Debugging::log( 'The following settings are being saved (General): ' . "\n" . wp_json_encode( $output ) );
134
135 return $output;
136 }
137
138 /**
139 * Updates global settings network options.
140 *
141 * @return void
142 *
143 * @since 2.9.0
144 */
145 public static function update_wp2fa_network_options() {
146 if ( ! \current_user_can( 'manage_options' ) ) {
147 \wp_die( \esc_html__( 'You do not have sufficient permissions to access this page.', 'wp-2fa' ) );
148 }
149
150 if ( isset( $_POST[ WP_2FA_SETTINGS_NAME ] ) ) {
151 check_admin_referer( 'wp_2fa_settings-options' );
152 $options = self::validate_and_sanitize( wp_unslash( $_POST[ WP_2FA_SETTINGS_NAME ] ) );
153 $settings_errors = get_settings_errors( WP_2FA_SETTINGS_NAME );
154 if ( ! empty( $settings_errors ) ) {
155 // redirect back to our options page.
156 wp_safe_redirect(
157 add_query_arg(
158 array(
159 'page' => 'wp-2fa-settings',
160 'wp_2fa_network_settings_error' => urlencode_deep( $settings_errors[0]['message'] ),
161 ),
162 network_admin_url( 'settings.php' )
163 )
164 );
165 exit;
166 }
167 WP2FA::update_plugin_settings( $options, false, WP_2FA_SETTINGS_NAME );
168
169 // redirect back to our options page.
170 wp_safe_redirect(
171 add_query_arg(
172 array(
173 'page' => 'wp-2fa-settings',
174 'tab' => 'generic-settings',
175 'wp_2fa_network_settings_updated' => 'true',
176 ),
177 network_admin_url( 'admin.php' )
178 )
179 );
180 exit;
181 }
182 }
183
184 /**
185 * Limit settings setting.
186 *
187 * @return void
188 *
189 * @since 2.0.0
190 */
191 private static function remove_data_upon_uninstall() {
192 ?>
193 <div class="danger-zone-wrapper">
194 <h3><?php \esc_html_e( 'Do you want to delete the plugin data from the database upon uninstall', 'wp-2fa' ); ?></h3>
195 <p class="description">
196 <?php \esc_html_e( 'The plugin saves its settings in the WordPress database. By default the plugin settings are kept in the database so if it is installed again, you do not have to reconfigure the plugin. Enable this setting to delete the plugin settings from the database upon uninstall.', 'wp-2fa' ); ?>
197 </p>
198 <table class="form-table">
199 <tbody>
200 <tr>
201 <th><label for="delete_data"><?php \esc_html_e( 'Delete data', 'wp-2fa' ); ?></label></th>
202 <td>
203 <fieldset>
204 <input type="checkbox" id="delete_data" name="wp_2fa_settings[delete_data_upon_uninstall]" value="delete_data_upon_uninstall"
205 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'delete_data_upon_uninstall' ), true ); ?>
206 >
207 <?php \esc_html_e( 'Delete data upon uninstall', 'wp-2fa' ); ?>
208 </fieldset>
209 </td>
210 </tr>
211 </tbody>
212 </table>
213 </div>
214 <?php
215 $last_user_to_update_settings = get_current_user_id();
216 ?>
217 <input type="hidden" id="2fa_main_user" name="wp_2fa_settings[2fa_settings_last_updated_by]" value="<?php echo \esc_attr( $last_user_to_update_settings ); ?>">
218 <?php
219 }
220
221 /**
222 * Limit settings access.
223 *
224 * @return void
225 *
226 * @since 2.0.0
227 */
228 private static function limit_settings_access() {
229 ?>
230 <br>
231 <h3><?php \esc_html_e( 'Limit 2FA settings access', 'wp-2fa' ); ?></h3>
232 <p class="description">
233 <?php \esc_html_e( 'Use this setting to hide this plugin configuration area from all other admins.', 'wp-2fa' ); ?>
234 </p>
235 <table class="form-table">
236 <tbody>
237 <tr>
238 <th><label for="limit_access"><?php \esc_html_e( 'Limit access to 2FA settings', 'wp-2fa' ); ?></label></th>
239 <td>
240 <fieldset>
241 <input type="checkbox" id="limit_access" name="wp_2fa_settings[limit_access]" value="limit_access"
242 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'limit_access' ), true ); ?>
243 >
244 <?php \esc_html_e( 'Hide settings from other administrators', 'wp-2fa' ); ?>
245 </fieldset>
246 </td>
247 </tr>
248 </tbody>
249 </table>
250 <?php
251 }
252
253 /**
254 * Enable REST API
255 *
256 * @return void
257 *
258 * @since 2.9.1
259 */
260 private static function enable_rest() {
261 ?>
262 <br>
263 <h3><?php \esc_html_e( 'Enable the REST API endpoints for 2FA', 'wp-2fa' ); ?></h3>
264 <p class="description">
265 <?php \esc_html_e( 'Choose how WP 2FA verifies the 2FA by default. The native method works for most setups, but you can switch to REST API verification if needed. Only change this setting if you are experiencing issues with the default method.', 'wp-2fa' ); ?>
266 </p>
267 <table class="form-table">
268 <tbody>
269 <tr>
270 <th><label for="enable_rest"><?php \esc_html_e( 'Select the default 2FA verification mechanism', 'wp-2fa' ); ?></label></th>
271 <td>
272 <fieldset>
273 <input type="radio" id="enable_native" name="wp_2fa_settings[enable_rest]" value=""
274 <?php \checked( false, WP2FA::get_wp2fa_general_setting( 'enable_rest' ), true ); ?>
275 >
276 <label for="enable_native"><?php \esc_html_e( 'Native', 'wp-2fa' ); ?></label>
277 <br><input type="radio" id="enable_rest" name="wp_2fa_settings[enable_rest]" value="enable_rest"
278 <?php \checked( true, WP2FA::get_wp2fa_general_setting( 'enable_rest' ), true ); ?>
279 >
280 <label for="enable_rest">
281 <?php \esc_html_e( 'REST API', 'wp-2fa' ); ?></label>
282 </fieldset>
283 </td>
284 </tr>
285 </tbody>
286 </table>
287 <?php
288 }
289
290 /**
291 * Disable brute force protection setting.
292 *
293 * @return void
294 *
295 * @since 2.5.0
296 */
297 private static function disable_brute_force_settings() {
298 ?>
299 <br>
300 <h3><?php \esc_html_e( 'Disable 2FA code brute force protection', 'wp-2fa' ); ?></h3>
301 <p class="description">
302 <?php \esc_html_e( 'When using email and SMS 2FA, the plugin sends the users a new one-time code whenever they enter the wrong code when logging in. This is a security enhancement, a sort of brute force protection. You can disable this feature from the below setting, however, it is not recommended.', 'wp-2fa' ); ?>
303 </p>
304 <table class="form-table">
305 <tbody>
306 <tr>
307 <th><label for="brute_force_disable"><?php \esc_html_e( 'Disable one-time code brute force protection', 'wp-2fa' ); ?></label></th>
308 <td>
309 <fieldset>
310 <input type="checkbox" id="brute_force_disable" name="wp_2fa_settings[brute_force_disable]" value="brute_force_disable"
311 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'brute_force_disable' ), true ); ?>
312 >
313 </fieldset>
314 </td>
315 </tr>
316 </tbody>
317 </table>
318 <?php
319 }
320
321 /**
322 * Rendering settings when there are no methods.
323 *
324 * @return void
325 *
326 * @since 2.2.0
327 */
328 private static function no_method_exists() {
329 ?>
330 <p class="description">
331 <?php
332 printf(
333 // translators: support email.
334 \esc_html__( 'Use this setting below to configure the properties of the two-factor authentication on your website and how users use it. If you have any questions send us an email at %1$s.', 'wp-2fa' ),
335 '<a href="mailto:support@melapress.com">support@melapress.com</a>'
336 );
337 ?>
338 </p>
339 <h3><?php \esc_html_e( 'What should the plugin do if the 2FA method used during a user login is unavailable', 'wp-2fa' ); ?></h3>
340 <p class="description">
341 <?php \esc_html_e( 'There may be cases in which the 2FA service is unavailable when a user is trying to log in. For example, the service is unreachable or there are no credits to complete the action. In this case you can configure the plugin to either block the login process, or allow the user to log in without 2FA authentication.', 'wp-2fa' ); ?>
342 </p>
343 <table class="form-table">
344 <tbody>
345 <tr>
346 <th><label for="no-methods"><?php \esc_html_e( 'Select action', 'wp-2fa' ); ?></label></th>
347 <td>
348 <fieldset class="contains-hidden-inputs" id="no-methods">
349 <label for="login_block">
350 <input type="radio" name="wp_2fa_settings[method_invalid_setting]" id="login_block" value="login_block"
351 <?php checked( WP2FA::get_wp2fa_general_setting( 'method_invalid_setting' ), 'login_block' ); ?>
352 >
353 <span><?php \esc_html_e( 'Block the login.', 'wp-2fa' ); ?></span>
354 </label>
355
356 <br/>
357 <label for="allow_login_without_method">
358 <input type="radio" name="wp_2fa_settings[method_invalid_setting]" id="allow_login_without_method" value="allow_login_without_method"
359 <?php checked( WP2FA::get_wp2fa_general_setting( 'method_invalid_setting' ), 'allow_login_without_method' ); ?>
360 data-unhide-when-checked=".custom-from-inputs">
361 <span><?php \esc_html_e( 'Allow the login without 2FA', 'wp-2fa' ); ?></span>
362 </label>
363 </fieldset>
364 </td>
365 </tr>
366 </tbody>
367 </table>
368 <?php
369 }
370 }
371 }
372