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

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