Last active
June 3, 2024 12:05
-
-
Save zakirsajib/81182f55586c2cee2bf4b142a95a1784 to your computer and use it in GitHub Desktop.
Non USA country restriction on Gravity Form submission
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
// Added on 06/03/23 by Zakir@globerunner.com | |
add_action('init', 'add_ipinfo_api_key_option'); | |
function add_ipinfo_api_key_option() { | |
if (get_option('ipinfo_api_key') === false) { | |
add_option('ipinfo_api_key', 'XXXXXXXX'); | |
} | |
} | |
// Add an action hook for handling the AJAX request | |
add_action('wp_ajax_get_user_location', 'get_user_location'); | |
add_action('wp_ajax_nopriv_get_user_location', 'get_user_location'); // Allow non-logged-in users to use this AJAX action | |
function get_user_location() { | |
$ipAddress = get_client_ip(); | |
$apiToken = get_option('ipinfo_api_key'); // Retrieve API key from wp_options | |
$locationInfo = get_location_info($ipAddress, $apiToken); | |
$userCountry = isset($locationInfo['country']) ? $locationInfo['country'] : 'Unknown'; | |
$response = array( | |
'userCountry' => $userCountry, | |
); | |
wp_send_json($response); | |
wp_die(); | |
} | |
// Function to get the client's IP address | |
function get_client_ip() { | |
$ipaddress = ''; | |
if (isset($_SERVER['HTTP_CLIENT_IP'])) | |
$ipaddress = $_SERVER['HTTP_CLIENT_IP']; | |
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) | |
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
else if(isset($_SERVER['HTTP_X_FORWARDED'])) | |
$ipaddress = $_SERVER['HTTP_X_FORWARDED']; | |
else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) | |
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; | |
else if(isset($_SERVER['HTTP_FORWARDED'])) | |
$ipaddress = $_SERVER['HTTP_FORWARDED']; | |
else if(isset($_SERVER['REMOTE_ADDR'])) | |
$ipaddress = $_SERVER['REMOTE_ADDR']; | |
else | |
$ipaddress = 'UNKNOWN'; | |
return $ipaddress; | |
} | |
// Function to get location information using ipinfo.io | |
function get_location_info($ip, $token) { | |
$url = "http://ipinfo.io/{$ip}?token={$token}"; | |
$response = wp_remote_get($url); | |
if (is_wp_error($response)) { | |
return array(); | |
} | |
$body = wp_remote_retrieve_body($response); | |
return json_decode($body, true); | |
} | |
add_filter('gform_validation_3', 'restrict_form_submission_to_us'); | |
add_filter('gform_validation_6', 'restrict_form_submission_to_us'); | |
function restrict_form_submission_to_us($validation_result) { | |
$ipAddress = get_client_ip(); | |
$apiToken = get_option('ipinfo_api_key'); // Retrieve API key from wp_options | |
$locationInfo = get_location_info($ipAddress, $apiToken); | |
//error_log('IP Address: ' . $ipAddress); | |
//error_log('Location Info: ' . print_r($locationInfo, true)); | |
$userCountry = isset($locationInfo['country']) ? $locationInfo['country'] : 'Unknown'; | |
if ($userCountry !== 'US') { | |
$validation_result['is_valid'] = false; | |
// Attach the validation message to a field or form error message | |
foreach ($validation_result['form']['fields'] as &$field) { | |
// Attach to the first field with a validation message | |
if ($field->type !== 'hidden') { // Target the first non-hidden field | |
$field->failed_validation = true; | |
$field->validation_message = 'Form submission is restricted to users in the United States only.'; | |
break; | |
} | |
} | |
// Add a form-wide error message | |
add_filter('gform_validation_message_' . $validation_result['form']['id'], function($message, $form) { | |
return '<div class="validation_error">Form submission is restricted to users in the United States only.</div>'; | |
}, 10, 2); | |
} | |
return $validation_result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment