PluginProbe
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar / 2.1.22
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar v2.1.22
2.1.22 2.1.21 2.1.20 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 trunk 1.1 2.0 2.0.1 2.0.10.2 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.18 2.0.2 2.0.20 2.0.21 All 56 releases
booking-manager / core / wpbc / wpbm-bc-import.php

wpbm-bc-import.php in Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar 2.1.22, at core/wpbc/wpbm-bc-import.php

1,501 lines 64.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @description Booking Calendar integration - Import bookings from ICS events
5 *
6 * @author wpdevelop
7 * @link https://oplugins.com/
8 * @email [email protected]
9 *
10 * @modified 2017-06-28
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15
16 /**
17 * Move All imported bookings into the Trash
18 *
19 * @return bool
20 */
21 function wpbm_delete_all_imported_bookings( $params ){ //FixIn: 2.0.10.3
22
23 $booking_ics_force_trash_before_import = get_bk_option( 'booking_ics_force_trash_before_import' ); //FixIn: 2.0.10.1 <- available in Booking Calendar since update 2.0.10
24
25 if ( 'Off' == $booking_ics_force_trash_before_import ) {
26 return false;
27 }
28
29 global $wpdb;
30
31 $resource_id = $params['resource_id'];
32 $is_trash = 1;
33
34 //FixIn: 2.0.24.1 //FixIn: 9.1.2.6
35 if (
36 ( 'On' == $booking_ics_force_trash_before_import )
37 || ( 'trash' == $booking_ics_force_trash_before_import )
38 ) {
39 // Trash bookings
40 $my_sql = "UPDATE {$wpdb->prefix}booking AS bk SET bk.trash = {$is_trash} WHERE sync_gid != '' AND trash != 1 AND booking_type = {$resource_id}";
41
42 if ( false === $wpdb->query( $my_sql ) ) {
43 ?> <script type="text/javascript"> var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error( 'Error during trash booking in DB', __FILE__, __LINE__ ) ), ENT_QUOTES ); ?>'; wpbc_admin_show_message( my_message, 'error', 30000 ); </script> <?php
44 return false;
45 }
46
47 } else { // Permanently delete bookings
48
49 $bookings_obj_arr = $wpdb->get_results( "SELECT booking_id as ID FROM {$wpdb->prefix}booking WHERE sync_gid != '' AND trash != 1 AND booking_type = {$resource_id} LIMIT 0, 1000" );
50
51 $id_arr = array();
52 foreach ( $bookings_obj_arr as $booking_obj ) {
53 $id_arr[] = $booking_obj->ID;
54 }
55 $string_id = implode( ',', $id_arr );
56
57 if ( $string_id != '' ) {
58
59 // D E L E T E Dates
60 if ( false === $wpdb->query( "DELETE FROM {$wpdb->prefix}bookingdates WHERE booking_id IN ({$string_id})" ) ) { ?> <script type="text/javascript"> document.getElementById('ajax_working').innerHTML = '<div style=&quot;height:20px;width:100%;text-align:center;margin:15px auto;&quot;><?php debuge_error('Error during updating exist booking for deleting dates in BD' ,__FILE__,__LINE__); ?></div>'; </script> <?php die(); }
61 // D E L E T E Bookings
62 if ( false === $wpdb->query( "DELETE FROM {$wpdb->prefix}booking WHERE booking_id IN ({$string_id})" ) ){ ?> <script type="text/javascript"> document.getElementById('ajax_working').innerHTML = '<div style=&quot;height:20px;width:100%;text-align:center;margin:15px auto;&quot;><?php debuge_error('Error during deleting booking at DB',__FILE__,__LINE__ ); ?></div>'; </script> <?php die(); }
63 }
64
65 }
66
67 return true;
68 }
69
70
71 /**
72 * Append one extra day after the currently last imported booking date.
73 *
74 * This is an opt-in extension for sites that want to block one additional day
75 * after the imported iCalendar checkout boundary.
76 *
77 * @param array $booking_dates Dates in MySQL format.
78 *
79 * @return array
80 */
81 function wpbm_ics_import_append_extra_checkout_day( $booking_dates ) {
82
83 if ( empty( $booking_dates ) || ! is_array( $booking_dates ) ) {
84 return $booking_dates;
85 }
86
87 $last_date = $booking_dates[ count( $booking_dates ) - 1 ];
88 $last_date = strtotime( $last_date );
89 $last_date = strtotime( '+1 day', $last_date );
90
91 $booking_dates[] = date_i18n( 'Y-m-d H:i:s', $last_date );
92
93 return $booking_dates;
94 }
95
96 ////////////////////////////////////////////////////////////////////////////////////////////
97 // I M P O R T
98 ////////////////////////////////////////////////////////////////////////////////////////////
99
100 /** Import ICS feed and create bookings .in Booking Calendar
101 *
102 * @param array $attr = array(
103 'url' => ''
104 , 'resource_id' => 1
105 , 'import_conditions' => '' | 'if_dates_free' // maybe rename ??? if_dates_booked_then_not_import
106 )
107 * @return int|bool number of imported bookings or FALSE if error
108 */
109 function wpbm_ics_import_start( $attr ) {
110
111 if ( ! wpbm_is_wpbc_supported() ) {
112 // echo 'Booking Manager require Booking Calendar 9.8 or newer';
113 return wpbm_ics_import_start_legacy( $attr );
114
115 }
116
117 // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
118 /////////////////////////////////////////////////////////////////////
119 // Parse / validate parameters
120 /////////////////////////////////////////////////////////////////////
121 $defaults = array(
122 'url' => ''
123 , 'resource_id' => 1
124 , 'delete' => 0
125 , 'import_conditions' => ''
126 , 'from' => 'any' // 'today' // '00:00 today'
127 , 'from_offset' => ''
128 , 'until' => 'any' // 'year-end'
129 , 'until_offset' => ''
130 , 'max' => ''
131 , 'is_all_dates_in' => true // Conditional of Dates checking. TRUE - Remove event if al least 1 day not in conditional interval, FALSE - save event, if at leat one date in conditional interval
132 );
133 $shortcode = array();
134
135 foreach ( $attr as $param_name => $param_value ) {
136
137 switch ( $param_name) { // Validate Params
138
139 case 'url':
140 $shortcode[ $param_name ] = esc_url_raw( $param_value ); // $shortcode[ 'url' ]
141 $shortcode[ $param_name ] = str_replace( '&amp;', '&', $shortcode[ $param_name ] ); //FixIn: 2.0.14.2
142 break;
143
144 case 'import_conditions':
145 $shortcode[ $param_name ] = esc_attr( $param_value ); // $shortcode[ 'resource_id' ]
146 break;
147
148 case 'resource_id':
149 $shortcode[ $param_name ] = intval($param_value ); // $shortcode[ 'resource_id' ]
150 break;
151
152 case 'from': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-start', 'any', 'date' = 2017-08-07
153 $shortcode[ $param_name ] = $param_value;
154 break;
155
156 case 'from_offset': // 5d, 10h, 5m, 30s -- if from: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-start' }
157 $shortcode[ $param_name ] = $param_value;
158 break;
159
160 case 'until': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-end', 'any', 'date' = 2017-08-07
161 $shortcode[ $param_name ] = $param_value;
162 break;
163
164 case 'until_offset': // 5d, 10h, 5m, 30s -- if until: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-end' }
165 $shortcode[ $param_name ] = $param_value;
166 break;
167
168 case 'max':
169 $shortcode[ $param_name ] = intval( $param_value );
170 break;
171
172 case 'is_all_dates_in': // Conditional of Dates checking. TRUE - Remove event if al least 1 day not in conditional interval, FALSE - save event, if at leat one date in conditional interval
173 $shortcode[ $param_name ] = intval( $param_value );
174 break;
175
176
177 default:
178 $shortcode[ $param_name ] = $param_value;
179 break;
180 }
181 }
182 $shortcode = wp_parse_args( $shortcode, $defaults );
183 // </editor-fold>
184
185 // <editor-fold defaultstate="collapsed" desc=" Notice - Import Parameters | Error No URL" >
186
187 do_action( 'wpbc_show_debug', array( 'Import Parameters' , $shortcode ) ); // S_Y_S_T_E_M L_O_G
188
189 if ( empty( $shortcode[ 'url' ] ) ) {
190 do_action( 'wpbc_admin_show_top_notice', __( 'No ics url feed', 'booking-manager' ), 'error', 5000 ); // N_O_T_I_C_E in H_E_A_D_E_R
191 return false;
192 }
193 // </editor-fold>
194
195 ///////////////////////////////////////////////////////////////////// - Get, Parse ICS Feed
196
197 $ics_array = wpbm_ics_file_to_array( $shortcode[ 'url' ] );
198
199 // <editor-fold defaultstate="collapsed" desc=" Notice - feed contain N events | Error Importing " >
200 do_action( 'wpbc_show_debug', array( 'Imported data' , $ics_array) ); // S_Y_S_T_E_M L_O_G
201
202 // If Error
203 if ( is_wp_error( $ics_array ) ) {
204
205 $error_message = $ics_array->get_error_message();
206 do_action( 'wpbc_admin_show_top_notice', $error_message, 'error', 5000 ); // N_O_T_I_C_E in H_E_A_D_E_R
207 return false;
208 }
209
210 //FixIn: 2.0.7.3
211 $ics_array_events_num = 0;
212 if ( ( ! empty( $ics_array ) ) && ( ! empty( $ics_array[ 'events' ] ) ) && ( is_array( $ics_array[ 'events' ] ) ) ) {
213 $ics_array_events_num = count( $ics_array[ 'events' ] );
214 }
215 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
216 , sprintf ( __( '.ics feed contain %s events at URL %s', 'booking-manager' ), '<b>' . $ics_array_events_num . '</b>', '<b><a href="'. $shortcode[ 'url' ] .'">' . $shortcode[ 'url' ] . '</a></b>' )
217 , 'info', 5000 );
218 // </editor-fold>
219
220 if ( 'skip' != $shortcode['delete'] ) {
221 wpbm_delete_all_imported_bookings( array( 'resource_id' => $shortcode['resource_id'] ) ); //FixIn: 2.0.18.3 //FixIn: 2.0.10.3
222 }
223
224
225 $bk_array = array();
226 // Get Only '_BOOKING...' field from ICS array
227 if ( $ics_array !== false ) {
228 $bk_array = wpbm_get_booking_fields_from_ics_array( $ics_array[ 'events' ] );
229 }
230
231 do_action( 'wpbc_show_debug', array( 'Imported Events', $bk_array ) ); // S_Y_S_T_E_M L_O_G
232
233
234 //FixIn: 2.0.6.1 - Set timezone frrom Booking > Settings > Sync page for booking listing shortcode
235 $tzid = get_bk_option( 'booking_gcal_timezone' );
236 if ( ! empty( $tzid ) ) {
237 foreach ( $bk_array as $bk_k => $bk_ics_data_arr ) {
238
239
240 //apply our timezone from the Booking > Settings > Search page
241 foreach ( $bk_array[ $bk_k ]['_BOOKING_DATES'] as $dk => $day ) {
242 //$bk_array[ $bk_k ][ '_BOOKING_DATES' ][ $dk ] = ZDateHelper::toLocalDateTime( get_gmt_from_date( $day ), $tzid );
243
244 //FixIn: 2.0.7.4 - Skip adding Timezone to "middle" days, if days start with time 00:00:00 - its means event for full day, not the start/end time
245 // Apply timezone only to fist and last days in a list, if the change over days activated in the Booking Calendar
246 $ics_time_in_day = substr( $day, -8);
247 if ( '00:00:00' == $ics_time_in_day ) {
248 continue;
249 }
250 //FixIn: 2.0.7.4 - Skip adding Timezone to "middle" days, if Booking Calendar use change over days, to prevent of having clock icon in middle days.
251 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
252 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
253 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
254 ) { //FixIn: 2.0.5.1
255 $days_number = count( $bk_array[ $bk_k ]['_BOOKING_DATES'] );
256 if ( ( 0 != $dk ) && ( ( $days_number - 1) != $dk ) ) {
257 continue;
258 }
259 // We are append one day to the booking, so skip this day for timezone, as well
260 if ( ( get_bk_option( 'booking_ics_import_append_checkout_day' ) !== 'Off' ) && ( ( $days_number - 1) == $dk ) ) {
261 continue;
262 }
263 }
264
265 $bk_array[ $bk_k ]['_BOOKING_DATES'][ $dk ] = ZDateHelper::toLocalDateTime( $day, $tzid );
266 }
267 }
268 }
269
270 ///////////////////////////////////////////////////////////////////// - Check if these EVENTS was imported before or NOT
271
272 $booking_ics_force_import = get_bk_option( 'booking_ics_force_import' ); //FixIn: 2.0.10.1
273 $booking_condition_import_only_new = get_bk_option( 'booking_condition_import_only_new' ); //FixIn: 9.8.15.8
274 if ( false === $booking_condition_import_only_new ) {
275 // OLD:
276 if ( 'On' !== $booking_ics_force_import ) {
277 $bk_array = wpbm_clear_events_from_exist_bookings( $bk_array );
278 }
279 } else {
280 // NEW
281 if ( 'On' === $booking_condition_import_only_new ) {
282 $bk_array = wpbm_clear_events_from_exist_bookings( $bk_array );
283 }
284 }
285
286
287 // <editor-fold defaultstate="collapsed" desc=" Notice - if events was imported previously | Already ALL Imported " >
288 do_action( 'wpbc_show_debug', array( 'Check, if events was imported previously. New events: ', $bk_array ) ); // S_Y_S_T_E_M L_O_G
289
290 if ( empty( $bk_array ) ) {
291
292 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
293 , '<strong>' . __( 'Warning', 'booking' ) . '!</strong> '
294 . sprintf( __( 'No any new events to import! These events was import previously, already.', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
295 , 'warning', 3000 );
296 return 0;
297 }
298 // </editor-fold>
299
300
301 ///////////////////////////////////////////////////////////////////// - Skip events that does not fit to filter parameters: FROM - UNTIL
302
303 // Sort Events
304 $bk_array = wpbm_sort_events_by( $bk_array );
305
306 // Filter By Dates - "From - Until"
307 $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
308
309 // Filter events by - "Max"
310 $bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode );
311
312
313 // <editor-fold defaultstate="collapsed" desc=" Notice - Creation of N bookings " >
314 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
315 , sprintf( __( 'Imported of %s bookings', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
316 , 'info', 3000 );
317
318 do_action( 'wpbc_show_debug', array( 'Create bookings after filtering', $bk_array ) ); // S_Y_S_T_E_M L_O_G
319 // </editor-fold>
320
321 ///////////////////////////////////////////////////////////////////// - Loop events > C r e a t e B o o k i n g s
322
323 // Get assigning fields for SUMMARY, DESCRIPTION, LOCATION
324 $assigned_fields_arr = WPBM_create_bookings_from_events::get_assigned_form_fields();
325
326 $booking_added_num = 0;
327
328 foreach ( $bk_array as $ics_event) {
329
330 //FixIn: 2.1.3
331 if (
332 ( 'On' == get_bk_option( 'booking_ics_import_append_checkout_day' ) )
333 || (
334 ( class_exists( 'wpdev_bk_biz_s' ) )
335 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
336 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
337 && ( count( $ics_event[ '_BOOKING_DATES' ] ) == 1 )
338 )
339 ) { //FixIn: 2.0.27.1 //FixIn: 9.5.4.1
340 // Add one additional day to .ics event (useful in some cases for bookings with change-over days), if the imported .ics dates is coming without check in/our times
341 // Later system is adding check in/out times from Booking Calendar to this event
342 $ics_event_check_out = $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ];
343
344 //FixIn Start: 2.0.28.1 ------------------------------------------------------------------------------------
345 // Is check in/out dates in this event it's the same date
346 $test_check_in = substr( $ics_event['_BOOKING_DATES'][0], 0, 10 );
347 $test_check_out = substr( $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ], 0, 10 );
348 if ( count( $ics_event['_BOOKING_DATES'] ) > 1 ) { //FixIn: 2.1.3
349 if ( $test_check_in === $test_check_out ) {
350 // It is the same date, like 2024-02-12 10:00:01, 2024-02-12 14:59:52, then remove this date, because we will add new date
351 unset( $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] );
352 // Important! Reindex array, after unset operation.
353 $ics_event['_BOOKING_DATES'] = array_values( $ics_event['_BOOKING_DATES'] );
354 } else {
355 // It is other date, in this case, we need to define other time:
356 $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] = $test_check_out . ' 00:00:00';
357 }
358 }
359 //FixIn End: 2.0.28.1 --------------------------------------------------------------------------------------
360
361 $ics_event_check_out = strtotime( $ics_event_check_out );
362 $ics_event_check_out = strtotime( '+1 day', $ics_event_check_out );
363 $ics_event_check_out = date_i18n( "Y-m-d H:i:s", $ics_event_check_out ); //FixIn: 2.0.28.1 //$ics_event_check_out = date_i18n( "Y-m-d 00:00:00", $ics_event_check_out );
364 $ics_event['_BOOKING_DATES'][] = $ics_event_check_out;
365
366 if (
367 ( 'On' === get_bk_option( 'booking_ics_import_append_checkout_day' ) )
368 && ( 'On' === get_bk_option( 'booking_ics_import_append_extra_checkout_day' ) )
369 ) {
370 $ics_event['_BOOKING_DATES'] = wpbm_ics_import_append_extra_checkout_day( $ics_event['_BOOKING_DATES'] );
371 }
372 }
373
374 $booking_data = array();
375
376 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
377 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
378 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
379 && ( count( $ics_event[ '_BOOKING_DATES' ] ) > 1 )
380 ) {
381 $dates_formats = array_fill( 0, count( $ics_event[ '_BOOKING_DATES' ] ), "Y-m-d" ); // Array ( [0] => Y-m-d [1] => Y-m-d ... )
382 } else {
383 $dates_formats = array_fill( 0, count( $ics_event[ '_BOOKING_DATES' ] ), "Y-m-d H:i:s" ); //FixIn: 2.0.15.4
384 }
385 $booking_dates_unix = array_map( 'strtotime', $ics_event[ '_BOOKING_DATES' ] ); // Array ( [0] => 1498262400 [1] => 1498348800 )
386 $simple_booking_dates = array_map( 'date_i18n', $dates_formats , $booking_dates_unix ); // Array ( '2017-06-23', '2017-06-24', '2017-06-25', '2017-06-26' )
387
388
389 //Add check in/out times to full day events //FixIn: 8.1.3.29
390 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
391 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
392 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
393 ) { //FixIn: 2.0.5.1
394
395 if ( ( is_array( $simple_booking_dates ) ) && ( count( $simple_booking_dates ) > 1 ) ) { //FixIn: 2.0.10.4
396 $wpbc_check_in = ' ' . get_bk_option( 'booking_range_selection_start_time') . ':01'; // ' 14:00:01'
397 $wpbc_check_out = ' ' . get_bk_option( 'booking_range_selection_end_time') . ':02'; // ' 10:00:02';
398 $simple_booking_dates[0] = $simple_booking_dates[0] . $wpbc_check_in;
399 $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ] = $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ] . $wpbc_check_out;
400 $ics_event['_BOOKING_DATES'][0] = $simple_booking_dates[0];
401 $ics_event['_BOOKING_DATES'][ ( count( $simple_booking_dates ) - 1 ) ] = $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ];
402 }
403 }
404
405
406 WPBM_create_bookings_from_events::set_ics_dates( $ics_event[ '_BOOKING_DATES' ] );
407
408 $booking = array(
409 'dates' => $simple_booking_dates // array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
410 , 'data' => array()
411 , 'resource_id' => $shortcode[ 'resource_id' ]
412 );
413
414
415 $bk_data = array();
416 foreach ( $assigned_fields_arr as $assigned_field ) {
417
418 switch ( $assigned_field[ 'ics_field_name' ] ) {
419
420 case 'title':
421 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_SUMMARY' ] ) );
422 break;
423
424 case 'description':
425 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_DESCRIPTION' ] ) );
426 break;
427
428 case 'where':
429 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_LOCATION' ] ) );
430 break;
431
432 default:
433 break;
434 }
435 }
436
437 // Email.
438 $email = '[email protected]'; //get_option ( 'admin_email' );
439 if ( ! empty( $ics_event['_BOOKING_ATTENDEE'] ) ) {
440 $email = str_replace( 'mailto:', '', $ics_event['_BOOKING_ATTENDEE'] );
441 if ( ! is_email( $email ) ) {
442 $email = '[email protected]';
443 }
444 }
445 $email = apply_filters( 'wpbm_replace_mailto_for__booking_attendee', $email );
446 $bk_data ['email'] = array( 'value' => $email, 'type' => 'email' );
447
448 // Optional Start and End times
449 $start_time = substr( $ics_event[ '_BOOKING_DATES' ][ 0 ], 11 );
450 $end_time = $ics_event[ '_BOOKING_DATES' ][ ( count( $ics_event[ '_BOOKING_DATES' ] ) - 1 ) ];
451 $end_time = substr( $end_time, 11 );
452
453 // Convert times H:i:s to seconds +1 +2 seconds and convert back to H:i -----------------------------------
454 $in_seconds = wpbc_transform__24_hours_his__in__seconds( $start_time );
455 $is_check_in_out__or_full = $in_seconds % 10; // 0, 1, 2
456 if ( 1 === $is_check_in_out__or_full ) {
457 $in_seconds = $in_seconds - 1;
458 }
459 $start_time = wpbc_transform__seconds__in__24_hours_his( $in_seconds );
460 $start_time = substr( $start_time, 0, 5 );
461
462 $in_seconds = wpbc_transform__24_hours_his__in__seconds( $end_time );
463 $is_check_in_out__or_full = $in_seconds % 10; // 0, 1, 2
464 if ( 2 === $is_check_in_out__or_full ) {
465 $in_seconds = $in_seconds + 8;
466 }
467 $end_time = wpbc_transform__seconds__in__24_hours_his( $in_seconds );
468 $end_time = substr( $end_time, 0, 5 );
469 // -------------------------------------------------------------------------------------------------------------
470
471 //Add check in/out times //FixIn: 8.1.3.29
472 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
473 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
474 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
475 ){ //FixIn: 2.0.5.1
476
477 $start_time = get_bk_option( 'booking_range_selection_start_time' ); // ' 14:00'
478 $end_time = get_bk_option( 'booking_range_selection_end_time' ); // ' 10:00'
479 }
480
481
482 if ( ( $start_time !== '00:00' ) || ( $end_time !== '00:00' ) ) {
483 $bk_data [ 'rangetime' ] = array( 'value' => $start_time . ' - ' . $end_time, 'type' => 'select-one' );
484 }
485
486 $booking['data'] = $bk_data;
487
488 $additional_params = array( 'sync_gid' => $ics_event['_BOOKING_UID'], 'is_send_emeils' => 0 ); //FixIn: 2.0.10.2
489
490 /**
491 *
492 *
493 // $ics_event[ '_BOOKING_DATES' ]; // array ( [0] => 2014-09-16 05:00:01 [1] => 2014-09-16 12:00:02 )
494 // $ics_event[ '_BOOKING_SUMMARY' ]; // Event (timezone Pacific GMT-07:00)
495 // $ics_event[ '_BOOKING_DESCRIPTION' ]; // 8/7/2017 1:00pm TO 3:30pm 8/8/2017 (GMT-07:00) Pacific Time
496 // $ics_event[ '_BOOKING_LOCATION' ]; // Pacific Coast Highway, Pacific Coast Hwy, Los Angeles, CA, USA
497 // $ics_event[ '_BOOKING_UID' ]; // [email protected]
498 // $ics_event[ '_BOOKING_MODIFIED' ]; // 2017-06-28 10:12:35
499 */
500
501 add_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10, 5 );
502
503
504 if ( ( function_exists( 'get_bk_option' ) ) && ( get_bk_option( 'booking_auto_approve_bookings_when_import' ) == 'On' ) ) {
505 $additional_params ['is_approve_booking'] = 1;
506 }
507
508
509 // Check dates availability and process only if dates available in specific booking resource!
510 if (
511 ( $shortcode[ 'import_conditions' ] == 'if_dates_free' )
512 || ( 'On' == get_bk_option( 'booking_condition_import_if_available' ) ) //FixIn: 9.8.15.8 - 'Import only, if days are available'
513 ) { //FixIn: 2.0.15.2
514
515 $is_dates_booked = wpbc_api_is_dates_booked( $ics_event['_BOOKING_DATES'], $booking['resource_id']
516 , array(
517 'is_use_booking_recurrent_time' => false // If we import bookings, all the times defined in dates, so no need to use it
518 )
519 );
520 } else{
521 $is_dates_booked = false;
522 $additional_params['save_booking_even_if_unavailable'] = 1;
523 }
524 // $additional_params['is_use_booking_recurrent_time'] = false; // FixIn: 2.1.17.1.
525 $additional_params['is_use_booking_recurrent_time'] = ( 'On' === get_bk_option( 'booking_recurrent_time' ) );
526
527 if ( ! $is_dates_booked ) {
528
529 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ] , $additional_params );
530
531 if ( ( is_int( $booking_id ) ) && ( $booking_id > 0 ) ) {
532
533 $booking_added_num++;
534
535 // Add notes to the booking relative source of imported booking // FixIn: 2.0.7.2
536 $import_url = parse_url( $shortcode[ 'url' ] );
537 if (
538 ( false !== $import_url )
539 && ( ! empty( $import_url[ "host" ]))
540 && ( class_exists('wpdev_bk_personal') )
541 ){
542 $remark_text = '[' . date_i18n( 'Y-m-d H:i:s' ) . '] ' . sprintf( __( 'Imported from %s ', 'booking-manager' ), $import_url["host"] );
543 $remark_text = str_replace( '%', '&#37;', $remark_text );
544 $remark_text = str_replace( array( '"', "'" ), '', $remark_text );
545 $remark_text = trim( $remark_text );
546
547 $is_append = true;
548 make_bk_action( 'wpdev_make_update_of_remark', $booking_id, $remark_text, $is_append );
549 }
550
551 do_action( 'wpbc_show_debug', sprintf ( 'Added new booking ID:<strong>%d</strong> ', $booking_id ) ); // S_Y_S_T_E_M L_O_G
552 }
553
554 if ( is_wp_error( $booking_id ) ) {
555 do_action( 'wpbc_show_debug', implode( ' ', $booking_id->get_error_messages()) );
556
557 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
558 , implode( ' ', $booking_id->get_error_messages())
559 , 'warning', 30000 );
560 }
561
562 } else {
563
564 do_action( 'wpbc_show_debug', // S_Y_S_T_E_M L_O_G
565 sprintf ( 'Event was not create becausse dates %s already booked in booking resource ID = %d'
566 , implode( ', ', $booking[ 'dates' ] ) , $booking[ 'resource_id' ] ) );
567 }
568
569 remove_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10 );
570
571
572 // Remove previously saved dates to our 'Static' class.
573 WPBM_create_bookings_from_events::erase_ics_dates();
574 }
575
576 return $booking_added_num;
577 }
578 add_action( 'wpbm_ics_import_start', 'wpbm_ics_import_start', 10, 1 );
579
580
581 // Legacy Import
582
583 /** Import ICS feed and create bookings .in Booking Calendar
584 *
585 * @param array $attr = array(
586 'url' => ''
587 , 'resource_id' => 1
588 , 'import_conditions' => '' | 'if_dates_free' // maybe rename ??? if_dates_booked_then_not_import
589 )
590 * @return int|bool number of imported bookings or FALSE if error
591 */
592 function wpbm_ics_import_start_legacy( $attr ) {
593
594 // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
595 /////////////////////////////////////////////////////////////////////
596 // Parse / validate parameters
597 /////////////////////////////////////////////////////////////////////
598 $defaults = array(
599 'url' => ''
600 , 'resource_id' => 1
601 , 'delete' => 0
602 , 'import_conditions' => ''
603 , 'from' => 'any' // 'today' // '00:00 today'
604 , 'from_offset' => ''
605 , 'until' => 'any' // 'year-end'
606 , 'until_offset' => ''
607 , 'max' => ''
608 , 'is_all_dates_in' => true // Conditional of Dates checking. TRUE - Remove event if al least 1 day not in conditional interval, FALSE - save event, if at leat one date in conditional interval
609 );
610 $shortcode = array();
611
612 foreach ( $attr as $param_name => $param_value ) {
613
614 switch ( $param_name) { // Validate Params
615
616 case 'url':
617 $shortcode[ $param_name ] = esc_url_raw( $param_value ); // $shortcode[ 'url' ]
618 $shortcode[ $param_name ] = str_replace( '&amp;', '&', $shortcode[ $param_name ] ); //FixIn: 2.0.14.2
619 break;
620
621 case 'import_conditions':
622 $shortcode[ $param_name ] = esc_attr( $param_value ); // $shortcode[ 'resource_id' ]
623 break;
624
625 case 'resource_id':
626 $shortcode[ $param_name ] = intval($param_value ); // $shortcode[ 'resource_id' ]
627 break;
628
629 case 'from': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-start', 'any', 'date' = 2017-08-07
630 $shortcode[ $param_name ] = $param_value;
631 break;
632
633 case 'from_offset': // 5d, 10h, 5m, 30s -- if from: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-start' }
634 $shortcode[ $param_name ] = $param_value;
635 break;
636
637 case 'until': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-end', 'any', 'date' = 2017-08-07
638 $shortcode[ $param_name ] = $param_value;
639 break;
640
641 case 'until_offset': // 5d, 10h, 5m, 30s -- if until: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-end' }
642 $shortcode[ $param_name ] = $param_value;
643 break;
644
645 case 'max':
646 $shortcode[ $param_name ] = intval( $param_value );
647 break;
648
649 case 'is_all_dates_in': // Conditional of Dates checking. TRUE - Remove event if al least 1 day not in conditional interval, FALSE - save event, if at leat one date in conditional interval
650 $shortcode[ $param_name ] = intval( $param_value );
651 break;
652
653
654 default:
655 $shortcode[ $param_name ] = $param_value;
656 break;
657 }
658 }
659 $shortcode = wp_parse_args( $shortcode, $defaults );
660 // </editor-fold>
661
662 //debuge($shortcode);
663 // <editor-fold defaultstate="collapsed" desc=" Notice - Import Parameters | Error No URL" >
664
665 do_action( 'wpbc_show_debug', array( 'Import Parameters' , $shortcode ) ); // S_Y_S_T_E_M L_O_G
666
667 if ( empty( $shortcode[ 'url' ] ) ) {
668 do_action( 'wpbc_admin_show_top_notice', __( 'No ics url feed', 'booking-manager' ), 'error', 5000 ); // N_O_T_I_C_E in H_E_A_D_E_R
669 return false;
670 }
671 // </editor-fold>
672
673 ///////////////////////////////////////////////////////////////////// - Get, Parse ICS Feed
674
675 $ics_array = wpbm_ics_file_to_array( $shortcode[ 'url' ] );
676
677 // <editor-fold defaultstate="collapsed" desc=" Notice - feed contain N events | Error Importing " >
678 do_action( 'wpbc_show_debug', array( 'Imported data' , $ics_array) ); // S_Y_S_T_E_M L_O_G
679
680 // If Error
681 if ( is_wp_error( $ics_array ) ) {
682
683 $error_message = $ics_array->get_error_message();
684 do_action( 'wpbc_admin_show_top_notice', $error_message, 'error', 5000 ); // N_O_T_I_C_E in H_E_A_D_E_R
685 return false;
686 }
687
688 //FixIn: 2.0.7.3
689 $ics_array_events_num = 0;
690 if ( ( ! empty( $ics_array ) ) && ( ! empty( $ics_array[ 'events' ] ) ) && ( is_array( $ics_array[ 'events' ] ) ) ) {
691 $ics_array_events_num = count( $ics_array[ 'events' ] );
692 }
693 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
694 , sprintf ( __( '.ics feed contain %s events at URL %s', 'booking-manager' ), '<b>' . $ics_array_events_num . '</b>', '<b><a href="'. $shortcode[ 'url' ] .'">' . $shortcode[ 'url' ] . '</a></b>' )
695 , 'info', 5000 );
696 // </editor-fold>
697
698 //FixIn: 2.0.18.3
699 if ( 'skip' != $shortcode['delete'] ) {
700 wpbm_delete_all_imported_bookings( array( 'resource_id' => $shortcode['resource_id'] ) ); //FixIn: 2.0.10.3
701 }
702
703 $bk_array = array();
704 // Get Only '_BOOKING...' field from ICS array
705 if ( $ics_array !== false ) {
706 $bk_array = wpbm_get_booking_fields_from_ics_array( $ics_array[ 'events' ] );
707 }
708
709 do_action( 'wpbc_show_debug', array( 'Imported Events', $bk_array ) ); // S_Y_S_T_E_M L_O_G
710
711
712 //FixIn: 2.0.6.1 - Set timezone frrom Booking > Settings > Sync page for booking listing shortcode
713 $tzid = get_bk_option( 'booking_gcal_timezone' );
714 if ( ! empty( $tzid ) ) {
715 foreach ( $bk_array as $bk_k => $bk_ics_data_arr ) {
716
717
718 //apply our timezone from the Booking > Settings > Search page
719 foreach ( $bk_array[ $bk_k ]['_BOOKING_DATES'] as $dk => $day ) {
720 //$bk_array[ $bk_k ][ '_BOOKING_DATES' ][ $dk ] = ZDateHelper::toLocalDateTime( get_gmt_from_date( $day ), $tzid );
721
722 //FixIn: 2.0.7.4 - Skip adding Timezone to "middle" days, if days start with time 00:00:00 - its means event for full day, not the start/end time
723 // Apply timezone only to fist and last days in a list, if the change over days activated in the Booking Calendar
724 $ics_time_in_day = substr( $day, -8);
725 if ( '00:00:00' == $ics_time_in_day ) {
726 continue;
727 }
728 //FixIn: 2.0.7.4 - Skip adding Timezone to "middle" days, if Booking Calendar use change over days, to prevent of having clock icon in middle days.
729 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
730 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
731 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
732 ) { //FixIn: 2.0.5.1
733 $days_number = count( $bk_array[ $bk_k ]['_BOOKING_DATES'] );
734 if ( ( 0 != $dk ) && ( ( $days_number - 1) != $dk ) ) {
735 continue;
736 }
737 // We are append one day to the booking, so skip this day for timezone, as well
738 if ( ( get_bk_option( 'booking_ics_import_append_checkout_day' ) !== 'Off' ) && ( ( $days_number - 1) == $dk ) ) {
739 continue;
740 }
741 }
742
743 $bk_array[ $bk_k ]['_BOOKING_DATES'][ $dk ] = ZDateHelper::toLocalDateTime( $day, $tzid );
744 }
745 }
746 }
747
748 ///////////////////////////////////////////////////////////////////// - Check if these EVENTS was imported before or NOT
749
750 $booking_ics_force_import = get_bk_option( 'booking_ics_force_import' ); //FixIn: 2.0.10.1
751 $booking_condition_import_only_new = get_bk_option( 'booking_condition_import_only_new' ); //FixIn: 9.8.15.8
752 if ( false === $booking_condition_import_only_new ) {
753 // OLD:
754 if ( 'On' !== $booking_ics_force_import ) {
755 $bk_array = wpbm_clear_events_from_exist_bookings( $bk_array );
756 }
757 } else {
758 // NEW:
759 if ( 'On' === $booking_condition_import_only_new ) {
760 $bk_array = wpbm_clear_events_from_exist_bookings( $bk_array );
761 }
762 }
763
764 // <editor-fold defaultstate="collapsed" desc=" Notice - if events was imported previously | Already ALL Imported " >
765 do_action( 'wpbc_show_debug', array( 'Check, if events was imported previously. New events: ', $bk_array ) ); // S_Y_S_T_E_M L_O_G
766
767 if ( empty( $bk_array ) ) {
768
769 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
770 , '<strong>' . __( 'Warning', 'booking' ) . '!</strong> '
771 . sprintf( __( 'No any new events to import! These events was import previously, already.', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
772 , 'warning', 3000 );
773 return 0;
774 }
775 // </editor-fold>
776
777
778 ///////////////////////////////////////////////////////////////////// - Skip events that does not fit to filter parameters: FROM - UNTIL
779
780 // $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
781
782
783 // Sort Events
784 $bk_array = wpbm_sort_events_by( $bk_array );
785
786 // Filter By Dates - "From - Until"
787 $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
788
789 // Filter events by - "Max"
790 $bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode );
791
792
793 // <editor-fold defaultstate="collapsed" desc=" Notice - Creation of N bookings " >
794 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
795 , sprintf( __( 'Imported of %s bookings', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
796 , 'info', 3000 );
797
798 do_action( 'wpbc_show_debug', array( 'Create bookings after filtering', $bk_array ) ); // S_Y_S_T_E_M L_O_G
799 // </editor-fold>
800
801 ///////////////////////////////////////////////////////////////////// - Loop events > C r e a t e B o o k i n g s
802
803 // Get assigning fields for SUMMARY, DESCRIPTION, LOCATION
804 $assigned_fields_arr = WPBM_create_bookings_from_events::get_assigned_form_fields();
805
806 $booking_added_num = 0;
807
808 foreach ( $bk_array as $ics_event) {
809
810 //FixIn: 2.0.5.2
811 //FixIn: 8.1.3.29
812 // if ( ( class_exists( 'wpdev_bk_biz_s' ) )
813 // && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
814 // && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
815 // && ( get_bk_option( 'booking_ics_import_append_checkout_day' ) !== 'Off' )
816 // ) {
817 if ( 'On' == get_bk_option( 'booking_ics_import_append_checkout_day' ) ) { //FixIn: 2.0.27.1 //FixIn: 9.5.4.1
818 // Add one additional day to .ics event (useful in some cases for bookings with change-over days), if the imported .ics dates is coming without check in/our times
819 // Later system is adding check in/out times from Booking Calendar to this event
820 $ics_event_check_out = $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ];
821
822 //FixIn Start: 2.0.28.1 ------------------------------------------------------------------------------------
823 // Is check in/out dates in this event it's the same date
824 $test_check_in = substr( $ics_event['_BOOKING_DATES'][0], 0, 10 );
825 $test_check_out = substr( $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ], 0, 10 );
826 if ( $test_check_in === $test_check_out ) {
827 // It is the same date, like 2024-02-12 10:00:01, 2024-02-12 14:59:52, then remove this date, because we will add new date
828 unset( $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] );
829 // Important! Reindex array, after unset operation.
830 $ics_event['_BOOKING_DATES'] = array_values( $ics_event['_BOOKING_DATES'] );
831 } else {
832 // It is other date, in this case, we need to define other time:
833 $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] = $test_check_out . ' 00:00:00';
834 }
835 //FixIn End: 2.0.28.1 --------------------------------------------------------------------------------------
836
837 $ics_event_check_out = strtotime( $ics_event_check_out );
838 $ics_event_check_out = strtotime( '+1 day', $ics_event_check_out );
839 $ics_event_check_out = date_i18n( "Y-m-d H:i:s", $ics_event_check_out ); //FixIn: 2.0.28.1 //$ics_event_check_out = date_i18n( "Y-m-d 00:00:00", $ics_event_check_out );
840 $ics_event['_BOOKING_DATES'][] = $ics_event_check_out;
841
842 if (
843 ( 'On' === get_bk_option( 'booking_ics_import_append_checkout_day' ) )
844 && ( 'On' === get_bk_option( 'booking_ics_import_append_extra_checkout_day' ) )
845 ) {
846 $ics_event['_BOOKING_DATES'] = wpbm_ics_import_append_extra_checkout_day( $ics_event['_BOOKING_DATES'] );
847 }
848 }
849
850 $booking_data = array();
851
852 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
853 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
854 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
855 && ( count( $ics_event[ '_BOOKING_DATES' ] ) > 1 )
856 ) {
857 $dates_formats = array_fill( 0, count( $ics_event[ '_BOOKING_DATES' ] ), "Y-m-d" ); // Array ( [0] => Y-m-d [1] => Y-m-d ... )
858 } else {
859 $dates_formats = array_fill( 0, count( $ics_event[ '_BOOKING_DATES' ] ), "Y-m-d H:i:s" ); //FixIn: 2.0.15.4
860 }
861 $booking_dates_unix = array_map( 'strtotime', $ics_event[ '_BOOKING_DATES' ] ); // Array ( [0] => 1498262400 [1] => 1498348800 )
862 $simple_booking_dates = array_map( 'date_i18n', $dates_formats , $booking_dates_unix ); // Array ( '2017-06-23', '2017-06-24', '2017-06-25', '2017-06-26' )
863
864
865 //FixIn: 8.1.3.29
866 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
867 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
868 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
869 ) { //FixIn: 2.0.5.1
870 //Add check in/out times to full day events
871 if ( ( is_array( $simple_booking_dates ) ) && ( count( $simple_booking_dates ) > 1 ) ) { //FixIn: 2.0.10.4
872 $wpbc_check_in = ' ' . get_bk_option( 'booking_range_selection_start_time') . ':01'; // ' 14:00:01'
873 $wpbc_check_out = ' ' . get_bk_option( 'booking_range_selection_end_time') . ':02'; // ' 10:00:02';
874 $simple_booking_dates[0] = $simple_booking_dates[0] . $wpbc_check_in;
875 $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ] = $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ] . $wpbc_check_out;
876 $ics_event['_BOOKING_DATES'][0] = $simple_booking_dates[0];
877 $ics_event['_BOOKING_DATES'][ ( count( $simple_booking_dates ) - 1 ) ] = $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ];
878 }
879 }
880
881 //debuge($ics_event[ '_BOOKING_DATES' ]);die;
882 // Tempoary save our dates
883
884 // if( count( $ics_event['_BOOKING_DATES'] ) > 2 ) {
885 // unset( $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] ); //Remove last imported date
886 // if ( count( $ics_event['_BOOKING_DATES'] ) > 2 ) { // Add one date if number of days in booking more than 2
887 // $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] = $simple_booking_dates[ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] . $wpbc_check_out;
888 // } else if ( count( $ics_event['_BOOKING_DATES'] ) == 1 ) { // Add one date, if we are having only 1 day in booking (previously was having 2 dates
889 // $ics_event['_BOOKING_DATES'][] = $simple_booking_dates[ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] . $wpbc_check_out;
890 // }
891 // }
892 WPBM_create_bookings_from_events::set_ics_dates( $ics_event[ '_BOOKING_DATES' ] );
893
894 $booking = array(
895 'dates' => $simple_booking_dates // array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
896 , 'data' => array()
897 , 'resource_id' => $shortcode[ 'resource_id' ]
898 );
899
900
901 $bk_data = array();
902 foreach ( $assigned_fields_arr as $assigned_field ) {
903
904 switch ( $assigned_field[ 'ics_field_name' ] ) {
905
906 case 'title':
907 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_SUMMARY' ] ) );
908 break;
909
910 case 'description':
911 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_DESCRIPTION' ] ) );
912 break;
913
914 case 'where':
915 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_LOCATION' ] ) );
916 break;
917
918 default:
919 break;
920 }
921 }
922
923 // Email
924 $home_url = explode( '://', home_url() );
925 //if (count($home_url>0)) //FixIn: 2.0.3.2
926 // $email = 'ics@' . $home_url[1];
927 //else
928 $email = get_option ( 'admin_email' );
929 $bk_data [ 'email' ] = array( 'value' => $email, 'type' => 'email' );
930
931 // Optional Start and End times
932 $start_time = substr( $ics_event[ '_BOOKING_DATES' ][ 0 ], 11, 5 );
933 $end_time = $ics_event[ '_BOOKING_DATES' ][ ( count( $ics_event[ '_BOOKING_DATES' ] ) - 1 ) ];
934 $end_time = substr( $end_time, 11, 5 );
935
936 //FixIn: 8.1.3.29
937 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
938 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
939 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
940 ){ //FixIn: 2.0.5.1
941 //Add check in/out times
942 $start_time = get_bk_option( 'booking_range_selection_start_time' ); // ' 14:00'
943 $end_time = get_bk_option( 'booking_range_selection_end_time' ); // ' 10:00'
944 }
945
946
947 if ( ( $start_time !== '00:00' ) || ( $end_time !== '00:00' ) ) {
948 $bk_data [ 'rangetime' ] = array( 'value' => $start_time . ' - ' . $end_time, 'type' => 'select-one' );
949 }
950
951 $booking['data'] = $bk_data;
952
953 $additional_params = array( 'sync_gid' => $ics_event['_BOOKING_UID'], 'is_send_emeils' => 0 ); //FixIn: 2.0.10.2
954
955 /**
956 // $ics_event[ '_BOOKING_DATES' ]; // array ( [0] => 2014-09-16 05:00:01 [1] => 2014-09-16 12:00:02 )
957 // $ics_event[ '_BOOKING_SUMMARY' ]; // Event (timezone Pacific GMT-07:00)
958 // $ics_event[ '_BOOKING_DESCRIPTION' ]; // 8/7/2017 1:00pm TO 3:30pm 8/8/2017 (GMT-07:00) Pacific Time
959 // $ics_event[ '_BOOKING_LOCATION' ]; // Pacific Coast Highway, Pacific Coast Hwy, Los Angeles, CA, USA
960 // $ics_event[ '_BOOKING_UID' ]; // [email protected]
961 // $ics_event[ '_BOOKING_MODIFIED' ]; // 2017-06-28 10:12:35
962 */
963
964 add_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10, 5 );
965
966 // Do not overupdate "child booking resources" when saving bookings to parent booking resource. Just skip this checking
967 add_filter( 'wpbc_is_reupdate_dates_to_child_resources', 'wpbm_is_reupdate_dates_to_child_resources', 10, 7 );
968
969 // Check dates availability and process
970 // only if dates available in specific booking resource!
971 if (
972 ( $shortcode[ 'import_conditions' ] == 'if_dates_free' )
973 || ( 'On' == get_bk_option( 'booking_condition_import_if_available' ) ) //FixIn: 9.8.15.8 - 'Import only, if days are available'
974 ){
975 $is_dates_booked = wpbc_api_is_dates_booked( $ics_event['_BOOKING_DATES'], $booking['resource_id'] ); //FixIn: 2.0.15.2
976 //$is_dates_booked = wpbc_api_is_dates_booked( $booking['dates'], $booking['resource_id'] );
977 } else{
978 $is_dates_booked = false;
979 }
980
981
982 if ( ! $is_dates_booked ) {
983
984 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ] , $additional_params );
985 $booking_added_num++;
986
987 //if ( ( defined( 'WP_BK_AUTO_APPROVE_WHEN_IMPORT_GCAL' ) ) && ( WP_BK_AUTO_APPROVE_WHEN_IMPORT_GCAL ) ){ // Auto approve booking if imported
988 if ( ( function_exists( 'get_bk_option' )) && ( get_bk_option( 'booking_auto_approve_bookings_when_import' ) == 'On' ) ) { //FixIn: 8.1.3.27
989 // Auto approve booking, when imported. //FixIn:7.0.1.59
990 global $wpdb;
991 if ( false === $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}bookingdates SET approved = %s WHERE booking_id IN ({$booking_id})", '1' ) ) ){
992 ?> <script type="text/javascript">
993 var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error('Error during updating to DB' ,__FILE__,__LINE__) ),ENT_QUOTES) ; ?>';
994 wpbc_admin_show_message( my_message, 'error', 30000 );
995 </script> <?php
996 die();
997 }
998 }
999
1000 //FixIn: 2.0.7.2 - add notes to the booking relative source of imported booking
1001 $import_url = parse_url( $shortcode[ 'url' ] );
1002 if ( ( false !== $import_url ) && ( ! empty( $import_url[ "host" ])) && ( class_exists('wpdev_bk_personal') ) ) {
1003
1004 $remark_text = str_replace('%','&#37;', '[' . date_i18n('Y-m-d H:i:s') . '] ' . sprintf( __( 'Imported from %s ', 'booking-manager'), $import_url[ "host" ] ) );
1005 $my_remark = str_replace( array( '"', "'" ),'',$remark_text);
1006 $my_remark = trim( $my_remark );
1007
1008 global $wpdb;
1009
1010 $update_sql = $wpdb->prepare( "UPDATE {$wpdb->prefix}booking AS bk SET bk.remark= %s WHERE bk.booking_id= %d ", $remark_text, $booking_id );
1011 if ( false === $wpdb->query( $update_sql ) ) {
1012 ?> <script type="text/javascript">
1013 var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error('Error during updating notes in DB' ,__FILE__,__LINE__) ),ENT_QUOTES) ; ?>';
1014 wpbc_admin_show_message( my_message, 'error', 30000 );
1015 </script> <?php
1016 die();
1017 }
1018 }
1019
1020 do_action( 'wpbc_show_debug', sprintf ( 'Added new booking ID:<strong>%d</strong> ', $booking_id ) ); // S_Y_S_T_E_M L_O_G
1021 } else {
1022
1023 do_action( 'wpbc_show_debug', // S_Y_S_T_E_M L_O_G
1024 sprintf ( 'Event was not create becausse dates %s already booked in booking resource ID = %d'
1025 , implode( ', ', $booking[ 'dates' ] ) , $booking[ 'resource_id' ] ) );
1026 }
1027
1028 remove_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10 );
1029 remove_filter( 'wpbc_is_reupdate_dates_to_child_resources', 'wpbm_is_reupdate_dates_to_child_resources', 10 );
1030
1031 // Remove previously saved dates to our 'Static' class.
1032 WPBM_create_bookings_from_events::erase_ics_dates();
1033 }
1034
1035 return $booking_added_num;
1036 }
1037
1038
1039
1040 ////////////////////////////////////////////////////////////////////////////////////////////
1041 // Booking Calendar support functions
1042 ////////////////////////////////////////////////////////////////////////////////////////////
1043
1044
1045 /** Clear array of Events from events that already exist in Booking table
1046 * Check UID and GUID sync paramaters
1047 *
1048 * @param array $bk_array - array of events
1049 * @return array - trimmed array
1050 */
1051 function wpbm_clear_events_from_exist_bookings( $bk_array ) {
1052
1053 $bk_uid = $bk_guid = array();
1054 // GET array of UID for imported bookings
1055 foreach ( $bk_array as $ics_key => $ics_event) {
1056
1057 $bk_uid[ $ics_key ] = $ics_event[ '_BOOKING_UID' ];
1058
1059 if ( strpos( $ics_event[ '_BOOKING_UID' ], '@google.com') !== false ) {
1060 // [email protected] - ID of event from ICS
1061 // 15ig8t0i739kajgjc8ekc386dt_20170815 - ID of event during import from Google Calendar (probabaly created by ID before @ + first date)
1062
1063 $bk_guid[ $ics_key ] = str_replace( '@google.com'
1064 , date_i18n( '_Ymd', strtotime( $ics_event[ '_BOOKING_DATES' ][0] ) )
1065 , $ics_event[ '_BOOKING_UID' ]
1066 );
1067 }
1068 }
1069 $bookings_check_uid = array_merge( $bk_uid,$bk_guid );
1070
1071 $bookings_exit_uid = wpbm_get_exist_bookings_gid( $bookings_check_uid );
1072
1073 // Remove events which already exist
1074 foreach ( $bk_uid as $ics_key => $ics_uid ) {
1075
1076 // If exist UID remove it from event
1077 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
1078 unset( $bk_array[ $ics_key ] );
1079 }
1080
1081 // If we are having such GUID and it exist, then remove ir
1082 if ( isset( $bk_guid[ $ics_key ] ) ) {
1083 $ics_gid = $bk_guid[ $ics_key ];
1084 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
1085 unset( $bk_array[ $ics_key ] );
1086 }
1087 }
1088 }
1089 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1090
1091 return $bk_array;
1092 }
1093
1094
1095 /**
1096 * Return existing non-trashed Booking Calendar synchronization UIDs.
1097 *
1098 * Event UIDs originate in remote iCalendar content. Each UID is therefore
1099 * bound through a separate SQL placeholder instead of being interpolated into
1100 * the dynamic IN clause.
1101 *
1102 * @global wpdb $wpdb WordPress database abstraction object.
1103 *
1104 * @param array $uid_arr Event UIDs to check.
1105 *
1106 * @return array Existing synchronization UIDs.
1107 */
1108 function wpbm_get_exist_bookings_gid( $uid_arr ) {
1109 if ( empty( $uid_arr ) || ! is_array( $uid_arr ) ) {
1110 return array();
1111 }
1112
1113 $uid_values = array();
1114 foreach ( $uid_arr as $uid_value ) {
1115 if ( ! is_scalar( $uid_value ) ) {
1116 continue;
1117 }
1118
1119 $uid_values[] = (string) $uid_value;
1120 }
1121
1122 if ( empty( $uid_values ) ) {
1123 return array();
1124 }
1125
1126 global $wpdb;
1127
1128 $uid_placeholders = implode( ', ', array_fill( 0, count( $uid_values ), '%s' ) );
1129 $query_values = array_merge( $uid_values, array( 1 ) );
1130
1131 // The placeholder list contains only fixed %s tokens; the table name uses WordPress's trusted prefix.
1132 $sql = $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1133 "SELECT sync_gid FROM {$wpdb->prefix}booking WHERE sync_gid IN ({$uid_placeholders}) AND trash != %d",
1134 $query_values
1135 );
1136
1137 if ( ! is_string( $sql ) ) {
1138 return array();
1139 }
1140
1141 $existing_booking_uids = $wpdb->get_col( $sql );
1142
1143 return is_array( $existing_booking_uids ) ? $existing_booking_uids : array();
1144 }
1145
1146
1147 /** Trim number of events in array
1148 *
1149 * @param array $bk_array
1150 * @param array $shortcode
1151 * @return array
1152 */
1153 function wpbm_clear_events_by_count( $bk_array, $shortcode ) {
1154
1155 if ( empty( $shortcode['max'] ) ) {
1156 return $bk_array;
1157 } else {
1158 $max = intval( $shortcode['max'] );
1159 }
1160
1161 $bk_count= count( $bk_array );
1162 if ( $max > $bk_count )
1163 return $bk_array;
1164
1165 $cnt = 0;
1166 $events_arr = array();
1167
1168 foreach ( $bk_array as $ev_key => $ev_arr ) {
1169
1170 $events_arr[] = $ev_arr;
1171
1172 $cnt++;
1173 if ( $cnt >= $max )
1174 break;
1175 }
1176
1177 return $events_arr;
1178 }
1179
1180
1181
1182
1183 /** Remove ONLY dates from event(s) that does not fit to filter parameters: FROM - UNTIL -- All events will exist here
1184 *
1185 * @param array $bk_array - array of events
1186 * @param array $shortcode
1187 * @return array - trimmed array
1188 */
1189 function wpbm_remove_dates_from_event_not_in_condition( $bk_array, $shortcode ) {
1190
1191 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
1192 return $bk_array;
1193 }
1194
1195 // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
1196
1197 // F R O M
1198 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'from_offset' ] );
1199
1200 if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
1201 if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
1202
1203 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
1204
1205 // </editor-fold>
1206
1207
1208 // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
1209
1210 // U N T I L
1211 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'until_offset' ] );
1212
1213 if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
1214 if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
1215
1216 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
1217
1218 // </editor-fold>
1219
1220
1221 foreach ( $bk_array as $e_key => $ics_event ) {
1222
1223 $new_dates = array();
1224 //debuge($ics_event[ '_BOOKING_DATES' ] );
1225 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
1226
1227 $ics_date_unix = strtotime( $ics_date );
1228 //debuge( $d_key, $ics_date , array( $condition_from, $condition_until ) );
1229 if ( ( $condition_from <= $ics_date_unix ) && ( $condition_until >= $ics_date_unix ) ) {
1230
1231 $new_dates[] = $ics_date;
1232 }
1233 }
1234 //debuge( $new_dates ); die;
1235 $bk_array[ $e_key ][ '_BOOKING_DATES' ] = $new_dates;
1236 }
1237
1238 return $bk_array;
1239 }
1240
1241
1242 /** Skip events that does not fit to filter parameters: FROM - UNTIL
1243 *
1244 * @param array $bk_array - array of events
1245 * @param array $shortcode
1246 * @return array - trimmed array
1247 */
1248 function wpbm_clear_events_by_dates( $bk_array, $shortcode ) {
1249
1250 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
1251 return $bk_array;
1252 }
1253
1254 // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
1255
1256 // F R O M
1257 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'from_offset' ] );
1258
1259 if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
1260 if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
1261
1262 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
1263
1264 // </editor-fold>
1265
1266
1267 // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
1268
1269 // U N T I L
1270 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'until_offset' ] );
1271
1272 if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
1273 if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
1274
1275 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
1276
1277 // </editor-fold>
1278
1279 // Conditional of Dates checking. TRUE - Remove event if al least 1 day not in conditional interval, FALSE - save event, if at leat one date in conditional interval
1280 if ( ! isset( $shortcode['is_all_dates_in'] ) )
1281 $is_all_dates_in_condition = true;
1282 else
1283 $is_all_dates_in_condition = (bool) $shortcode['is_all_dates_in'];
1284
1285 //debuge($condition_from, date_i18n('Y-m-d H:is',$condition_from));
1286 //debuge($condition_until, date_i18n('Y-m-d H:is',$condition_until));
1287 //debuge( (int) $is_all_dates_in_condition );
1288 //die;
1289 // Remove some events, that does not inside From | End time
1290 $remove_events_keys = array();
1291 foreach ( $bk_array as $e_key => $ics_event ) {
1292 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
1293 $ics_date = strtotime( $ics_date );
1294
1295 if ( $is_all_dates_in_condition ) {
1296 // STRICT - ALL DATES
1297 if ( ( $condition_from > $ics_date ) || ($condition_until < $ics_date ) ) {
1298
1299 $remove_events_keys[] = $e_key;
1300 continue;
1301 }
1302 } else {
1303 // AT LEAST 1 DATE
1304 if ( ( $condition_from <= $ics_date ) && ( $condition_until >= $ics_date ) ) {
1305
1306 $remove_events_keys = array_diff( $remove_events_keys, array( $e_key ) ); // Remove values "$e_key" from array
1307 break;
1308 } else {
1309 if ( ! in_array( $e_key, $remove_events_keys ) ) {
1310 $remove_events_keys[] = $e_key;
1311 }
1312 }
1313
1314 }
1315
1316 }
1317 }
1318
1319 // Remove them
1320 $remove_events_keys = array_unique( $remove_events_keys );
1321
1322 //debuge( $bk_array, $remove_events_keys);
1323 foreach ( $remove_events_keys as $evnt_key ) {
1324 unset( $bk_array[ $evnt_key ] );
1325 }
1326
1327 return $bk_array;
1328 }
1329
1330
1331 /** Get time offset in seconds based on parameter
1332 *
1333 * @param string $offset_param 30s | 5m | 2h | 7d | just seconds
1334 * @return int
1335 */
1336 function wpbm_get_offset_unix_from_param( $offset_param ) {
1337
1338 $offset = 0;
1339 if ( ! empty( $offset_param ) ) {
1340
1341 $offset_type = substr( $offset_param, -1 );
1342 $offset_value = substr( $offset_param, 0, -1 );
1343
1344 switch ( $offset_type ) {
1345 case "s": // Seconds
1346 $offset = intval( $offset_value );
1347 break;
1348 case "m": // Minutes
1349 $offset = intval( $offset_value ) * 60 ;
1350 break;
1351 case "h": // Hours
1352 $offset = intval( $offset_value ) * 3600 ;
1353 break;
1354 case "d": // Days
1355 $offset = intval( $offset_value ) * 86400 ;
1356 break;
1357 default:
1358 $offset = intval( $offset_value );
1359 }
1360 }
1361 return $offset;
1362 }
1363
1364
1365 /** Get Unix Time based on date parameter / condition and offset in seconds
1366 *
1367 * @param string $check_day - date type: // 'now' | 'today' | 'week' == 'week-start' | 'week-end' | 'month-start' | 'month-end' | 'year-start' | 'year-end' | 'any' == 'any-end' | 'any-start' | '2017-08-07'
1368 * @param int $offset_unix - offset in seconds
1369 * @return int - Unix time in seconds
1370 */
1371 function wpbm_get_time_unix_from_param_offset( $check_day, $offset_unix ) {
1372
1373 // $condition_end = strtotime ( $shortcode['until'] . ' +1 day - 1 second' );
1374
1375 $check_sql_day = explode( '-', $check_day );
1376 if ( count( $check_sql_day ) == 3 ) {
1377 $check_day = 'date';
1378 }
1379
1380 switch ( $check_day ) {
1381 // Don't just use time() for 'now', as this will effectively make cache duration 1 second.
1382 // Instead set to previous minute. Events in Google Calendar cannot be set to precision of seconds anyway
1383 case 'now':
1384
1385 //$time_unix = strtotime( date_i18n( 'Y-m-d H:i:s' ) ) + $offset_unix; // "Now" in "Timezone" from WordPress > Settings
1386 $time_unix = mktime( date_i18n( 'H' ), date_i18n( 'i' ), 0, date_i18n( 'm' ), date_i18n( 'j' ), date_i18n( 'Y' ) ) + $offset_unix ;
1387 break;
1388 case 'today':
1389 //$time_unix = strtotime( date_i18n( 'Y-m-d 00:00:00' ) ) + $offset_unix; // "Today 00:00" in "Timezone" from WordPress > Settings
1390 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), date_i18n( 'j' ), date_i18n( 'Y' ) ) + $offset_unix ;
1391 break;
1392 case 'week':
1393 case 'week-start':
1394
1395 $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
1396 if ( empty( $start_of_week ) )
1397 $start_of_week = 0;
1398
1399 $start_day = date_i18n( 'w' ) - $start_of_week ;
1400 if ( $start_day < 0 )
1401 $start_day = 7 + $start_day;
1402
1403 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), ( date_i18n( 'j' ) - $start_day ), date_i18n( 'Y' ) ) + $offset_unix ;
1404 break;
1405 case 'week-end':
1406
1407 $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
1408 if ( empty( $start_of_week ) )
1409 $start_of_week = 0;
1410
1411 $start_day = date_i18n( 'w' ) - $start_of_week ;
1412 if ( $start_day < 0 )
1413 $start_day = 7 + $start_day;
1414 // minus 1 second -- prevent of events exactly == start Next period
1415 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), ( date_i18n( 'j' ) - $start_day + 7 ), date_i18n( 'Y' ) ) + $offset_unix - 1;
1416 break;
1417 case 'month-start':
1418 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), 1, date_i18n( 'Y' ) ) + $offset_unix ;
1419 break;
1420 case 'month-end':
1421 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ) + 1, 1, date_i18n( 'Y' ) ) + $offset_unix - 1; // minus 1 second -- prevent of events exactly == start Next period
1422 break;
1423 case 'year-start':
1424 $time_unix = mktime( 0, 0, 0, 1, 1, date_i18n( 'Y' ) ) + $offset_unix ;
1425 break;
1426 case 'year-end':
1427 $time_unix = mktime( 0, 0, 0, 1, 1, date_i18n( 'Y' ) + 1 ) + $offset_unix - 1; // minus 1 second -- prevent of events exactly == start Next period
1428 break;
1429 case 'date':
1430
1431 if ( intval( $check_sql_day[0] ) - intval( date('Y') ) > 15 ) { //FixIn m.2.0.1
1432 $time_unix =2145916800;
1433 } else {
1434 $time_unix = mktime( 0, 0, 0, intval( $check_sql_day[1] ), intval( $check_sql_day[2] ), intval( $check_sql_day[0] ) );
1435 }
1436 break;
1437 case 'any-start':
1438 $time_unix = 0; // any - 1970-01-01 00:00
1439 break;
1440 case 'any-end':
1441 $time_unix = 2145916800; //any - 2038-01-01 00:00
1442 break;
1443 default:
1444 $time_unix = 2145916800; //any END - 2038-01-01 00:00
1445 }
1446
1447 //debuge($time_unix, date_i18n('Y-m-d H:i:s (D)',$time_unix));
1448
1449
1450 return $time_unix;
1451 }
1452
1453
1454 //FixIn: 2.0.11.4
1455 /**
1456 * Get booking ID, by searching in Booking Calendar by UID
1457 * @param string $uid
1458 *
1459 * @return empty string | int $booking_id
1460 */
1461 function wpbm_get_booking_id_by_UID( $uid ){
1462
1463 $booking_id = array(); //FixIn: 2.0.14.1
1464
1465 if ( function_exists( 'wpbc_api_get_bookings_arr' ) ) {
1466
1467 /*
1468 $param = array(
1469 'wh_booking_datenext' => 1,
1470 'wh_booking_dateprior' => 1,
1471 'wh_booking_date' => 3,
1472 'wh_trash' => 'any',
1473 'wh_modification_dateprior' => 1,
1474 'wh_modification_date' => 3,
1475 'wh_pay_status' => 'all',
1476
1477 'wh_booking_type' => '-999',
1478 'wh_keyword' => $uid
1479 );
1480
1481 $bookings_arr = wpbc_api_get_bookings_arr( $param );
1482 */
1483 global $wpdb;
1484
1485 $sql = "SELECT booking_id FROM {$wpdb->prefix}booking as bk WHERE bk.sync_gid LIKE '%%" . wpbc_clean_like_string_for_db( $uid ) . "%%'";
1486
1487 //$sql_escaped = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}booking as bk WHERE bk.sync_gid LIKE '%%%s%%'", $uid );
1488
1489 $res = $wpdb->get_results( $sql );
1490
1491 foreach ( $res as $booking_obj ) {
1492 $booking_id[] = $booking_obj->booking_id;
1493 }
1494
1495 //debuge('$bookings_arr',$res);
1496
1497 }
1498
1499 return implode( ',', $booking_id );
1500 }
1501