PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 1.3.0
WP 2FA – Two-factor authentication for WordPress v1.3.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 / WP2FA.php
wp-2fa / includes / classes Last commit date
Admin 6 years ago Authenticator 6 years ago Cron 6 years ago Shortcodes 6 years ago .gitkeep 6 years ago WP2FA.php 6 years ago
WP2FA.php
588 lines
1 <?php // phpcs:ignore
2
3 namespace WP2FA;
4
5 /**
6 * Main WP2FA Class.
7 */
8 class WP2FA {
9
10 /**
11 * Plugin version.
12 *
13 * @var string
14 */
15 public $version = WP_2FA_VERSION;
16
17 /**
18 * Options variables.
19 *
20 * @var array
21 */
22 protected static $wp_2fa_options;
23 protected static $wp_2fa_email_templates;
24
25 /**
26 * Instance wrapper.
27 *
28 * @var object
29 */
30 private static $instance = null;
31
32 /**
33 * Return plugin instance.
34 */
35 public static function get_instance() {
36
37 if ( null === self::$instance ) {
38 self::$instance = new self();
39 }
40
41 return self::$instance;
42 }
43
44 /**
45 * Contructor.
46 */
47 private function __construct() {
48 if ( self::is_this_multisite() ) {
49 self::$wp_2fa_options = get_network_option( null, 'wp_2fa_settings' );
50 self::$wp_2fa_email_templates = get_network_option( null, 'wp_2fa_email_settings' );
51 } else {
52 self::$wp_2fa_options = get_option( 'wp_2fa_settings' );
53 self::$wp_2fa_email_templates = get_option( 'wp_2fa_email_settings' );
54 }
55
56 // Activation/Deactivation.
57 register_activation_hook( WP_2FA_FILE, '\WP2FA\Core\activate' );
58 register_deactivation_hook( WP_2FA_FILE, '\WP2FA\Core\deactivate' );
59 // Register our uninstallation hook.
60 register_uninstall_hook( WP_2FA_FILE, '\WP2FA\Core\uninstall' );
61 }
62
63 /**
64 * Fire up classes.
65 */
66 public function init() {
67 // Bootstrap.
68 Core\setup();
69
70 $this->settings = new Admin\SettingsPage();
71 $this->wizard = new Admin\SetupWizard();
72 $this->authentication = new Authenticator\Authentication();
73 $this->backupcodes = new Authenticator\BackupCodes();
74 $this->login = new Authenticator\Login();
75 $this->user_notices = new Admin\UserNotices();
76 $this->crontasks = new Cron\CronTasks();
77 $this->user_registered = new Admin\UserRegistered();
78 $this->shortcodes = new Shortcodes\Shortcodes();
79
80 global $pagenow;
81 if ( 'profile.php' !== $pagenow || 'user-edit.php' !== $pagenow ) {
82 $this->user_profiles = new Admin\UserProfile();
83 }
84
85 $this->add_actions();
86 }
87
88 /**
89 * Add our plugins actions.
90 */
91 public function add_actions() {
92 // Plugin redirect on activation, only if we have no settings currently saved.
93 if ( empty( self::$wp_2fa_options ) || ! isset( self::$wp_2fa_options ) ) {
94 add_action( 'admin_init', array( $this, 'setup_redirect' ), 10 );
95 }
96
97 // SettingsPage.
98 if ( self::is_this_multisite() ) {
99 add_action( 'network_admin_menu', array( $this->settings, 'create_settings_admin_menu_multisite' ) );
100 add_action( 'network_admin_edit_update_wp2fa_network_options', array( $this->settings, 'update_wp2fa_network_options' ) );
101 add_action( 'network_admin_edit_update_wp2fa_network_email_options', array( $this->settings, 'update_wp2fa_network_email_options' ) );
102 add_action( 'network_admin_notices', array( $this->settings, 'settings_saved_network_admin_notice' ) );
103 } else {
104 add_action( 'admin_menu', array( $this->settings, 'create_settings_admin_menu' ) );
105 }
106 add_action( 'wp_ajax_get_all_users', array( $this->settings, 'get_all_users' ) );
107 add_action( 'wp_ajax_get_all_network_sites', array( $this->settings, 'get_all_network_sites' ) );
108 add_action( 'wp_ajax_unlock_account', array( $this->settings, 'unlock_account' ), 10, 1 );
109 add_action( 'admin_action_unlock_account', array( $this->settings, 'unlock_account' ), 10, 1 );
110 add_action( 'admin_action_remove_user_2fa', array( $this->settings, 'remove_user_2fa' ), 10, 1 );
111 add_action( 'wp_ajax_remove_user_2fa', array( $this->settings, 'remove_user_2fa' ), 10, 1 );
112 add_action( 'admin_menu', array( $this->settings, 'hide_settings' ), 999 );
113 add_action( 'plugin_action_links_' . WP_2FA_BASE, array( $this->settings, 'add_plugin_action_links' ) );
114 add_filter( 'display_post_states', array( $this->settings, 'add_display_post_states' ), 10, 2 );
115
116 // SetupWizard.
117 if ( self::is_this_multisite() ) {
118 add_action( 'network_admin_menu', array( $this->wizard, 'network_admin_menus' ), 10 );
119 add_action( 'admin_menu', array( $this->wizard, 'admin_menus' ), 10 );
120 } else {
121 add_action( 'admin_menu', array( $this->wizard, 'admin_menus' ), 10 );
122 }
123 add_action( 'plugins_loaded', array( $this, 'add_wizard_actions' ), 10 );
124 add_action( 'wp_ajax_send_authentication_setup_email', array( $this->wizard, 'send_authentication_setup_email' ) );
125 add_action( 'wp_ajax_regenerate_authentication_key', array( $this->wizard, 'regenerate_authentication_key' ) );
126
127 // UserNotices.
128 add_action( 'wp_ajax_dismiss_nag', array( $this->user_notices, 'dismiss_nag' ) );
129 add_action( 'wp_ajax_dismiss_nag', array( $this->user_notices, 'dismiss_reconfigure_nag' ) );
130 add_action( 'clear_auth_cookie', array( $this->user_notices, 'reset_nag' ) );
131
132 // UserProfile.
133 global $pagenow;
134 if ( 'profile.php' !== $pagenow || 'user-edit.php' !== $pagenow ) {
135 add_action( 'show_user_profile', array( $this->user_profiles, 'user_2fa_options' ) );
136 add_action( 'edit_user_profile', array( $this->user_profiles, 'user_2fa_options' ) );
137 if ( self::is_this_multisite() ) {
138 add_action( 'personal_options_update', array( $this->user_profiles, 'save_user_2fa_options' ) );
139 }
140 }
141 add_filter( 'user_row_actions', array( $this->user_profiles, 'user_2fa_row_actions' ), 10, 2 );
142 if ( self::is_this_multisite() ) {
143 add_filter( 'ms_user_row_actions', array( $this->user_profiles, 'user_2fa_row_actions' ), 10, 2 );
144 }
145 add_action( 'wp_ajax_validate_authcode_via_ajax', array( $this->user_profiles, 'validate_authcode_via_ajax' ) );
146
147 // Login.
148 add_action( 'init', array( $this->login, 'get_providers' ) );
149 add_action( 'wp_login', array( $this->login, 'wp_login' ), 10, 2 );
150 add_action( 'login_form_validate_2fa', array( $this->login, 'login_form_validate_2fa' ) );
151 add_action( 'login_form_backup_2fa', array( $this->login, 'backup_2fa' ) );
152
153 /**
154 * Keep track of all the user sessions for which we need to invalidate the
155 * authentication cookies set during the initial password check.
156 */
157 add_action( 'set_auth_cookie', array( $this->login, 'collect_auth_cookie_tokens' ) );
158 add_action( 'set_logged_in_cookie', array( $this->login, 'collect_auth_cookie_tokens' ) );
159
160 // Run only after the core wp_authenticate_username_password() check.
161 add_filter( 'authenticate', array( $this->login, 'filter_authenticate' ), 50 );
162 add_filter( 'wp_authenticate_user', array( $this->login, 'is_user_locked' ), 10, 2 );
163
164 // User Register.
165 add_action( 'set_user_role', array( $this->user_registered, 'check_user_upon_role_change' ), 10, 3 );
166 }
167
168 /**
169 * Add actions specific to the wizard.
170 */
171 public function add_wizard_actions() {
172 if ( function_exists( 'wp_get_current_user' ) && current_user_can( 'read' ) ) {
173 add_action( 'admin_init', array( $this->wizard, 'setup_page' ), 10 );
174 }
175 }
176
177 /**
178 * Redirect user to 1st time setup.
179 */
180 public function setup_redirect() {
181
182 // Bail early before the redirect if the user can't manage options.
183 if ( ! current_user_can( 'manage_options' ) ) {
184 return;
185 }
186
187 if ( get_option( 'wp_2fa_redirect_on_activate', false ) ) {
188 // Delete redirect option.
189 delete_option( 'wp_2fa_redirect_on_activate' );
190
191 // Redirect URL.
192 $redirect = '';
193
194 // If current site is multisite and user is super-admin then redirect to network audit log.
195 if ( self::is_this_multisite() && is_super_admin() ) {
196 $redirect = add_query_arg( 'page', 'wp-2fa-setup', network_admin_url( 'index.php' ) );
197 } else {
198 // Otherwise redirect to main audit log view.
199 $redirect = add_query_arg( 'page', 'wp-2fa-setup', admin_url( 'options-general.php' ) );
200 }
201 wp_safe_redirect( $redirect );
202 exit();
203 }
204 }
205
206 /**
207 * Check is this is a multisite setup.
208 */
209 public static function is_this_multisite() {
210 return function_exists( 'is_multisite' ) && is_multisite();
211 }
212
213 /**
214 * Return user roles.
215 *
216 * @return array User roles.
217 */
218 public static function wp_2fa_get_roles() {
219 global $wp_roles;
220 $roles = $wp_roles->role_names;
221 return $roles;
222 }
223
224 /**
225 * Check to see if the user or user role is excluded.
226 *
227 * @param int $user_id User id.
228 * @return boolean Is user excluded or not.
229 */
230 public static function is_user_excluded( $user_id, $excluded_users = '', $excluded_roles = '', $excluded_sites = '' ) {
231
232 // Check if the $user_id is actually an object, if so lets just use it.
233 if ( ! is_a( $user_id, '\WP_User' ) ) {
234 if ( isset( $user_id ) ) {
235 $user = get_user_by( 'id', $user_id );
236 $user_roles = $user->roles;
237 } elseif ( isset( $_GET['user_id'] ) ) {
238 $user_id = (int) $_GET['user_id'];
239 $user = get_user_by( 'id', $user_id );
240 $user_roles = $user->roles;
241 } else {
242 $user = wp_get_current_user();
243 $user_roles = $user->roles;
244 }
245 } else {
246 $user = $user_id;
247 $user_roles = $user->roles;
248 }
249
250 $user_excluded = false;
251
252 if ( isset( $excluded_users ) && ! empty( $excluded_users ) ) {
253 $excluded_users = $excluded_users;
254 } else {
255 $excluded_users = WP2FA::get_wp2fa_setting( 'excluded_users' );
256 }
257
258 if ( ! empty( $excluded_users ) ) {
259 // Turn it into an array.
260 $excluded_users_array = explode( ',', $excluded_users );
261 // Compare our roles with the users and see if we get a match.
262 $result = in_array( $user->user_login, $excluded_users_array, true );
263 if ( $result ) {
264 $user_excluded = true;
265 return true;
266 }
267 }
268
269 if ( isset( $excluded_roles ) && ! empty( $excluded_roles ) ) {
270 $excluded_roles = $excluded_roles;
271 } else {
272 $excluded_roles = WP2FA::get_wp2fa_setting( 'excluded_roles' );
273 }
274
275 if ( ! empty( $excluded_roles ) ) {
276 // Turn it into an array.
277 $excluded_roles_array = explode( ',', strtolower( $excluded_roles ) );
278 // Compare our roles with the users and see if we get a match.
279 $result = array_intersect( $excluded_roles_array, $user->roles );
280 if ( $result ) {
281 $user_excluded = true;
282 return true;
283 }
284 }
285
286 if ( self::is_this_multisite() ) {
287 if ( isset( $excluded_sites ) && ! empty( $excluded_sites ) ) {
288 $excluded_sites = $excluded_sites;
289 } else {
290 $excluded_sites = WP2FA::get_wp2fa_setting( 'excluded_sites' );
291 }
292
293 if ( ! empty( $excluded_sites ) ) {
294 // Turn it into an array.
295 $excluded_site_array = explode( ',', strtolower( $excluded_sites ) );
296 $site_ids_only = array();
297 // Remove everything but the excluded sites ID, which we will use to check if user is a member.
298 foreach ( $excluded_site_array as $excluded_site ) {
299 if ( isset( explode( ':', $excluded_site )[1] ) ) {
300 $id = trim( explode( ':', $excluded_site )[1] );
301 $site_ids_only[] = $id;
302 }
303 }
304 foreach ( $site_ids_only as $site_id ) {
305 if ( is_user_member_of_blog( $user->ID, $site_id ) ) {
306 // User is a member of the a blog we are excluding from 2FA.
307 $user_excluded = true;
308 return true;
309 } else {
310 // User is NOT a member of the a blog we are excluding.
311 $user_excluded = false;
312 }
313 }
314 }
315 }
316
317 if ( true === $user_excluded ) {
318 return true;
319 }
320
321 return false;
322 }
323
324 /**
325 * Util function to grab settings or apply defaults if no settings are saved into the db.
326 *
327 * @param string $setting_name Settings to grab value of.
328 * @return string Settings value or default value.
329 */
330 public static function get_wp2fa_setting( $setting_name = '' ) {
331 $default_settings = array(
332 'enable_totp' => 'enable_totp',
333 'enable_email' => 'enable_email',
334 'enforcment-policy' => 'do-not-enforce',
335 'excluded_users' => '',
336 'excluded_roles' => '',
337 'enforced_users' => '',
338 'enforced_roles' => '',
339 'grace-period' => 3,
340 'grace-period-denominator' => 'days',
341 'enable_grace_cron' => '',
342 'enable_destroy_session' => '',
343 'limit_access' => '',
344 '2fa_settings_last_updated_by' => '',
345 '2fa_main_user' => '',
346 'grace-period-expiry-time' => '',
347 'plugin_version' => WP_2FA_VERSION,
348 'delete_data_upon_uninstall' => '',
349 'excluded_sites' => '',
350 'create-custom-user-page' => 'no',
351 'custom-user-page-url' => '',
352 'custom-user-page-id' => '',
353 );
354
355 $apply_defaults = false;
356
357 // If we have no setting name, return them all.
358 if ( empty( $setting_name ) ) {
359 return self::$wp_2fa_options;
360 }
361
362 // First lets check if any options have been saved.
363 if ( empty( self::$wp_2fa_options ) || ! isset( self::$wp_2fa_options ) ) {
364 $apply_defaults = true;
365 }
366
367 if ( $apply_defaults ) {
368 return $default_settings[ $setting_name ];
369 } elseif ( ! isset( self::$wp_2fa_options[ $setting_name ] ) ) {
370 return false;
371 } else {
372 return self::$wp_2fa_options[ $setting_name ];
373 }
374 }
375
376 /**
377 * Util function to grab EMAIL settings or apply defaults if no settings are saved into the db.
378 *
379 * @param string $setting_name Settings to grab value of.
380 * @return string Settings value or default value.
381 */
382 public static function get_wp2fa_email_templates( $setting_name = '', $grab_default = '' ) {
383
384 if ( ! empty( WP2FA::get_wp2fa_setting( 'custom-user-page-id' ) ) ) {
385 // Setup enable 2fa email.
386 $enable_2fa_message = sprintf(
387 /* translators: %1$s: is the user name, %2$s is the website address */
388 '<p>%1$s %2$s %3$s <strong>%4$s</strong> %5$s <strong>%6$s.</strong></p><p>%11$s <a href="%12$s" target="_blank">%12$s.</a></p><p>%7$s</p><p>%8$s</p><p>%9$s <a href="https://www.wpwhitesecurity.com/wordpress-plugins/wp-2fa/" target="_blank">%10$s</a></p>',
389 esc_html__( 'The administrator of the website', 'wp-2fa' ),
390 '{site_url}',
391 esc_html__( 'enforced two-factor authentication. You have', 'wp-2fa' ),
392 '{grace_period}',
393 esc_html__( 'to enable and configure 2FA on your WordPress user', 'wp-2fa' ),
394 '{user_login_name}',
395 esc_html__( 'Failing to enable 2FA within the grace period will result in a locked account. In case that happens contact the website’s administrator.', 'wp-2fa' ),
396 esc_html__( 'Thank you.', 'wp-2fa' ),
397 esc_html__( 'Email sent by', 'wp-2fa' ),
398 esc_html__( 'WP 2FA plugin.', 'wp-2fa' ),
399 esc_html__( 'You can configure 2FA from this page:', 'wp-2fa' ),
400 '{2fa_settings_page_url}'
401 );
402 } else {
403 // Setup enable 2fa email.
404 $enable_2fa_message = sprintf(
405 /* translators: %1$s: is the user name, %2$s is the website address */
406 '<p>%1$s %2$s %3$s <strong>%4$s</strong> %5$s <strong>%6$s.</strong></p><p>%7$s</p><p>%8$s</p><p>%9$s <a href="https://www.wpwhitesecurity.com/wordpress-plugins/wp-2fa/" target="_blank">%10$s</a></p>',
407 esc_html__( 'The administrator of the website', 'wp-2fa' ),
408 '{site_url}',
409 esc_html__( 'enforced two-factor authentication. You have', 'wp-2fa' ),
410 '{grace_period}',
411 esc_html__( 'to enable and configure 2FA on your WordPress user', 'wp-2fa' ),
412 '{user_login_name}',
413 esc_html__( 'Failing to enable 2FA within the grace period will result in a locked account. In case that happens contact the website’s administrator.', 'wp-2fa' ),
414 esc_html__( 'Thank you.', 'wp-2fa' ),
415 esc_html__( 'Email sent by', 'wp-2fa' ),
416 esc_html__( 'WP 2FA plugin.', 'wp-2fa' )
417 );
418 }
419
420 $enable_2fa_subject = __( 'Please enable 2FA on {site_url}', 'wp-2fa' );
421 $enable_2fa_body = $enable_2fa_message;
422
423 // Setup login code email.
424 $login_code_message = sprintf(
425 /* translators: %s: the token for the user to use */
426 '<p>%1$s <strong>%2$s</strong> %3$s</p><p>%4$s</p><p>%5$s <a href="https://www.wpwhitesecurity.com/wordpress-plugins/wp-2fa/" target="_blank">%6$s</a></p>',
427 esc_html__( 'Enter', 'wp-2fa' ),
428 '{login_code}',
429 esc_html__( 'to log in.', 'wp-2fa' ),
430 esc_html__( 'Thank you.', 'wp-2fa' ),
431 esc_html__( 'Email sent by', 'wp-2fa' ),
432 esc_html__( 'WP 2FA plugin.', 'wp-2fa' )
433 );
434
435 $login_code_subject = __( 'Your login confirmation code for {site_name}', 'wp-2fa' );
436 $login_code_body = $login_code_message;
437
438 // Setup user locked email.
439 $user_locked_message = sprintf(
440 /* translators: %1$s: is the user name, %2$s is the website name */
441 '<p>%1$s</p><p>%2$s %3$s %4$s %5$s %6$s</p><p>%7$s</p><p>%8$s</p><p>%9$s <a href="https://www.wpwhitesecurity.com/wordpress-plugins/wp-2fa/" target="_blank">%10$s</a></p>',
442 esc_html__( 'Hello.', 'wp-2fa' ),
443 esc_html__( 'Since you have not enabled two-factor authentication for the user', 'wp-2fa' ),
444 '{user_login_name}',
445 esc_html__( 'on the website', 'wp-2fa' ),
446 '{site_name}',
447 esc_html__( 'within the grace period, your account has been locked.', 'wp-2fa' ),
448 esc_html__( 'Contact your website administrator to unlock your account.', 'wp-2fa' ),
449 esc_html__( 'Thank you.', 'wp-2fa' ),
450 esc_html__( 'Email sent by', 'wp-2fa' ),
451 esc_html__( 'WP 2FA plugin.', 'wp-2fa' )
452 );
453
454 $user_locked_subject = __( 'Your user on {site_name} has been locked', 'wp-2fa' );
455 $user_locked_body = $user_locked_message;
456
457 if ( ! empty( WP2FA::get_wp2fa_setting( 'custom-user-page-id' ) ) ) {
458 // Setup user unlocked email.
459 $user_unlocked_message = sprintf(
460 /* translators: %1$s: is the user name, %2$s is the website address */
461 '<p>%1$s</p><p>%2$s <strong>%3$s</strong> %4$s %5$s %6$s</p><p>%10$s <a href="%11$s" target="_blank">%11$s.</a></p><p>%7$s</p><p>%8$s <a href="https://www.wpwhitesecurity.com/wordpress-plugins/wp-2fa/" target="_blank">%9$s</a></p>',
462 esc_html__( 'Hello,', 'wp-2fa' ),
463 esc_html__( 'Your user', 'wp-2fa' ),
464 '{user_login_name}',
465 esc_html__( 'on the website', 'wp-2fa' ),
466 '{site_url}',
467 esc_html__( 'has been unlocked. Please configure two-factor authentication within the grace period, otherwise your account will be locked again.', 'wp-2fa' ),
468 esc_html__( 'Thank you.', 'wp-2fa' ),
469 esc_html__( 'Email sent by', 'wp-2fa' ),
470 esc_html__( 'WP 2FA plugin', 'wp-2fa' ),
471 esc_html__( 'You can configure 2FA from this page:', 'wp-2fa' ),
472 '{2fa_settings_page_url}'
473 );
474 } else {
475 // Setup user unlocked email.
476 $user_unlocked_message = sprintf(
477 /* translators: %1$s: is the user name, %2$s is the website address */
478 '<p>%1$s</p><p>%2$s <strong>%3$s</strong> %4$s %5$s %6$s</p><p>%7$s</p><p>%8$s <a href="https://www.wpwhitesecurity.com/wordpress-plugins/wp-2fa/" target="_blank">%9$s</a></p>',
479 esc_html__( 'Hello,', 'wp-2fa' ),
480 esc_html__( 'Your user', 'wp-2fa' ),
481 '{user_login_name}',
482 esc_html__( 'on the website', 'wp-2fa' ),
483 '{site_url}',
484 esc_html__( 'has been unlocked. Please configure two-factor authentication within the grace period, otherwise your account will be locked again.', 'wp-2fa' ),
485 esc_html__( 'Thank you.', 'wp-2fa' ),
486 esc_html__( 'Email sent by', 'wp-2fa' ),
487 esc_html__( 'WP 2FA plugin', 'wp-2fa' )
488 );
489 }
490
491 $user_unlocked_subject = __( 'Your user on {site_name} has been unlocked', 'wp-2fa' );
492 $user_unlocked_body = $user_unlocked_message;
493
494 // Array of defaults, now we have things setup above.
495 $default_settings = array(
496 'email_from_setting' => 'use-defaults',
497 'custom_from_email_address' => '',
498 'custom_from_display_name' => '',
499 'enforced_email_subject' => $enable_2fa_subject,
500 'enforced_email_body' => $enable_2fa_body,
501 'login_code_email_subject' => $login_code_subject,
502 'login_code_email_body' => $login_code_body,
503 'user_account_locked_email_subject' => $user_locked_subject,
504 'user_account_locked_email_body' => $user_locked_body,
505 'user_account_unlocked_email_subject' => $user_unlocked_subject,
506 'user_account_unlocked_email_body' => $user_unlocked_body,
507 'send_enforced_email' => 'enable_enforced_email',
508 'send_account_locked_email' => 'enable_account_locked_email',
509 'send_account_unlocked_email' => 'enable_account_unlocked_email',
510 );
511
512 $apply_defaults = false;
513
514 // If we have no setting name, return them all.
515 if ( empty( $setting_name ) ) {
516 return self::$wp_2fa_email_templates;
517 }
518
519 // First lets check if any options have been saved.
520 if ( empty( self::$wp_2fa_email_templates ) || ! isset( self::$wp_2fa_email_templates ) ) {
521 $apply_defaults = true;
522 }
523
524 if ( $apply_defaults || ! empty( $grab_default ) ) {
525 return $default_settings[ $setting_name ];
526 } elseif ( ! isset( self::$wp_2fa_email_templates[ $setting_name ] ) ) {
527 return false;
528 } else {
529 return self::$wp_2fa_email_templates[ $setting_name ];
530 }
531 }
532
533 /**
534 * Util which we use to replace our {strings} with actual, useful stuff.
535 *
536 * @param string $input Text we are working on.
537 * @param int $user_id User id, if its needed.
538 * @param string $token Login code, if its needed..
539 * @return string The output, with all the {strings} swapped out.
540 */
541 public static function replace_email_strings( $input = '', $user_id = '', $token = '' ) {
542
543 // Gather grace period.
544 $grace_period = self::get_wp2fa_setting( 'grace-period' );
545 $grace_period_denominator = self::get_wp2fa_setting( 'grace-period-denominator' );
546 $grace_period = $grace_period . ' ' . $grace_period_denominator;
547
548 // Setup user data.
549 if ( isset( $user_id ) && ! empty( $user_id ) ) {
550 $user = get_userdata( $user_id );
551 } else {
552 $user = wp_get_current_user();
553 }
554
555 // Setup token.
556 if ( isset( $token ) && ! empty( $token ) ) {
557 $login_code = $token;
558 } else {
559 $login_code = '';
560 }
561
562 $new_page_id = WP2FA::get_wp2fa_setting( 'custom-user-page-id' );
563 if ( ! empty( $new_page_id ) ) {
564 $new_page_permalink = get_permalink( $new_page_id );
565 } else {
566 $new_page_permalink = '';
567 }
568
569 // These are the strings we are going to search for, as well as there respective replacements.
570 $replacements = array(
571 '{site_url}' => esc_url( get_bloginfo( 'url' ) ),
572 '{site_name}' => sanitize_text_field( get_bloginfo( 'name' ) ),
573 '{grace_period}' => sanitize_text_field( $grace_period ),
574 '{user_login_name}' => sanitize_text_field( $user->user_login ),
575 '{login_code}' => sanitize_text_field( $login_code ),
576 '{2fa_settings_page_url}' => esc_url( $new_page_permalink ),
577 );
578
579 $replacements = apply_filters(
580 'wp_2fa_replacement_email_strings',
581 $replacements
582 );
583
584 $final_output = str_replace( array_keys( $replacements ), array_values( $replacements ), $input );
585 return $final_output;
586 }
587 }
588