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