PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.8.0
WP 2FA – Two-factor authentication for WordPress v2.8.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-methods.php 1 year ago class-settings.php 1 year ago index.php 1 year ago
class-methods.php
111 lines
1 <?php
2 /**
3 * Responsible for the plugin methods
4 *
5 * @package wp2fa
6 * @subpackage admin_controllers
7 * @copyright 2024 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 * 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
46 *
47 * @return string[]
48 * @since 2.0.0
49 */
50 public static function get_available_2fa_methods(): array {
51 $available_methods = array();
52
53 /**
54 * Add an option for external providers to implement their own 2fa methods and set them as available.
55 *
56 * @param array $available_methods - The array with all the available methods.
57 *
58 * @since 2.0.0
59 */
60 return \apply_filters( WP_2FA_PREFIX . 'available_2fa_methods', $available_methods );
61 }
62
63 /**
64 * Returns array with all the enabled methods in the plugin for the current role
65 *
66 * @param string $role - Role to extract data for.
67 *
68 * @return array
69 *
70 * @since 2.2.0
71 */
72 public static function get_enabled_methods( $role = 'global' ): array {
73 if ( null === self::$enabled_methods || ! isset( self::$enabled_methods[ $role ] ) ) {
74 self::$enabled_methods[ $role ] = array();
75 $providers = Settings::get_providers();
76
77 foreach ( $providers as $provider ) {
78 if ( Settings::is_provider_enabled_for_role( $role, $provider ) ) {
79 $method = Methods_Helper::get_method_by_provider_name( $provider );
80 if ( $method && \method_exists( $method, 'is_secondary' ) && $method::is_secondary() ) {
81 continue;
82 } elseif ( class_exists( '\WP2FA\Extensions\OutOfBand\Out_Of_Band', false ) && Out_Of_Band::METHOD_NAME === $provider ) {
83 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . $provider . '_email', false, false, $role );
84 } else {
85 self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . $provider, false, false, $role );
86 }
87 }
88 }
89
90 self::$enabled_methods[ $role ] = array_filter( self::$enabled_methods[ $role ] );
91 }
92
93 return self::$enabled_methods;
94 }
95
96 /**
97 * Returns text with the number of methods supported for the given role
98 *
99 * @since 2.2.0
100 *
101 * @return string
102 */
103 public static function get_number_of_methods_text() {
104 return esc_html__(
105 'There are {available_methods_count} methods available to choose from for 2FA:',
106 'wp-2fa'
107 );
108 }
109 }
110 }
111