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