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

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