PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.24
Timetics – Appointment Booking Calendar & Scheduling v1.0.24
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.24, at core/integrations/google/service/calendar.php

276 lines 7.6 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 = apply_filters( 'timetics/booking/create/google-event', $args);
36
37 $args = wp_parse_args( $args, $defaults );
38 $data = $this->prepare_request_data( $args );
39
40 $query_params = build_query( [
41 'conferenceDataVersion' => '1',
42 // 'sendUpdates' => 'all',
43 ] );
44
45 $response = wp_remote_post( self::TIMETICS_CALENDAR_EVENT . '?' . $query_params, $data );
46
47 if ( is_wp_error( $response ) ) {
48 return false;
49 }
50
51 $status_code = wp_remote_retrieve_response_code( $response );
52
53 if ( 200 != $status_code ) {
54 return false;
55 }
56
57 $data = json_decode( wp_remote_retrieve_body( $response ), true );
58
59 return $data;
60 }
61
62 /**
63 * Update calender event
64 *
65 * @param array $args
66 *
67 * @return array
68 */
69 public function update_event( $event_id, $args ) {
70 $defaults = [
71 'summary' => '',
72 'description' => '',
73 'location' => '',
74 'start' => '',
75 'end' => '',
76 'attendees' => [],
77 'google_meet' => true,
78 'access_token' => '',
79 'method' => 'PUT',
80 ];
81
82 $args = wp_parse_args( $args, $defaults );
83 $query_params = build_query( [
84 'conferenceDataVersion' => '1',
85 'sendUpdates' => 'all',
86 ] );
87
88 $data = $this->prepare_request_data( $args );
89 $data['method'] = 'PUT';
90
91 $response = wp_remote_post( self::TIMETICS_CALENDAR_EVENT . $event_id . '?' . $query_params, $data );
92
93 if ( is_wp_error( $response ) ) {
94 return false;
95 }
96
97 $status_code = wp_remote_retrieve_response_code( $response );
98
99 if ( 200 != $status_code ) {
100 return false;
101 }
102
103 $data = json_decode( wp_remote_retrieve_body( $response ), true );
104
105 return $data;
106 }
107
108 /**
109 * Get timzeson
110 *
111 * @return string | WP_Error
112 */
113 public function get_timezone( $access_token ) {
114 $data = [
115 'headers' => [
116 'Authorization' => 'Bearer ' . $access_token,
117 ],
118 ];
119
120 $response = wp_remote_get( self::TIMETICS_TIMEZONE_URI, $data );
121
122 if ( ! is_wp_error( $response ) ) {
123 $data = json_decode( wp_remote_retrieve_body( $response ), true );
124 return $data['value'];
125 }
126
127 return $response;
128 }
129
130 /**
131 * Delete google calendar event
132 *
133 * @param string $event_id
134 *
135 * @return array
136 */
137 public function delete_event( $event_id, $access_token ) {
138 $query_params = build_query( [
139 'conferenceDataVersion' => '1',
140 'sendUpdates' => 'all',
141 ] );
142
143 $response = wp_remote_post( self::TIMETICS_CALENDAR_EVENT . $event_id . '?' . $query_params, [
144 'headers' => [
145 'Authorization' => 'Bearer ' . $access_token,
146 'Content-Type' => 'application/json; charset=utf-8',
147 ],
148 'method' => 'DELETE',
149 ] );
150
151 if ( is_wp_error( $response ) ) {
152 return false;
153 }
154
155 $status_code = wp_remote_retrieve_response_code( $response );
156
157 if ( 200 != $status_code ) {
158 return false;
159 }
160
161 $data = json_decode( wp_remote_retrieve_body( $response ), true );
162
163 return $data;
164 }
165
166 /**
167 * Get timezone offset
168 *
169 * @param string $timezone
170 *
171 * @return string
172 */
173 public function get_timezone_offset( $timezone ) {
174 $current = timezone_open( $timezone );
175 $utc_time = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
176 $offset_insecs = timezone_offset_get( $current, $utc_time );
177 $hours_and_sec = gmdate( 'H:i', abs( $offset_insecs ) );
178
179 return stripos( $offset_insecs, '-' ) === false ? "+{$hours_and_sec}" : "-{$hours_and_sec}";
180 }
181
182 /**
183 * Prepare time for calendar event
184 *
185 * @param array $data
186 *
187 * @return array
188 */
189 private function prepare_time( $data, $access_token ) {
190 $start_date = isset( $data['start']['date'] ) ? $data['start']['date'] : gmdate( 'Y-m-d' );
191 $start_time = isset( $data['start']['time'] ) ? $data['start']['time'] : gmdate( 'H:i:s' );
192 $end_date = isset( $data['end']['date'] ) ? $data['end']['date'] : gmdate( 'Y-m-d' );
193 $end_time = isset( $data['end']['time'] ) ? $data['end']['time'] : gmdate( 'H:i:s' );
194 $timezone = isset( $data['timezone'] ) ? $data['timezone'] : 'Asia/Dhaka';
195 $timezone_offset = $this->get_timezone_offset( $timezone );
196
197 $start_date_time = $start_date . 'T' . $this->convertTo24HourFormat( $start_time ) . $timezone_offset;
198 $end_date_time = $end_date . 'T' . $this->convertTo24HourFormat( $end_time ) . $timezone_offset;
199
200 $date_time = [
201 'start' => [
202 'dateTime' => $start_date_time,
203 'timeZone' => $timezone,
204 ],
205 'end' => [
206 'dateTime' => $end_date_time,
207 'timeZone' => $timezone,
208 ],
209 ];
210
211 return $date_time;
212 }
213
214 /**
215 * Convet 12 hours format to 24 hours format
216 *
217 * @param string $time
218 *
219 * @return string
220 */
221 public function convertTo24HourFormat( $time ) {
222 return date('H:i:s', strtotime( $time ) );
223 }
224
225 /**
226 * Prepare event create requested data
227 *
228 * @param array $args
229 *
230 * @return array
231 */
232 private function prepare_request_data( $args = [] ) {
233 $access_token = $args['access_token'];
234 $date = $this->prepare_time(
235 [
236 'start' => $args['start'],
237 'end' => $args['end'],
238 'timezone' => $args['timezone'],
239 ],
240 $access_token
241 );
242
243 $args['start'] = [$date['start']];
244 $args['end'] = [$date['end']];
245
246 if ( $args['google_meet'] ) {
247 $args['conferenceData'] = [
248 'createRequest' => [
249 'requestId' => 'sample123',
250 'conferenceSolutionKey' => ['type' => 'hangoutsMeet'],
251 ],
252 ];
253 }
254
255 unset( $args['access_token'] );
256 $data = [
257 'headers' => [
258 'Authorization' => 'Bearer ' . $access_token,
259 'Content-Type' => 'application/json; charset=utf-8',
260 ],
261 'body' => json_encode( $args ),
262 ];
263
264 return $data;
265 }
266
267 /**
268 * Get google calendar auth scope
269 *
270 * @return string
271 */
272 public static function scope() {
273 return 'https://www.googleapis.com/auth/calendar';
274 }
275 }
276