· 9 years ago · Feb 05, 2017, 07:32 PM
1<?php
2/**
3 * AWE Library Function
4 *
5 * @class AWE_function
6 * @version 1.0
7 * @package AweBooking/Classes/
8 * @author AweTeam
9 */
10
11if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13}
14
15/**
16 * Helper functions.
17 */
18class AWE_function {
19
20 /**
21 * Create rooms for a room type.
22 *
23 * @param int $room_type_id Room type ID.
24 * @param int $number_of_rooms Number of rooms to create.
25 * @param int $begin Begin create from.
26 *
27 * @return void
28 * @since 2.0
29 */
30 public static function bulk_create_rooms( $room_type_id, $number_of_rooms, $begin = 1 ) {
31 $room_type = get_post( $room_type_id );
32 for ( $i = $begin; $i <= $number_of_rooms; $i++ ) {
33 wp_insert_post( array(
34 'post_type' => 'apb_room',
35 'post_title' => 1 == $number_of_rooms ? get_the_title( $room_type_id ) : 'no.' . $i,
36 'post_name' => $room_type->post_name . '-' . $i,
37 'post_parent' => $room_type_id,
38 'post_status' => 'publish',
39 ) );
40 }
41 }
42
43 /**
44 * Remove rooms for a room type.
45 *
46 * @param int $room_type_id Room type ID.
47 * @param int $number_of_rooms Number of rooms to create.
48 * @param int $begin Begin remove from.
49 *
50 * @return void
51 * @since 2.0
52 */
53 public static function bulk_remove_rooms( $room_type_id, $number_of_rooms, $begin = 1 ) {
54 $room_type = get_post( $room_type_id );
55 for ( $i = $begin; $i <= $number_of_rooms; $i++ ) {
56 $room_slug = $room_type->post_name . '-' . $i;
57 $temp_rooms = get_posts( array(
58 'name' => $room_slug,
59 'post_type' => 'apb_room',
60 'post_status' => 'any',
61 ) );
62 if ( ! empty( $temp_rooms[0] ) ) {
63 wp_delete_post( $temp_rooms[0]->ID, true );
64 }
65 }
66 }
67
68
69 /**
70 * Get rooms of room type.
71 *
72 * @param int $room_type_id Room type ID.
73 * @return array Array of room object: (id => room).
74 * @since 2.0
75 */
76 public static function get_rooms_of_room_type( $room_type_id, $post_status = null ) {
77 $query_args = array(
78 'post_parent' => $room_type_id,
79 'post_type' => 'apb_room',
80 'numberposts' => -1,
81 'orderby' => 'ID',
82 'order' => 'asc',
83 );
84
85 if ( $post_status ) {
86 $query_args['post_status'] = $post_status;
87 }
88
89 $room = get_children( $query_args );
90
91 return $room;
92 }
93
94
95 /**
96 * Get a room from room type to book.
97 *
98 * @param int $room_type_id Room type ID.
99 * @param string $from From date.
100 * @param string $to To date.
101 *
102 * @return int
103 * @since 2.0
104 */
105 public static function get_room_available_from_room_type( $room_type_id, $from, $to ) {
106 $room = AWE_function::get_rooms_of_room_type( $room_type_id );
107
108 if ( empty( $room ) ) {
109 return false;
110 }
111
112 $unavailable_room = AWE_function::get_room_unavailable( $from, $to, $room_type_id );
113 $unavailable_room_id = array();
114 foreach ( $unavailable_room as $v ) {
115 $unavailable_room_id[] = $v->unit_id;
116 }
117
118 $cart = AWE_function::get_cart();
119
120 foreach ( $room as $id => $object ) {
121 if ( in_array( $id, $unavailable_room_id ) ) {
122 continue;
123 }
124
125 if ( isset( $cart[ $id ] ) ) {
126 continue;
127 }
128
129 return $id;
130 }
131
132 return false;
133 }
134
135
136 /**
137 * Get list nights.
138 *
139 * @param string $start Arrival date.
140 * @param string $end Departure date.
141 * @param string $format Output date format.
142 * @param string $step Date step.
143 * @return array
144 */
145 public static function range_night( $start, $end, $format = 'm/d/Y', $step = '+1 day' ) {
146 $dates = array();
147
148 $current = strtotime( $start );
149 $last = strtotime( $end );
150
151 while ( $current < $last ) {
152 $dates[] = date( $format, $current );
153 $current = strtotime( $step, $current );
154 }
155 return $dates;
156 }
157
158
159 /**
160 * Get room unavailable.
161 *
162 * @param string $from Arrival date. Converted date.
163 * @param string $to Departure date. Converted date.
164 * @param int $room_type_id Room type ID.
165 * @return array
166 * @since 2.0
167 */
168 public static function get_room_unavailable( $from, $to, $room_type_id = null ) {
169 global $wpdb;
170
171 $dates = AWE_function::range_night( $from, $to );
172
173 if ( empty( $dates ) ) {
174 return array();
175 }
176
177 $room_ids = array();
178 if ( ! empty( $room_type_id ) ) {
179 $rooms = AWE_function::get_rooms_of_room_type( $room_type_id );
180 foreach ( $rooms as $k => $v ) {
181 $room_ids[] = $k;
182 }
183 }
184
185 $where = array();
186 foreach ( $dates as $date ) {
187 $d = explode( '/', $date );
188 $field_year = $d[2];
189 $field_month = absint( $d[0] );
190 $field_day = 'd' . absint( $d[1] );
191
192 if ( AWE_function::prevent_book_pending() ) {
193 $where[] = sprintf(
194 'year = %d AND month = %d AND %s != 2',
195 $field_year,
196 $field_month,
197 $field_day
198 );
199 } else {
200 $where[] = sprintf(
201 'year = %d AND month = %d AND %s < 2',
202 $field_year,
203 $field_month,
204 $field_day
205 );
206 }
207 }
208
209 $where = implode( ' OR ', $where );
210
211 if ( ! empty( $room_ids ) ) {
212 $where .= ' AND unit_id IN ( ' . implode( ',', $room_ids ) . ' )';
213 }
214
215 $sql = "SELECT unit_id FROM {$wpdb->prefix}apb_availability WHERE {$where}";
216 return $wpdb->get_results( $sql );
217 }
218
219
220 /**
221 * Check available.
222 *
223 * @param string $from Arrival date with m/d/Y format.
224 * @param string $to Departure date with m/d/Y format.
225 * @param int $adult Number of adult.
226 * @param int $child Number of child.
227 * @param int $room_type_id Room type ID.
228 * @return array (room_type_id => remain_rooms).
229 * @since 2.0
230 */
231 public static function check_available( $from, $to, $adult, $child, $room_type_id = null, $post_data = array() ) {
232 $unavailable = AWE_function::get_room_unavailable( $from, $to, $room_type_id );
233
234 $number_nights = AWE_function::get_number_of_nights( $from, $to );
235
236 $unavailable_room_type = array();
237 foreach ( $unavailable as $v ) {
238 $room_type_id2 = wp_get_post_parent_id( $v->unit_id );
239 if ( isset( $unavailable_room_type[ $room_type_id2 ] ) ) {
240 $unavailable_room_type[ $room_type_id2 ] += 1;
241 } else {
242 $unavailable_room_type[ $room_type_id2 ] = 1;
243 }
244
245 if ( AWE_function::activated_wpml() ) {
246 $room_type_id2_trans = icl_object_id( $room_type_id2, 'apb_room_type', true, ICL_LANGUAGE_CODE );
247 if ( $room_type_id2 != $room_type_id2_trans ) {
248 if ( isset( $unavailable_room_type[ $room_type_id2_trans ] ) ) {
249 $unavailable_room_type[ $room_type_id2_trans ] += 1;
250 } else {
251 $unavailable_room_type[ $room_type_id2_trans ] = 1;
252 }
253 }
254 }
255 }
256
257 $cart = AWE_function::get_cart();
258 foreach ( $cart as $k => $v ) {
259 $room_type_id2 = wp_get_post_parent_id( $k );
260 if ( isset( $unavailable_room_type[ $room_type_id2 ] ) ) {
261 $unavailable_room_type[ $room_type_id2 ] += 1;
262 } else {
263 $unavailable_room_type[ $room_type_id2 ] = 1;
264 }
265
266 if ( AWE_function::activated_wpml() ) {
267 $room_type_id2_trans = icl_object_id( $room_type_id2, 'apb_room_type', true, ICL_LANGUAGE_CODE );
268 if ( $room_type_id2 != $room_type_id2_trans ) {
269 if ( isset( $unavailable_room_type[ $room_type_id2_trans ] ) ) {
270 $unavailable_room_type[ $room_type_id2_trans ] += 1;
271 } else {
272 $unavailable_room_type[ $room_type_id2_trans ] = 1;
273 }
274 }
275 }
276 }
277
278 $room_type = array();
279 $args = array(
280 'post_type' => 'apb_room_type',
281 'posts_per_page' => -1,
282 'meta_query' => array(
283 'relation' => 'AND',
284 array(
285 'key' => 'min_night',
286 'value' => $number_nights,
287 'compare' => '<=',
288 'type' => 'NUMERIC',
289 ),
290 array(
291 'key' => 'min_sleeps',
292 'value' => $adult + $child,
293 'compare' => '<=',
294 'type' => 'NUMERIC',
295 ),
296 array(
297 'key' => 'max_sleeps',
298 'value' => $adult + $child,
299 'compare' => '>=',
300 'type' => 'NUMERIC',
301 ),
302 array(
303 'relation' => 'OR',
304 array(
305 'key' => 'min_children',
306 'compare' => 'NOT EXISTS',
307 ),
308 array(
309 'key' => 'min_children',
310 'value' => $child,
311 'compare' => '<=',
312 'type' => 'NUMERIC',
313 ),
314 array(
315 'key' => 'min_children',
316 'value' => '',
317 'compare' => '=',
318 ),
319 ),
320 array(
321 'relation' => 'OR',
322 array(
323 'key' => 'max_children',
324 'compare' => 'NOT EXISTS',
325 ),
326 array(
327 'key' => 'max_children',
328 'value' => $child,
329 'compare' => '>=',
330 'type' => 'NUMERIC',
331 ),
332 array(
333 'key' => 'max_children',
334 'value' => '',
335 'compare' => '=',
336 ),
337 ),
338 ),
339 );
340
341 if ( ! is_null( $room_type_id ) ) {
342 $args['p'] = $room_type_id;
343 }
344
345 $apb_query = new WP_Query( apply_filters( 'apb_check_available_query_args', $args, $post_data ) );
346
347 if ( $apb_query->have_posts() ) {
348 while ( $apb_query->have_posts() ) {
349 $apb_query->the_post();
350
351 $number_rooms = get_post_meta( get_the_ID(), 'number_of_rooms', true );
352 if ( isset( $unavailable_room_type[ get_the_ID() ] ) && $unavailable_room_type[ get_the_ID() ] >= $number_rooms ) {
353 continue;
354 }
355
356 $count = isset( $unavailable_room_type[ get_the_ID() ] ) ? $unavailable_room_type[ get_the_ID() ] : 0;
357 $room_type[ get_the_ID() ] = $number_rooms - $count;
358 }
359 wp_reset_postdata();
360
361 }
362 return $room_type;
363 }
364
365
366 /**
367 * Check available for a specific room type.
368 *
369 * @param string $from Arrival date with m/d/Y format.
370 * @param string $to Departure date with m/d/Y format.
371 * @param int $adult Number of adult.
372 * @param int $child Number of child.
373 * @param int $room_type_id Room type ID.
374 * @return string Result id: guest, min-night, available, unavailable.
375 * @since 2.5.4
376 */
377 public static function single_check_available( $from, $to, $adult, $child, $room_type_id = null, $post_data = array() ) {
378 $original_id = AWE_function::wpml_get_default_room_type( $room_type_id );
379
380 $min_guest = get_post_meta( $original_id, 'min_sleeps', true );
381 $max_guest = get_post_meta( $original_id, 'max_sleeps', true );
382 $min_child = get_post_meta( $original_id, 'min_children', true );
383 $max_child = get_post_meta( $original_id, 'max_children', true );
384 $min_night = get_post_meta( $original_id, 'min_night', true );
385
386
387 $guests = $adult + $child;
388 $number_nights = AWE_function::get_number_of_nights( $from, $to );
389
390 if ( $guests < $min_guest || $guests > $max_guest || $child < $min_child || $child > $max_child ) {
391 return 'guest';
392 }
393
394 if ( $number_nights < $min_night ) {
395 return 'min-night';
396 }
397
398
399 $unavailable = AWE_function::get_room_unavailable( $from, $to, $original_id );
400 $number_rooms = get_post_meta( $original_id, 'number_of_rooms', true );
401
402 if ( count( $unavailable ) < $number_rooms ) {
403 return 'available';
404 }
405
406 return 'unavailable';
407 }
408
409
410 public static function get_remaining_count( $room_type_id, $from, $to ) {
411 $unavailable = AWE_function::get_room_unavailable( $from, $to, $room_type_id );
412 $number_rooms = get_post_meta( $room_type_id, 'number_of_rooms', true );
413
414 return $number_rooms - count( $unavailable );
415 }
416
417
418 /**
419 * Update room available.
420 *
421 * @param string $from Arrival date.
422 * @param string $to Departure date.
423 * @param int $room_id Room ID.
424 * @param int $status Available status
425 * 0: completed
426 * 1: not available
427 * 2: Available
428 * 3: Pending.
429 *
430 * @since 2.0
431 */
432 public static function update_available( $from, $to, $room_id, $status ) {
433 global $wpdb;
434 //$from = AWE_function::convert_date_to_mdY( $from );
435 //$to = AWE_function::convert_date_to_mdY( $to );
436 $dates = AWE_function::range_night( $from, $to );
437
438 if ( empty( $dates ) ) {
439 return;
440 }
441
442 foreach ( $dates as $date ) {
443 $d = explode( '/', $date );
444 $field_year = $d[2];
445 $field_month = $d[0];
446 $field_day = 'd' . absint( $d[1] );
447
448 $check_exists = $wpdb->get_var(
449 $wpdb->prepare(
450 "SELECT COUNT(*) FROM {$wpdb->prefix}apb_availability WHERE unit_id = %d AND year = %d AND month = %d",
451 $room_id,
452 $field_year,
453 $field_month
454 )
455 );
456 $check_exists = absint( $check_exists );
457
458 if ( $check_exists ) {
459 $wpdb->update(
460 "{$wpdb->prefix}apb_availability",
461 array(
462 $field_day => $status,
463 ),
464 array(
465 'unit_id' => $room_id,
466 'year' => $field_year,
467 'month' => $field_month,
468 ),
469 array( '%s' )
470 );
471 } else {
472 $wpdb->insert(
473 "{$wpdb->prefix}apb_availability",
474 array(
475 'unit_id' => $room_id,
476 'year' => $field_year,
477 'month' => $field_month,
478 $field_day => $status,
479 ),
480 array( '%d', '%d', '%d', '%d' )
481 );
482 }
483 }
484 }
485
486
487 /**
488 * Check if preventing booking in pending day.
489 *
490 * @return bool
491 * @since 2.0
492 */
493 public static function prevent_book_pending() {
494 return ( bool ) get_option( '_booking_pending' );
495 }
496
497
498 /**
499 * Check if use woo checkout.
500 *
501 * @return bool
502 * @since 2.0
503 */
504 public static function use_woo_checkout() {
505 return 1 == get_option( 'rooms_checkout_style' );
506 }
507
508
509 /**
510 * Get list days.
511 *
512 * @param string $start Arrival date.
513 * @param string $end Departure date.
514 * @param string $format Output date format.
515 * @param string $step Date step.
516 * @return array
517 */
518 public static function range_date( $start, $end, $format = 'm/d/Y', $step = '+1 day' ) {
519 $dates = array();
520
521 $current = strtotime( $start );
522 $last = strtotime( $end );
523
524 while ( $current <= $last ) {
525 $dates[] = date( $format, $current );
526 $current = strtotime( $step, $current );
527 }
528 return $dates;
529 }
530
531
532 /**
533 * Get number of nights.
534 *
535 * @param string $from Arrival date with format m/d/Y or Y-m-d.
536 * @param string $to Departure date with format m/d/Y or Y-m-d.
537 *
538 * @return int Number of nights.
539 * @since 2.0
540 */
541 public static function get_number_of_nights( $from, $to ) {
542 $from = strtotime( $from );
543 $to = strtotime( $to );
544 return ceil( ( $to - $from ) / DAY_IN_SECONDS );
545 }
546
547 /**
548 * Register order status.
549 * @return array Order status.
550 * @since 1.0
551 */
552 public static function apb_get_order_statuses() {
553 $order_statuses = array(
554 'apb-pending' => _x( 'Pending Payment', 'Order status', 'awebooking' ),
555 'apb-completed' => _x( 'Completed', 'Order status', 'awebooking' ),
556 'apb-cancelled' => _x( 'Cancelled', 'Order status', 'awebooking' ),
557 );
558 return apply_filters( 'apb_order_statuses', $order_statuses );
559 }
560
561 public static function apb_get_trans_order_statuses( $status ) {
562 $order_statuses = array(
563 'apb-pending' => _x( 'Pending Payment', 'Order status', 'awebooking' ),
564 'apb-completed' => _x( 'Completed', 'Order status', 'awebooking' ),
565 'apb-cancelled' => _x( 'Cancelled', 'Order status', 'awebooking' ),
566 );
567
568 switch ( $status ) {
569 case 'apb-pending':
570 return 'Pending Payment';
571 break;
572
573 case 'apb-completed':
574 return 'Completed Payment';
575 break;
576
577 case 'apb-cancelled':
578 return 'Cancelled Payment';
579 break;
580 }
581 }
582
583
584
585
586 /**
587 * check_room_available - Show room available.
588 * @param date $from
589 * @param date $to
590 */
591 public static function check_room_available( $from, $to, $room_id = '', $check_all_day = false, $status_filter = false, $status = null ) {
592 global $wpdb;
593 $to = strtotime( $to );
594 $to = $to - DAY_IN_SECONDS;
595 $to = date( 'm/d/Y', $to );
596 // var_dump( $to);
597
598 // Default year.
599 $start_year = date( 'Y', strtotime( $from ) );
600 $end_year = date( 'Y', strtotime( $to ) );
601
602 // Default month.
603 $start_month = date( 'm', strtotime( $from ) );
604 $end_month = date( 'm', strtotime( $to ) );
605
606 // Default day.
607 $start_day = date( 'd', strtotime( $from ) );
608 $end_day = date( 'd', strtotime( $to ) );
609
610 /*if ( $end_day > 1 ) {
611 $end_day--;
612 } else {
613 if ( 1 == $end_month ) {
614 $end_month = 12;
615 $end_year--;
616 $end_day = 31;
617 } else {
618 $end_month--;
619 for ( $i = 31; $i >= 28; $i++ ) {
620 if ( checkdate( $end_month, $i, $end_year ) ) {
621 $end_day = $i;
622 break;
623 }
624 }
625 }
626 }*/
627
628 // Get list month of start date and end date.
629 $list_month = self::get_list_days( $start_year, $end_year, $start_month, $end_month );
630
631 if ( count( $list_month ) > 2 ) {
632 for ( $day = $start_day; $day <= 31; $day++ ) {
633 $for_date = $start_year . '-' . $start_month . '-' . $day;
634 if ( strtotime( $for_date ) >= strtotime( $from ) && strtotime( $for_date ) <= strtotime( $to ) ) {
635 $_days[ $start_year . '-' . $start_month ][ 'd' . ( int ) $day ] = ( int ) $day;
636 }
637 }
638 for ( $d_center = 1; $d_center <= count( $list_month ) - 2; $d_center++ ) {
639 for ( $day = 1; $day <= 31; $day++ ) {
640 $_days[ $list_month[ $d_center ]['y'] . '-' . $list_month[ $d_center ]['m'] ][ 'd' . $day ] = $day;
641 }
642 }
643
644 for ( $day = 1; $day <= $end_day; $day++ ) {
645 $for_date = $end_year . '-' . $end_month . '-' . $day;
646 if ( strtotime( $for_date ) <= strtotime( $to ) ) {
647 $_days[ $end_year . '-' . $end_month ][ 'd' . $day ] = $day;
648 }
649 }
650 } else {
651 for ( $day = $start_day; $day <= 31; $day++ ) {
652 $for_date = $start_year . '-' . $start_month . '-' . $day;
653 if ( strtotime( $for_date ) >= strtotime( $from ) && strtotime( $for_date ) <= strtotime( $to ) ) {
654 $_days[ $start_year . '-' . $start_month ][ 'd' . ( int ) $day ] = ( int ) $day;
655 }
656 }
657 if ( count( $list_month ) > 1 ) {
658 for ( $day = 1; $day <= $end_day; $day++ ) {
659 $for_date = $end_year . '-' . $end_month . '-' . $day;
660 if ( strtotime( $for_date ) <= strtotime( $to ) ) {
661 $_days[ $end_year . '-' . $end_month ][ 'd' . $day ] = $day;
662 }
663 }
664 }
665 }
666
667 /*
668 * Query by list day
669 * Status 0 : completed
670 * Status 1 : not available
671 * Status 2 : Available
672 * Status 3 : Pending
673 */
674 if ( isset( $_days) ) {
675 foreach ( $_days as $key_item => $val_item ) {
676 $esc_param = implode( ' AND ', self::available_get_array_param( $val_item ) );
677
678 $m = ( int ) date( 'm', strtotime( $key_item ) );
679 $y = date( 'Y', strtotime( $key_item ) );
680
681 if ( '' == $room_id ) {
682 $param_day = array( $y, $m );
683 for ( $i = 0; $i<=count( $val_item)-1;$i++) {
684 $param_day[] = 1;
685 }
686 $sql = $wpdb->prepare("SELECT unit_id FROM {$wpdb->prefix}apb_availability where year = %d and month = %d and $esc_param ", $param_day);
687
688 }else{
689 if ( $check_all_day == FALSE) {
690 /*---------- Check by Status room by start date and End Date : Type ">" ----------*/
691 $param_day = array( $y, $m, $room_id);
692 $sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}apb_availability where year = %d and month = %d and unit_id = %d ", $param_day);
693 }
694 if ( $check_all_day == TRUE) {
695
696 if ( $status_filter == TRUE) {
697 /*---------- Check by Status room by start date and End Date : Type "=" ----------*/
698
699 /**
700 *
701 * check row date exists
702 * insert row if not exists
703 */
704
705 $date_exists = $wpdb->get_results( $wpdb->prepare("SELECT unit_id FROM {$wpdb->prefix}apb_availability where year = %d and month = %d and unit_id = %d ",array( $y, $m, $room_id) ) );
706 if ( empty( $date_exists ) ) {
707 AWE_function::update_available( $from, $to, $room_id , 2 );
708 // AWE_Controller::update_day_available( $from, $to, $room_id , 2);
709 }
710 $esc_param = implode(" and ", self::available_get_array_param( $val_item,"=") );
711
712 $param_day = array( $y, $m, $room_id);
713
714 for( $i = 0; $i<=count( $val_item)-1;$i++) {
715 $param_day[] = $status;
716 }
717 $sql = $wpdb->prepare("SELECT unit_id FROM {$wpdb->prefix}apb_availability where year = %d and month = %d and unit_id = %d and $esc_param ", $param_day);
718 }
719
720 if ( $status_filter == FALSE) {
721 /*---------- Check by Status room by start date and End Date : Type ">" ----------*/
722
723 $param_day = array( $y, $m, $room_id);
724 for( $i = 0; $i<=count( $val_item)-1;$i++) {
725 $param_day[] = 1;
726 }
727 $sql = $wpdb->prepare("SELECT unit_id FROM {$wpdb->prefix}apb_availability where year = %d and month = %d and unit_id = %d and $esc_param ", $param_day);
728 }
729
730 }
731 }
732 return $wpdb->get_results( $sql);
733 }
734 }
735
736 }
737
738 public static function available_get_array_param( $param, $custom = false ) {
739 foreach ( $param as $key => $val ) {
740 if ( false == $custom ) {
741 $array_param[ $key ] = "$key > %d";
742 } else {
743 $array_param[ $key ] = "$key " . $custom . " %d";
744 }
745 }
746 return $array_param;
747 }
748
749 public static function get_new_post_id() {
750 global $wpdb;
751 $data_id = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} order by ID DESC limit 1");
752 return $data_id[0]->ID;
753 }
754
755 /**
756 * Get_room_option (Room package).
757 * @param int $object_id Room type id.
758 * @param string $type Deprecated from 2.0.
759 *
760 * @return array
761 */
762 public static function get_room_option( $object_id, $type = 'apb_room_type' ) {
763 if ( AWE_function::activated_wpml() ) {
764 $object_id = icl_object_id( $object_id, 'apb_room_type', true, ICL_LANGUAGE_CODE );
765 }
766 global $wpdb;
767 $sql = $wpdb->prepare(
768 "SELECT * FROM {$wpdb->prefix}apb_booking_options WHERE entity_type = '%s' AND object_id = %d",
769 $type,
770 $object_id
771 );
772 return $wpdb->get_results( $sql );
773 }
774
775 public static function awe_get_link( $action, $paramt = '' ) {
776 return admin_url( 'edit.php?post_type=apb_room_type&page=rooms.php&action=' . $action . $paramt );
777 }
778
779 /**
780 * Type change price of room.
781 *
782 * @return array
783 * @deprecated 2.0
784 */
785 public static function operation() {
786 _deprecated_function( __FUNCTION__, '2.0' );
787 return array(
788 'add' => 'Add to price',
789 'sub' => 'Subtract from price',
790 'replace' => 'Replace price',
791 'increase' => 'Increase price by % amount',
792 'decrease' => 'Decrease price by % amount',
793 );
794 }
795
796 /**
797 * Get template from plugin or theme.
798 *
799 * @param string $file Template file name.
800 * @param array $param Params to add to template.
801 *
802 * @return string
803 */
804 public static function template_exsits( $file, $param = array() ) {
805 extract( $param );
806 if ( locate_template( 'apb-template/' . $file . '.php' ) ) {
807 $template_load = locate_template( 'apb-template/' . $file . '.php' );
808 } else {
809 $template_load = AWE_BK_PLUGIN_DIR . '/apb-template/' . $file . '.php';
810 }
811
812 $path = apply_filters( 'apb_template_exists', $template_load, $file, $param );
813
814 return $path;
815 }
816
817 /**
818 * Display select page.
819 * @param string $name Field name.
820 * @param int $id Page id.
821 * @return void
822 */
823 public static function select_page( $name, $id = 0 ) {
824 $args = array(
825 'post_type' => 'page',
826 );
827 $posts_array = get_pages( $args );
828 $data = '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '">';
829 $data .= '<option>-- ' . esc_html__( 'Select a page', 'awebooking' ) . ' --</option>';
830 foreach ( $posts_array as $item ) {
831 $data .= ' <option ' . selected( $id, $item->ID, false ) . ' value="' . absint( $item->ID ). '">' . esc_html( $item->post_title ) . '</option>';
832 }
833 $data .= '</select>';
834
835 echo $data;
836 }
837
838 /**
839 *
840 * Func get_list_days
841 * fucntion result data
842 *
843 */
844
845 public static function get_list_days( $start_year, $end_year, $start_month, $end_month ) {
846 $total_year = $end_year - $start_year;
847
848 if ( $start_year != $end_year ) {
849 $days = array();
850
851 for ( $m = $start_month; $m <= 12; $m++ ) {
852 $days[] = array(
853 'm' => $m,
854 'y' => $start_year,
855 );
856 }
857 for ( $y = 1; $y <= $total_year - 1; $y++ ) {
858 for ( $m_n = 1; $m_n <= 12; $m_n++ ) {
859 $days[] = array(
860 'm' => $m_n,
861 'y' => $start_year + $y,
862 );
863 }
864 }
865 for ( $m2 = 1; $m2 <= $end_month; $m2++ ) {
866 $days[] = array(
867 'm' => $m2,
868 'y' => $end_year,
869 );
870 }
871 } else {
872 $days = array();
873 for ( $m = $start_month; $m <= $start_month + ( $end_month - $start_month ); $m++ ) {
874 $days[] = array(
875 'm' => $m,
876 'y' => $end_year,
877 );
878 }
879 }
880 return $days;
881 }
882
883 /**
884 * List currencies.
885 * @return array
886 * @since 1.0
887 */
888 public static function list_currencies() {
889 return apply_filters( 'apb_currencies', array(
890 'AED' => 'United Arab Emirates Dirham',
891 'ARS' => 'Argentine Peso',
892 'AUD' => 'Australian Dollars',
893 'BDT' => 'Bangladeshi Taka',
894 'BRL' => 'Brazilian Real',
895 'BGN' => 'Bulgarian Lev',
896 'CAD' => 'Canadian Dollars',
897 'CLP' => 'Chilean Peso',
898 'CNY' => 'Chinese Yuan',
899 'COP' => 'Colombian Peso',
900 'CZK' => 'Czech Koruna',
901 'DKK' => 'Danish Krone',
902 'DOP' => 'Dominican Peso',
903 'EUR' => 'Euros',
904 'HKD' => 'Hong Kong Dollar',
905 'HRK' => 'Croatia kuna',
906 'HUF' => 'Hungarian Forint',
907 'ISK' => 'Icelandic krona',
908 'IDR' => 'Indonesia Rupiah',
909 'INR' => 'Indian Rupee',
910 'NPR' => 'Nepali Rupee',
911 'ILS' => 'Israeli Shekel',
912 'JPY' => 'Japanese Yen',
913 'KIP' => 'Lao Kip',
914 'KRW' => 'South Korean Won',
915 'MYR' => 'Malaysian Ringgits',
916 'MXN' => 'Mexican Peso',
917 'NGN' => 'Nigerian Naira',
918 'NOK' => 'Norwegian Krone',
919 'NZD' => 'New Zealand Dollar',
920 'PYG' => 'Paraguayan GuaranÃ',
921 'PHP' => 'Philippine Pesos',
922 'PLN' => 'Polish Zloty',
923 'GBP' => 'Pounds Sterling',
924 'GHS' => 'Ghanaian Cedi',
925 'RON' => 'Romanian Leu',
926 'RUB' => 'Russian Ruble',
927 'SAR' => 'Saudi Arabia riyal',
928 'SGD' => 'Singapore Dollar',
929 'ZAR' => 'South African rand',
930 'SEK' => 'Swedish Krona',
931 'CHF' => 'Swiss Franc',
932 'TWD' => 'Taiwan New Dollars',
933 'THB' => 'Thai Baht',
934 'TRY' => 'Turkish Lira',
935 'UAH' => 'Ukrainian Hryvnia',
936 'USD' => 'US Dollars',
937 'VND' => 'Vietnamese Dong',
938 'EGP' => 'Egyptian Pound',
939 ) );
940 }
941
942
943 /**
944 * Get currency symbol.
945 *
946 * @param string $currency Currency code.
947 * @return string
948 */
949 public static function get_currency( $currency = '' ) {
950 $currency_symbol = '';
951
952 switch ( $currency ) {
953 case 'AED' :
954 $currency_symbol = 'د.إ';
955 break;
956 case 'AUD' :
957 case 'ARS' :
958 case 'CAD' :
959 case 'CLP' :
960 case 'COP' :
961 case 'HKD' :
962 case 'MXN' :
963 case 'NZD' :
964 case 'SGD' :
965 case 'USD' :
966 $currency_symbol = '$';
967 break;
968 case 'BDT':
969 $currency_symbol = '৳ ';
970 break;
971 case 'BGN' :
972 $currency_symbol = 'лв. ';
973 break;
974 case 'BRL' :
975 $currency_symbol = 'R$';
976 break;
977 case 'CHF' :
978 $currency_symbol = 'CHF';
979 break;
980 case 'CNY' :
981 case 'JPY' :
982 case 'RMB' :
983 $currency_symbol = '¥';
984 break;
985 case 'CZK' :
986 $currency_symbol = 'Kč';
987 break;
988 case 'DKK' :
989 $currency_symbol = 'DKK';
990 break;
991 case 'DOP' :
992 $currency_symbol = 'RD$';
993 break;
994 case 'EGP' :
995 $currency_symbol = 'EGP';
996 break;
997 case 'EUR' :
998 $currency_symbol = '€';
999 break;
1000 case 'GBP' :
1001 $currency_symbol = '£';
1002 break;
1003 case 'GHS' :
1004 $currency_symbol = 'GHS';
1005 break;
1006 case 'HRK' :
1007 $currency_symbol = 'Kn';
1008 break;
1009 case 'HUF' :
1010 $currency_symbol = 'Ft';
1011 break;
1012 case 'IDR' :
1013 $currency_symbol = 'Rp';
1014 break;
1015 case 'ILS' :
1016 $currency_symbol = '₪';
1017 break;
1018 case 'INR' :
1019 $currency_symbol = 'Rs. ';
1020 break;
1021 case 'ISK' :
1022 $currency_symbol = 'Kr. ';
1023 break;
1024 case 'KIP' :
1025 $currency_symbol = '₭';
1026 break;
1027 case 'KRW' :
1028 $currency_symbol = '₩';
1029 break;
1030 case 'MYR' :
1031 $currency_symbol = 'RM';
1032 break;
1033 case 'NGN' :
1034 $currency_symbol = '₦';
1035 break;
1036 case 'NOK' :
1037 $currency_symbol = 'kr';
1038 break;
1039 case 'NPR' :
1040 $currency_symbol = 'Rs. ';
1041 break;
1042 case 'PHP' :
1043 $currency_symbol = '₱';
1044 break;
1045 case 'PLN' :
1046 $currency_symbol = 'zł';
1047 break;
1048 case 'PYG' :
1049 $currency_symbol = '₲';
1050 break;
1051 case 'RON' :
1052 $currency_symbol = 'lei';
1053 break;
1054 case 'RUB' :
1055 $currency_symbol = 'руб. ';
1056 break;
1057 case 'SAR':
1058 $currency_symbol = 'ر.س';
1059 break;
1060 case 'SEK' :
1061 $currency_symbol = 'kr';
1062 break;
1063 case 'THB' :
1064 $currency_symbol = '฿';
1065 break;
1066 case 'TRY' :
1067 $currency_symbol = '₺';
1068 break;
1069 case 'TWD' :
1070 $currency_symbol = 'NT$';
1071 break;
1072 case 'UAH' :
1073 $currency_symbol = '₴';
1074 break;
1075 case 'VND' :
1076 $currency_symbol = '₫';
1077 break;
1078 case 'ZAR' :
1079 $currency_symbol = 'R';
1080 break;
1081 default :
1082 $currency_symbol = '';
1083 break;
1084 }
1085
1086 $currency_symbol = apply_filters( 'apb_get_currency_symbol', $currency_symbol, $currency );
1087
1088 return $currency_symbol;
1089 }
1090
1091
1092 /**
1093 * Get cart.
1094 *
1095 * @return array
1096 * @since 2.0
1097 */
1098 public static function get_cart() {
1099 $cart = isset( $_SESSION['apb_cart'] ) && is_array( $_SESSION['apb_cart'] ) ? $_SESSION['apb_cart'] : array();
1100 return $cart;
1101 }
1102
1103
1104 /**
1105 * Update cart.
1106 *
1107 * @param array $data New cart data.
1108 * @since 2.0
1109 */
1110 public static function update_cart( $data ) {
1111 $_SESSION['apb_cart'] = $data;
1112 }
1113
1114
1115 /**
1116 * Delete cart.
1117 *
1118 * @since 2.0
1119 */
1120 public static function delete_cart() {
1121 $_SESSION['apb_cart'] = array();
1122
1123 if ( AWE_function::use_woo_checkout() ) {
1124 if ( class_exists( 'WooCommerce' ) ) {
1125 WC()->cart->empty_cart();
1126 }
1127 }
1128 }
1129
1130
1131 /**
1132 * Add room to cart.
1133 *
1134 * @param int $room_id Room ID.
1135 * @param string $from Arrival date.
1136 * @param string $to Departure date.
1137 * @param int $adult Adult.
1138 * @param int $child Child.
1139 * @param float $price Price.
1140 * @param array $sale_info Sale data.
1141 * @param array $package_data Package data.
1142 */
1143 public static function add_room_to_cart( $room_id, $from, $to, $adult, $child, $price, $sale_info = null, $package_data = null, $cart_index = -1, $post_data = array() ) {
1144 $cart = AWE_function::get_cart();
1145
1146 if ( isset( $cart[ $room_id ] ) ) {
1147 return false;
1148 }
1149
1150 $cart_total = ! empty( $cart['total'] ) ? (float) $cart['total'] : 0;
1151
1152 $room_data = apply_filters( 'apb_cart_item_data', array(
1153 'room_id' => $room_id,
1154 'from' => $from,
1155 'to' => $to,
1156 'price' => $price,
1157 'adult' => $adult,
1158 'child' => $child,
1159 'sale_info' => $sale_info,
1160 'package_data' => $package_data,
1161 ), $post_data );
1162
1163 if ( $cart_index >= 0 ) {
1164 array_splice( $cart, $cart_index, 0, array( $room_id => $room_data ) );
1165 $new_cart = array();
1166 foreach ( $cart as $room ) {
1167 $new_cart[ $room['room_id'] ] = $room;
1168 }
1169 $cart = $new_cart;
1170 } else {
1171 $cart[ $room_id ] = $room_data;
1172 }
1173
1174 $cart_total += (float) $room_data['price'];
1175 $cart['total'] = $cart_total;
1176
1177 AWE_function::update_cart( $cart );
1178
1179 if ( AWE_function::use_woo_checkout() && class_exists( 'WooCommerce' ) ) {
1180 $cart_item_data = $room_data;
1181 WC()->cart->add_to_cart( $room_id, 1, null, null, $cart_item_data );
1182 }
1183
1184 return true;
1185 }
1186
1187
1188 /**
1189 * Remove room from cart.
1190 *
1191 * @param int $room_id Room ID.
1192 * @since 2.0
1193 */
1194 public static function remove_room_from_cart( $room_id ) {
1195 $cart = AWE_function::get_cart();
1196
1197 if ( ! isset( $cart[ $room_id ] ) ) {
1198 return;
1199 }
1200
1201 unset( $cart[ $room_id ] );
1202
1203 AWE_function::update_cart( $cart );
1204
1205 // var_dump($cart);
1206
1207 if ( AWE_function::use_woo_checkout() && class_exists( 'WooCommerce' ) ) {
1208 $cart = WC()->cart->get_cart();
1209 foreach ( $cart as $cart_key => $cart_item ) {
1210 if ( $room_id == $cart_item['product_id'] ) {
1211 WC()->cart->remove_cart_item( $cart_key );
1212 }
1213 }
1214 }
1215 }
1216
1217 public static function apb_get_option_to_selected( $args = array() ) {
1218 if ( isset( $args['name'] ) && isset( $args['count_num'] ) ) {
1219 $html = '<select ';
1220 if ( isset( $args['name'] ) && '' != $args['name'] ) {
1221 $html .= 'name="' . esc_attr( $args['name'] ) . '" ';
1222 }
1223 if ( isset( $args['data'] ) ) {
1224 foreach ( $args['data'] as $attr => $value ) {
1225 $html .= $attr . '="' . esc_attr( $value ) . '" ';
1226 }
1227 }
1228 $start = isset( $args['start_num'] ) ? $args['start_num'] : 1;
1229
1230 $html .= '>';
1231 if ( isset( $args['default'] ) ) {
1232 $html .= '<option selected="selected" value="' . $args['default']['value'] . '">' . $args['default']['label'] . '</option>';
1233 }
1234 for ( $i = $start; $i <= $args['count_num']; $i++ ) {
1235 $html .= '<option ';
1236 if ( isset( $args['select'] ) && $args['select'] == $i ) {
1237 $html .= 'selected="selected"';
1238 }
1239 $html .= ' value="' . esc_attr( $i ) . '">' . $i . '</option>';
1240 }
1241 $html .= '</select>';
1242 echo $html;
1243 }
1244 }
1245
1246 public static function apb_gen_input( $args = array() ) {
1247 $html ='<input ';
1248 if ( ! empty( $args ) ) {
1249 foreach ( $args as $attr => $value ) {
1250 $html .= ' ' . $attr . '="' . esc_attr( $value ) . '" ';
1251 }
1252 }
1253 $html .='>';
1254 echo $html;
1255 }
1256
1257 public static function apb_icon_type_price( $type ) {
1258 _deprecated_function( __FUNCTION__, '2.0' );
1259 $list = array(
1260 'add' => 'Add',
1261 'sub' => 'Sub',
1262 'replace' => 'Replace',
1263 'increase' => 'Add %',
1264 'decrease' => 'Sub %',
1265 );
1266 return $list[ $type ];
1267 }
1268
1269 /**
1270 * Get price detail in a range days of room.
1271 *
1272 * @param string $from From date.
1273 * @param string $to To date.
1274 * @param int $room_id Room ID.
1275 * @param integer $night Number of nights.
1276 *
1277 * @return array
1278 * @since 1.0.0
1279 */
1280 public static function get_pricing_of_days( $from, $to, $room_id, $night = 1 ) {
1281 if ( AWE_function::activated_wpml() ) {
1282 $lang = wpml_get_default_language();
1283 $room_id = icl_object_id( $room_id, 'apb_room_type', true, $lang );
1284 }
1285
1286 $start_month = date( 'm', strtotime( $from ) );
1287 $end_month = date( 'm', strtotime( $to ) );
1288
1289 $start_year = date( 'Y', strtotime( $from ) );
1290 $end_year = date( 'Y', strtotime( $to ) );
1291
1292 $start_date = date( 'd', strtotime( $from ) );
1293 $end_date = date( 'd', strtotime( $to ) );
1294
1295 $key_pricing = array();
1296 if ( $start_month == $end_month ) {
1297
1298 $check_pricing = AWE_function::check_apb_pricing( $start_year, $start_month, $room_id );
1299 if ( ! empty( $check_pricing ) ) {
1300
1301 for ( $day = ( int ) $start_date; $day <= ( int ) $end_date - $night; $day++ ) {
1302 $get_price = 'd' . $day;
1303 if ( checkdate( $start_month, $day, $start_year ) ) {
1304 $key_pricing[ $start_month ][ $day ] = $check_pricing[0]->$get_price;
1305 }
1306 }
1307 } else {
1308 for ( $day = ( int ) $start_date; $day <= ( int ) $end_date - $night; $day++ ) {
1309 $get_price = 'd' . $day;
1310 if ( checkdate( $start_month, $day, $start_year ) ) {
1311 $key_pricing[ $start_month ][ $day ] = get_post_meta( $room_id, 'base_price', true );
1312 }
1313 }
1314 }
1315 } else {
1316 $list_month = AWE_function::get_list_day( $start_year, $end_year, $start_month, $end_month );
1317 if ( count( $list_month ) > 2 ) {
1318 //====== List month > 2 ============//
1319 // Month day start
1320
1321 $_monthStart = $list_month[0];
1322 $_monthEnd = $list_month[ count( $list_month ) - 1 ];
1323 unset( $list_month[0] );
1324 unset( $list_month[ count( $list_month ) ] );
1325
1326 $check_pricing_start = AWE_function::check_apb_pricing( $_monthStart['y'], $_monthStart['m'], $room_id );
1327 if ( ! empty( $check_pricing_start ) ) {
1328 $total_day = date( 't', mktime( 0, 0, 0, $start_year, 1, $start_month ) );
1329 for ( $day = ( int ) $start_date; $day <= ( int ) $total_day; $day++ ) {
1330 $get_price = 'd' . $day;
1331 if ( checkdate( $_monthStart['m'], $day, $start_year ) ) {
1332 $key_pricing[ $_monthStart['m'] ][ $day ] = $check_pricing_start[0]->$get_price;
1333 }
1334 }
1335 } else {
1336 $total_day = date( 't', mktime( 0, 0, 0, $start_year, 1, $start_month ) );
1337 for ( $day = ( int ) $start_date; $day <= ( int ) $total_day; $day++ ) {
1338 $get_price = 'd' . $day;
1339 if ( checkdate( $_monthStart['m'], $day, $start_year ) ) {
1340 $key_pricing[ $_monthStart['m'] ][ $day ] = get_post_meta( $room_id, 'base_price', true );
1341 }
1342 }
1343 }
1344 foreach ( $list_month as $monthCenter ) {
1345 $check_pricing = AWE_function::check_apb_pricing( $monthCenter['y'], $monthCenter['m'], $room_id );
1346
1347 if ( ! empty( $check_pricing ) ) {
1348
1349 $total_day = date( 't', mktime( 0, 0, 0, $start_year, 1, $start_month ) );
1350 for ( $day = 0; $day <= ( int ) $total_day; $day++ ) {
1351 $get_price = 'd' . $day;
1352 if ( checkdate( $monthCenter['m'], $day, $start_year ) ) {
1353 $key_pricing[ $monthCenter['m'] ][ $day ] = $check_pricing[0]->$get_price;
1354 }
1355 }
1356 } else {
1357
1358 $total_day = date( 't', mktime( 0, 0, 0, $start_year, 1, $start_month ) );
1359 for ( $day = 0; $day <= ( int ) $total_day; $day++ ) {
1360 if ( checkdate( $monthCenter['m'], $day, $start_year ) ) {
1361 $key_pricing[ $monthCenter['m'] ][ $day ] = get_post_meta( $room_id, 'base_price', true );
1362 }
1363 }
1364 }
1365 }
1366
1367 // Month day end.
1368 $check_pricing_end = AWE_function::check_apb_pricing( $_monthEnd['y'], $_monthEnd['m'], $room_id );
1369 if ( ! empty( $check_pricing_end ) ) {
1370 for ( $day = 1; $day <= ( int ) $end_date - $night; $day++ ) {
1371 $get_price = 'd' . $day;
1372 if ( checkdate( $_monthEnd['m'], $day, $start_year ) ) {
1373 $key_pricing[ $_monthEnd['m'] ][ $day ] = $check_pricing_end[0]->$get_price;
1374 }
1375 }
1376 } else {
1377 for ( $day = 1; $day <= ( int ) $end_date - $night; $day++ ) {
1378 $get_price = 'd' . $day;
1379 if ( checkdate( $_monthEnd['m'], $day, $start_year ) ) {
1380 $key_pricing[ $_monthEnd['m'] ][ $day ] = get_post_meta( $room_id, 'base_price', true );
1381 }
1382 }
1383 }
1384 } else {
1385 // Month day start.
1386 $check_pricing_start = AWE_function::check_apb_pricing( $list_month[0]['y'], $list_month[0]['m'], $room_id );
1387 if ( ! empty( $check_pricing_start ) ) {
1388
1389 $total_day = date( 't', mktime( 0, 0, 0, $start_year, 1, $start_month ) );
1390 for ( $day = ( int ) $start_date; $day <= ( int ) $total_day; $day++ ) {
1391 $get_price = 'd' . $day;
1392 if ( checkdate( $list_month[0]['m'], $day, $start_year ) ) {
1393 $key_pricing[ $list_month[0]['m'] ][ $day ] = $check_pricing_start[0]->$get_price;
1394 }
1395 }
1396 } else {
1397 $total_day = date( 't', mktime( 0, 0, 0, $start_year, 1, $start_month ) );
1398 for ( $day = ( int ) $start_date; $day <= ( int ) $total_day; $day++ ) {
1399 $get_price = 'd' . $day;
1400 if ( checkdate( $list_month[0]['m'], $day, $start_year ) ) {
1401 $key_pricing[ $list_month[0]['m'] ][ $day ] = get_post_meta( $room_id, 'base_price', true );
1402 }
1403 }
1404 }
1405 // Month day start.
1406 $check_pricing_end = AWE_function::check_apb_pricing( $list_month[ count( $list_month ) - 1 ]['y'], $list_month[ count( $list_month ) - 1 ]['m'], $room_id );
1407 if ( ! empty( $check_pricing_end ) ) {
1408 for ( $day = 1; $day <= ( int ) $end_date - $night; $day++ ) {
1409 $get_price = 'd' . $day;
1410 if ( checkdate( $list_month[ count( $list_month ) - 1 ]['m'], $day, $start_year ) ) {
1411 $key_pricing[ $list_month[ count( $list_month ) - 1 ]['m'] ][ $day ] = $check_pricing_end[0]->$get_price;
1412 }
1413 }
1414 } else {
1415 for ( $day = 1; $day <= ( int ) $end_date - $night; $day++ ) {
1416 $get_price = 'd' . $day;
1417 if ( checkdate( $list_month[ count( $list_month ) - 1 ]['m'], $day, $start_year ) ) {
1418 $key_pricing[ $list_month[ count( $list_month ) - 1 ]['m'] ][ $day ] = get_post_meta( $room_id, 'base_price', true );
1419 }
1420 }
1421 }
1422 }
1423 }
1424 return $key_pricing;
1425 }
1426
1427
1428 static public function get_list_day( $start_year, $end_year, $start_month, $end_month ) {
1429 $total_year = $end_year - $start_year;
1430
1431 if ( $start_year != $end_year ) {
1432 $days = array();
1433
1434 for ( $m = $start_month; $m <= 12; $m++ ) {
1435 $days[] = array(
1436 'm' => $m,
1437 'y' => $start_year,
1438 );
1439 }
1440 for ( $y = 1; $y <= $total_year - 1; $y++ ) {
1441 for ( $m_n = 1; $m_n <= 12; $m_n++ ) {
1442 $days[] = array(
1443 'm' => $m_n,
1444 'y' => $start_year + $y,
1445 );
1446 }
1447 }
1448 for ( $m2 = 1; $m2 <= $end_month; $m2++) {
1449 $days[] = array(
1450 'm' => $m2,
1451 'y' => $end_year,
1452 );
1453 }
1454 } else {
1455 $days = array();
1456 for ( $m = $start_month; $m <= $start_month + ( $end_month - $start_month); $m++) {
1457 $days[] = array(
1458 'm' => $m,
1459 'y' => $end_year,
1460 );
1461 }
1462 }
1463 return $days;
1464 }
1465
1466
1467 public static function get_total_price( $list_pricing_of_days ) {
1468 $price = 0;
1469 if ( ! empty( $list_pricing_of_days ) ) {
1470 foreach ( $list_pricing_of_days as $key => $value ) {
1471 foreach ( $value as $pr ) {
1472 $price += $pr;
1473 }
1474 }
1475 return $price;
1476 }
1477 }
1478
1479
1480 public static function apb_setState( $name, $value ) {
1481 $_SESSION[$name] = $value;
1482 }
1483
1484
1485 public static function apb_getStateFlash( $name ) {
1486 if (isset( $_SESSION[$name] ) ) {
1487 return $_SESSION[$name];
1488 }
1489 }
1490
1491
1492 public static function Apb_get_permalink( $object = '', $args = array(), $action = '' ) {
1493 $current = get_option( 'permalink_structure' );
1494 if ( '' != $current ) {
1495 $uri = trailingslashit( get_permalink( $object ) );
1496 if ( 'action' == $action ) {
1497 $uri .= '?action=apb';
1498 }
1499 foreach ( $args as $key => $value ) {
1500 $uri .= $key . '/' . $value;
1501 }
1502 $url = str_replace( 'archives/', '', $uri );
1503 return str_replace( $uri, get_permalink( $object ), ( $url ) );
1504 } else {
1505
1506 $uri = get_permalink( $object );
1507
1508 foreach ( $args as $key => $value ) {
1509 if ( 'room_type_info' == $key ) {
1510 $uri .= '&rt_id=' . $value;
1511 }
1512 }
1513 return rtrim( $uri );
1514 }
1515 }
1516
1517 public static function awe_help( $text = '' ) {
1518 echo '<img class="help_tip" data-tip="' . esc_attr( $text ) . '" src="' . esc_url( AWE_BK_BASE_URL_PLUGIN . '/assets/backend/images/help.png' ) . '" height="16" width="16" /></p>';
1519 }
1520
1521 public static function get_type_of_sale( $key ) {
1522 $list = array( 'replace' => 'Replace price', 'sub' => 'Subtract from price', 'decrease' => 'Decrease price by %' );
1523 return $list[ $key ];
1524 }
1525
1526 public static function get_symbol_of_sale( $key ) {
1527 $list = array( 'sub' => '-', 'decrease' => '%' );
1528 return $list[ $key ];
1529 }
1530
1531 public static function apb_price( $price = 0 ) {
1532 $price = ( float ) $price;
1533 $currency_pos = AWE_function::get_option( 'woocommerce_currency_pos' ) ? esc_attr( AWE_function::get_option( 'woocommerce_currency_pos' ) ) : 'left';
1534 switch ( $currency_pos ) {
1535 case 'left':
1536 $price_format = '%2$s%1$s';
1537 break;
1538
1539 case 'right':
1540 $price_format = '%1$s%2$s';
1541 break;
1542
1543 case 'left_space':
1544 $price_format = '%2$s %1$s';
1545 break;
1546
1547 case 'right_space':
1548 $price_format = '%1$s %2$s';
1549 break;
1550 }
1551
1552 $args = array(
1553 'currency' => AWE_function::get_option( 'woocommerce_currency' ) ? esc_attr( AWE_function::get_option( 'woocommerce_currency' ) ) : '$',
1554 'thousand_separator' => AWE_function::get_option( 'woocommerce_price_thousand_sep' ) ? esc_attr( AWE_function::get_option( 'woocommerce_price_thousand_sep' ) ) : '',
1555 'decimal_separator' => AWE_function::get_option( 'woocommerce_price_decimal_sep' ) ? esc_attr( AWE_function::get_option( 'woocommerce_price_decimal_sep' ) ) : '',
1556 'decimals' => AWE_function::get_option( 'woocommerce_price_num_decimals' ) ? absint( AWE_function::get_option( 'woocommerce_price_num_decimals' ) ) : 0,
1557 'price_format' => $price_format,
1558 );
1559
1560 extract( $args );
1561 $price = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
1562
1563 return sprintf( $price_format, esc_attr( $price ), esc_attr( AWE_function::get_currency( $currency ) ) );
1564 }
1565
1566 public static function apb_get_extra_sale( $extra_sale, $total_day, $from = '' ) {
1567 $BeforeDay = count( AWE_function::range_date( date( 'Y-m-d' ), $from ) );
1568 $days = array();
1569 foreach ( $extra_sale as $item_extra_sale ) {
1570 $sale_type = ! empty( $item_extra_sale['sale_type'] ) ? $item_extra_sale['sale_type'] : 'sub';
1571
1572 if ( 'Month' == $item_extra_sale['type_duration'] ) {
1573 $days[ $item_extra_sale['total'] * 30 ] = array(
1574 'type_duration' => $item_extra_sale['type_duration'],
1575 'total_day' => $item_extra_sale['total'] * 30,
1576 'total' => $item_extra_sale['total'],
1577 'amount' => $item_extra_sale['amount'],
1578 'sale_type' => $sale_type,
1579 );
1580 }
1581 if ( 'Week' == $item_extra_sale['type_duration'] ) {
1582 $days[ $item_extra_sale['total'] * 7 ] = array(
1583 'type_duration' => $item_extra_sale['type_duration'],
1584 'total_day' => $item_extra_sale['total'] * 7,
1585 'total' => $item_extra_sale['total'],
1586 'amount' => $item_extra_sale['amount'],
1587 'sale_type' => $sale_type,
1588 );
1589 }
1590 if ( 'Day' == $item_extra_sale['type_duration'] ) {
1591 $days[ $item_extra_sale['total'] ] = array(
1592 'type_duration' => $item_extra_sale['type_duration'],
1593 'total_day' => $item_extra_sale['total'],
1594 'total' => $item_extra_sale['total'],
1595 'amount' => $item_extra_sale['amount'],
1596 'sale_type' => $sale_type,
1597 );
1598 }
1599 if ( 'Before-Day' == $item_extra_sale['type_duration'] ) {
1600 if ( $BeforeDay >= $item_extra_sale['total'] ) {
1601 $days[ 'before-' . $item_extra_sale['total'] ] = array(
1602 'type_duration' => $item_extra_sale['type_duration'],
1603 'total_day' => $item_extra_sale['total'],
1604 'total' => $item_extra_sale['total'],
1605 'amount' => $item_extra_sale['amount'],
1606 'sale_type' => $sale_type,
1607 'type_sale' => 'before-day',
1608 );
1609 }
1610 }
1611 }
1612 $_days = array();
1613 foreach ( $days as $day ) {
1614 if ( 'Before-Day' == $day['type_duration'] ) {
1615 if ( $BeforeDay >= $day['total_day'] ) {
1616 $_days[] = 'before-' . $day['total_day'];
1617 }
1618 } else {
1619 if ( $total_day - 1 >= $day['total_day'] ) {
1620 $_days[] = $day['total_day'];
1621 }
1622 }
1623 }
1624 if ( ! empty( $_days ) ) {
1625 sort( $_days );
1626 return( $days[ $_days[ count( $_days ) - 1 ] ] );
1627 } else {
1628 return '';
1629 }
1630 }
1631
1632
1633 public static function get_room_type() {
1634 return get_posts( array( 'post_type' => 'apb_room_type', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'asc', 'suppress_filters' => false ) );
1635 }
1636
1637 public static function apb_datepicker_lang( $key = '' ) {
1638 $i18 = apply_filters( 'apb_datepicker_lang', array(
1639 // 'datepicker-af' => 'datepicker-af',
1640 // 'datepicker-ar-DZ' => 'datepicker-ar-DZ',
1641 'datepicker-ar' => 'Arabic',
1642 // 'datepicker-az' => 'datepicker-az',
1643 // 'datepicker-be' => 'datepicker-be',
1644 // 'datepicker-bg' => 'Bulgarian',
1645 'datepicker-ca' => 'Catalan',
1646 'datepicker-cs' => 'Czech',
1647 // 'datepicker-cy-GB' => 'datepicker-cy-GB',
1648 // 'datepicker-da' => 'datepicker-da',
1649 'datepicker-zh-CN' => 'Chinese (China)',
1650 // 'datepicker-zh-HK' => 'Chinese (Hong Kong) ',
1651 'datepicker-zh-TW' => 'Chinese (Taiwan)',
1652 'datepicker-nl-BE' => 'Dutch (Belgium)',
1653 'datepicker-nl' => 'Dutch (The Netherlands)',
1654 'datepicker-en-AU' => 'English (Australia)',
1655 'datepicker-en-GB' => 'English (United Kingdom)',
1656 'datepicker-en-NZ' => 'English (New Zealand)',
1657 'datepicker-fr-CA' => 'French (Canada)',
1658 'datepicker-fr-CH' => 'French (Switzerland)',
1659 'datepicker-fr' => 'French (France)',
1660 'datepicker-de' => 'German',
1661 'datepicker-ja' => 'Japanese',
1662 'datepicker-it' => 'Italian',
1663 'datepicker-it-CH' => 'Italian (Switzerland)',
1664 'datepicker-pl' => 'Polish (Poland)',
1665 'datepicker-pt-BR' => 'Portuguese (Brazil)',
1666 'datepicker-pt' => 'Portuguese (Portugal)',
1667 'datepicker-ru' => 'Russian',
1668 'datepicker-es' => 'Spanish (Spain)',
1669 'datepicker-ta' => 'Tamil (India)',
1670 'datepicker-tr' => 'Turkish',
1671 'datepicker-el' => 'Greek',
1672 // 'datepicker-eo' => 'datepicker-eo',
1673 // 'datepicker-et' => 'Estonian',
1674 // 'datepicker-eu' => 'datepicker-eu',
1675 // 'datepicker-fa' => 'datepicker-fa',
1676 // 'datepicker-fi' => 'datepicker-fi',
1677 // 'datepicker-fo' => 'datepicker-fo',
1678 // 'datepicker-gl' => 'datepicker-gl',
1679 'datepicker-he' => 'Hebrew',
1680 // 'datepicker-hi' => 'datepicker-hi',
1681 // 'datepicker-hr' => 'datepicker-hr',
1682 'datepicker-hu' => 'Hungarian',
1683 // 'datepicker-hy' => 'datepicker-hy',
1684 // 'datepicker-id' => 'Indonesian',
1685 // 'datepicker-is' => 'datepicker-is',
1686 // 'datepicker-ka' => 'datepicker-ka',
1687 // 'datepicker-kk' => 'datepicker-kk',
1688 // 'datepicker-km' => 'datepicker-km',
1689 'datepicker-ko' => 'Korean',
1690 // 'datepicker-ky' => 'datepicker-ky',
1691 // 'datepicker-lb' => 'datepicker-lb',
1692 // 'datepicker-lt' => 'datepicker-lt',
1693 // 'datepicker-lv' => 'datepicker-lv',
1694 // 'datepicker-mk' => 'datepicker-mk',
1695 // 'datepicker-ml' => 'datepicker-ml',
1696 // 'datepicker-ms' => 'datepicker-ms',
1697 // 'datepicker-nb' => 'datepicker-nb',
1698 // 'datepicker-nn' => 'datepicker-nn',
1699 // 'datepicker-no' => 'datepicker-no',
1700 // 'datepicker-rm' => 'datepicker-rm',
1701 // 'datepicker-ro' => 'datepicker-ro',
1702 // 'datepicker-sk' => 'datepicker-sk',
1703 'datepicker-sk' => 'Slovakia',
1704 // 'datepicker-sq' => 'datepicker-sq',
1705 // 'datepicker-sr-SR' => 'datepicker-sr-SR',
1706 // 'datepicker-sr' => 'datepicker-sr',
1707 // 'datepicker-sv' => 'datepicker-sv',
1708 'datepicker-th' => 'Thailand',
1709 // 'datepicker-tj' => 'datepicker-tj',
1710 // 'datepicker-uk' => 'datepicker-uk',
1711 'datepicker-vi' => 'Vietnamese',
1712 ) );
1713
1714 if ( '' != $key ) {
1715 return $i18[ $key ];
1716 } else {
1717 return $i18;
1718 }
1719 }
1720
1721 public static function Apb_Js_FormatDate( $key ) {
1722 _deprecated_function( __FUNCTION__, '2.0' );
1723 $list_format = array(
1724 'dd/mm/yy' => 'd/m/Y',
1725 'dd.mm.yy' => 'd.m.Y',
1726 'dd-mm-yy' => 'd-m-Y',
1727 'yy-mm-dd' => 'Y-m-d',
1728 'yy/mm/dd' => 'Y/m/d',
1729 'd.m.yy' => 'd.m.Y',
1730 'yy.mm.dd. ' => 'Y.m.d',
1731 'yy. m. d. ' => 'Y.m.d',
1732 'mm/dd/yy' => 'm/d/Y',
1733 );
1734 return $list_format[ $key ];
1735 }
1736
1737 /**
1738 * Detect if we should use a light or dark colour on a background colour
1739 *
1740 * @param mixed $color
1741 * @param string $dark (default: '#000000' )
1742 * @param string $light (default: '#FFFFFF' )
1743 * @return string
1744 */
1745 public static function apb_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
1746
1747 $hex = str_replace( '#', '', $color );
1748
1749 $c_r = hexdec( substr( $hex, 0, 2 ) );
1750 $c_g = hexdec( substr( $hex, 2, 2 ) );
1751 $c_b = hexdec( substr( $hex, 4, 2 ) );
1752
1753 $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
1754
1755 return $brightness > 155 ? $dark : $light;
1756 }
1757
1758 /**
1759 * Hex darker/lighter/contrast functions for colours
1760 *
1761 * @param mixed $color
1762 * @return string
1763 */
1764 public static function apb_rgb_from_hex( $color ) {
1765 $color = str_replace( '#', '', $color );
1766 // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF"
1767 $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color );
1768
1769 $rgb = array();
1770 $rgb['R'] = hexdec( $color{0}.$color{1} );
1771 $rgb['G'] = hexdec( $color{2}.$color{3} );
1772 $rgb['B'] = hexdec( $color{4}.$color{5} );
1773
1774 return $rgb;
1775 }
1776
1777 public static function apb_hex_darker( $color, $factor = 30 ) {
1778 $base = AWE_function::apb_rgb_from_hex( $color );
1779 $color = '#';
1780
1781 foreach ( $base as $k => $v ) {
1782 $amount = $v / 100;
1783 $amount = round( $amount * $factor );
1784 $new_decimal = $v - $amount;
1785
1786 $new_hex_component = dechex( $new_decimal );
1787 if ( strlen( $new_hex_component ) < 2 ) {
1788 $new_hex_component = '0' . $new_hex_component;
1789 }
1790 $color .= $new_hex_component;
1791 }
1792
1793 return $color;
1794 }
1795
1796 /**
1797 * Hex darker/lighter/contrast functions for colours
1798 *
1799 * @param mixed $color
1800 * @param int $factor (default: 30)
1801 * @return string
1802 */
1803 public static function apb_hex_lighter( $color, $factor = 30 ) {
1804 $base = AWE_function::apb_rgb_from_hex( $color );
1805 $color = '#';
1806
1807 foreach ( $base as $k => $v ) {
1808 $amount = 255 - $v;
1809 $amount = $amount / 100;
1810 $amount = round( $amount * $factor );
1811 $new_decimal = $v + $amount;
1812
1813 $new_hex_component = dechex( $new_decimal );
1814 if ( strlen( $new_hex_component ) < 2 ) {
1815 $new_hex_component = "0" . $new_hex_component;
1816 }
1817 $color .= $new_hex_component;
1818 }
1819
1820 return $color;
1821 }
1822
1823 /**
1824 * Move template action.
1825 *
1826 * @param string $template_type
1827 */
1828 public static function move_template_action( $template_file , $path ) {
1829 if ( ! is_dir( get_stylesheet_directory() . '/apb-template/' ) ) {
1830 mkdir( get_stylesheet_directory() . '/apb-template/' );
1831 }
1832 if ( ! is_dir( get_stylesheet_directory() . $path ) ) {
1833 mkdir( get_stylesheet_directory() . $path );
1834 }
1835 if ( ! file_exists( get_stylesheet_directory() . $path . $template_file . '.php' ) ) {
1836 $f = fopen( get_stylesheet_directory() . $path . $template_file . '.php', 'w+' );
1837 copy( AWE_BK_PLUGIN_DIR . $path . $template_file . '.php', get_stylesheet_directory() . $path . $template_file . '.php' );
1838 }
1839 }
1840
1841 /**
1842 * Delete template action.
1843 *
1844 * @param string $template_type
1845 */
1846 public static function delete_template_action( $template_file, $path ) {
1847 if ( is_dir( get_stylesheet_directory() . $path ) ) {
1848 unlink( get_stylesheet_directory() . $path . $template_file . '.php' );
1849 }
1850 }
1851
1852 public static function apb_print_js( $code ) {
1853
1854 echo "<!-- Awebooking JavaScript -->\n<script type=\"text/javascript\">\njQuery(function( $) {";
1855
1856 // Sanitize
1857 $wc_queued_js = wp_check_invalid_utf8( $code );
1858 $wc_queued_js = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", $code );
1859 $wc_queued_js = str_replace( "\r", '', $code );
1860
1861 echo $wc_queued_js . "});\n</script>\n";
1862 }
1863
1864
1865 /**
1866 * Check if room in cart.
1867 *
1868 * @param int $room_id Room ID.
1869 * @return bool True if room in cart.
1870 * @since 1.9
1871 */
1872 public static function check_room_in_cart( $room_id ) {
1873 $cart = AWE_function::apb_cart( 'apb_cart' );
1874 if ( ! empty( $cart ) && is_array( $cart ) ) {
1875 foreach ( $cart as $key_item => $item_cart ) {
1876 if ( $item_cart['room_id'] == $room_id ) {
1877 return true;
1878 }
1879 }
1880 }
1881 return false;
1882 }
1883
1884
1885 /**
1886 * Get_day_for_month_start : Get all day for one month.
1887 * @param $year int
1888 * @param $month int
1889 * @param $start_date int
1890 * @param $end_date int
1891 * @param $room_id int
1892 * @param $price int
1893 * @param $action status get result
1894 * @param $control String
1895 */
1896 static public function get_day_for_month_start( $year, $month, $start_date, $end_date, $room_id, $price, $operation, $action, $control = '' ) {
1897 if ( 'start' == $action ) {
1898
1899 for ( $day = 1; $day <= 31; $day++ ) {
1900 $for_date = $year . '-' . $month . '-' . $day;
1901 if ( strtotime( $for_date ) >= strtotime( $start_date ) && strtotime( $for_date ) <= strtotime( $end_date ) ) {
1902 $days_price[ 'd' . $day ] = AWE_function::get_price_operation( $operation, $room_id, $month, $year, 'd' . $day, $price);
1903 } else {
1904 $days_price[ 'd' . $day ] = get_post_meta( wp_kses( $room_id, '' ), 'base_price', true );
1905 }
1906 }
1907 } elseif ( 'default' == $action ) {
1908 for ( $day = 1; $day <= 31; $day++ ) {
1909 $for_date = $year . '-' . $month . '-' . $day;
1910 $days_price[ 'd' . $day ] = AWE_function::get_price_operation( $operation, $room_id, $month, $year, 'd' . $day, $price );
1911 }
1912 } elseif ( 'end' == $action ) {
1913 for ( $day = 1; $day <= 31; $day++ ) {
1914 $for_date = $year . '-' . $month . '-' . $day;
1915 if ( strtotime( $for_date ) <= strtotime( $end_date ) ) {
1916 $days_price[ 'd' . $day ] = AWE_function::get_price_operation( $operation, $room_id, $month, $year, 'd' . $day, $price );
1917 } else {
1918 $days_price[ 'd' . $day ] = get_post_meta( wp_kses( $room_id, '' ), 'base_price', true );
1919 }
1920 }
1921 }
1922 return $days_price;
1923 }
1924
1925
1926 /**
1927 * get_price_operation : Get price by operation.
1928 * @param $operation string
1929 * @param $room_id int
1930 * @param $month int
1931 * @param $year int
1932 * @param $day int
1933 * @param $input_price int
1934 */
1935 static public function get_price_operation( $operation, $room_id, $month, $year, $day, $input_price ) {
1936 $room = AWE_function::check_apb_pricing( $year, $month, $room_id );
1937
1938 if ( ! empty( $room ) && intval( $room[0]->$day ) ) {
1939 $price_default = $room[0]->$day;
1940 } else {
1941 $price_default = get_post_meta( $room_id, 'base_price', true );
1942 }
1943
1944 switch ( $operation ) {
1945 case 'add':
1946 return $input_price + $price_default;
1947 break;
1948 case 'sub':
1949 return $price_default - $input_price;
1950 break;
1951 case 'replace':
1952 return $input_price;
1953 break;
1954 case 'increase':
1955 return $price_default + $input_price / 100 * $price_default;
1956 break;
1957 case 'decrease':
1958 return $price_default - $input_price / 100 * $price_default;
1959 break;
1960 default :
1961 return $input_price;
1962 }
1963 }
1964
1965
1966 /**
1967 * check_rooms_avb : Check room availability manage exists
1968 * @param $year int
1969 * @param $month int
1970 * @param $room_id int
1971 */
1972 public static function check_rooms_avb( $year, $month, $room_id ) {
1973 global $wpdb;
1974 $sql = $wpdb->prepare(
1975 "SELECT * FROM {$wpdb->prefix}apb_availability where unit_id = %d and year = %d and month = %d",
1976 absint( $room_id ),
1977 absint( $year ),
1978 absint( $month )
1979 );
1980 return $wpdb->get_results( $sql );
1981 }
1982
1983
1984 /**
1985 * check_apb_pricing : Check room pricing manage exists
1986 * @param $year int
1987 * @param $month int
1988 * @param $room_id int
1989 */
1990 static public function check_apb_pricing( $year, $month, $room_id ) {
1991 global $wpdb;
1992 return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}apb_pricing where unit_id = $room_id and year = '{$year}' and month = '{$month}'" );
1993 }
1994
1995
1996 /**
1997 * Get minimum night of room.
1998 *
1999 * @param int $room_id Room ID.
2000 * @return int Minimum of night.
2001 * @since 1.9
2002 */
2003 public static function get_room_min_night( $room_id ) {
2004 return absint( get_post_meta( $room_id, 'mid_night', true ) );
2005 }
2006
2007
2008 /**
2009 * Get total of day price.
2010 *
2011 * @param array $price_days Day price array.
2012 *
2013 * @return float
2014 * @since 1.10
2015 */
2016 public static function get_total_day_price( $price_days ) {
2017 $result = 0;
2018 foreach ( $price_days as $month => $list_day ) {
2019 foreach ( $list_day as $day => $price ) {
2020 $result += $price;
2021 }
2022 }
2023 return ( float ) $result;
2024 }
2025
2026
2027 /**
2028 * Get number of day price.
2029 *
2030 * @param array $price_days Day price array.
2031 *
2032 * @return int
2033 * @since 1.10
2034 */
2035 public static function get_number_day_price( $price_days ) {
2036 $result = 0;
2037 foreach ( $price_days as $month => $list_day ) {
2038 foreach ( $list_day as $day => $price ) {
2039 $result++;
2040 }
2041 }
2042 return absint( $result );
2043 }
2044
2045
2046 /**
2047 * Change language code from WordPress to FullCalendar.
2048 *
2049 * @param string $code Datepicker language code.
2050 *
2051 * @return string FullCalendar language code.
2052 * @since 1.11
2053 */
2054 public static function lang_code_datepicker_to_fullcalendar( $code ) {
2055 if ( 'default' == $code || '' == $code ) {
2056 $fc_code = false;
2057 } else {
2058 $code = strtolower( substr( $code, 11 ) );
2059 switch ( $code ) {
2060 case 'ar-dz':
2061 $fc_code = 'ar-tn';
2062 break;
2063
2064 case 'cy-gb':
2065 $fc_code = 'en-gb';
2066 break;
2067
2068 case 'fr-ch':
2069 $fc_code = 'fr';
2070 break;
2071
2072 // case 'it-ch':
2073 // $fc_code = 'it';
2074 // break;
2075
2076 case 'nl-be':
2077 $fc_code = 'nl';
2078 break;
2079
2080 default:
2081 $fc_code = $code;
2082 }
2083 }
2084
2085 $fc_code = apply_filters( 'apb_lang_code_datepicker_to_fullcalendar', $fc_code, $code );
2086
2087 return $fc_code;
2088 }
2089
2090
2091 /**
2092 * Change language code from WPML to Datepicker.
2093 *
2094 * @param string $code WPML language code.
2095 *
2096 * @return string Datepicker language code.
2097 * @since 1.11
2098 */
2099 public static function lang_code_wpml_to_datepicker( $code ) {
2100 switch ( $code ) {
2101 case 'all':
2102 $datepicker_code = 'default';
2103 break;
2104
2105 case 'en':
2106 $datepicker_code = 'datepicker-en-GB';
2107 break;
2108
2109 case 'pt-pt':
2110 $datepicker_code = 'datepicker-pt';
2111 break;
2112
2113 case 'zh-hans':
2114 $datepicker_code = 'datepicker-zh-CN';
2115 break;
2116
2117 case 'zh-hant':
2118 $datepicker_code = 'datepicker-zh-TW';
2119 break;
2120
2121 default:
2122 $datepicker_code = 'datepicker-' . $code;
2123 }
2124
2125 $datepicker_code = apply_filters( 'apb_lang_code_wpml_to_datepicker', $datepicker_code, $code );
2126
2127 return $datepicker_code;
2128 }
2129
2130
2131 /**
2132 * Get current date format, according datepicker language option.
2133 *
2134 * @return string Date format.
2135 * @since 1.11
2136 */
2137 public static function get_current_date_format() {
2138 $format = 'm/d/Y';
2139 $datepicker_lang = AWE_function::get_datepicker_lang();
2140
2141 switch ( $datepicker_lang ) {
2142 case 'datepicker-ar':
2143 case 'datepicker-ca':
2144 case 'datepicker-nl-BE':
2145 case 'datepicker-en-AU':
2146 case 'datepicker-en-GB':
2147 case 'datepicker-en-NZ':
2148 case 'datepicker-fr':
2149 case 'datepicker-it':
2150 case 'datepicker-he':
2151 case 'datepicker-pt-BR':
2152 case 'datepicker-pt':
2153 case 'datepicker-es':
2154 case 'datepicker-el':
2155 case 'datepicker-ta':
2156 case 'datepicker-th':
2157 case 'datepicker-vi':
2158 $format = 'd/m/Y';
2159 break;
2160
2161 case 'datepicker-nl':
2162 $format = 'd-m-Y';
2163 break;
2164
2165 case 'datepicker-fr-CH':
2166 case 'datepicker-de':
2167 case 'datepicker-cs':
2168 case 'datepicker-it-CH':
2169 case 'datepicker-pl':
2170 case 'datepicker-ru':
2171 case 'datepicker-tr':
2172 case 'datepicker-sk':
2173 $format = 'd.m.Y';
2174 break;
2175
2176 case 'datepicker-zh-TW':
2177 case 'datepicker-ja':
2178 $format = 'Y/m/d';
2179 break;
2180
2181 case 'datepicker-fr-CA':
2182 case 'datepicker-zh-CN':
2183 case 'datepicker-ko':
2184 $format = 'Y-m-d';
2185 break;
2186
2187 case 'datepicker-hu':
2188 $format = 'Y.m.d.';
2189 break;
2190
2191 /*case 'datepicker-ko':
2192 $format = 'Y. m. d.';
2193 break;*/
2194
2195 default:
2196 $format = 'm/d/Y';
2197 }
2198
2199 $format = apply_filters( 'apb_current_date_format', $format );
2200
2201 return $format;
2202 }
2203
2204
2205 /**
2206 * Convert date string from other format to m/d/Y.
2207 *
2208 * @param string $date_string Date string.
2209 * @param string $date_format Optional. Date format. If empty, use current date format.
2210 *
2211 * @return string
2212 * @since 1.11
2213 */
2214 public static function convert_date_to_mdY( $date_string, $date_format = null ) {
2215 if ( ! $date_format ) {
2216 $date_format = AWE_function::get_current_date_format();
2217 }
2218
2219 if ( 'm/d/Y' == $date_format ) {
2220 return $date_string;
2221 }
2222
2223 $d = DateTime::createFromFormat( $date_format, $date_string );
2224 return $d->format( 'm/d/Y' );
2225 }
2226
2227 public static function convert_date_to_Ymd( $date_string, $date_format = null ) {
2228 if ( ! $date_format ) {
2229 $date_format = AWE_function::get_current_date_format();
2230 }
2231
2232 if ( 'Y-m-d' == $date_format ) {
2233 return $date_string;
2234 }
2235
2236 $d = DateTime::createFromFormat( $date_format, $date_string );
2237 return $d->format( 'Y-m-d' );
2238 }
2239
2240
2241
2242 public static function is_system_format( $date_string ) {
2243 $stamp = strtotime( $date_string );
2244
2245 if ( ! $stamp ) {
2246 return false;
2247 }
2248
2249 return date( 'm/d/Y', $stamp ) === $date_string;
2250 }
2251
2252
2253 /**
2254 * Replace some value in email.
2255 *
2256 * @param string $string String need replace.
2257 * @param object $order_id Order ID.
2258 * @return string
2259 * @since 1.11
2260 */
2261 public static function email_str_replace( $string, $order_id ) {
2262 $new_string = $string;
2263
2264 $key = array(
2265 '{site_title}',
2266 '{order_number}',
2267 '{order_date}',
2268 );
2269
2270 $value = array(
2271 get_bloginfo( 'name' ),
2272 $order_id,
2273 get_the_time( AWE_function::get_current_date_format(), $order_id ),
2274 );
2275
2276 $new_string = str_replace( $key, $value, $string );
2277
2278 return apply_filters( 'email_str_replace', $new_string, $string, $order_id );
2279 }
2280
2281
2282 /**
2283 * Get check available page url.
2284 *
2285 * @return string
2286 * @since 2.0
2287 */
2288 public static function get_check_available_page() {
2289 $page_id = AWE_function::get_option( 'check_avb' );
2290 // if ( AWE_function::activated_wpml() ) {
2291 // $page_id = icl_object_id( $page_id, 'page', true );
2292 // }
2293 return esc_url( get_permalink( $page_id ) );
2294 }
2295
2296
2297 /**
2298 * Get check available page id.
2299 *
2300 * @return string
2301 * @since 2.0
2302 */
2303 public static function get_check_available_page_id() {
2304 $page_id = AWE_function::get_option( 'check_avb' );
2305 // if ( AWE_function::activated_wpml() ) {
2306 // $page_id = icl_object_id( $page_id, 'page', true );
2307 // }
2308 return $page_id;
2309 }
2310
2311
2312 /**
2313 * Get list room page id.
2314 *
2315 * @return string
2316 * @since 2.0
2317 */
2318 public static function get_list_room_page_id() {
2319 $page_id = AWE_function::get_option( 'list_room' );
2320 // if ( AWE_function::activated_wpml() ) {
2321 // $page_id = icl_object_id( $page_id, 'page', true );
2322 // }
2323 return $page_id;
2324 }
2325
2326
2327 /**
2328 * Get default checkout type page id.
2329 *
2330 * @return string
2331 * @since 2.0
2332 */
2333 public static function get_checkout_page_id() {
2334 $page_id = AWE_function::get_option( 'apb_checkout' );
2335 // if ( AWE_function::activated_wpml() ) {
2336 // $page_id = icl_object_id( $page_id, 'page', true );
2337 // }
2338 return $page_id;
2339 }
2340
2341
2342 /**
2343 * Get checkout page url
2344 * Include checkout type checking.
2345 *
2346 * @return string
2347 * @since 2.2.1
2348 */
2349 public static function get_checkout_page_url() {
2350 $link_checkout = '';
2351 if ( 1 == get_option( 'rooms_checkout_style' ) ) {
2352 if ( class_exists( 'WC_Cart' ) ) {
2353 global $woocommerce;
2354 $link_checkout = $woocommerce->cart->get_checkout_url();
2355 }
2356 } elseif ( 2 == get_option( 'rooms_checkout_style' ) ) {
2357 $link_checkout = get_permalink( AWE_function::get_checkout_page_id() );
2358 }
2359
2360 return apply_filters( 'apb_get_checkout_page_url', $link_checkout );
2361 }
2362
2363
2364 /**
2365 * Check current page is check available page.
2366 *
2367 * @return bool
2368 * @since 2.0
2369 */
2370 public static function is_check_available_page() {
2371 $page_id = AWE_function::get_option( 'check_avb' );
2372 // if ( AWE_function::activated_wpml() ) {
2373 // $page_id = icl_object_id( $page_id, 'page', true );
2374 // }
2375 return get_the_ID() == $page_id;
2376 }
2377
2378
2379 /**
2380 * Get datepicker language.
2381 *
2382 * @return string
2383 * @since 2.0
2384 */
2385 public static function get_datepicker_lang() {
2386 $lang = AWE_function::get_option( 'datepicker_lang' );
2387 // if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
2388 // $lang = AWE_function::lang_code_wpml_to_datepicker( ICL_LANGUAGE_CODE );
2389 // // $format = icl_translate( 'Formats', $format, $format );
2390 // }
2391
2392 $lang = apply_filters( 'apb_get_datepicker_lang', $lang );
2393
2394 return $lang;
2395 }
2396
2397
2398 /**
2399 * Get max nights.
2400 *
2401 * @return int
2402 * @since 2.0
2403 */
2404 public static function get_max_night() {
2405 return absint( get_option( 'max_night' ) );
2406 }
2407
2408
2409 /**
2410 * Get max adult.
2411 *
2412 * @return int
2413 * @since 2.0
2414 */
2415 public static function get_max_adult() {
2416 return absint( get_option( 'max_adult' ) );
2417 }
2418
2419
2420 /**
2421 * Get max child.
2422 *
2423 * @return int
2424 * @since 2.0
2425 */
2426 public static function get_max_child() {
2427 return absint( get_option( 'max_child' ) );
2428 }
2429
2430
2431 /**
2432 * Remove old version data.
2433 * @return void
2434 * @since 2.1
2435 */
2436 public static function reset_old_data() {
2437 global $wpdb;
2438 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}rooms_availability" );
2439 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}rooms_booking_unit_options" );
2440 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}rooms_pricing" );
2441 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}rooms_unit_type" );
2442 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}apb_availability" );
2443 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}apb_booking_options" );
2444 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}apb_pricing" );
2445 $wpdb->query( "DELETE FROM {$wpdb->prefix}posts WHERE post_type IN ('apb_room_type', 'apb_room', 'shop_order', 'apb_order')" );
2446 }
2447
2448
2449 /**
2450 * Check if show single calendar.
2451 * @return bool
2452 */
2453 public static function show_single_calendar() {
2454 return absint( get_option( 'apb_show_single_calendar' ) );
2455 }
2456
2457
2458 public static function get_similar_name( $room_id ) {
2459 _deprecated_function( __FUNCTION__, '2.4' );
2460 $room = get_post( $room_id );
2461
2462 $room_type_id = $room->post_parent;
2463
2464 $room_title = $room->post_title;
2465
2466 $room_type_title = get_the_title( $room_type_id );
2467
2468 $number_of_rooms = get_post_meta( $room_type_id, 'number_of_rooms', true );
2469
2470 $result = 1 == $number_of_rooms ? $room_title : $room_type_title . ' (' . $room_title . ')';
2471
2472 return $result;
2473 }
2474
2475
2476 public static function get_extra_guest_data( $base, $adult, $child ) {
2477 $result = array( 'adult' => 0, 'child' => 0 );
2478
2479 $total = $adult + $child;
2480
2481 if ( $total > $base ) {
2482 $extra_guess = $total - $base;
2483
2484 if ( $adult <= $base ) {
2485 $result['child'] = $extra_guess;
2486 } else {
2487 $result['adult'] = $adult - $base;
2488 $result['child'] = $child;
2489 }
2490 }
2491
2492 return apply_filters( 'apb_get_extra_guest_data', $result, $base, $adult, $child );
2493 }
2494
2495
2496 public static function get_extra_price_data( $room_type_id, $adult, $child ) {
2497 $result = array( 'adult' => 0, 'child' => 0 );
2498
2499 $base_price_for = get_post_meta( $room_type_id, 'base_price_for', true ) ? absint( get_post_meta( $room_type_id, 'base_price_for', true ) ) : 1;
2500
2501 $extra_guess_data = self::get_extra_guest_data( $base_price_for, $adult, $child );
2502
2503 $extra_adult = get_post_meta( $room_type_id, 'extra_adult', true );
2504 $extra_child = get_post_meta( $room_type_id, 'extra_child', true );
2505
2506 if ( ! empty( $extra_guess_data['adult'] ) && ! empty( $extra_adult ) ) {
2507 foreach ( $extra_adult as $v ) {
2508 if ( $v['number'] == $extra_guess_data['adult'] ) {
2509 $result['adult'] = $v['price'];
2510 break;
2511 }
2512 }
2513 }
2514
2515 if ( ! empty( $extra_guess_data['child'] ) && ! empty( $extra_child ) ) {
2516 foreach ( $extra_child as $v ) {
2517 if ( $v['number'] == $extra_guess_data['child'] ) {
2518 $result['child'] = $v['price'];
2519 break;
2520 }
2521 }
2522 }
2523
2524 return $result;
2525 }
2526
2527
2528 /**
2529 * Get room type name to display in input field in room type meta box.
2530 *
2531 * @param int $room_type_id Room type ID.
2532 * @return string
2533 * @since 2.2.1
2534 */
2535 public static function get_room_name_for_meta_value( $room_type_id ) {
2536 $rooms = self::get_rooms_of_room_type( $room_type_id );
2537 if ( empty( $rooms ) ) {
2538 return '';
2539 }
2540
2541 $value = array();
2542 foreach ( $rooms as $room ) {
2543 $value[] = $room->post_title;
2544 }
2545
2546 return implode( ',', $value );
2547 }
2548
2549
2550 /**
2551 * Support order by title numeric.
2552 *
2553 * @param string $orderby Post where string.
2554 * @return string
2555 * @since 2.2.1
2556 */
2557 public static function orderby_post_title_int( $orderby ) {
2558 return 'LENGTH(wp_posts.post_title) ASC, wp_posts.post_title ASC';
2559 }
2560
2561
2562 /**
2563 * Calculate total price.
2564 *
2565 * @param int $room_type_id Room type ID.
2566 * @param string $from Arrival date in m/d/Y format.
2567 * @param string $to Departure date in m/d/Y format.
2568 * @param int $adult Number of adults.
2569 * @param int $child Number of children.
2570 * @param array $package_data Package data.
2571 * @return float Total price.
2572 */
2573 public static function calculate_total_price( $room_type_id, $from, $to, $adult, $child, $package_data = null ) {
2574 $total = 0;
2575
2576 $number_nights = count( self::range_date( $from, $to ) ) - 1;
2577
2578 $price_nights = self::get_pricing_of_days( $from, $to, $room_type_id );
2579 $total_price_nights = self::get_total_price( $price_nights );
2580
2581
2582 $extra_sale = get_post_meta( $room_type_id, 'extra_sale', true );
2583 $base_price_for = get_post_meta( $room_type_id, 'base_price_for', true );
2584 $extra_guess_data = self::get_extra_guest_data( $base_price_for, $adult, $child );
2585 $extra_price_data = self::get_extra_price_data( $room_type_id, $adult, $child );
2586
2587 $total += $total_price_nights;
2588
2589 if ( ! empty( $extra_price_data['adult'] ) ) {
2590 $total += $number_nights * $extra_price_data['adult'];
2591 }
2592
2593 if ( ! empty( $extra_price_data['child'] ) ) {
2594 $total += $number_nights * $extra_price_data['child'];
2595 }
2596
2597 if ( ! empty( $extra_sale ) ) {
2598 $number_dates = $number_nights + 1;
2599 $data_extra_sale = self::apb_get_extra_sale( $extra_sale, $number_dates, $from );
2600 if ( ! empty( $data_extra_sale ) ) {
2601 if ( 'sub' == $data_extra_sale['sale_type'] ) {
2602 $total = $total - $data_extra_sale['amount'];
2603 }
2604 if ( 'decrease' == $data_extra_sale['sale_type'] ) {
2605 $total = $total - $data_extra_sale['amount'] / 100 * $total;
2606 }
2607 }
2608 }
2609
2610 return $total;
2611 }
2612
2613
2614 /**
2615 * Create room for room type.
2616 *
2617 * @param int $room_type_id Room type id.
2618 * @param string $room_name Room name.
2619 * @return int|false
2620 * @since 2.2.1
2621 */
2622 public static function create_room( $room_type_id, $room_name ) {
2623 $room_id = wp_insert_post( array(
2624 'post_parent' => $room_type_id,
2625 'post_title' => $room_name,
2626 'post_type' => 'apb_room',
2627 'post_status' => 'publish',
2628 ) );
2629
2630 return $room_id;
2631 }
2632
2633
2634 /**
2635 * Get displayed tax text.
2636 *
2637 * @param float $price Price to calculate.
2638 * @param float $tax_amount Tax amount.
2639 * @param string $tax_type Tax type.
2640 * @return string
2641 * @since 2.2.1
2642 */
2643 public static function get_displayed_tax( $price = null, $tax_amount = null, $tax_type = null ) {
2644 if ( ! $tax_amount ) {
2645 $tax_amount = (float) get_option( 'apb_tax_amount' );
2646 }
2647
2648 if ( ! $tax_type ) {
2649 $tax_type = get_option( 'apb_tax_type' );
2650 }
2651
2652 $output = '';
2653
2654 if ( ! empty( $tax_amount ) ) {
2655 if ( ! $price ) {
2656 if ( 'fixed' == $tax_type ) {
2657 $output = AWE_function::apb_price( $tax_amount );
2658 } else {
2659 $output = $tax_amount . '%';
2660 }
2661 } else {
2662 $output = self::apb_price( self::calculate_tax( $price, $tax_amount, $tax_type ) - $price );
2663
2664 }
2665 }
2666
2667 return apply_filters( 'apb_get_displayed_tax', $output );
2668 }
2669
2670
2671 /**
2672 * Calculate tax.
2673 *
2674 * @param float $price Price to calculate.
2675 * @param float $tax_amount Tax amount.
2676 * @param string $tax_type Tax type.
2677 * @return float
2678 * @since 2.2.1
2679 */
2680 public static function calculate_tax( $price, $tax_amount = null, $tax_type = null ) {
2681 if ( ! $tax_amount ) {
2682 $tax_amount = (float) get_option( 'apb_tax_amount' );
2683 }
2684
2685 if ( ! $tax_type ) {
2686 $tax_type = get_option( 'apb_tax_type' );
2687 }
2688
2689 $result = $price;
2690
2691 if ( ! empty( $tax_amount ) ) {
2692 if ( 'fixed' == $tax_type ) {
2693 $result += $tax_amount;
2694 } else {
2695 $result += $tax_amount / 100 * $price;
2696 }
2697 }
2698
2699 return apply_filters( 'apb_calculate_tax', $result, $price );
2700 }
2701
2702
2703 /**
2704 * Get option.
2705 *
2706 * @param string $option_name Option name.
2707 * @return mixed Option value.
2708 * @since 2.3.1
2709 */
2710 public static function get_option( $option_name ) {
2711 $option_name = apply_filters( 'apb_option_name', $option_name );
2712
2713 $value = get_option( $option_name );
2714
2715 return $value;
2716 }
2717
2718
2719 /**
2720 * Update option.
2721 *
2722 * @param string $option_name Option name.
2723 * @param mixed $value Option value.
2724 * @since 2.3.1
2725 */
2726 public static function update_option( $option_name, $value ) {
2727 $option_name = apply_filters( 'apb_option_name', $option_name );
2728 update_option( $option_name, $value );
2729 }
2730
2731
2732 /**
2733 * Check WPML activated.
2734 *
2735 * @return bool
2736 * @since 2.3.1
2737 */
2738 public static function activated_wpml() {
2739 return class_exists( 'SitePress' );
2740 }
2741
2742
2743 /**
2744 * Get master post id. Use for wpml.
2745 *
2746 * @param int $id Post id.
2747 * @return int
2748 * @since 2.4
2749 */
2750 public static function get_master_post_id( $id ) {
2751 if ( AWE_function::activated_wpml() ) {
2752 if ( 'apb_room' == get_post_type( $id ) ) {
2753 if ( get_post_meta( $id, '_icl_lang_duplicate_of', true ) ) {
2754 return (int) get_post_meta( $id, '_icl_lang_duplicate_of', true );
2755 }
2756
2757 return $id;
2758 }
2759
2760 return icl_object_id( $id, get_post_type( $id ), true, wpml_get_default_language() );
2761 }
2762
2763 return $id;
2764 }
2765
2766
2767 /**
2768 * Get room type id in current language.
2769 *
2770 * @param int $id room type id.
2771 * @return int
2772 * @since 2.4.1
2773 */
2774 public static function get_room_type_id_current_lang( $id ) {
2775 if ( AWE_function::activated_wpml() ) {
2776 $id = icl_object_id( $id, 'apb_room_type', true, ICL_LANGUAGE_CODE );
2777 }
2778
2779 return $id;
2780 }
2781
2782
2783 /**
2784 * Check customer name and email, if email doesn't exists, create new customer.
2785 *
2786 * @param string $name Customer name.
2787 * @param string $email Customer email.
2788 * @return int Customer id.
2789 * @since 2.4.1
2790 */
2791 public static function maybe_create_customer( $name, $email ) {
2792 if ( empty( $name ) || empty( $email ) ) {
2793 return false;
2794 }
2795
2796 if ( email_exists( $email ) ) {
2797 $customer = get_user_by( 'email', $email );
2798 $customer_id = $customer->ID;
2799 } else {
2800 $random_password = wp_generate_password( 12, false );
2801 $customer_id = wp_insert_user( array(
2802 'user_email' => $email,
2803 'user_login' => $email,
2804 'display_name' => $name,
2805 'user_pass' => $random_password,
2806 'role' => 'customer',
2807 ) );
2808 }
2809
2810 return $customer_id;
2811 }
2812
2813
2814 /**
2815 * Get customer email from order id.
2816 *
2817 * @param int $order_id Order id.
2818 * @return string|false
2819 */
2820 public static function get_customer_email( $order_id ) {
2821 $customer = get_post_meta( $order_id, 'custommer', true );
2822 $customer = get_userdata( $customer );
2823
2824 if ( ! empty( $customer->user_email ) ) {
2825 return $customer->user_email;
2826 }
2827
2828 return false;
2829 }
2830
2831
2832 /**
2833 * Get customer name from order id.
2834 *
2835 * @param int $order_id Order id.
2836 * @return string|false
2837 */
2838 public static function get_customer_name( $order_id ) {
2839 $customer = get_post_meta( $order_id, 'custommer', true );
2840 $customer = get_userdata( $customer );
2841
2842 if ( ! empty( $customer->user_login ) ) {
2843 return $customer->user_login;
2844 }
2845
2846 return false;
2847 }
2848
2849
2850 public static function wpml_get_default_room_type( $room_type_id ) {
2851 if ( ! function_exists( 'wpml_get_default_language' ) ) {
2852 return $room_type_id;
2853 }
2854
2855 return apply_filters( 'wpml_object_id', $room_type_id, 'apb_room_type', true, wpml_get_default_language() );
2856 }
2857}
2858
2859
2860/**
2861 * Filter option date_format to translate date with WPML.
2862 *
2863 * @param string $format Date format.
2864 *
2865 * @return string
2866 * @since 1.11
2867 */
2868function apb_translate_date_format( $format ) {
2869 if ( function_exists( 'icl_translate' ) ) {
2870 $format = AWE_function::lang_code_wpml_to_datepicker( ICL_LANGUAGE_CODE );
2871 // $format = icl_translate( 'Formats', $format, $format );
2872 }
2873 return $format;
2874}
2875// add_filter( 'option_date_format', 'apb_translate_date_format' );
2876
2877
2878/**
2879 * Send mail to user when order is cancelled.
2880 *
2881 * @param int $order_id Order ID.
2882 * @return void
2883 * @since 1.11
2884 */
2885function apb_send_mail_when_cancelled( $order_id ) {
2886 $config_mail = get_option( 'apb_mail_cancel' );
2887 if ( empty( $config_mail['notice_status'] ) ) {
2888 return;
2889 }
2890
2891 $customer_email = AWE_function::get_customer_email( $order_id );
2892 $customer_name = AWE_function::get_customer_name( $order_id );
2893
2894 $subject = ! empty( $config_mail['subject'] ) ? $config_mail['subject'] : '[{site_title}] Cancelled order ({order_number})';
2895 $subject = AWE_function::email_str_replace( $subject, $order_id );
2896
2897 ob_start();
2898 do_action( 'apb_mail_cancelled_order', $order_id );
2899 $message = ob_get_clean();
2900
2901 $email = new APB_Email();
2902
2903 if ( isset( $config_mail['notice_status'] ) && 1 == $config_mail['notice_status'] ) {
2904 $message_user = $email->apb_style_inline( $email->apb_wrap_message( $config_mail['header'], $message ) );
2905 $email->apb_sendMail( $customer_email, $subject, $message_user, 0, $customer_name );
2906 }
2907 $email->destroy();
2908}
2909add_action( 'apb-cancelled_shop_order', 'apb_send_mail_when_cancelled' );
2910
2911
2912/**
2913 * Send mail to user when order is completed.
2914 *
2915 * @param int $order_id Order ID.
2916 * @return void
2917 * @since 1.11
2918 */
2919function apb_send_mail_when_completed( $order_id ) {
2920 $config_mail = get_option( 'apb_mail_complete' );
2921 if ( empty( $config_mail['notice_status'] ) ) {
2922 return;
2923 }
2924
2925 $customer_email = AWE_function::get_customer_email( $order_id );
2926 $customer_name = AWE_function::get_customer_name( $order_id );
2927
2928 $subject = ! empty( $config_mail['subject'] ) ? $config_mail['subject'] : 'Your {site_title} booking from {order_date} is completed';
2929 $subject = AWE_function::email_str_replace( $subject, $order_id );
2930
2931 ob_start();
2932 do_action( 'apb_mail_complete_order', $order_id );
2933 $message = ob_get_clean();
2934
2935 $email = new APB_Email();
2936
2937 if ( isset( $config_mail['notice_status'] ) && 1 == $config_mail['notice_status'] ) {
2938 $message_user = $email->apb_style_inline( $email->apb_wrap_message( $config_mail['header'], $message ) );
2939 $email->apb_sendMail( $customer_email, $subject, $message_user, 0, $customer_name );
2940 }
2941 $email->destroy();
2942}
2943add_action( 'apb-completed_shop_order', 'apb_send_mail_when_completed' );
2944
2945
2946/**
2947 * Update available and order status when order is cancelled.
2948 *
2949 * @param int $order_id Order ID.
2950 * @return void
2951 * @since 1.11
2952 */
2953function apb_update_available_when_cancelled( $order_id ) {
2954 $from = get_post_meta( $order_id, 'from', true );
2955 $to = get_post_meta( $order_id, 'to', true );
2956 $order_data = get_post_meta( $order_id, 'apb_data_order', true );
2957
2958 if ( ! is_array( $order_data ) ) {
2959 return;
2960 }
2961
2962 foreach ( $order_data as $v ) {
2963 Boxes_info_booking::update_status( $v['id'], 'apb-cancelled' );
2964 AWE_function::update_available( $from, $to, $v['order_room_id'], 2 );
2965 // AWE_Controller::update_day_available( $from, $to, $v['order_room_id'], 2 );
2966 }
2967}
2968add_action( 'apb-cancelled_shop_order', 'apb_update_available_when_cancelled' );
2969add_action( 'trash_shop_order', 'apb_update_available_when_cancelled' );
2970
2971
2972function apb_delete_order( $order_id ) {
2973 if ( 'apb_order' != get_post_type( $order_id ) ) {
2974 return;
2975 }
2976
2977 $order_data = get_post_meta( $order_id, 'apb_data_order', true );
2978
2979 if ( is_array( $order_data ) ) {
2980 foreach ( $order_data as $v ) {
2981 AWE_function::update_available( $v['from'], $v['to'], $v['order_room_id'], 2 );
2982
2983 wp_delete_post( $v['id'], true );
2984 }
2985 }
2986}
2987add_action( 'delete_post', 'apb_delete_order' );
2988
2989
2990/**
2991 * Update available and order status when order is pending.
2992 *
2993 * @param int $order_id Order ID.
2994 * @return void
2995 * @since 2.0
2996 */
2997function apb_update_available_when_pending( $order_id ) {
2998 $from = get_post_meta( $order_id, 'from', true );
2999 $to = get_post_meta( $order_id, 'to', true );
3000 $order_data = get_post_meta( $order_id, 'apb_data_order', true );
3001
3002 if ( ! is_array( $order_data ) ) {
3003 return;
3004 }
3005
3006 foreach ( $order_data as $v ) {
3007 Boxes_info_booking::update_status( $v['id'], 'apb-pending' );
3008 AWE_function::update_available( $from, $to, $v['order_room_id'], 3 );
3009 }
3010}
3011add_action( 'apb-pending_shop_order', 'apb_update_available_when_pending' );
3012
3013
3014/**
3015 * Update available and order status when order is completed.
3016 *
3017 * @param int $order_id Order ID.
3018 * @return void
3019 * @since 2.0
3020 */
3021function apb_update_available_when_completed( $order_id ) {
3022 $from = get_post_meta( $order_id, 'from', true );
3023 $to = get_post_meta( $order_id, 'to', true );
3024 $order_data = get_post_meta( $order_id, 'apb_data_order', true );
3025
3026 if ( ! is_array( $order_data ) ) {
3027 return;
3028 }
3029
3030 foreach ( $order_data as $v ) {
3031 Boxes_info_booking::update_status( $v['id'], 'apb-completed' );
3032 AWE_function::update_available( $from, $to, $v['order_room_id'], 0 );
3033 }
3034}
3035add_action( 'apb-completed_shop_order', 'apb_update_available_when_completed' );
3036
3037
3038/**
3039 * Add room when import.
3040 *
3041 * @param int $post_id Room type ID.
3042 * @param string $key Meta key.
3043 * @param string $value Meta value.
3044 * @return void
3045 */
3046function apb_add_room_import( $post_id, $key, $value ) {
3047 if ( 'apb_room_type' == get_post_type( $post_id ) && 'number_of_rooms' == $key && ! empty( $value ) ) {
3048 AWE_function::bulk_create_rooms( $post_id, $value );
3049 }
3050}
3051add_action( 'import_post_meta', 'apb_add_room_import', 10, 3 );
3052
3053
3054/**
3055 * Remove room when trash room type.
3056 *
3057 * @param int $post_id Room type ID.
3058 * @return void
3059 * @since 2.1
3060 */
3061function apb_remove_room_when_remove_room_type( $post_id ) {
3062 if ( 'apb_room_type' != get_post_type( $post_id ) ) {
3063 return;
3064 }
3065
3066 $rooms = AWE_function::get_rooms_of_room_type( $post_id );
3067 foreach ( $rooms as $room ) {
3068 wp_trash_post( $room->ID );
3069 }
3070}
3071add_action( 'trash_apb_room_type', 'apb_remove_room_when_remove_room_type' );
3072
3073
3074/**
3075 * Create room when untrash room type.
3076 *
3077 * @param WP_Post $post Room type object.
3078 * @return void
3079 * @since 2.1
3080 */
3081function apb_restore_room_when_restore_room_type( $post ) {
3082 if ( 'apb_room_type' != $post->post_type ) {
3083 return;
3084 }
3085
3086 $rooms = AWE_function::get_rooms_of_room_type( $post->ID, true );
3087
3088 foreach ( $rooms as $room ) {
3089 wp_publish_post( $room->ID );
3090 }
3091}
3092add_action( 'trash_to_publish', 'apb_restore_room_when_restore_room_type' );
3093
3094
3095/**
3096 * Filter to body class.
3097 *
3098 * @param array $classes Body class.
3099 * @return array
3100 *
3101 * @since 2.2
3102 */
3103function apb_body_class( $classes ) {
3104 if ( is_page() && get_the_ID() == AWE_function::get_checkout_page_id() ) {
3105 $classes[] = 'apb-checkout-page';
3106 }
3107
3108 return $classes;
3109}
3110add_filter( 'body_class', 'apb_body_class' );
3111
3112
3113/**
3114 * Fix issue with Woocommerce Multilingual
3115 *
3116 * Cart always empty when book.
3117 *
3118 * @since 2.2.1
3119 */
3120function apb_fix_wcml_empty_cart() {
3121 if ( ! class_exists( 'woocommerce_wpml' ) ) {
3122 return;
3123 }
3124
3125 global $woocommerce_wpml;
3126 $wcml_product = $woocommerce_wpml->products;
3127
3128 remove_action( 'woocommerce_before_calculate_totals', array( $wcml_product, 'woocommerce_calculate_totals' ) );
3129}
3130add_action( 'init', 'apb_fix_wcml_empty_cart', 99 );
3131
3132
3133/**
3134 * Remove room when remove room type.
3135 *
3136 * @param int $post_id Room type id.
3137 * @return void
3138 * @since 2.2.1
3139 */
3140function apb_remove_rooms( $post_id ) {
3141 if ( 'apb_room_type' != get_post_type( $post_id ) ) {
3142 return;
3143 }
3144
3145 $rooms = AWE_function::get_rooms_of_room_type( $post_id );
3146 foreach ( $rooms as $room ) {
3147 wp_delete_post( $room->ID, true );
3148 }
3149}
3150add_action( 'delete_post', 'apb_remove_rooms' );
3151
3152
3153/**
3154 * Duplicate room when room type is duplicated.
3155 *
3156 * @param int $master_post_id Master post id.
3157 * @param string $lang Language code.
3158 * @param array $post_array Post array.
3159 * @param int $id Pois id.
3160 * @since 2.3.1
3161 */
3162function apb_duplicate_room( $master_post_id, $lang, $post_array, $id ) {
3163 if ( 'apb_room_type' != get_post_type( $master_post_id ) ) {
3164 return;
3165 }
3166
3167 global $sitepress;
3168
3169 $rooms = AWE_function::get_rooms_of_room_type( $master_post_id );
3170 foreach ( $rooms as $room ) {
3171 $new_room_id = $sitepress->make_duplicate( $room->ID, $lang );
3172
3173 wp_update_post( array(
3174 'ID' => $new_room_id,
3175 'post_parent' => $id,
3176 ) );
3177 }
3178}
3179add_action( 'icl_make_duplicate', 'apb_duplicate_room', 10, 4 );
3180
3181
3182/**
3183 * Filter option name.
3184 *
3185 * @param string $option_name Option name.
3186 * @return string
3187 * @since 2.3.1
3188 */
3189function apb_filter_option_name( $option_name ) {
3190 if ( AWE_function::activated_wpml() ) {
3191 $default_lang = wpml_get_default_language();
3192 $current_lang = ICL_LANGUAGE_CODE;
3193
3194 if ( $current_lang != $default_lang ) {
3195 $option_name = $option_name . '_' . $current_lang;
3196 }
3197 }
3198
3199 return $option_name;
3200}
3201add_filter( 'apb_option_name', 'apb_filter_option_name' );
3202
3203
3204/**
3205 * Filter product class. Fix bug can't add to cart in woocommerce 2.6.
3206 *
3207 * @param string $class Product class.
3208 * @param string $product_type Product type.
3209 * @param string $post_type Post type.
3210 * @return string
3211 * @since 2.5.1
3212 */
3213function apb_wc_product_class( $class, $product_type, $post_type ) {
3214 if ( 'apb_room_type' == $post_type || 'apb_room' == $post_type ) {
3215 $class = 'WC_Product_Simple';
3216 }
3217
3218 return $class;
3219}
3220add_filter( 'woocommerce_product_class', 'apb_wc_product_class', 10, 3 );
3221
3222
3223function apb_unavailable_message_guest( $room_type_id ) {
3224 echo '<p class="error">';
3225
3226 $messages = array();
3227
3228 $messages[] = esc_html__( 'The search information is not right with the following requirements:', 'awebooking' );
3229
3230 $messages[] = sprintf(
3231 esc_html__( 'Minimum guests: %s', 'awebooking' ),
3232 get_post_meta( $room_type_id, 'min_sleeps', true )
3233 );
3234
3235 $messages[] = sprintf(
3236 esc_html__( 'Maximum guests: %s', 'awebooking' ),
3237 get_post_meta( $room_type_id, 'max_sleeps', true )
3238 );
3239
3240 $messages[] = sprintf(
3241 esc_html__( 'Minimum children: %s', 'awebooking' ),
3242 get_post_meta( $room_type_id, 'min_children', true )
3243 );
3244
3245 $messages[] = sprintf(
3246 esc_html__( 'Maximum children: %s', 'awebooking' ),
3247 get_post_meta( $room_type_id, 'max_children', true )
3248 );
3249
3250 $messages[] = sprintf(
3251 esc_html__( 'Minimum nights: %s', 'awebooking' ),
3252 get_post_meta( $room_type_id, 'min_night', true )
3253 );
3254
3255
3256 echo implode( '<br>', $messages ); // WPCS: XSS ok.
3257 echo '<br>';
3258
3259 $messages[] = esc_html_e( 'Please change your search information and try again.. Thanks', 'awebooking' );
3260
3261 echo '</p>';
3262}
3263add_action( 'apb_unavailable_message_guest', 'apb_unavailable_message_guest' );
3264add_action( 'apb_unavailable_message_min-night', 'apb_unavailable_message_min_night' );
3265
3266
3267function apb_unavailable_message_unavailable( $room_type_id ) {
3268 echo '<p class="error">';
3269
3270 printf(
3271 esc_html__( 'There are no rooms available in %s on these dates. Please change your search and try again. Thanks', 'awebooking' ),
3272 '<b>'.strtoupper( get_the_title( $room_type_id ) ).'</b>'
3273 );
3274
3275 echo '</p>';
3276}
3277add_action( 'apb_unavailable_message_unavailable', 'apb_unavailable_message_unavailable' );
3278
3279/**
3280 * [apb_export_to_excel description]
3281 * @param [type] $array [array of order id export]
3282 * @return [type] [description]
3283 */
3284function apb_export_to_excel( $array ) {
3285 $args = array(
3286 'post_type' => 'shop_order',
3287 'posts_per_page' => '-1',
3288 'post_status' => array( 'apb-pending', 'apb-completed' ),
3289 'post__in' => $array,
3290);
3291$posts = get_posts( $args );
3292
3293$html = '';
3294$html .= '<table>';
3295 $html .= '<thead>';
3296 $html .= '<th>' . __( 'Order','awebooking' ) . '</th>';
3297 $html .= '<th>' . __( 'Arrival Date','awebooking' ) . '</th>';
3298 $html .= '<th>' . __( 'Departure Date','awebooking' ) . '</th>';
3299 $html .= '<th>' . __( 'Username','awebooking' ) . '</th>';
3300 $html .= '<th>' . __( 'Email','awebooking' ) . '</th>';
3301 $html .= '<th>' . __( 'Room','awebooking' ) . '</th>';
3302 $html .= '<th>' . __( 'Adult','awebooking' ) . '</th>';
3303 $html .= '<th>' . __( 'Child','awebooking' ) . '</th>';
3304 $html .= '<th>' . __( 'Order Status','awebooking' ) . '</th>';
3305 $html .= '<th>' . __( 'Total','awebooking' ) . '</th>';
3306 $html .= '</thead>';
3307 $html .= '<tbody>';
3308 $username = '';
3309 $i=1;
3310 foreach ($posts as $post) {
3311 $order_data = AWE_Export::get_order_data( $post->ID );
3312 $order_date = AWE_Export::get_date( $post->ID );
3313
3314 $html .= '<tr>';
3315 $html .= '<td>' . $i++ . '</td>';
3316 $html .= '<td>' . $order_date['arrival_date'] . '</td>';
3317 $html .= '<td>' . $order_date['departure_date'] . '</td>';
3318 $html .= '<td>' . AWE_Export::get_user_name( $post->ID ) . '</td>';
3319 $html .= '<td>' . AWE_Export::get_email( $post->ID ) . '</td>';
3320 $html .= '<td>' . AWE_Export::get_room_current( $order_data['order_room_id'] ) . '</td>';
3321 $html .= '<td>' . $order_data['room_adult'] . '</td>';
3322 $html .= '<td>' . $order_data['room_child'] . '</td>';
3323 $html .= '<td>' . AWE_Export::get_order_status( $post->ID ) . '</td>';
3324 $html .= '<td>' . $order_data['total_price'] . '</td>';
3325 $html .= '</tr>';
3326 }
3327 $html .= '</tbody>';
3328
3329
3330$html .= '</table>';
3331
3332return apply_filters( 'apb_export_to_excel', $html );
3333}
3334
3335/**
3336 * [apb_calculate_deposit description]
3337 * @param [varchar] $prepayment_type [type of deposit]
3338 * @param [varchar] $total [amount of cash]
3339 * @return [void] [amount need to pay]
3340 */
3341function apb_calculate_remain_amount($prepayment_type, $total) {
3342 $amount = $total;
3343 if( 'full' == $prepayment_type ) {
3344 return '0';
3345 } else {
3346
3347 $ex = explode( '|', $prepayment_type );
3348 if( '%' == $ex[1]) {
3349 return $total - ( ($ex[0]/100) * $total );
3350 } else {
3351 return $total - $prepayment_type;
3352 }
3353 }
3354}
3355
3356function apb_calculate_deposit($prepayment_type, $total) {
3357 $amount = $total;
3358 if( 'full' == $prepayment_type ) {
3359 return $amount;
3360 } else {
3361
3362 $ex = explode( '|', $prepayment_type );
3363 if( '%' == $ex[1]) {
3364 return ($ex[0]/100) * $total;
3365 } else {
3366 return $prepayment_type;
3367 }
3368 }
3369}
3370
3371
3372function apb_get_deposit_type($value) {
3373 switch ($value) {
3374 case 'percent':
3375 return __( 'Prepayment by percent','awebooking' );
3376 break;
3377 case 'money':
3378 return __( 'Prepayment by money number','awebooking' );
3379 break;
3380
3381 default:
3382
3383 break;
3384 }
3385}