class-methods.php
123 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 | use WP2FA\Admin\Controllers\Settings; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 18 | |
| 19 | /** |
| 20 | * Methods class |
| 21 | */ |
| 22 | if ( ! class_exists( '\WP2FA\Admin\Controllers\Methods' ) ) { |
| 23 | |
| 24 | /** |
| 25 | * All the methods related functionality must be extracted from this class. Responsible only for global methods data, not the user method related stuff. |
| 26 | * |
| 27 | * @since 2.2.0 |
| 28 | */ |
| 29 | class Methods { |
| 30 | |
| 31 | /** |
| 32 | * Holds all the enabled methods in the plugin |
| 33 | * |
| 34 | * @var array |
| 35 | * |
| 36 | * @since 2.2.0 |
| 37 | */ |
| 38 | private static $enabled_methods = null; |
| 39 | |
| 40 | /** |
| 41 | * Works our a list of available 2FA methods. It doesn't include the disabled ones. |
| 42 | * |
| 43 | * 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 |
| 44 | * |
| 45 | * @return string[] |
| 46 | * @since 2.0.0 |
| 47 | */ |
| 48 | public static function get_available_2fa_methods(): array { |
| 49 | $available_methods = array(); |
| 50 | |
| 51 | if ( ! empty( Settings::get_role_or_default_setting( 'enable_email', 'current' ) ) ) { |
| 52 | $available_methods[] = 'email'; |
| 53 | } |
| 54 | |
| 55 | if ( ! empty( Settings::get_role_or_default_setting( 'enable_totp', 'current' ) ) ) { |
| 56 | $available_methods[] = 'totp'; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add an option for external providers to implement their own 2fa methods and set them as available. |
| 61 | * |
| 62 | * @param array $available_methods - The array with all the available methods. |
| 63 | * |
| 64 | * @since 2.0.0 |
| 65 | */ |
| 66 | return apply_filters( WP_2FA_PREFIX . 'available_2fa_methods', $available_methods ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns array with all the enabled methods in the plugin for the current role |
| 71 | * |
| 72 | * @param string $role - Role to extract data for. |
| 73 | * |
| 74 | * @return array |
| 75 | * |
| 76 | * @since 2.2.0 |
| 77 | */ |
| 78 | public static function get_enabled_methods( $role = 'global' ): array { |
| 79 | if ( null === self::$enabled_methods || ! isset( self::$enabled_methods[ $role ] ) ) { |
| 80 | self::$enabled_methods[ $role ] = array(); |
| 81 | $providers = Settings::get_providers(); |
| 82 | |
| 83 | foreach ( $providers as $provider ) { |
| 84 | if ( Settings::is_provider_enabled_for_role( $role, $provider ) ) { |
| 85 | if ( 'backup_codes' === $provider ) { |
| 86 | // Backup codes is a secondary provider - ignore it. |
| 87 | continue; |
| 88 | } elseif ( 'backup_email' === $provider ) { |
| 89 | // Backup email codes is a secondary provider - ignore it. |
| 90 | continue; |
| 91 | } elseif ( 'oob' === $provider ) { |
| 92 | self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . $provider . '_email', false, false, $role ); |
| 93 | } else { |
| 94 | self::$enabled_methods[ $role ][ $provider ] = WP2FA::get_wp2fa_setting( 'enable_' . $provider, false, false, $role ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | self::$enabled_methods[ $role ] = array_filter( self::$enabled_methods[ $role ] ); |
| 100 | } |
| 101 | |
| 102 | return self::$enabled_methods; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns text with the number of methods supported for the given role |
| 107 | * |
| 108 | * @param string $role - Role to extract data for. |
| 109 | * |
| 110 | * @since 2.2.0 |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public static function get_number_of_methods_text( $role = 'global' ) { |
| 115 | return esc_html__( |
| 116 | 'There are {available_methods_count} methods available to choose from for 2FA:', |
| 117 | 'wp-2fa' |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | } |
| 123 |