| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Get timezone list. |
| 5 |
* |
| 6 |
* @return array |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
if ( ! function_exists( 'booktics_get_timezone_list' ) ) { |
| 10 |
function booktics_get_timezone_list() { |
| 11 |
$tzlist = wp_timezone_choice( 'UTC' ); |
| 12 |
$doc = new DOMDocument(); |
| 13 |
$doc->loadHTML( $tzlist ); |
| 14 |
|
| 15 |
$elements = $doc->getElementsByTagName( 'option' ); |
| 16 |
$timezones = array(); |
| 17 |
|
| 18 |
if ( ! empty( $elements ) ) { |
| 19 |
foreach ( $elements as $element ) { |
| 20 |
$data = array( |
| 21 |
'value' => $element->getAttribute( 'value' ), |
| 22 |
'name' => $element->textContent, |
| 23 |
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 24 |
); |
| 25 |
|
| 26 |
$timezones[] = $data; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
return $timezones; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if ( ! function_exists( 'booktics_get_auth_redirect_uri' ) ) { |
| 35 |
/** |
| 36 |
* Get auth redirect uri |
| 37 |
* |
| 38 |
* @param string $auth |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
function booktics_get_auth_redirect_uri( $auth = '' ) { |
| 43 |
return site_url( 'booktics-integration/' . $auth ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if ( ! function_exists( 'booktics_get_google_auth' ) ) { |
| 49 |
/** |
| 50 |
* Get google auth |
| 51 |
* |
| 52 |
* @param integer $user_id |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
function booktics_get_google_auth( $user_id = 0 ) { |
| 57 |
return get_user_meta( $user_id, 'booktics_google_auth', true ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! function_exists( 'booktics_get_posts' ) ) { |
| 62 |
/** |
| 63 |
* Get posts with date range |
| 64 |
* |
| 65 |
* @param array $args |
| 66 |
* |
| 67 |
* @return WP_Query |
| 68 |
*/ |
| 69 |
function booktics_get_posts( $args = array() ) { |
| 70 |
$defaults = array( |
| 71 |
'post_type' => 'post', |
| 72 |
'post_status' => 'publish', |
| 73 |
'nopaging' => true, |
| 74 |
'date_range' => '', |
| 75 |
); |
| 76 |
|
| 77 |
$args = wp_parse_args( $args, $defaults ); |
| 78 |
|
| 79 |
// Date range found. |
| 80 |
if ( $args['date_range'] ) { |
| 81 |
$from_date = $args['date_range']['start']; |
| 82 |
$to_date = $args['date_range']['end']; |
| 83 |
|
| 84 |
$args['date_query'] = array( |
| 85 |
'relation' => 'AND', |
| 86 |
array( |
| 87 |
'before' => array( |
| 88 |
'year' => gmdate( 'Y', strtotime( $to_date ) ), |
| 89 |
'month' => gmdate( 'm', strtotime( $to_date ) ), |
| 90 |
'day' => gmdate( 'd', strtotime( $to_date ) ), |
| 91 |
), |
| 92 |
'after' => array( |
| 93 |
'year' => gmdate( 'Y', strtotime( $from_date ) ), |
| 94 |
'month' => gmdate( 'm', strtotime( $from_date ) ), |
| 95 |
'day' => gmdate( 'd', strtotime( $from_date ) ), |
| 96 |
), |
| 97 |
'inclusive' => true, |
| 98 |
), |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
// The Query |
| 103 |
$posts = new WP_Query( $args ); |
| 104 |
|
| 105 |
return $posts; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if ( ! function_exists( 'booktics_count_posts()' ) ) { |
| 110 |
/** |
| 111 |
* Count post with date range or without date range |
| 112 |
* |
| 113 |
* @param array $args Required query params |
| 114 |
* |
| 115 |
* @return integer |
| 116 |
*/ |
| 117 |
function booktics_count_posts( $args = array() ) { |
| 118 |
$posts = booktics_get_posts( $args ); |
| 119 |
|
| 120 |
return $posts->post_count; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! function_exists( 'booktics_count_users' ) ) { |
| 125 |
/** |
| 126 |
* Count user by role and date range |
| 127 |
* |
| 128 |
* @param array $args Required query params |
| 129 |
* |
| 130 |
* @return integer |
| 131 |
*/ |
| 132 |
function booktics_count_users( $args = array() ) { |
| 133 |
$defaults = array( |
| 134 |
'role' => '', |
| 135 |
'date_range' => '', |
| 136 |
'count_total' => true, |
| 137 |
); |
| 138 |
|
| 139 |
$args = wp_parse_args( $args, $defaults ); |
| 140 |
|
| 141 |
// Date range found. |
| 142 |
if ( $args['date_range'] ) { |
| 143 |
$from_date = $args['date_range']['start']; |
| 144 |
$to_date = $args['date_range']['end']; |
| 145 |
|
| 146 |
$args['date_query'] = array( |
| 147 |
'relation' => 'AND', |
| 148 |
array( |
| 149 |
'before' => array( |
| 150 |
'year' => gmdate( 'Y', strtotime( $to_date ) ), |
| 151 |
'month' => gmdate( 'm', strtotime( $to_date ) ), |
| 152 |
'day' => gmdate( 'd', strtotime( $to_date ) ), |
| 153 |
), |
| 154 |
'after' => array( |
| 155 |
'year' => gmdate( 'Y', strtotime( $from_date ) ), |
| 156 |
'month' => gmdate( 'm', strtotime( $from_date ) ), |
| 157 |
'day' => gmdate( 'd', strtotime( $from_date ) ), |
| 158 |
), |
| 159 |
'inclusive' => true, |
| 160 |
), |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
// The Query |
| 165 |
$user_query = new WP_User_Query( $args ); |
| 166 |
|
| 167 |
// Return total users. |
| 168 |
return $user_query->total_users; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
if ( ! function_exists( 'booktics_get_default_timezone' ) ) { |
| 174 |
/** |
| 175 |
* Get default timezone |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
function booktics_get_default_timezone() { |
| 180 |
$timezone = get_option( 'timezone_string' ); |
| 181 |
|
| 182 |
return $timezone; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( ! function_exists( 'booktics_get_currencies' ) ) { |
| 187 |
/** |
| 188 |
* Get currency list |
| 189 |
* |
| 190 |
* @return array |
| 191 |
*/ |
| 192 |
function booktics_get_currencies() { |
| 193 |
$currencies = require_once __DIR__ . '/currency.php'; |
| 194 |
|
| 195 |
return $currencies; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if ( ! function_exists( 'booktics_update_default_settings' ) ) { |
| 200 |
|
| 201 |
/** |
| 202 |
* Update default settings |
| 203 |
* |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
function booktics_update_default_settings() { |
| 207 |
$settings = array( |
| 208 |
'google_auth_redirect_uri' => booktics_get_auth_redirect_uri( 'google-auth' ), |
| 209 |
'default_booking_status' => 'pending', |
| 210 |
'currency' => 'USD', |
| 211 |
'primary_color' => '#3161F1', |
| 212 |
'secondary_color' => '#6188ff', |
| 213 |
); |
| 214 |
|
| 215 |
$settings = apply_filters( 'booktics_default_settings', $settings ); |
| 216 |
|
| 217 |
foreach ( $settings as $key => $value ) { |
| 218 |
booktics_update_option( $key, $value ); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! function_exists( 'booktics_get_post_status' ) ) { |
| 224 |
|
| 225 |
/** |
| 226 |
* Get post status |
| 227 |
* |
| 228 |
* @return array |
| 229 |
*/ |
| 230 |
function booktics_get_post_status() { |
| 231 |
return array( |
| 232 |
'approved', |
| 233 |
'pending', |
| 234 |
'cancel', |
| 235 |
'completed', |
| 236 |
); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! function_exists( 'booktics_replace_placeholder' ) ) { |
| 241 |
/** |
| 242 |
* Replace string with placeholder |
| 243 |
* |
| 244 |
* @param array $placeholders |
| 245 |
* @param string $text |
| 246 |
* |
| 247 |
* @return string |
| 248 |
*/ |
| 249 |
function booktics_replace_placeholder( $text, $placeholders = array() ) { |
| 250 |
|
| 251 |
if ( ! $placeholders ) { |
| 252 |
return $text; |
| 253 |
} |
| 254 |
|
| 255 |
foreach ( $placeholders as $placeholder => $replace_with ) { |
| 256 |
$text = str_replace( $placeholder, $replace_with, $text ); |
| 257 |
} |
| 258 |
|
| 259 |
return $text; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
if ( ! function_exists( 'booktics_register_cron' ) ) { |
| 264 |
/** |
| 265 |
* Register booktics cron |
| 266 |
* |
| 267 |
* @return void |
| 268 |
*/ |
| 269 |
function booktics_register_cron() { |
| 270 |
wp_schedule_event( time(), 'daily', 'booktics_booking_clear_schedule' ); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if ( ! function_exists( 'booktics_is_woocommerce_active' ) ) { |
| 275 |
|
| 276 |
/** |
| 277 |
* Check woocommerce is active or not |
| 278 |
* |
| 279 |
* @return bool |
| 280 |
*/ |
| 281 |
function booktics_is_woocommerce_active() { |
| 282 |
|
| 283 |
if ( function_exists( 'WC' ) && booktics_get_option( 'wc_integration' ) ) { |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
return false; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! function_exists( 'booktics_add_woocommerce_product_cat' ) ) { |
| 292 |
/** |
| 293 |
* Add woocommerce product category |
| 294 |
* |
| 295 |
* @return void |
| 296 |
*/ |
| 297 |
function booktics_add_woocommerce_product_cat() { |
| 298 |
$category_name = __( 'Booktics Meeting', 'booktics' ); |
| 299 |
|
| 300 |
// Prepare category arguments |
| 301 |
$category_args = array( |
| 302 |
'taxonomy' => 'product_cat', |
| 303 |
'cat_name' => $category_name, |
| 304 |
'category_description' => __( 'Booktics meeting', 'booktics' ), |
| 305 |
'slug' => 'booktics-meeting', |
| 306 |
); |
| 307 |
|
| 308 |
// Insert the category |
| 309 |
wp_insert_term( $category_args['cat_name'], $category_args['taxonomy'], $category_args ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if ( ! function_exists( 'booktics_is_valid_timezone' ) ) { |
| 314 |
/** |
| 315 |
* Check a timezone is valid or not |
| 316 |
* |
| 317 |
* @param string $timezone |
| 318 |
* |
| 319 |
* @return bool |
| 320 |
*/ |
| 321 |
function booktics_is_valid_timezone( $timezone ) { |
| 322 |
try { |
| 323 |
new DateTimeZone( $timezone ); |
| 324 |
} catch ( Exception $e ) { |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
return true; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if ( ! function_exists( 'booktics_datetime' ) ) { |
| 333 |
|
| 334 |
/** |
| 335 |
* Booktics date time with timezone |
| 336 |
* |
| 337 |
* @param int | string $date |
| 338 |
* @param string $timezone |
| 339 |
* |
| 340 |
* @return string |
| 341 |
*/ |
| 342 |
function booktics_datetime( $format, $date, $timezone = '' ) { |
| 343 |
$date = is_int( $date ) ? gmdate( 'Y-m-d g:ia', $date ) : $date; |
| 344 |
|
| 345 |
if ( ! $timezone ) { |
| 346 |
$timezone = 'UTC'; |
| 347 |
} |
| 348 |
|
| 349 |
// Create a DateTime object with the provided timestamp and time zone |
| 350 |
$datetime = new \DateTime( $date, new \DateTimeZone( $timezone ) ); |
| 351 |
|
| 352 |
// Get the converted timestamp |
| 353 |
return $datetime->format( $format ); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
if ( ! function_exists( 'booktics_convert_timezone' ) ) { |
| 358 |
|
| 359 |
/** |
| 360 |
* Convert date and time from timezone to another timezone |
| 361 |
* |
| 362 |
* @param string $format |
| 363 |
* @param string $date_time |
| 364 |
* @param string $from |
| 365 |
* @param string $to |
| 366 |
* |
| 367 |
* @return \DateTime |
| 368 |
*/ |
| 369 |
function booktics_convert_timezone( $date_time_string, $from, $to ) { |
| 370 |
|
| 371 |
if ( empty( $from ) || empty( $to ) ) { |
| 372 |
return new DateTime( $date_time_string ); |
| 373 |
} |
| 374 |
|
| 375 |
$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 ) ); |
| 376 |
|
| 377 |
$datetime = new DateTime( $date_time_string, new DateTimeZone( $from ) ); |
| 378 |
$datetime->setTimezone( new DateTimeZone( $to ) ); |
| 379 |
|
| 380 |
return $datetime; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
if ( ! function_exists( 'booktics_generate_username' ) ) { |
| 385 |
/** |
| 386 |
* Generate unique username |
| 387 |
* |
| 388 |
* @param string $email |
| 389 |
* |
| 390 |
* @return string |
| 391 |
*/ |
| 392 |
function booktics_generate_username( $email ) { |
| 393 |
$username = strtok( $email, '@' ); |
| 394 |
|
| 395 |
if ( username_exists( $username ) ) { |
| 396 |
$username = $username . wp_rand( 10, 100 ); |
| 397 |
} |
| 398 |
|
| 399 |
return $username; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
if ( ! function_exists( 'booktics_is_json' ) ) { |
| 404 |
/** |
| 405 |
* Check a string is valid json or not |
| 406 |
* |
| 407 |
* @param string $json_string |
| 408 |
* |
| 409 |
* @return bool |
| 410 |
*/ |
| 411 |
function booktics_is_json( $json_string ) { |
| 412 |
if ( ! is_string( $json_string ) ) { |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
$data = json_decode( $json_string, true ); |
| 417 |
|
| 418 |
return $data !== null && json_last_error() === JSON_ERROR_NONE; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if ( ! function_exists( 'booktics_get_current_user' ) ) { |
| 424 |
/** |
| 425 |
* Get current user data |
| 426 |
* |
| 427 |
* @return array |
| 428 |
*/ |
| 429 |
function booktics_get_current_user() { |
| 430 |
$user_id = get_current_user_id(); |
| 431 |
$user = get_userdata( $user_id ); |
| 432 |
|
| 433 |
$user_data = array( |
| 434 |
'id' => $user->ID, |
| 435 |
'username' => $user->user_login, |
| 436 |
'email' => $user->user_email, |
| 437 |
'roles' => $user->roles, |
| 438 |
); |
| 439 |
|
| 440 |
return $user_data; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if ( ! function_exists( 'booktics_validate' ) ) { |
| 445 |
/** |
| 446 |
* Validate user input |
| 447 |
* |
| 448 |
* @param array $request |
| 449 |
* @param array $rules |
| 450 |
* |
| 451 |
* @return bool | WP_Error |
| 452 |
*/ |
| 453 |
function booktics_validate( $request, $rules ) { |
| 454 |
$validator = new \Booktics\Validation\Validator( $request ); |
| 455 |
|
| 456 |
$validator->set_rules( $rules ); |
| 457 |
|
| 458 |
if ( ! $validator->validate() ) { |
| 459 |
return $validator->get_error(); |
| 460 |
} |
| 461 |
|
| 462 |
return true; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if ( ! function_exists( 'booktics_snake_case' ) ) { |
| 467 |
/** |
| 468 |
* Convert a string to snake_case. |
| 469 |
* |
| 470 |
* @param string $string |
| 471 |
* |
| 472 |
* @return string |
| 473 |
*/ |
| 474 |
function booktics_snake_case( string $string ): string { |
| 475 |
$string = preg_replace( '/([a-z])([A-Z])/', '$1_$2', $string ); |
| 476 |
|
| 477 |
return strtolower( str_replace( array( '-', ' ' ), '_', $string ) ); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if ( ! function_exists( 'booktics_kebab_case' ) ) { |
| 482 |
/** |
| 483 |
* Convert a string to kebab-case. |
| 484 |
* |
| 485 |
* @param string $string |
| 486 |
* |
| 487 |
* @return string |
| 488 |
*/ |
| 489 |
function booktics_kebab_case( string $string ): string { |
| 490 |
$string = preg_replace( '/([a-z])([A-Z])/', '$1-$2', $string ); |
| 491 |
|
| 492 |
return strtolower( str_replace( array( '_', ' ' ), '-', $string ) ); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
if ( ! function_exists( 'booktics_camel_case' ) ) { |
| 497 |
/** |
| 498 |
* Convert a string to camelCase. |
| 499 |
* |
| 500 |
* @param string $string |
| 501 |
* |
| 502 |
* @return string |
| 503 |
*/ |
| 504 |
function booktics_camel_case( string $string ): string { |
| 505 |
$string = str_replace( array( '-', '_' ), ' ', strtolower( $string ) ); |
| 506 |
$string = ucwords( $string ); |
| 507 |
$string = str_replace( ' ', '', $string ); |
| 508 |
|
| 509 |
return lcfirst( $string ); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if ( ! function_exists( 'booktics_pascal_case' ) ) { |
| 514 |
/** |
| 515 |
* Convert a string to PascalCase. |
| 516 |
* |
| 517 |
* @param string $string |
| 518 |
* |
| 519 |
* @return string |
| 520 |
*/ |
| 521 |
function booktics_pascal_case( string $string ): string { |
| 522 |
$string = str_replace( array( '-', '_' ), ' ', strtolower( $string ) ); |
| 523 |
$string = ucwords( $string ); |
| 524 |
|
| 525 |
return str_replace( ' ', '', $string ); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
if ( ! function_exists( 'booktics_date_range_loop' ) ) { |
| 530 |
/** |
| 531 |
* Make date range iterable array |
| 532 |
* |
| 533 |
* @param $start |
| 534 |
* @param $end |
| 535 |
* @param $step |
| 536 |
* @param $format |
| 537 |
* |
| 538 |
* @return array |
| 539 |
* @throws Exception |
| 540 |
*/ |
| 541 |
function booktics_date_range_loop( $start, $end, $step = '+1 day', $format = 'Y-m-d' ) { |
| 542 |
$dates = array(); |
| 543 |
|
| 544 |
$current_date = new DateTime( $start ); |
| 545 |
$end_date = new DateTime( $end ); |
| 546 |
|
| 547 |
while ( $current_date <= $end_date ) { |
| 548 |
$dates[] = $current_date->format( $format ); |
| 549 |
$current_date->modify( $step ); |
| 550 |
} |
| 551 |
|
| 552 |
return $dates; |
| 553 |
} |
| 554 |
|
| 555 |
if ( ! function_exists( 'booktics_get_option' ) ) { |
| 556 |
/** |
| 557 |
* Get option for Booktics |
| 558 |
* |
| 559 |
* @param string $key Option key |
| 560 |
* @param mixed $default Default value to return if option doesn't exist |
| 561 |
* |
| 562 |
* @return mixed |
| 563 |
*/ |
| 564 |
function booktics_get_option( $key = '', $default = false ) { |
| 565 |
return \Booktics\Settings\Settings::get( $key, $default ); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
if ( ! function_exists( 'booktics_update_option' ) ) { |
| 570 |
/** |
| 571 |
* Update Booktics option |
| 572 |
* |
| 573 |
* @param string $key Option key |
| 574 |
* @param mixed $value Option value |
| 575 |
* |
| 576 |
* @return void |
| 577 |
*/ |
| 578 |
function booktics_update_option( $key, $value ) { |
| 579 |
\Booktics\Settings\Settings::update( $key, $value ); |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
if ( ! function_exists( 'is_booktics_pro' ) ) { |
| 585 |
/** |
| 586 |
* Check Booktics Pro plugin activate or deactivate |
| 587 |
* |
| 588 |
* @return bool |
| 589 |
*/ |
| 590 |
function is_booktics_pro(): bool { |
| 591 |
return is_plugin_active( 'booktics-pro/booktics-pro.php' ); |
| 592 |
} |
| 593 |
} |
| 594 |
|