PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 1.7.0
WP 2FA – Two-factor authentication for WordPress v1.7.0
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 / UserNotices.php
wp-2fa / includes / classes / Admin Last commit date
Controllers 5 years ago Views 5 years ago SettingsPage.php 5 years ago SetupWizard.php 5 years ago User.php 5 years ago UserListing.php 5 years ago UserNotices.php 5 years ago UserProfile.php 5 years ago UserRegistered.php 5 years ago index.php 5 years ago
UserNotices.php
148 lines
1 <?php // phpcs:ignore
2
3 namespace WP2FA\Admin;
4
5 use \WP2FA\WP2FA as WP2FA;
6 use WP2FA\Utils\DateTimeUtils;
7 use WP2FA\Admin\Controllers\Settings;
8 use \WP2FA\Authenticator\Authentication as Authentication;
9
10 /**
11 * UserNotices - Class for displaying notices to our users.
12 */
13 class UserNotices {
14
15 /**
16 * Lets set things up
17 */
18 public function __construct() {
19 $enforcement_policy = WP2FA::get_wp2fa_setting( 'enforcement-policy' );
20 if ( ! empty( $enforcement_policy ) ) {
21 // Check we are supposed to, before adding action to show nag.
22 if ( in_array( $enforcement_policy, [ 'all-users', 'certain-roles-only', 'certain-users-only', 'superadmins-only', 'superadmins-siteadmins-only', 'enforce-on-multisite' ] ) ) {
23 add_action( 'admin_notices', array( $this, 'user_setup_2fa_nag' ) );
24 add_action( 'network_admin_notices', array( $this, 'user_setup_2fa_nag' ) );
25 } elseif ( 'do-not-enforce' === WP2FA::get_wp2fa_setting( 'enforcement-policy' ) ) {
26 add_action( 'admin_notices', array( $this, 'user_reconfigure_2fa_nag' ) );
27 add_action( 'network_admin_notices', array( $this, 'user_setup_2fa_nag' ) );
28 }
29 }
30 }
31
32 /**
33 * The nag content
34 */
35 public function user_setup_2fa_nag( $is_shortcode = '', $configure_2fa_url = '' ) {
36
37 $this->ensureUser();
38
39 if ( isset( $_GET['user_id'] ) ) {
40 $current_profile_user_id = (int) $_GET['user_id'];
41 } elseif ( ! is_null( $this->wp2faUser->getUser() ) ) {
42 $current_profile_user_id = $this->wp2faUser->getUser()->ID;
43 } else {
44 $current_profile_user_id = false;
45 }
46
47 if ( ! $current_profile_user_id ||
48 isset( $_GET['user_id'] ) &&
49 $_GET['user_id'] !== $this->wp2faUser->getUser()->ID ||
50 $this->wp2faUser->getEnforcedInstantly() ) {
51 return;
52 }
53
54 $grace_expiry = $this->wp2faUser->getGracePeriodExpiration();
55
56 $class = 'notice notice-info wp-2fa-nag';
57
58 if ( $this->wp2faUser->needsToReconfigure2FA() ) {
59 $message = esc_html__( 'The 2FA method you were using is no longer allowed on this website. Please reconfigure 2FA using one of the supported methods within', 'wp-2fa' );
60 } else {
61 $message = esc_html__( 'This website’s administrator requires you to enable 2FA authentication', 'wp-2fa' );
62 }
63
64 $is_nag_dismissed = $this->wp2faUser->getDismissedNag();
65 $is_nag_needed = WP2FA::isUserEnforced( $this->wp2faUser->getUser()->ID );
66 $is_user_excluded = WP2FA::is_user_excluded( $this->wp2faUser->getUser()->ID );
67 $enabled_methods = $this->wp2faUser->getEnabledMethods();
68 $new_page_id = WP2FA::get_wp2fa_setting( 'custom-user-page-id' );
69 $new_page_permalink = get_permalink( $new_page_id );
70
71 $setup_url = Settings::getSetupPageLink();
72
73 // Allow setup URL to be customized if outputting via shortcode.
74 if ( isset( $is_shortcode ) && 'output_shortcode' === $is_shortcode && ! empty( $configure_2fa_url ) ) {
75 $setup_url = $configure_2fa_url;
76 }
77
78 // Stop the page from being a link to a page this user cant access if needed.
79 if ( WP2FA::is_this_multisite() && ! is_user_member_of_blog( $this->wp2faUser->getUser()->ID ) ) {
80 $new_page_id = false;
81 }
82
83 // If we have a custom page generated, lets use it.
84 if ( ! empty( $new_page_id ) && $new_page_permalink ) {
85 $setup_url = $new_page_permalink;
86 }
87
88 // If the nag has not already been dismissed, and of course if the user is eligible, lets show them something.
89 if ( ! $is_nag_dismissed && $is_nag_needed && empty( $enabled_methods ) && ! $is_user_excluded && ! empty( $grace_expiry ) ) {
90 echo '<div class="'.esc_attr( $class ).'">';
91 echo '<p>'.esc_html( $message );
92 echo ' <span class="grace-period-countdown">'.esc_attr( DateTimeUtils::format_grace_period_expiration_string(null, $grace_expiry) ).'</span>';
93 echo ' <a href="'.esc_url( $setup_url ).'" class="button button-primary">'.esc_html__( 'Configure 2FA now', 'wp-2fa' ).'</a>';
94 echo ' <a href="#" class="button button-secondary dismiss-user-configure-nag">'.esc_html__( 'Remind me on next login', 'wp-2fa' ).'</a></p>';
95 echo '</div>';
96 } else {
97 $this->user_reconfigure_2fa_nag();
98 }
99 }
100
101 /**
102 * The nag content
103 */
104 public function user_reconfigure_2fa_nag() {
105
106 $this->ensureUser();
107
108 // If the nag has not already been dismissed, and of course if the user is eligible, lets show them something.
109 if ( $this->wp2faUser->needsToReconfigureMethod() ) {
110 $class = 'notice notice-info wp-2fa-nag';
111
112 $message = esc_html__( 'The 2FA method you were using is no longer allowed on this website. Please reconfigure 2FA using one of the supported methods.', 'wp-2fa' );
113
114 echo '<div class="'.esc_attr( $class ).'"><p>'.esc_html( $message );
115 echo ' <a href="'.esc_url( Settings::getSetupPageLink() ).'" class="button button-primary">'.esc_html__( 'Configure 2FA now', 'wp-2fa' ).'</a>';
116 echo ' <a href="#" class="button button-secondary dismiss-user-reconfigure-nag">'.esc_html__( 'I\'ll do it later', 'wp-2fa' ).'</a></p>';
117 echo '</div>';
118 }
119 }
120
121 /**
122 * Dismiss notice and setup a user meta value so we know its been dismissed
123 */
124 public function dismiss_nag() {
125 $this->ensureUser();
126 $this->wp2faUser->setDismissedNag();
127 }
128
129 /**
130 * Reset the nag when the user logs out, so they get it again next time.
131 */
132 public function reset_nag( $user_id ) {
133 $this->wp2faUser = new User( $user_id );
134 $this->wp2faUser->deleteUserMeta('wp_2fa_update_nag_dismissed');
135 }
136
137 /**
138 * Sets user variable
139 *
140 * @return void
141 */
142 public function ensureUser() {
143 if ( ! isset( $this->wp2faUser ) ) {
144 $this->wp2faUser = new User();
145 }
146 }
147 }
148