Attach ICS File to Notifications

Attach an ICS file to specified notifications that includes all bookings from the entry.

Instructions

  1. Install this snippet: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
  2. Set ‘form_id’ and notification in the configuration at the bottom. Find the notification ID in the “nid” parameter when editing the notification. A notification ID array is also supported.

Code

Filename: gpb-attach-ics-to-notification.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
 * Gravity Perks // Bookings // Attach ICS File to Notifications
 *
 * Attach an ICS file to specified notifications that includes all bookings from the entry.
 *
 * Instructions:
 * 1. Install this snippet: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 * 2. Set 'form_id' and `notification` in the configuration at the bottom. Find the notification ID in the "nid" parameter when editing the notification. A notification ID array is also supported.
 */
class GPB_Notification_ICS {
	private $form_id;
	private $notification_ids;
	private $include_pending;
	private $files = array();

	public function __construct( array $args ) {
		$args = wp_parse_args( $args, array(
			'form_id'         => 0,
			'notification'    => array(),
			'include_pending' => false,
		) );

		$this->form_id          = (int) $args['form_id'];
		$this->notification_ids = (array) $args['notification'];
		$this->include_pending  = (bool) $args['include_pending'];

		add_filter( 'gform_pre_send_email', array( $this, 'attach' ), 20, 4 );
		add_action( 'gform_after_email', array( $this, 'after_email' ), 20, 6 );
		add_action( 'shutdown', array( $this, 'cleanup' ) );
	}

	public function attach( $email, $format, $notification, $entry ) {
		if ( ! empty( $email['abort_email'] )
			|| (int) rgar( $entry, 'form_id' ) !== $this->form_id
			|| ! in_array( rgar( $notification, 'id' ), $this->notification_ids, true )
			|| empty( $entry['id'] )
			|| ! class_exists( '\GP_Bookings\Queries\Booking_Query' )
			|| ! class_exists( '\Sabre\VObject\Component\VCalendar' ) ) {
			return $email;
		}

		try {
			$bookings = array_values( array_filter(
				\GP_Bookings\Queries\Booking_Query::get_entry_bookings( (int) $entry['id'] ),
				static function ( $booking ) {
					return $booking->get_type() === 'service';
				}
			) );

			// No saved booking is a normal case (for example, an early notification).
			if ( empty( $bookings ) ) {
				return $email;
			}

			$utc      = new \DateTimeZone( 'UTC' );
			$calendar = new \Sabre\VObject\Component\VCalendar( array(
				'VERSION'  => '2.0',
				'PRODID'   => '-//Gravity Wiz//GPB Notification ICS//EN',
				'CALSCALE' => 'GREGORIAN',
			) );

			foreach ( $bookings as $booking ) {
				try {
					$status  = $booking->get_status();
					$allowed = $status === 'confirmed' || ( $this->include_pending && $status === 'pending' );
					$service = $booking->get_bookable();
					if ( ! $allowed || ! $service ) {
						continue;
					}

					// Build separately so a failed booking cannot leave a partial event.
					$event = $calendar->createComponent( 'VEVENT', array(
						'UID'         => 'gpb-notification-' . hash( 'sha256', home_url( '/' ) . '|' . $booking->get_id() ),
						'DTSTAMP'     => new \DateTimeImmutable( 'now', $utc ),
						'SUMMARY'     => wp_strip_all_tags( $service->get_name() ),
						'DESCRIPTION' => 'Your booking: ' . wp_strip_all_tags( $service->get_name() ),
						'STATUS'      => $status === 'pending' ? 'TENTATIVE' : 'CONFIRMED',
					) );

					$mode = $booking->get_mode();
					if ( $mode === 'datetime-range' ) {
						$start = $this->parse_datetime( $booking->get_start_datetime_utc(), $utc );
						$end   = $this->parse_datetime( $booking->get_end_datetime_utc(), $utc );
						$event->add( 'DTSTART', $start );
						$event->add( 'DTEND', $end );
					} elseif ( in_array( $mode, array( 'nightly', 'date-range-end-inclusive' ), true ) ) {
						// Keep date-only values as local calendar dates, without a UTC shift.
						$start = $this->parse_datetime( $booking->get_start_datetime(), $utc )->setTime( 0, 0 );
						$end   = $this->parse_datetime( $booking->get_end_datetime(), $utc )->setTime( 0, 0 );
						// ICS end dates are exclusive; GPB's day-range end is inclusive.
						if ( $mode === 'date-range-end-inclusive' ) {
							$end = $end->modify( '+1 day' );
						}
						$event->add( 'DTSTART', $start->format( 'Ymd' ), array( 'VALUE' => 'DATE' ) );
						$event->add( 'DTEND', $end->format( 'Ymd' ), array( 'VALUE' => 'DATE' ) );
					} else {
						continue;
					}

					if ( $end <= $start ) {
						throw new \RuntimeException( 'Invalid booking date range.' );
					}
					$calendar->add( $event );
				} catch ( \Throwable $exception ) {
					// Other valid bookings can still be attached. Do not log customer data.
					\GFCommon::log_error( __METHOD__ . ': Skipped an invalid booking in the ICS attachment.' );
				}
			}

			if ( ! count( $calendar->select( 'VEVENT' ) ) ) {
				return $email;
			}

			// A random, private directory outside WordPress uploads; no public feed URL.
			$directory = trailingslashit( sys_get_temp_dir() ) . 'gpb-ics-' . bin2hex( random_bytes( 16 ) );
			if ( ! mkdir( $directory, 0700 ) ) {
				throw new \RuntimeException( 'Could not create temporary directory.' );
			}

			$file                 = $directory . '/booking.ics';
			$this->files[ $file ] = $directory;
			$content              = $calendar->serialize();
			if ( file_put_contents( $file, $content ) !== strlen( $content ) || ! chmod( $file, 0600 ) ) {
				throw new \RuntimeException( 'Could not write temporary calendar file.' );
			}

			// Keep all attachments supplied by Gravity Forms or other snippets.
			$attachments          = $email['attachments'] ?? array();
			$email['attachments'] = array_merge( $attachments ? (array) $attachments : array(), array( $file ) );
		} catch ( \Throwable $exception ) {
			// Keep the notification working if an attachment cannot be generated.
			// Do not log calendar contents or customer data.
			\GFCommon::log_error( __METHOD__ . ': Could not generate the booking ICS attachment.' );
		}

		return $email;
	}

	private function parse_datetime( $value, $timezone ) {
		// Empty/malformed values must not silently become "now" or a normalized date.
		$date = \DateTimeImmutable::createFromFormat( '!Y-m-d H:i:s', (string) $value, $timezone );
		if ( ! $date || $date->format( 'Y-m-d H:i:s' ) !== $value ) {
			throw new \RuntimeException( 'Invalid booking datetime.' );
		}
		return $date;
	}

	public function after_email( $success, $to, $subject, $message, $headers, $attachments ) {
		foreach ( (array) $attachments as $file ) {
			$this->remove_file( $file );
		}
	}

	private function remove_file( $file ) {
		if ( ! isset( $this->files[ $file ] ) ) {
			return;
		}
		if ( is_file( $file ) ) {
			unlink( $file );
		}
		if ( is_dir( $this->files[ $file ] ) ) {
			rmdir( $this->files[ $file ] );
		}
		unset( $this->files[ $file ] );
	}

	public function cleanup() {
		foreach ( array_keys( $this->files ) as $file ) {
			$this->remove_file( $file );
		}
	}
}

# Configuration

new GPB_Notification_ICS( array(
	'form_id'      => 123,
	'notification' => 'abc123def456',
	//'include_pending' => true, // Uncomment to attach pending bookings. Only confirmed are attached by default.
) );

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.