Skip to content

Commit 9ed38d4

Browse files
authored
gpld-limit-year-dropdown-options.php: Added snippet to limit date dropdown year options based on configured min/max.
1 parent e502a8c commit 9ed38d4

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Limit Dates // Limit Year Dropdown Options
4+
* https://gravitywiz.com/documentation/gravity-forms-limit-dates/
5+
*
6+
* Limits year dropdown options based on configured min/max dates in GP Limit Dates.
7+
* Note: min/max rules that reference other date fields are not supported.
8+
*/
9+
10+
class GPLD_Limit_Year_Dropdown {
11+
protected $form_id;
12+
protected $field_id;
13+
14+
public function __construct( $args = array() ) {
15+
$args = array_merge(
16+
array(
17+
'form_id' => null,
18+
'field_id' => null,
19+
),
20+
$args
21+
);
22+
23+
$this->form_id = $args['form_id'];
24+
$this->field_id = $args['field_id'];
25+
26+
add_filter( 'gform_date_min_year', array( $this, 'filter_min_year' ), 10, 3 );
27+
add_filter( 'gform_date_max_year', array( $this, 'filter_max_year' ), 10, 3 );
28+
}
29+
30+
public function filter_min_year( $min_year, $form, $field ) {
31+
return $this->filter_year( 'min', $min_year, $form, $field );
32+
}
33+
34+
public function filter_max_year( $max_year, $form, $field ) {
35+
return $this->filter_year( 'max', $max_year, $form, $field );
36+
}
37+
38+
protected function filter_year( $type, $default_year, $form, $field ) {
39+
if ( ! $this->is_applicable( $form, $field ) ) {
40+
return $default_year;
41+
}
42+
43+
$options = gp_limit_dates()->get_limit_dates_field_options( $field );
44+
$date_key = $type . 'Date';
45+
$mod_key = $type . 'DateMod';
46+
47+
if ( empty( $options[ $date_key ] ) ) {
48+
return $default_year;
49+
}
50+
51+
$date_value = $options[ $date_key ];
52+
$modifier = rgar( $options, $mod_key );
53+
54+
if ( $date_value == '{today}' ) {
55+
$timestamp = strtotime( 'today midnight' );
56+
} elseif ( is_numeric( $date_value ) && $date_value > 0 ) {
57+
return $default_year;
58+
} else {
59+
$timestamp = strtotime( $date_value );
60+
}
61+
62+
if ( $timestamp && ! empty( $modifier ) ) {
63+
$timestamp = strtotime( $modifier, $timestamp );
64+
}
65+
66+
if ( $timestamp ) {
67+
$calculated_year = gmdate( 'Y', $timestamp );
68+
return $type === 'min' ? max( $calculated_year, $default_year ) : min( $calculated_year, $default_year );
69+
}
70+
71+
return $default_year;
72+
}
73+
74+
protected function is_applicable( $form, $field ) {
75+
if ( ! function_exists( 'gp_limit_dates' ) || $field->dateType !== 'datedropdown' ) {
76+
return false;
77+
}
78+
79+
$form_id = rgar( $form, 'id' );
80+
81+
if ( null !== $this->form_id && intval( $this->form_id ) !== intval( $form_id ) ) {
82+
return false;
83+
}
84+
85+
if ( null !== $this->field_id && intval( $this->field_id ) !== intval( $field->id ) ) {
86+
return false;
87+
}
88+
89+
return true;
90+
}
91+
}
92+
93+
# Configuration
94+
95+
new GPLD_Limit_Year_Dropdown(array(
96+
'form_id' => 123,
97+
'field_id' => 4,
98+
));

0 commit comments

Comments
 (0)