class-login-attempts.php
5 days ago
class-methods-wizards-trait.php
5 days ago
class-provider-trait.php
5 days ago
class-settings-trait.php
5 days ago
class-validation-trait.php
5 days ago
class-white-label-trait.php
5 days ago
index.php
5 days ago
class-methods-wizards-trait.php
230 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the plugin wizard ordering |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage traits |
| 7 | * @copyright 2026 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\WP2FA; |
| 15 | use WP2FA\Utils\Settings_Utils; |
| 16 | use WP2FA\Admin\Helpers\Methods_Helper; |
| 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 | * Inits the class hooks |
| 28 | * |
| 29 | * @return void |
| 30 | * |
| 31 | * @since 2.6.0 |
| 32 | */ |
| 33 | public static function common_init() { |
| 34 | \add_filter( WP_2FA_PREFIX . 'methods_policy_settings_new', array( __CLASS__, 'get_policy_settings' ), 10, 2 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns the order in the wizard |
| 39 | * |
| 40 | * @param string $role - The name of the role - could be empty. |
| 41 | * @param array $methods - The array with currently collected methods. |
| 42 | * |
| 43 | * @return integer |
| 44 | * |
| 45 | * @since 2.6.0 |
| 46 | */ |
| 47 | public static function get_order( ?string $role = null, array $methods = array() ): int { |
| 48 | |
| 49 | $methods_order = Settings_Utils::get_setting_role( $role, Methods_Helper::POLICY_SETTINGS_NAME, true ); |
| 50 | |
| 51 | if ( \is_array( $methods_order ) ) { |
| 52 | $methods_order = \array_flip( $methods_order ); |
| 53 | |
| 54 | if ( isset( $methods_order[ self::get_main_class()::METHOD_NAME ] ) ) { |
| 55 | static::$order = (int) $methods_order[ self::get_main_class()::METHOD_NAME ]; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if ( isset( $methods[ static::$order ] ) ) { |
| 60 | // Obviously we have a problem here - such order already exists in the methods array, so grab the biggest order and increase it. |
| 61 | // TODO: maybe we need to update the settings for that method as well ? |
| 62 | |
| 63 | static::$order = max( array_keys( $methods ) ); |
| 64 | |
| 65 | ++static::$order; |
| 66 | } |
| 67 | |
| 68 | return static::$order; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the main class of the given wizard steps class. |
| 73 | * |
| 74 | * @return string |
| 75 | * |
| 76 | * @since 2.6.0 |
| 77 | */ |
| 78 | public static function get_main_class(): string { |
| 79 | return static::$main_class; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates hidden field for the method order |
| 84 | * |
| 85 | * @param string $role - The name of the role (if present). |
| 86 | * |
| 87 | * @return string |
| 88 | * |
| 89 | * @since 2.6.0 |
| 90 | */ |
| 91 | public static function hidden_order_setting( ?string $role = null ): string { |
| 92 | $name_prefix = WP_2FA_POLICY_SETTINGS_NAME; |
| 93 | if ( null !== $role && '' !== trim( (string) $role ) ) { |
| 94 | $name_prefix .= "[{$role}]"; |
| 95 | } |
| 96 | $hidden_field = '<input type="hidden" name="' . \esc_attr( $name_prefix ) . '[methods_order][]" value="' . \esc_attr( self::get_main_class()::METHOD_NAME ) . '">'; |
| 97 | |
| 98 | return $hidden_field; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the policy settings for the method. |
| 103 | * |
| 104 | * @param array $settings - The current array with the settings. |
| 105 | * @param null|string $role - The name of the role (if present). |
| 106 | * |
| 107 | * @return array |
| 108 | * |
| 109 | * @since 2.6.0 |
| 110 | */ |
| 111 | public static function get_policy_settings( array $settings, $role = null ) { |
| 112 | |
| 113 | $method_settings = array( |
| 114 | 'id' => self::get_main_class()::METHOD_NAME, |
| 115 | 'title' => \esc_html( WP2FA::get_wp2fa_white_label_setting( self::get_main_class()::METHOD_INTERNAL_NAME . '-option-label', true ) ), |
| 116 | 'enabled' => self::get_main_class()::is_enabled( $role ), |
| 117 | 'setting' => self::get_main_class()::POLICY_SETTINGS_NAME, |
| 118 | ); |
| 119 | |
| 120 | $extra_settings = static::get_extra_policy_settings_html( $role ); |
| 121 | if ( ! empty( $extra_settings ) ) { |
| 122 | $method_settings['extra_settings'] = $extra_settings; |
| 123 | } |
| 124 | |
| 125 | $integration_settings = static::get_integration_settings_html( $role ); |
| 126 | if ( ! empty( $integration_settings ) ) { |
| 127 | $method_settings['integration_settings'] = $integration_settings; |
| 128 | } |
| 129 | |
| 130 | $description = static::get_method_description(); |
| 131 | if ( ! empty( $description ) ) { |
| 132 | $method_settings['description'] = $description; |
| 133 | } |
| 134 | |
| 135 | $title_hint = static::get_method_title_hint(); |
| 136 | if ( ! empty( $title_hint ) ) { |
| 137 | $method_settings['title_hint'] = $title_hint; |
| 138 | } |
| 139 | |
| 140 | // Check if the provider requires external setup and whether it is configured. |
| 141 | $setup_url = static::get_setup_url(); |
| 142 | if ( ! empty( $setup_url ) ) { |
| 143 | $main_class = self::get_main_class(); |
| 144 | $method_settings['is_configured'] = \method_exists( $main_class, 'is_set' ) && $main_class::is_set(); |
| 145 | $method_settings['setup_url'] = $setup_url; |
| 146 | } |
| 147 | |
| 148 | $settings[ self::get_main_class()::METHOD_NAME ] = $method_settings; |
| 149 | |
| 150 | return $settings; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns HTML for extra policy settings displayed beneath the method |
| 155 | * checkbox in the sortable list. |
| 156 | * |
| 157 | * Override in individual wizard-steps classes to provide method-specific |
| 158 | * settings (e.g. code validity period, allow custom email). |
| 159 | * |
| 160 | * @param null|string $role - The name of the role (if present). |
| 161 | * |
| 162 | * @return string Empty string when no extra settings exist. |
| 163 | * |
| 164 | * @since 3.2.0 |
| 165 | */ |
| 166 | public static function get_extra_policy_settings_html( $role = null ): string { |
| 167 | return ''; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the URL to the provider setup page. |
| 172 | * |
| 173 | * Override in individual wizard-steps classes for methods that require |
| 174 | * external configuration (e.g. Authy, Twilio, Clickatell, Yubico). |
| 175 | * |
| 176 | * @return string Empty string when no setup is required. |
| 177 | * |
| 178 | * @since 3.2.0 |
| 179 | */ |
| 180 | public static function get_setup_url(): string { |
| 181 | return ''; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns HTML for integration settings displayed in a collapsible section |
| 186 | * beneath the method card in the policy settings. |
| 187 | * |
| 188 | * Override in individual wizard-steps classes for methods that have |
| 189 | * integration credentials (e.g. Twilio Account SID, Auth token). |
| 190 | * |
| 191 | * @param null|string $role - The name of the role (if present). |
| 192 | * |
| 193 | * @return string Empty string when no integration settings exist. |
| 194 | * |
| 195 | * @since 3.2.1 |
| 196 | */ |
| 197 | public static function get_integration_settings_html( $role = null ): string { |
| 198 | return ''; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns a description string for the method, shown below the title |
| 203 | * in the policy method card. May contain HTML links. |
| 204 | * |
| 205 | * Override in individual wizard-steps classes to provide a method-specific |
| 206 | * description with documentation links. |
| 207 | * |
| 208 | * @return string Empty string when no description is needed. |
| 209 | * |
| 210 | * @since 3.2.1 |
| 211 | */ |
| 212 | public static function get_method_description(): string { |
| 213 | return ''; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns an inline hint shown next to the method title in the sortable list. |
| 218 | * May contain HTML links. |
| 219 | * |
| 220 | * Override in individual wizard-steps classes to provide method-specific hints. |
| 221 | * |
| 222 | * @return string Empty string when no hint is needed. |
| 223 | * |
| 224 | * @since 4.0.0 |
| 225 | */ |
| 226 | public static function get_method_title_hint(): string { |
| 227 | return ''; |
| 228 | } |
| 229 | } |
| 230 |