spivurno
8/29/2013 - 12:05 PM

Gravity Wiz // Limit Form Creation By Role

Gravity Wiz // Limit Form Creation By Role

<?php
/**
* Limit Form Creation By Role
* http://gravitywiz.com/
* https://gist.github.com/spivurno/6377212
* 
* Limit the number of forms that can be created collectively by a specific role.
*/

class GWLimitFormCreationByRole {
    
    function __construct( $args ) {
        
        $args = wp_parse_args( $args, array( 
            'limits' => array(),
            'limit_message' => 'You\'ve created as many forms as you\'re allowed.'
            ) );
        
        $this->options = $args;
        
        // handle saving new forms
        add_action( 'wp_ajax_gf_save_new_form', array( $this, 'save_new_form' ), 1 );
        add_action( 'gform_new_form_button', array( $this, 'new_form_button' ) );
        add_action( 'gform_after_save_form', array( $this, 'save_user_role' ), 10, 2 );
        
        // handle duplicating existing forms
        add_action( 'admin_init', array( $this, 'maybe_prevent_duplicating_form' ) );
        add_filter( 'gform_form_actions', array( $this, 'maybe_remove_duplicate_action' ) );
        
    }
    
    function save_new_form() {
        if( $this->role_limit_reached() )
            die( json_encode( array( 'error' => $this->options['limit_message'] ) ) );
    }
    
    function maybe_prevent_duplicating_form() {
        if( GFForms::get_page() == 'form_list' && rgpost( 'action' ) == 'duplicate' && $this->role_limit_reached() )
            die( $this->options['limit_message'] );
    }
    
    function maybe_remove_duplicate_action( $actions ) {
        
        if( $this->role_limit_reached() )
            unset( $actions['duplicate'] );
                
        return $actions;
    }
    
    function new_form_button( $button ) {
        return ! $this->role_limit_reached() ? $button : '<div style="background-color: #FFEBE8;border: 1px solid #CC0000;padding:0.6em;border-radius:4px;">' . 
            $this->options['limit_message'] . '</div>';
    }
    
    function role_limit_reached() {
        
        $role = $this->get_user_role();
        if( !$role )
            return false;
        
        $role_limit = $this->get_role_limit( $role );
        $role_count = $this->get_role_form_count( $role );
        
        if( $role_limit === 0 )
            return true;
        
        return $role_limit && $role_limit <= $role_count;
    }
    
    function get_user_role() {
        
        $user = wp_get_current_user();

        // if user has no roles, let's bail out
        if( empty( $user->roles ) || ! is_array( $user->roles ) )
            return;
        
        return $user->roles[0];
    }
    
    function get_role_limit( $role ) {
        return rgar( $this->options['limits'], $role );
    }
    
    function get_role_form_count( $role ) {
        
        $role_form_counts = get_option( 'gwlfcbr_role_form_counts' );
        $role_form_count = rgar( $role_form_counts, $role );
        
        return $role_form_count ? $role_form_count : 0;
    }
    
    function save_user_role( $form_meta, $is_new ) {
        
        if( !$is_new )
            return;
        
        $role = $this->get_user_role();
        if( !$role )
            return;
            
        $role_form_counts = get_option( 'gwlfcbr_role_form_counts' );
        $role_form_count = rgar( $role_form_counts, $role );
        
        if( !$role_form_count )
            $role_form_count = 0;
        
        $role_form_count++;
        $role_form_counts[$role] = $role_form_count;
        
        update_option( 'gwlfcbr_role_form_counts', $role_form_counts );
        
    }
    
}

new GWLimitFormCreationByRole( array(
    'limits' => array( 
        'administrator' => 2, 
        'subscriber' => 2 
        ),
    'limit_message' => 'You have exceeded your form limit.'
    ) );