PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.9.2
WP 2FA – Two-factor authentication for WordPress v2.9.2
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 / Helpers / class-methods-helper.php
wp-2fa / includes / classes / Admin / Helpers Last commit date
class-ajax-helper.php 10 months ago class-classes-helper.php 10 months ago class-file-writer.php 10 months ago class-methods-helper.php 10 months ago class-php-helper.php 10 months ago class-user-helper.php 10 months ago class-wp-helper.php 10 months ago index.php 10 months ago
class-methods-helper.php
223 lines
1 <?php
2 /**
3 * Responsible for the User's operations
4 *
5 * @package wp2fa
6 * @subpackage helpers
7 * @since 2.6.0
8 * @copyright 2025 Melapress
9 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
10 * @link https://wordpress.org/plugins/wp-2fa/
11 */
12
13 namespace WP2FA\Admin\Helpers;
14
15 use WP2FA\Admin\Helpers\Classes_Helper;
16
17 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
18
19 /**
20 * User's settings class
21 */
22 if ( ! class_exists( '\WP2FA\Admin\Helpers\Methods_Helper' ) ) {
23
24 /**
25 * All the user related settings must go trough this class.
26 *
27 * @since 2.6.0
28 */
29 class Methods_Helper {
30 const METHODS_NAMESPACE = '\WP2FA\Methods';
31
32 const POLICY_SETTINGS_NAME = 'methods_order';
33
34 /**
35 * Cached methods array
36 *
37 * @var array
38 *
39 * @since 2.7.0
40 */
41 public static $methods = array();
42
43 /**
44 * Inits the class and initializes all the methods
45 *
46 * @return void
47 *
48 * @since 2.6.0
49 */
50 public static function init() {
51
52 foreach ( self::get_methods() as $method ) {
53 if ( method_exists( $method, 'init' ) ) {
54 call_user_func_array( array( $method, 'init' ), array() );
55 }
56 }
57
58 \add_action( WP_2FA_PREFIX . 'methods_setup', array( __CLASS__, 'methods_settings' ), 10, 3 );
59 \add_filter( WP_2FA_PREFIX . 'filter_output_content', array( __CLASS__, 'settings_store' ), 10, 2 );
60 \add_action( WP_2FA_PREFIX . 'methods_options', array( __CLASS__, 'methods_options' ) );
61 \add_action( WP_2FA_PREFIX . 'methods_reconfigure_options', array( __CLASS__, 'methods_re_configure' ) );
62 }
63
64 /**
65 * Sets the methods in correct order for re-configuring. Checks the selected method for the user and puts it on top of the list
66 *
67 * @return void
68 *
69 * @since 2.6.0
70 */
71 public static function methods_re_configure() {
72 $role = User_Helper::get_user_role();
73
74 /**
75 * Option to re-configure the methods - all the methods are called and their order and code is collected. Then the currently selected method is positioned on top and methods are shown in order. That is called in the user profile page.
76 *
77 * @param array - All the collected methods and their order.
78 * @param string $role - The role of the current user
79 *
80 * @since 2.6.0
81 */
82 $methods = \apply_filters( WP_2FA_PREFIX . 'methods_re_configure', array(), $role );
83
84 $enabled_method = User_Helper::get_enabled_method_for_user();
85
86 foreach ( $methods as $order => $method ) {
87 if ( $enabled_method === $method['name'] ) {
88 $methods[-1] = $method;
89 unset( $methods[ $order ] );
90
91 break;
92 }
93 }
94
95 ksort( $methods );
96
97 foreach ( $methods as $method ) {
98 echo $method['output']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
99 }
100 }
101
102 /**
103 * Collects the methods options and shows them in order
104 *
105 * @return void
106 *
107 * @since 2.6.0
108 */
109 public static function methods_options() {
110 $role = User_Helper::get_user_role();
111
112 /**
113 * Shows methods in order. Every method is called and its code and order is collected. That is used when there are no methods selected from the user.
114 *
115 * @param array - All the collected methods and their order.
116 * @param string $role - The role of the current user
117 *
118 * @since 2.6.0
119 */
120 $methods = \apply_filters( WP_2FA_PREFIX . 'methods_modal_options', array(), $role );
121
122 ksort( $methods );
123
124 foreach ( $methods as $method ) {
125 echo $method; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
126 }
127 }
128
129 /**
130 * Settings page and first time wizard settings render
131 *
132 * @param boolean $setup_wizard - Is that the first time setup wizard.
133 * @param string $data_role - Additional HTML data attribute.
134 * @param mixed $role - Name of the role.
135 *
136 * @return void
137 *
138 * @since 2.6.0
139 */
140 public static function methods_settings( bool $setup_wizard, string $data_role, $role = null ) {
141
142 /**
143 * Shows methods in order. Every method is called and its code and order is collected. Used in the wizards.
144 *
145 * @param array - All the collected methods and their order.
146 * @param bool - Is that a setup wizard call or not?
147 * @param string - Additional HTML data attribute.
148 * @param string $role - The role, that is when global settings of the plugin are selected.
149 *
150 * @since 2.6.0
151 */
152 $methods = \apply_filters( WP_2FA_PREFIX . 'methods_settings', array(), $setup_wizard, $data_role, $role );
153
154 ksort( $methods );
155
156 foreach ( $methods as $method ) {
157 echo $method; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
158 }
159 }
160
161 /**
162 * Adds and filters extension values in the settings store array ($output).
163 *
164 * @param array $output - Array with the currently stored settings.
165 * @param array $input - Array with the input ($_POST) values.
166 *
167 * @return array
168 *
169 * @since 2.6.0
170 */
171 public static function settings_store( array $output, array $input ) {
172 if ( isset( $input[ self::POLICY_SETTINGS_NAME ] ) && \is_array( $input[ self::POLICY_SETTINGS_NAME ] ) ) {
173 foreach ( $input[ self::POLICY_SETTINGS_NAME ] as $order => $method ) {
174 $output[ self::POLICY_SETTINGS_NAME ][ $order ] = $method;
175 }
176 }
177
178 return $output;
179 }
180
181 /**
182 * Returns the method by its slug.
183 *
184 * @param string $provider_name - The slug to search for.
185 *
186 * @return bool|\WP2FA\Methods
187 *
188 * @since 2.7.0
189 */
190 public static function get_method_by_provider_name( string $provider_name ) {
191 foreach ( self::get_methods() as $method ) {
192 if ( $provider_name === $method::METHOD_NAME ) {
193 return $method;
194 }
195 }
196
197 return \false;
198 }
199
200 /**
201 * Returns all of the registered methods.
202 *
203 * @return array
204 *
205 * @since 2.7.0
206 */
207 private static function get_methods(): array {
208 if ( empty( self::$methods ) ) {
209 /**
210 * Gives the ability to add classes to the Class_Helper array.
211 *
212 * @since 2.7.0
213 */
214 \do_action( WP_2FA_PREFIX . 'add_to_class_map' );
215
216 self::$methods = Classes_Helper::get_classes_by_namespace( self::METHODS_NAMESPACE );
217 }
218
219 return self::$methods;
220 }
221 }
222 }
223