PluginProbe
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar / 2.0.12
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar v2.0.12
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.0.12, at core/wpbc/wpbm-bc-import.php

903 lines 36.6 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 ( 'On' !== $booking_ics_force_trash_before_import ) {
26 return false;
27 }
28
29
30 global $wpdb;
31
32 $resource_id = $params['resource_id'];
33
34 $is_trash = 1;
35
36 $my_sql = "UPDATE {$wpdb->prefix}booking AS bk SET bk.trash = {$is_trash} WHERE sync_gid != '' AND trash != 1 AND booking_type = {$resource_id}";
37
38 if ( false === $wpdb->query( $my_sql ) ){
39 ?> <script type="text/javascript">
40 var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error('Error during trash booking in DB' ,__FILE__,__LINE__) ),ENT_QUOTES) ; ?>';
41 wpbc_admin_show_message( my_message, 'error', 30000 );
42 </script> <?php
43
44 return false;
45 }
46
47 return true;
48 }
49
50 ////////////////////////////////////////////////////////////////////////////////////////////
51 // I M P O R T
52 ////////////////////////////////////////////////////////////////////////////////////////////
53
54 /** Import ICS feed and create bookings .in Booking Calendar
55 *
56 * @param array $attr = array(
57 'url' => ''
58 , 'resource_id' => 1
59 , 'import_conditions' => '' | 'if_dates_free' // maybe rename ??? if_dates_booked_then_not_import
60 )
61 * @return int|bool number of imported bookings or FALSE if error
62 */
63 function wpbm_ics_import_start( $attr ) {
64
65 // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
66 /////////////////////////////////////////////////////////////////////
67 // Parse / validate parameters
68 /////////////////////////////////////////////////////////////////////
69 $defaults = array(
70 'url' => ''
71 , 'resource_id' => 1
72 , 'import_conditions' => ''
73 , 'from' => 'any' // 'today' // '00:00 today'
74 , 'from_offset' => ''
75 , 'until' => 'any' // 'year-end'
76 , 'until_offset' => ''
77 , 'max' => ''
78 , '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
79 );
80 $shortcode = array();
81
82 foreach ( $attr as $param_name => $param_value ) {
83
84 switch ( $param_name) { // Validate Params
85
86 case 'url':
87 $shortcode[ $param_name ] = esc_url_raw( $param_value ); // $shortcode[ 'url' ]
88 break;
89
90 case 'import_conditions':
91 $shortcode[ $param_name ] = esc_attr( $param_value ); // $shortcode[ 'resource_id' ]
92 break;
93
94 case 'resource_id':
95 $shortcode[ $param_name ] = intval($param_value ); // $shortcode[ 'resource_id' ]
96 break;
97
98 case 'from': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-start', 'any', 'date' = 2017-08-07
99 $shortcode[ $param_name ] = $param_value;
100 break;
101
102 case 'from_offset': // 5d, 10h, 5m, 30s -- if from: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-start' }
103 $shortcode[ $param_name ] = $param_value;
104 break;
105
106 case 'until': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-end', 'any', 'date' = 2017-08-07
107 $shortcode[ $param_name ] = $param_value;
108 break;
109
110 case 'until_offset': // 5d, 10h, 5m, 30s -- if until: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-end' }
111 $shortcode[ $param_name ] = $param_value;
112 break;
113
114 case 'max':
115 $shortcode[ $param_name ] = intval( $param_value );
116 break;
117
118 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
119 $shortcode[ $param_name ] = intval( $param_value );
120 break;
121
122
123 default:
124 $shortcode[ $param_name ] = $param_value;
125 break;
126 }
127 }
128 $shortcode = wp_parse_args( $shortcode, $defaults );
129 // </editor-fold>
130
131
132 wpbm_delete_all_imported_bookings( array( 'resource_id' => $shortcode['resource_id'] ) ); //FixIn: 2.0.10.3
133
134
135 //debuge($shortcode);
136 // <editor-fold defaultstate="collapsed" desc=" Notice - Import Parameters | Error No URL" >
137
138 do_action( 'wpbc_show_debug', array( 'Import Parameters' , $shortcode ) ); // S_Y_S_T_E_M L_O_G
139
140 if ( empty( $shortcode[ 'url' ] ) ) {
141 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
142 return false;
143 }
144 // </editor-fold>
145
146
147 ///////////////////////////////////////////////////////////////////// - Get, Parse ICS Feed
148
149 $ics_array = wpbm_ics_file_to_array( $shortcode[ 'url' ] );
150
151 // <editor-fold defaultstate="collapsed" desc=" Notice - feed contain N events | Error Importing " >
152 do_action( 'wpbc_show_debug', array( 'Imported data' , $ics_array) ); // S_Y_S_T_E_M L_O_G
153
154 // If Error
155 if ( is_wp_error( $ics_array ) ) {
156
157 $error_message = $ics_array->get_error_message();
158 do_action( 'wpbc_admin_show_top_notice', $error_message, 'error', 5000 ); // N_O_T_I_C_E in H_E_A_D_E_R
159 return false;
160 }
161
162 //FixIn: 2.0.7.3
163 $ics_array_events_num = 0;
164 if ( ( ! empty( $ics_array ) ) && ( ! empty( $ics_array[ 'events' ] ) ) && ( is_array( $ics_array[ 'events' ] ) ) ) {
165 $ics_array_events_num = count( $ics_array[ 'events' ] );
166 }
167 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
168 , 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>' )
169 , 'info', 5000 );
170 // </editor-fold>
171
172 $bk_array = array();
173 // Get Only '_BOOKING...' field from ICS array
174 if ( $ics_array !== false ) {
175 $bk_array = wpbm_get_booking_fields_from_ics_array( $ics_array[ 'events' ] );
176 }
177
178 do_action( 'wpbc_show_debug', array( 'Imported Events', $bk_array ) ); // S_Y_S_T_E_M L_O_G
179
180
181 //FixIn: 2.0.6.1 - Set timezone frrom Booking > Settings > Sync page for booking listing shortcode
182 $tzid = get_bk_option( 'booking_gcal_timezone' );
183 if ( ! empty( $tzid ) ) {
184 foreach ( $bk_array as $bk_k => $bk_ics_data_arr ) {
185
186
187 //apply our timezone from the Booking > Settings > Search page
188 foreach ( $bk_array[ $bk_k ]['_BOOKING_DATES'] as $dk => $day ) {
189 //$bk_array[ $bk_k ][ '_BOOKING_DATES' ][ $dk ] = ZDateHelper::toLocalDateTime( get_gmt_from_date( $day ), $tzid );
190
191 //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
192 // Apply timezone only to fist and last days in a list, if the change over days activated in the Booking Calendar
193 $ics_time_in_day = substr( $day, -8);
194 if ( '00:00:00' == $ics_time_in_day ) {
195 continue;
196 }
197 //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.
198 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
199 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
200 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
201 ) { //FixIn: 2.0.5.1
202 $days_number = count( $bk_array[ $bk_k ]['_BOOKING_DATES'] );
203 if ( ( 0 != $dk ) && ( ( $days_number - 1) != $dk ) ) {
204 continue;
205 }
206 // We are append one day to the booking, so skip this day for timezone, as well
207 if ( ( get_bk_option( 'booking_ics_import_append_checkout_day' ) !== 'Off' ) && ( ( $days_number - 1) == $dk ) ) {
208 continue;
209 }
210 }
211
212 $bk_array[ $bk_k ]['_BOOKING_DATES'][ $dk ] = ZDateHelper::toLocalDateTime( $day, $tzid );
213 }
214 }
215 }
216
217 ///////////////////////////////////////////////////////////////////// - Check if these EVENTS was imported before or NOT
218
219 $booking_ics_force_import = get_bk_option( 'booking_ics_force_import' ); //FixIn: 2.0.10.1
220
221 if ( 'On' !== $booking_ics_force_import ) {
222 $bk_array = wpbm_clear_events_from_exist_bookings( $bk_array );
223 }
224
225
226 // <editor-fold defaultstate="collapsed" desc=" Notice - if events was imported previously | Already ALL Imported " >
227 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
228
229 if ( empty( $bk_array ) ) {
230
231 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
232 , '<strong>' . __( 'Warning', 'booking' ) . '!</strong> '
233 . sprintf( __( 'No any new events to import! These events was import previously, already.', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
234 , 'warning', 3000 );
235 return 0;
236 }
237 // </editor-fold>
238
239
240 ///////////////////////////////////////////////////////////////////// - Skip events that does not fit to filter parameters: FROM - UNTIL
241
242 // $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
243
244
245 // Sort Events
246 $bk_array = wpbm_sort_events_by( $bk_array );
247
248 // Filter By Dates - "From - Until"
249 $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
250
251 // Filter events by - "Max"
252 $bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode );
253
254
255 // <editor-fold defaultstate="collapsed" desc=" Notice - Creation of N bookings " >
256 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
257 , sprintf( __( 'Imported of %s bookings', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
258 , 'info', 3000 );
259
260 do_action( 'wpbc_show_debug', array( 'Create bookings after filtering', $bk_array ) ); // S_Y_S_T_E_M L_O_G
261 // </editor-fold>
262
263 ///////////////////////////////////////////////////////////////////// - Loop events > C r e a t e B o o k i n g s
264
265 // Get assigning fields for SUMMARY, DESCRIPTION, LOCATION
266 $assigned_fields_arr = WPBM_create_bookings_from_events::get_assigned_form_fields();
267
268 $booking_added_num = 0;
269
270 foreach ( $bk_array as $ics_event) {
271
272 //FixIn: 2.0.5.2
273 //FixIn: 8.1.3.29
274 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
275 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
276 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
277 && ( get_bk_option( 'booking_ics_import_append_checkout_day' ) !== 'Off' )
278 ) {
279 // 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
280 // Later system is adding check in/out times from Booking Calendar to this event
281 $ics_event_check_out = $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ];
282 $ics_event_check_out = strtotime( $ics_event_check_out );
283 $ics_event_check_out = strtotime( '+1 day', $ics_event_check_out );
284 $ics_event_check_out = date_i18n( "Y-m-d 00:00:00", $ics_event_check_out );
285 $ics_event['_BOOKING_DATES'][] = $ics_event_check_out;
286 }
287
288 $booking_data = array();
289
290 $dates_formats = array_fill( 0, count( $ics_event[ '_BOOKING_DATES' ] ), "Y-m-d" ); // Array ( [0] => Y-m-d [1] => Y-m-d ... )
291 $booking_dates_unix = array_map( 'strtotime', $ics_event[ '_BOOKING_DATES' ] ); // Array ( [0] => 1498262400 [1] => 1498348800 )
292 $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' )
293 //debuge('$ics_event, $simple_booking_dates', $ics_event, $simple_booking_dates);
294
295 //FixIn: 8.1.3.29
296 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
297 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
298 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
299 ) { //FixIn: 2.0.5.1
300 //Add check in/out times to full day events
301 if ( ( is_array( $simple_booking_dates ) ) && ( count( $simple_booking_dates ) > 1 ) ) { //FixIn: 2.0.10.4
302 $wpbc_check_in = ' ' . get_bk_option( 'booking_range_selection_start_time') . ':01'; // ' 14:00:01'
303 $wpbc_check_out = ' ' . get_bk_option( 'booking_range_selection_end_time') . ':02'; // ' 10:00:02';
304 $simple_booking_dates[0] = $simple_booking_dates[0] . $wpbc_check_in;
305 $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ] = $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ] . $wpbc_check_out;
306 $ics_event['_BOOKING_DATES'][0] = $simple_booking_dates[0];
307 $ics_event['_BOOKING_DATES'][ ( count( $simple_booking_dates ) - 1 ) ] = $simple_booking_dates[ ( count( $simple_booking_dates ) - 1 ) ];
308 }
309 }
310
311 //debuge($ics_event[ '_BOOKING_DATES' ]);die;
312 // Tempoary save our dates
313
314 // if( count( $ics_event['_BOOKING_DATES'] ) > 2 ) {
315 // unset( $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] ); //Remove last imported date
316 // if ( count( $ics_event['_BOOKING_DATES'] ) > 2 ) { // Add one date if number of days in booking more than 2
317 // $ics_event['_BOOKING_DATES'][ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] = $simple_booking_dates[ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] . $wpbc_check_out;
318 // } 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
319 // $ics_event['_BOOKING_DATES'][] = $simple_booking_dates[ ( count( $ics_event['_BOOKING_DATES'] ) - 1 ) ] . $wpbc_check_out;
320 // }
321 // }
322 WPBM_create_bookings_from_events::set_ics_dates( $ics_event[ '_BOOKING_DATES' ] );
323
324 $booking = array(
325 'dates' => $simple_booking_dates // array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
326 , 'data' => array()
327 , 'resource_id' => $shortcode[ 'resource_id' ]
328 );
329
330
331 $bk_data = array();
332 foreach ( $assigned_fields_arr as $assigned_field ) {
333
334 switch ( $assigned_field[ 'ics_field_name' ] ) {
335
336 case 'title':
337 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_SUMMARY' ] ) );
338 break;
339
340 case 'description':
341 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_DESCRIPTION' ] ) );
342 break;
343
344 case 'where':
345 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_LOCATION' ] ) );
346 break;
347
348 default:
349 break;
350 }
351 }
352
353 // Email
354 $home_url = explode( '://', home_url() );
355 //if (count($home_url>0)) //FixIn: 2.0.3.2
356 // $email = 'ics@' . $home_url[1];
357 //else
358 $email = get_option ( 'admin_email' );
359 $bk_data [ 'email' ] = array( 'value' => $email, 'type' => 'email' );
360
361 // Optional Start and End times
362 $start_time = substr( $ics_event[ '_BOOKING_DATES' ][ 0 ], 11, 5 );
363 $end_time = $ics_event[ '_BOOKING_DATES' ][ ( count( $ics_event[ '_BOOKING_DATES' ] ) - 1 ) ];
364 $end_time = substr( $end_time, 11, 5 );
365
366 //FixIn: 8.1.3.29
367 if ( ( class_exists( 'wpdev_bk_biz_s' ) )
368 && ( get_bk_option( 'booking_range_selection_time_is_active') == 'On' )
369 && ( get_bk_option( 'booking_ics_import_add_change_over_time' ) !== 'Off' )
370 ){ //FixIn: 2.0.5.1
371 //Add check in/out times
372 $start_time = get_bk_option( 'booking_range_selection_start_time' ); // ' 14:00'
373 $end_time = get_bk_option( 'booking_range_selection_end_time' ); // ' 10:00'
374 }
375
376
377 if ( ( $start_time !== '00:00' ) || ( $end_time !== '00:00' ) ) {
378 $bk_data [ 'rangetime' ] = array( 'value' => $start_time . ' - ' . $end_time, 'type' => 'select-one' );
379 }
380
381 $booking['data'] = $bk_data;
382
383 $additional_params = array( 'sync_gid' => $ics_event['_BOOKING_UID'], 'is_send_emeils' => 0 ); //FixIn: 2.0.10.2
384
385 /**
386 // $ics_event[ '_BOOKING_DATES' ]; // array ( [0] => 2014-09-16 05:00:01 [1] => 2014-09-16 12:00:02 )
387 // $ics_event[ '_BOOKING_SUMMARY' ]; // Event (timezone Pacific GMT-07:00)
388 // $ics_event[ '_BOOKING_DESCRIPTION' ]; // 8/7/2017 1:00pm TO 3:30pm 8/8/2017 (GMT-07:00) Pacific Time
389 // $ics_event[ '_BOOKING_LOCATION' ]; // Pacific Coast Highway, Pacific Coast Hwy, Los Angeles, CA, USA
390 // $ics_event[ '_BOOKING_UID' ]; // [email protected]
391 // $ics_event[ '_BOOKING_MODIFIED' ]; // 2017-06-28 10:12:35
392 */
393
394 add_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10, 5 );
395
396 // Do not overupdate "child booking resources" when saving bookings to parent booking resource. Just skip this checking
397 add_filter( 'wpbc_is_reupdate_dates_to_child_resources', 'wpbm_is_reupdate_dates_to_child_resources', 10, 7 );
398
399 // Check dates availability and process
400 // only if dates available in specific booking resource!
401 if ( $shortcode[ 'import_conditions' ] == 'if_dates_free' )
402 $is_dates_booked = wpbc_api_is_dates_booked( $booking[ 'dates' ], $booking[ 'resource_id' ] );
403 else
404 $is_dates_booked = false;
405
406 if ( ! $is_dates_booked ) {
407
408 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ] , $additional_params );
409 $booking_added_num++;
410
411 //if ( ( defined( 'WP_BK_AUTO_APPROVE_WHEN_IMPORT_GCAL' ) ) && ( WP_BK_AUTO_APPROVE_WHEN_IMPORT_GCAL ) ){ // Auto approve booking if imported
412 if ( ( function_exists( 'get_bk_option' )) && ( get_bk_option( 'booking_auto_approve_bookings_when_import' ) == 'On' ) ) { //FixIn: 8.1.3.27
413 // Auto approve booking, when imported. //FixIn:7.0.1.59
414 global $wpdb;
415 if ( false === $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}bookingdates SET approved = %s WHERE booking_id IN ({$booking_id})", '1' ) ) ){
416 ?> <script type="text/javascript">
417 var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error('Error during updating to DB' ,__FILE__,__LINE__) ),ENT_QUOTES) ; ?>';
418 wpbc_admin_show_message( my_message, 'error', 30000 );
419 </script> <?php
420 die();
421 }
422 }
423
424 //FixIn: 2.0.7.2 - add notes to the booking relative source of imported booking
425 $import_url = parse_url( $shortcode[ 'url' ] );
426 if ( ( false !== $import_url ) && ( ! empty( $import_url[ "host" ])) && ( class_exists('wpdev_bk_personal') ) ) {
427
428 $remark_text = str_replace('%','&#37;', '[' . date_i18n('Y-m-d H:i:s') . '] ' . sprintf( __( 'Imported from %s ', 'booking-manager'), $import_url[ "host" ] ) );
429 $my_remark = str_replace( array( '"', "'" ),'',$remark_text);
430 $my_remark = trim( $my_remark );
431
432 global $wpdb;
433
434 $update_sql = $wpdb->prepare( "UPDATE {$wpdb->prefix}booking AS bk SET bk.remark= %s WHERE bk.booking_id= %d ", $remark_text, $booking_id );
435 if ( false === $wpdb->query( $update_sql ) ) {
436 ?> <script type="text/javascript">
437 var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error('Error during updating notes in DB' ,__FILE__,__LINE__) ),ENT_QUOTES) ; ?>';
438 wpbc_admin_show_message( my_message, 'error', 30000 );
439 </script> <?php
440 die();
441 }
442 }
443
444 do_action( 'wpbc_show_debug', sprintf ( 'Added new booking ID:<strong>%d</strong> ', $booking_id ) ); // S_Y_S_T_E_M L_O_G
445 } else {
446
447 do_action( 'wpbc_show_debug', // S_Y_S_T_E_M L_O_G
448 sprintf ( 'Event was not create becausse dates %s already booked in booking resource ID = %d'
449 , implode( ', ', $booking[ 'dates' ] ) , $booking[ 'resource_id' ] ) );
450 }
451
452 remove_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10 );
453 remove_filter( 'wpbc_is_reupdate_dates_to_child_resources', 'wpbm_is_reupdate_dates_to_child_resources', 10 );
454
455 // Remove previously saved dates to our 'Static' class.
456 WPBM_create_bookings_from_events::erase_ics_dates();
457 }
458
459 return $booking_added_num;
460 }
461 add_action( 'wpbm_ics_import_start', 'wpbm_ics_import_start', 10, 1 );
462
463
464 ////////////////////////////////////////////////////////////////////////////////////////////
465 // Booking Calendar support functions
466 ////////////////////////////////////////////////////////////////////////////////////////////
467
468
469 /** Clear array of Events from events that already exist in Booking table
470 * Check UID and GUID sync paramaters
471 *
472 * @param array $bk_array - array of events
473 * @return array - trimmed array
474 */
475 function wpbm_clear_events_from_exist_bookings( $bk_array ) {
476
477 $bk_uid = $bk_guid = array();
478 // GET array of UID for imported bookings
479 foreach ( $bk_array as $ics_key => $ics_event) {
480
481 $bk_uid[ $ics_key ] = $ics_event[ '_BOOKING_UID' ];
482
483 if ( strpos( $ics_event[ '_BOOKING_UID' ], '@google.com') !== false ) {
484 // [email protected] - ID of event from ICS
485 // 15ig8t0i739kajgjc8ekc386dt_20170815 - ID of event during import from Google Calendar (probabaly created by ID before @ + first date)
486
487 $bk_guid[ $ics_key ] = str_replace( '@google.com'
488 , date_i18n( '_Ymd', strtotime( $ics_event[ '_BOOKING_DATES' ][0] ) )
489 , $ics_event[ '_BOOKING_UID' ]
490 );
491 }
492 }
493 $bookings_check_uid = array_merge( $bk_uid,$bk_guid );
494
495 $bookings_exit_uid = wpbm_get_exist_bookings_gid( $bookings_check_uid );
496
497 // Remove events which already exist
498 foreach ( $bk_uid as $ics_key => $ics_uid ) {
499
500 // If exist UID remove it from event
501 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
502 unset( $bk_array[ $ics_key ] );
503 }
504
505 // If we are having such GUID and it exist, then remove ir
506 if ( isset( $bk_guid[ $ics_key ] ) ) {
507 $ics_gid = $bk_guid[ $ics_key ];
508 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
509 unset( $bk_array[ $ics_key ] );
510 }
511 }
512 }
513 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
514
515 return $bk_array;
516 }
517
518
519 /** Check if bookings exist with specific sync UID
520 *
521 * @global type $wpdb
522 * @param array of UID to check
523 * @return array of exist UID
524 */
525 function wpbm_get_exist_bookings_gid( $uid_arr ) {
526
527 $sql_sync_gid = implode( "','", $uid_arr );
528
529 $exist_bookings_guid = array();
530
531 if ( ! empty( $sql_sync_gid ) ) {
532 global $wpdb;
533
534 $my_sql = "SELECT * FROM {$wpdb->prefix}booking WHERE sync_gid IN ('{$sql_sync_gid}') AND trash != 1"; //FixIn: 2.0.9.3
535 //debuge($sql_sync_gid);
536 //$my_sql = "SELECT * FROM wp_booking WHERE sync_gid IN ('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]')";
537 //debuge($my_sql);
538 $exist_bookings = $wpdb->get_results( $my_sql );
539
540 //debuge( 'wpbc_show_debug', array( 'SQL: ', $my_sql, $exist_bookings ) );
541
542 foreach ( $exist_bookings as $bk ) {
543 $exist_bookings_guid[] = $bk->sync_gid;
544 }
545 }
546 return $exist_bookings_guid;
547 }
548
549
550 /** Trim number of events in array
551 *
552 * @param array $bk_array
553 * @param array $shortcode
554 * @return array
555 */
556 function wpbm_clear_events_by_count( $bk_array, $shortcode ) {
557
558 if ( empty( $shortcode['max'] ) ) {
559 return $bk_array;
560 } else {
561 $max = intval( $shortcode['max'] );
562 }
563
564 $bk_count= count( $bk_array );
565 if ( $max > $bk_count )
566 return $bk_array;
567
568 $cnt = 0;
569 $events_arr = array();
570
571 foreach ( $bk_array as $ev_key => $ev_arr ) {
572
573 $events_arr[] = $ev_arr;
574
575 $cnt++;
576 if ( $cnt >= $max )
577 break;
578 }
579
580 return $events_arr;
581 }
582
583
584
585
586 /** Remove ONLY dates from event(s) that does not fit to filter parameters: FROM - UNTIL -- All events will exist here
587 *
588 * @param array $bk_array - array of events
589 * @param array $shortcode
590 * @return array - trimmed array
591 */
592 function wpbm_remove_dates_from_event_not_in_condition( $bk_array, $shortcode ) {
593
594 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
595 return $bk_array;
596 }
597
598 // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
599
600 // F R O M
601 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'from_offset' ] );
602
603 if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
604 if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
605
606 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
607
608 // </editor-fold>
609
610
611 // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
612
613 // U N T I L
614 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'until_offset' ] );
615
616 if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
617 if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
618
619 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
620
621 // </editor-fold>
622
623
624 foreach ( $bk_array as $e_key => $ics_event ) {
625
626 $new_dates = array();
627 //debuge($ics_event[ '_BOOKING_DATES' ] );
628 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
629
630 $ics_date_unix = strtotime( $ics_date );
631 //debuge( $d_key, $ics_date , array( $condition_from, $condition_until ) );
632 if ( ( $condition_from <= $ics_date_unix ) && ( $condition_until >= $ics_date_unix ) ) {
633
634 $new_dates[] = $ics_date;
635 }
636 }
637 //debuge( $new_dates ); die;
638 $bk_array[ $e_key ][ '_BOOKING_DATES' ] = $new_dates;
639 }
640
641 return $bk_array;
642 }
643
644
645 /** Skip events that does not fit to filter parameters: FROM - UNTIL
646 *
647 * @param array $bk_array - array of events
648 * @param array $shortcode
649 * @return array - trimmed array
650 */
651 function wpbm_clear_events_by_dates( $bk_array, $shortcode ) {
652
653 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
654 return $bk_array;
655 }
656
657 // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
658
659 // F R O M
660 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'from_offset' ] );
661
662 if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
663 if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
664
665 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
666
667 // </editor-fold>
668
669
670 // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
671
672 // U N T I L
673 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'until_offset' ] );
674
675 if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
676 if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
677
678 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
679
680 // </editor-fold>
681
682 // 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
683 if ( ! isset( $shortcode['is_all_dates_in'] ) )
684 $is_all_dates_in_condition = true;
685 else
686 $is_all_dates_in_condition = (bool) $shortcode['is_all_dates_in'];
687
688 //debuge($condition_from, date_i18n('Y-m-d H:is',$condition_from));
689 //debuge($condition_until, date_i18n('Y-m-d H:is',$condition_until));
690 //debuge( (int) $is_all_dates_in_condition );
691 //die;
692 // Remove some events, that does not inside From | End time
693 $remove_events_keys = array();
694 foreach ( $bk_array as $e_key => $ics_event ) {
695 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
696 $ics_date = strtotime( $ics_date );
697
698 if ( $is_all_dates_in_condition ) {
699 // STRICT - ALL DATES
700 if ( ( $condition_from > $ics_date ) || ($condition_until < $ics_date ) ) {
701
702 $remove_events_keys[] = $e_key;
703 continue;
704 }
705 } else {
706 // AT LEAST 1 DATE
707 if ( ( $condition_from <= $ics_date ) && ( $condition_until >= $ics_date ) ) {
708
709 $remove_events_keys = array_diff( $remove_events_keys, array( $e_key ) ); // Remove values "$e_key" from array
710 break;
711 } else {
712 if ( ! in_array( $e_key, $remove_events_keys ) ) {
713 $remove_events_keys[] = $e_key;
714 }
715 }
716
717 }
718
719 }
720 }
721
722 // Remove them
723 $remove_events_keys = array_unique( $remove_events_keys );
724
725 //debuge( $bk_array, $remove_events_keys);
726 foreach ( $remove_events_keys as $evnt_key ) {
727 unset( $bk_array[ $evnt_key ] );
728 }
729
730 return $bk_array;
731 }
732
733
734 /** Get time offset in seconds based on parameter
735 *
736 * @param string $offset_param 30s | 5m | 2h | 7d | just seconds
737 * @return int
738 */
739 function wpbm_get_offset_unix_from_param( $offset_param ) {
740
741 $offset = 0;
742 if ( ! empty( $offset_param ) ) {
743
744 $offset_type = substr( $offset_param, -1 );
745 $offset_value = substr( $offset_param, 0, -1 );
746
747 switch ( $offset_type ) {
748 case "s": // Seconds
749 $offset = intval( $offset_value );
750 break;
751 case "m": // Minutes
752 $offset = intval( $offset_value ) * 60 ;
753 break;
754 case "h": // Hours
755 $offset = intval( $offset_value ) * 3600 ;
756 break;
757 case "d": // Days
758 $offset = intval( $offset_value ) * 86400 ;
759 break;
760 default:
761 $offset = intval( $offset_value );
762 }
763 }
764 return $offset;
765 }
766
767
768 /** Get Unix Time based on date parameter / condition and offset in seconds
769 *
770 * @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'
771 * @param int $offset_unix - offset in seconds
772 * @return int - Unix time in seconds
773 */
774 function wpbm_get_time_unix_from_param_offset( $check_day, $offset_unix ) {
775
776 // $condition_end = strtotime ( $shortcode['until'] . ' +1 day - 1 second' );
777
778 $check_sql_day = explode( '-', $check_day );
779 if ( count( $check_sql_day ) == 3 ) {
780 $check_day = 'date';
781 }
782
783 switch ( $check_day ) {
784 // Don't just use time() for 'now', as this will effectively make cache duration 1 second.
785 // Instead set to previous minute. Events in Google Calendar cannot be set to precision of seconds anyway
786 case 'now':
787
788 //$time_unix = strtotime( date_i18n( 'Y-m-d H:i:s' ) ) + $offset_unix; // "Now" in "Timezone" from WordPress > Settings
789 $time_unix = mktime( date_i18n( 'H' ), date_i18n( 'i' ), 0, date_i18n( 'm' ), date_i18n( 'j' ), date_i18n( 'Y' ) ) + $offset_unix ;
790 break;
791 case 'today':
792 //$time_unix = strtotime( date_i18n( 'Y-m-d 00:00:00' ) ) + $offset_unix; // "Today 00:00" in "Timezone" from WordPress > Settings
793 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), date_i18n( 'j' ), date_i18n( 'Y' ) ) + $offset_unix ;
794 break;
795 case 'week':
796 case 'week-start':
797
798 $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
799 if ( empty( $start_of_week ) )
800 $start_of_week = 0;
801
802 $start_day = date_i18n( 'w' ) - $start_of_week ;
803 if ( $start_day < 0 )
804 $start_day = 7 + $start_day;
805
806 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), ( date_i18n( 'j' ) - $start_day ), date_i18n( 'Y' ) ) + $offset_unix ;
807 break;
808 case 'week-end':
809
810 $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
811 if ( empty( $start_of_week ) )
812 $start_of_week = 0;
813
814 $start_day = date_i18n( 'w' ) - $start_of_week ;
815 if ( $start_day < 0 )
816 $start_day = 7 + $start_day;
817 // minus 1 second -- prevent of events exactly == start Next period
818 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), ( date_i18n( 'j' ) - $start_day + 7 ), date_i18n( 'Y' ) ) + $offset_unix - 1;
819 break;
820 case 'month-start':
821 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), 1, date_i18n( 'Y' ) ) + $offset_unix ;
822 break;
823 case 'month-end':
824 $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
825 break;
826 case 'year-start':
827 $time_unix = mktime( 0, 0, 0, 1, 1, date_i18n( 'Y' ) ) + $offset_unix ;
828 break;
829 case 'year-end':
830 $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
831 break;
832 case 'date':
833
834 if ( intval( $check_sql_day[0] ) - intval( date('Y') ) > 15 ) { //FixIn m.2.0.1
835 $time_unix =2145916800;
836 } else {
837 $time_unix = mktime( 0, 0, 0, intval( $check_sql_day[1] ), intval( $check_sql_day[2] ), intval( $check_sql_day[0] ) );
838 }
839 break;
840 case 'any-start':
841 $time_unix = 0; // any - 1970-01-01 00:00
842 break;
843 case 'any-end':
844 $time_unix = 2145916800; //any - 2038-01-01 00:00
845 break;
846 default:
847 $time_unix = 2145916800; //any END - 2038-01-01 00:00
848 }
849
850 //debuge($time_unix, date_i18n('Y-m-d H:i:s (D)',$time_unix));
851
852
853 return $time_unix;
854 }
855
856
857 //FixIn: 2.0.11.4
858 /**
859 * Get booking ID, by searching in Booking Calendar by UID
860 * @param string $uid
861 *
862 * @return empty string | int $booking_id
863 */
864 function wpbm_get_booking_id_by_UID( $uid ){
865
866 $booking_id = [];
867
868 if ( function_exists( 'wpbc_api_get_bookings_arr' ) ) {
869
870 /*
871 $param = array(
872 'wh_booking_datenext' => 1,
873 'wh_booking_dateprior' => 1,
874 'wh_booking_date' => 3,
875 'wh_trash' => 'any',
876 'wh_modification_dateprior' => 1,
877 'wh_modification_date' => 3,
878 'wh_pay_status' => 'all',
879 'view_mode' => 'vm_listing',
880 'wh_booking_type' => '-999',
881 'wh_keyword' => $uid
882 );
883
884 $bookings_arr = wpbc_api_get_bookings_arr( $param );
885 */
886 global $wpdb;
887
888 $sql = "SELECT booking_id FROM {$wpdb->prefix}booking as bk WHERE bk.sync_gid LIKE '%%" . wpbc_clean_like_string_for_db( $uid ) . "%%'";
889
890 //$sql_escaped = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}booking as bk WHERE bk.sync_gid LIKE '%%%s%%'", $uid );
891
892 $res = $wpdb->get_results( $sql );
893
894 foreach ( $res as $booking_obj ) {
895 $booking_id[] = $booking_obj->booking_id;
896 }
897
898 //debuge('$bookings_arr',$res);
899
900 }
901
902 return implode( ',', $booking_id );
903 }