-
Notifications
You must be signed in to change notification settings - Fork 2
/
B3Onboarding.php
1406 lines (1165 loc) · 72 KB
/
B3Onboarding.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: B3 OnBoarding
Plugin URI: https://b3onboarding.berryplasman.com
Description: This plugin styles the default WordPress pages into your own design. It gives you full control over the registration/login process (aka onboarding).
Version: 3.13.0
Requires at least: 4.3
Tested up to: 6.6.2
Requires PHP: 5.6
Author: Beee
Author URI: https://berryplasman.com
Tags: user, management, registration, login, lost password, reset password, account, multisite, wpml, multilang, onboarding, onboard, user registration, user management, forms, email, override, otp, one time password, magic link
License: GPL v2 (or later)
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Network: true
___ ____ ____ ____
/ _ )/ __/ __/ __/
/ _ / _/ _/ _/
/____/___/____/____/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'B3Onboarding' ) ) {
/**
* Class B3Onboarding
*/
class B3Onboarding {
/**
* Construct
*/
private array $settings = [];
function __construct() {
if ( ! defined( 'B3OB_PLUGIN_URL' ) ) {
$plugin_url = plugins_url( '/', __FILE__ );
define( 'B3OB_PLUGIN_URL', $plugin_url );
}
if ( ! defined( 'B3OB_PLUGIN_PATH' ) ) {
$plugin_path = dirname( __FILE__ );
define( 'B3OB_PLUGIN_PATH', $plugin_path );
}
if ( ! defined( 'B3OB_PLUGIN_SETTINGS' ) ) {
$settings_url = admin_url( 'admin.php?page=b3-onboarding' );
define( 'B3OB_PLUGIN_SETTINGS', $settings_url );
}
if ( ! defined( 'B3OB_PLUGIN_SITE' ) ) {
$plugin_site = 'https://b3onboarding.berryplasman.com';
define( 'B3OB_PLUGIN_SITE', $plugin_site );
}
}
/**
* This initializes the whole shabang
*/
public function init() {
$this->settings = [
'path' => trailingslashit( dirname( __FILE__ ) ),
'registration_type' => get_option( 'b3_registration_type' ),
'version' => get_option( 'b3ob_version' ),
];
// actions
register_activation_hook( __FILE__, [ $this, 'b3_plugin_activation' ] );
register_deactivation_hook( __FILE__, [ $this, 'b3_plugin_deactivation' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'b3_enqueue_scripts_frontend' ], 40 );
add_action( 'wp_enqueue_scripts', [ $this, 'b3_add_recaptcha_js_to_footer' ] );
add_action( 'login_enqueue_scripts', [ $this, 'b3_add_recaptcha_js_to_footer' ] );
add_action( 'wp_head', [ $this, 'b3_add_rc3' ] );
add_action( 'admin_init', [ $this, 'b3_set_version' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'b3_enqueue_scripts_backend' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'b3_enqueue_scripts_backend_footer' ], 99 );
add_action( 'admin_menu', [ $this, 'b3_add_admin_pages' ] );
add_action( 'template_redirect', [ $this, 'b3_template_redirect' ] );
add_action( 'widgets_init', [ $this, 'b3_register_widgets' ] );
add_action( 'wp_dashboard_setup', [ $this, 'b3_add_dashboard_widget' ] );
add_action( 'init', [ $this, 'b3_load_plugin_text_domain' ] );
add_action( 'init', [ $this, 'b3_registration_form_handling' ] );
add_action( 'init', [ $this, 'b3_reset_user_password' ] );
add_action( 'init', [ $this, 'b3_magic_link_form_handling' ] );
add_action( 'init', [ $this, 'b3_check_magic_link' ] );
add_action( 'admin_notices', [ $this, 'b3_admin_notices' ] );
add_action( 'load-users.php', [ $this, 'b3_load_users_page' ] );
if ( is_multisite() ) {
add_action( 'wp_initialize_site', [ $this, 'b3_after_create_site' ] );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ $this, 'b3_settings_link' ] );
include 'includes/constants.php';
include 'includes/true-false.php';
include 'includes/actions.php';
include 'includes/class-b3-shortcodes.php';
include 'includes/do-stuff.php';
include 'includes/filters.php';
include 'includes/functions.php';
include 'includes/defaults.php';
include 'includes/emails.php';
include 'includes/redirects.php';
include 'includes/form-handling.php';
include 'includes/tabs/tabs.php';
include 'admin/help-tabs.php';
}
/*
* Do stuff upon plugin activation
*
* @since 2.0.0
*/
public function b3_plugin_activation() {
b3_setup_initial_pages();
b3_set_default_settings();
if ( ! is_multisite() ) {
$b3_activation = get_role( 'b3_activation' );
if ( ! $b3_activation ) {
add_role( 'b3_activation', esc_html__( 'Awaiting activation', 'b3-onboarding' ), [] );
}
$b3_approval = get_role( 'b3_approval' );
if ( ! $b3_approval ) {
add_role( 'b3_approval', esc_html__( 'Awaiting approval', 'b3-onboarding' ), [] );
}
}
}
/**
* Do stuff upon plugin deactivation
*/
public function b3_plugin_deactivation() {
// set registration option accordingly
$registration_type = get_option( 'b3_registration_type' );
if ( is_multisite() ) {
if ( is_main_site() ) {
if ( 'none' === $registration_type ) {
update_site_option( 'registration', 'none' );
} else {
update_site_option( 'registration', 'all' );
}
}
} else {
if ( 'none' === $registration_type ) {
update_option( 'users_can_register', '0' );
} else {
update_option( 'users_can_register', '1' );
}
}
delete_option( 'b3ob_version' );
}
/**
* Set version
*/
public function b3_set_version() {
$stored = get_option( 'b3ob_version' );
$plugin_data = get_plugin_data( trailingslashit( dirname( __FILE__ ) ) . basename( __FILE__ ) );
if ( $stored !== $plugin_data[ 'Version' ] ) {
update_option( 'b3ob_version', $plugin_data[ 'Version' ] );
}
}
/**
* Load plugin text domain
*/
public function b3_load_plugin_text_domain() {
$plugin_folder = dirname( plugin_basename( __FILE__ ) );
$locale = apply_filters( 'plugin_locale', get_locale(), $plugin_folder );
load_textdomain( $plugin_folder, trailingslashit( WP_LANG_DIR ) . $plugin_folder . '/' . $plugin_folder . '-' . $locale . '.mo' );
load_plugin_textdomain( $plugin_folder, false, $plugin_folder . '/languages/' );
}
/*
* Enqueue scripts front-end
*/
public function b3_enqueue_scripts_frontend() {
if ( ! is_admin() ) {
if ( ! wp_script_is( 'jquery' ) ) {
wp_enqueue_script( 'jquery' );
}
}
if ( false != get_option( 'b3_use_popup', false ) ) {
wp_enqueue_script( 'modal', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js', [ 'jquery' ], '0.9.1' );
wp_enqueue_style( 'modal', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css', false, '0.9.1' );
}
wp_enqueue_style( 'b3ob-main', plugins_url( 'assets/css/style.css', __FILE__ ), [], $this->settings[ 'version' ] );
wp_enqueue_script( 'b3ob', plugins_url( 'assets/js/js.js', __FILE__ ), [ 'jquery' ], $this->settings[ 'version' ] );
wp_localize_script( 'b3ob', 'b3ob_vars', [
'recaptcha_theme' => get_option( 'b3_recaptcha_theme', 'light' ),
] );
}
/*
* Enqueue scripts in backend
*/
public function b3_enqueue_scripts_backend() {
wp_enqueue_style( 'b3ob-admin', plugins_url( 'assets/css/admin.css', __FILE__ ), [], $this->settings[ 'version' ] );
if ( ! ( 'toplevel_page_b3-onboarding' === get_current_screen()->id ) ) {
return;
}
wp_enqueue_script( 'b3ob-admin', plugins_url( 'assets/js/admin.js', __FILE__ ), [ 'jquery' ], $this->settings[ 'version' ] );
// https://wpreset.com/add-codemirror-editor-plugin-theme/
$b3cm_settings[ 'codeEditor' ] = wp_enqueue_code_editor( [
'type' => 'text/css',
] );
wp_localize_script( 'jquery', 'b3cm_settings', $b3cm_settings );
wp_enqueue_style( 'wp-codemirror' );
// @src https://github.com/thomasgriffin/New-Media-Image-Uploader
// This function loads in the required media files for the media manager.
wp_enqueue_media();
// Register, localize and enqueue our custom JS.
wp_register_script( 'b3-media', plugins_url( '/assets/js/media.js', __FILE__ ), [ 'jquery' ], $this->settings[ 'version' ], true );
wp_localize_script( 'b3-media', 'b3_media', [
'title' => esc_attr__( 'Upload or choose your custom logo', 'b3-onboarding' ),
'button' => esc_attr__( 'Insert logo', 'b3-onboarding' ),
] );
wp_enqueue_script( 'b3-media' );
}
public function b3_enqueue_scripts_backend_footer() {
wp_enqueue_script( 'wp-theme-plugin-editor', '', '', '', true );
}
/*
* Adds a page to admin sidebar menu
*/
public function b3_add_admin_pages() {
include 'admin/admin-page.php';
add_menu_page( 'B3 OnBoarding', 'B3 OnBoarding', apply_filters( 'b3_user_cap', 'manage_options' ), 'b3-onboarding', 'b3_user_register_settings', B3OB_PLUGIN_URL . 'assets/images/logo-b3onboarding-small.png', 99 );
if ( in_array( get_option( 'b3_registration_type' ), [ 'request_access' ] ) || is_multisite() && get_option( 'b3_needs_admin_approval' ) ) {
include 'admin/user-approval-page.php';
add_submenu_page( 'b3-onboarding', 'B3 OnBoarding - ' . esc_html__( 'User Approval', 'b3-onboarding' ), esc_html__( 'User Approval', 'b3-onboarding' ), apply_filters( 'b3_user_cap', 'manage_options' ), 'b3-user-approval', 'b3_user_approval' );
}
if ( is_localhost() || get_option( 'b3_debug_info' ) ) {
include 'admin/debug-page.php';
add_submenu_page( 'b3-onboarding', 'B3 OnBoarding - ' . esc_html__( 'Debug info', 'b3-onboarding' ), esc_html__( 'Debug info', 'b3-onboarding' ), apply_filters( 'b3_user_cap', 'manage_options' ), 'b3-debug', 'b3_debug_page' );
}
}
/**
* Redirect user away from certain pages
*/
public function b3_template_redirect() {
$account_page_id = b3_get_account_url( true );
$account_url = b3_get_account_url();
$approval_page_id = b3_get_user_approval_link( true );
$current_url = b3_get_current_url();
$login_page_id = b3_get_login_url( true );
$login_url = ( false != $login_page_id ) ? get_the_permalink( $login_page_id ) : wp_login_url();
$logout_page_id = b3_get_logout_url( true );
if ( is_page() ) {
$current_page = get_post( get_the_ID() );
if ( false != $account_page_id ) {
if ( ! is_user_logged_in() && ( $account_page_id == $current_page->ID || $account_page_id == $current_page->post_parent ) ) {
$login_url = add_query_arg( 'redirect_to', urlencode( $current_url ), $login_url );
$redirect_url = $login_url;
}
}
if ( false != $approval_page_id && $current_page->ID == $approval_page_id ) {
if ( is_user_logged_in() ) {
if ( ! current_user_can( 'promote_users' ) ) {
$redirect_url = $account_url;
}
} else {
$login_url = add_query_arg( 'redirect_to', urlencode( $current_url ), $login_url );
$redirect_url = $login_url;
}
}
if ( false != $logout_page_id && $current_page->ID == $logout_page_id ) {
check_admin_referer( 'logout' );
$user = wp_get_current_user();
wp_logout();
if ( ! empty( $_REQUEST[ 'redirect_to' ] ) ) {
$redirect_to = $_REQUEST[ 'redirect_to' ];
$requested_redirect_to = $_REQUEST[ 'redirect_to' ];
} else {
$redirect_to = site_url( 'wp-login.php?loggedout=true' );
$requested_redirect_to = '';
}
$redirect_url = apply_filters( 'logout_redirect', $redirect_to, $requested_redirect_to, $user );
}
if ( is_home() || is_front_page() ) {
if ( isset( $_REQUEST[ 'logout' ] ) ) {
check_admin_referer( 'logout' );
$user = wp_get_current_user();
wp_logout();
$redirect_to = home_url();
$redirect_url = apply_filters( 'logout_redirect', $redirect_to, '', $user );
}
}
}
if ( isset( $redirect_url ) ) {
wp_safe_redirect( $redirect_url );
exit;
}
}
/*
* Register widgets (if activated)
*/
public function b3_register_widgets() {
if ( is_main_site() ) {
include 'includes/class-b3-sidebar-widget.php';
}
}
/*
* Add dashboard widget
*/
public function b3_add_dashboard_widget() {
/*
* Includes dashboard widget function + call
*/
if ( is_main_site() ) {
include 'admin/dashboard-widget-users.php';
if ( is_localhost() || apply_filters( 'b3_show_email_widget', false ) ) {
include 'admin/dashboard-widget-emails.php';
}
}
}
/**
* Add settings link to plugin page
*
* @param $links
*
* @return mixed
*/
public function b3_settings_link( $links ) {
$settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=b3-onboarding' ), esc_html__( 'Settings', 'b3-onboarding' ) );
array_unshift( $links, $settings_link );
return $links;
}
/**
* Check if user actions need to be taken
*/
public function b3_load_users_page() {
add_action( 'admin_notices', [ $this, 'b3_admin_notices' ] );
if ( isset( $_GET[ 'action' ] ) && in_array( $_GET[ 'action' ], [ 'activate', 'resendactivation' ] ) ) {
$user_id = isset( $_GET[ 'user_id' ] ) ? $_GET[ 'user_id' ] : false;
if ( ! $user_id ) {
wp_die( esc_html__( "There's no user with that ID.", 'b3-onboarding' ) );
} elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
wp_die( esc_html__( "You're not allowed to edit that user.", 'b3-onboarding' ) );
}
$user = new WP_User( $user_id );
$registration_type = false;
if ( in_array( 'b3_activation', $user->roles ) ) {
$registration_type = 'email_activation';
} elseif ( in_array( 'b3_approval', $user->roles ) ) {
$registration_type = 'request_access';
}
$redirect_to = isset( $_REQUEST[ 'wp_http_referer' ] ) ? remove_query_arg( [
'wp_http_referer',
'updated',
], stripslashes( $_REQUEST[ 'wp_http_referer' ] ) ) : 'users.php';
switch( $_GET[ 'action' ] ) {
case 'activate' :
check_admin_referer( 'manual-activation' );
do_action( 'b3_manual_user_activate', $user_id );
$redirect_to = add_query_arg( 'update', 'activated', $redirect_to );
break;
case 'resendactivation' :
check_admin_referer( 'resend-activation' );
if ( 'email_activation' === $registration_type ) {
do_action( 'b3_resend_user_activation', $user_id );
$redirect_to = add_query_arg( 'update', 'sendactivation', $redirect_to );
}
break;
}
wp_safe_redirect( $redirect_to );
exit;
}
}
/*
* Error function
*
* @return WP_Error
*/
public static function b3_errors() {
static $wp_error; // Will hold global variable safely
return isset( $wp_error ) ? $wp_error : ( $wp_error = new WP_Error( null, null, null ) );
}
/*
* Displays error messages from form submissions
*/
public static function b3_show_admin_notices() {
if ( $codes = B3Onboarding::b3_errors()->get_error_codes() ) {
if ( is_wp_error( B3Onboarding::b3_errors() ) ) {
// Loop error codes and display errors
$notice_class = false;
$prefix = false;
foreach( $codes as $code ) {
if ( strpos( $code, 'success' ) !== false ) {
$notice_class = 'updated notice ';
$prefix = false;
} elseif ( strpos( $code, 'error' ) !== false ) {
$notice_class = 'notice notice-error error ';
$prefix = esc_html__( 'Error', 'b3-onboarding' );
} elseif ( strpos( $code, 'warning' ) !== false ) {
$notice_class = 'notice notice-warning ';
$prefix = esc_html__( 'Warning', 'b3-onboarding' );
} elseif ( strpos( $code, 'info' ) !== false ) {
$notice_class = 'notice notice-info ';
$prefix = false;
} else {
$notice_class = 'notice--error ';
$prefix = esc_html__( 'Error', 'b3-onboarding' );
}
}
echo '<div class="' . $notice_class . 'is-dismissible">';
foreach( $codes as $code ) {
$message = B3Onboarding::b3_errors()->get_error_message( $code );
$message = ( true == $prefix ) ? '<strong>' . $prefix . ':</strong> ' . $message : $message;
echo sprintf( '<p>%s</p>', $message );
}
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . esc_attr__( 'Dismiss this notice', 'b3-onboarding' ) . '</span></button>';
echo '</div>';
}
}
}
/**
* An action function used to include the reCAPTCHA JavaScript file
* at the end of the page.
*/
public function b3_add_recaptcha_js_to_footer() {
if ( 1 == get_option( 'b3_activate_recaptcha' ) && is_page( b3_get_register_url( true ) ) ) {
wp_enqueue_script( 'recaptcha', 'https://www.google.com/recaptcha/api.js', [] );
}
}
/*
* Enqueue js for recaptcha
*/
public function b3_add_rc3() {
if ( 1 == get_option( 'b3_activate_recaptcha' ) && is_page( b3_get_register_url( true ) ) ) {
?>
<script>
function onSubmit(token) {
document.getElementById('registerform').submit();
}
</script>
<?php
}
}
/**
* Handle registration form
*/
public function b3_registration_form_handling() {
if ( 'POST' === $_SERVER[ 'REQUEST_METHOD' ] ) {
if ( isset( $_POST[ 'b3_register_user_nonce' ] ) ) {
$redirect_url = b3_get_register_url();
if ( ! wp_verify_nonce( $_POST[ 'b3_register_user_nonce' ], 'b3-register-user-nonce' ) ) {
$redirect_url = add_query_arg( 'registration-error', 'unknown', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
} else {
$meta_data = [];
$registration_type = get_option( 'b3_registration_type' );
$user_email = ( isset( $_POST[ 'user_email' ] ) ) ? sanitize_email( $_POST[ 'user_email' ] ) : false;
if ( get_option( 'b3_honeypot' ) && isset( $_POST[ 'b3_pooh' ] ) ) {
$errors = new WP_Error();
$errors->add( 'honeypot', $this->b3_get_return_message( 'no_robots' ) );
return $errors;
}
if ( 'blog' != $registration_type && ! is_email( $user_email ) ) {
$redirect_url = add_query_arg( 'registration-error', 'invalid_email', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
}
if ( isset( $_POST[ 'first_name' ] ) ) {
$meta_data[ 'first_name' ] = sanitize_text_field( $_POST[ 'first_name' ] );
}
if ( isset( $_POST[ 'last_name' ] ) ) {
$meta_data[ 'last_name' ] = sanitize_text_field( $_POST[ 'last_name' ] );
}
if ( ! is_multisite() ) {
$user_login = ( isset( $_POST[ 'user_login' ] ) ) ? sanitize_user( $_POST[ 'user_login' ] ) : false;
$register = true;
$role = get_option( 'default_role', 'subscriber' );
if ( 'none' === $registration_type ) {
// Registration closed, display error
$redirect_url = add_query_arg( 'registration-error', 'closed', $redirect_url );
$register = false;
} elseif ( false != get_option( 'b3_activate_recaptcha' ) && ! b3_verify_recaptcha() ) {
// Recaptcha check failed, display error
$redirect_url = add_query_arg( 'registration-error', 'recaptcha_failed', $redirect_url );
$register = false;
}
if ( true == $register && 'none' !== $registration_type ) {
// Registration is not closed
if ( 'request_access' === $registration_type ) {
$role = 'b3_approval';
$query_arg = 'access_requested';
} elseif ( 'email_activation' === $registration_type ) {
$role = 'b3_activation';
$query_arg = 'confirm_email';
} else {
$query_arg = 'success';
$reset_password = ( true == get_option( 'b3_redirect_set_password' ) ) ? true : false;
}
$result = $this->b3_register_user( $user_email, $user_login, $registration_type, $role );
if ( is_wp_error( $result ) ) {
// Parse errors into a string and append as parameter to redirect
$errors = join( ',', $result->get_error_codes() );
$redirect_url = add_query_arg( 'registration-error', $errors, $redirect_url );
} else {
// Success
if ( isset( $reset_password ) && true == $reset_password ) {
$reset_password_url = b3_get_lostpassword_url();
if ( false != $reset_password_url ) {
$redirect_url = $reset_password_url;
$redirect_url = add_query_arg( 'registered', $query_arg, $redirect_url );
$redirect_url = apply_filters( 'b3_redirect_after_register', $redirect_url );
// @TODO: also add to MU register
} else {
$login_url = b3_get_login_url();
$redirect_url = $login_url;
}
} else {
// redirect to login page
$redirect_url = b3_get_login_url();
$redirect_url = add_query_arg( 'registered', $query_arg, $redirect_url );
$redirect_url = apply_filters( 'b3_redirect_after_register', $redirect_url );
}
}
}
} else {
// if is_multisite
$user_login = ( isset( $_POST[ 'user_name' ] ) ) ? sanitize_user( $_POST[ 'user_name' ] ) : false;
$register = false;
if ( is_main_site() ) {
if ( 'none' === $registration_type ) {
// Registration closed, display error
$redirect_url = add_query_arg( 'registration-error', 'closed', $redirect_url );
} elseif ( 'blog' === $registration_type ) {
$user = get_userdata( get_current_user_id() );
$user_login = $user->user_login;
$user_email = $user->user_email;
$register = true;
} elseif ( false != get_option( 'b3_activate_recaptcha' ) && ! b3_verify_recaptcha() ) {
// Recaptcha check failed, display error
$redirect_url = add_query_arg( 'registration-error', 'recaptcha_failed', $redirect_url );
} else {
$register = true;
}
}
if ( true == $register ) {
$signup_for = ( isset( $_POST[ 'signup_for' ] ) ) ? $_POST[ 'signup_for' ] : false;
$user_valid = wpmu_validate_user_signup( $user_login, $user_email );
$errors = $user_valid[ 'errors' ];
$admin_approval = get_option( 'b3_needs_admin_approval' );
if ( $errors->has_errors() ) {
if ( 'blog' != $registration_type ) {
$error_message_user_name = $errors->get_error_message( 'user_name' );
$error_message_user_email = $errors->get_error_message( 'user_email' );
if ( ! empty( $error_message_user_name ) ) {
if ( __( 'Sorry, that username already exists!' ) === $error_message_user_name ) {
$error_codes[] = 'username_exists';
} elseif ( __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) === $error_message_user_name ) {
$error_codes[] = 'username_no_uppercase';
} elseif ( __( 'That username is currently reserved but may be available in a couple of days.' ) === $error_message_user_name ) {
$error_codes[] = 'wpmu_user_reserved';
}
}
if ( ! empty( $error_message_user_email ) ) {
if ( __( 'Sorry, that email address is already used!' ) === $error_message_user_email ) {
$error_codes[] = 'email_exists';
} elseif ( __( 'That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.' ) === $error_message_user_email ) {
$error_codes[] = 'wpmu_email_in_use';
}
}
if ( empty( $error_message_user_name ) && empty( $error_message_user_email ) ) {
// unknown error
$redirect_url = add_query_arg( 'registration-error', 'unknown', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
}
}
}
if ( isset( $error_codes ) && ! empty( $error_codes ) ) {
$errors = join( ',', $error_codes );
$redirect_url = add_query_arg( 'registration-error', $errors, $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
} else {
if ( 'user' === $signup_for ) {
$result = $this->b3_register_wpmu_user( $user_login, $user_email, false, false, false, $meta_data );
if ( true == $result ) {
// Success, redirect to login page.
$redirect_url = b3_get_login_url();
if ( $admin_approval ) {
$redirect_url = add_query_arg( 'registered', 'access_requested', $redirect_url );
} else {
$redirect_url = add_query_arg( 'registered', 'confirm_email', $redirect_url );
}
} elseif ( is_wp_error( $result ) ) {
$redirect_url = b3_get_register_url();
$errors = join( ',', $result->get_error_codes() );
$redirect_url = add_query_arg( 'registration-error', $errors, $redirect_url );
}
}
}
if ( 'blog' === $signup_for && empty( $error_codes ) ) {
$meta_data[ 'lang_id' ] = ( isset( $_POST[ 'lang_id' ] ) ) ? $_POST[ 'lang_id' ] : 1;
$meta_data[ 'public' ] = ( isset( $_POST[ 'blog_public' ] ) ) ? $_POST[ 'blog_public' ] : 1;
$user = '';
if ( $admin_approval ) {
$meta_data[ 'active' ] = 0;
$meta_data[ 'public' ] = 0;
}
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} elseif ( isset( $user_login ) && ! empty( $user_login ) ) {
$user = new WP_User();
$user->user_login = $user_login;
}
$blog_info = wpmu_validate_blog_signup( sanitize_text_field( $_POST[ 'blogname' ] ), sanitize_title( $_POST[ 'blog_title' ] ), $user );
$domain = $blog_info[ 'domain' ];
$path = $blog_info[ 'path' ];
$blog_title = $blog_info[ 'blog_title' ];
$errors = $blog_info[ 'errors' ];
$error_codes = [];
if ( $errors->has_errors() ) {
$error_message_name = $errors->get_error_message( 'blogname' );
$error_message_title = $errors->get_error_message( 'blog_title' );
if ( ! empty( $error_message_name ) ) {
if ( 'Please enter a site name.' === $error_message_name ) {
$error_codes[] = 'no_address';
} elseif ( 'Site name must be at least 4 characters.' === $error_message_name ) {
$error_codes[] = 'site_min4';
} elseif ( 'Sorry, site names must have letters too!' === $error_message_name ) {
$error_codes[] = 'site_letters';
} elseif ( 'Sorry, that site already exists!' === $error_message_name ) {
$error_codes[] = 'domain_exists';
}
} elseif ( ! empty( $error_message_title ) ) {
if ( 'Please enter a site title.' === $error_message_title ) {
$error_codes[] = 'no_title';
}
}
if ( ! empty( $error_codes ) ) {
$errors = join( ',', $error_codes );
$redirect_url = add_query_arg( 'registration-error', $errors, $redirect_url );
}
}
if ( empty( $error_codes ) ) {
$result = $this->b3_register_wpmu_user( $user_login, $user_email, $domain, $blog_title, $path, $meta_data );
if ( is_wp_error( $result ) ) {
$errors = join( ',', $result->get_error_codes() );
$redirect_url = add_query_arg( 'registration-error', $errors, $redirect_url );
} else {
if ( $admin_approval ) {
$redirect_url = add_query_arg( 'registered', 'access_requested', $redirect_url );
} elseif ( 'blog' === $registration_type ) {
// Success, redirect to message.
$redirect_url = add_query_arg( 'registered', 'new_blog', $redirect_url );
$redirect_url = add_query_arg( 'site_id', $result, $redirect_url );
} elseif ( true == $result ) {
// Success, redirect to login page.
$redirect_url = b3_get_login_url();
$redirect_url = add_query_arg( 'registered', 'wpmu_confirm_email', $redirect_url );
}
}
}
}
}
}
wp_safe_redirect( $redirect_url );
exit;
}
}
}
}
/**
* Resets the user's password if the password reset form was submitted (with custom passwords)
*
* @since 1.0.6
*/
public function b3_reset_user_password() {
if ( 'POST' === $_SERVER[ 'REQUEST_METHOD' ] ) {
$rp_key = ( isset( $_REQUEST[ 'rp_key' ] ) ) ? $_REQUEST[ 'rp_key' ] : false;
$rp_login = ( isset( $_REQUEST[ 'rp_login' ] ) ) ? $_REQUEST[ 'rp_login' ] : false;
if ( $rp_key && $rp_login ) {
$user = check_password_reset_key( $rp_key, $rp_login );
if ( ! $user || is_wp_error( $user ) ) {
$login_url = b3_get_login_url();
if ( $user && $user->get_error_code() === 'expired_key' ) {
$redirect_url = add_query_arg( 'login', 'expiredkey', $login_url );
} else {
$redirect_url = add_query_arg( 'login', 'invalidkey', $login_url );
}
wp_safe_redirect( $redirect_url );
exit;
}
if ( isset( $_POST[ 'pass1' ] ) ) {
if ( $_POST[ 'pass1' ] != $_POST[ 'pass2' ] || empty( $_POST[ 'pass1' ] ) ) {
// Password is empty or don't match
$redirect_url = b3_get_reset_password_url();
$redirect_url = add_query_arg( 'key', $rp_key, $redirect_url );
$redirect_url = add_query_arg( 'login', $rp_login, $redirect_url );
$redirect_url = add_query_arg( 'error', 'password_reset_empty', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
}
// Parameter checks OK, reset password
reset_password( $user, $_POST[ 'pass1' ] );
$redirect_url = b3_get_login_url();
$redirect_url = add_query_arg( 'password', 'changed', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
} else {
echo "Invalid request.";
}
}
}
}
public function b3_magic_link_form_handling() {
if ( 'POST' === $_SERVER[ 'REQUEST_METHOD' ] ) {
if ( isset( $_POST[ 'b3_set_otp_nonce' ] ) ) {
$redirect_url = b3_get_login_url();
if ( ! wp_verify_nonce( $_POST[ 'b3_set_otp_nonce' ], 'b3-set-otp-nonce' ) ) {
$redirect_url = add_query_arg( 'login-error', 'unknown', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
} elseif ( ! isset( $_POST[ 'email' ] ) || empty( $_POST[ 'email' ] ) ) {
$redirect_url = add_query_arg( 'login-error', 'empty_email', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
} elseif ( isset( $_POST[ 'email' ] ) ) {
$user_email = $_POST[ 'email' ];
$existing_user = get_user_by( 'email', $user_email );
if ( $existing_user instanceof WP_User ) {
$amount_minutes = apply_filters( 'b3_magic_link_time_out', 5 );
$pw_special_chars = apply_filters( 'b3_password_special_chars', true );
$pw_extra_special_chars = apply_filters( 'b3_password_extra_special_chars', false );
$otp_password = wp_generate_password( 8, $pw_special_chars, $pw_extra_special_chars );
$hashed_password = password_hash( $otp_password, PASSWORD_BCRYPT );
$slug = sprintf( '%s:%s', $user_email, $hashed_password );
$hashed_slug = base64_encode( $slug );
$transient_set = set_transient( sprintf( 'otp_%s', $user_email ), $hashed_password, $amount_minutes * MINUTE_IN_SECONDS );
if ( $transient_set ) {
$vars = []; // empty right now, but might be filled later on...
$to = $user_email;
$subject = __( 'Magic login link for %blog_name%', 'b3-onboarding' );
$subject = strtr( $subject, b3_get_replacement_vars( 'subject' ) );
$message = b3_get_magic_link_email( $otp_password, $hashed_slug );
if ( ! empty( $message ) ) {
$message = b3_replace_template_styling( $message );
$message = strtr( $message, b3_get_replacement_vars( 'message', $vars ) );
$message = htmlspecialchars_decode( stripslashes( $message ) );
wp_mail( $to, $subject, $message, [] );
} else {
error_log( 'Email message not set for magic link.' );
}
} else {
error_log( sprintf( 'Transient is not set when "%s" requested a magic link and thus email is not sent.', $user_email ) );
}
}
} else {
error_log( 'FALLBACK' );
}
}
}
}
/**
* Verify a link clicked from an email
*
* @return void
*/
public function b3_check_magic_link() {
if ( isset( $_GET[ 'otpcode' ] ) ) {
$verify_otp = b3_verify_otp( $_GET[ 'otpcode' ] );
if ( $verify_otp instanceof WP_User ) {
do_action( 'b3_log_user_in', $verify_otp );
} else {
$redirect_url = b3_get_login_url();
$redirect_url = add_query_arg( 'error', 'verification_fail', $redirect_url );
wp_safe_redirect( $redirect_url );
exit;
}
}
}
/**
* Finds and returns a matching error message for the given error code.
*
* @param string $error_code The error code to look up.
*
* @return string An error message.
* @since 1.0.6
*
*/
public function b3_get_return_message( $error_code, $sprintf = false ) {
switch( $error_code ) {
case 'banned_domain':
return esc_html__( 'This domain is not allowed to register.', 'b3-onboarding' );
case 'empty_username':
return esc_html__( 'Please enter a user name.', 'b3-onboarding' );
case 'empty_password':
return esc_html__( 'Please enter a password.', 'b3-onboarding' );
case 'empty_email':
return esc_html__( 'Please enter an email address.', 'b3-onboarding' );
case 'incorrect_password':
$error_message = esc_html__( "The username or password you entered wasn't quite right.", 'b3-onboarding' );
$error_message .= '<br>';
$error_message .= sprintf( esc_html__( 'Did you %s your password ?', 'b3-onboarding' ), sprintf( '<a href="%s">%s</a>', wp_lostpassword_url(), esc_html__( 'forget', 'b3-onboarding' ) ) );
return $error_message;
case 'logged_in':
return esc_html__( 'You are now logged in.', 'b3-onboarding' );
case 'logged_out':
return esc_html__( 'You are logged out.', 'b3-onboarding' );
case 'verification_fail':
return esc_html__( 'Your code seems to be invalid. Please try again.', 'b3-onboarding' );
case 'unknown':
return esc_html__( 'An unkown error has occured. Please try again.', 'b3-onboarding' );
// Login errors
case 'code_sent':
$message = esc_html__( 'If your email address is associated with a user, you will receive an email shortly with a magic link.', 'b3-onboarding' );
$message .= ' ';
$message .= esc_html__( sprintf( 'The link is valid for %d minutes.', apply_filters( 'b3_magic_link_time_out', 5 ) ), 'b3-onboarding' );
return $message;
case 'unknown_user':
return esc_html__( 'There is no user with this email address.', 'b3-onboarding' );
// Registration errors
case 'username_exists':
return esc_html__( 'This username is already in use.', 'b3-onboarding' );
case 'username_no_uppercase':
return esc_html__( 'Usernames can only contain lowercase letters (a-z) and numbers.', 'b3-onboarding' );
case 'disallowed_username':
return esc_html__( 'That user name is not allowed, please choose another.', 'b3-onboarding' );
case 'invalid_email':
return esc_html__( 'The email address you entered is not valid.', 'b3-onboarding' );
case 'invalid_username':
if ( 1 == get_option( 'b3_register_email_only' ) ) {
return esc_html__( 'The email address you entered is not valid.', 'b3-onboarding' );
} else {
return esc_html__( 'The user login you entered is not valid.', 'b3-onboarding' );
}
case 'email_exists':
return esc_html__( 'An account already exists with this email address.', 'b3-onboarding' );
case 'closed':
return esc_html__( 'Registering new users is currently not allowed.', 'b3-onboarding' );
case 'recaptcha_failed':
return esc_html__( 'The Google reCAPTCHA check failed. Are you a robot?', 'b3-onboarding' );
case 'no_privacy':
return esc_html__( 'You have to accept the privacy statement.', 'b3-onboarding' );
case 'empty_field':
if ( false != $sprintf ) {
return sprintf( esc_html__( "You didn't select an option for '%s'.", 'b3-onboarding' ), $sprintf );
} else {
return esc_html__( "You didn't select an option.", 'b3-onboarding' );
}
case 'access_requested':
$access_requested_string = esc_html__( 'You have sucessfully requested access. Someone will check your request.', 'b3-onboarding' );
return apply_filters( 'b3_registration_access_requested_message', $access_requested_string );
case 'confirm_email':
$confirm_email_string = esc_html__( 'You have sucessfully registered but need to confirm your email address first. Please check your email for an activation link.', 'b3-onboarding' );
return apply_filters( 'b3_registration_confirm_email_message', $confirm_email_string );
case 'honeypot':
return esc_html__( 'No robo signups.', 'b3-onboarding' );
// Lost password
case 'invalidcombo':
return esc_html__( 'There are no users registered with this email address.', 'b3-onboarding' );