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 / Admin / SettingsPages / class-settings-page-general.php
wp-2fa / includes / classes / Admin / SettingsPages Last commit date
class-settings-page-email.php 2 years ago class-settings-page-general.php 2 years ago class-settings-page-policies.php 2 years ago class-settings-page-render.php 2 years ago class-settings-page-white-label.php 2 years ago index.php 2 years ago
class-settings-page-general.php
324 lines
1 <?php
2 /**
3 * Generals settings class.
4 *
5 * @package wp2fa
6 * @subpackage settings-pages
7 *
8 * @copyright %%YEAR%% 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 settings_fields( WP_2FA_SETTINGS_NAME );
39 self::no_method_exists();
40 self::disable_brute_force_settings();
41 self::limit_settings_access();
42 self::remove_data_upon_uninstall();
43 submit_button( null, 'primary', WP_2FA_SETTINGS_NAME . '[submit]' );
44 }
45
46 /**
47 * Validate options before saving.
48 *
49 * @param array $input The settings array.
50 *
51 * @return array|void
52 */
53 public static function validate_and_sanitize( $input ) {
54 // Bail if user doesn't have permissions to be here.
55 if ( ! current_user_can( 'manage_options' ) || ! isset( $_POST['action'] ) && ! check_admin_referer( 'wp2fa-step-choose-method' ) ) {
56 return;
57 }
58
59 Debugging::log( 'The following settings will be processed (General): ' . "\n" . wp_json_encode( $input ) );
60
61 $simple_settings_we_can_loop = array(
62 'enable_destroy_session',
63 'limit_access',
64 'brute_force_disable',
65 'delete_data_upon_uninstall',
66 'method_invalid_setting',
67 );
68
69 /**
70 * Gives the ability to change the default general settings.
71 *
72 * @param array $general_settings - The array with the default settings.
73 *
74 * @since 2.0.0
75 */
76 $simple_settings_we_can_loop = apply_filters( WP_2FA_PREFIX . 'loop_general_settings', $simple_settings_we_can_loop );
77
78 $settings_to_turn_into_bools = array(
79 'enable_destroy_session',
80 'limit_access',
81 'brute_force_disable',
82 'delete_data_upon_uninstall',
83 );
84
85 foreach ( $simple_settings_we_can_loop as $simple_setting ) {
86 if ( ! in_array( $simple_setting, $settings_to_turn_into_bools, true ) ) {
87 // Is item is not one of our possible settings we want to turn into a bool, process.
88 $output[ $simple_setting ] = ( isset( $input[ $simple_setting ] ) && ! empty( $input[ $simple_setting ] ) ) ? trim( sanitize_text_field( $input[ $simple_setting ] ) ) : false;
89 } else {
90 // This item is one we treat as a bool, so process correctly.
91 $output[ $simple_setting ] = ( isset( $input[ $simple_setting ] ) && ! empty( $input[ $simple_setting ] ) ) ? true : false;
92 }
93 }
94
95 if ( isset( $input['2fa_settings_last_updated_by'] ) && ! empty( $input['2fa_settings_last_updated_by'] ) ) {
96 $policies = WP2FA::get_wp2fa_setting();
97 if ( false === $policies ) {
98 $policies = WP2FA::get_default_settings();
99 }
100 $policies['2fa_settings_last_updated_by'] = (int) $input['2fa_settings_last_updated_by'];
101
102 WP2FA::update_plugin_settings( $policies );
103 }
104
105 // 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.
106 global $wp_settings_errors;
107 if ( isset( $wp_settings_errors ) ) {
108 $errors = array_map( 'unserialize', array_unique( array_map( 'serialize', $wp_settings_errors ) ) );
109 $wp_settings_errors = $errors; // phpcs:ignore
110 }
111
112 /**
113 * Filter the values we are about to store in the plugin settings.
114 *
115 * @param array $output - The output array with all the data we will store in the settings.
116 * @param array $input - The input array with all the data we received from the user.
117 *
118 * @since 2.0.0
119 */
120 $output = apply_filters( WP_2FA_PREFIX . 'filter_output_content_general_settings', $output, $input );
121
122 // We have overridden any defaults by now so can clear this.
123 Settings_Utils::delete_option( WP_2FA_PREFIX . 'default_settings_applied' );
124
125 Debugging::log( 'The following settings are being saved (General): ' . "\n" . wp_json_encode( $output ) );
126
127 return $output;
128 }
129
130 /**
131 * Updates global settings network options.
132 *
133 * @return void
134 *
135 * @SuppressWarnings(PHPMD.ExitExpressions)
136 */
137 public static function update_wp2fa_network_options() {
138 if ( isset( $_POST[ WP_2FA_SETTINGS_NAME ] ) ) {
139 check_admin_referer( 'wp_2fa_settings-options' );
140 $options = self::validate_and_sanitize(wp_unslash($_POST[WP_2FA_SETTINGS_NAME])); // phpcs:ignore
141 $settings_errors = get_settings_errors( WP_2FA_SETTINGS_NAME );
142 if ( ! empty( $settings_errors ) ) {
143 // redirect back to our options page.
144 wp_safe_redirect(
145 add_query_arg(
146 array(
147 'page' => 'wp-2fa-settings',
148 'wp_2fa_network_settings_error' => urlencode_deep( $settings_errors[0]['message'] ),
149 ),
150 network_admin_url( 'settings.php' )
151 )
152 );
153 exit;
154 }
155 WP2FA::update_plugin_settings( $options, false, WP_2FA_SETTINGS_NAME );
156
157 // redirect back to our options page.
158 wp_safe_redirect(
159 add_query_arg(
160 array(
161 'page' => 'wp-2fa-settings',
162 'tab' => 'generic-settings',
163 'wp_2fa_network_settings_updated' => 'true',
164 ),
165 network_admin_url( 'admin.php' )
166 )
167 );
168 exit;
169 }
170 }
171
172 /**
173 * Limit settings setting.
174 *
175 * @return void
176 *
177 * @since 2.0.0
178 */
179 private static function remove_data_upon_uninstall() {
180 ?>
181 <div class="danger-zone-wrapper">
182 <h3><?php esc_html_e( 'Do you want to delete the plugin data from the database upon uninstall?', 'wp-2fa' ); ?></h3>
183 <p class="description">
184 <?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' ); ?>
185 </p>
186 <table class="form-table">
187 <tbody>
188 <tr>
189 <th><label for="delete_data"><?php esc_html_e( 'Delete data', 'wp-2fa' ); ?></label></th>
190 <td>
191 <fieldset>
192 <input type="checkbox" id="delete_data" name="wp_2fa_settings[delete_data_upon_uninstall]" value="delete_data_upon_uninstall"
193 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'delete_data_upon_uninstall' ), true ); ?>
194 >
195 <?php esc_html_e( 'Delete data upon uninstall', 'wp-2fa' ); ?>
196 </fieldset>
197 </td>
198 </tr>
199 </tbody>
200 </table>
201 </div>
202 <?php
203 $last_user_to_update_settings = get_current_user_id();
204
205 ?>
206 <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 ); ?>">
207 <?php
208 }
209
210 /**
211 * Limit settings setting.
212 *
213 * @return void
214 *
215 * @since 2.0.0
216 */
217 private static function limit_settings_access() {
218 ?>
219 <br>
220 <h3><?php esc_html_e( 'Limit 2FA settings access?', 'wp-2fa' ); ?></h3>
221 <p class="description">
222 <?php esc_html_e( 'Use this setting to hide this plugin configuration area from all other admins.', 'wp-2fa' ); ?>
223 </p>
224 <table class="form-table">
225 <tbody>
226 <tr>
227 <th><label for="limit_access"><?php esc_html_e( 'Limit access to 2FA settings', 'wp-2fa' ); ?></label></th>
228 <td>
229 <fieldset>
230 <input type="checkbox" id="limit_access" name="wp_2fa_settings[limit_access]" value="limit_access"
231 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'limit_access' ), true ); ?>
232 >
233 <?php esc_html_e( 'Hide settings from other administrators', 'wp-2fa' ); ?>
234 </fieldset>
235 </td>
236 </tr>
237 </tbody>
238 </table>
239 <?php
240 }
241
242 /**
243 * Disable brute force setting.
244 *
245 * @return void
246 *
247 * @since 2.5.0
248 */
249 private static function disable_brute_force_settings() {
250 ?>
251 <br>
252 <h3><?php esc_html_e( 'Disable 2FA code brute force protection?', 'wp-2fa' ); ?></h3>
253 <p class="description">
254 <?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' ); ?>
255 </p>
256 <table class="form-table">
257 <tbody>
258 <tr>
259 <th><label for="brute_force_disable"><?php esc_html_e( 'Disable one-time code brute force protection', 'wp-2fa' ); ?></label></th>
260 <td>
261 <fieldset>
262 <input type="checkbox" id="brute_force_disable" name="wp_2fa_settings[brute_force_disable]" value="brute_force_disable"
263 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'brute_force_disable' ), true ); ?>
264 >
265 </fieldset>
266 </td>
267 </tr>
268 </tbody>
269 </table>
270 <?php
271 }
272
273 /**
274 * Rendering settings when there are no methods.
275 *
276 * @return void
277 *
278 * @since 2.2.0
279 */
280 private static function no_method_exists() {
281 ?>
282 <p class="description">
283 <?php
284 printf(
285 // translators: support email.
286 \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' ),
287 '<a href="mailto:support@melapress.com">support@melapress.com</a>'
288 );
289 ?>
290 </p>
291 <h3><?php esc_html_e( 'What should the plugin do if the 2FA method used during a user login is unavailable?', 'wp-2fa' ); ?></h3>
292 <p class="description">
293 <?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' ); ?>
294 </p>
295 <table class="form-table">
296 <tbody>
297 <tr>
298 <th><label for="no-methods"><?php esc_html_e( 'Select action', 'wp-2fa' ); ?></label></th>
299 <td>
300 <fieldset class="contains-hidden-inputs" id="no-methods">
301 <label for="login_block">
302 <input type="radio" name="wp_2fa_settings[method_invalid_setting]" id="login_block" value="login_block"
303 <?php checked( WP2FA::get_wp2fa_general_setting( 'method_invalid_setting' ), 'login_block' ); ?>
304 >
305 <span><?php esc_html_e( 'Block the login.', 'wp-2fa' ); ?></span>
306 </label>
307
308 <br/>
309 <label for="allow_login_without_method">
310 <input type="radio" name="wp_2fa_settings[method_invalid_setting]" id="allow_login_without_method" value="allow_login_without_method"
311 <?php checked( WP2FA::get_wp2fa_general_setting( 'method_invalid_setting' ), 'allow_login_without_method' ); ?>
312 data-unhide-when-checked=".custom-from-inputs">
313 <span><?php esc_html_e( 'Allow the login without 2FA', 'wp-2fa' ); ?></span>
314 </label>
315 </fieldset>
316 </td>
317 </tr>
318 </tbody>
319 </table>
320 <?php
321 }
322 }
323 }
324