Conditional Event Colors

Set Google Calendar event colors conditionally based on form field values.

Instructions

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

  2. Configure your rules at the bottom of this file: set form_id (and optionally feed_id), then add a conditionals entry for each color, using the color IDs below. Rules are evaluated top-to-bottom and the first match wins.

Google Calendar Color IDs: 1 = Lavender (blue-purple) #a4bdfc 2 = Sage (muted green) #7ae7bf 3 = Grape (purple) #dbadff 4 = Flamingo (pink) #ff887c 5 = Banana (yellow) #fbd75b 6 = Tangerine (orange) #ffb878 7 = Peacock (teal) #46d6db 8 = Graphite (gray) #e1e1e1 9 = Blueberry (blue) #5484ed 10 = Basil (green) #51b749 11 = Tomato (red) #dc2127

Code

Filename: gcgc-conditional-event-colors.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
<?php
/**
 * Gravity Connect // Google Calendar // Conditional Event Colors
 * https://gravitywiz.com/documentation/gravity-connect-google-calendar/
 *
 * Set Google Calendar event colors conditionally based on form field values.
 *
 * Instructions
 *
 * 1. Install this snippet by following the steps here:
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *
 * 2. Configure your rules at the bottom of this file: set `form_id` (and optionally `feed_id`),
 *    then add a `conditionals` entry for each color, using the color IDs below. Rules are
 *    evaluated top-to-bottom and the first match wins.
 *
 * Google Calendar Color IDs:
 *  1  = Lavender   (blue-purple)  #a4bdfc
 *  2  = Sage       (muted green)  #7ae7bf
 *  3  = Grape      (purple)       #dbadff
 *  4  = Flamingo   (pink)         #ff887c
 *  5  = Banana     (yellow)       #fbd75b
 *  6  = Tangerine  (orange)       #ffb878
 *  7  = Peacock    (teal)         #46d6db
 *  8  = Graphite   (gray)         #e1e1e1
 *  9  = Blueberry  (blue)         #5484ed
 *  10 = Basil      (green)        #51b749
 *  11 = Tomato     (red)          #dc2127
 */
class GCGC_Conditional_Event_Colors {

	private $_args = array();

	public function __construct( $args = array() ) {
		$this->_args = wp_parse_args( $args, array(
			'form_id'      => false,
			'feed_id'      => false,
			'conditionals' => array(),
		) );

		add_action( 'plugins_loaded', array( $this, 'init' ), 15 );
	}

	public function init() {
		if ( ! function_exists( 'gc_google_calendar' ) ) {
			return;
		}

		add_filter( 'gcgc_create_calendar_event_params', array( $this, 'apply_color' ), 10, 4 );
		add_filter( 'gcgc_update_calendar_event_params', array( $this, 'apply_color' ), 10, 4 );
	}

	public function apply_color( $params, $form, $feed, $entry ) {
		if ( ! $this->is_applicable_form( $form ) || ! $this->is_applicable_feed( $feed ) ) {
			return $params;
		}

		foreach ( $this->_args['conditionals'] as $conditional ) {
			if ( GFCommon::evaluate_conditional_logic( $conditional['conditionalLogic'], $form, $entry ) ) {
				$params['colorId'] = (string) $conditional['colorId'];
				break;
			}
		}

		return $params;
	}

	public function is_applicable_form( $form ) {
		$form_id = isset( $form['id'] ) ? $form['id'] : $form;

		return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
	}

	public function is_applicable_feed( $feed ) {
		$feed_id = isset( $feed['id'] ) ? $feed['id'] : $feed;

		return empty( $this->_args['feed_id'] ) || $feed_id == $this->_args['feed_id'];
	}
}

# Configuration

new GCGC_Conditional_Event_Colors( array(
	'form_id'      => 123,
	'feed_id'      => false, // Optional: restrict to a specific feed ID
	'conditionals' => array(
		array(
			'colorId'          => '11', // Tomato (red)
			'conditionalLogic' => array(
				'logicType'  => 'any',
				'actionType' => 'show',
				'rules'      => array(
					array(
						'fieldId'  => '3',
						'operator' => 'is',
						'value'    => 'urgent',
					),
				),
			),
		),
		array(
			'colorId'          => '5', // Banana (yellow)
			'conditionalLogic' => array(
				'logicType'  => 'any',
				'actionType' => 'show',
				'rules'      => array(
					array(
						'fieldId'  => '3',
						'operator' => 'is',
						'value'    => 'pending',
					),
				),
			),
		),
	),
) );

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.