class-first-time-wizard-steps.php
2 years ago
class-grace-period-notifications.php
2 years ago
class-passord-reset-2fa.php
2 years ago
class-wizard-steps.php
2 years ago
index.php
2 years ago
class-passord-reset-2fa.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Roles and main settings grace period notifications class. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage views |
| 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 | |
| 14 | declare(strict_types=1); |
| 15 | |
| 16 | namespace WP2FA\Admin\Views; |
| 17 | |
| 18 | use WP2FA\Admin\Controllers\Settings; |
| 19 | use WP2FA\Extensions\RoleSettings\Role_Settings_Controller; |
| 20 | |
| 21 | if ( ! class_exists( '\WP2FA\Admin\Views\Password_Reset_2FA' ) ) { |
| 22 | /** |
| 23 | * Password_Reset_2FA - Class for rendering the plugin settings related to 2fa when user resets the password. |
| 24 | * |
| 25 | * @since 2.5.0 |
| 26 | */ |
| 27 | class Password_Reset_2FA { |
| 28 | public const PASSWORD_RESET_SETTINGS_NAME = 'password-reset-2fa-show'; |
| 29 | |
| 30 | public const ENABLED_SETTING_VALUE = 'password-reset-2fa'; |
| 31 | |
| 32 | /** |
| 33 | * Inits all the class related hooks. |
| 34 | * |
| 35 | * @return void |
| 36 | * |
| 37 | * @since 2.5.0 |
| 38 | */ |
| 39 | public static function init() { |
| 40 | if ( is_admin() ) { |
| 41 | \add_filter( WP_2FA_PREFIX . 'before_grace_period', array( __CLASS__, 'password_reset_setting' ), 10, 5 ); |
| 42 | \add_filter( WP_2FA_PREFIX . 'loop_settings', array( __CLASS__, 'add_setting_value' ) ); |
| 43 | } |
| 44 | \add_filter( WP_2FA_PREFIX . 'default_settings', array( __CLASS__, 'add_default_settings' ) ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Shows the settings for the grace period notifications behavior. |
| 49 | * |
| 50 | * @param string $role - The name of the role. |
| 51 | * @param string $name_prefix - Name prefix for the input name, includes the role name if provided. |
| 52 | * @param string $data_role - Data attribute - used by the JS. |
| 53 | * @param string $role_id - The role name, used to identify the inputs. |
| 54 | * |
| 55 | * @return string |
| 56 | * |
| 57 | * @since 2.5.0 |
| 58 | */ |
| 59 | public static function reset_settings( string $role = '', string $name_prefix = '', string $data_role = '', string $role_id = '' ) { |
| 60 | ob_start(); |
| 61 | |
| 62 | if ( class_exists( 'WP2FA\Extensions\RoleSettings\Role_Settings_Controller' ) ) { |
| 63 | $password_reset_action = Role_Settings_Controller::get_setting( $role, self::PASSWORD_RESET_SETTINGS_NAME, true ); |
| 64 | } else { |
| 65 | $password_reset_action = Settings::get_role_or_default_setting( self::PASSWORD_RESET_SETTINGS_NAME, null, null, true ); |
| 66 | } |
| 67 | ?> |
| 68 | <div class="sub-setting-indent"> |
| 69 | <fieldset> |
| 70 | <label for="password-reset-2fa<?php echo \esc_attr( $role_id ); ?>" style="margin-bottom: 10px; display: inline-block;"> |
| 71 | <input type="checkbox" name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( self::PASSWORD_RESET_SETTINGS_NAME ); ?>]" |
| 72 | id="password-reset-2fa<?php echo \esc_attr( $role_id ); ?>" |
| 73 | <?php echo $data_role; // phpcs:ignore?> |
| 74 | value="<?php echo esc_attr( self::ENABLED_SETTING_VALUE ); ?>" <?php checked( $password_reset_action, self::ENABLED_SETTING_VALUE ); ?> class="js-nested"> |
| 75 | <span><?php echo \esc_html__( 'Require 2FA on password reset', 'wp-2fa' ); ?></span> |
| 76 | </label> |
| 77 | </fieldset> |
| 78 | </div> |
| 79 | <?php |
| 80 | $html_content = ob_get_contents(); |
| 81 | ob_end_clean(); |
| 82 | |
| 83 | return $html_content; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Adds global plugin setting options. |
| 88 | * |
| 89 | * @param array $loop_settings - Array with current plugin settings. |
| 90 | * |
| 91 | * @return array |
| 92 | * |
| 93 | * @since 2.5.0 |
| 94 | */ |
| 95 | public static function add_setting_value( array $loop_settings ) { |
| 96 | $loop_settings[] = self::PASSWORD_RESET_SETTINGS_NAME; |
| 97 | |
| 98 | return $loop_settings; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Adds the extension default settings to the main plugin settings. |
| 103 | * |
| 104 | * @param array $default_settings - array with plugin default settings. |
| 105 | * |
| 106 | * @return array |
| 107 | * |
| 108 | * @since 2.5.0 |
| 109 | */ |
| 110 | public static function add_default_settings( array $default_settings ) { |
| 111 | $default_settings[ self::PASSWORD_RESET_SETTINGS_NAME ] = self::PASSWORD_RESET_SETTINGS_NAME; |
| 112 | |
| 113 | return $default_settings; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Password reset settings. |
| 118 | * |
| 119 | * @param string $content - HTML content. |
| 120 | * @param string $role - The name of the role. |
| 121 | * @param string $name_prefix - Name prefix for the input name, includes the role name if provided. |
| 122 | * @param string $data_role - Data attribute - used by the JS. |
| 123 | * @param string $role_id - The role name, used to identify the inputs. |
| 124 | * |
| 125 | * @return string |
| 126 | * |
| 127 | * @since 2.5.0 |
| 128 | */ |
| 129 | public static function password_reset_setting( string $content, string $role = '', string $name_prefix = '', string $data_role = '', string $role_id = '' ) { |
| 130 | ob_start(); |
| 131 | ?> |
| 132 | <h3><?php esc_html_e( 'Do you want to require 2FA when users reset their password?', 'wp-2fa' ); ?></h3> |
| 133 | <p class="description"> |
| 134 | <?php esc_html_e( 'When you enable this setting users will be required to enter a one-time code sent to them via email when resetting the password.', 'wp-2fa' ); ?> |
| 135 | </p> |
| 136 | |
| 137 | <table class="form-table"> |
| 138 | <tbody> |
| 139 | <tr> |
| 140 | <th><label for="password-reset-2fa<?php echo \esc_attr( $role_id ); ?>"><?php esc_html_e( 'Password reset', 'wp-2fa' ); ?></label></th> |
| 141 | <td> |
| 142 | <fieldset class="contains-hidden-inputs"> |
| 143 | <?php echo self::reset_settings( $role, $name_prefix, $data_role, $role_id ); ?> |
| 144 | </fieldset> |
| 145 | </td> |
| 146 | </tr> |
| 147 | </tbody> |
| 148 | </table> |
| 149 | <?php |
| 150 | |
| 151 | $html_content = ob_get_contents(); |
| 152 | ob_end_clean(); |
| 153 | |
| 154 | return $html_content; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 |