Skip to content

Commit 9351ef0

Browse files
committed
feat: google calendar template and jobs
1 parent 3ceac9d commit 9351ef0

File tree

5 files changed

+385
-33
lines changed

5 files changed

+385
-33
lines changed

forms-bridge/addons/gcalendar/class-gcalendar-addon.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function ping( $backend ) {
5555
array(
5656
'name' => '__gcalendar-' . time(),
5757
'backend' => $backend,
58-
'endpoint' => '/calendars/primary',
58+
'endpoint' => '/calendar/v3/users/me/calendarList',
5959
'method' => 'GET',
6060
)
6161
);
@@ -141,12 +141,11 @@ public function fetch( $endpoint, $backend ) {
141141
* @return array List of fields and content type of the endpoint.
142142
*/
143143
public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
144-
if ( 'POST' !== $method ) {
144+
if ( ! in_array( $method, array( 'POST', 'PUT' ), true ) ) {
145145
return array();
146146
}
147147

148-
// Google Calendar events have a standard schema
149-
$fields = array(
148+
return array(
150149
array(
151150
'name' => 'summary',
152151
'schema' => array(
@@ -169,43 +168,53 @@ public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
169168
),
170169
),
171170
array(
172-
'name' => 'start.dateTime',
171+
'name' => 'start',
173172
'schema' => array(
174-
'type' => 'string',
175-
'description' => 'Start date and time (ISO 8601 format)',
176-
),
177-
),
178-
array(
179-
'name' => 'start.timeZone',
180-
'schema' => array(
181-
'type' => 'string',
182-
'description' => 'Start timezone',
183-
),
184-
),
185-
array(
186-
'name' => 'end.dateTime',
187-
'schema' => array(
188-
'type' => 'string',
189-
'description' => 'End date and time (ISO 8601 format)',
173+
'type' => 'object',
174+
'properties' => array(
175+
'dateTime' => array(
176+
'type' => 'string',
177+
'description' => 'Start date and time (ISO 8601 format)',
178+
),
179+
'timeZone' => array(
180+
'type' => 'string',
181+
'description' => 'Start timezone',
182+
),
183+
),
184+
'additionalProperties' => false,
185+
'required' => array( 'dateTime' ),
190186
),
191187
),
192188
array(
193-
'name' => 'end.timeZone',
189+
'name' => 'end',
194190
'schema' => array(
195-
'type' => 'string',
196-
'description' => 'End timezone',
191+
'type' => 'object',
192+
'properties' => array(
193+
'dateTime' => array(
194+
'type' => 'string',
195+
'description' => 'End date and time (ISO 8601 format)',
196+
),
197+
'timeZone' => array(
198+
'type' => 'string',
199+
'description' => 'End timezone',
200+
),
201+
),
202+
'additionalProperties' => false,
203+
'required' => array( 'dateTime' ),
197204
),
198205
),
199206
array(
200207
'name' => 'attendees',
201208
'schema' => array(
202-
'type' => 'string',
203-
'description' => 'Comma-separated list of attendee emails',
209+
'type' => 'array',
210+
'items' => array(
211+
'type' => 'string',
212+
'description' => 'attendee email address',
213+
),
214+
'additionalItems' => true,
204215
),
205216
),
206217
);
207-
208-
return $fields;
209218
}
210219
}
211220

forms-bridge/addons/gcalendar/class-gcalendar-form-bridge.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ private function transform_to_event( $payload ) {
132132
$event['colorId'] = $payload['colorId'];
133133
}
134134

135+
if ( ! ( isset( $event['start'] ) && isset( $event['end'] ) ) ) {
136+
return new WP_Error(
137+
'missing_event_dates',
138+
'Event must have a start and an end date',
139+
$payload,
140+
);
141+
}
142+
135143
if ( ! isset( $event['summary'] ) ) {
136144
return new WP_Error(
137145
'missing_summary',

forms-bridge/addons/gcalendar/hooks.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ function ( $schema, $addon ) {
1616
return $schema;
1717
}
1818

19-
$schema['properties']['endpoint']['default'] =
20-
'/calendars/primary/events';
19+
$schema['properties']['endpoint']['default'] = '/calendars/v3/calendar/{$calendarId}/events';
2120

2221
$schema['properties']['backend']['default'] = 'Calendar API';
2322

@@ -79,7 +78,7 @@ function ( $defaults, $addon, $schema ) {
7978
'name' => 'scope',
8079
'label' => __( 'Scope', 'forms-bridge' ),
8180
'type' => 'text',
82-
'value' => 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events',
81+
'value' => 'https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar.events',
8382
'required' => true,
8483
),
8584
array(
@@ -124,13 +123,13 @@ function ( $defaults, $addon, $schema ) {
124123
),
125124
'bridge' => array(
126125
'backend' => 'Calendar API',
127-
'endpoint' => '',
126+
'endpoint' => '/calendar/v3/calendars/{$calendarId}/events',
128127
),
129128
'credential' => array(
130129
'name' => '',
131130
'schema' => 'Bearer',
132131
'oauth_url' => 'https://accounts.google.com/o/oauth2/v2',
133-
'scope' => 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events',
132+
'scope' => 'https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar.events',
134133
'client_id' => '',
135134
'client_secret' => '',
136135
'access_token' => '',
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Google Calendar event dates job
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
if ( ! defined( 'ABSPATH' ) ) {
9+
exit();
10+
}
11+
12+
return array(
13+
'title' => __( 'Event dates', 'forms-bridge' ),
14+
'description' => array(
15+
'Given a datetime and a duration in hours and minuts, sets up the event dates',
16+
'forms-bridge',
17+
),
18+
'method' => 'forms_bridge_gcalendar_event_dates',
19+
'input' => array(
20+
array(
21+
'name' => 'datetime',
22+
'schema' => array( 'type' => 'string' ),
23+
'required' => true,
24+
),
25+
array(
26+
'name' => 'duration_hours',
27+
'schema' => array( 'type' => 'string' ),
28+
'required' => true,
29+
),
30+
array(
31+
'name' => 'duration_minutes',
32+
'schema' => array( 'type' => 'string' ),
33+
),
34+
),
35+
'output' => array(
36+
array(
37+
'name' => 'start',
38+
'schema' => array(
39+
'type' => 'object',
40+
'properties' => array(
41+
'dateTime' => array( 'type' => 'string' ),
42+
'timeZone' => array( 'type' => 'string' ),
43+
),
44+
'additionalProperties' => false,
45+
'required' => array( 'dateTime' ),
46+
),
47+
),
48+
array(
49+
'name' => 'end',
50+
'schema' => array(
51+
'type' => 'object',
52+
'properties' => array(
53+
'dateTime' => array( 'type' => 'string' ),
54+
'timeZone' => array( 'type' => 'string' ),
55+
),
56+
'additionalProperties' => false,
57+
'required' => array( 'dateTime' ),
58+
),
59+
),
60+
),
61+
);
62+
63+
/**
64+
* Given a datetime and a duration in hours and minuts, sets up the event dates,
65+
*
66+
* @param array $payload Bridge payload.
67+
*
68+
* @return array
69+
*/
70+
function forms_bridge_gcalendar_event_dates( $payload ) {
71+
$datetime = $payload['datetime'];
72+
$time = strtotime( $datetime );
73+
$endtime = $time + $payload['duration_hours'] * 3600 + ( $payload['duration_minutes'] ?? 0 ) * 60;
74+
75+
$timezone = wp_timezone_string();
76+
77+
$payload['start'] = array(
78+
'dateTime' => gmdate( 'Y-m-d\TH:i:s', $time ),
79+
'timeZone' => $timezone,
80+
);
81+
82+
$payload['end'] = array(
83+
'dateTime' => gmdate( 'Y-m-d\TH:i:s', $endtime ),
84+
'timeZone' => $timezone,
85+
);
86+
87+
return $payload;
88+
}

0 commit comments

Comments
 (0)