PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.38
Timetics – Appointment Booking Calendar & Scheduling v1.0.38
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.38, at utils/global-helper.php

754 lines 20.1 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 'value' => $element->getAttribute( 'value' ),
27 'name' => $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 if ( ! $data ) {
62 return false;
63 }
64
65 timetics_update_google_auth( $user_id, $data );
66 }
67
68 return $data['access_token'];
69 }
70 }
71
72 if ( ! function_exists( 'timetics_get_google_refresh_token' ) ) {
73
74 /**
75 * Get google auth refresh token
76 *
77 * @param integer $user_id Authenticate user id
78 *
79 * @return string
80 */
81 function timetics_get_google_refresh_token( $user_id ) {
82 return get_user_meta( $user_id, 'timetics_google_refresh_token', true );
83 }
84 }
85
86 if ( ! function_exists( 'timetics_get_auth_redirect_uri' ) ) {
87 /**
88 * Get auth redirect uri
89 *
90 * @param string $auth
91 *
92 * @return string
93 */
94 function timetics_get_auth_redirect_uri( $auth = '' ) {
95 return site_url( 'timetics-integration/' . $auth );
96 }
97 }
98
99 if ( ! function_exists( 'timetics_update_google_auth' ) ) {
100 /**
101 * Update google auth token
102 *
103 * @param integer $user_id
104 * @param array $data
105 *
106 * @return void
107 */
108 function timetics_update_google_auth( $user_id = 0, $data = [] ) {
109 if ( ! empty( $data['code'] ) ) {
110 update_user_meta( $user_id, 'timetics_google_auth_code', $data['code'] );
111 }
112
113 if ( ! empty( $data['refresh_token'] ) ) {
114 update_user_meta( $user_id, 'timetics_google_refresh_token', $data['refresh_token'] );
115 }
116
117 $data['expires_in'] = $data['expires_in'] + time();
118
119 update_user_meta( $user_id, 'timetics_google_auth', $data );
120 }
121 }
122
123 if ( ! function_exists( 'timetics_get_google_auth' ) ) {
124 /**
125 * Get google auth
126 *
127 * @param integer $user_id
128 *
129 * @return array
130 */
131 function timetics_get_google_auth( $user_id = 0 ) {
132 return get_user_meta( $user_id, 'timetics_google_auth', true );
133 }
134 }
135
136 if ( ! function_exists( 'timetics_get_google_client' ) ) {
137 /**
138 * Get google auth client
139 *
140 * @return Timetics\Core\Integrations\Google\Client
141 */
142 function timetics_get_google_client() {
143 $client_id = timetics_get_option( 'google_app_client_id' );
144 $client_secret = timetics_get_option( 'google_app_client_secret' );
145
146 $redirect_uri = timetics_get_auth_redirect_uri( 'google-auth' );
147
148 $client = new Client();
149 $client->add_scope( Calendar::scope() );
150 $client->set_auth_config(
151 [
152 'client_id' => $client_id,
153 'client_secrete' => $client_secret,
154 ]
155 );
156
157 $client->set_redirect_uri( $redirect_uri );
158
159 return $client;
160 }
161 }
162
163 if ( ! function_exists( 'timetics_get_google_auth_url' ) ) {
164 /**
165 * Get google auth url
166 *
167 * @return string
168 */
169 function timetics_get_google_auth_url() {
170 $client = timetics_get_google_client();
171
172 return $client->get_auth_url();
173 }
174 }
175
176 if ( ! function_exists( 'timetics_get_posts' ) ) {
177 /**
178 * Get posts with date range
179 *
180 * @param array $args
181 *
182 * @return Object
183 */
184 function timetics_get_posts( $args = [] ) {
185 $defaults = [
186 'post_type' => 'post',
187 'post_status' => 'publish',
188 'nopaging' => true,
189 'date_range' => '',
190 ];
191
192 $args = wp_parse_args( $args, $defaults );
193
194 // Date range found.
195 if ( $args['date_range'] ) {
196 $from_date = $args['date_range']['start'];
197 $to_date = $args['date_range']['end'];
198
199 $args['date_query'] = [
200 'relation' => 'AND',
201 [
202 'before' => [
203 'year' => gmdate( 'Y', strtotime( $to_date ) ),
204 'month' => gmdate( 'm', strtotime( $to_date ) ),
205 'day' => gmdate( 'd', strtotime( $to_date ) ),
206 ],
207 'after' => [
208 'year' => gmdate( 'Y', strtotime( $from_date ) ),
209 'month' => gmdate( 'm', strtotime( $from_date ) ),
210 'day' => gmdate( 'd', strtotime( $from_date ) ),
211 ],
212 'inclusive' => true,
213 ],
214 ];
215 }
216
217 // The Query
218 $posts = new WP_Query( $args );
219
220 return $posts;
221 }
222 }
223
224 if ( ! function_exists( 'timetics_count_posts()' ) ) {
225 /**
226 * Count post with date range or without date range
227 *
228 * @param array $args Required query params
229 *
230 * @return integer
231 */
232 function timetics_count_posts( $args = [] ) {
233 $posts = timetics_get_posts( $args );
234
235 return $posts->post_count;
236 }
237 }
238
239 if ( ! function_exists( 'timetics_count_users' ) ) {
240 /**
241 * Count user by role and date range
242 *
243 * @param array $args Required query params
244 *
245 * @return integer
246 */
247 function timetics_count_users( $args = [] ) {
248 $defaults = [
249 'role' => '',
250 'date_range' => '',
251 'count_total' => true,
252 ];
253
254 $args = wp_parse_args( $args, $defaults );
255
256 // Date range found.
257 if ( $args['date_range'] ) {
258 $from_date = $args['date_range']['start'];
259 $to_date = $args['date_range']['end'];
260
261 $args['date_query'] = [
262 'relation' => 'AND',
263 [
264 'before' => [
265 'year' => gmdate( 'Y', strtotime( $to_date ) ),
266 'month' => gmdate( 'm', strtotime( $to_date ) ),
267 'day' => gmdate( 'd', strtotime( $to_date ) ),
268 ],
269 'after' => [
270 'year' => gmdate( 'Y', strtotime( $from_date ) ),
271 'month' => gmdate( 'm', strtotime( $from_date ) ),
272 'day' => gmdate( 'd', strtotime( $from_date ) ),
273 ],
274 'inclusive' => true,
275 ],
276 ];
277 }
278
279 // The Query
280 $user_query = new WP_User_Query( $args );
281
282 // Return total users.
283 return $user_query->total_users;
284 }
285 }
286
287 if ( ! function_exists( 'timetics_get_total_sale' ) ) {
288 /**
289 * Get total sale with date range or without date range
290 *
291 * @param array $args Sales Required Args
292 *
293 * @return integer
294 */
295 function timetics_get_total_sale( $args = [] ) {
296 $defaults = [
297 'post_type' => 'timetics-booking',
298 'post_status' => 'approved',
299 ];
300
301 $args = wp_parse_args( $args, $defaults );
302
303 $posts = timetics_get_posts( $args );
304
305 $total = 0;
306
307 foreach ( $posts->posts as $post ) {
308 $booking = new Booking( $post->ID );
309 $total += floatval( $booking->get_total() );
310 }
311
312 return $total;
313 }
314 }
315
316 if ( ! function_exists( 'timetics_get_staff_integrations' ) ) {
317 /**
318 * Get all staff integrations
319 *
320 * @return array
321 */
322 function timetics_get_staff_integrations( $user_id = 0 ) {
323 $google_auth = timetics_get_google_access_token( $user_id );
324 $google_credentials = timetics_get_google_credentials();
325 $is_setup = ! empty( $google_credentials['client_id'] ) && ! empty( $google_credentials['client_secret'] );
326 $current_user_id = ( $user_id == get_current_user_id() ) ? true : false;
327
328 $integrations = [
329 [
330 'id' => 'google-calendar-meet',
331 'name' => esc_html__( 'Google Meet', 'timetics' ),
332 'description' => esc_html__( 'Connect your Meet to sync your booked events.', 'timetics' ),
333 'auth_url' => timetics_get_google_auth_url(),
334 'connected' => ! empty( $google_auth ),
335 'setup' => $is_setup,
336 'current_user' => $current_user_id,
337 ],
338 [
339 'id' => 'google-calendar',
340 'name' => esc_html__( 'Google Calendar', 'timetics' ),
341 'description' => esc_html__( 'Connect your Meet to sync your booked events.', 'timetics' ),
342 'auth_url' => timetics_get_google_auth_url(),
343 'connected' => ! empty( $google_auth ),
344 'setup' => $is_setup,
345 'current_user' => $current_user_id,
346 ],
347 ];
348
349 $integrations = apply_filters( 'timetics_staff_integrations', $integrations, $user_id );
350
351 return $integrations;
352 }
353 }
354
355 if ( ! function_exists( 'timetics_get_google_credentials' ) ) {
356 /**
357 * Get google auth credentials
358 *
359 * @return array
360 */
361 function timetics_get_google_credentials() {
362 $client_id = timetics_get_option( 'google_app_client_id' );
363 $client_secret = timetics_get_option( 'google_app_client_secret' );
364
365 return [
366 'client_id' => $client_id,
367 'client_secret' => $client_secret,
368 ];
369 }
370 }
371
372 if ( ! function_exists( 'timetics_get_default_timezone' ) ) {
373 /**
374 * Get default timezone
375 *
376 * @return string
377 */
378 function timetics_get_default_timezone() {
379 $timezone = get_option( 'timezone_string' );
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 'outlook_auth_redirect_uri'=> timetics_get_auth_redirect_uri( 'outlook-auth' ),
408 'default_booking_status' => 'pending',
409 'currency' => 'USD',
410 'primary_color' => '#3161F1',
411 'secondary_color' => '#6188ff',
412 ];
413
414 $settings = apply_filters( 'timetics_default_settings', $settings );
415
416 foreach ( $settings as $key => $value ) {
417 timetics_update_option( $key, $value );
418 }
419 }
420 }
421
422 if ( ! function_exists( 'timetics_google_setup' ) ) {
423
424 /**
425 * Checking google auth is configured or not
426 *
427 * @return bool
428 */
429 function timetics_google_setup() {
430 $client_id = timetics_get_option( 'google_app_client_id' );
431 $client_secret = timetics_get_option( 'google_app_client_secret' );
432
433 if ( $client_id && $client_secret ) {
434 return true;
435 }
436
437 return false;
438 }
439 }
440
441 if ( ! function_exists( 'timetics_get_post_status' ) ) {
442
443 /**
444 * Get post status
445 *
446 * @return array
447 */
448 function timetics_get_post_status() {
449 return [
450 'approved',
451 'pending',
452 'cancel',
453 'completed',
454 ];
455 }
456 }
457
458 if ( ! function_exists( 'timetics_get_payment_methods' ) ) {
459 /**
460 * Get payment methods
461 *
462 * @return array
463 */
464 function timetics_get_payment_methods() {
465 $payment_methods = [
466 [
467 'name' => esc_html__( 'Stripe', 'timetics' ),
468 'status' => timetics_get_option( 'stripe_status' ),
469 ],
470 [
471 'name' => esc_html__( 'Local Pay', 'timetics' ),
472 'status' => timetics_get_option( 'local_pay_status' ),
473 ],
474 [
475 'name' => esc_html__( 'Woocommerce', 'timetics' ),
476 'status' => timetics_is_woocommerce_active(),
477 ],
478 ];
479
480 return apply_filters( 'timetics_payment_methods', $payment_methods );
481 }
482 }
483
484 if ( ! function_exists( 'timetics_replace_placeholder' ) ) {
485 /**
486 * Replace string with placeholder
487 *
488 * @param array $placeholders
489 * @param string $text
490 *
491 * @return string
492 */
493 function timetics_replace_placeholder( $text, $placeholders = [] ) {
494
495 if ( ! $placeholders ) {
496 return $text;
497 }
498
499 foreach ( $placeholders as $placeholder => $replace_with ) {
500 $text = str_replace( $placeholder, $replace_with, $text );
501 }
502
503 return $text;
504 }
505 }
506
507 if ( ! function_exists( 'timetics_register_cron' ) ) {
508 /**
509 * Register timetics cron
510 *
511 * @return void
512 */
513 function timetics_register_cron() {
514 wp_schedule_event( time(), 'daily', 'timetics_booking_clear_schedule' );
515 }
516 }
517
518 if ( ! function_exists( 'timetics_is_woocommerce_active' ) ) {
519
520 /**
521 * Check woocommerce is active or not
522 *
523 * @return bool
524 */
525 function timetics_is_woocommerce_active() {
526
527 if ( function_exists( 'WC' ) && timetics_get_option( 'wc_integration' ) ) {
528 return true;
529 }
530
531 return false;
532 }
533 }
534
535 if ( ! function_exists( 'timetics_add_woocommerce_product_cat' ) ) {
536 /**
537 * Add woocommerce product category
538 *
539 * @return void
540 */
541 function timetics_add_woocommerce_product_cat() {
542 $category_name = __( 'Timetics Meeting', 'timetics' );
543
544 // Prepare category arguments
545 $category_args = array(
546 'taxonomy' => 'product_cat',
547 'cat_name' => $category_name,
548 'category_description' => __( 'Timetics meeting', 'timetics' ),
549 'slug' => 'timetics-meeting',
550 );
551
552 // Insert the category
553 wp_insert_term( $category_args['cat_name'], $category_args['taxonomy'], $category_args );
554 }
555 }
556
557 if ( ! function_exists( 'timetics_is_valid_timezone' ) ) {
558 /**
559 * Check a timezone is valid or not
560 *
561 * @param string $timezone
562 *
563 * @return bool
564 */
565 function timetics_is_valid_timezone( $timezone ) {
566 try {
567 new DateTimeZone( $timezone );
568 } catch ( Exception $e ) {
569 return false;
570 }
571
572 return true;
573 }
574 }
575
576 if ( ! function_exists( 'timetics_datetime' ) ) {
577
578 /**
579 * Timetics date time with timezone
580 *
581 * @param int | string $date
582 * @param string $timezone
583 *
584 * @return string
585 */
586 function timetics_datetime( $format, $date, $timezone = '' ) {
587 $date = is_int( $date ) ? gmdate( 'Y-m-d g:ia', $date ) : $date;
588
589 if ( ! $timezone ) {
590 $timezone = 'UTC';
591 }
592
593 // Create a DateTime object with the provided timestamp and time zone
594 $datetime = new \DateTime( $date, new \DateTimeZone( $timezone ) );
595
596 // Get the converted timestamp
597 return $datetime->format( $format );
598 }
599 }
600
601 if ( ! function_exists( 'timetics_convert_timezone' ) ) {
602
603 /**
604 * Convert date and time fron timezone to another timezone
605 *
606 * @param string $format
607 * @param string $date_time
608 * @param string $from
609 * @param string $to
610 *
611 * @return \DateTime
612 */
613 function timetics_convert_timezone( $date_time_string, $from, $to ) {
614
615 if ( empty( $from ) || empty( $to ) ) {
616 return new DateTime( $date_time_string );
617 }
618
619 $date_time_string = is_int( $date_time_string ) ? gmdate( 'Y-m-d g:ia', $date_time_string ) : gmdate( 'Y-m-d g:ia', strtotime( $date_time_string ) );
620
621 $datetime = new DateTime( $date_time_string, new DateTimeZone( $from ) );
622 $datetime->setTimezone( new DateTimeZone( $to ) );
623
624 return $datetime;
625 }
626 }
627
628 if ( ! function_exists( 'timetics_generate_username' ) ) {
629 /**
630 * Generate unique username
631 *
632 * @param string $email
633 *
634 * @return string
635 */
636 function timetics_generate_username( $email ) {
637 $username = strtok( $email, '@' );
638
639 if ( username_exists( $username ) ) {
640 $username = $username . wp_rand( 10, 100 );
641 }
642
643 return $username;
644 }
645 }
646
647 if ( ! function_exists( 'timetics_is_json' ) ) {
648 /**
649 * Check a string is valid json or not
650 *
651 * @param string $json_string
652 *
653 * @return bool
654 */
655 function timetics_is_json( $json_string ) {
656 if ( ! is_string( $json_string ) ) {
657 return false;
658 }
659
660 $data = json_decode( $json_string, true );
661
662 return $data !== null && json_last_error() === JSON_ERROR_NONE;
663 }
664 }
665
666 if ( ! function_exists( 'timetics_is_google_meet_connected' ) ) {
667
668 /**
669 * Check google meet is connected or not
670 *
671 * @param integer $user_id
672 *
673 * @return bool
674 */
675 function timetics_is_google_meet_connected( $user_id = 0 ) {
676 if ( ! $user_id ) {
677 $user_id = get_current_user_id();
678 }
679
680 $google_auth = timetics_get_google_access_token( $user_id );
681
682 return ! empty( $google_auth );
683 }
684 }
685
686 if ( ! function_exists( 'timetics_is_zoom_connected' ) ) {
687
688 /**
689 * Check google meet is connected or not
690 *
691 * @param integer $user_id
692 *
693 * @return bool
694 */
695 function timetics_is_zoom_connected( $user_id = 0 ) {
696 if ( ! $user_id ) {
697 $user_id = get_current_user_id();
698 }
699
700 if ( ! function_exists( 'timetics_get_zoom_access_token' ) ) {
701 return false;
702 }
703
704 $zoom_auth = timetics_get_zoom_access_token( $user_id );
705
706 return ! empty( $zoom_auth );
707 }
708 }
709
710 if ( ! function_exists( 'timetics_get_current_user' ) ) {
711 /**
712 * Get current user data
713 *
714 * @return array
715 */
716 function timetics_get_current_user() {
717 $user_id = get_current_user_id();
718 $user = get_userdata( $user_id );
719
720 $user_data = [
721 'id' => $user->ID,
722 'username' => $user->user_login,
723 'email' => $user->user_email,
724 'roles' => $user-> roles,
725 ];
726 return $user_data;
727 }
728 }
729
730 if ( ! function_exists( 'timetics_connected_platforms' ) ) {
731
732 /**
733 * Get connected platforms
734 *
735 * @param integer $id
736 *
737 * @return array Connected platform lists
738 */
739 function timetics_connected_platforms( $id ) {
740 $integrations = timetics_get_staff_integrations( $id );
741 $integration_names = [];
742
743 if ( $integrations ) {
744 foreach ( $integrations as $integration ) {
745 if ( $integration['connected'] ) {
746 $integration_names[] = $integration['id'];
747 }
748 }
749 }
750
751 return $integration_names;
752 }
753 }
754