Exclude Blocked Dates from Modifiers

Automatically excludes blocked dates from a Date Modifier in GP Limit Dates.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gpld-exclude-blocked-dates-from-modifiers.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
<?php
/**
 * Gravity Perks // GP Limit Dates // Exclude Blocked Dates from Modifiers
 *
 * Automatically excludes blocked dates from a Date Modifier in GP Limit Dates.
 *
 * @version 0.1
 * @author  David Smith <david@gravitywiz.com>
 * @license GPL-2.0+
 * @link    https://gravitywiz.com
 *
 * Plugin Name: GP Limit Dates — Exclude Blocked Dates from Modifiers
 * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-limit-dates/
 * Description: Automatically excludes blocked dates from a Date Modifier in GP Limit Dates.
 * Author: Gravity Wiz
 * Version: 0.1
 * Author URI: https://gravitywiz.com
 *
 */
add_filter( 'gpld_date_value', 'gpld_extend_modifiers_by_blocked_dates', 10, 4 );
function gpld_extend_modifiers_by_blocked_dates( $end_date, $field, $key, $options ) {

	if ( ! $end_date ) {
		return $end_date;
	}

	$value       = rgar( $options, $key );
	$is_field_id = is_numeric( $value ) && $value > 0;

	if ( $value == '{today}' ) {
        // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
		$date = strtotime( 'midnight', current_time( 'timestamp' ) );
		//$date = strtotime( '05/21/2016 midnight' );
	} elseif ( $is_field_id ) {

		$mod_field_id = $value;
		$form         = GFAPI::get_form( $field->formId );
		$mod_field    = GFFormsModel::get_field( $form, $mod_field_id );

		$date = gp_limit_dates()->parse_timestamp( rgpost( sprintf( 'input_%s', $mod_field->id ) ), $mod_field );

	} else {
		$date = strtotime( $value );
	}

	$blocked_count = 0;

	// phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date
	$begin    = new DateTime( date( 'Y-m-d', $date ) );
	$end      = new DateTime( date( 'Y-m-d', $end_date ) );
	$interval = DateInterval::createFromDateString( '1 day' );

	// period should not include the starting day, should always inlude the very last day
	$period = new DatePeriod( $begin, $interval, $end->add( $interval ) );

	foreach ( $period as $_date ) {
		if ( ! gpld_is_valid_date( $_date->getTimestamp(), $field ) ) {
			$blocked_count++;
		}
	}

	if ( ! $blocked_count ) {
		return $end_date;
	}

	$end = new DateTime( date( 'Y-m-d', $end_date ) );

	// extend endDate to account for blocked dates
	while ( $blocked_count > 0 ) {
		$end->add( DateInterval::createFromDateString( '1 day' ) );
		// only deduct valid dates from our counter
		if ( gpld_is_valid_date( $end->getTimestamp(), $field ) ) {
			$blocked_count--;
		}
	}

	// set end date to first unblocked date
	while ( ! gpld_is_valid_date( $end->getTimestamp(), $field ) ) {
		$end->add( $interval );
	}

	return $end->getTimestamp();
}

function gpld_is_valid_date( $date, $field ) {

	$date = gp_limit_dates()->parse_timestamp( $date, $field );

	$is_valid_day = gp_limit_dates()->is_valid_day_of_week( $date, $field );
	$is_valid     = $is_valid_day;
	$is_excepted  = gp_limit_dates()->is_excepted( $date, $field );

	if ( $is_excepted ) {
		$is_valid = ! $is_valid;
	}

	return $is_valid;
}

add_filter( 'wp_footer', 'gpld_exclude_blocked_dates_js' );
add_filter( 'gform_preview_footer', 'gpld_exclude_blocked_dates_js' );
function gpld_exclude_blocked_dates_js() {
	?>

	<script type="text/javascript">

		if( window.gform ) {

			gform.addFilter( 'gpld_modified_date', function( modifiedDate, modifier, date, data, fieldId ) {
				return gpldGetModifiedDateWithBlockedDates( date, modifiedDate, data, fieldId );
			} );

			function gpldGetModifiedDateWithBlockedDates( startDate, endDate, data, fieldId ) {

				var date         = new Date( startDate ),
					endDate      = new Date( endDate ),
					blockedCount = 0;

				// find all the blocked dates between the start and end dates
				for ( date; date <= endDate; date.setDate( date.getDate() + 1 ) ) {
					if( ! GPLimitDates.isDateShown( date, data, fieldId )[0] ) {
						blockedCount++;
					}
				}

				if( ! blockedCount ) {
					return endDate;
				}

				// extend endDate to account for blocked dates
				while( blockedCount > 0 ) {
					endDate.setDate( endDate.getDate() + 1 );
					// only deduct valid dates from our counter
					if( GPLimitDates.isDateShown( endDate, data, fieldId )[0] ) {
						blockedCount--;
					}
				}

				// set end date to first unblocked date
				while( ! GPLimitDates.isDateShown( endDate, data, fieldId )[0] ) {
					endDate.setDate( endDate.getDate() + 1 );
				}

				return endDate;
			}

		}

	</script>

	<?php
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.