Created
October 2, 2015 14:47
-
-
Save mauriciogofas/8af815fe97d71ba3999a to your computer and use it in GitHub Desktop.
//Gravity forms - limit entries
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
//Gravity forms - limite de entradas | |
class GWLimitBySum { | |
private $_args; | |
function __construct($args) { | |
$this->_args = wp_parse_args($args, array( | |
'form_id' => false, | |
'field_id' => false, | |
'limit' => 20, | |
'limit_message' => __('Sorry, this item is sold out.'), | |
'validation_message' => __('You ordered %1$s of this item. There are only %2$s of this item left.'), | |
'approved_payments_only' => false, | |
'hide_form' => false | |
)); | |
$this->_args['input_id'] = $this->_args['field_id']; | |
extract($this->_args); | |
add_filter("gform_pre_render_$form_id", array(&$this, 'limit_by_field_values')); | |
add_filter("gform_validation_$form_id", array(&$this, 'limit_by_field_values_validation')); | |
// add 'sum' action for [gravityforms] shortcode | |
add_filter( 'gform_shortcode_sum', array( $this, 'shortcode_sum' ), 10, 2 ); | |
add_filter( 'gform_shortcode_remaining', array( $this, 'shortcode_remaining' ), 10, 2 ); | |
if($approved_payments_only) | |
add_filter('gwlimitbysum_query', array(&$this, 'limit_by_approved_only')); | |
} | |
public function limit_by_field_values($form) { | |
$sum = self::get_field_values_sum($form['id'], $this->_args['input_id']); | |
if($sum < $this->_args['limit']) | |
return $form; | |
if($this->_args['hide_form']) { | |
add_filter('gform_get_form_filter', create_function('', 'return "' . $this->_args['limit_message'] . '";')); | |
} else { | |
add_filter('gform_field_input', array(&$this, 'hide_field'), 10, 2); | |
} | |
return $form; | |
} | |
public function limit_by_field_values_validation($validation_result) { | |
extract($this->_args); | |
$form = $validation_result['form']; | |
$exceeded_limit = false; | |
foreach($form['fields'] as &$field) { | |
if($field['id'] != intval($input_id)) | |
continue; | |
$requested_value = rgpost("input_" . str_replace('.', '_', $input_id)); | |
$field_sum = self::get_field_values_sum($form['id'], $input_id); | |
if($field_sum + $requested_value <= $limit) | |
continue; | |
$exceeded_limit = true; | |
$number_left = $limit - $field_sum >= 0 ? $limit - $field_sum : 0; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = sprintf($validation_message, $requested_value, $number_left); | |
} | |
$validation_result['form'] = $form; | |
$validation_result['is_valid'] = !$validation_result['is_valid'] ? false : !$exceeded_limit; | |
return $validation_result; | |
} | |
public function hide_field($field_content, $field) { | |
if($field['id'] == intval($this->_args['input_id'])) | |
return "<div class=\"ginput_container\">{$this->_args['limit_message']}</div>"; | |
return $field_content; | |
} | |
public static function get_field_values_sum($form_id, $input_id) { | |
global $wpdb; | |
$query = apply_filters( 'gwlimitbysum_query', array( | |
'select' => 'SELECT sum( ld.value )', | |
'from' => "FROM {$wpdb->prefix}rg_lead_detail ld", | |
'join' => '', | |
'where' => $wpdb->prepare( "WHERE ld.form_id = %d AND CAST( ld.field_number as unsigned ) = %d", $form_id, $input_id ) | |
), $form_id, $input_id ); | |
$wpdb->show_errors(); | |
$sql = implode( ' ', $query ); | |
$result = $wpdb->get_var( $sql ); | |
return intval( $result ); | |
} | |
public static function limit_by_approved_only( $query ) { | |
global $wpdb; | |
$query['from'] .= " INNER JOIN {$wpdb->prefix}rg_lead l ON l.id = ld.lead_id"; | |
$query['where'] .= ' AND l.payment_status = \'Approved\''; | |
return $query; | |
} | |
public function shortcode_sum( $output, $atts ) { | |
$atts = shortcode_atts( array( | |
'id' => false, | |
'input_id' => false | |
), $atts ); | |
extract( $atts ); // gives us $id, $input_id | |
return intval( self::get_field_values_sum( $id, $input_id ) ); | |
} | |
public function shortcode_remaining( $output, $atts ) { | |
$atts = shortcode_atts( array( | |
'id' => false, | |
'input_id' => false, | |
'limit' => false | |
), $atts ); | |
extract( $atts ); // gives us $id, $input_id | |
$remaining = $limit - intval( self::get_field_values_sum( $id, $input_id ) ); | |
return max( 0, $remaining ); | |
} | |
} | |
# Configuration | |
new GWLimitBySum( array( | |
'form_id' => 5, | |
'field_id' => 5.70, | |
'limit' => 70, | |
'limit_message' => '<h3>Desculpe, acabaram os ingressos.</h3>', | |
'validation_message' => 'Você selecionou %1$s ingressos, restam ainda %2$s ingressos à venda.', | |
'approved_payments_only' => true, | |
'hide_form' => true | |
) ); | |
new GWLimitBySum( array( | |
'form_id' => 5, | |
'field_id' => 8.40, | |
'limit' => 40, | |
'limit_message' => '<h3>Desculpe, acabaram os ingressos para crianças.</h3>', | |
'validation_message' => 'Você selecionou %1$s ingressos, restam ainda %2$s ingressos à venda.', | |
'approved_payments_only' => true, | |
'hide_form' => false | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment