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