PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.0
Timetics – Appointment Booking Calendar & Scheduling v1.0.0
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 / utils / global-helper.php

global-helper.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.0, at utils/global-helper.php

450 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 use function Timetics\timetics;
3 use Timetics\Core\Bookings\Booking;
4 use Timetics\Core\Integrations\Google\Client;
5 use Timetics\Core\Integrations\Google\Service\Calendar;
6
7 /**
8 * Get timezone list.
9 *
10 * @since 1.0.0
11 *
12 * @return array
13 */
14 if ( ! function_exists( 'timetics_get_timezone_list' ) ) {
15 function timetics_get_timezone_list() {
16 $tzlist = wp_timezone_choice( 'UTC' );
17 $doc = new DOMDocument();
18 $doc->loadHTML( $tzlist );
19
20 $elements = $doc->getElementsByTagName( 'option' );
21 $timezones = [];
22
23 if ( ! empty( $elements ) ) {
24 foreach ( $elements as $element ) {
25 $data = [
26 'name' => $element->getAttribute( 'value' ),
27 'value' => $element->textContent, // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
28 ];
29
30 $timezones[] = $data;
31 }
32 }
33
34 return $timezones;
35 }
36 }
37
38 if ( ! function_exists( 'timetics_get_google_access_token' ) ) {
39 /**
40 * Get google access token
41 *
42 * @param integer $user_id Authenticate user id
43 *
44 * @since 1.0.0
45 *
46 * @return string
47 */
48 function timetics_get_google_access_token( $user_id = 0 ) {
49 $data = timetics_get_google_auth( $user_id );
50
51 if ( ! $data ) {
52 return false;
53 }
54
55 if ( ( $data['expires_in'] - 30 ) < time() ) {
56 $refresh_toekn = timetics_get_google_refresh_token( $user_id );
57 $client = timetics_get_google_client();
58
59 $data = $client->fetch_access_token_with_refresh_token( $refresh_toekn );
60
61 timetics_update_google_auth( $user_id, $data );
62 }
63
64 return $data['access_token'];
65 }
66 }
67
68 if ( ! function_exists( 'timetics_get_google_refresh_token' ) ) {
69
70 /**
71 * Get google auth refresh token
72 *
73 * @param integer $user_id Authenticate user id
74 *
75 * @return string
76 */
77 function timetics_get_google_refresh_token( $user_id ) {
78 return get_user_meta( $user_id, 'timetics_google_refresh_token', true );
79 }
80 }
81
82 if ( ! function_exists( 'timetics_get_auth_redirect_uri' ) ) {
83 /**
84 * Get auth redirect uri
85 *
86 * @param string $auth
87 *
88 * @return string
89 */
90 function timetics_get_auth_redirect_uri( $auth = '' ) {
91 return site_url( 'timetics-integration/' . $auth );
92 }
93 }
94
95 if ( ! function_exists( 'timetics_update_google_auth' ) ) {
96 /**
97 * Update google auth token
98 *
99 * @param integer $user_id
100 * @param array $data
101 *
102 * @return void
103 */
104 function timetics_update_google_auth( $user_id = 0, $data = [] ) {
105 if ( ! empty( $data['code'] ) ) {
106 update_user_meta( $user_id, 'timetics_google_auth_code', $data['code'] );
107 }
108
109 if ( ! empty( $data['refresh_token'] ) ) {
110 update_user_meta( $user_id, 'timetics_google_refresh_token', $data['refresh_token'] );
111 }
112
113 $data['expires_in'] = $data['expires_in'] + time();
114
115 update_user_meta( $user_id, 'timetics_google_auth', $data );
116 }
117 }
118
119 if ( ! function_exists( 'timetics_get_google_auth' ) ) {
120 /**
121 * Get google auth
122 *
123 * @param integer $user_id
124 *
125 * @return array
126 */
127 function timetics_get_google_auth( $user_id = 0 ) {
128 return get_user_meta( $user_id, 'timetics_google_auth', true );
129 }
130 }
131
132 if ( ! function_exists( 'timetics_get_google_client' ) ) {
133 /**
134 * Get google auth client
135 *
136 * @return Timetics\Core\Integrations\Google\Client
137 */
138 function timetics_get_google_client() {
139 $client_id = timetics_get_option( 'google_app_client_id' );
140 $client_secret = timetics_get_option( 'google_app_client_secret' );
141
142 $redirect_uri = timetics_get_auth_redirect_uri( 'google-auth' );
143
144 $client = new Client();
145 $client->add_scope( Calendar::scope() );
146 $client->set_auth_config(
147 [
148 'client_id' => $client_id,
149 'client_secrete' => $client_secret,
150 ]
151 );
152
153 $client->set_redirect_uri( $redirect_uri );
154
155 return $client;
156 }
157 }
158
159 if ( ! function_exists( 'timetics_get_google_auth_url' ) ) {
160 /**
161 * Get google auth url
162 *
163 * @return string
164 */
165 function timetics_get_google_auth_url() {
166 $client = timetics_get_google_client();
167
168 return $client->get_auth_url();
169 }
170 }
171
172 if ( ! function_exists( 'timetics_get_posts' ) ) {
173 /**
174 * Get posts with date range
175 *
176 * @param array $args
177 *
178 * @return Object
179 */
180 function timetics_get_posts( $args = [] ) {
181 $defaults = [
182 'post_type' => 'post',
183 'post_status' => 'any',
184 'nopaging' => true,
185 'date_range' => '',
186 ];
187
188 $args = wp_parse_args( $args, $defaults );
189
190 // Date range found.
191 if ( $args['date_range'] ) {
192 $from_date = $args['date_range']['start'];
193 $to_date = $args['date_range']['end'];
194
195 $args['date_query'] = [
196 'relation' => 'AND',
197 [
198 'before' => [
199 'year' => gmdate( 'Y', strtotime( $to_date ) ),
200 'month' => gmdate( 'm', strtotime( $to_date ) ),
201 'day' => gmdate( 'd', strtotime( $to_date ) ),
202 ],
203 'after' => [
204 'year' => gmdate( 'Y', strtotime( $from_date ) ),
205 'month' => gmdate( 'm', strtotime( $from_date ) ),
206 'day' => gmdate( 'd', strtotime( $from_date ) ),
207 ],
208 'inclusive' => true,
209 ],
210 ];
211 }
212
213 // The Query
214 $posts = new WP_Query( $args );
215
216 return $posts;
217 }
218 }
219
220 if ( ! function_exists( 'timetics_count_posts()' ) ) {
221 /**
222 * Count post with date range or without date range
223 *
224 * @param array $args Required query params
225 *
226 * @return integer
227 */
228 function timetics_count_posts( $args = [] ) {
229 $posts = timetics_get_posts( $args );
230
231 return $posts->post_count;
232 }
233 }
234
235 if ( ! function_exists( 'timetics_count_users' ) ) {
236 /**
237 * Count user by role and date range
238 *
239 * @param array $args Required query params
240 *
241 * @return integer
242 */
243 function timetics_count_users( $args = [] ) {
244 $defaults = [
245 'role' => '',
246 'date_range' => '',
247 'count_total' => true,
248 ];
249
250 $args = wp_parse_args( $args, $defaults );
251
252 // Date range found.
253 if ( $args['date_range'] ) {
254 $from_date = $args['date_range']['start'];
255 $to_date = $args['date_range']['end'];
256
257 $args['date_query'] = [
258 'relation' => 'AND',
259 [
260 'before' => [
261 'year' => gmdate( 'Y', strtotime( $to_date ) ),
262 'month' => gmdate( 'm', strtotime( $to_date ) ),
263 'day' => gmdate( 'd', strtotime( $to_date ) ),
264 ],
265 'after' => [
266 'year' => gmdate( 'Y', strtotime( $from_date ) ),
267 'month' => gmdate( 'm', strtotime( $from_date ) ),
268 'day' => gmdate( 'd', strtotime( $from_date ) ),
269 ],
270 'inclusive' => true,
271 ],
272 ];
273 }
274
275 // The Query
276 $user_query = new WP_User_Query( $args );
277
278 // Return total users.
279 return $user_query->total_users;
280 }
281 }
282
283 if ( ! function_exists( 'timetics_get_total_sale' ) ) {
284 /**
285 * Get total sale with date range or without date range
286 *
287 * @param array $args Sales Required Args
288 *
289 * @return integer
290 */
291 function timetics_get_total_sale( $args = [] ) {
292 $defaults = [
293 'post_type' => 'timetics-booking',
294 ];
295 $args = wp_parse_args( $args, $defaults );
296
297 $posts = timetics_get_posts( $args );
298
299 $total = 0;
300
301 foreach ( $posts->posts as $post ) {
302 $booking = new Booking( $post->ID );
303 $total += $booking->get_total();
304 }
305
306 return $total;
307 }
308 }
309
310 if ( ! function_exists( 'timetics_get_staff_integrations' ) ) {
311 /**
312 * Get all staff integrations
313 *
314 * @return array
315 */
316 function timetics_get_staff_integrations( $user_id = 0 ) {
317 $google_auth = get_user_meta( $user_id, 'timetics_google_auth_code', true );
318 $google_credentials = timetics_get_google_credentials();
319 $is_setup = ! empty( $google_credentials['client_id'] ) && ! empty( $google_credentials['client_secret'] );
320 $current_user_id = ($user_id == get_current_user_id()) ? true : false;
321
322
323 $integrations = [];
324 $integrations[] = [
325 'id' => 'google-calendar-meet',
326 'name' => __( 'Google Meet', 'timetics' ),
327 'description' => __('Connect your Meet to sync your booked events.', 'timetics'),
328 'auth_url' => timetics_get_google_auth_url(),
329 'connected' => ! empty( $google_auth ),
330 'setup' => $is_setup,
331 'current_user' => $current_user_id,
332 ];
333 $integrations[] = [
334 'id' => 'google-calendar',
335 'name' => __( 'Google Calendar', 'timetics' ),
336 'description' => __('Connect your Meet to sync your booked events.', 'timetics'),
337 'auth_url' => timetics_get_google_auth_url(),
338 'connected' => ! empty( $google_auth ),
339 'setup' => $is_setup,
340 'current_user' => $current_user_id,
341 ];
342
343 return $integrations;
344 }
345 }
346
347 if ( ! function_exists( 'timetics_get_google_credentials' ) ) {
348 /**
349 * Get google auth credentials
350 *
351 * @return array
352 */
353 function timetics_get_google_credentials() {
354 $client_id = timetics_get_option( 'google_app_client_id' );
355 $client_secret = timetics_get_option( 'google_app_client_secret' );
356
357 return [
358 'client_id' => $client_id,
359 'client_secret' => $client_secret,
360 ];
361 }
362 }
363
364 if ( ! function_exists( 'timetics_get_default_timezone' ) ) {
365 /**
366 * Get default timezone
367 *
368 * @return string
369 */
370 function timetics_get_default_timezone() {
371 $timezone = date_default_timezone_get();
372 $offset = get_option( 'gmt_offset' );
373
374 if ( $offset < 0 ) {
375 $timezone = $timezone . $offset;
376 } else {
377 $timezone = $timezone . '+' . $offset;
378 }
379
380 return $timezone;
381 }
382 }
383
384 if ( ! function_exists( 'timetics_get_currencies' ) ) {
385 /**
386 * Get currency list
387 *
388 * @return array
389 */
390 function timetics_get_currencies() {
391 $currencies = require_once dirname( __FILE__ ) . '/currency.php';
392
393 return $currencies;
394 }
395 }
396
397 if ( ! function_exists( 'timetics_update_default_settings' ) ) {
398
399 /**
400 * Update default settings
401 *
402 * @return void
403 */
404 function timetics_update_default_settings() {
405 $settings = [
406 'google_auth_redirect_uri' => timetics_get_auth_redirect_uri( 'google-auth' ),
407 ];
408
409 foreach ( $settings as $key => $value ) {
410 timetics_update_option( $key, $value );
411 }
412 }
413 }
414
415 if ( ! function_exists( 'timetics_google_setup' ) ) {
416
417 /**
418 * Checking google auth is configured or not
419 *
420 * @return bool
421 */
422 function timetics_google_setup() {
423 $client_id = timetics_get_option( 'google_app_client_id' );
424 $client_secret = timetics_get_option( 'google_app_client_secret' );
425
426 if ( $client_id && $client_secret ) {
427 return true;
428 }
429
430 return false;
431 }
432 }
433
434 if ( ! function_exists( 'timetics_get_post_status' ) ) {
435
436 /**
437 * Get post status
438 *
439 * @return array
440 */
441 function timetics_get_post_status() {
442 return [
443 'approved',
444 'pending',
445 'cancel',
446 'completed',
447 ];
448 }
449 }
450