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

660 lines 17.9 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 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' => 'publish',
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 'post_status' => 'approved',
295 ];
296
297 $args = wp_parse_args( $args, $defaults );
298
299 $posts = timetics_get_posts( $args );
300
301 $total = 0;
302
303 foreach ( $posts->posts as $post ) {
304 $booking = new Booking( $post->ID );
305 $total += floatval( $booking->get_total() );
306 }
307
308 return $total;
309 }
310 }
311
312 if ( ! function_exists( 'timetics_get_staff_integrations' ) ) {
313 /**
314 * Get all staff integrations
315 *
316 * @return array
317 */
318 function timetics_get_staff_integrations( $user_id = 0 ) {
319 $google_auth = get_user_meta( $user_id, 'timetics_google_auth_code', true );
320 $google_credentials = timetics_get_google_credentials();
321 $is_setup = ! empty( $google_credentials['client_id'] ) && ! empty( $google_credentials['client_secret'] );
322 $current_user_id = ( $user_id == get_current_user_id() ) ? true : false;
323
324 $integrations = [
325 [
326 'id' => 'google-calendar-meet',
327 'name' => esc_html__( 'Google Meet', 'timetics' ),
328 'description' => esc_html__( 'Connect your Meet to sync your booked events.', 'timetics' ),
329 'auth_url' => timetics_get_google_auth_url(),
330 'connected' => ! empty( $google_auth ),
331 'setup' => $is_setup,
332 'current_user' => $current_user_id,
333 ],
334 [
335 'id' => 'google-calendar',
336 'name' => esc_html__( 'Google Calendar', 'timetics' ),
337 'description' => esc_html__( 'Connect your Meet to sync your booked events.', 'timetics' ),
338 'auth_url' => timetics_get_google_auth_url(),
339 'connected' => ! empty( $google_auth ),
340 'setup' => $is_setup,
341 'current_user' => $current_user_id,
342 ],
343 ];
344
345 $integrations = apply_filters( 'timetics_staff_integrations', $integrations, $user_id );
346
347 return $integrations;
348 }
349 }
350
351 if ( ! function_exists( 'timetics_get_google_credentials' ) ) {
352 /**
353 * Get google auth credentials
354 *
355 * @return array
356 */
357 function timetics_get_google_credentials() {
358 $client_id = timetics_get_option( 'google_app_client_id' );
359 $client_secret = timetics_get_option( 'google_app_client_secret' );
360
361 return [
362 'client_id' => $client_id,
363 'client_secret' => $client_secret,
364 ];
365 }
366 }
367
368 if ( ! function_exists( 'timetics_get_default_timezone' ) ) {
369 /**
370 * Get default timezone
371 *
372 * @return string
373 */
374 function timetics_get_default_timezone() {
375 $timezone = get_option( 'timezone_string' );
376 return $timezone;
377 }
378 }
379
380 if ( ! function_exists( 'timetics_get_currencies' ) ) {
381 /**
382 * Get currency list
383 *
384 * @return array
385 */
386 function timetics_get_currencies() {
387 $currencies = require_once dirname( __FILE__ ) . '/currency.php';
388
389 return $currencies;
390 }
391 }
392
393 if ( ! function_exists( 'timetics_update_default_settings' ) ) {
394
395 /**
396 * Update default settings
397 *
398 * @return void
399 */
400 function timetics_update_default_settings() {
401 $settings = [
402 'google_auth_redirect_uri' => timetics_get_auth_redirect_uri( 'google-auth' ),
403 'default_booking_status' => 'pending',
404 'currency' => 'USD',
405 'primary_color' => '#3161F1',
406 'secondary_color' => '#6188ff',
407 ];
408
409 $settings = apply_filters( 'timetics_default_settings', $settings );
410
411 foreach ( $settings as $key => $value ) {
412 timetics_update_option( $key, $value );
413 }
414 }
415 }
416
417 if ( ! function_exists( 'timetics_google_setup' ) ) {
418
419 /**
420 * Checking google auth is configured or not
421 *
422 * @return bool
423 */
424 function timetics_google_setup() {
425 $client_id = timetics_get_option( 'google_app_client_id' );
426 $client_secret = timetics_get_option( 'google_app_client_secret' );
427
428 if ( $client_id && $client_secret ) {
429 return true;
430 }
431
432 return false;
433 }
434 }
435
436 if ( ! function_exists( 'timetics_get_post_status' ) ) {
437
438 /**
439 * Get post status
440 *
441 * @return array
442 */
443 function timetics_get_post_status() {
444 return [
445 'approved',
446 'pending',
447 'cancel',
448 'completed',
449 ];
450 }
451 }
452
453 if ( ! function_exists( 'timetics_get_payment_methods' ) ) {
454 /**
455 * Get payment methods
456 *
457 * @return array
458 */
459 function timetics_get_payment_methods() {
460 $payment_methods = [
461 [
462 'name' => esc_html__( 'Stripe', 'timetics' ),
463 'status' => timetics_get_option( 'stripe_status' ),
464 ],
465 [
466 'name' => esc_html__( 'Local Pay', 'timetics' ),
467 'status' => timetics_get_option( 'local_pay_status' ),
468 ],
469 [
470 'name' => esc_html__( 'Woocommerce', 'timetics' ),
471 'status' => timetics_is_woocommerce_active(),
472 ],
473 ];
474
475 return apply_filters( 'timetics_payment_methods', $payment_methods );
476 }
477 }
478
479 if ( ! function_exists( 'timetics_replace_placeholder' ) ) {
480 /**
481 * Replace string with placeholder
482 *
483 * @param array $placeholders
484 * @param string $text
485 *
486 * @return string
487 */
488 function timetics_replace_placeholder( $text, $placeholders = [] ) {
489
490 if ( ! $placeholders ) {
491 return $text;
492 }
493
494 foreach ( $placeholders as $placeholder => $replace_with ) {
495 $text = str_replace( $placeholder, $replace_with, $text );
496 }
497
498 return $text;
499 }
500 }
501
502 if ( ! function_exists( 'timetics_register_cron' ) ) {
503 /**
504 * Register timetics cron
505 *
506 * @return void
507 */
508 function timetics_register_cron() {
509 wp_schedule_event( time(), 'daily', 'timetics_booking_clear_schedule' );
510 }
511 }
512
513 if ( ! function_exists( 'timetics_is_woocommerce_active' ) ) {
514
515 /**
516 * Check woocommerce is active or not
517 *
518 * @return bool
519 */
520 function timetics_is_woocommerce_active() {
521
522 if ( function_exists( 'WC' ) && timetics_get_option( 'wc_integration' ) ) {
523 return true;
524 }
525
526 return false;
527 }
528 }
529
530 if ( ! function_exists( 'timetics_add_woocommerce_product_cat' ) ) {
531 /**
532 * Add woocommerce product category
533 *
534 * @return void
535 */
536 function timetics_add_woocommerce_product_cat() {
537 $category_name = __( 'Timetics Meeting', 'timetics' );
538
539 // Prepare category arguments
540 $category_args = array(
541 'taxonomy' => 'product_cat',
542 'cat_name' => $category_name,
543 'category_description' => __( 'Timetics meeting', 'timetics' ),
544 'slug' => 'timetics-meeting',
545 );
546
547 // Insert the category
548 wp_insert_term( $category_args['cat_name'], $category_args['taxonomy'], $category_args );
549 }
550 }
551
552 if ( ! function_exists( 'timetics_is_valid_timezone' ) ) {
553 /**
554 * Check a timezone is valid or not
555 *
556 * @param string $timezone
557 *
558 * @return bool
559 */
560 function timetics_is_valid_timezone( $timezone ) {
561 try {
562 new DateTimeZone( $timezone );
563 } catch ( Exception $e ) {
564 return false;
565 }
566
567 return true;
568 }
569 }
570
571 if ( ! function_exists( 'timetics_datetime' ) ) {
572
573 /**
574 * Timetics date time with timezone
575 *
576 * @param int | string $date
577 * @param string $timezone
578 *
579 * @return string
580 */
581 function timetics_datetime( $format, $date, $timezone = '' ) {
582 $date = is_int( $date ) ? gmdate( 'Y-m-d g:ia', $date ) : $date;
583
584 if ( ! $timezone ) {
585 $timezone = 'UTC';
586 }
587
588 // Create a DateTime object with the provided timestamp and time zone
589 $datetime = new \DateTime( $date, new \DateTimeZone( $timezone ) );
590
591 // Get the converted timestamp
592 return $datetime->format( $format );
593 }
594 }
595
596 if ( ! function_exists( 'timetics_convert_timezone' ) ) {
597
598 /**
599 * Convert date and time fron timezone to another timezone
600 *
601 * @param string $format
602 * @param string $date_time
603 * @param string $from
604 * @param string $to
605 *
606 * @return \DateTime
607 */
608 function timetics_convert_timezone( $date_time_string, $from, $to ) {
609
610 if ( empty( $from ) || empty( $to ) ) {
611 return new DateTime( $date_time_string );
612 }
613
614 $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 ) );
615
616 $datetime = new DateTime( $date_time_string, new DateTimeZone( $from ) );
617 $datetime->setTimezone( new DateTimeZone( $to ) );
618
619 return $datetime;
620 }
621
622 if ( ! function_exists( 'timetics_generate_username' ) ) {
623 /**
624 * Generate unique username
625 *
626 * @param string $email
627 *
628 * @return string
629 */
630 function timetics_generate_username( $email ) {
631 $username = strtok( $email, '@' );
632
633 if ( username_exists( $username ) ) {
634 $username = $username . wp_rand( 10, 100 );
635 }
636
637 return $username;
638 }
639 }
640
641 if ( ! function_exists( 'timetics_is_json' ) ) {
642 /**
643 * Check a string is valid json or not
644 *
645 * @param string $json_string
646 *
647 * @return bool
648 */
649 function timetics_is_json( $json_string ) {
650 if ( ! is_string( $json_string ) ) {
651 return false;
652 }
653
654 $data = json_decode( $json_string, true );
655
656 return $data !== null && json_last_error() === JSON_ERROR_NONE;
657 }
658 }
659 }
660