PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.9.2
WP 2FA – Two-factor authentication for WordPress v2.9.2
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 / Controllers / class-methods.php
wp-2fa / includes / classes / Admin / Controllers Last commit date
class-api-login.php 10 months ago class-endpoints.php 10 months ago class-methods.php 10 months ago class-settings.php 10 months ago index.php 10 months ago
class-methods.php
139 lines
1 <?php
2 /**
3 * Responsible for the plugin methods
4 *
5 * @package wp2fa
6 * @subpackage admin_controllers
7 * @copyright 2025 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\Controllers;
13
14 use WP2FA\WP2FA;
15 use WP2FA\Admin\Controllers\Settings;
16 use WP2FA\Admin\Helpers\Methods_Helper;
17 use WP2FA\Extensions\OutOfBand\Out_Of_Band;
18
19 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
20
21 /**
22 * Methods class
23 */
24 if ( ! class_exists( '\WP2FA\Admin\Controllers\Methods' ) ) {
25
26 /**
27 * All the methods related functionality must be extracted from this class. Responsible only for global methods data, not the user method related stuff.
28 *
29 * @since 2.2.0
30 */
31 class Methods {
32
33 /**
34 * Holds all the enabled methods in the plugin
35 *
36 * @var array
37 *
38 * @since 2.2.0
39 */
40 private static $enabled_methods = null;
41
42 /**
43 * Works our a list of available 2FA methods. It doesn't include the disabled ones.
44 *
45 * @param string $role - The role to get the methods for. Default is 'global'.
46 *
47 * @return string[]
48 *
49 * @since 2.0.0
50 * @since 2.9.0 - Added role parameter so the methods can be filtered base on the user role.
51 */
52 public static function get_available_2fa_methods( string $role = 'global' ): array {
53 $available_methods = array();
54
55 /**
56 * Add an option for external providers to implement their own 2fa methods and set them as available.
57 *
58 * @param array $available_methods - The array with all the available methods.
59 *
60 * @since 2.0.0
61 */
62 return \apply_filters( WP_2FA_PREFIX . 'available_2fa_methods', $available_methods, $role );
63 }
64
65 /**
66 * Returns array with all the enabled methods in the plugin for the current role
67 *
68 * @param string $role - Role to extract data for.
69 *
70 * @return array
71 *
72 * @since 2.2.0
73 */
74 public static function get_enabled_methods( $role = 'global' ): array {
75 $role = \sanitize_text_field( $role );
76
77 if ( null === self::$enabled_methods || ! isset( self::$enabled_methods[ $role ] ) ) {
78 self::$enabled_methods[ $role ] = array();
79 $providers = Settings::get_providers();
80
81 foreach ( $providers as $provider ) {
82 if ( Settings::is_provider_enabled_for_role( $role, $provider ) ) {
83 $method = Methods_Helper::get_method_by_provider_name( $provider );
84 if ( $method && \method_exists( $method, 'is_secondary' ) && $method::is_secondary() ) {
85 continue;
86 } elseif ( class_exists( '\WP2FA\Extensions\OutOfBand\Out_Of_Band', false ) && Out_Of_Band::METHOD_NAME === $provider ) {
87 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . \sanitize_key( $provider ) . '_email', false, false, $role );
88 } else {
89 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . \sanitize_key( $provider ), false, false, $role );
90 }
91 }
92 }
93
94 self::$enabled_methods[ $role ] = array_filter( self::$enabled_methods[ $role ] );
95 }
96
97 return self::$enabled_methods;
98 }
99
100 /**
101 * Checks if given methog is enabled for given role and returns true or false.
102 *
103 * @param string $provider_name - The name of the method.
104 * @param string $role - The name of the role to check for. Default is 'global.
105 *
106 * @return boolean
107 *
108 * @since 2.9.0
109 */
110 public static function is_method_enabled_for_role( string $provider_name, string $role = 'global' ): bool {
111 $role = \sanitize_text_field( $role );
112
113 if ( ! isset( self::$enabled_methods[ $role ] ) ) {
114 self::get_enabled_methods( $role );
115 }
116
117 if ( isset( self::$enabled_methods[ $role ][ $provider_name ] ) && self::$enabled_methods[ $role ][ $provider_name ] ) {
118 return true;
119 }
120
121 return false;
122 }
123
124 /**
125 * Returns text with the number of methods supported for the given role
126 *
127 * @since 2.2.0
128 *
129 * @return string
130 */
131 public static function get_number_of_methods_text() {
132 return esc_html__(
133 'There are {available_methods_count} methods available to choose from for 2FA:',
134 'wp-2fa'
135 );
136 }
137 }
138 }
139