From 7e88bf8fc6c67cb08cbb8de5a5365117faf35fb9 Mon Sep 17 00:00:00 2001 From: SebastianWiz <165194375+SebastianWiz@users.noreply.github.com> Date: Wed, 5 Nov 2025 18:00:14 -0800 Subject: [PATCH 1/2] `gpld-limit-year-dropdown-options.php`: Added snippet to limit date dropdown year options based on configured min/max. --- .../gpld-limit-year-dropdown-options.php | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 gp-limit-dates/gpld-limit-year-dropdown-options.php diff --git a/gp-limit-dates/gpld-limit-year-dropdown-options.php b/gp-limit-dates/gpld-limit-year-dropdown-options.php new file mode 100644 index 000000000..70849f54c --- /dev/null +++ b/gp-limit-dates/gpld-limit-year-dropdown-options.php @@ -0,0 +1,98 @@ + null, + 'field_id' => null, + ), + $args + ); + + $this->form_id = $args['form_id']; + $this->field_id = $args['field_id']; + + add_filter( 'gform_date_min_year', array( $this, 'filter_min_year' ), 10, 3 ); + add_filter( 'gform_date_max_year', array( $this, 'filter_max_year' ), 10, 3 ); + } + + public function filter_min_year( $min_year, $form, $field ) { + return $this->filter_year( 'min', $min_year, $form, $field ); + } + + public function filter_max_year( $max_year, $form, $field ) { + return $this->filter_year( 'max', $max_year, $form, $field ); + } + + protected function filter_year( $type, $default_year, $form, $field ) { + if ( ! $this->is_applicable( $form, $field ) ) { + return $default_year; + } + + $options = gp_limit_dates()->get_limit_dates_field_options( $field ); + $date_key = $type . 'Date'; + $mod_key = $type . 'DateMod'; + + if ( empty( $options[ $date_key ] ) ) { + return $default_year; + } + + $date_value = $options[ $date_key ]; + $modifier = rgar( $options, $mod_key ); + + if ( $date_value == '{today}' ) { + $timestamp = strtotime( 'today midnight' ); + } elseif ( is_numeric( $date_value ) && $date_value > 0 ) { + return $default_year; + } else { + $timestamp = strtotime( $date_value ); + } + + if ( $timestamp && ! empty( $modifier ) ) { + $timestamp = strtotime( $modifier, $timestamp ); + } + + if ( $timestamp ) { + $calculated_year = date( 'Y', $timestamp ); + return $type === 'min' ? max( $calculated_year, $default_year ) : min( $calculated_year, $default_year ); + } + + return $default_year; + } + + protected function is_applicable( $form, $field ) { + if ( ! function_exists( 'gp_limit_dates' ) || $field->dateType !== 'datedropdown' ) { + return false; + } + + $form_id = rgar( $form, 'id' ); + + if ( null !== $this->form_id && intval( $this->form_id ) !== intval( $form_id ) ) { + return false; + } + + if ( null !== $this->field_id && intval( $this->field_id ) !== intval( $field->id ) ) { + return false; + } + + return true; + } +} + +# Configuration + +new GPLD_Limit_Year_Dropdown(array( + 'form_id' => 123, + 'field_id' => 4, +)); From 8a2ae6d49e1109a3a8d02d43ff7bc76f94063e7a Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Fri, 7 Nov 2025 20:15:41 +0530 Subject: [PATCH 2/2] `gpld-limit-year-dropdown-options.php`: Added snippet to limit date dropdown year options based on configured min/max. --- gp-limit-dates/gpld-limit-year-dropdown-options.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gp-limit-dates/gpld-limit-year-dropdown-options.php b/gp-limit-dates/gpld-limit-year-dropdown-options.php index 70849f54c..666ed5d6d 100644 --- a/gp-limit-dates/gpld-limit-year-dropdown-options.php +++ b/gp-limit-dates/gpld-limit-year-dropdown-options.php @@ -40,16 +40,16 @@ protected function filter_year( $type, $default_year, $form, $field ) { return $default_year; } - $options = gp_limit_dates()->get_limit_dates_field_options( $field ); + $options = gp_limit_dates()->get_limit_dates_field_options( $field ); $date_key = $type . 'Date'; - $mod_key = $type . 'DateMod'; + $mod_key = $type . 'DateMod'; if ( empty( $options[ $date_key ] ) ) { return $default_year; } $date_value = $options[ $date_key ]; - $modifier = rgar( $options, $mod_key ); + $modifier = rgar( $options, $mod_key ); if ( $date_value == '{today}' ) { $timestamp = strtotime( 'today midnight' ); @@ -64,7 +64,7 @@ protected function filter_year( $type, $default_year, $form, $field ) { } if ( $timestamp ) { - $calculated_year = date( 'Y', $timestamp ); + $calculated_year = gmdate( 'Y', $timestamp ); return $type === 'min' ? max( $calculated_year, $default_year ) : min( $calculated_year, $default_year ); }