Skip Registration if Email Exists
If submitted email is already registered, skip registration.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gw-user-registration-skip-registration-for-existing-email.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
<?php
/**
* Gravity Wiz // GF User Registration // Skip Registration if Email Exists
* https://gravitywiz.com/
*
* Skip registration if the submitted email address, is already registered with a user account.
*
* Plugin Name: Gravity Forms - Skip Registration if Email Exists
* Plugin URI: https://gravitywiz.com/
* Description: If submitted email is already registered, skip registration.
* Author: Gravity Wiz
* Version: 0.6
* Author URI: https://gravitywiz.com/
*/
class GW_GFUR_Skip_Registration_If_Email_Exists {
private static $instance = null;
private $_args = array();
public static function get_instance( $args ) {
if ( null == self::$instance ) {
self::$instance = new self( $args );
}
return self::$instance;
}
private function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_ids' => array(),
'exclude_form_ids' => array(),
) );
add_filter( 'gform_is_delayed_pre_process_feed', array( $this, 'maybe_delay_feeds' ), 10, 4 );
add_action( 'gform_pre_process', array( $this, 'maybe_skip_user_registration_validation' ) );
add_action( 'gform_validation', array( $this, 'ignore_email_validation' ) );
}
public function maybe_delay_feeds( $is_delayed, $form, $entry, $slug ) {
if ( $slug == 'gravityformsuserregistration' && $this->is_applicable_form( $form ) ) {
if ( ( $this->does_feed_email_exist( $form, $entry ) || ! $this->is_email_valid( $form, $entry ) ) ) {
return true;
}
}
return $is_delayed;
}
public function maybe_skip_user_registration_validation( $form ) {
if ( function_exists( 'gf_user_registration' ) && $this->is_applicable_form( $form ) ) {
remove_filter( 'gform_validation', array( gf_user_registration(), 'validate' ) );
}
return $form;
}
public function does_feed_email_exist( $form, $entry = false ) {
$email = $this->get_email_by_feed( $form, $entry );
return get_user_by( 'email', $email ) !== false;
}
public function is_email_valid( $form, $entry = false ) {
$email = $this->get_email_by_feed( $form, $entry );
return ! rgblank( $email ) && GFCommon::is_valid_email( $email );
}
public function get_email_by_feed( $form, $entry = false ) {
if ( $entry == false ) {
$entry = GFFormsModel::get_current_lead();
}
$feed = gf_user_registration()->get_single_submission_feed( $entry, $form );
if ( empty( $feed ) ) {
return false;
}
$email_field_id = rgars( $feed, 'meta/email' );
$email = rgar( $entry, $email_field_id );
return $email;
}
public function ignore_email_validation( $validation_result ) {
// invalid emails should only be ignored for users who are registering; logged in users should not be able to
// enter an incorrect email
if ( is_user_logged_in() ) {
return $validation_result;
}
$has_validation_error = false;
foreach ( $validation_result['form']['fields'] as &$field ) {
if ( $field['validation_message'] == esc_html__( 'Please enter a valid email address.', 'gravityforms' ) ) {
$field['failed_validation'] = false;
}
if ( $field['failed_validation'] ) {
$has_validation_error = true;
}
}
$validation_result['is_valid'] = ! $has_validation_error;
return $validation_result;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
if ( ! empty( $this->_args['exclude_form_ids'] ) ) {
return ! in_array( $form_id, $this->_args['exclude_form_ids'] );
} elseif ( ! empty( $this->_args['form_ids'] ) ) {
return in_array( $form_id, $this->_args['form_ids'] );
}
return true;
}
}
function gw_gfur_skip_registration_if_email_exists( $args = array() ) {
return GW_GFUR_Skip_Registration_If_Email_Exists::get_instance( $args );
}
// Default configuration; applies to all applicable forms automatically.
gw_gfur_skip_registration_if_email_exists();
// Use this to exclude forms from this snippet.
// gw_gfur_skip_registration_if_email_exists( array(
// 'exclude_form_ids' => array( 1, 2, 3 )
// ) );