Quantcast
Channel: Snippets Archives - Gravity Wiz
Viewing all articles
Browse latest Browse all 196

Better Limit Submission Per Time Period by User or IP

$
0
0
This snippet allows you to limit submissions to any (or all) Gravity Forms per a time period (i.e. 30 minutes, 24 hours, 2 days, etc) by a WP User ID, by the submitting users IP adddress, or from a specific embed URL.

This snippet replaces both the Limit User to One Submission Per Time Period and the Limit IP to One Submission Per Time Period snippets.

This snippet was updated on January 15th, 2012 to provide support for limiting by the embed URL of the form and also specifying multiple “limit_by” parameters. Big thanks to Lee for funding this enhancement!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<?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.'
));
view raw gistfile1.php This Gist brought to you by GitHub.

How do I install this snippet?

Easy peasy. Just copy and paste the code above into your theme's functions.php file.

How to I use this functionality?

All you need to do is create a new instance of the GWSubmissionLimit() class, populated with your specific details, like so:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
           
           
           
new GWSubmissionLimit(array(
'form_id' => 86,
'limit' => 2,
));
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
         
view raw gistfile1.php This Gist brought to you by GitHub.

Want to apply the same submission limit to all forms?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
new GWSubmissionLimit(array(
'limit' => 5
));
           
           
           
           
           
           
           
         
view raw gistfile1.php This Gist brought to you by GitHub.

Want to apply multiple limitations? This example allows you to limit the number of a submissions a logged in user can make to specific form from the same embed URL. This would allow you embed the same form on multiple pages and allow users to submit that form up to the submission limit on each page.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
new GWSubmissionLimit(array(
'form_id' => 2,
'limit' => 1,
'limit_message' => 'Aha! You have been limited.',
'limit_by' => array('embed_url', 'user_id')
));
view raw gistfile1.php This Gist brought to you by GitHub.

Here is a full list of available options:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
           
           
           
           
           
           
           
           
           
new GWSubmissionLimit(array(
'form_id' => 2,
'limit' => 5,
'time_period' => 60 * 60 * 24 * 2, // two days in seconds
'limit_message' => 'You are all maxed out! Try again in a couple days.',
'limit_by' => 'ip', // default is 'ip', also supports 'user_id' and 'embed_url'; can alternately provide an array of limitations: array('ip', 'embed_url')
));
           
           
           
           
           
           
           
           
           
           
           
           
         
view raw gistfile1.php This Gist brought to you by GitHub.
  • form_id The form ID of the form you would like to limit. If you want to apply the the same submission limit to all forms, set the form_id as false or do not provide it at all.
  • limit The number of submissions allowed before the user will no longer be able to make additional submission.
  • time_period The period of time to which the limit applies. The default time period is one day. In any 24 hour period, if the user reaches the limit they will no longer be able to make new submissions.
  • limit_message The message which should be displayed to the user once they have reached the specified submission limit.
  • limit_by Specify which identifying data the user should be limited by. The default is ip. user_id is also supported which allows you to limit logged in WP users. embed_url is now supported to limit submissions of a form from a specific embed URL.

Viewing all articles
Browse latest Browse all 196

Latest Images

Trending Articles



Latest Images