PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.2.1
WP 2FA – Two-factor authentication for WordPress v2.2.1
4.1.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 4 years ago class-settings-page-general.php 4 years ago class-settings-page-policies.php 4 years ago class-settings-page-white-label.php 4 years ago
class-settings-page-general.php
338 lines
1 <?php
2 /**
3 * Generals settings class.
4 *
5 * @package wp2fa
6 * @subpackage settings-pages
7 * @copyright 2021 WP White Security
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://wordpress.org/plugins/wp-2fa/
10 */
11
12 namespace WP2FA\Admin\SettingsPages;
13
14 use \WP2FA\WP2FA as WP2FA;
15 use \WP2FA\Utils\Debugging as Debugging;
16 use WP2FA\Utils\Settings_Utils as Settings_Utils;
17
18
19 /**
20 * General settings tab
21 */
22 if ( ! class_exists( '\WP2FA\Admin\SettingsPages\Settings_Page_General' ) ) {
23 /**
24 * Settings_Page_General - Class for handling general settings
25 *
26 * @since 2.0.0
27 */
28 class Settings_Page_General {
29
30 /**
31 * Renders the settings
32 *
33 * @return void
34 *
35 * @since 2.0.0
36 */
37 public function render() {
38 settings_fields( WP_2FA_SETTINGS_NAME );
39 self::no_method_exists();
40 self::grace_period_frequency();
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
55 // Bail if user doesn't have permissions to be here.
56 if ( ! current_user_can( 'manage_options' ) || ! isset( $_POST['action'] ) && ! check_admin_referer( 'wp2fa-step-choose-method' ) ) {
57 return;
58 }
59
60 $simple_settings_we_can_loop = array(
61 'enable_grace_cron',
62 'enable_destroy_session',
63 'limit_access',
64 'delete_data_upon_uninstall',
65 'method_invalid_setting',
66 );
67
68 /**
69 * Gives the ability to change the default general settings.
70 *
71 * @param array $general_settings - The array with the default settings.
72 *
73 * @since 2.0.0
74 */
75 $simple_settings_we_can_loop = apply_filters( WP_2FA_PREFIX . 'loop_general_settings', $simple_settings_we_can_loop );
76
77 $settings_to_turn_into_bools = array(
78 'enable_grace_cron',
79 'enable_destroy_session',
80 'limit_access',
81 'delete_data_upon_uninstall',
82 );
83
84 foreach ( $simple_settings_we_can_loop as $simple_setting ) {
85 if ( ! in_array( $simple_setting, $settings_to_turn_into_bools, true ) ) {
86 // Is item is not one of our possible settings we want to turn into a bool, process.
87 $output[ $simple_setting ] = ( isset( $input[ $simple_setting ] ) && ! empty( $input[ $simple_setting ] ) ) ? trim( sanitize_text_field( $input[ $simple_setting ] ) ) : false;
88 } else {
89 // This item is one we treat as a bool, so process correctly.
90 $output[ $simple_setting ] = ( isset( $input[ $simple_setting ] ) && ! empty( $input[ $simple_setting ] ) ) ? true : false;
91 }
92 }
93
94 if ( isset( $input['2fa_settings_last_updated_by'] ) && ! empty( $input['2fa_settings_last_updated_by'] ) ) {
95 $policies = WP2FA::get_wp2fa_setting();
96 if ( false === $policies ) {
97 $policies = WP2FA::get_default_settings();
98 }
99 $policies['2fa_settings_last_updated_by'] = (int) $input['2fa_settings_last_updated_by'];
100
101 WP2FA::update_plugin_settings( $policies );
102 }
103
104 // 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.
105 global $wp_settings_errors;
106 if ( isset( $wp_settings_errors ) ) {
107 $errors = array_map( 'unserialize', array_unique( array_map( 'serialize', $wp_settings_errors ) ) );
108 $wp_settings_errors = $errors; // phpcs:ignore
109 }
110
111 $log_content = __( 'Settings saving processes complete', 'wp-2fa' );
112 Debugging::log( $log_content );
113
114 /**
115 * Filter the values we are about to store in the plugin settings.
116 *
117 * @param array $output - The output array with all the data we will store in the settings.
118 * @param array $input - The input array with all the data we received from the user.
119 *
120 * @since 2.0.0
121 */
122 $output = apply_filters( WP_2FA_PREFIX . 'filter_output_content_general_settings', $output, $input );
123
124 // We have overridden any defaults by now so can clear this.
125 Settings_Utils::delete_option( WP_2FA_PREFIX . 'default_settings_applied' );
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
139 if ( isset( $_POST[ WP_2FA_SETTINGS_NAME ] ) ) {
140 check_admin_referer( 'wp_2fa_settings-options' );
141 $options = self::validate_and_sanitize( wp_unslash( $_POST[ WP_2FA_SETTINGS_NAME ] ) ); // phpcs:ignore
142 $settings_errors = get_settings_errors( WP_2FA_SETTINGS_NAME );
143 if ( ! empty( $settings_errors ) ) {
144
145 // redirect back to our options page.
146 wp_safe_redirect(
147 add_query_arg(
148 array(
149 'page' => 'wp-2fa-settings',
150 'wp_2fa_network_settings_error' => urlencode_deep( $settings_errors[0]['message'] ),
151 ),
152 network_admin_url( 'settings.php' )
153 )
154 );
155 exit;
156
157 }
158 WP2FA::update_plugin_settings( $options, false, WP_2FA_SETTINGS_NAME );
159
160 // redirect back to our options page.
161 wp_safe_redirect(
162 add_query_arg(
163 array(
164 'page' => 'wp-2fa-settings',
165 'tab' => 'generic-settings',
166 'wp_2fa_network_settings_updated' => 'true',
167 ),
168 network_admin_url( 'admin.php' )
169 )
170 );
171 exit;
172 }
173 }
174
175 /**
176 * Limit settings setting
177 *
178 * @return void
179 *
180 * @since 2.0.0
181 */
182 private static function remove_data_upon_uninstall() {
183 ?>
184 <div class="danger-zone-wrapper">
185 <h3><?php esc_html_e( 'Do you want to delete the plugin data from the database upon uninstall?', 'wp-2fa' ); ?></h3>
186 <p class="description">
187 <?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' ); ?>
188 </p>
189 <table class="form-table">
190 <tbody>
191 <tr>
192 <th><label for="delete_data"><?php esc_html_e( 'Delete data', 'wp-2fa' ); ?></label></th>
193 <td>
194 <fieldset>
195 <input type="checkbox" id="delete_data" name="wp_2fa_settings[delete_data_upon_uninstall]" value="delete_data_upon_uninstall"
196 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'delete_data_upon_uninstall' ), true ); ?>
197 >
198 <?php esc_html_e( 'Delete data upon uninstall', 'wp-2fa' ); ?>
199 </fieldset>
200 </td>
201 </tr>
202 </tbody>
203 </table>
204 </div>
205 <?php
206 $last_user_to_update_settings = get_current_user_id();
207
208 ?>
209 <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 ); ?>">
210 <?php
211 }
212
213 /**
214 * Grace period frequency
215 *
216 * @return void
217 *
218 * @since 2.0.0
219 */
220 private static function grace_period_frequency() {
221 ?>
222 <h3><?php esc_html_e( 'How often should the plugin check if a user\'s grace period is over?', 'wp-2fa' ); ?></h3>
223 <p class="description">
224 <?php esc_html_e( 'By default the plugin checks if a users grace periods to setup 2FA has passed when the user tries to login. If you would like the plugin to advise the user within an hour, enable the below option to add a cron job that runs every hour.', 'wp-2fa' ); ?>
225 </p>
226 <table class="form-table">
227 <tbody>
228 <tr>
229 <th><label for="grace-cron"><?php esc_html_e( 'Enable cron', 'wp-2fa' ); ?></label></th>
230 <td>
231 <fieldset>
232 <input type="checkbox" id="grace-cron" name="wp_2fa_settings[enable_grace_cron]" value="enable_grace_cron"
233 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'enable_grace_cron' ), true ); ?>
234 >
235 <?php esc_html_e( 'Use cron job to check grace periods', 'wp-2fa' ); ?>
236 </fieldset>
237 </td>
238 </tr>
239 <tr class="disabled destory-session-setting">
240 <th><label for="destory-session"><?php esc_html_e( 'Destroy session', 'wp-2fa' ); ?></label></th>
241 <td>
242 <fieldset>
243 <input type="checkbox" id="destory-session" name="wp_2fa_settings[enable_destroy_session]" value="enable_destroy_session"
244 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'enable_destroy_session' ), true ); ?>
245 >
246 <?php esc_html_e( 'Destroy user session when grace period expires?', 'wp-2fa' ); ?>
247 </fieldset>
248 </td>
249 </tr>
250 </tbody>
251 </table>
252 <?php
253 }
254
255 /**
256 * Limit settings setting
257 *
258 * @return void
259 *
260 * @since 2.0.0
261 */
262 private static function limit_settings_access() {
263 ?>
264 <br>
265 <h3><?php esc_html_e( 'Limit 2FA settings access?', 'wp-2fa' ); ?></h3>
266 <p class="description">
267 <?php esc_html_e( 'Use this setting to hide this plugin configuration area from all other admins.', 'wp-2fa' ); ?>
268 </p>
269 <table class="form-table">
270 <tbody>
271 <tr>
272 <th><label for="limit_access"><?php esc_html_e( 'Limit access to 2FA settings', 'wp-2fa' ); ?></label></th>
273 <td>
274 <fieldset>
275 <input type="checkbox" id="limit_access" name="wp_2fa_settings[limit_access]" value="limit_access"
276 <?php checked( 1, WP2FA::get_wp2fa_general_setting( 'limit_access' ), true ); ?>
277 >
278 <?php esc_html_e( 'Hide settings from other administrators', 'wp-2fa' ); ?>
279 </fieldset>
280 </td>
281 </tr>
282 </tbody>
283 </table>
284 <?php
285 }
286
287 /**
288 * Rendering settings when there are no methods
289 *
290 * @return void
291 *
292 * @since 2.2.0
293 */
294 private static function no_method_exists() {
295 ?>
296 <p class="description">
297 <?php
298 printf(
299 // translators: support email.
300 \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' ),
301 '<a href="mailto:support@wpwhitesecurity.com">support@withesecurity.com</a>'
302 );
303 ?>
304 </p>
305 <h3><?php esc_html_e( 'What should the plugin do if the 2FA method used during a user login is unavailable?', 'wp-2fa' ); ?></h3>
306 <p class="description">
307 <?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' ); ?>
308 </p>
309 <table class="form-table">
310 <tbody>
311 <tr>
312 <th><label for="no-methods"><?php esc_html_e( 'Select action', 'wp-2fa' ); ?></label></th>
313 <td>
314 <fieldset class="contains-hidden-inputs" id="no-methods">
315 <label for="login_block">
316 <input type="radio" name="wp_2fa_settings[method_invalid_setting]" id="login_block" value="login_block"
317 <?php checked( WP2FA::get_wp2fa_general_setting( 'method_invalid_setting' ), 'login_block' ); ?>
318 >
319 <span><?php esc_html_e( 'Block the login.', 'wp-2fa' ); ?></span>
320 </label>
321
322 <br/>
323 <label for="allow_login_without_method">
324 <input type="radio" name="wp_2fa_settings[method_invalid_setting]" id="allow_login_without_method" value="allow_login_without_method"
325 <?php checked( WP2FA::get_wp2fa_general_setting( 'method_invalid_setting' ), 'allow_login_without_method' ); ?>
326 data-unhide-when-checked=".custom-from-inputs">
327 <span><?php esc_html_e( 'Allow the login without 2FA', 'wp-2fa' ); ?></span>
328 </label>
329 </fieldset>
330 </td>
331 </tr>
332 </tbody>
333 </table>
334 <?php
335 }
336 }
337 }
338