PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.2.1
WP 2FA – Two-factor authentication for WordPress v2.2.1
4.1.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 / Controllers / class-methods.php
wp-2fa / includes / classes / Admin / Controllers Last commit date
class-login-attempts.php 4 years ago class-methods.php 4 years ago class-settings.php 4 years ago
class-methods.php
132 lines
1 <?php
2 /**
3 * Responsible for the plugin methods
4 *
5 * @package wp2fa
6 * @subpackage admin_controllers
7 * @copyright 2021 WP White Security
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
16 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
17
18 /**
19 * Methods class
20 */
21 if ( ! class_exists( '\WP2FA\Admin\Controllers\Methods' ) ) {
22
23 /**
24 * All the methods related functionality must be extracted from this class. Responsible only for global methods data, not the user method related stuff.
25 *
26 * @since 2.2.0
27 */
28 class Methods {
29
30 /**
31 * Holds all the enabled methods in the plugin
32 *
33 * @var array
34 *
35 * @since 2.2.0
36 */
37 private static $enabled_methods = null;
38
39 /**
40 * Works our a list of available 2FA methods. It doesn't include the disabled ones.
41 *
42 * TODO: There is a high possibility that this method is duplication of the Settings::get_providers - check and make the changes as there must be only one way to extract that info
43 *
44 * @return string[]
45 * @since 2.0.0
46 */
47 public static function get_available_2fa_methods(): array {
48 $available_methods = array();
49
50 if ( ! empty( Settings::get_role_or_default_setting( 'enable_email', 'current' ) ) ) {
51 $available_methods[] = 'email';
52 }
53
54 if ( ! empty( Settings::get_role_or_default_setting( 'enable_totp', 'current' ) ) ) {
55 $available_methods[] = 'totp';
56 }
57
58 /**
59 * Add an option for external providers to implement their own 2fa methods and set them as available.
60 *
61 * @param array $available_methods - The array with all the available methods.
62 *
63 * @since 2.0.0
64 */
65 return apply_filters( WP_2FA_PREFIX . 'available_2fa_methods', $available_methods );
66 }
67
68 /**
69 * Returns array with all the enabled methods in the plugin for the current role
70 *
71 * @param string $role - Role to extract data for.
72 *
73 * @return array
74 *
75 * @since 2.2.0
76 */
77 public static function get_enabled_methods( $role = 'global' ): array {
78 if ( null === self::$enabled_methods || ! isset( self::$enabled_methods[ $role ] ) ) {
79 self::$enabled_methods[ $role ] = array();
80 $providers = Settings::get_providers();
81
82 foreach ( $providers as $provider ) {
83 if ( 'backup_codes' === $provider ) {
84 // Backup codes is a secondary provider - ignore it.
85 continue;
86 } elseif ( 'email-backup' === $provider ) {
87 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable-' . $provider, false, false, $role );
88 } elseif ( 'oob' === $provider ) {
89 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . $provider . '_email', false, false, $role );
90 } else {
91 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . $provider, false, false, $role );
92 }
93 }
94
95 self::$enabled_methods[ $role ] = array_filter( self::$enabled_methods[ $role ] );
96 }
97
98 return self::$enabled_methods;
99 }
100
101 /**
102 * Returns text with the number of methods supported for the given role
103 *
104 * @param string $role - Role to extract data for.
105 *
106 * @since 2.2.0
107 *
108 * @return string
109 */
110 public static function get_number_of_methods_text( $role = 'global' ) {
111 $methods_count = count( self::get_enabled_methods( $role )[ $role ] );
112
113 if ( \class_exists( 'NumberFormatter' ) ) {
114 $number_formatter = new \NumberFormatter( get_locale(), \NumberFormatter::SPELLOUT );
115 $methods_count = $number_formatter->format( $methods_count );
116 }
117
118 return sprintf(
119 // translators: %s - the number of methods.
120 \_n(
121 'There is %s method available from which you can choose for 2FA:',
122 'There are %s methods available from which you can choose for 2FA:',
123 $methods_count,
124 'wp-2fa'
125 ),
126 $methods_count
127 );
128 }
129
130 }
131 }
132