-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgpld-conditional-limits.php
206 lines (163 loc) · 5.59 KB
/
gpld-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
<?php
/**
* Gravity Perks // Limit Dates // Conditional Limits
* https://gravitywiz.com/documentation/gravity-forms-limit-dates/
*
* Provide conditional date options for your date fields.
*
*/
class GPLD_Conditional_Limits {
private $_args = array();
public function __construct( $args = array() ) {
if ( ! function_exists( 'gp_limit_dates' ) ) {
return;
}
// 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,
) );
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_form_scripts' ) );
add_action( 'gform_field_validation', array( $this, 'validate' ), 11, 4 );
// Enables date limits for the form field, allowing the `gp-limit-dates` script to be enqueued.
add_filter( "gpld_has_limit_dates_enabled_{$this->_args['form_id']}_{$this->_args['field_id']}", '__return_true', 10, 2 );
}
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 ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GPLDConditionalLimits = 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() {
var $input = $( '#input_' + self.formId + '_' + self.fieldId );
gform.addAction( 'gform_input_change', function( elem, formId, fieldId ) {
if ( !window.GPLimitDates ) {
return;
};
for ( var i = 0; i < self.conditionals.length; i++ ) {
if ( gf_get_field_action( self.formId, self.conditionals[ i ].conditionalLogic ) == 'show' ) {
gform.addFilter( 'gpld_datepicker_data', function( data ) {
return {
...data, [args.fieldId]: { ...data[args.fieldId], ...self.conditionals[ i ].options }
}
} );
$input.removeClass( 'hasDatepicker' );
gformInitSingleDatepicker( $input );
break;
}
}
} );
// Trigger `gform_input_change' action on multipage form navigation
gf_input_change( $input, self.formId, self.fieldId );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
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 GPLDConditionalLimits( ' . 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 is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
public function is_applicable_field( $field ) {
$field_id = isset( $field['id'] ) ? $field['id'] : $field;
return $field['type'] == 'date' && $field['dateType'] == 'datepicker' && $field_id == $this->_args['field_id'];
}
public function validate( $result, $value, $form, $field ) {
if ( ! $this->is_applicable_field( $field ) || ! $this->is_applicable_form( $form ) ) {
return $result;
}
foreach ( $this->_args['conditionals'] as $conditional ) {
if ( GFCommon::evaluate_conditional_logic( $conditional['conditionalLogic'], $form, GFFormsModel::get_current_lead() ) ) {
add_filter( "gpld_limit_dates_options_{$this->_args['form_id']}_{$this->_args['field_id']}", function( $options ) use ( $conditional ) {
return array_merge( $options, $conditional['options'] );
} );
break;
}
}
if ( ! rgblank( $value ) && ! gp_limit_dates()->is_valid_date( $value, $field ) ) {
$result['is_valid'] = false;
$result['message'] = __( 'Please enter a valid date.', 'gp-limit-dates' );
}
return $result;
}
}
# Configuration
new GPLD_Conditional_Limits( array(
'form_id' => 30,
'field_id' => 3,
'conditionals' => array(
array(
'options' => array(
'minDate' => '{today}',
'minDateMod' => '+2 days',
'daysOfWeek' => array( 1 ),
),
'conditionalLogic' => array(
'logicType' => 'any',
'actionType' => 'show',
'rules' => array(
array(
'fieldId' => '1',
'operator' => 'is',
'value' => '68462',
),
),
),
),
array(
'options' => array(
'minDate' => '{today}',
'minDateMod' => '+2 days',
'daysOfWeek' => array( 1, 2 ),
),
'conditionalLogic' => array(
'logicType' => 'any',
'actionType' => 'show',
'rules' => array(
array(
'fieldId' => '1',
'operator' => 'is',
'value' => '68502',
),
),
),
),
),
) );