Filter Google Calendar Events Blocking Availability

By default, every busy event on your connected Google Calendar blocks booking availability, including invitations you haven’t accepted and events added by shared calendars. This snippet lets you choose which events block availability and which are ignored, leaving those times open for booking.

Instructions

  1. Install this snippet by following the steps here: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/

  2. Pick or adjust an example in the configuration area at the bottom of the snippet. Options can be combined:

    • only_block_own_events (true/false) — Only events you created or organize block availability. Invitations and events added by shared calendars are ignored.
    • ignore_unaccepted_invitations (true/false) — Ignore invitations you haven’t accepted (tentative, unanswered, declined).
    • ignore_if_title_contains (array of phrases) — Ignore any event whose title contains one of these (case-insensitive).
    • only_block_if_title_contains (array of phrases) — ONLY events whose title contains one of these block availability (case-insensitive).
  3. Re-sync your Google Calendar connection to ensure the updated settings take effect.

Code

Filename: gpb-filter-google-calendar-events-block-availability.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
<?php
/**
 * Gravity Perks // Bookings // Filter Google Calendar Events Blocking Availability
 * https://gravitywiz.com/documentation/gravity-forms-bookings/
 *
 * By default, every busy event on your connected Google Calendar blocks booking availability,
 * including invitations you haven't accepted and events added by shared calendars. This snippet
 * lets you choose which events block availability and which are ignored, leaving those times
 * open for booking.
 *
 * Instructions:
 *
 * 1. Install this snippet by following the steps here:
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *
 * 2. Pick or adjust an example in the configuration area at the bottom of the snippet.
 *    Options can be combined:
 *
 *    - `only_block_own_events` (true/false) — Only events you created or organize
 *      block availability. Invitations and events added by shared calendars are ignored.
 *    - `ignore_unaccepted_invitations` (true/false) — Ignore invitations you haven't
 *      accepted (tentative, unanswered, declined).
 *    - `ignore_if_title_contains` (array of phrases) — Ignore any event whose title
 *      contains one of these (case-insensitive).
 *    - `only_block_if_title_contains` (array of phrases) — ONLY events whose title
 *      contains one of these block availability (case-insensitive).
 *
 * 3. Re-sync your Google Calendar connection to ensure the updated settings take effect.
 */
class GPB_Google_Calendar_Event_Filter {

	private $config;

	public function __construct( array $config = array() ) {
		$this->config = array_merge(
			array(
				'only_block_own_events'         => false,
				'ignore_unaccepted_invitations' => false,
				'ignore_if_title_contains'      => array(),
				'only_block_if_title_contains'  => array(),
			),
			$config
		);

		add_filter( 'gpb_google_calendar_event_block_data', array( $this, 'filter_block_data' ), 10, 2 );
	}

	public function filter_block_data( $block_data, $event ) {
		if ( ! is_array( $block_data ) ) {
			return $block_data;
		}

		// Organizer/creator "self" is only set for events the connected account owns.
		if ( $this->config['only_block_own_events'] && empty( $event['organizer']['self'] ) && empty( $event['creator']['self'] ) ) {
			return null;
		}

		if ( $this->config['ignore_unaccepted_invitations'] && $this->is_unaccepted_invitation( $event ) ) {
			return null;
		}

		$title = (string) ( $event['summary'] ?? '' );

		if ( $this->title_matches( $title, $this->config['ignore_if_title_contains'] ) ) {
			return null;
		}

		if ( $this->config['only_block_if_title_contains'] && ! $this->title_matches( $title, $this->config['only_block_if_title_contains'] ) ) {
			return null;
		}

		return $block_data;
	}

	private function is_unaccepted_invitation( $event ) {
		foreach ( (array) ( $event['attendees'] ?? array() ) as $attendee ) {
			if ( ! empty( $attendee['self'] ) ) {
				return ( $attendee['responseStatus'] ?? '' ) !== 'accepted';
			}
		}

		return false;
	}

	private function title_matches( $title, $needles ) {
		$title = strtolower( $title );

		foreach ( (array) $needles as $needle ) {
			$needle = strtolower( trim( (string) $needle ) );

			if ( $needle !== '' && strpos( $title, $needle ) !== false ) {
				return true;
			}
		}

		return false;
	}
}

# Configuration

// Example: only events you created or organize block availability.
new GPB_Google_Calendar_Event_Filter(
	array(
		'only_block_own_events' => true,
	)
);

// Example: everything blocks availability except invitations you haven't accepted.
// new GPB_Google_Calendar_Event_Filter(
// 	array(
// 		'ignore_unaccepted_invitations' => true,
// 	)
// );

// Example: events with "Hold" or "Tentative" in the title don't block availability.
// new GPB_Google_Calendar_Event_Filter(
// 	array(
// 		'ignore_if_title_contains' => array( 'Hold', 'Tentative' ),
// 	)
// );

// Example: ONLY events with "Busy" or "PTO" in the title block availability.
// new GPB_Google_Calendar_Event_Filter(
// 	array(
// 		'only_block_if_title_contains' => array( 'Busy', 'PTO' ),
// 	)
// );

// Example: combine options — ignore unaccepted invitations and any event titled "Hold".
// new GPB_Google_Calendar_Event_Filter(
// 	array(
// 		'ignore_unaccepted_invitations' => true,
// 		'ignore_if_title_contains'      => array( 'Hold' ),
// 	)
// );

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.