Skip to content

Instantly share code, notes, and snippets.

@spivurno
Created September 12, 2012 23:07
Show Gist options
  • Save spivurno/3710653 to your computer and use it in GitHub Desktop.
Save spivurno/3710653 to your computer and use it in GitHub Desktop.
Gravity Wiz // Limit IP to One Submission Per Time Period
<?php
/**
* Limit IP to One Submission Per Time Period
* http://gravitywiz.com/2012/05/12/limit-ip-to-one-submission-per-time-period
*/
$gws_limit_message = 'You may only submit this form once every 24 hours.';
$gws_limit_time = 86400; // must be specified in seconds; 86400 seconds is equal to 24 hours
// update the "5" to your form ID
add_action('gform_pre_render_5', 'gform_limit_ip_submissions');
function gform_limit_ip_submissions($form){
global $gws_limit_message, $gws_limit_time;
if(!gform_has_ip_exceeded_limit($form, $gws_limit_time))
return $form;
$submission_info = rgar(GFFormDisplay::$submission, $form['id']);
// if no submission, hide form
// if submission and not valid, hide form
if(!$submission_info || !rgar($submission_info, 'is_valid')) {
add_filter('gform_get_form_filter', create_function('', "return '<div class=\"limit-message\">$gws_limit_message</div>';") );
}
return $form;
}
add_filter('gform_validation_5', 'gform_validate_limit_ip_submissions');
function gform_validate_limit_ip_submissions($validation_result) {
global $gws_limit_message, $gws_limit_time;
if(gform_has_ip_exceeded_limit($validation_result['form'], $gws_limit_time))
$validation_result['is_valid'] = false;
return $validation_result;
}
function gform_has_ip_exceeded_limit($form, $limit_time) {
global $wpdb;
$current_ip = RGFormsModel::get_ip();
$last_submission = $wpdb->get_var($wpdb->prepare("SELECT date_created FROM {$wpdb->prefix}rg_lead WHERE ip = %s and form_id = %d ORDER BY date_created DESC", $current_ip, $form['id']));
if(empty($last_submission))
return false;
$time_out = strtotime($last_submission) + $limit_time;
$current_time = time();
return $current_time > $time_out ? false : true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment