PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.14
Timetics – Appointment Booking Calendar & Scheduling v1.0.14
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / integrations / google / service / calendar.php

calendar.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.14, at core/integrations/google/service/calendar.php

286 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Google Calendar Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Integrations\Google\Service;
8
9 /**
10 * Class Calendar
11 */
12 class Calendar {
13 const TIMETICS_TIMEZONE_URI = 'https://www.googleapis.com/calendar/v3/users/me/settings/timezone';
14 const TIMETICS_CALENDAR_EVENT = 'https://www.googleapis.com/calendar/v3/calendars/primary/events/';
15
16 /**
17 * Create event
18 *
19 * @param array $args Event data
20 *
21 * @return JSON | WP_Error
22 */
23 public function create_event( $args = [] ) {
24 $defaults = [
25 'summary' => '',
26 'description' => '',
27 'location' => '',
28 'start' => '',
29 'end' => '',
30 'attendees' => [],
31 'google_meet' => true,
32 'access_token' => '',
33 ];
34
35 $args = wp_parse_args( $args, $defaults );
36 $data = $this->prepare_request_data( $args );
37
38 $query_params = build_query( [
39 'conferenceDataVersion' => '1',
40 // 'sendUpdates' => 'all',
41 ] );
42
43 $response = wp_remote_post( self::TIMETICS_CALENDAR_EVENT . '?' . $query_params, $data );
44
45 if ( is_wp_error( $response ) ) {
46 return false;
47 }
48
49 $status_code = wp_remote_retrieve_response_code( $response );
50
51 if ( 200 != $status_code ) {
52 return false;
53 }
54
55 $data = json_decode( wp_remote_retrieve_body( $response ), true );
56
57 return $data;
58 }
59
60 /**
61 * Update calender event
62 *
63 * @param array $args
64 *
65 * @return array
66 */
67 public function update_event( $event_id, $args ) {
68 $defaults = [
69 'summary' => '',
70 'description' => '',
71 'location' => '',
72 'start' => '',
73 'end' => '',
74 'attendees' => [],
75 'google_meet' => true,
76 'access_token' => '',
77 'method' => 'PUT',
78 ];
79
80 $args = wp_parse_args( $args, $defaults );
81 $query_params = build_query( [
82 'conferenceDataVersion' => '1',
83 'sendUpdates' => 'all',
84 ] );
85
86 $data = $this->prepare_request_data( $args );
87 $data['method'] = 'PUT';
88
89 $response = wp_remote_post( self::TIMETICS_CALENDAR_EVENT . $event_id . '?' . $query_params, $data );
90
91 if ( is_wp_error( $response ) ) {
92 return false;
93 }
94
95 $status_code = wp_remote_retrieve_response_code( $response );
96
97 if ( 200 != $status_code ) {
98 return false;
99 }
100
101 $data = json_decode( wp_remote_retrieve_body( $response ), true );
102
103 return $data;
104 }
105
106 /**
107 * Get timzeson
108 *
109 * @return string | WP_Error
110 */
111 public function get_timezone( $access_token ) {
112 $data = [
113 'headers' => [
114 'Authorization' => 'Bearer ' . $access_token,
115 ],
116 ];
117
118 $response = wp_remote_get( self::TIMETICS_TIMEZONE_URI, $data );
119
120 if ( ! is_wp_error( $response ) ) {
121 $data = json_decode( wp_remote_retrieve_body( $response ), true );
122 return $data['value'];
123 }
124
125 return $response;
126 }
127
128 /**
129 * Delete google calendar event
130 *
131 * @param string $event_id
132 *
133 * @return array
134 */
135 public function delete_event( $event_id, $access_token ) {
136 $query_params = build_query( [
137 'conferenceDataVersion' => '1',
138 'sendUpdates' => 'all',
139 ] );
140
141 $response = wp_remote_post( self::TIMETICS_CALENDAR_EVENT . $event_id . '?' . $query_params, [
142 'headers' => [
143 'Authorization' => 'Bearer ' . $access_token,
144 'Content-Type' => 'application/json; charset=utf-8',
145 ],
146 'method' => 'DELETE',
147 ] );
148
149 if ( is_wp_error( $response ) ) {
150 return false;
151 }
152
153 $status_code = wp_remote_retrieve_response_code( $response );
154
155 if ( 200 != $status_code ) {
156 return false;
157 }
158
159 $data = json_decode( wp_remote_retrieve_body( $response ), true );
160
161 return $data;
162 }
163
164 /**
165 * Get timezone offset
166 *
167 * @param string $timezone
168 *
169 * @return string
170 */
171 public function get_timezone_offset( $timezone ) {
172 $current = timezone_open( $timezone );
173 $utc_time = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
174 $offset_insecs = timezone_offset_get( $current, $utc_time );
175 $hours_and_sec = gmdate( 'H:i', abs( $offset_insecs ) );
176
177 return stripos( $offset_insecs, '-' ) === false ? "+{$hours_and_sec}" : "-{$hours_and_sec}";
178 }
179
180 /**
181 * Prepare time for calendar event
182 *
183 * @param array $data
184 *
185 * @return array
186 */
187 private function prepare_time( $data, $access_token ) {
188 $start_date = isset( $data['start']['date'] ) ? $data['start']['date'] : gmdate( 'Y-m-d' );
189 $start_time = isset( $data['start']['time'] ) ? $data['start']['time'] : gmdate( 'H:i:s' );
190 $end_date = isset( $data['end']['date'] ) ? $data['end']['date'] : gmdate( 'Y-m-d' );
191 $end_time = isset( $data['end']['time'] ) ? $data['end']['time'] : gmdate( 'H:i:s' );
192 $timezone = $this->get_timezone( $access_token );
193 $timezone_offset = $this->get_timezone_offset( $timezone );
194
195 $start_date_time = $start_date . 'T' . $this->convertTo24HourFormat( $start_time ) . $timezone_offset;
196 $end_date_time = $end_date . 'T' . $this->convertTo24HourFormat( $end_time ) . $timezone_offset;
197
198 $date_time = [
199 'start' => [
200 'dateTime' => $start_date_time,
201 'timeZone' => $timezone,
202 ],
203 'end' => [
204 'dateTime' => $end_date_time,
205 'timeZone' => $timezone,
206 ],
207 ];
208
209 return $date_time;
210 }
211
212 /**
213 * Convet 12 hours format to 24 hours format
214 *
215 * @param string $time
216 *
217 * @return string
218 */
219 public function convertTo24HourFormat( $time ) {
220 $hours = intval( substr( $time, 0, 2 ) );
221 $minutes = intval( substr( $time, 3, 2 ) );
222 $seconds = intval( substr( $time, 6, 2 ) );
223 $ampm = substr( $time, 9 );
224
225 if ( strtolower( $ampm ) == 'pm' && $hours < 12 ) {
226 $hours += 12;
227 }
228
229 if ( strtolower( $ampm ) == "am" && $hours == 12 ) {
230 $hours = 0;
231 }
232
233 return sprintf( "%02d:%02d:%02d", $hours, $minutes, $seconds );
234 }
235
236 /**
237 * Prepare event create requested data
238 *
239 * @param array $args
240 *
241 * @return array
242 */
243 private function prepare_request_data( $args = [] ) {
244 $access_token = $args['access_token'];
245 $date = $this->prepare_time(
246 [
247 'start' => $args['start'],
248 'end' => $args['end'],
249 ],
250 $access_token
251 );
252
253 $args['start'] = [$date['start']];
254 $args['end'] = [$date['end']];
255
256 if ( $args['google_meet'] ) {
257 $args['conferenceData'] = [
258 'createRequest' => [
259 'requestId' => 'sample123',
260 'conferenceSolutionKey' => ['type' => 'hangoutsMeet'],
261 ],
262 ];
263 }
264
265 unset( $args['access_token'] );
266 $data = [
267 'headers' => [
268 'Authorization' => 'Bearer ' . $access_token,
269 'Content-Type' => 'application/json; charset=utf-8',
270 ],
271 'body' => json_encode( $args ),
272 ];
273
274 return $data;
275 }
276
277 /**
278 * Get google calendar auth scope
279 *
280 * @return string
281 */
282 public static function scope() {
283 return 'https://www.googleapis.com/auth/calendar';
284 }
285 }
286