class-ajax-helper.php
2 years ago
class-classes-helper.php
2 years ago
class-file-writer.php
2 years ago
class-methods-helper.php
2 years ago
class-php-helper.php
2 years ago
class-user-helper.php
2 years ago
class-wp-helper.php
2 years ago
index.php
2 years ago
class-methods-helper.php
173 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 %%YEAR%% 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 | * Inits the class and initializes all the methods |
| 36 | * |
| 37 | * @return void |
| 38 | * |
| 39 | * @since 2.6.0 |
| 40 | */ |
| 41 | public static function init() { |
| 42 | $methods = Classes_Helper::get_classes_by_namespace( self::METHODS_NAMESPACE ); |
| 43 | foreach ( $methods as $method ) { |
| 44 | if ( method_exists( $method, 'init' ) ) { |
| 45 | call_user_func_array( array( $method, 'init' ), array() ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | \add_action( WP_2FA_PREFIX . 'methods_setup', array( __CLASS__, 'methods_settings' ), 10, 3 ); |
| 50 | \add_filter( WP_2FA_PREFIX . 'filter_output_content', array( __CLASS__, 'settings_store' ), 10, 2 ); |
| 51 | \add_action( WP_2FA_PREFIX . 'methods_options', array( __CLASS__, 'methods_options' ) ); |
| 52 | \add_action( WP_2FA_PREFIX . 'methods_reconfigure_options', array( __CLASS__, 'methods_re_configure' ) ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the methods in correct order for re-configuring. Checks the selected method for the user and puts it on top of the list |
| 57 | * |
| 58 | * @return void |
| 59 | * |
| 60 | * @since 2.6.0 |
| 61 | */ |
| 62 | public static function methods_re_configure() { |
| 63 | $role = User_Helper::get_user_role(); |
| 64 | |
| 65 | /** |
| 66 | * 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. |
| 67 | * |
| 68 | * @param array - All the collected methods and their order. |
| 69 | * @param string $role - The role of the current user |
| 70 | * |
| 71 | * @since 2.6.0 |
| 72 | */ |
| 73 | $methods = \apply_filters( WP_2FA_PREFIX . 'methods_re_configure', array(), $role ); |
| 74 | |
| 75 | $enabled_method = User_Helper::get_enabled_method_for_user(); |
| 76 | |
| 77 | foreach ( $methods as $order => $method ) { |
| 78 | if ( $enabled_method === $method['name'] ) { |
| 79 | $methods[-1] = $method; |
| 80 | unset( $methods[ $order ] ); |
| 81 | |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | ksort( $methods ); |
| 87 | |
| 88 | foreach ( $methods as $method ) { |
| 89 | echo $method['output']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Collects the methods options and shows them in order |
| 95 | * |
| 96 | * @return void |
| 97 | * |
| 98 | * @since 2.6.0 |
| 99 | */ |
| 100 | public static function methods_options() { |
| 101 | $role = User_Helper::get_user_role(); |
| 102 | |
| 103 | /** |
| 104 | * 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. |
| 105 | * |
| 106 | * @param array - All the collected methods and their order. |
| 107 | * @param string $role - The role of the current user |
| 108 | * |
| 109 | * @since 2.6.0 |
| 110 | */ |
| 111 | $methods = \apply_filters( WP_2FA_PREFIX . 'methods_modal_options', array(), $role ); |
| 112 | |
| 113 | ksort( $methods ); |
| 114 | |
| 115 | foreach ( $methods as $method ) { |
| 116 | echo $method; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Settings page and first time wizard settings render |
| 122 | * |
| 123 | * @param boolean $setup_wizard - Is that the first time setup wizard. |
| 124 | * @param string $data_role - Additional HTML data attribute. |
| 125 | * @param mixed $role - Name of the role. |
| 126 | * |
| 127 | * @return void |
| 128 | * |
| 129 | * @since 2.6.0 |
| 130 | */ |
| 131 | public static function methods_settings( bool $setup_wizard, string $data_role, $role = null ) { |
| 132 | |
| 133 | /** |
| 134 | * Shows methods in order. Every method is called and its code and order is collected. Used in the wizards. |
| 135 | * |
| 136 | * @param array - All the collected methods and their order. |
| 137 | * @param bool - Is that a setup wizard call or not? |
| 138 | * @param string - Additional HTML data attribute. |
| 139 | * @param string $role - The role, that is when global settings of the plugin are selected. |
| 140 | * |
| 141 | * @since 2.6.0 |
| 142 | */ |
| 143 | $methods = \apply_filters( WP_2FA_PREFIX . 'methods_settings', array(), $setup_wizard, $data_role, $role ); |
| 144 | |
| 145 | ksort( $methods ); |
| 146 | |
| 147 | foreach ( $methods as $method ) { |
| 148 | echo $method; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Adds and filters extension values in the settings store array ($output). |
| 154 | * |
| 155 | * @param array $output - Array with the currently stored settings. |
| 156 | * @param array $input - Array with the input ($_POST) values. |
| 157 | * |
| 158 | * @return array |
| 159 | * |
| 160 | * @since 2.6.0 |
| 161 | */ |
| 162 | public static function settings_store( array $output, array $input ) { |
| 163 | if ( isset( $input[ self::POLICY_SETTINGS_NAME ] ) && \is_array( $input[ self::POLICY_SETTINGS_NAME ] ) ) { |
| 164 | foreach ( $input[ self::POLICY_SETTINGS_NAME ] as $order => $method ) { |
| 165 | $output[ self::POLICY_SETTINGS_NAME ][ $order ] = $method; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return $output; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 |