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

727 lines 19.4 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 'default_booking_status' => 'pending',
408 'currency' => 'USD',
409 'primary_color' => '#3161F1',
410 'secondary_color' => '#6188ff',
411 ];
412
413 $settings = apply_filters( 'timetics_default_settings', $settings );
414
415 foreach ( $settings as $key => $value ) {
416 timetics_update_option( $key, $value );
417 }
418 }
419 }
420
421 if ( ! function_exists( 'timetics_google_setup' ) ) {
422
423 /**
424 * Checking google auth is configured or not
425 *
426 * @return bool
427 */
428 function timetics_google_setup() {
429 $client_id = timetics_get_option( 'google_app_client_id' );
430 $client_secret = timetics_get_option( 'google_app_client_secret' );
431
432 if ( $client_id && $client_secret ) {
433 return true;
434 }
435
436 return false;
437 }
438 }
439
440 if ( ! function_exists( 'timetics_get_post_status' ) ) {
441
442 /**
443 * Get post status
444 *
445 * @return array
446 */
447 function timetics_get_post_status() {
448 return [
449 'approved',
450 'pending',
451 'cancel',
452 'completed',
453 ];
454 }
455 }
456
457 if ( ! function_exists( 'timetics_get_payment_methods' ) ) {
458 /**
459 * Get payment methods
460 *
461 * @return array
462 */
463 function timetics_get_payment_methods() {
464 $payment_methods = [
465 [
466 'name' => esc_html__( 'Stripe', 'timetics' ),
467 'status' => timetics_get_option( 'stripe_status' ),
468 ],
469 [
470 'name' => esc_html__( 'Local Pay', 'timetics' ),
471 'status' => timetics_get_option( 'local_pay_status' ),
472 ],
473 [
474 'name' => esc_html__( 'Woocommerce', 'timetics' ),
475 'status' => timetics_is_woocommerce_active(),
476 ],
477 ];
478
479 return apply_filters( 'timetics_payment_methods', $payment_methods );
480 }
481 }
482
483 if ( ! function_exists( 'timetics_replace_placeholder' ) ) {
484 /**
485 * Replace string with placeholder
486 *
487 * @param array $placeholders
488 * @param string $text
489 *
490 * @return string
491 */
492 function timetics_replace_placeholder( $text, $placeholders = [] ) {
493
494 if ( ! $placeholders ) {
495 return $text;
496 }
497
498 foreach ( $placeholders as $placeholder => $replace_with ) {
499 $text = str_replace( $placeholder, $replace_with, $text );
500 }
501
502 return $text;
503 }
504 }
505
506 if ( ! function_exists( 'timetics_register_cron' ) ) {
507 /**
508 * Register timetics cron
509 *
510 * @return void
511 */
512 function timetics_register_cron() {
513 wp_schedule_event( time(), 'daily', 'timetics_booking_clear_schedule' );
514 }
515 }
516
517 if ( ! function_exists( 'timetics_is_woocommerce_active' ) ) {
518
519 /**
520 * Check woocommerce is active or not
521 *
522 * @return bool
523 */
524 function timetics_is_woocommerce_active() {
525
526 if ( function_exists( 'WC' ) && timetics_get_option( 'wc_integration' ) ) {
527 return true;
528 }
529
530 return false;
531 }
532 }
533
534 if ( ! function_exists( 'timetics_add_woocommerce_product_cat' ) ) {
535 /**
536 * Add woocommerce product category
537 *
538 * @return void
539 */
540 function timetics_add_woocommerce_product_cat() {
541 $category_name = __( 'Timetics Meeting', 'timetics' );
542
543 // Prepare category arguments
544 $category_args = array(
545 'taxonomy' => 'product_cat',
546 'cat_name' => $category_name,
547 'category_description' => __( 'Timetics meeting', 'timetics' ),
548 'slug' => 'timetics-meeting',
549 );
550
551 // Insert the category
552 wp_insert_term( $category_args['cat_name'], $category_args['taxonomy'], $category_args );
553 }
554 }
555
556 if ( ! function_exists( 'timetics_is_valid_timezone' ) ) {
557 /**
558 * Check a timezone is valid or not
559 *
560 * @param string $timezone
561 *
562 * @return bool
563 */
564 function timetics_is_valid_timezone( $timezone ) {
565 try {
566 new DateTimeZone( $timezone );
567 } catch ( Exception $e ) {
568 return false;
569 }
570
571 return true;
572 }
573 }
574
575 if ( ! function_exists( 'timetics_datetime' ) ) {
576
577 /**
578 * Timetics date time with timezone
579 *
580 * @param int | string $date
581 * @param string $timezone
582 *
583 * @return string
584 */
585 function timetics_datetime( $format, $date, $timezone = '' ) {
586 $date = is_int( $date ) ? gmdate( 'Y-m-d g:ia', $date ) : $date;
587
588 if ( ! $timezone ) {
589 $timezone = 'UTC';
590 }
591
592 // Create a DateTime object with the provided timestamp and time zone
593 $datetime = new \DateTime( $date, new \DateTimeZone( $timezone ) );
594
595 // Get the converted timestamp
596 return $datetime->format( $format );
597 }
598 }
599
600 if ( ! function_exists( 'timetics_convert_timezone' ) ) {
601
602 /**
603 * Convert date and time fron timezone to another timezone
604 *
605 * @param string $format
606 * @param string $date_time
607 * @param string $from
608 * @param string $to
609 *
610 * @return \DateTime
611 */
612 function timetics_convert_timezone( $date_time_string, $from, $to ) {
613
614 if ( empty( $from ) || empty( $to ) ) {
615 return new DateTime( $date_time_string );
616 }
617
618 $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 ) );
619
620 $datetime = new DateTime( $date_time_string, new DateTimeZone( $from ) );
621 $datetime->setTimezone( new DateTimeZone( $to ) );
622
623 return $datetime;
624 }
625 }
626
627 if ( ! function_exists( 'timetics_generate_username' ) ) {
628 /**
629 * Generate unique username
630 *
631 * @param string $email
632 *
633 * @return string
634 */
635 function timetics_generate_username( $email ) {
636 $username = strtok( $email, '@' );
637
638 if ( username_exists( $username ) ) {
639 $username = $username . wp_rand( 10, 100 );
640 }
641
642 return $username;
643 }
644 }
645
646 if ( ! function_exists( 'timetics_is_json' ) ) {
647 /**
648 * Check a string is valid json or not
649 *
650 * @param string $json_string
651 *
652 * @return bool
653 */
654 function timetics_is_json( $json_string ) {
655 if ( ! is_string( $json_string ) ) {
656 return false;
657 }
658
659 $data = json_decode( $json_string, true );
660
661 return $data !== null && json_last_error() === JSON_ERROR_NONE;
662 }
663 }
664
665 if ( ! function_exists( 'timetics_is_google_meet_connected' ) ) {
666
667 /**
668 * Check google meet is connected or not
669 *
670 * @param integer $user_id
671 *
672 * @return bool
673 */
674 function timetics_is_google_meet_connected( $user_id = 0 ) {
675 if ( ! $user_id ) {
676 $user_id = get_current_user_id();
677 }
678
679 $google_auth = timetics_get_google_access_token( $user_id );
680
681 return ! empty( $google_auth );
682 }
683 }
684
685 if ( ! function_exists( 'timetics_is_zoom_connected' ) ) {
686
687 /**
688 * Check google meet is connected or not
689 *
690 * @param integer $user_id
691 *
692 * @return bool
693 */
694 function timetics_is_zoom_connected( $user_id = 0 ) {
695 if ( ! $user_id ) {
696 $user_id = get_current_user_id();
697 }
698
699 if ( ! function_exists( 'timetics_get_zoom_access_token' ) ) {
700 return false;
701 }
702
703 $zoom_auth = timetics_get_zoom_access_token( $user_id );
704
705 return ! empty( $zoom_auth );
706 }
707 }
708
709 if ( ! function_exists( 'timetics_get_current_user' ) ) {
710 /**
711 * Get current user data
712 *
713 * @return array
714 */
715 function timetics_get_current_user() {
716 $user_id = get_current_user_id();
717 $user = get_userdata( $user_id );
718
719 $user_data = [
720 'id' => $user->ID,
721 'username' => $user->user_login,
722 'email' => $user->user_email,
723 'roles' => $user-> roles,
724 ];
725 return $user_data;
726 }
727 }