Created
June 2, 2021 23:40
-
-
Save mattneal-stafflink/692d104e84a2892ed6fac9d556273888 to your computer and use it in GitHub Desktop.
Restrict email addresses on Gravity forms register sign up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* | |
* Include a list of valid domains for Gravity Form Email fields. | |
* | |
* @version 1.4 | |
* @author Matt Neal <[email protected]> | |
* @license GPL-2.0+ | |
*/ | |
class sl_Email_Domain_Validator { | |
private $_args; | |
function __construct($args) { | |
$this->_args = wp_parse_args( $args, [ | |
'form_id' => false, | |
'field_id' => false, | |
'domains' => false, | |
'validation_message' => __( 'Sorry, <strong>%s</strong> email accounts are not eligible for this form.' ), | |
'mode' => 'ban' // also accepts "limit" | |
] ); | |
// convert field ID to an array for consistency, it can be passed as an array or a single ID | |
if($this->_args['field_id'] && !is_array($this->_args['field_id'])) | |
$this->_args['field_id'] = array($this->_args['field_id']); | |
$form_filter = $this->_args['form_id'] ? "_{$this->_args['form_id']}" : ''; | |
add_filter("gform_validation{$form_filter}", array($this, 'validate')); | |
} | |
function validate($validation_result) { | |
$form = $validation_result['form']; | |
foreach($form['fields'] as &$field) { | |
// if this is not an email field, skip | |
if(RGFormsModel::get_input_type($field) != 'email') | |
continue; | |
// if field ID was passed and current field is not in that array, skip | |
if($this->_args['field_id'] && !in_array($field['id'], $this->_args['field_id'])) | |
continue; | |
$page_number = GFFormDisplay::get_source_page( $form['id'] ); | |
if( $page_number > 0 && $field->pageNumber != $page_number ) { | |
continue; | |
} | |
if( GFFormsModel::is_field_hidden( $form, $field, array() ) ) { | |
continue; | |
} | |
$domain = $this->get_email_domain($field); | |
// if domain is valid OR if the email field is empty, skip | |
if($this->is_domain_valid($domain) || empty($domain)) | |
continue; | |
$validation_result['is_valid'] = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = sprintf($this->_args['validation_message'], $domain); | |
} | |
$validation_result['form'] = $form; | |
return $validation_result; | |
} | |
function get_email_domain( $field ) { | |
$email = explode( '@', rgpost( "input_{$field['id']}" ) ); | |
return trim( rgar( $email, 1 ) ); | |
} | |
function is_domain_valid( $domain ) { | |
$mode = $this->_args['mode']; | |
$domain = strtolower( $domain ); | |
foreach( $this->_args['domains'] as $_domain ) { | |
$_domain = strtolower( $_domain ); | |
$full_match = $domain == $_domain; | |
$suffix_match = strpos( $_domain, '.' ) === 0 && $this->str_ends_with( $domain, $_domain ); | |
$has_match = $full_match || $suffix_match; | |
if( $mode == 'ban' && $has_match ) { | |
return false; | |
} else if( $mode == 'limit' && $has_match ) { | |
return true; | |
} | |
} | |
return $mode == 'limit' ? false : true; | |
} | |
function str_ends_with( $string, $text ) { | |
$length = strlen( $string ); | |
$text_length = strlen( $text ); | |
if( $text_length > $length ) { | |
return false; | |
} | |
return substr_compare( $string, $text, $length - $text_length, $text_length ) === 0; | |
} | |
} | |
/* | |
* Valid email addresses and options for validation. | |
*/ | |
class lsEmailDomainControl extends sl_Email_Domain_Validator { } | |
new sl_Email_Domain_Validator( [ | |
'form_id' => 1, | |
'field_id' => 2, | |
'domains' => ['nitschke.com.au', 'stafflink.com.au'], | |
'validation_message' => __( 'Sorry! <strong>%s</strong> email accounts are not eligible for this form. Please use a valid email address.' ), | |
'mode' => 'limit' | |
] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment