Gravity Perks // GP Limit Choices // Limit Per User
<?php
/**
* Gravity Perks // GP Limit Choices // Limit Per User
*
* By default, GP Limit Choices limits apply to all users. Use this snippet to
* apply limits per user.
*
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravityperks.com
* @copyright 2015 Gravity Wiz
*/
class GWLimitChoicesPerUser {
function __construct( $args ) {
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_ids' => array()
) );
add_filter( "gwlc_choice_counts_query_{$this->_args['form_id']}", array( $this, 'modify_choice_counts_query' ), 10, 2 );
}
function modify_choice_counts_query( $query, $field ) {
global $wpdb;
if( ! is_user_logged_in() ) {
return $query;
}
$form = GFAPI::get_form( $field['formId'] );
foreach( $form['fields'] as $field ) {
if( empty( $this->_args['field_ids'] ) || in_array( $field['id'], $this->_args['field_ids'] ) ) {
$per_user_sql = $wpdb->prepare( 'created_by = %d', get_current_user_id() );
$query['where'] .= ' AND ' . $per_user_sql;
}
}
return $query;
}
}
new GWLimitChoicesPerUser( array(
'form_id' => 436,
'field_ids' => array( 7 )
) );