class-login-attempts.php
2 years ago
class-methods-wizards-trait.php
2 years ago
index.php
2 years ago
class-methods-wizards-trait.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the plugin login attempts |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage traits |
| 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\Methods\Traits; |
| 13 | |
| 14 | use WP2FA\Admin\Controllers\Settings; |
| 15 | use WP2FA\Admin\Helpers\Methods_Helper; |
| 16 | use WP2FA\Extensions\RoleSettings\Role_Settings_Controller; |
| 17 | |
| 18 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 19 | /** |
| 20 | * Responsible for the login attempts |
| 21 | * |
| 22 | * @since 2.6.0 |
| 23 | */ |
| 24 | trait Methods_Wizards_Trait { |
| 25 | |
| 26 | /** |
| 27 | * Returns the order in the wizard |
| 28 | * |
| 29 | * @param string $role - The name of the role - could be empty. |
| 30 | * @param array $methods - The array with currently collected methods. |
| 31 | * |
| 32 | * @return integer |
| 33 | * |
| 34 | * @since 2.6.0 |
| 35 | */ |
| 36 | public static function get_order( string $role = null, array $methods = array() ): int { |
| 37 | if ( null !== $role && ! empty( $role ) && class_exists( '\WP2FA\Extensions\RoleSettings\Role_Settings_Controller' ) ) { |
| 38 | $methods_order = Role_Settings_Controller::get_setting( $role, Methods_Helper::POLICY_SETTINGS_NAME ); |
| 39 | |
| 40 | if ( \is_array( $methods_order ) ) { |
| 41 | $methods_order = \array_flip( $methods_order ); |
| 42 | |
| 43 | if ( isset( $methods_order[ self::get_main_class()::METHOD_NAME ] ) ) { |
| 44 | static::$order = (int) $methods_order[ self::get_main_class()::METHOD_NAME ]; |
| 45 | } |
| 46 | } |
| 47 | } else { |
| 48 | $use_role_setting = null; |
| 49 | if ( null === $role || '' === trim( (string) $role ) ) { |
| 50 | $use_role_setting = \WP_2FA_PREFIX . 'no-user'; |
| 51 | } |
| 52 | |
| 53 | $methods_order = Settings::get_role_or_default_setting( Methods_Helper::POLICY_SETTINGS_NAME, $use_role_setting, $role, true ); |
| 54 | |
| 55 | if ( \is_array( $methods_order ) ) { |
| 56 | $methods_order = \array_flip( $methods_order ); |
| 57 | |
| 58 | if ( isset( $methods_order[ self::get_main_class()::METHOD_NAME ] ) ) { |
| 59 | static::$order = (int) $methods_order[ self::get_main_class()::METHOD_NAME ]; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ( isset( $methods[ static::$order ] ) ) { |
| 65 | // Obviously we have a problem here - such order already exists in the methods array, so grab the biggest order and increase it. |
| 66 | // TODO: maybe we need to update the settings for that method as well ? |
| 67 | |
| 68 | static::$order = max( array_keys( $methods ) ); |
| 69 | |
| 70 | ++static::$order; |
| 71 | } |
| 72 | |
| 73 | return static::$order; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the main class of the given wizard steps class. |
| 78 | * |
| 79 | * @return string |
| 80 | * |
| 81 | * @since 2.6.0 |
| 82 | */ |
| 83 | public static function get_main_class(): string { |
| 84 | return static::$main_class; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Creates hidden field for the method order |
| 89 | * |
| 90 | * @param string $role - The name of the role (if present). |
| 91 | * |
| 92 | * @return string |
| 93 | * |
| 94 | * @since 2.6.0 |
| 95 | */ |
| 96 | public static function hidden_order_setting( string $role = null ): string { |
| 97 | $name_prefix = WP_2FA_POLICY_SETTINGS_NAME; |
| 98 | if ( null !== $role && '' !== trim( (string) $role ) ) { |
| 99 | $name_prefix .= "[{$role}]"; |
| 100 | } |
| 101 | $hidden_field = '<input type="hidden" name="' . \esc_attr( $name_prefix ) . '[methods_order][]" value="' . \esc_attr( self::get_main_class()::METHOD_NAME ) . '">'; |
| 102 | |
| 103 | return $hidden_field; |
| 104 | } |
| 105 | } |
| 106 |