Traits
1 year ago
class-backup-codes.php
1 year ago
class-email-wizard-steps.php
1 year ago
class-email.php
1 year ago
class-totp-wizard-steps.php
1 year ago
class-totp.php
1 year ago
index.php
1 year ago
class-email.php
205 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for WP2FA user's email method manipulation. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage methods |
| 7 | * |
| 8 | * @copyright 2024 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'] = 'specify-email_hotp'; |
| 142 | $default_settings['method_help_hotp_intro'] = '<h3>' . __( 'Setting up HOTP (one-time code via email)', 'wp-2fa' ) . '</h3><p>' . __( 'Please select the email address where the one-time code should be sent:', 'wp-2fa' ) . '</p>'; |
| 143 | $default_settings['method_help_hotp_help'] = __( 'To complete the 2FA configuration you will be sent a one-time code over email, therefore you should have access to the mailbox of this email address. If you do not receive the email with the one-time code please check your spam folder and contact your administrator', 'wp-2fa' ); |
| 144 | $default_settings['method_help_hotp_help_email'] = '<b>' . __( 'IMPORTANT', 'wp-2fa' ) . '</b><p>' . __( 'To ensure you always receive the one-time code whitelist the email address from which the codes are sent. This is {from_email}', 'wp-2fa' ) . '</p>'; |
| 145 | $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>'; |
| 146 | $default_settings['hotp_reconfigure_intro'] = '<h3>' . __( '{reconfigure_or_configure_capitalized} one-time code over email method', 'wp-2fa' ) . '</h3><p>' . __( 'Click the below button to {reconfigure_or_configure} the email address where the one-time code should be sent.', 'wp-2fa' ) . '</p>'; |
| 147 | $default_settings['email-option-label'] = __( 'One-time code via email', 'wp-2fa' ); |
| 148 | |
| 149 | return $default_settings; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Add extension settings to the loop array |
| 154 | * |
| 155 | * @param array $loop_settings - Currently available settings array. |
| 156 | * |
| 157 | * @return array |
| 158 | * |
| 159 | * @since 2.6.0 |
| 160 | */ |
| 161 | public static function settings_loop( array $loop_settings ) { |
| 162 | array_push( $loop_settings, self::POLICY_SETTINGS_NAME ); |
| 163 | array_push( $loop_settings, 'specify-email_hotp' ); |
| 164 | |
| 165 | |
| 166 | return $loop_settings; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Extracts the selected value from the global settings (if set), and adds it to the output array |
| 171 | * |
| 172 | * @param array $output - The array with output values. |
| 173 | * |
| 174 | * @return array |
| 175 | * |
| 176 | * @since 2.6.0 |
| 177 | */ |
| 178 | public static function return_default_selection( array $output ) { |
| 179 | // No method is enabled, fall back to previous selected one - we don't want to break the logic. |
| 180 | $email_enabled = WP2FA::get_wp2fa_setting( self::POLICY_SETTINGS_NAME ); |
| 181 | |
| 182 | if ( $email_enabled ) { |
| 183 | $output[ self::POLICY_SETTINGS_NAME ] = $email_enabled; |
| 184 | } |
| 185 | |
| 186 | return $output; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the status of the mail method (enabled | disabled) |
| 191 | * |
| 192 | * @since 2.6.0 |
| 193 | * |
| 194 | * @return boolean |
| 195 | */ |
| 196 | public static function is_enabled(): bool { |
| 197 | if ( null === self::$email_enabled ) { |
| 198 | self::$email_enabled = empty( Settings::get_role_or_default_setting( 'enable_email', 'current' ) ) ? false : true; |
| 199 | } |
| 200 | |
| 201 | return self::$email_enabled; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 |