Create Recurring Event

Make events created by GC Google Calendar recurring.

Instructions

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

Code

Filename: gcgc-create-recurring-events.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
<?php

/**
 * Gravity Connect // Google Calendar // Create Recurring Event
 *
 * Make events created by GC Google Calendar recurring.
 *
 * @reference The Google Calendar insert event API reference: https://developers.google.com/workspace/calendar/api/v3/reference/events/insert
 *
 * Installation:
 *   1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 */

function gcgc_create_recurring_event( $form_id, $feed_id, $rules ) {
	function add_recurrence( $event_params, $rules ) {
		if ( ! is_array( rgar( $event_params, 'recurrence' ) ) ) {
			$event_params['recurrence'] = array();
		}

		$event_params['recurrence'] = array_merge(
			$event_params['recurrence'],
			$rules
		);

		return $event_params;
	}

	add_filter(
		implode( '_', array( 'gcgc_create_calendar_event_params', $form_id, $feed_id ) ),
		function(
				$event_params,
				$form,
				$feed,
				$entry
		) use ( $rules ) {
			return add_recurrence( $event_params, $rules );
		},
		10,
		4
	);

	add_filter(
		implode( '_', array( 'gcgc_update_calendar_event_params', $form_id, $feed_id ) ),
		function(
				$event_params,
				$form,
				$feed,
				$entry,
				$event_id
		) use ( $rules ) {
			return add_recurrence( $event_params, $rules );
		},
		10,
		5
	);
}

// Usage Examples:

// Create a weekly event that recurs exactly three times.
// Scoped to form id 12 / feed id 29
gcgc_create_recurring_event(
	'12',
	'29',
	array( 'RRULE:FREQ=WEEKLY;COUNT=3' )
);

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.