PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.6.0
WP 2FA – Two-factor authentication for WordPress v2.6.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 / class-user-notices.php
wp-2fa / includes / classes / Admin Last commit date
Controllers 2 years ago Helpers 2 years ago Methods 2 years ago SettingsPages 2 years ago Views 2 years ago class-help-contact-us.php 2 years ago class-premium-features.php 2 years ago class-settings-page.php 2 years ago class-setup-wizard.php 2 years ago class-user-listing.php 2 years ago class-user-notices.php 2 years ago class-user-profile.php 2 years ago class-user-registered.php 2 years ago index.php 5 years ago
class-user-notices.php
260 lines
1 <?php
2 /**
3 * Responsible for WP2FA user's notifying.
4 *
5 * @package wp2fa
6 * @subpackage user-utils
7 * @copyright %%YEAR%% Melapress
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;
13
14 use WP2FA\WP2FA;
15 use WP2FA\Admin\Helpers\WP_Helper;
16 use WP2FA\Freemius\User_Licensing;
17 use WP2FA\Admin\Controllers\Methods;
18 use WP2FA\Admin\Helpers\User_Helper;
19 use WP2FA\Admin\Controllers\Settings;
20 use WP2FA\Admin\Views\Grace_Period_Notifications;
21 use WP2FA\Extensions\Integrations\Plugins\Woocommerce;
22 use WP2FA\Extensions\Integrations\WP2FA_Integrations;
23 use WP2FA\Extensions\WhiteLabeling\White_Labeling_Render;
24
25 /**
26 * User_Notices class with user notification filters
27 *
28 * @since 2.4.0
29 */
30 if ( ! class_exists( '\WP2FA\Admin\User_Notices' ) ) {
31 /**
32 * User_Notices - Class for displaying notices to our users.
33 */
34 class User_Notices {
35
36 /**
37 * Lets set things up
38 */
39 public static function init() {
40 $enforcement_policy = WP2FA::get_wp2fa_setting( 'enforcement-policy' );
41 if ( ! empty( $enforcement_policy ) ) {
42 // Check we are supposed to, before adding action to show nag.
43 if ( in_array( $enforcement_policy, array( 'all-users', 'certain-roles-only', 'certain-users-only', 'superadmins-only', 'superadmins-siteadmins-only', 'enforce-on-multisite', true ), true ) ) {
44 $global_methods = Methods::get_available_2fa_methods();
45 $user = User_Helper::get_user_object();
46 $users_method = User_Helper::get_enabled_method_for_user( User_Helper::get_user_object() );
47
48 if ( Grace_Period_Notifications::notify_using_dashboard( User_Helper::get_user_object() ) ) {
49 add_action( 'admin_notices', array( __CLASS__, 'user_setup_2fa_nag' ) );
50 add_action( 'network_admin_notices', array( __CLASS__, 'user_setup_2fa_nag' ) );
51 }
52
53 // If enaabled method is no longer available, show nag so users reconfigures using an available remaining method.
54 if ( User_Helper::is_enforced( $user ) && ! empty( $users_method ) && empty( \array_intersect( array( $users_method ), $global_methods ) ) ) {
55 }
56 } elseif ( 'do-not-enforce' === WP2FA::get_wp2fa_setting( 'enforcement-policy' ) ) {
57 add_action( 'admin_notices', array( __CLASS__, 'user_reconfigure_2fa_nag' ) );
58 add_action( 'network_admin_notices', array( __CLASS__, 'user_setup_2fa_nag' ) );
59 }
60 }
61 }
62
63 /**
64 * The nag content
65 *
66 * @param string $is_shortcode - Is that a call from shortcode.
67 * @param string $configure_2fa_url - The configuration url.
68 *
69 * @return void
70 */
71 public static function user_setup_2fa_nag( $is_shortcode = '', $configure_2fa_url = '' ) {
72
73 if ( isset( $_GET['user_id'] ) ) { // phpcs:ignore
74 $current_profile_user_id = (int) $_GET['user_id']; // phpcs:ignore
75 } elseif ( ! is_null( User_Helper::get_user_object() ) ) {
76 $current_profile_user_id = User_Helper::get_user_object()->ID;
77 } else {
78 $current_profile_user_id = false;
79 }
80
81 if ( ! $current_profile_user_id ||
82 isset( $_GET['user_id'] ) && // phpcs:ignore
83 $_GET['user_id'] !== User_Helper::get_user_object()->ID || // phpcs:ignore
84 User_Helper::get_user_enforced_instantly( User_Helper::get_user_object() ) ) {
85 return;
86 }
87
88 $grace_expiry = (int) User_Helper::get_user_expiry_date( User_Helper::get_user_object() );
89
90 $class = 'notice notice-info wp-2fa-nag';
91
92 if ( User_Helper::get_user_needs_to_reconfigure_2fa( User_Helper::get_user_object() ) ) {
93 $message = WP2FA::get_wp2fa_white_label_setting( 'default-2fa-resetup-required-notice', true );
94 } else {
95 $message = WP2FA::get_wp2fa_white_label_setting( 'default-2fa-required-notice', true );
96 }
97
98 $is_nag_dismissed = User_Helper::get_nag_status();
99 $is_nag_needed = User_Helper::is_enforced( User_Helper::get_user_object()->ID );
100 $is_user_excluded = User_Helper::is_excluded( User_Helper::get_user_object()->ID );
101 $enabled_methods = User_Helper::get_enabled_method_for_user( User_Helper::get_user_object() );
102 $new_page_id = WP2FA::get_wp2fa_setting( 'custom-user-page-id' );
103
104 if ( empty( $new_page_id ) ) {
105 $new_page_id = Settings::get_custom_settings_page_id( '', User_Helper::get_user_object() );
106 }
107
108 $new_page_permalink = get_permalink( $new_page_id );
109
110 $setup_url = Settings::get_setup_page_link();
111
112 // Allow setup URL to be customized if outputting via shortcode.
113 if ( isset( $is_shortcode ) && 'output_shortcode' === $is_shortcode && ! empty( $configure_2fa_url ) ) {
114 $setup_url = $configure_2fa_url;
115 }
116
117 // Stop the page from being a link to a page this user cant access if needed.
118 if ( WP_Helper::is_multisite() && ! is_user_member_of_blog( User_Helper::get_user_object()->ID ) ) {
119 $new_page_id = false;
120 }
121
122 // If we have a custom page generated, lets use it.
123 if ( ! empty( $new_page_id ) && $new_page_permalink ) {
124 $setup_url = $new_page_permalink;
125 }
126
127 // If the nag has not already been dismissed, and of course if the user is eligible, lets show them something.
128 if ( ! $is_nag_dismissed && $is_nag_needed && empty( $enabled_methods ) && ! $is_user_excluded && ! empty( $grace_expiry ) ) {
129
130 $show = true;
131
132 if ( class_exists( '\WP2FA\Freemius\User_Licensing' ) ) {
133 $show = User_Licensing::enable_2fa_user_setting( true );
134 }
135
136 if ( $show ) {
137 echo '<div class="' . esc_attr( $class ) . '">';
138 echo wpautop( wp_kses_post( WP2FA::replace_remaining_grace_period( $message, (int) $grace_expiry ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
139 echo ' <a href="' . esc_url( $setup_url ) . '" class="button button-primary">' . esc_html__( 'Configure 2FA now', 'wp-2fa' ) . '</a>';
140 echo ' <a href="#" class="button button-secondary dismiss-user-configure-nag">' . esc_html__( 'Remind me on next login', 'wp-2fa' ) . '</a></p>';
141 echo '</div>';
142 }
143 } else {
144 self::user_reconfigure_2fa_nag();
145 }
146 }
147
148 /**
149 * The nag content
150 */
151 public static function user_reconfigure_2fa_nag() {
152
153 // If the nag has not already been dismissed, and of course if the user is eligible, lets show them something.
154 if ( User_Helper::needs_to_reconfigure_method() ) {
155 $class = 'notice notice-info wp-2fa-nag';
156
157 $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' );
158
159 echo '<div class="' . esc_attr( $class ) . '"><p>' . esc_html( $message );
160 echo ' <a href="' . esc_url( Settings::get_setup_page_link() ) . '" class="button button-primary">' . esc_html__( 'Configure 2FA now', 'wp-2fa' ) . '</a>';
161 echo ' <a href="#" class="button button-secondary wp-2fa-button-secondary dismiss-user-reconfigure-nag">' . esc_html__( 'I\'ll do it later', 'wp-2fa' ) . '</a></p>';
162 echo '</div>';
163 }
164 }
165
166
167 /**
168 * Dismiss notice and setup a user meta value so we know its been dismissed
169 */
170 public static function dismiss_nag() {
171 User_Helper::set_nag_status( true );
172 }
173
174 /**
175 * Reset the nag when the user logs out, so they get it again next time.
176 *
177 * @param [type] $user_id - The ID of the user.
178 *
179 * @return void
180 */
181 public static function reset_nag( $user_id ) {
182 User_Helper::remove_nag_status( $user_id );
183 }
184
185 /**
186 * Adds setting option in the white label settings page.
187 *
188 * @return void
189 *
190 * @since 2.5.0
191 */
192 public static function white_label_settings_text() {
193 ?>
194 <tr>
195 <th><label for="email-backup-method"><?php esc_html_e( '2FA mandatory notice', 'wp-2fa' ); ?></label></th>
196 <td>
197 <?php
198 echo White_Labeling_Render::get_method_text_editor( 'default-2fa-required-notice' ); // phpcs:ignore
199 ?>
200 <div style="margin-top: 5px;"><span><strong><i><?php esc_html_e( 'Note:', 'wp-2fa' ); ?></i></strong> <?php esc_html_e( 'Only plain text is allowed.', 'wp-2fa' ); ?></span></div>
201 </td>
202 </tr>
203 <tr>
204 <th><label for="email-backup-method"><?php esc_html_e( '2FA reconfiguration mandatory notice', 'wp-2fa' ); ?></label></th>
205 <td>
206 <?php
207 echo White_Labeling_Render::get_method_text_editor( 'default-2fa-resetup-required-notice' ); // phpcs:ignore
208 ?>
209 <div style="margin-top: 5px;"><span><strong><i><?php esc_html_e( 'Note:', 'wp-2fa' ); ?></i></strong> <?php esc_html_e( 'Only plain text is allowed.', 'wp-2fa' ); ?></span></div>
210 </td>
211 </tr>
212 <tr>
213 <th><label><?php esc_html_e( 'User profile 2FA configuration area title', 'wp-2fa' ); ?></label></th>
214 <td>
215 <?php
216 echo White_Labeling_Render::get_method_text_editor( 'user-profile-form-preamble-title' ); // phpcs:ignore
217 ?>
218 <div style="margin-top: 5px;"><span><strong><i><?php esc_html_e( 'Note:', 'wp-2fa' ); ?></i></strong> <?php esc_html_e( 'Only plain text is allowed.', 'wp-2fa' ); ?></span></div>
219 </td>
220 </tr>
221 <tr>
222 <th><label><?php esc_html_e( 'User profile 2FA configuration area description', 'wp-2fa' ); ?></label></th>
223 <td>
224 <?php
225 echo White_Labeling_Render::get_method_text_editor( 'user-profile-form-preamble-desc' ); // phpcs:ignore
226 ?>
227 <div style="margin-top: 5px;"><span><strong><i><?php esc_html_e( 'Note:', 'wp-2fa' ); ?></i></strong> <?php esc_html_e( 'Only plain text is allowed.', 'wp-2fa' ); ?></span></div>
228 </td>
229 </tr>
230 <?php
231 // phpcs:disable
232 // phpcs:enable
233 ?>
234 <?php
235 }
236
237 /**
238 * Adds and filters extension values in the settings store array ($output).
239 *
240 * @param array $output - Array with the currently stored settings.
241 * @param array $input - Array with the input ($_POST) values.
242 *
243 * @return array
244 *
245 * @since 2.5.0
246 */
247 public static function settings_store( array $output, array $input ) {
248 if ( isset( $input['default-2fa-required-notice'] ) ) {
249 $output['default-2fa-required-notice'] = \wp_kses_post( $input['default-2fa-required-notice'] );
250 }
251
252 if ( isset( $input['default-2fa-resetup-required-notice'] ) ) {
253 $output['default-2fa-resetup-required-notice'] = \wp_kses_post( $input['default-2fa-resetup-required-notice'] );
254 }
255
256 return $output;
257 }
258 }
259 }
260