Skip to content

Commit 59ebc5c

Browse files
authored
gpld-set-next-available-date.php: Added snippet to set next available date as default.
1 parent 0bdeec1 commit 59ebc5c

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Limit Dates // Set Next Available Date as Default Value
4+
* https://gravitywiz.com/documentation/gravity-forms-limit-dates/
5+
*
6+
* This snippet sets the default value of specified Date fields to the next available date
7+
* based on their GP Limit Dates configuration.
8+
*
9+
* Instructions:
10+
*
11+
* 1. Install the snippet.
12+
* https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets
13+
*
14+
* 2. Add the parameter `gpld_next_available_date` to the "Allow field to be populated dynamically" setting
15+
*
16+
* 3. Profit.
17+
*/
18+
add_filter( 'gform_field_value_gpld_next_available_date', function( $value, $field, $name ) {
19+
20+
// Only set default if field is empty
21+
if ( ! empty( $value ) ) {
22+
return $value;
23+
}
24+
25+
// Find and return the next available date
26+
$next_date = gpld_find_next_available_date( $field );
27+
28+
return $next_date ?: $value;
29+
30+
}, 10, 3 );
31+
32+
/**
33+
* Find the next available date for a field with GP Limit Dates configuration
34+
*
35+
* @param GF_Field $field The date field
36+
* @param int $start_date Optional starting date timestamp. Defaults to today.
37+
* @param int $max_days Maximum days to check ahead. Defaults to 365.
38+
* @return string|null Next available date in field format, or null if none found
39+
*/
40+
if ( ! function_exists( 'gpld_find_next_available_date' ) ) {
41+
function gpld_find_next_available_date( $field, $start_date = null, $max_days = 365 ) {
42+
// Check if GP Limit Dates is active
43+
if ( ! function_exists( 'gp_limit_dates' ) || ! gp_limit_dates()->has_limit_dates_enabled( $field ) ) {
44+
return null;
45+
}
46+
47+
// Start from today if no start date provided
48+
if ( $start_date === null ) {
49+
$start_date = gp_limit_dates()->get_midnight( $field );
50+
}
51+
52+
// Check each day starting from the start date
53+
for ( $i = 0; $i < $max_days; $i++ ) {
54+
$check_date = strtotime( "+{$i} days", $start_date );
55+
56+
if ( gp_limit_dates()->is_valid_date( $check_date, $field ) ) {
57+
// Convert timestamp to the field's date format
58+
return gpld_format_date_for_field( $check_date, $field );
59+
}
60+
}
61+
62+
return null;
63+
}
64+
}
65+
66+
/**
67+
* Format a timestamp according to the field's date format
68+
*
69+
* @param int $timestamp Unix timestamp
70+
* @param GF_Field $field The date field
71+
* @return string Formatted date string
72+
*/
73+
if ( ! function_exists( 'gpld_format_date_for_field' ) ) {
74+
function gpld_format_date_for_field( $timestamp, $field ) {
75+
$date_format = $field->dateFormat ? $field->dateFormat : 'mdy';
76+
77+
switch ( $date_format ) {
78+
case 'mdy':
79+
return date( 'm/d/Y', $timestamp );
80+
case 'dmy':
81+
return date( 'd/m/Y', $timestamp );
82+
case 'dmy_dash':
83+
return date( 'd-m-Y', $timestamp );
84+
case 'dmy_dot':
85+
return date( 'd.m.Y', $timestamp );
86+
case 'ymd_slash':
87+
return date( 'Y/m/d', $timestamp );
88+
case 'ymd_dash':
89+
return date( 'Y-m-d', $timestamp );
90+
case 'ymd_dot':
91+
return date( 'Y.m.d', $timestamp );
92+
default:
93+
return date( 'm/d/Y', $timestamp ); // fallback to mdy
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)