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

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