-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgplcb-conditional-limits.php
246 lines (191 loc) · 6.74 KB
/
gplcb-conditional-limits.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Gravity Wiz // Gravity Perks // GP Limit Checkboxes // Conditional Limits
*
* Provide conditional min/max for your checkbox fields.
*
* @version 0.5
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
*
* Plugin Name: GP Limit Checkboxes - Conditional Limits
* Plugin URI: http://gravitywiz.com/...
* Description: Provide conditional min/max for your checkbox fields.
* Author: Gravity Wiz
* Version: 0.4
* Author URI: http://gravitywiz.com
*/
class GPLCB_Conditional_Limits {
protected static $is_script_output = false;
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false,
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
add_filter( 'gform_pre_render', array( $this, 'add_trigger_class' ), 10, 2 );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_form_scripts' ) );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_filter( 'gplc_group', array( $this, 'handle_conditionals' ), 10, 2 );
}
public function add_trigger_class( $form ) {
foreach ( $form['fields'] as &$field ) {
$field->cssClass .= ' gfield_trigger_change';
}
return $form;
}
public function enqueue_form_scripts( $form ) {
if ( $this->is_applicable_form( $form ) ) {
wp_enqueue_script( 'gform_conditional_logic' );
}
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! self::$is_script_output && ! $this->is_ajax_submission( $form['id'], $is_ajax_enabled ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function is_ajax_submission( $form_id, $is_ajax_enabled ) {
return isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
// Temporary solution until GF 2.3.
if( ! window['gf_form_conditional_logic'] ) {
window['gf_form_conditional_logic'] = [];
}
window.GPLCBConditionalLimits = function( args ) {
var self = this;
// copy all args to current object: (list expected props)
for( prop in args ) {
if( args.hasOwnProperty( prop ) )
self[prop] = args[prop];
}
self.init = function() {
gform.addFilter( 'gplc_group', function( group, fieldId, $elem, gpLimitCheckboxes ) {
// Only evaluate for conditional logic triggered checkbox clicks.
// if( fieldId !== null ) {
// return group;
// }
if( $.inArray( self.fieldId, group.fields ) === -1 ) {
return group;
}
for( var i = 0; i < self.conditionals.length; i++ ) {
if( gf_get_field_action( self.formId, self.conditionals[ i ].conditionalLogic ) == 'show' ) {
if( group.max != self.conditionals[ i ].max ) {
group.max = self.conditionals[ i ].max;
// @todo uncheck all checkboxes
}
break;
}
}
return group;
} );
gform.addAction( 'gform_input_change', function( elem, formId, fieldId ) {
for( var i = 0; i < self.conditionals.length; i++ ) {
for( var j = 0; j < self.conditionals[ i ].conditionalLogic.rules.length; j++ ) {
var rule = self.conditionals[ i ].conditionalLogic.rules[ j ];
if( parseInt( rule.fieldId ) == parseInt( fieldId ) ) {
window.GPLimitCheckboxes.instances[ self.formId ].handleCheckboxClick();
}
}
}
} );
// force GPLimitCheckbox groups to be re-evaluated anytime conditional logic changes; this is a
// short term solution; long term we will need to listen for the specific inputs because we can't
// always assume the trigger field will be using Gravity Forms default conditional logic.
// $( document ).on( 'gform_post_conditional_logic', function( event, formId ) {
// if( self.formId == formId && window.GPLimitCheckboxes ) {
// window.GPLimitCheckboxes.instances[ self.formId ].handleCheckboxClick();
// }
// } );
};
self.init();
}
} )( jQuery );
</script>
<?php
self::$is_script_output = true;
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $this->_args['form_id'],
'fieldId' => $this->_args['field_id'],
'conditionals' => $this->_args['conditionals'],
);
$script = 'new GPLCBConditionalLimits( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( __class__, $this->_args['form_id'], $this->_args['field_id'] ) );
GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function handle_conditionals( $group, $form ) {
if ( ! in_array( $this->_args['field_id'], $group['fields'] ) ) {
return $group;
}
foreach ( $this->_args['conditionals'] as $conditional ) {
if ( GFCommon::evaluate_conditional_logic( $conditional['conditionalLogic'], $form, GFFormsModel::get_current_lead() ) ) {
$group['min'] = $conditional['min'];
$group['max'] = $conditional['max'];
break;
}
}
return $group;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
}
# Configuration
new GPLCB_Conditional_Limits( array(
'form_id' => 1654,
'field_id' => 7,
'conditionals' => array(
array(
'min' => 2,
'max' => 2,
'conditionalLogic' => array(
'logicType' => 'all',
'actionType' => 'show',
'rules' => array(
array(
'fieldId' => 2,
'operator' => 'is',
'value' => 'MX250-2',
),
),
),
),
array(
'min' => 3,
'max' => 3,
'conditionalLogic' => array(
'logicType' => 'all',
'actionType' => 'show',
'rules' => array(
array(
'fieldId' => 2,
'operator' => 'is',
'value' => 'MX 250-3',
),
),
),
),
),
) );