-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravityforms-block-email-domains.php
114 lines (104 loc) · 4.17 KB
/
gravityforms-block-email-domains.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
<?php
/**
* Plugin Name: Gravity Forms Block Email Domains
* Plugin URI: http://roadwarriorcreative.com
* Description: Easily set a list of email domains to block on email fields in Gravity Forms.
* Version: 1.0.2
* Author: Road Warrior Creative
* Author URI: https://roadwarriorcreative.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
defined( 'ABSPATH' ) or die( 'No direct file access allowed!' );
/*
Check if Gravity Forms is installed
*/
if ( class_exists( 'GFCommon' ) ) {
/*
Define Custiom Setting
*/
add_action( 'gform_field_advanced_settings', 'rwc_bed_advanced_settings', 10, 2 );
function rwc_bed_advanced_settings( $position, $form_id ) {
// create settings on position 425 (right after visibility)
if ( $position == 425 ) {
?>
<li class="block_email_domains_setting field_setting">
<label for="field_block_email_domains">
<?php esc_html_e( 'Block Email Domains', 'gravityforms' ); ?>
<?php gform_tooltip( 'form_field_block_email_domains' ) ?>
</label>
<input type="text" id="field_block_email_domains" class="fieldwidth-3" onkeyup="SetFieldProperty('block_email_domains', this.value);" />
</li>
<li class="block_email_domains_validation field_setting">
<label for="field_block_email_domains_validation">
<?php esc_html_e( 'Block Email Domains Validation Message', 'gravityforms' ); ?>
<?php gform_tooltip( 'form_field_block_email_domains_validation' ) ?>
</label>
<input type="text" id="field_block_email_domains_validation" class="fieldwidth-3" onkeyup="SetFieldProperty('block_email_domains_validation', this.value);" />
</li>
<?php
}
}
/*
Add Custom Setting to Email Fields
*/
add_action( 'gform_editor_js', 'rwc_bed_editor_script' );
function rwc_bed_editor_script(){
?>
<script type='text/javascript'>
jQuery(document).ready(function($) {
// adding setting to fields of type "email"
fieldSettings.email += ', .block_email_domains_setting';
fieldSettings.email += ', .block_email_domains_validation';
// binding to the load field settings event to initialize the text input
$(document).bind('gform_load_field_settings', function(event, field, form){
$('#field_block_email_domains').val(field.block_email_domains);
$('#field_block_email_domains_validation').val(field.block_email_domains_validation);
});
});
</script>
<?php
}
/*
Custom Setting Tooltip
*/
add_filter( 'gform_tooltips', 'rwc_bed_add_block_domains_tooltips' );
function rwc_bed_add_block_domains_tooltips( $tooltips ) {
$tooltips['form_field_block_email_domains'] = "<h6>Block Email Domains</h6>Add a comma separated list of email domains to block. Example: gmail.com, yahoo.com, outlook.com";
$tooltips['form_field_block_email_domains_validation'] = "<h6>Block Email Domains Validation Message</h6>The message that will show if a blocked email domain is entered.";
return $tooltips;
}
/*
Custom Email Field Validation
*/
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
if ( $field->type == 'email' ) {
// replace white space
$blocked_domains_string = preg_replace('/\s+/', '', strtolower($field["block_email_domains"]));
// convert string to array
$blocked_domains = explode(",", $blocked_domains_string);
// check if value is an array for when a second email confirmation filed is enabled
if(is_array($value)){
$values = $value;
}else{
$values[] = $value;
}
if($values){
foreach ($values as $value) {
$domain = substr(strrchr(strtolower($value), "@"), 1);
if($field["block_email_domains"] && in_array($domain, $blocked_domains)){
$result['is_valid'] = false;
if(!empty($field["block_email_domains_validation"])){
$error_message = $field["block_email_domains_validation"];
}else{
$error_message = 'Sorry, '.$domain.' email addresses are not accepted on this field. Please provide another email address and try again.';
}
$result['message'] = empty( $field->errorMessage ) ? __( $error_message, 'gravityforms' ) : $field->errorMessage;
}
}
}
}
return $result;
}, 10, 4 );
}
?>