spivurno
8/24/2013 - 1:23 PM

MOD: Better Limit Submissions Per Time Period by User or IP (without hiding form)

MOD: Better Limit Submissions Per Time Period by User or IP (without hiding form)

<?php

/**
* MOD: Better Limit Submissions Per Time Period by User or IP (without hiding form)
* http://gravitywiz.com/better-limit-submission-per-time-period-by-user-or-ip/
* http://screencast.com/t/I11vWq02Qswn
*/
    
class GWSubmissionLimit_YvesSandyck extends GWSubmissionLimit {
    
    function __construct( $args ) {
        
        parent::__construct( $args );
        
        $form_filter = $this->_args['form_id'] ? "_{$this->_args['form_id']}" : '';
        remove_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( ! $submission_info || ! rgar( $submission_info, 'is_valid' ) )
            add_filter( 'gform_get_form_filter', create_function( '$form_string', 'return \'<div class="limit-message">' . $this->_args['limit_message'] . '</div>\' . $form_string;' ) );
         
        return $form;
    }
    
}

new GWSubmissionLimit_YvesSandyck( array(
    'form_id' => 327,
    'limit' => 1,
    'time_period' => 60 * 60 * 24 * 1,
    'limit_message' => 'You are all maxed out! Try again in a couple days.',
    'limit_by' => 'user_id',
    ) );