Skip to content

Commit 5bd185c

Browse files
authored
Merge pull request #13 from codeccoop/feat/google-calendar
feat: gcalendar addon
2 parents 548f7b9 + 9da3e4e commit 5bd185c

File tree

14 files changed

+2472
-8
lines changed

14 files changed

+2472
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Forms Bridge has the following addons:
4141
- [Brevo](https://formsbridge.codeccoop.org/documentation/brevo/)
4242
- [Dolibarr](https://formsbridge.codeccoop.org/documentation/dolibarr/)
4343
- [FinanCoop](https://formsbridge.codeccoop.org/documentation/financoop/)
44+
- [Google Calendar](https://formsbridge.codeccoop.org/documentation/google-calendar/)
4445
- [Google Sheets](https://formsbridge.codeccoop.org/documentation/google-sheets/)
4546
- [Holded](https://formsbridge.codeccoop.org/documentation/holded/)
4647
- [Listmonk](https://formsbridge.codeccoop.org/documentation/listmonk/)
6.74 KB
Loading
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
/**
3+
* Class GCalendar_Addon
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
namespace FORMS_BRIDGE;
9+
10+
use FBAPI;
11+
use WP_Error;
12+
13+
if ( ! defined( 'ABSPATH' ) ) {
14+
exit();
15+
}
16+
17+
require_once 'class-gcalendar-form-bridge.php';
18+
require_once 'hooks.php';
19+
20+
/**
21+
* Google Calendar addon class.
22+
*/
23+
class GCalendar_Addon extends Addon {
24+
25+
/**
26+
* Handles the addon's title.
27+
*
28+
* @var string
29+
*/
30+
const TITLE = 'Google Calendar';
31+
32+
/**
33+
* Handles the addon's name.
34+
*
35+
* @var string
36+
*/
37+
const NAME = 'gcalendar';
38+
39+
/**
40+
* Handles the addon's custom bridge class.
41+
*
42+
* @var string
43+
*/
44+
const BRIDGE = '\FORMS_BRIDGE\GCalendar_Form_Bridge';
45+
46+
/**
47+
* Performs a request against the backend to check the connexion status.
48+
*
49+
* @param string $backend Backend name.
50+
*
51+
* @return boolean
52+
*/
53+
public function ping( $backend ) {
54+
$bridge = new GCalendar_Form_Bridge(
55+
array(
56+
'name' => '__gcalendar-' . time(),
57+
'backend' => $backend,
58+
'endpoint' => '/calendar/v3/users/me/calendarList',
59+
'method' => 'GET',
60+
)
61+
);
62+
63+
$backend = $bridge->backend;
64+
if ( ! $backend ) {
65+
Logger::log( 'Google Calendar backend ping error: Bridge has no valid backend', Logger::ERROR );
66+
return false;
67+
}
68+
69+
$credential = $backend->credential;
70+
if ( ! $credential ) {
71+
Logger::log( 'Google Calendar backend ping error: Backend has no valid credential', Logger::ERROR );
72+
return false;
73+
}
74+
75+
$parsed = wp_parse_url( $backend->base_url );
76+
$host = $parsed['host'] ?? '';
77+
78+
if ( 'www.googleapis.com' !== $host ) {
79+
Logger::log( 'Google Calendar backend ping error: Backend does not point to the Google Calendar API endpoints', Logger::ERROR );
80+
return false;
81+
}
82+
83+
$access_token = $credential->get_access_token();
84+
85+
if ( ! $access_token ) {
86+
Logger::log( 'Google Calendar backend ping error: Unable to recover the credential access token', Logger::ERROR );
87+
return false;
88+
}
89+
90+
return true;
91+
}
92+
93+
/**
94+
* Performs a GET request against the backend endpoint and retrieve the response data.
95+
*
96+
* @param string $endpoint Calendar ID or endpoint.
97+
* @param string $backend Backend name.
98+
*
99+
* @return array|WP_Error
100+
*/
101+
public function fetch( $endpoint, $backend ) {
102+
$backend = FBAPI::get_backend( $backend );
103+
if ( ! $backend ) {
104+
return new WP_Error( 'invalid_backend' );
105+
}
106+
107+
$credential = $backend->credential;
108+
if ( ! $credential ) {
109+
return new WP_Error( 'invalid_credential' );
110+
}
111+
112+
$access_token = $credential->get_access_token();
113+
if ( ! $access_token ) {
114+
return new WP_Error( 'invalid_credential' );
115+
}
116+
117+
$response = http_bridge_get(
118+
'https://www.googleapis.com/calendar/v3/users/me/calendarList',
119+
array(),
120+
array(
121+
'Authorization' => "Bearer {$access_token}",
122+
'Accept' => 'application/json',
123+
)
124+
);
125+
126+
if ( is_wp_error( $response ) ) {
127+
return $response;
128+
}
129+
130+
return $response;
131+
}
132+
133+
/**
134+
* Performs an introspection of the backend endpoint and returns API fields
135+
* and accepted content type.
136+
*
137+
* @param string $endpoint Calendar ID.
138+
* @param string $backend Backend name.
139+
* @param string|null $method HTTP method.
140+
*
141+
* @return array List of fields and content type of the endpoint.
142+
*/
143+
public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
144+
if ( ! in_array( $method, array( 'POST', 'PUT' ), true ) ) {
145+
return array();
146+
}
147+
148+
return array(
149+
array(
150+
'name' => 'summary',
151+
'schema' => array(
152+
'type' => 'string',
153+
'description' => 'Event title',
154+
),
155+
),
156+
array(
157+
'name' => 'description',
158+
'schema' => array(
159+
'type' => 'string',
160+
'description' => 'Event description',
161+
),
162+
),
163+
array(
164+
'name' => 'location',
165+
'schema' => array(
166+
'type' => 'string',
167+
'description' => 'Event location',
168+
),
169+
),
170+
array(
171+
'name' => 'start',
172+
'schema' => array(
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' ),
186+
),
187+
),
188+
array(
189+
'name' => 'end',
190+
'schema' => array(
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' ),
204+
),
205+
),
206+
array(
207+
'name' => 'attendees',
208+
'schema' => array(
209+
'type' => 'array',
210+
'items' => array(
211+
'type' => 'string',
212+
'description' => 'attendee email address',
213+
),
214+
'additionalItems' => true,
215+
),
216+
),
217+
);
218+
}
219+
}
220+
221+
GCalendar_Addon::setup();

0 commit comments

Comments
 (0)