PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.1.0
WP 2FA – Two-factor authentication for WordPress v2.1.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 / SettingsPage.php
wp-2fa / includes / classes / Admin Last commit date
Controllers 4 years ago SettingsPages 4 years ago Views 4 years ago HelpContactUs.php 4 years ago PremiumFeatures.php 4 years ago SettingsPage.php 4 years ago SetupWizard.php 4 years ago User.php 4 years ago UserListing.php 4 years ago UserNotices.php 4 years ago UserProfile.php 4 years ago UserRegistered.php 4 years ago index.php 5 years ago
SettingsPage.php
653 lines
1 <?php // phpcs:ignore
2
3 namespace WP2FA\Admin;
4
5 use \WP2FA\WP2FA as WP2FA;
6 use WP2FA\Utils\UserUtils;
7 use WP2FA\Utils\SettingsUtils;
8 use WP2FA\Admin\Controllers\Settings;
9 use WP2FA\Admin\Views\Settings_Page_Render;
10 use WP2FA\Admin\SettingsPages\{
11 Settings_Page_Policies,
12 Settings_Page_General,
13 Settings_Page_White_Label,
14 Settings_Page_Email
15 };
16
17 /**
18 * SettingsPage - Class for handling settings
19 */
20 class SettingsPage {
21
22 const TOP_MENU_SLUG = 'wp-2fa-policies';
23
24 /**
25 * Holds the status of the backupcodes functionality
26 *
27 * @var bool[]
28 */
29 private static $backupCodesEnabled = array();
30
31 /**
32 * Create admin menu entry and settings page
33 */
34 public function create_settings_admin_menu() {
35 // Create admin menu item.
36 add_menu_page(
37 esc_html__( 'WP 2FA', 'wp-2fa' ),
38 esc_html__( 'WP 2FA', 'wp-2fa' ),
39 'manage_options',
40 self::TOP_MENU_SLUG,
41 null,
42 "data:image/svg+xml;base64,".base64_encode(file_get_contents(WP_2FA_PATH . 'dist/images/wp-2fa-white-icon20x28.svg')),
43 81
44 );
45
46 $settings_policies = new Settings_Page_Policies();
47 $settings_general = new Settings_Page_General();
48 $settings_white_label = new Settings_Page_White_Label();
49 $settings_email = new Settings_Page_Email();
50 $settings_render = new Settings_Page_Render();
51
52 add_submenu_page(
53 self::TOP_MENU_SLUG,
54 esc_html__( '2FA Policies', 'wp-2fa' ),
55 esc_html__( '2FA Policies', 'wp-2fa' ),
56 'manage_options',
57 self::TOP_MENU_SLUG,
58 array( $settings_policies, 'render' ),
59 1
60 );
61
62 add_submenu_page(
63 self::TOP_MENU_SLUG,
64 esc_html__( 'WP 2FA Settings', 'wp-2fa' ),
65 esc_html__( 'Settings', 'wp-2fa' ),
66 'manage_options',
67 'wp-2fa-settings',
68 array( $settings_render, 'render' ),
69 2
70 );
71
72 // Register our policy settings.
73 register_setting(
74 WP_2FA_POLICY_SETTINGS_NAME,
75 WP_2FA_POLICY_SETTINGS_NAME,
76 array( $settings_policies, 'validate_and_sanitize' )
77 );
78
79 // Register our white label settings.
80 register_setting(
81 WP_2FA_WHITE_LABEL_SETTINGS_NAME,
82 WP_2FA_WHITE_LABEL_SETTINGS_NAME,
83 array( $settings_white_label, 'validate_and_sanitize' )
84 );
85
86 // Register our settings page.
87 register_setting(
88 WP_2FA_SETTINGS_NAME,
89 WP_2FA_SETTINGS_NAME,
90 array( $settings_general, 'validate_and_sanitize' )
91 );
92
93 register_setting(
94 WP_2FA_EMAIL_SETTINGS_NAME,
95 WP_2FA_EMAIL_SETTINGS_NAME,
96 array( $settings_email, 'validate_and_sanitize' )
97 );
98
99 do_action( 'wp_2fa_after_admin_menu_created', self::TOP_MENU_SLUG, false);
100 }
101
102 /**
103 * Create admin menu entry and settings page
104 */
105 public function create_settings_admin_menu_multisite() {
106 // Create admin menu item.
107 add_menu_page(
108 esc_html__( 'WP 2FA Settings', 'wp-2fa' ),
109 esc_html__( 'WP 2FA', 'wp-2fa' ),
110 'manage_options',
111 self::TOP_MENU_SLUG,
112 null,
113 "data:image/svg+xml;base64,".base64_encode(file_get_contents(WP_2FA_PATH . 'dist/images/wp-2fa-white-icon20x28.svg')),
114 81
115 );
116
117 $settings_policies = new Settings_Page_Policies();
118 add_submenu_page(
119 self::TOP_MENU_SLUG,
120 esc_html__( '2FA Policies', 'wp-2fa' ),
121 esc_html__( '2FA Policies', 'wp-2fa' ),
122 'manage_options',
123 self::TOP_MENU_SLUG,
124 array( $settings_policies, 'render' ),
125 1
126 );
127
128 $settings_render = new Settings_Page_Render();
129 add_submenu_page(
130 self::TOP_MENU_SLUG,
131 esc_html__( 'WP 2FA Settings', 'wp-2fa' ),
132 esc_html__( 'Settings', 'wp-2fa' ),
133 'manage_options',
134 'wp-2fa-settings',
135 array( $settings_render, 'render' ),
136 2
137 );
138
139 do_action( 'wp_2fa_after_admin_menu_created', self::TOP_MENU_SLUG, true);
140 }
141
142 /**
143 * Get all users
144 */
145 public function get_all_users() {
146 // Die if user does not have permission to view.
147 if ( ! current_user_can( 'manage_options' ) ) {
148 die( 'Access Denied.' );
149 }
150 // Filter $_GET array for security.
151 $get_array = filter_input_array( INPUT_GET );
152
153 // Die if nonce verification failed.
154 if ( ! wp_verify_nonce( sanitize_text_field( $get_array['wp_2fa_nonce'] ), 'wp-2fa-settings-nonce' ) ) {
155 die( esc_html__( 'Nonce verification failed.', 'wp-2fa' ) );
156 }
157
158 $users_args = array(
159 'fields' => array( 'ID', 'user_login' ),
160 );
161 if ( WP2FA::is_this_multisite() ) {
162 $users_args['blog_id'] = 0;
163 }
164 $users_data = UserUtils::get_all_user_ids_and_login_names( 'query', $users_args );
165
166 // Create final array which we will fill in below.
167 $users = [];
168
169 foreach ( $users_data as $user ) {
170 if ( strpos( $user['user_login'], $get_array['term'] ) !== false ) {
171 array_push( $users, [
172 'value' => $user['user_login'],
173 'label' => $user['user_login']
174 ]);
175 }
176 }
177
178 echo wp_json_encode( $users );
179 exit;
180 }
181
182 /**
183 * Get all network sites
184 */
185 public function get_all_network_sites() {
186 // Die if user does not have permission to view.
187 if ( ! current_user_can( 'manage_options' ) ) {
188 die( 'Access Denied.' );
189 }
190 // Filter $_GET array for security.
191 $get_array = filter_input_array( INPUT_GET );
192 // Die if nonce verification failed.
193 if ( ! wp_verify_nonce( sanitize_text_field( $get_array['wp_2fa_nonce'] ), 'wp-2fa-settings-nonce' ) ) {
194 die( esc_html__( 'Nonce verification failed.', 'wp-2fa' ) );
195 }
196 // Fetch sites.
197 $sites_found = array();
198
199 foreach ( get_sites() as $site ) {
200 $subsite_id = get_object_vars( $site )['blog_id'];
201 $subsite_name = get_blog_details( $subsite_id )->blogname;
202 $site_details = '';
203 $site_details[ $subsite_id ] = $subsite_name;
204 if ( false !== stripos( $subsite_name, $get_array['term'] ) ) {
205 array_push( $sites_found, [
206 'label' => $subsite_id,
207 'value' => $subsite_name
208 ]);
209 }
210 }
211 echo wp_json_encode( $sites_found );
212 exit;
213 }
214
215 /**
216 * Unlock users accounts if they have overrun grace period
217 *
218 * @param int $user_id User ID.
219 */
220 public function unlock_account( $user_id ) {
221 // Die if user does not have permission to view.
222 if ( ! current_user_can( 'manage_options' ) ) {
223 die( 'Access Denied.' );
224 }
225
226 $grace_period = WP2FA::get_wp2fa_setting( 'grace-period' );
227 $grace_period_denominator = WP2FA::get_wp2fa_setting( 'grace-period-denominator' );
228 $create_a_string = $grace_period . ' ' . $grace_period_denominator;
229 // Turn that string into a time.
230 $grace_expiry = strtotime( $create_a_string );
231
232 // Filter $_GET array for security.
233 $get_array = filter_input_array( INPUT_GET );
234 $nonce = sanitize_text_field( $get_array['wp_2fa_nonce'] );
235
236 // Die if nonce verification failed.
237 if ( ! wp_verify_nonce( $nonce, 'wp-2fa-unlock-account-nonce' ) ) {
238 die( esc_html__( 'Nonce verification failed.', 'wp-2fa' ) );
239 }
240
241 if ( isset( $get_array['user_id'] ) ) {
242 global $wpdb;
243 $wpdb->query(
244 $wpdb->prepare(
245 "
246 DELETE FROM $wpdb->usermeta
247 WHERE user_id = %d
248 AND meta_key IN ( %s, %s )
249 ",
250 [
251 intval( $get_array['user_id'] ),
252 WP_2FA_PREFIX . 'user_grace_period_expired',
253 WP_2FA_PREFIX . 'locked_account_notification',
254 ]
255 )
256 );
257 update_user_meta( intval( $get_array['user_id'] ), WP_2FA_PREFIX . 'grace_period_expiry', $grace_expiry );
258 $this->send_account_unlocked_email( intval( $get_array['user_id'] ) );
259 add_action( 'admin_notices', array( $this, 'user_unlocked_notice' ) );
260 }
261 }
262
263 /**
264 * Remove user 2fa config
265 *
266 * @param int $user_id User ID.
267 */
268 public function remove_user_2fa( $user_id ) {
269 // Filter $_GET array for security.
270 $get_array = filter_input_array( INPUT_GET );
271 $nonce = sanitize_text_field( $get_array['wp_2fa_nonce'] );
272
273 if ( ! wp_verify_nonce( $nonce, 'wp-2fa-remove-user-2fa-nonce' ) ) {
274 die( esc_html__( 'Nonce verification failed.', 'wp-2fa' ) );
275 }
276
277 if ( isset( $get_array['user_id'] ) ) {
278 $user_id = intval( $get_array['user_id'] );
279
280 $user_meta_values = array_filter(
281 get_user_meta( $user_id ),
282 function( $key ) {
283 return strpos( $key, 'wp_2fa_' ) === 0;
284 },
285 ARRAY_FILTER_USE_KEY
286 );
287
288 foreach ( array_keys( $user_meta_values ) as $meta_name ) {
289 \delete_user_meta( $user_id, $meta_name );
290 }
291
292 $is_needed = User::is_enforced( $user_id );
293
294 if ( $is_needed ) {
295 if ( 'do-not-enforce' !== WP2FA::get_wp2fa_setting( 'enforcement-policy' ) ) {
296 // Turn inputs into a useable string.
297 $create_a_string = WP2FA::get_wp2fa_setting( 'grace-period' ) . ' ' . WP2FA::get_wp2fa_setting( 'grace-period-denominator' );
298 // Turn that string into a time.
299 $grace_expiry = strtotime( $create_a_string );
300 update_user_meta( $user_id, WP_2FA_PREFIX . 'grace_period_expiry', $grace_expiry );
301 update_user_meta( $user_id, WP_2FA_PREFIX . 'update_nag_dismissed', true );
302 }
303 $grace_policy = WP2FA::get_wp2fa_setting( 'grace-policy' );
304 if ( 'no-grace-period' === $grace_policy ) {
305 update_user_meta( $user_id, WP_2FA_PREFIX . 'user_enforced_instantly', true );
306 // Set this to a known older value so its already expired.
307 update_user_meta( $user_id, WP_2FA_PREFIX . 'grace_period_expiry', '1609502400' );
308 // Get sessions for user with ID $user_id.
309 $sessions = \WP_Session_Tokens::get_instance( $user_id );
310 // Log them out.
311 $sessions->destroy_all();
312 }
313 }
314 if ( isset( $get_array['admin_reset'] ) ) {
315 add_action( 'admin_notices', array( $this, 'admin_deleted_2fa_notice' ) );
316 } else {
317 add_action( 'admin_notices', array( $this, 'user_deleted_2fa_notice' ) );
318 }
319 }
320 }
321
322 /**
323 * Send account unlocked notification via email.
324 *
325 * @param int $user_id user ID.
326 *
327 * @return boolean
328 */
329 public static function send_account_unlocked_email( $user_id ) {
330 // Bail if the user has not enabled this email.
331 if ( 'enable_account_unlocked_email' !== WP2FA::get_wp2fa_email_templates( 'send_account_unlocked_email' ) ) {
332 return false;
333 }
334
335 // Grab user data.
336 $user = get_userdata( $user_id );
337 // Grab user email.
338 $email = $user->user_email;
339 // Setup the email contents.
340 $subject = wp_strip_all_tags( WP2FA::replace_email_strings( WP2FA::get_wp2fa_email_templates( 'user_account_unlocked_email_subject' ) ) );
341 $message = wpautop( WP2FA::replace_email_strings( WP2FA::get_wp2fa_email_templates( 'user_account_unlocked_email_body' ), $user_id ) );
342
343 self::send_email($email, $subject, $message);
344 }
345
346 /**
347 * Hide settings menu item
348 */
349 public function hide_settings() {
350 $user = wp_get_current_user();
351
352 // Check we have a user before doing anything else.
353 if ( is_a( $user, '\WP_User' ) ) {
354 if ( ! empty( WP2FA::get_wp2fa_setting( '2fa_settings_last_updated_by' ) ) ) {
355 $main_user = (int) WP2FA::get_wp2fa_setting( '2fa_settings_last_updated_by' );
356 } else {
357 $main_user = get_current_user_id();
358 }
359 if ( ! empty( WP2FA::get_wp2fa_general_setting( 'limit_access' ) ) && $user->ID !== $main_user ) {
360 // Remove admin menu item.
361 remove_submenu_page( 'options-general.php', 'wp-2fa-policies' );
362 }
363 }
364 }
365
366 /**
367 * Add unlock user link to user actions.
368 *
369 * @param array $links Default row content.
370 *
371 * @return array
372 * @throws \Freemius_Exception
373 */
374 public function add_plugin_action_links( $links ) {
375 // add link to the external free trial page in free version and also in premium version if license is not active.
376 if ( ! function_exists( 'wp2fa_freemius' ) || ! wp2fa_freemius()->has_active_valid_license() ) {
377 $trial_link = 'https://wp2fa.io/get-wp-2fa-premium-trial/?utm_source=plugin&utm_medium=referral&utm_campaign=WP2FA';
378 $links = array_merge(
379 array(
380 '<a style="font-weight:bold" href="' . $trial_link . '" target="_blank">' . __( 'Free 14-day Premium Trial', 'wp-2fa' ) . '</a>',
381 ),
382 $links
383 );
384 }
385
386 // add link to the plugin settings page.
387 $url = Settings::get_settings_page_link();
388 $links = array_merge(
389 array(
390 '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Configure 2FA Settings', 'wp-2fa' ) . '</a>',
391 ),
392 $links
393 );
394
395 return $links;
396 }
397
398 /**
399 * User unlocked notice.
400 */
401 public function user_unlocked_notice() {
402 ?>
403 <div class="notice notice-success is-dismissible">
404 <p><?php esc_html_e( 'User account successfully unlocked. User can login again.', 'wp-2fa' ); ?></p>
405 <button type="button" class="notice-dismiss">
406 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
407 </button>
408 </div>
409 <?php
410 }
411
412 /**
413 * User deleted 2FA settings notification
414 */
415 public function user_deleted_2fa_notice() {
416 ?>
417 <div class="notice notice-success is-dismissible">
418 <p><?php esc_html_e( 'Your 2FA settings have been removed.', 'wp-2fa' ); ?></p>
419 <button type="button" class="notice-dismiss">
420 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
421 </button>
422 </div>
423 <?php
424 }
425
426 /**
427 * Admin deleted user 2FA settings notification
428 */
429 public function admin_deleted_2fa_notice() {
430 ?>
431 <div class="notice notice-success is-dismissible">
432 <p><?php esc_html_e( 'User 2FA settings have been removed.', 'wp-2fa' ); ?></p>
433 <button type="button" class="notice-dismiss">
434 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
435 </button>
436 </div>
437 <?php
438 }
439
440 public function update_wp2fa_network_options() {
441
442 $settings_policies = new Settings_Page_Policies();
443
444 $settings_policies->update_wp2fa_network_options();
445
446 $settings_general = new Settings_Page_General();
447
448 $settings_general->update_wp2fa_network_options();
449
450 $settings_white_label = new Settings_Page_White_Label();
451
452 $settings_white_label->update_wp2fa_network_options();
453 }
454
455 /**
456 * Handle saving email options to the network main site options.
457 */
458 public function update_wp2fa_network_email_options() {
459 $settings_email = new Settings_Page_Email();
460
461 $settings_email->update_wp2fa_network_options();
462 }
463
464 /**
465 * These are used instead of add_settings_error which in a network site. Used to show if settings have been updated or failed.
466 */
467 public function settings_saved_network_admin_notice() {
468 if ( isset( $_GET['wp_2fa_network_settings_updated'] ) && $_GET['wp_2fa_network_settings_updated'] == 'true' ) :
469 ?>
470 <div class="notice notice-success is-dismissible">
471 <p><?php esc_html_e( '2FA Settings Updated', 'wp-2fa' ); ?></p>
472 <button type="button" class="notice-dismiss">
473 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
474 </button>
475 </div>
476 <?php
477 endif;
478 if ( isset( $_GET['wp_2fa_network_settings_updated'] ) && $_GET['wp_2fa_network_settings_updated'] == 'false' ) :
479 ?>
480 <div class="notice notice-error is-dismissible">
481 <p><?php esc_html_e( 'Please ensure both custom email address and display name are provided.', 'wp-2fa' ); ?></p>
482 <button type="button" class="notice-dismiss">
483 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
484 </button>
485 </div>
486 <?php
487 endif;
488 if ( isset( $_GET['wp_2fa_network_settings_error'] ) ) :
489 ?>
490 <div class="notice notice-error is-dismissible">
491 <p><?php echo urldecode_deep( $_GET['wp_2fa_network_settings_error'] ); ?></p>
492 <button type="button" class="notice-dismiss">
493 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
494 </button>
495 </div>
496 <?php
497 endif;
498 }
499
500 /**
501 * These are used instead of add_settings_error which in a network site. Used to show if settings have been updated or failed.
502 *
503 * @return void
504 *
505 * @since 2.0.0
506 */
507 public function settings_saved_admin_notice() {
508 if ( isset( $_GET['settings-updated'] ) && 'true' === $_GET['settings-updated'] ) :
509 $wp_settings_errors = get_settings_errors();
510
511 if ( count( $wp_settings_errors ) ) {
512 foreach ( $wp_settings_errors as $error ) {
513 ?>
514 <div class="notice notice-<?php echo \esc_attr( $error['type'] ); ?> is-dismissible">
515 <p><?php echo \esc_html( $error['message'] ); ?></p>
516 <button type="button" class="notice-dismiss">
517 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
518 </button>
519 </div>
520 <?php
521 }
522 } else {
523 ?>
524 <div class="notice notice-success is-dismissible">
525 <p><?php esc_html_e( '2FA Settings Updated', 'wp-2fa' ); ?></p>
526 <button type="button" class="notice-dismiss">
527 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
528 </button>
529 </div>
530 <?php
531 }
532 endif;
533 if ( isset( $_GET['settings-updated'] ) && 'false' === $_GET['settings-updated'] ) :
534 ?>
535 <div class="notice notice-error is-dismissible">
536 <p><?php esc_html_e( 'Please ensure both custom email address and display name are provided.', 'wp-2fa' ); ?></p>
537 <button type="button" class="notice-dismiss">
538 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
539 </button>
540 </div>
541 <?php
542 endif;
543 if ( isset( $_GET['settings_error'] ) ) :
544 ?>
545 <div class="notice notice-error is-dismissible">
546 <p><?php echo urldecode_deep( $_GET['settings_error'] ); ?></p>
547 <button type="button" class="notice-dismiss">
548 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-2fa' ); ?></span>
549 </button>
550 </div>
551 <?php
552 endif;
553 }
554
555 /**
556 * Add our custom state to our created page.
557 */
558 public function add_display_post_states( $post_states, $post ) {
559 if ( ! empty( WP2FA::get_wp2fa_setting( 'custom-user-page-id' ) ) ) {
560 if ( WP2FA::get_wp2fa_setting( 'custom-user-page-id' ) === $post->ID ) {
561 $post_states['wp_2fa_page_for_user'] = __( 'WP 2FA User Page', 'wp-2fa' );
562 }
563 }
564
565 return $post_states;
566 }
567
568 /**
569 * Handles sending of an email. It sets necessary header such as content type and custom from email address and name.
570 *
571 * @param string $recipient_email Email address to send message to.
572 * @param string $subject Email subject.
573 * @param string $message Message contents.
574 *
575 * @return bool Whether the email contents were sent successfully.
576 */
577 public static function send_email( $recipient_email, $subject, $message ) {
578
579 // Specify our desired headers.
580 $headers = 'Content-type: text/html;charset=utf-8' . "\r\n";
581
582 if ( 'use-custom-email' === WP2FA::get_wp2fa_email_templates( 'email_from_setting' ) ) {
583 $headers .= 'From: ' . WP2FA::get_wp2fa_email_templates( 'custom_from_display_name' ) . ' <' . WP2FA::get_wp2fa_email_templates( 'custom_from_email_address' ) . '>' . "\r\n";
584 } else {
585 $headers .= 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
586 }
587
588 // Fire our email.
589 return wp_mail( $recipient_email, $subject, $message, $headers );
590
591 }
592
593 /**
594 * Turns user roles data in any form and shape to an array of strings.
595 *
596 * @param mixed $value User role names (slugs) as raw value.
597 *
598 * @return string[] List of user role names (slugs).
599 */
600 public static function extract_roles_from_input( $value ) {
601 if ( is_array( $value ) ) {
602 return $value;
603 }
604
605 if ( is_string( $value ) && ! empty( $value ) ) {
606 return explode( ',', $value );
607 }
608
609 return [];
610 }
611
612 /**
613 * Determine if any BG processes are currently running.
614 *
615 * @return int|false Number of jobs.
616 */
617 public function get_current_number_of_active_bg_processes() {
618 global $wpdb;
619
620 $bg_jobs = $wpdb->get_results(
621 "SELECT option_value FROM $wpdb->options
622 WHERE option_name LIKE '%_2fa_bg_%'"
623 );
624
625 return count( $bg_jobs );
626 }
627
628 /**
629 * Checks if the backup codes option is globally enabled
630 *
631 * @param string $role - The role name.
632 *
633 * @return bool
634 */
635 public static function are_backup_codes_enabled( $role = 'global' ) {
636
637 $role = is_null( $role ) ? 'global' : $role;
638
639 if ( ! isset( self::$backupCodesEnabled[ $role ] ) ) {
640 self::$backupCodesEnabled[ $role ] = false;
641
642 if ( 'global' === $role ) {
643 $setting_value = Settings::get_role_or_default_setting( 'backup_codes_enabled' );
644 } else {
645 $setting_value = Settings::get_role_or_default_setting( 'backup_codes_enabled', 'current', $role );
646 }
647 self::$backupCodesEnabled[ $role ] = SettingsUtils::string_to_bool( $setting_value );
648 }
649
650 return self::$backupCodesEnabled[ $role ];
651 }
652 }
653