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