-
Notifications
You must be signed in to change notification settings - Fork 19
/
class-gf-user-registration.php
4907 lines (3872 loc) · 156 KB
/
class-gf-user-registration.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
defined( 'ABSPATH' ) || die();
// Includes the feeds portion of the add-on framework
GFForms::include_feed_addon_framework();
// Includes deprecated functionality for backwards compatibility
require_once( plugin_dir_path( __FILE__ ) . 'includes/deprecated.php');
/**
* User Registration functionality using the add-on framework
*
* Contains most of the functionality of the add-on
*
* @see GFFeedAddOn
*/
class GF_User_Registration extends GFFeedAddOn {
protected $_version = GF_USER_REGISTRATION_VERSION;
protected $_min_gravityforms_version = '2.2.6';
protected $_slug = 'gravityformsuserregistration';
protected $_path = 'gravityformsuserregistration/userregistration.php';
protected $_full_path = __FILE__;
protected $_url = 'http://www.gravityforms.com';
protected $_title = 'User Registration Add-On';
protected $_short_title = 'User Registration';
protected $_single_feed_submission = true;
protected $_enable_rg_autoupgrade = true;
protected $login_form = array();
// Members plugin integration
protected $_capabilities = array( 'gravityforms_user_registration', 'gravityforms_user_registration_uninstall' );
// Permissions
protected $_capabilities_settings_page = 'gravityforms_user_registration';
protected $_capabilities_form_settings = 'gravityforms_user_registration';
protected $_capabilities_uninstall = 'gravityforms_user_registration_uninstall';
private static $_instance = null;
/**
* Creates a new instance of the GF_User_Registration
*
* Only creates a new instance if it does not already exist
*
* @static
*
* @return object The GF_User_Registration class object
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Handles anything which requires early initialization such as including the username field.
*/
public function pre_init() {
parent::pre_init();
if ( $this->is_gravityforms_supported() && class_exists( 'GF_Field' ) ) {
require_once 'includes/class-gf-field-username.php';
add_action( 'parse_request', array( $this, 'redirect_legacy_activation_page' ) );
}
if ( ! wp_next_scheduled ( 'gform_userregistration_cron' ) ) {
wp_schedule_event( time(), 'twicedaily', 'gform_userregistration_cron' );
}
}
/**
* Initializes GFAddon and adds the actions that we need
*
* @see GFAddon
*/
public function init() {
// Add functionality from the parent GFAddon class
parent::init();
$this->add_delayed_payment_support(
array(
'option_label' => esc_html__( 'Process the feed only when a payment is received.', 'gravityformsuserregistration' ),
)
);
add_filter( 'gaddon_no_output_field_properties', array( $this, 'no_output_field_properties' ) );
add_filter( 'gform_enable_password_field', '__return_true' );
add_action( 'wp', array( $this, 'maybe_activate_user' ) );
add_action( 'wp_loaded', array( $this, 'custom_registration_page' ) );
add_action( 'gform_pre_render', array( __class__, 'maybe_prepopulate_form' ) );
add_filter( 'gform_validation', array( $this, 'validate' ) );
add_action( 'gform_pre_submission', array( $this, 'handle_existing_images_submission' ) );
add_filter( 'gform_field_input', array( $this, 'maybe_update_field_input' ), 10, 5 );
add_action( 'gform_user_registration_validation', array( $this, 'validate_multisite_submission' ), 10, 3 );
add_action( 'gform_user_registered', array( $this, 'create_site' ), 10, 4 );
add_action( 'gform_user_updated', array( $this, 'create_site' ), 10, 4 );
add_action( 'gform_after_create_post', array( $this, 'set_user_as_post_author' ), 10, 3 );
// If BuddyPress is active, adds additional actions
if ( self::is_bp_active() ) {
add_action( 'gform_user_registered', array( $this, 'update_buddypress_data' ), 10, 3 );
add_action( 'gform_user_updated', array( $this, 'update_buddypress_data' ), 10, 3);
add_action( 'gform_user_registered', array( $this, 'do_buddypress_user_signup' ) );
}
// process users from unspammed entries
add_action( 'gform_update_status', array( $this, 'process_feed_when_unspammed' ), 10, 3 );
// PayPal options
if ( $this->is_gravityforms_supported( '2.0-beta-2' ) ) {
if ( method_exists( $this, 'add_post_payment_actions' ) ) {
remove_filter( 'gform_addon_feed_settings_fields', array( $this, 'add_post_payment_actions' ) );
} else {
remove_filter( 'gform_gravityformspaypal_feed_settings_fields', array( $this, 'add_paypal_post_payment_actions' ) );
}
} else {
remove_action( 'gform_paypal_action_fields', array( $this, 'add_paypal_settings' ), 10 );
remove_filter( 'gform_paypal_save_config', array( $this, 'save_paypal_settings' ) );
}
if ( method_exists( $this, 'add_post_payment_actions' ) ) {
add_filter( 'gform_addon_feed_settings_fields', array( $this, 'add_feed_settings' ), 10, 2 );
} else {
add_filter( 'gform_gravityformspaypal_feed_settings_fields', array( $this, 'add_paypal_settings' ), 10, 2 );
}
// add paypal ipn hooks
add_action( 'gform_subscription_canceled', array( $this, 'downgrade_user' ), 10, 2 );
add_action( 'gform_subscription_canceled', array( $this, 'downgrade_site' ), 10, 2 );
// Add user meta shortcode
add_filter( 'gform_shortcode_user', array( $this, 'parse_user_meta_shortcode' ), 10, 3 );
// Add login form shortcode and sign on hooks
add_filter( 'gform_shortcode_login', array( $this, 'parse_login_shortcode' ), 10, 3 );
add_action( 'wp', array( $this, 'handle_login_submission' ) );
$this->load_pending_activations();
// Add support for data retention.
add_filter( 'gform_entry_ids_automatic_deletion', array( $this, 'filter_gform_entry_ids_automatic_deletion' ) );
// add support for UR related merge tags
add_action( 'gform_admin_pre_render', array( $this, 'add_merge_tags' ) );
add_filter( 'gform_replace_merge_tags', array( $this, 'replace_merge_tags' ), 10, 7 );
$this->define_gf_new_user_notification();
}
/**
* Enqueues required JavaScript
*
* Defines required scripts for the User Registration add-on, and adds them to scripts in GFFeedAddOn
*
* @see GFFeedAddOn::scripts()
*
* @return array Contains the scripts to be enqueued
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'gform_user_registration_widget_editor',
'src' => $this->get_base_url() . "/js/widget_editor{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery' ),
'enqueue' => array(
array( $this, 'can_enqueue_widget_editor_script' ),
array( 'admin_page' => array( 'customizer' ) ),
)
),
array(
'handle' => 'gform_user_registration_feed_settings',
'src' => $this->get_base_url() . '/js/feed_settings.js',
'version' => $this->_version,
'deps' => array( 'jquery' ),
'enqueue' => array(
array(
'admin_page' => array( 'form_settings' ),
'tab' => $this->_slug,
),
)
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Enqueues required styleseheets
*
* Defines required styles for the User Registration add-on, and adds them to styles in GFFeedAddOn
*
* @see GFFeedAddOn::styles()
*
* @return array Contains the styles to be enqueued
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_user_registration_widget_editor',
'src' => $this->get_base_url() . "/css/widget_editor{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( $this, 'can_enqueue_widget_editor_script' ),
array( 'admin_page' => array( 'customizer' ) ),
)
),
array(
'handle' => 'gform_user_registration_feed_settings',
'src' => $this->get_base_url() . '/css/feed_settings.css',
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'form_settings' ),
'tab' => $this->_slug,
),
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 4.5
*
* @return string
*/
public function get_menu_icon() {
return file_get_contents( $this->get_base_path() . '/images/menu-icon.svg' );
}
/**
* Determines if the current screen is the widget editor
*
* @return bool True if current screen is the widget editor. Otherwise, false
*/
public function can_enqueue_widget_editor_script() {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}
/* Get the current screen. */
$screen = get_current_screen();
return isset( $screen ) && is_object( $screen ) ? $screen->id === 'widgets' : false;
}
/**
* Loads the pending user activations object
*
* @see GF_Pending_Activations
*
* @return object The GF_Pending_Activations class object
*/
public function load_pending_activations() {
require_once( 'includes/class-gf-pending-activations.php' );
gf_pending_activations();
}
/**
* Downgrades the role of a user
*
* Used when a subscription is canceled
*
* @param array $entry The Entry object
* @param array $feed The Feed object
*/
public function downgrade_user( $entry, $feed ) {
if ( ! $feed || ! rgars( $feed, 'meta/cancellationActionUserEnable' ) ) {
return;
}
$user = $this->get_user_by_entry_id( $entry['id'] );
if ( ! $user || is_wp_error( $user ) ) {
$this->log( 'No user found.' );
return;
}
$user->set_role( rgars( $feed, 'meta/cancellationActionUserValue' ) );
}
/**
* Downgrades a site within a multisite installation
*
* Used when a subscription is canceled
*
* @param array $entry The Entry object
* @param array $feed The Feed object
*/
public function downgrade_site( $entry, $feed ) {
global $current_site;
if ( ! is_multisite() ) {
return;
}
if ( ! rgars( $feed, 'meta/cancellationActionSiteEnable' ) ) {
return;
}
$site_id = $this->get_site_by_entry_id( $entry['id'] );
// Log the error if site is not found
if ( ! $site_id ) {
$this->log( 'No site found.' );
return;
}
// Gets the action defined in the feed
$action = rgars( $feed, 'meta/cancellationActionSiteValue' );
// Checks the action to take defined within a feed
switch( $action ) {
case 'deactivate':
/** This action is documented in /wp-admin/network/sites */
do_action( 'deactivate_blog', $site_id );
update_blog_status( $site_id, 'deleted', '1' );
break;
case 'delete':
require_once( ABSPATH . 'wp-admin/includes/ms.php' );
if ( $site_id != '0' && $site_id != $current_site->blog_id ) {
wpmu_delete_blog( $site_id, true );
}
break;
}
}
/**
* Redirect to the custom registration page as specified in the User Registration settings.
*
* By default, this function checks if the user is accessing the default WP registration page
* "/wp-login.php?action=register" and if so, processes the redirect.
*
* If BuddyPress is active, it checks if the current page is the the BP registration page
* (as specified in the BP Page settings) and if so, processes the redirect. We also check
* to ensure that the User Registration Custom Registration Page ID is not the same as the
* BP Register Page ID.
*
* @global object $bp The BuddyPress object
*
* @see self::is_bp_active()
* @see $this->get_plugin_settings()
*/
public function custom_registration_page() {
if ( is_user_logged_in() ) {
return;
}
global $bp;
$action = rgget( 'action' );
$redirect = false;
// If BuddyPress is active and this is the registration page, redirect
if ( self::is_bp_active() && bp_is_register_page() ) {
$redirect = true;
}
// if "wp-login.php?action=register", aka default WP registration page
$script_name = substr( $_SERVER['SCRIPT_NAME'], - 12, 12 ); // get last 12 characters of script name (we want wp-login.php);
if ( $script_name == 'wp-login.php' && $action == 'register' ) {
$redirect = true;
}
// add support for multi-site
$script_name = substr( $_SERVER['SCRIPT_NAME'], - 13, 13 ); // get last 12 characters of script name (we want wp-login.php);
if ( is_multisite() && $script_name == 'wp-signup.php' ) {
$redirect = true;
}
if ( ! $redirect ) {
return;
}
$ur_settings = $this->get_plugin_settings();
$reg_page_id = rgar( $ur_settings, 'custom_registration_page' );
$reg_page_url = rgar( $ur_settings, 'custom_registration_page_custom' );
if ( empty( $ur_settings ) || ! rgar( $ur_settings, 'custom_registration_page_enable' ) ) {
return;
}
// if BP is active, BP Register Page is set and BP Register Page ID is the same as the UR Register Page ID, cancel redirect
if ( self::is_bp_active() && isset( $bp->pages->register->id ) && $bp->pages->register->id == $reg_page_id ) {
return;
}
if ( 'gf_custom' === $reg_page_id ) {
wp_redirect( $reg_page_url );
} else {
wp_redirect( get_permalink( $reg_page_id ) );
}
exit;
}
// # USER CREATION -------------------------------------------------------------------------------------------------
/**
* Adds custom validation to gform_validation
*
* @see filter gform_validation
* @see GFFormsModel::get_current_lead()
* @see GFFormsModel::get_field()
* @see GFFormsModel::is_field_hidden()
* @see $this->get_meta_value
*
* @param array $validation_result The validation result passed from the gform_validation filter
*
* @return array $validation_result The validation result after completion
*/
public function validate( $validation_result ) {
$form = $validation_result['form'];
$entry = GFFormsModel::get_current_lead();
$feed = $this->get_filtered_single_submission_feed( $entry, $form );
if ( ! $feed ) {
return $validation_result;
}
return $this->do_validate( $validation_result, $feed, $form, $entry );
}
public function do_validate( $validation_result, $feed, $form, $entry ) {
$submitted_page = GFFormDisplay::get_source_page( $form['id'] );
$username_field = GFFormsModel::get_field( $form, rgars( $feed, 'meta/username' ) );
$email_field = GFFormsModel::get_field( $form, rgars( $feed, 'meta/email' ) );
$password_field = GFFormsModel::get_field( $form, $feed['meta']['password'] );
$is_username_hidden = GFFormsModel::is_field_hidden( $form, $username_field, array() );
$is_email_hidden = !$email_field || GFFormsModel::is_field_hidden( $form, $email_field, array() );
$is_password_hidden = GFFormsModel::is_field_hidden( $form, $password_field, array() );
$username = $this->get_meta_value( 'username', $feed, $form, $entry );
$user_email = $this->get_meta_value( 'email', $feed, $form, $entry );
$user_pass = $this->get_meta_value( 'password', $feed, $form, $entry );
/**
* Filters the username of the user being registered
*
* @param int $form ['id'] The ID of the form being submitted
* @param string $username The username of the being created
* @param array $feed The Feed object
* @param array $form The Form object
* @param array $entry The Entry object
*/
$username = gf_apply_filters( 'gform_username', $form['id'], $username, $feed, $form, $entry );
if ( !function_exists( 'username_exists' ) ) {
require_once( ABSPATH . WPINC . '/registration.php' );
}
if ( ! $is_password_hidden && $password_field && $password_field->pageNumber <= $submitted_page ) {
if ( strpos( $user_pass, "\\" ) !== false ) {
$form = $this->add_validation_error( $password_field->id, $form, __( 'Passwords may not contain the character "\"', 'gravityformsuserregistration' ) );
}
}
/**
* Filters the ID of the user being registered
*
* @param int $form ['id'] The ID of the form being submitted
* @param int $current_user_id The ID of the current user
* @param array $feed The Entry object
* @param array $form The Form object
* @param array $entry The Entry object
*/
$user_id = gf_apply_filters( 'gform_user_registration_update_user_id', $form['id'], get_current_user_id(), $entry, $form, $feed );
// Additional processing of multisite installs
if ( is_multisite() ) {
// Convert username to lowercase
$username = strtolower( $username );
$result = wpmu_validate_user_signup( $username, $user_email );
$errors = $result['errors']->errors;
// Validation overrides for feeds configured for user updates
if ( $this->is_update_feed( $feed ) ) {
// Avoid validating user update feeds
if ( isset( $errors['user_name'] ) ) {
unset( $errors['user_name'] );
}
// Check if the email already belongs to a user
if ( isset( $errors['user_email'] ) ) {
for ( $i = count( $errors['user_email'] ) - 1; $i >= 0; $i -- ) {
$error_message = $errors['user_email'][$i];
// If the user is submitting their own email address, allow it by removing the error entry
if ( $error_message == __( 'Sorry, that email address is already used!' ) && $this->is_users_email( $user_email, $user_id ) ) {
unset( $errors['user_email'][$i] );
} // Same as the above, but for a different message.
elseif ( $error_message == __( '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.' ) && $this->is_users_email( $user_email, $user_id ) ) {
unset( $errors['user_email'][$i] );
}
}
// If there aren't any errors left, remove the key completely
if ( count( $errors['user_email'] ) <= 0 ) {
unset( $errors['user_email'] );
}
}
}
// Check if there are any errors
if ( !empty( $errors ) ) {
foreach ( $errors as $type => $error_msgs ) {
foreach ( $error_msgs as $error_msg ) {
// Depending on the error type, display a different validation error.
switch ( $type ) {
case 'user_name':
if ( !$is_username_hidden && ( $username_field->pageNumber <= $submitted_page ) ) {
$form = $this->add_validation_error( $feed['meta']['username'], $form, $error_msg );
}
break;
case 'user_email':
if ( !$is_email_hidden && $email_field->pageNumber <= $submitted_page ) {
$form = $this->add_validation_error( $feed['meta']['email'], $form, $error_msg );
}
break;
}
}
}
}
// Validation if multisite is not enabled
} else {
// Validation for email fields
if ( !$is_email_hidden && $email_field->pageNumber <= $submitted_page ) {
$email_valid = true;
$email_exists = email_exists( $user_email );
// Throws an error if the email was not entered
if ( !$user_email ) {
$email_valid = false;
$form = $this->add_validation_error( $feed['meta']['email'], $form, __( 'The email address can not be empty', 'gravityformsuserregistration' ) );
}
// Throws an error if the email is valid, but is already pending activation
if ( $email_valid && $this->pending_activation_exists( 'user_email', $user_email ) ) {
$email_valid = false;
$form = $this->add_validation_error( $feed['meta']['email'], $form, __( '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.' ) );
}
// Throws an error if the email is already registered
if ( $email_valid && !$this->is_update_feed( $feed ) && $email_exists ) {
$form = $this->add_validation_error( $feed['meta']['email'], $form, __( 'This email address is already registered', 'gravityformsuserregistration' ) );
} elseif ( $email_valid && $this->is_update_feed( $feed ) && $email_exists && !$this->is_users_email( $user_email, $user_id ) ) {
$form = $this->add_validation_error( $feed['meta']['email'], $form, __( 'This email address is already registered', 'gravityformsuserregistration' ) );
}
}
// Validation for username fields. Ignores user update feeds
if ( !$this->is_update_feed( $feed ) && !$is_username_hidden && $username_field->pageNumber <= $submitted_page ) {
$username_valid = true;
// Throws an error if the username wasn't submitted
if ( empty( $username ) ) {
$username_valid = false;
$form = $this->add_validation_error( $feed['meta']['username'], $form, __( 'The username can not be empty', 'gravityformsuserregistration' ) );
}
// Throws an error if the username contains invalid characters
if ( $username_valid && ! validate_username( $username ) ) {
$username_valid = false;
$form = $this->add_validation_error( $feed['meta']['username'], $form, __( 'This username is invalid because it uses illegal characters. Please enter a valid username.', 'gravityformsuserregistration' ) );
}
// Throws an error if a user on a BuddyPress site contains a space or other invalid characters
if ( $username_valid && self::is_bp_active() && strpos( $username, " " ) !== false ) {
$username_valid = false;
$form = $this->add_validation_error( $feed['meta']['username'], $form, __( 'The username can only contain alphanumeric characters (A-Z, 0-9), underscores and dashes', 'gravityformsuserregistration' ) );
}
// Throws an error if the username already exists
if ( $username_valid && username_exists( $username ) ) {
$username_valid = false;
$form = $this->add_validation_error( $feed['meta']['username'], $form, __( 'This username is already registered', 'gravityformsuserregistration' ) );
}
// Throws an error if the user is pending activation
if ( $username_valid && $this->pending_activation_exists( 'user_login', $username ) ) {
$form = $this->add_validation_error( $feed['meta']['username'], $form, __( 'That username is currently reserved but may be available in a couple of days' ) );
}
}
}
/**
* Filters the form object, allowing for extended validation of user registration submissions
*
* @param array $form The Form object
* @param array $feed The Feed object
* @param int $submitted_page The ID of the form page that was submitted
*/
$form = apply_filters( 'gform_user_registration_validation', $form, $feed, $submitted_page );
$validation_result['is_valid'] = $this->is_form_valid( $form );
$validation_result['form'] = $form;
return $validation_result;
}
/**
* Processes the feed for the User Registration add-on if delayed
*
* @see GFFeedAddOn->delay_feed()
*
* @param array $feed The Feed object
* @param array $entry The Entry object
* @param array $form The Form object
*/
public function delay_feed( $feed, $entry, $form ) {
$user_data = $this->get_user_data( $entry, $form, $feed );
$password = rgar( $user_data, 'password' );
if ( $password ) {
gform_update_meta( $entry['id'], 'userregistration_password', wp_hash_password( $password ) );
}
}
/**
* Processed the feed for the User Registration add-on
*
* @see GFFeedAddOn->process_feed()
*
* @param array $feed The Feed object
* @param array $entry The Entry object
* @param array $form The Form object
*/
public function process_feed( $feed, $entry, $form ) {
// Log that the feed is being processed
$this->log( "form #{$form['id']} - starting process_feed()." );
// Get user data. If none found, log the error
$user_data = $this->get_user_data( $entry, $form, $feed );
if ( ! $user_data ) {
$this->log( 'aborting. user_login or user_email are empty.' );
return;
}
/**
* Disables registration.
*
* Defaults to false unless overridden
*
* @param bool false Registration enabled. Change to true to disable
* @param array $form The Form object
* @param array $entry The Entry object
* @param null $fulfilled Deprecated
*/
$disable_registration = apply_filters( 'gform_disable_registration', false, $form, $entry, null /* $fullfilled deprecated */ );
if ( $disable_registration ) {
$this->log( 'aborting. gform_disable_registration hook was used.' );
return;
}
if ( $this->is_update_feed( $feed ) ) {
$this->update_user( $entry, $form, $feed );
} else {
$is_user_activation = rgars( $feed, 'meta/userActivationEnable' ) == true;
if ( $is_user_activation ) {
$this->log( 'Calling handle_user_activation().' );
$this->handle_user_activation( $entry, $form, $feed );
} else {
$this->log( 'Calling create_user().' );
$this->create_user( $entry, $form, $feed );
}
}
// password will be stored in entry meta for delayed feeds, delete after processing feed
gform_delete_meta( $entry['id'], 'userregistration_password' );
}
public function process_feed_when_unspammed( $entry_id, $status, $prev_status ) {
$is_unspammed = $prev_status == 'spam' && $status == 'active';
if ( ! $is_unspammed ) {
return;
}
$this->log( sprintf( 'Entry has been unspammed (ID: %d).', $entry_id ) );
// check if user has already been created for this entry (prevents multiple users being created if an entry has been unspammed before)
if ( $this->get_user_by_entry_id( $entry_id, true ) ) {
return;
}
$this->log( sprintf( 'User has not been created for this entry (ID: %d).', $entry_id ) );
$entry = GFAPI::get_entry( $entry_id );
$form = GFAPI::get_form( $entry['form_id'] );
$this->log( 'Calling maybe_process_feed(). The first feed with matching conditions will be processed.' );
$this->maybe_process_feed( $entry, $form );
}
/**
* Create a WordPress user from the given entry and feed.
*
* @since 4.4.3 The `$password_hash` parameter was added.
*
* @param array $entry Current entry.
* @param array $form Current form.
* @param bool $feed Current User Registration feed.
* @param string $password Deprecated. Specified when creating a user via User Activation.
* @param string $password_hash Optional. Specified when creating a user via User Activation.
*
* @return array|bool
*/
public function create_user( $entry, $form, $feed = false, $password = '', $password_hash = '' ) {
$this->log( sprintf( 'Start with form id: %s; entry: %s', $form['id'], print_r( $entry, true ) ) );
if ( ! $feed ) {
$feed = $this->get_filtered_single_submission_feed( $entry, $form );
}
$meta = rgar( $feed, 'meta' );
$user_data = $this->get_user_data( $entry, $form, $feed );
if ( ! empty( $password ) ) {
$user_data['password'] = $password;
}
if ( ! empty ( $password_hash ) ) {
$user_data['password_hash'] = $password_hash;
}
$user_id = $this->user_login_exists( $user_data['user_login'] );
if ( $user_id ) {
$this->log( sprintf( 'User already exists. User ID: %s', $user_id ) );
return false;
}
$this->log( sprintf( 'Calling wp_create_user() for login "%s" with email "%s".', $user_data['user_login'], $user_data['user_email'] ) );
$password = $user_data['password'] ? $user_data['password'] : wp_generate_password( 24, false );
$user_id = wp_create_user( $user_data['user_login'], $password, $user_data['user_email'] );
if ( is_wp_error( $user_id ) ) {
$this->log( 'Aborting; wp_create_user() returned an error: ' . print_r( $user_id, 1 ) );
return false;
}
$this->add_user_meta( $user_id, $feed, $form, $entry, array() );
if ( $user_data['password_hash'] ) {
$this->log( sprintf( 'Setting hashed password for user ID %d.', $user_id ) );
$this->set_hashed_password( $user_id, $user_data['password_hash'] );
}
clean_user_cache( $user_id );
// Updating display name after user meta because of dependency.
$update_args = array(
'ID' => $user_id,
'display_name' => $this->get_display_name( $user_id, $feed ),
);
$role = rgar( $meta, 'role' );
if ( $role ) {
$this->log( sprintf( 'Setting role: %s', $role ) );
$update_args['role'] = $role;
}
wp_update_user( $update_args );
$this->send_wp_new_user_notification( $entry, $form, $feed, $user_id );
GFAPI::send_notifications( $form, $entry, 'gfur_user_registered' );
/**
* Determine if entry creator should be associated with newly created user.
*
* @since 4.0.9
*
* @param bool $update_entry_creator If entry creator should be associated with user.
* @param int $user_id New user ID.
* @param array $feed The Feed object.
* @param array $entry The Entry object.
* @param array $form The Form object.
*/
$update_entry_creator = apply_filters( 'gform_userregistration_associate_entry_with_user', ! rgar( $entry, 'created_by' ), $user_id, $feed, $entry, $form );
// Update entry creator.
if ( $update_entry_creator ) {
GFAPI::update_entry_property( $entry['id'], 'created_by', $user_id );
$entry['created_by'] = $user_id;
}
// set post author if feed was delayed by PayPal or entry was marked as spam
if ( ! rgempty( 'post_id', $entry ) && rgar( $meta, 'setPostAuthor' ) ) {
$this->attribute_post_author( $user_id, $entry['post_id'] );
}
do_action( 'gform_user_registered', $user_id, $feed, $entry, $password );
$user_data['user_id'] = $user_id;
return $user_data;
}
/**
* Maybe set the post author.
*
* @param int $post_id The ID of the post which was created from the entry.
* @param array $entry The entry currently being processed.
* @param array $form The form for the current entry.
*/
public function set_user_as_post_author( $post_id, $entry, $form ) {
$feed = $this->get_filtered_single_submission_feed( $entry, $form );
if ( $feed && ! $this->is_update_feed( $feed ) && rgars( $feed, 'meta/setPostAuthor' ) ) {
$user_id = $this->get_user_by_entry_id( $entry['id'], true );
if ( $user_id ) {
$this->attribute_post_author( $user_id, $post_id );
}
}
}
public function add_user_meta( $user_id, $feed, $form, $entry, $name_fields ) {
$is_update_feed = $this->is_update_feed( $feed );
$this->log( sprintf( '%s user meta.', $is_update_feed ? 'Updating' : 'Adding' ) );
$standard_meta = array(
'first_name',
'last_name',
'nickname'
);
foreach ( $standard_meta as $meta_key ) {
$this->log( sprintf( 'Processing meta item: %s', $meta_key ) );
if ( ! $this->is_meta_key_mapped( $meta_key, $feed ) ) {
$this->log( 'Meta item is empty. Skipping it.' );
continue;
}
$meta_value = rgars( $feed, "meta/$meta_key" );
$value = $this->get_meta_value( $meta_key, $feed, $form, $entry );
$this->log( sprintf( 'Meta item mapped to field: %s; Value: %s', $meta_value, $value ) );
$result = update_user_meta( $user_id, $meta_key, $value );
$this->log( sprintf( 'Result: %s', var_export( (bool) $result, 1 ) ) );
}
// to track which entry the user was registered/updated from
if ( $is_update_feed ) {
update_user_meta( $user_id, '_gform-update-entry-id', $entry['id'] );
} else {
update_user_meta( $user_id, 'entry_id', $entry['id'] ); // @deprecated
update_user_meta( $user_id, '_gform-entry-id', $entry['id'] ); // @future
}
// add custom user meta
$custom_meta = $this->get_dynamic_field_map_fields( $feed, 'userMeta' );
if ( ! is_array( $custom_meta ) || empty( $custom_meta ) ) {
return;
}
foreach ( $custom_meta as $meta_key => $meta_value ) {
$this->log( sprintf( 'Adding meta item: %s', $meta_key ) );
// skip empty meta items
if ( ! $meta_key || ! $meta_value ) {
$this->log( 'Meta item is empty. Skipping it.' );
continue;
}
$value = $this->get_meta_value( $meta_key, $custom_meta, $form, $entry ); // @review
$this->log( sprintf( 'Meta item mapped to field: %s; value: %s', $meta_value, var_export( $value, true ) ) );
if ( $value && in_array( $meta_key, array( 'user_url', 'user_nicename' ) ) ) {
$result = $this->update_user_property( $user_id, $meta_key, $value );
} elseif ( rgblank( $value ) ) {
$result = $is_update_feed ? delete_user_meta( $user_id, $meta_key ) : true;
} else {
$result = update_user_meta( $user_id, $meta_key, $value );
}
$this->log( sprintf( 'Result: %s', var_export( (bool) $result, 1 ) ) );
}
}
/**
* Get the type of New User notification that should be sent (if any).
*
* @since 4.4.3
*
* @param $feed
*
* @return string
*/
public function get_send_email_type( $feed ) {
// If new meta is set, use it.
if ( isset( $feed['meta']['sendEmailEnable'] ) ) {
return (bool) $feed['meta']['sendEmailEnable'] ? rgar( $feed['meta'], 'sendEmailValue' ) : '';
}
// If the user hasn't updated their feed since upgrading to GFUR 4.5, they might still have the deprecated
// Auto Generate Password option selected. If so, treat it the same as if Email Link is selected.
if ( $feed['meta']['password'] === 'generatepass' ) {
return 'default';
}
// If not, default to old meta.
return rgar( $feed['meta'], 'sendEmail' );
}
/**
* Prepare and send WordPress' New User notification email modified according to the given User Registration feed.
*
* @since 4.4.3
*
* @param array $entry Current entry.
* @param array $form Current form.
* @param array $feed Current User Registration feed.
* @param int $user_id ID of the user to/for whom the email should be sent.
*/
public function send_wp_new_user_notification( $entry, $form, $feed, $user_id ) {
$this->log( sprintf( 'Calling wp_new_user_notification() for user id: %s', $user_id ) );
$email_type = $this->get_send_email_type( $feed );
// If Password setting is configured for a field, let's modify the new user notification mesage.
if ( is_numeric( rgars( $feed, 'meta/password' ) ) ) {