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 / Methods / class-email.php
wp-2fa / includes / classes / Admin / Methods Last commit date
Traits 2 years ago class-backup-codes.php 2 years ago class-email-wizard-steps.php 2 years ago class-email.php 2 years ago class-totp-wizard-steps.php 2 years ago class-totp.php 2 years ago index.php 2 years ago
class-email.php
203 lines
1 <?php
2 /**
3 * Responsible for WP2FA user's email method manipulation.
4 *
5 * @package wp2fa
6 * @subpackage methods
7 *
8 * @copyright %%YEAR%% Melapress
9 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
10 *
11 * @see https://wordpress.org/plugins/wp-2fa/
12 *
13 * @since 2.6.0
14 */
15
16 declare(strict_types=1);
17
18 namespace WP2FA\Methods;
19
20 use WP2FA\WP2FA;
21 use WP2FA\Admin\Controllers\Settings;
22 use WP2FA\Methods\Wizards\Email_Wizard_Steps;
23
24 /**
25 * Class for handling email codes.
26 *
27 * @since 2.6.0
28 *
29 * @package WP2FA
30 */
31 if ( ! class_exists( '\WP2FA\Methods\Email' ) ) {
32 /**
33 * Email code class, for handling email method code generation and such.
34 *
35 * @since 2.6.0
36 */
37 class Email {
38
39 /**
40 * The name of the method.
41 *
42 * @var string
43 *
44 * @since 2.6.0
45 */
46 public const METHOD_NAME = 'email';
47
48 /**
49 * The name of the method stored in the policy
50 *
51 * @var string
52 *
53 * @since 2.6.0
54 */
55 public const POLICY_SETTINGS_NAME = 'enable_email';
56
57 /**
58 * Is the mail enabled
59 *
60 * @since 2.6.0
61 *
62 * @var bool
63 */
64 private static $email_enabled = null;
65
66 /**
67 * Inits the class and sets the filters.
68 *
69 * @return void
70 *
71 * @since 2.6.0
72 */
73 public static function init() {
74
75 \add_filter( WP_2FA_PREFIX . 'providers_translated_names', array( __CLASS__, 'email_provider_name_translated' ) );
76
77 \add_filter( WP_2FA_PREFIX . 'providers', array( __CLASS__, 'email_provider' ) );
78
79 \add_filter( WP_2FA_PREFIX . 'default_settings', array( __CLASS__, 'add_default_settings' ) );
80
81 \add_filter( WP_2FA_PREFIX . 'loop_settings', array( __CLASS__, 'settings_loop' ), 10, 1 );
82
83 \add_filter( WP_2FA_PREFIX . 'no_method_enabled', array( __CLASS__, 'return_default_selection' ), 10, 1 );
84
85 // add the TOTP methods to the list of available methods if enabled.
86 \add_filter(
87 WP_2FA_PREFIX . 'available_2fa_methods',
88 function ( $available_methods ) {
89 if ( ! empty( Settings::get_role_or_default_setting( self::POLICY_SETTINGS_NAME, 'current' ) ) ) {
90 array_push( $available_methods, self::METHOD_NAME );
91 }
92
93 return $available_methods;
94 }
95 );
96
97 Email_Wizard_Steps::init();
98 }
99
100 /**
101 * Adds email provider translatable name
102 *
103 * @param array $providers - Array with all currently supported providers and their translated names.
104 *
105 * @return array
106 *
107 * @since 2.6.0
108 */
109 public static function email_provider_name_translated( array $providers ) {
110 $providers[ self::METHOD_NAME ] = esc_html__( 'HOTP (Email)', 'wp-2fa' );
111
112 return $providers;
113 }
114
115 /**
116 * Adds email as a provider
117 *
118 * @param array $providers - Array with all currently supported providers.
119 *
120 * @return array
121 *
122 * @since 2.6.0
123 */
124 public static function email_provider( array $providers ) {
125 array_push( $providers, self::METHOD_NAME );
126
127 return $providers;
128 }
129
130 /**
131 * Adds the extension default settings to the main plugin settings
132 *
133 * @param array $default_settings - array with plugin default settings.
134 *
135 * @return array
136 *
137 * @since 2.6.0
138 */
139 public static function add_default_settings( array $default_settings ) {
140 $default_settings[ self::POLICY_SETTINGS_NAME ] = self::POLICY_SETTINGS_NAME;
141 $default_settings['specify-email_hotp'] = '';
142 $default_settings['method_help_hotp_intro'] = '<h3>' . __( 'Setting up HOTP', 'wp-2fa' ) . '</h3><p>' . __( 'Please select the email address where the one-time code should be sent:', 'wp-2fa' ) . '</p>';
143 $default_settings['method_verification_hotp_pre'] = '<h3>' . __( 'Almost there…', 'wp-2fa' ) . '</h3><p>' . __( 'Please type in the one-time code sent to your email address to finalize the setup', 'wp-2fa' ) . '</p>';
144 $default_settings['hotp_reconfigure_intro'] = '<h3>' . __( '{reconfigure_or_configure_capitalized} one-time code over email method', 'wp-2fa' ) . '</h3><p>' . __( 'Please select the email address where the one-time code should be sent:', 'wp-2fa' ) . '</p>';
145 $default_settings['email-option-label'] = __( 'One-time code via email', 'wp-2fa' );
146
147 return $default_settings;
148 }
149
150 /**
151 * Add extension settings to the loop array
152 *
153 * @param array $loop_settings - Currently available settings array.
154 *
155 * @return array
156 *
157 * @since 2.6.0
158 */
159 public static function settings_loop( array $loop_settings ) {
160 array_push( $loop_settings, self::POLICY_SETTINGS_NAME );
161 array_push( $loop_settings, 'specify-email_hotp' );
162
163
164 return $loop_settings;
165 }
166
167 /**
168 * Extracts the selected value from the global settings (if set), and adds it to the output array
169 *
170 * @param array $output - The array with output values.
171 *
172 * @return array
173 *
174 * @since 2.6.0
175 */
176 public static function return_default_selection( array $output ) {
177 // No method is enabled, fall back to previous selected one - we don't want to break the logic.
178 $email_enabled = WP2FA::get_wp2fa_setting( self::POLICY_SETTINGS_NAME );
179
180 if ( $email_enabled ) {
181 $output[ self::POLICY_SETTINGS_NAME ] = $email_enabled;
182 }
183
184 return $output;
185 }
186
187 /**
188 * Returns the status of the mail method (enabled | disabled)
189 *
190 * @since 2.6.0
191 *
192 * @return boolean
193 */
194 public static function is_enabled(): bool {
195 if ( null === self::$email_enabled ) {
196 self::$email_enabled = empty( Settings::get_role_or_default_setting( 'enable_email', 'current' ) ) ? false : true;
197 }
198
199 return self::$email_enabled;
200 }
201 }
202 }
203