Skip to content

Commit d324431

Browse files
authored
gpld-conditional-limits.php: Added a new snippet to enable conditional date limits.
1 parent 30437d7 commit d324431

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
/**
3+
* Gravity Wiz // Gravity Perks // GP Limit Dates // Conditional Limits
4+
* https://gravitywiz.com/documentation/gravity-forms-limit-dates/
5+
*
6+
* Provide conditional date options for your date fields.
7+
*
8+
*/
9+
class GPLD_Conditional_Limits {
10+
11+
private $_args = array();
12+
13+
public function __construct( $args = array() ) {
14+
if ( ! function_exists( 'gp_limit_dates' ) ) {
15+
return;
16+
}
17+
18+
// set our default arguments, parse against the provided arguments, and store for use throughout the class
19+
$this->_args = wp_parse_args( $args, array(
20+
'form_id' => false,
21+
'field_id' => false,
22+
) );
23+
24+
add_action( 'init', array( $this, 'init' ) );
25+
}
26+
27+
public function init() {
28+
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
29+
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
30+
add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_form_scripts' ) );
31+
add_action( 'gform_field_validation', array( $this, 'validate' ), 11, 4 );
32+
}
33+
34+
public function enqueue_form_scripts( $form ) {
35+
if ( $this->is_applicable_form( $form ) ) {
36+
wp_enqueue_script( 'gform_conditional_logic' );
37+
}
38+
}
39+
40+
public function load_form_script( $form, $is_ajax_enabled ) {
41+
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
42+
add_action( 'wp_footer', array( $this, 'output_script' ) );
43+
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
44+
}
45+
46+
return $form;
47+
}
48+
49+
public function output_script() {
50+
?>
51+
52+
<script type="text/javascript">
53+
54+
( function( $ ) {
55+
56+
window.GPLDConditionalLimits = function( args ) {
57+
58+
var self = this;
59+
60+
// copy all args to current object: (list expected props)
61+
for( prop in args ) {
62+
if( args.hasOwnProperty( prop ) )
63+
self[prop] = args[prop];
64+
}
65+
66+
self.init = function() {
67+
68+
var $input = $( '#input_' + self.formId + '_' + self.fieldId );
69+
70+
gform.addAction( 'gform_input_change', function( elem, formId, fieldId ) {
71+
72+
if ( !window.GPLimitDates ) {
73+
return;
74+
};
75+
76+
for ( var i = 0; i < self.conditionals.length; i++ ) {
77+
if ( gf_get_field_action( self.formId, self.conditionals[ i ].conditionalLogic ) == 'show' ) {
78+
79+
gform.addFilter( 'gpld_datepicker_data', function( data ) {
80+
return {
81+
...data, [args.fieldId]: { ...data[args.fieldId], ...self.conditionals[ i ].options }
82+
}
83+
} );
84+
85+
$input.removeClass( 'hasDatepicker' );
86+
gformInitSingleDatepicker( $input );
87+
88+
break;
89+
}
90+
}
91+
} );
92+
93+
// Trigger `gform_input_change' action on multipage form navigation
94+
gf_input_change( $input, self.formId, self.fieldId );
95+
};
96+
97+
self.init();
98+
}
99+
100+
} )( jQuery );
101+
102+
</script>
103+
104+
<?php
105+
}
106+
107+
public function add_init_script( $form ) {
108+
if ( ! $this->is_applicable_form( $form ) ) {
109+
return;
110+
}
111+
112+
$args = array(
113+
'formId' => $this->_args['form_id'],
114+
'fieldId' => $this->_args['field_id'],
115+
'conditionals' => $this->_args['conditionals'],
116+
);
117+
118+
$script = 'new GPLDConditionalLimits( ' . json_encode( $args ) . ' );';
119+
$slug = implode( '_', array( __class__, $this->_args['form_id'], $this->_args['field_id'] ) );
120+
121+
GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
122+
}
123+
124+
public function is_applicable_form( $form ) {
125+
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
126+
127+
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
128+
}
129+
130+
public function is_applicable_field( $field ) {
131+
$field_id = isset( $field['id'] ) ? $field['id'] : $field;
132+
133+
return $field['type'] == 'date' && $field['dateType'] == 'datepicker' && $field_id == $this->_args['field_id'];
134+
}
135+
136+
public function validate( $result, $value, $form, $field ) {
137+
if ( ! $this->is_applicable_field( $field ) || ! $this->is_applicable_form( $form ) ) {
138+
return $result;
139+
}
140+
141+
foreach ( $this->_args['conditionals'] as $conditional ) {
142+
if ( GFCommon::evaluate_conditional_logic( $conditional['conditionalLogic'], $form, GFFormsModel::get_current_lead() ) ) {
143+
add_filter( "gpld_limit_dates_options_{$this->_args['form_id']}_{$this->_args['field_id']}", function( $options ) use ( $conditional ) {
144+
return array_merge( $options, $conditional['options'] );
145+
} );
146+
147+
break;
148+
}
149+
}
150+
151+
if ( ! rgblank( $value ) && ! gp_limit_dates()->is_valid_date( $value, $field ) ) {
152+
$result['is_valid'] = false;
153+
$result['message'] = __( 'Please enter a valid date.', 'gp-limit-dates' );
154+
}
155+
156+
return $result;
157+
}
158+
}
159+
160+
# Configuration
161+
162+
new GPLD_Conditional_Limits( array(
163+
'form_id' => 30,
164+
'field_id' => 3,
165+
'conditionals' => array(
166+
array(
167+
'options' => array(
168+
'minDate' => '{today}',
169+
'minDateMod' => '+2 days',
170+
'daysOfWeek' => array( 1 ),
171+
),
172+
'conditionalLogic' => array(
173+
'logicType' => 'any',
174+
'actionType' => 'show',
175+
'rules' => array(
176+
array(
177+
'fieldId' => '1',
178+
'operator' => 'is',
179+
'value' => '68462',
180+
),
181+
),
182+
),
183+
),
184+
array(
185+
'options' => array(
186+
'minDate' => '{today}',
187+
'minDateMod' => '+2 days',
188+
'daysOfWeek' => array( 1, 2 ),
189+
),
190+
'conditionalLogic' => array(
191+
'logicType' => 'any',
192+
'actionType' => 'show',
193+
'rules' => array(
194+
array(
195+
'fieldId' => '1',
196+
'operator' => 'is',
197+
'value' => '68502',
198+
),
199+
),
200+
),
201+
),
202+
),
203+
) );

0 commit comments

Comments
 (0)