-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-main-method-wizard-steps.php
319 lines (289 loc) · 11.4 KB
/
class-main-method-wizard-steps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* Responsible for WP2FA user's Email manipulation.
*
* @package wp2fa
* @subpackage methods-wizard
* @since 1.0.0
* @copyright %%YEAR%% Melapress
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*
* @see https://wordpress.org/plugins/wp-2fa/
*/
declare(strict_types=1);
namespace WP2FA\Methods\Wizards;
use WP2FA\Methods\Main_Method;
use WP2FA\Admin\Helpers\WP_Helper;
use WP2FA\Admin\Views\Wizard_Steps;
use WP2FA\Admin\Helpers\User_Helper;
use WP2FA\Admin\Controllers\Settings;
use WP2FA\Admin\Methods\Traits\Methods_Wizards_Trait;
use WP2FA\Extensions\RoleSettings\Role_Settings_Controller;
/**
* Class for handling method codes.
*
* @since 1.0.0
*
* @package WP2FA
*/
if ( ! class_exists( '\WP2FA\Methods\Wizards\Main_Method_Wizard_Steps' ) ) {
/**
* Email code class, for handling method code generation and such.
*
* @since 1.0.0
*/
class Main_Method_Wizard_Steps extends Wizard_Steps {
use Methods_Wizards_Trait;
/**
* Keeps the main class method name, so we can call it when needed.
*
* @var string
*
* @since 1.0.0
*/
private static $main_class = Main_Method::class;
/**
* The default value of the method order in the wizards.
*
* @var integer
*
* @since 1.0.0
*/
private static $order = 6;
/**
* Inits the class hooks
*
* @return void
*
* @since 1.0.0
*/
public static function init() {
\add_filter( WP_2FA_PREFIX . 'methods_modal_options', array( __CLASS__, 'main_method_option' ), 10, 2 );
\add_action( WP_2FA_PREFIX . 'modal_methods', array( __CLASS__, 'main_method_modal_configure' ) );
\add_filter( WP_2FA_PREFIX . 'methods_re_configure', array( __CLASS__, 'main_method_re_configure' ), 10, 2 );
\add_filter( WP_2FA_PREFIX . 'methods_settings', array( __CLASS__, 'main_method_wizard_settings' ), 10, 4 );
\add_action( WP_2FA_PREFIX . 'login_form', array( __CLASS__, 'login_form' ), 10, 2 );
}
/**
* Shows the option for main method reconfiguring (if applicable)
*
* @param array $methods - Array of methods collected.
* @param string $role - The name of the role to show option to.
*
* @since 1.0.0 - Parameter $methods is added, parameter $role (name) is added and array is now returned
*
* @return array
*/
public static function main_method_re_configure( array $methods, string $role ): array {
if ( ! self::$main_class::is_enabled() ) {
return $methods;
}
\ob_start();
?>
<div class="option-pill">
<h3><?php echo \esc_html__( 'Setting up Main Method Example', 'wp-2fa' ); ?></h3>
<p><?php echo \esc_html__( 'This is an intro text for the user', 'wp-2fa' ); ?></p>
<div class="wp2fa-setup-actions">
<a class="button button-primary wp-2fa-button-primary" data-name="next_step_setting_modal_wizard" data-user-id="<?php echo esc_attr( User_Helper::get_user_object()->ID ); ?>" data-next-step="2fa-wizard-<?php echo \esc_attr( self::$main_class::METHOD_NAME ); ?>"><?php esc_html_e( 'Change Main Method Example', 'wp-2fa' ); ?></a>
</div>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
$methods[ self::get_order( $role, $methods ) ] = array(
'name' => self::$main_class::METHOD_NAME,
'output' => $output,
);
return $methods;
}
/**
* Shows the initial method setup options based on enabled methods
*
* @param array $methods - Array of methods collected.
* @param string $role - The name of the role to show option to.
*
* @since 1.0.0 - Parameter $methods is added, parameter $role (name) is added and array is now returned
*
* @return array
*/
public static function main_method_option( array $methods, string $role ): array {
if ( self::$main_class::is_enabled() ) {
\ob_start();
?>
<div class="option-pill">
<label>
<input name="wp_2fa_enabled_methods" type="radio" value="email">
<?php \esc_html__( 'Main method Example', 'wp-2fa' ); ?>
</label>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
$methods[ self::get_order( $role, $methods ) ] = $output;
}
return $methods;
}
/**
* Settings page and first time wizard settings render
*
* @param array $methods - Array with all the methods in which we have to add this one.
* @param boolean $setup_wizard - Is that the first time setup wizard.
* @param string $data_role - Additional HTML data attribute.
* @param mixed $role - Name of the role.
*
* @return array - The array with the methods with all the methods wizard steps.
*
* @since 1.0.0
*/
public static function main_method_wizard_settings( array $methods, bool $setup_wizard, string $data_role, $role = null ) {
$name_prefix = WP_2FA_POLICY_SETTINGS_NAME;
$role_id = '';
if ( null !== $role && '' !== trim( (string) $role ) ) {
$name_prefix .= "[{$role}]";
$data_role = 'data-role="' . $role . '"';
$role_id = '-' . $role;
}
\ob_start();
?>
<div id="<?php echo \esc_attr( self::$main_class::METHOD_NAME ); ?>-method-wrapper" class="method-wrapper">
<?php echo self::hidden_order_setting( $role ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<label for="<?php echo \esc_attr( self::$main_class::METHOD_NAME . $role_id ); ?>" style="margin-bottom: 0 !important;">
<input type="checkbox" id="<?php echo \esc_attr( self::$main_class::METHOD_NAME . $role_id ); ?>" name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo esc_attr( self::$main_class::POLICY_SETTINGS_NAME ); ?>]" value="<?php echo \esc_attr( self::$main_class::POLICY_SETTINGS_NAME ); ?>"
<?php echo $data_role; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php if ( null !== $role && ! empty( $role ) ) { ?>
<?php checked( self::$main_class::POLICY_SETTINGS_NAME, Role_Settings_Controller::get_setting( $role, self::$main_class::POLICY_SETTINGS_NAME ), true ); ?>
<?php
} else {
$use_role_setting = null;
if ( null === $role || '' === trim( (string) $role ) ) {
$use_role_setting = \WP_2FA_PREFIX . 'no-user';
}
$enabled_settings = Settings::get_role_or_default_setting( self::$main_class::POLICY_SETTINGS_NAME, $use_role_setting, $role, true );
?>
<?php checked( $enabled_settings, self::$main_class::POLICY_SETTINGS_NAME ); ?>
<?php } ?>
>
<?php
esc_html_e( 'Example Main Method', 'wp-2fa' );
?>
</label>
<?php
if ( $setup_wizard ) {
echo '<p class="description">' . esc_html__( 'This is an example only.', 'wp-2fa' ) . '</p>';
}
?>
<?php
if ( null !== $role ) {
$enabled_settings = Role_Settings_Controller::get_setting( $role, self::$main_class::POLICY_SETTINGS_NAME );
} else {
$enabled_settings = Settings::get_role_or_default_setting( self::$main_class::POLICY_SETTINGS_NAME, ( ( null !== $role && '' !== $role ) ? '' : false ), $role, true, true );
}
?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
$methods[ self::get_order( $role, $methods ) ] = $output;
return $methods;
}
/**
* Reconfigures method form
*
* @since 1.0.0
*
* @return void
*/
public static function main_method_modal_configure() {
if ( ! self::$main_class::is_enabled() ) {
return;
}
?>
<div class="wizard-step" id="2fa-wizard-<?php echo \esc_attr( self::$main_class::METHOD_NAME ); ?>">
<fieldset>
<div class="step-setting-wrapper active">
<div class="mb-20">
<h3><?php echo \esc_html__( 'Setting up Main Method Example', 'wp-2fa' ); ?></h3>
<p><?php echo \esc_html__( 'This is an intro text for the user', 'wp-2fa' ); ?></p>
</div>
<div class="wp2fa-setup-actions">
<button class="button button-primary wp-2fa-button-primary" data-confirm-main-method-ajax name="next_step_setting_confirm" value="<?php esc_attr_e( 'I\'m Ready', 'wp-2fa' ); ?>" data-user-id="<?php echo esc_attr( User_Helper::get_user_object()->ID ); ?>" <?php echo WP_Helper::create_data_nonce( 'nonce' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> type="button"><?php esc_html_e( 'I\'m Ready', 'wp-2fa' ); ?></button>
<a class="button button-primary wp-2fa-button-primary modal_cancel"><?php esc_attr_e( 'Cancel', 'wp-2fa' ); ?></a>
</div>
</div>
<script>
jQuery( document ).on( 'click', '[data-confirm-main-method-ajax]', function( e ) {
e.preventDefault();
const thisButton = jQuery( this );
let actionToRun = 'confirm_main_method_via_ajax';
let authcode = false;
if( jQuery('#wp-2fa-totp-authcode').length && jQuery('#wp-2fa-totp-authcode').val().length ) {
authcode = true;
}
if (typeof jQuery(this).data('oob-test') !== 'undefined') {
actionToRun = 'validate_oob_authcode_via_ajax';
}
const nonceValue = jQuery( this ).attr( 'data-nonce' );
const currentPageURL = window.location.href;
jQuery.ajax( {
type: 'POST',
dataType: 'json',
url: wp2faData.ajaxURL,
data: {
action: actionToRun,
_wpnonce: nonceValue,
},
complete: function( data ) {
if ( false === data.responseJSON.success ) {
jQuery( thisButton ).parent().find( '.verification-response' ).html( `<span style="color:red">${data.responseJSON.data['error']}</span>` );
}
if ( true === data.responseJSON.success ) {
let nextSubStep = jQuery( '#2fa-wizard-config-backup-codes' );
if ( authcode ) {
if ( jQuery('#2fa-wizard-backup-methods').length ) {
nextSubStep = jQuery( '#2fa-wizard-backup-methods' );
} else if ( jQuery( '#2fa-wizard-email-backup-selected' ).length ) {
nextSubStep = jQuery( '#2fa-wizard-email-backup-selected' );
}
}
jQuery( this ).parent().parent().find( '.active' ).not( '.step-setting-wrapper' ).removeClass( 'active' );
jQuery( '.wizard-step.active' ).removeClass( 'active' );
jQuery( nextSubStep ).addClass( 'active' );
jQuery( document ).on( 'click', '#select-backup-method', function( e ) {
e.preventDefault();
var backupRadio = jQuery("input[name=backup_method_select]:checked");
jQuery( '.wizard-step.active' ).removeClass( 'active' );
jQuery( '#'+backupRadio.data('step') ).addClass( 'active' );
} );
}
}
}, );
}
);
</script>
</fieldset>
</div>
<?php
}
/**
* Shows final step when method is selected. Checks for the provided credentials and logs in/out the user.
*
* @param \WP_User $user - WP_User object of the logged-in user.
* @param string $provider - The name of the provider.
*
* @return void
*
* @since 1.0.0
*/
public static function login_form( $user, $provider ) {
if ( self::$main_class::METHOD_NAME === $provider ) {
?>
<p style="margin-bottom: 3em;"><?php echo \esc_html__( 'Thank you for using the example method to login.', 'wp-2fa' ); ?></p>
<hr style="margin-bottom: 3em;" />
<?php
// Include submit_button function and set the submit button show to true - your method may not need that. Changes based on your needs.
require_once ABSPATH . '/wp-admin/includes/template.php';
\add_filter( WP_2FA_PREFIX . 'login_disable_submit_button', '\__return_false' );
}
}
}
}