Forked from spivurno/gw-gravity-forms-submission-limit.php
Created
August 15, 2013 14:12
-
-
Save jevgen/6241121 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Better Limit Submissions Per Time Period by User or IP | |
* http://gravitywiz.com/2012/05/12/limit-ip-to-one-submission-per-time-period | |
*/ | |
class GWSubmissionLimit { | |
private $_args; | |
function __construct($args) { | |
$this->_args = wp_parse_args($args, array( | |
'form_id' => false, | |
'limit' => 1, | |
'time_period' => 60 * 60 * 24, // must be provided in seconds: 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day | |
'limit_message' => __('Sorry, you have reached the submission limit for this form.'), | |
'limit_by' => 'ip' // also accepts "user_id" | |
)); | |
$form_filter = $this->_args['form_id'] ? "_{$this->_args['form_id']}" : ''; | |
add_filter("gform_pre_render{$form_filter}", array($this, 'pre_render')); | |
add_filter("gform_validation{$form_filter}", array($this, 'validate')); | |
} | |
function pre_render($form) { | |
if( !$this->is_limit_reached($form['id']) ) | |
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\">{$this->_args['limit_message']}</div>';") ); | |
//add_filter('gform_get_form_filter', create_function('$form_string, $form', 'return $form["id"] == ' . $form['id'] . ' ? \'<div class=\"limit-message\">' . $this->_args['limit_message'] . '</div>\' : $form_string;'), 10, 2 ); | |
} | |
return $form; | |
} | |
function validate($validation_result) { | |
if($this->is_limit_reached($validation_result['form']['id'])) | |
$validation_result['is_valid'] = false; | |
return $validation_result; | |
} | |
public function is_limit_reached($form_id) { | |
global $wpdb; | |
$limit_by = is_array($this->_args['limit_by']) ? $this->_args['limit_by'] : array($this->_args['limit_by']); | |
$where = array(); | |
foreach($limit_by as $limiter) { | |
switch($limiter) { | |
case 'user_id': | |
$where[] = $wpdb->prepare('created_by = %s', get_current_user_id()); | |
break; | |
case 'embed_url': | |
$where[] = $wpdb->prepare('source_url = %s', RGFormsModel::get_current_page_url()); | |
break; | |
default: | |
$where[] = $wpdb->prepare('ip = %s', RGFormsModel::get_ip()); | |
} | |
} | |
$where[] = $wpdb->prepare('form_id = %d', $form_id); | |
$where[] = $wpdb->prepare('date_created BETWEEN DATE_SUB(utc_timestamp(), INTERVAL %d SECOND) AND utc_timestamp()', $this->_args['time_period']); | |
$where = implode(' AND ', $where); | |
$sql = "SELECT count(id) | |
FROM {$wpdb->prefix}rg_lead | |
WHERE $where"; | |
$entry_count = $wpdb->get_var($sql); | |
return $entry_count >= $this->_args['limit']; | |
} | |
} | |
// standard usage | |
new GWSubmissionLimit(array( | |
'form_id' => 86, | |
'limit' => 2, | |
'limit_message' => 'Aha! You have been limited.' | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment