class-login-attempts.php
1 day ago
class-methods-wizards-trait.php
1 day ago
class-provider-trait.php
1 day ago
class-settings-trait.php
1 day ago
class-validation-trait.php
1 day ago
class-white-label-trait.php
1 day ago
index.php
1 day ago
class-provider-trait.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the plugin login attempts |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage traits |
| 7 | * @copyright 2026 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\Methods\Traits; |
| 13 | |
| 14 | use WP2FA\Utils\Settings_Utils; |
| 15 | use WP2FA\Admin\Helpers\WP_Helper; |
| 16 | use WP2FA\Admin\SettingsPages\Settings_Page_Policies; |
| 17 | use WP2FA\Extensions\RoleSettings\Role_Settings_Controller; |
| 18 | |
| 19 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 20 | |
| 21 | if ( ! class_exists( '\WP2FA\Admin\Methods\Traits\Providers' ) ) { |
| 22 | /** |
| 23 | * Responsible for the providers basic functionality. |
| 24 | * |
| 25 | * @since 2.9.2 |
| 26 | */ |
| 27 | trait Providers { |
| 28 | |
| 29 | /** |
| 30 | * Stores the globally enabled methods (if it is enables as a global method or in some of the roles) |
| 31 | * |
| 32 | * @var array |
| 33 | * |
| 34 | * @since 3.0.0 |
| 35 | */ |
| 36 | private static $globally_enabled = array(); |
| 37 | |
| 38 | /** |
| 39 | * Inits all of the common hooks for all of the providers. |
| 40 | * |
| 41 | * @return void |
| 42 | * |
| 43 | * @since 2.9.2 |
| 44 | */ |
| 45 | public static function always_init() { |
| 46 | |
| 47 | \add_filter( WP_2FA_PREFIX . 'providers', array( __CLASS__, 'provider' ) ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Adds provider to the global providers array |
| 52 | * |
| 53 | * @param array $providers - Array with all currently supported providers. |
| 54 | * |
| 55 | * @return array |
| 56 | * |
| 57 | * @since 2.6.0 |
| 58 | */ |
| 59 | public static function provider( array $providers ) { |
| 60 | $providers[ static::class ] = static::METHOD_NAME; |
| 61 | |
| 62 | return $providers; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Extracts the selected value from the global settings (if set), and adds it to the output array |
| 67 | * |
| 68 | * @param array $output - The array with output values. |
| 69 | * |
| 70 | * @return array |
| 71 | * |
| 72 | * @since 3.0.0 |
| 73 | */ |
| 74 | public static function return_default_selection( array $output ) { |
| 75 | // No method is enabled, fall back to previous selected one - we don't want to break the logic. |
| 76 | $provider_enabled = Settings_Utils::get_setting_role( null, self::POLICY_SETTINGS_NAME ); |
| 77 | |
| 78 | if ( $provider_enabled ) { |
| 79 | $output[ static::POLICY_SETTINGS_NAME ] = $provider_enabled; |
| 80 | } |
| 81 | |
| 82 | return $output; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the status of the mail method (enabled | disabled) for the current user role |
| 87 | * |
| 88 | * @param string $role - The name of the role to check for. |
| 89 | * |
| 90 | * @since 3.0.0 |
| 91 | * |
| 92 | * @return boolean |
| 93 | */ |
| 94 | public static function is_enabled( ?string $role = null ): bool { |
| 95 | if ( null === static::$enabled || ! isset( static::$enabled[ $role ] ) ) { |
| 96 | static::$enabled[ $role ] = empty( Settings_Utils::get_setting_role( $role, static::POLICY_SETTINGS_NAME ) ) ? false : true; |
| 97 | } |
| 98 | |
| 99 | return static::$enabled[ $role ]; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Checks if given provided is enabled globally (for some of the roles or as a global provider) |
| 104 | * |
| 105 | * @return boolean |
| 106 | * |
| 107 | * @since 3.1.0 |
| 108 | */ |
| 109 | public static function is_globally_enabled() { |
| 110 | if ( empty( self::$globally_enabled ) || ! isset( self::$globally_enabled[ static::METHOD_NAME ] ) ) { |
| 111 | $roles = WP_Helper::get_roles(); |
| 112 | |
| 113 | self::$globally_enabled[ static::METHOD_NAME ] = false; |
| 114 | |
| 115 | if ( static::is_enabled() ) { |
| 116 | // Short circuit - if it is globally enabled - no need to check every role. |
| 117 | self::$globally_enabled[ static::METHOD_NAME ] = true; |
| 118 | } else { |
| 119 | foreach ( $roles as $role ) { |
| 120 | if ( static::is_enabled( $role ) ) { |
| 121 | self::$globally_enabled[ static::METHOD_NAME ] = true; |
| 122 | |
| 123 | // Once is enough - bounce. |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return self::$globally_enabled[ static::METHOD_NAME ]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks value from input array ($_POST) and based on that prevents plugin to show "no method enabled", if the user selected given method |
| 135 | * |
| 136 | * @param boolean $status - Current status of the filter. |
| 137 | * @param array $input - The array with values to check for this method. |
| 138 | * |
| 139 | * @return bool |
| 140 | * |
| 141 | * @since 3.1.0 |
| 142 | */ |
| 143 | public static function method_enabled( bool $status, array $input ) { |
| 144 | if ( isset( $input[ static::POLICY_SETTINGS_NAME ] ) ) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | return $status; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Disable method globally (removes it from all roles and global settings) |
| 153 | * |
| 154 | * @return void |
| 155 | * |
| 156 | * @since 3.1.0 |
| 157 | */ |
| 158 | public static function disable_globally() { |
| 159 | self::$globally_enabled[ static::METHOD_NAME ] = false; |
| 160 | |
| 161 | if ( class_exists( Role_Settings_Controller::class, false ) ) { |
| 162 | |
| 163 | Role_Settings_Controller::remove_roles_setting( 'enable_' . static::METHOD_NAME ); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | $policies = Settings_Utils::get_option( WP_2FA_POLICY_SETTINGS_NAME ); |
| 168 | |
| 169 | if ( isset( $policies[ 'enable_' . static::METHOD_NAME ] ) ) { |
| 170 | $policies[ 'enable_' . static::METHOD_NAME ] = ''; |
| 171 | |
| 172 | \remove_filter( 'sanitize_option_wp_2fa_policy', array( Settings_Page_Policies::class, 'validate_and_sanitize' ) ); |
| 173 | |
| 174 | Settings_Utils::update_option( WP_2FA_POLICY_SETTINGS_NAME, $policies ); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 |