Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alpha1/c71891fbee4422148a19789bc2c52adc to your computer and use it in GitHub Desktop.
Save alpha1/c71891fbee4422148a19789bc2c52adc to your computer and use it in GitHub Desktop.
Gravity Forms Sane Validation Rules
<?php
/*
Plugin Name: WP Hacks Gravity Forms Validation Rules
Description:
Author: WP Hacks
Version: 0.1.0
Author URI: https://wphacks.org
*/
/*
TODO:
- Stop all feeds when email is of a certain domain, or TLD
- Assign a spam score to meta data before feeds process
- add meta data to confirmations/notifications conditions
*/
/*
Proper case names when submitted in app caps
*/
add_filter( 'gform_save_field_value', 'wphacks_gravity_forms_propercase_all_caps_names', 1, 4);
function wphacks_gravity_forms_propercase_all_caps_names( $value, $entry, $field, $form, $input_id ){
if($field->type == "name" ){
$first = trim( rgar( $value, $field->id . '.3' ) );
$last = trim( rgar( $value, $field->id . '.6' ) );
if( ctype_upper( $first ) ){
$first = ucwords($first);
}
if( ctype_upper( $last ) ){
$first = ucwords($first);
}
//TODO
}
return $value;
}
/*
lowercase all email addresses
*/
add_filter( 'gform_save_field_value', 'wphacks_gravity_forms_lowercase_all_emails', 10, 5 );
function wphacks_gravity_forms_lowercase_all_emails( $value, $entry, $field, $form, $input_id ){
if($field->type == "email" ){
$value = trim( strtolower( $value ) );
}
return $value;
}
/*
generic function to disallow values from a field.
*/
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_disallowed_values', 10, 4 );
function wphacks_gravity_forms_disallowed_values( $result, $value, $form, $field ){
$error = array( 'is_valid' => 0, 'message' => "Providing invalid information will get your registration cancelled without notice.");
if(in_array($field->type, array("text") ) ){
$value = trim( strtolower( $value ) );
$disallowed_values = array(
'n/a',
'na',
);
if(in_array($value, $disallowed_values)){
return $error;
}
}
return $result;
}
/*
ban certain domains from a website field, for front end validations
*/
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_disallow_domains_in_website_field', 10, 5 );
function wphacks_gravity_forms_disallow_domains_in_website_field( $result, $value, $form, $field ){
if(in_array($field->type, array("website") ) ){
$domain = $value; //TODO
$bad_websites = array(
'example.com',
'nobody.com',
);
if(in_array($domain, $bad_websites)){
return $error;
}
}
return $result;
}
/*
prevent the same first and last name
*/
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_prevent_same_name', 1, 4);
function wphacks_gravity_forms_prevent_same_name( $result, $value, $form, $field ){
if($field->type == "name" ){
$error = array( 'is_valid' => 0, 'message' => 'First Name and Last Name Cannot be the same.' );
$first = trim( rgar( $value, $field->id . '.3' ) );
$last = trim( rgar( $value, $field->id . '.6' ) );
if( ($first === $last ) || strtolower( $first ) === strtolower( $last ) ){
$error = array( 'is_valid' => 0, 'message' => 'First and last name cannot be the same' );
return $error;
}
}
return $result;
}
/*
bans certain email addresses
*/
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_disallow_emails', 10, 5 );
function wphacks_gravity_forms_disallow_emails( $result, $value, $form, $field ){
if($field->type == "email" ){
$bad_emails = array(
'[email protected]',
'[email protected]',
);
if( in_array( strtolower( $value ), $bad_emails ) ){
$error = array( 'is_valid' => 0, 'message' => 'This email is now allowed' );
return $error;
}
}
return $result;
}
/*
bans certain domains addresses
*/
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_disallow_domains', 10, 5 );
function wphacks_gravity_forms_disallow_domains( $result, $value, $form, $field ){
if($field->type == "email" ){
$bad_domains = array(
'example.com',
'nobody.com',
);
$email_array = explode( "@", $value );
if( in_array( strtolower( end( $email_array ) ), $bad_domains ) ){
$error = array( 'is_valid' => 0, 'message' => 'This domain is not allowed' );
return $error;
}
}
return $result;
}
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_disallow_tlds', 10, 5 );
function wphacks_gravity_forms_disallow_tlds( $result, $value, $form, $field ){
if($field->type == "email" ){
$email_array = explode( "@", $value );
$email_domain_array = explode( ".", end( $email_array ) );
$tld = end( $email_domain_array );
$bad_tlds = array(
'ru',
'hk',
);
if( in_array( strtolower( $tld ), $bad_tlds ) ){
$error = array( 'is_valid' => 0, 'message' => 'This TLD ('. $tld .' is not allowed' );
return $error;
}
}
return $result;
}
//add_filter( 'gform_field_validation', 'wphacks_gravity_forms_check_if_domain_exists', 10000, 4);
function wphacks_gravity_forms_check_if_domain_exists( $result, $value, $form, $field ){
if($result['is_valid'] == 0){
if($field->type == "email" ){
$email_array = explode( "@", $value );
$dns_result = checkdnsrr( end( $email_array ), 'MX' );
if( !$dns_result ){
$error = array( 'is_valid' => 0, 'message' => 'Domain does not exist' );
return $error;
}
}
}
}
/*
forces the name field to be longer than 1 character
*/
add_filter( 'gform_field_validation', 'wphacks_gravity_forms_name_longer_than_one_char', 1, 4);
function wphacks_gravity_forms_name_longer_than_one_char( $result, $value, $form, $field ){
if($field->type == "name" ){
$first = rgar( trim( $value ) , $field->id . '.3' );
$last = rgar( trim( $value ), $field->id . '.6' );
if( strlen( $first ) == 1 ){
$error = array( 'is_valid' => 0, 'message' => 'First Name must be more than one characters' );
return $error;
}
if( strlen( $last ) == 1 ){
$error = array( 'is_valid' => 0, 'message' => 'Last Name must be more than one characters' );
return $error;
}
}
return $result;
}
/*
prevent many fake phone numbers
*/
add_filter( 'gform_field_validation_212_12', 'wphacks_gravity_forms_prevent_fake_phone_numbers', 1, 4);
function wphacks_gravity_forms_prevent_fake_phone_numbers( $result, $value, $form, $field ){
$error = array( 'is_valid' => 0, 'message' => 'Please use a real phone number' );
if ( $field->type == 'phone' || ( $field->type == 'text' && $field->inputMask == 1 && $field->inputMaskValue == '(999) 999-9999? x99999') ){
$numbers_only = implode("", array_filter(str_split($value),function($array){ return is_numeric($array); }));
$bad_phone_numbers = array(
'0111111111',
'0001234567',
'1234567890',
'0123456789',
'5555551212',
'1231231234',
'1234151234',
'1234512345',
'1231234567',
'5671237890',
);
if(in_array($numbers_only, $bad_phone_numbers)){
return $error;
}
if( in_array(substr( $numbers_only, 0, 10), $bad_phone_numbers ) ){
return $error;
}
if( substr( $numbers_only, 3, 7) == 8675309 ){
$error = array( 'is_valid' => 0, 'message' => "Please enter your number, not Jenny's number." );
return $error;
}
if( strlen( $numbers_only === 10 ) && in_array( substr( $numbers_only, 3, 3 ), array( 555 ) ) ){
return $error;
}
//Is the phone number using an un-used area codes in north america
//Up to date as of 6/15/2022 with 2023 planned expansions
//Source: https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Assignment_activities_by_year
//TODO: check for +1
if( in_array( substr( $numbers_only, 0, 3 ), array (200, 211, 221, 222, 233, 233, 235, 237, 238, 241, 243, 255, 245, 247, 257, 258, 259, 265, 271, 273, 275, 280, 282, 285, 286, 287, 300, 322, 324, 328, 333, 335, 342, 344, 348, 349, 350, 355, 356, 357, 358, 359, 262, 366, 381, 383, 384, 388, 389, 390, 391, 392, 393, 394, 395, 395, 397, 398, 399, 400, 420, 421, 422, 426, 427, 429, 433, 436, 439, 444, 446, 449, 451, 454, 455, 457, 459, 460, 461, 462, 465, 466, 467, 471, 476, 477, 481, 482, 483, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 536, 537, 565, 568, 576, 583, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599,621, 624, 625, 632, 637, 638, 642, 643, 645, 648, 652, 653, 654, 663, 665, 666, 668, 673, 676, 685, 686, 687, 722, 723, 728, 729, 733, 738, 739, 741, 744, 745, 746, 748, 751, 752, 755, 759, 761, 766, 768, 776, 777, 783, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 821, 823, 824, 827, 834, 836, 837, 841, 842, 846, 851, 852, 853, 871, 874, 875, 880, 881, 882, 883, 884, 885, 886, 887, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 921, 922, 923, 924, 926, 933, 942, 944, 946, 953, 955, 957, 958,974, 976, 977, 981, 982, 987, 988, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999 ) ) ){
return $error;
}
//if the entire array is the same number
if(count(array_unique(array_filter(str_split($numbers_only),function($array){return is_numeric($array);}))) === 1){
return $error;
}
}
return $result;
}
add_filter( 'gform_entry_is_spam', 'mark_as_spam_banned_domains_on_website_fields', 10, 3 );
function mark_as_spam_banned_domains_on_website_fields( $is_spam, $form, $entry ) {
if( $is_spam ){
return $is_spam;
}
$banned_domains = array(
'tinyurl.com',
'thetranny',
'pornhub.com',
'yandex.ru'
);
if( isset( $form['fields'] ) ){
foreach( $form['fields'] as $field ){
if( "website" == $field->type ){
$website = rgar( $entry, $field->id);
$parsed_url = parse_url( $website );
print_r( $parsed_url );
echo $parsed_url['host'];
if(in_array( $parsed_url['host'], $banned_domains)){
return true;
}
}
}
}
return $is_spam;
}
add_filter( 'gform_entry_is_spam', 'mark_as_spam_banned_tlds_on_website_fields', 10, 3 );
function mark_as_spam_banned_tlds_on_website_fields( $is_spam, $form, $entry ) {
if( $is_spam ){
return $is_spam;
}
$banned_tlds = array(
'ru',
'ch',
'kp',
'kg',
'pk',
'ph'
);
if( isset( $form['fields'] ) ){
foreach( $form['fields'] as $field ){
if( $field->type == "website"){
$website = rgar( $entry, $field->id);
if( !empty( $website ) ){
$parsed_url = parse_url( $website );
foreach($banned_tlds as $banned_tld){
$length = strlen( $banned_tld );
if( !$length ) {
return $is_spam;
}
if( substr( $parsed_url['host'], -$length ) === $banned_tld ){
return true;
}
}
}
}
}
}
return $is_spam;
}
add_filter( 'gform_entry_is_spam', 'mark_as_spam_banned_tlds_on_email_fields', 10, 3 );
function mark_as_spam_banned_tlds_on_email_fields( $is_spam, $form, $entry ) {
if( $is_spam ){
return $is_spam;
}
$banned_tlds = array(
'ru',
'ch',
'kp',
'kg',
'pk',
'ph'
);
if( isset( $form['fields'] ) ){
foreach( $form['fields'] as $field ){
if( $field->type == "email"){
$email = rgar( $entry, $field->id);
$email_domain = array_pop( explode( '@', $email ) );
foreach( $banned_tlds as $banned_tld ){
$length = strlen( $banned_tld );
if( !$length ) {
return $is_spam;
}
if( substr( $email_domain, -$length ) === $banned_tld ){
return true;
}
}
}
}
}
return $is_spam;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment