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 +277 -215 2.1.7 → 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
@@ -66,14 +66,40 @@
66 66
67 67 return true;
68 68 }
69 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 +
70 96 ////////////////////////////////////////////////////////////////////////////////////////////
71 97 // I M P O R T
72 98 ////////////////////////////////////////////////////////////////////////////////////////////
73 99
74 -/** Import ICS feed and create bookings .in Booking Calendar
75 - *
100 +/** Import ICS feed and create bookings .in Booking Calendar
101 + *
76 102 * @param array $attr = array(
77 103 'url' => ''
78 104 , 'resource_id' => 1
79 105 , 'import_conditions' => '' | 'if_dates_free' // maybe rename ??? if_dates_booked_then_not_import
@@ -87,9 +113,9 @@
87 113 return wpbm_ics_import_start_legacy( $attr );
88 114
89 115 }
90 116
91 - // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
117 + // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
92 118 /////////////////////////////////////////////////////////////////////
93 119 // Parse / validate parameters
94 120 /////////////////////////////////////////////////////////////////////
95 121 $defaults = array(
@@ -109,76 +135,76 @@
109 135 foreach ( $attr as $param_name => $param_value ) {
110 136
111 137 switch ( $param_name) { // Validate Params
112 138
113 - case 'url':
139 + case 'url':
114 140 $shortcode[ $param_name ] = esc_url_raw( $param_value ); // $shortcode[ 'url' ]
115 141 $shortcode[ $param_name ] = str_replace( '&amp;', '&', $shortcode[ $param_name ] ); //FixIn: 2.0.14.2
116 142 break;
117 143
118 144 case 'import_conditions':
119 - $shortcode[ $param_name ] = esc_attr( $param_value ); // $shortcode[ 'resource_id' ]
145 + $shortcode[ $param_name ] = esc_attr( $param_value ); // $shortcode[ 'resource_id' ]
120 146 break;
121 -
122 - case 'resource_id':
123 - $shortcode[ $param_name ] = intval($param_value ); // $shortcode[ 'resource_id' ]
147 +
148 + case 'resource_id':
149 + $shortcode[ $param_name ] = intval($param_value ); // $shortcode[ 'resource_id' ]
124 150 break;
125 -
151 +
126 152 case 'from': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-start', 'any', 'date' = 2017-08-07
127 153 $shortcode[ $param_name ] = $param_value;
128 154 break;
129 -
155 +
130 156 case 'from_offset': // 5d, 10h, 5m, 30s -- if from: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-start' }
131 - $shortcode[ $param_name ] = $param_value;
157 + $shortcode[ $param_name ] = $param_value;
132 158 break;
133 -
159 +
134 160 case 'until': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-end', 'any', 'date' = 2017-08-07
135 161 $shortcode[ $param_name ] = $param_value;
136 162 break;
137 -
163 +
138 164 case 'until_offset': // 5d, 10h, 5m, 30s -- if until: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-end' }
139 - $shortcode[ $param_name ] = $param_value;
165 + $shortcode[ $param_name ] = $param_value;
140 166 break;
141 -
142 - case 'max':
143 - $shortcode[ $param_name ] = intval( $param_value );
167 +
168 + case 'max':
169 + $shortcode[ $param_name ] = intval( $param_value );
144 170 break;
145 -
171 +
146 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
147 - $shortcode[ $param_name ] = intval( $param_value );
173 + $shortcode[ $param_name ] = intval( $param_value );
148 174 break;
149 -
150 -
175 +
176 +
151 177 default:
152 178 $shortcode[ $param_name ] = $param_value;
153 179 break;
154 - }
155 - }
180 + }
181 + }
156 182 $shortcode = wp_parse_args( $shortcode, $defaults );
157 183 // </editor-fold>
158 184
159 - // <editor-fold defaultstate="collapsed" desc=" Notice - Import Parameters | Error No URL" >
160 -
185 + // <editor-fold defaultstate="collapsed" desc=" Notice - Import Parameters | Error No URL" >
186 +
161 187 do_action( 'wpbc_show_debug', array( 'Import Parameters' , $shortcode ) ); // S_Y_S_T_E_M L_O_G
162 188
163 - if ( empty( $shortcode[ 'url' ] ) ) {
164 - 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
165 191 return false;
166 - }
192 + }
167 193 // </editor-fold>
168 194
169 - ///////////////////////////////////////////////////////////////////// - Get, Parse ICS Feed
170 -
195 + ///////////////////////////////////////////////////////////////////// - Get, Parse ICS Feed
196 +
171 197 $ics_array = wpbm_ics_file_to_array( $shortcode[ 'url' ] );
172 198
173 - // <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 " >
174 200 do_action( 'wpbc_show_debug', array( 'Imported data' , $ics_array) ); // S_Y_S_T_E_M L_O_G
175 201
176 202 // If Error
177 203 if ( is_wp_error( $ics_array ) ) {
178 -
179 - $error_message = $ics_array->get_error_message();
180 - 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
181 207 return false;
182 208 }
183 209
184 210 //FixIn: 2.0.7.3
@@ -187,9 +213,9 @@
187 213 $ics_array_events_num = count( $ics_array[ 'events' ] );
188 214 }
189 215 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
190 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>' )
191 - , 'info', 5000 );
217 + , 'info', 5000 );
192 218 // </editor-fold>
193 219
194 220 if ( 'skip' != $shortcode['delete'] ) {
195 221 wpbm_delete_all_imported_bookings( array( 'resource_id' => $shortcode['resource_id'] ) ); //FixIn: 2.0.18.3 //FixIn: 2.0.10.3
@@ -196,13 +222,13 @@
196 222 }
197 223
198 224
199 225 $bk_array = array();
200 - // Get Only '_BOOKING...' field from ICS array
201 - if ( $ics_array !== false ) {
202 - $bk_array = wpbm_get_booking_fields_from_ics_array( $ics_array[ 'events' ] );
203 - }
204 -
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 +
205 231 do_action( 'wpbc_show_debug', array( 'Imported Events', $bk_array ) ); // S_Y_S_T_E_M L_O_G
206 232
207 233
208 234 //FixIn: 2.0.6.1 - Set timezone frrom Booking > Settings > Sync page for booking listing shortcode
@@ -261,45 +287,45 @@
261 287 // <editor-fold defaultstate="collapsed" desc=" Notice - if events was imported previously | Already ALL Imported " >
262 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
263 289
264 290 if ( empty( $bk_array ) ) {
265 -
266 - do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
267 - , '<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> '
268 294 . sprintf( __( 'No any new events to import! These events was import previously, already.', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
269 - , 'warning', 3000 );
270 - return 0;
295 + , 'warning', 3000 );
296 + return 0;
271 297 }
272 298 // </editor-fold>
273 -
274 -
275 - ///////////////////////////////////////////////////////////////////// - Skip events that does not fit to filter parameters: FROM - UNTIL
276 -
299 +
300 +
301 + ///////////////////////////////////////////////////////////////////// - Skip events that does not fit to filter parameters: FROM - UNTIL
302 +
277 303 // Sort Events
278 304 $bk_array = wpbm_sort_events_by( $bk_array );
279 305
280 - // Filter By Dates - "From - Until"
306 + // Filter By Dates - "From - Until"
281 307 $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
282 308
283 - // Filter events by - "Max"
309 + // Filter events by - "Max"
284 310 $bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode );
285 311
286 -
287 - // <editor-fold defaultstate="collapsed" desc=" Notice - Creation of N bookings " >
312 +
313 + // <editor-fold defaultstate="collapsed" desc=" Notice - Creation of N bookings " >
288 314 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
289 315 , sprintf( __( 'Imported of %s bookings', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
290 - , 'info', 3000 );
291 -
316 + , 'info', 3000 );
317 +
292 318 do_action( 'wpbc_show_debug', array( 'Create bookings after filtering', $bk_array ) ); // S_Y_S_T_E_M L_O_G
293 319 // </editor-fold>
294 -
320 +
295 321 ///////////////////////////////////////////////////////////////////// - Loop events > C r e a t e B o o k i n g s
296 322
297 - // Get assigning fields for SUMMARY, DESCRIPTION, LOCATION
323 + // Get assigning fields for SUMMARY, DESCRIPTION, LOCATION
298 324 $assigned_fields_arr = WPBM_create_bookings_from_events::get_assigned_form_fields();
299 -
325 +
300 326 $booking_added_num = 0;
301 -
327 +
302 328 foreach ( $bk_array as $ics_event) {
303 329
304 330 //FixIn: 2.1.3
305 331 if (
@@ -335,8 +361,15 @@
335 361 $ics_event_check_out = strtotime( $ics_event_check_out );
336 362 $ics_event_check_out = strtotime( '+1 day', $ics_event_check_out );
337 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 );
338 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 + }
339 372 }
340 373
341 374 $booking_data = array();
342 375
@@ -374,9 +407,9 @@
374 407
375 408 $booking = array(
376 409 'dates' => $simple_booking_dates // array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
377 410 , 'data' => array()
378 - , 'resource_id' => $shortcode[ 'resource_id' ]
411 + , 'resource_id' => $shortcode[ 'resource_id' ]
379 412 );
380 413
381 414
382 415 $bk_data = array();
@@ -383,17 +416,17 @@
383 416 foreach ( $assigned_fields_arr as $assigned_field ) {
384 417
385 418 switch ( $assigned_field[ 'ics_field_name' ] ) {
386 419
387 - case 'title':
420 + case 'title':
388 421 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_SUMMARY' ] ) );
389 422 break;
390 423
391 - case 'description':
424 + case 'description':
392 425 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_DESCRIPTION' ] ) );
393 426 break;
394 427
395 - case 'where':
428 + case 'where':
396 429 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_LOCATION' ] ) );
397 430 break;
398 431
399 432 default:
@@ -401,13 +434,13 @@
401 434 }
402 435 }
403 436
404 437 // Email
405 - $email = '[email protected]'; //get_option ( 'admin_email' );
438 + $email = '[email protected]'; //get_option ( 'admin_email' );
406 439 if ( ! empty( $ics_event['_BOOKING_ATTENDEE'] ) ) {
407 440 $email = str_replace( 'mailto:', '', $ics_event['_BOOKING_ATTENDEE'] );
408 441 if ( ! is_email( $email ) ) {
409 - $email = '[email protected]';
442 + $email = '[email protected]';
410 443 }
411 444 }
412 445 $bk_data [ 'email' ] = array( 'value' => $email, 'type' => 'email' );
413 446
@@ -486,9 +519,10 @@
486 519 } else{
487 520 $is_dates_booked = false;
488 521 $additional_params['save_booking_even_if_unavailable'] = 1;
489 522 }
490 - $additional_params['is_use_booking_recurrent_time'] = false;
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' ) );
491 525
492 526 if ( ! $is_dates_booked ) {
493 527
494 528 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ] , $additional_params );
@@ -527,9 +561,9 @@
527 561 } else {
528 562
529 563 do_action( 'wpbc_show_debug', // S_Y_S_T_E_M L_O_G
530 564 sprintf ( 'Event was not create becausse dates %s already booked in booking resource ID = %d'
531 - , implode( ', ', $booking[ 'dates' ] ) , $booking[ 'resource_id' ] ) );
565 + , implode( ', ', $booking[ 'dates' ] ) , $booking[ 'resource_id' ] ) );
532 566 }
533 567
534 568 remove_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10 );
535 569
@@ -534,12 +568,12 @@
534 568 remove_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10 );
535 569
536 570
537 571 // Remove previously saved dates to our 'Static' class.
538 - WPBM_create_bookings_from_events::erase_ics_dates();
572 + WPBM_create_bookings_from_events::erase_ics_dates();
539 573 }
540 -
541 - return $booking_added_num;
574 +
575 + return $booking_added_num;
542 576 }
543 577 add_action( 'wpbm_ics_import_start', 'wpbm_ics_import_start', 10, 1 );
544 578
545 579
@@ -802,8 +836,15 @@
802 836 $ics_event_check_out = strtotime( $ics_event_check_out );
803 837 $ics_event_check_out = strtotime( '+1 day', $ics_event_check_out );
804 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 );
805 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 + }
806 847 }
807 848
808 849 $booking_data = array();
809 850
@@ -1001,9 +1042,9 @@
1001 1042
1002 1043
1003 1044 /** Clear array of Events from events that already exist in Booking table
1004 1045 * Check UID and GUID sync paramaters
1005 - *
1046 + *
1006 1047 * @param array $bk_array - array of events
1007 1048 * @return array - trimmed array
1008 1049 */
1009 1050 function wpbm_clear_events_from_exist_bookings( $bk_array ) {
@@ -1010,11 +1051,11 @@
1010 1051
1011 1052 $bk_uid = $bk_guid = array();
1012 1053 // GET array of UID for imported bookings
1013 1054 foreach ( $bk_array as $ics_key => $ics_event) {
1014 -
1055 +
1015 1056 $bk_uid[ $ics_key ] = $ics_event[ '_BOOKING_UID' ];
1016 -
1057 +
1017 1058 if ( strpos( $ics_event[ '_BOOKING_UID' ], '@google.com') !== false ) {
1018 1059 // [email protected] - ID of event from ICS
1019 1060 // 15ig8t0i739kajgjc8ekc386dt_20170815 - ID of event during import from Google Calendar (probabaly created by ID before @ + first date)
1020 1061
@@ -1019,24 +1060,24 @@
1019 1060 // 15ig8t0i739kajgjc8ekc386dt_20170815 - ID of event during import from Google Calendar (probabaly created by ID before @ + first date)
1020 1061
1021 1062 $bk_guid[ $ics_key ] = str_replace( '@google.com'
1022 1063 , date_i18n( '_Ymd', strtotime( $ics_event[ '_BOOKING_DATES' ][0] ) )
1023 - , $ics_event[ '_BOOKING_UID' ]
1064 + , $ics_event[ '_BOOKING_UID' ]
1024 1065 );
1025 - }
1026 - }
1066 + }
1067 + }
1027 1068 $bookings_check_uid = array_merge( $bk_uid,$bk_guid );
1028 -
1069 +
1029 1070 $bookings_exit_uid = wpbm_get_exist_bookings_gid( $bookings_check_uid );
1030 1071
1031 1072 // Remove events which already exist
1032 1073 foreach ( $bk_uid as $ics_key => $ics_uid ) {
1033 -
1074 +
1034 1075 // If exist UID remove it from event
1035 1076 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
1036 1077 unset( $bk_array[ $ics_key ] );
1037 1078 }
1038 -
1079 +
1039 1080 // If we are having such GUID and it exist, then remove ir
1040 1081 if ( isset( $bk_guid[ $ics_key ] ) ) {
1041 1082 $ics_gid = $bk_guid[ $ics_key ];
1042 1083 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
@@ -1049,191 +1090,212 @@
1049 1090 return $bk_array;
1050 1091 }
1051 1092
1052 1093
1053 - /** Check if bookings exist with specific sync UID
1054 - *
1055 - * @global type $wpdb
1056 - * @param array of UID to check
1057 - * @return array of exist UID
1058 - */
1059 - 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 +}
1060 1144
1061 - $sql_sync_gid = implode( "','", $uid_arr );
1062 1145
1063 - $exist_bookings_guid = array();
1064 -
1065 - if ( ! empty( $sql_sync_gid ) ) {
1066 - global $wpdb;
1067 -
1068 - $my_sql = "SELECT * FROM {$wpdb->prefix}booking WHERE sync_gid IN ('{$sql_sync_gid}') AND trash != 1"; //FixIn: 2.0.9.3
1069 -//debuge($sql_sync_gid);
1070 -//$my_sql = "SELECT * FROM wp_booking WHERE sync_gid IN ('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]')";
1071 -//debuge($my_sql);
1072 - $exist_bookings = $wpdb->get_results( $my_sql );
1073 -
1074 -//debuge( 'wpbc_show_debug', array( 'SQL: ', $my_sql, $exist_bookings ) );
1075 -
1076 - foreach ( $exist_bookings as $bk ) {
1077 - $exist_bookings_guid[] = $bk->sync_gid;
1078 - }
1079 - }
1080 - return $exist_bookings_guid;
1081 - }
1082 -
1083 -
1084 1146 /** Trim number of events in array
1085 - *
1147 + *
1086 1148 * @param array $bk_array
1087 1149 * @param array $shortcode
1088 1150 * @return array
1089 - */
1151 + */
1090 1152 function wpbm_clear_events_by_count( $bk_array, $shortcode ) {
1091 -
1153 +
1092 1154 if ( empty( $shortcode['max'] ) ) {
1093 1155 return $bk_array;
1094 1156 } else {
1095 1157 $max = intval( $shortcode['max'] );
1096 1158 }
1097 -
1159 +
1098 1160 $bk_count= count( $bk_array );
1099 1161 if ( $max > $bk_count )
1100 1162 return $bk_array;
1101 -
1163 +
1102 1164 $cnt = 0;
1103 1165 $events_arr = array();
1104 -
1166 +
1105 1167 foreach ( $bk_array as $ev_key => $ev_arr ) {
1106 -
1168 +
1107 1169 $events_arr[] = $ev_arr;
1108 -
1170 +
1109 1171 $cnt++;
1110 - if ( $cnt >= $max )
1172 + if ( $cnt >= $max )
1111 1173 break;
1112 1174 }
1113 -
1175 +
1114 1176 return $events_arr;
1115 -}
1116 -
1177 +}
1117 1178
1118 1179
1119 1180
1181 +
1120 1182 /** Remove ONLY dates from event(s) that does not fit to filter parameters: FROM - UNTIL -- All events will exist here
1121 - *
1183 + *
1122 1184 * @param array $bk_array - array of events
1123 1185 * @param array $shortcode
1124 1186 * @return array - trimmed array
1125 - */
1187 + */
1126 1188 function wpbm_remove_dates_from_event_not_in_condition( $bk_array, $shortcode ) {
1127 -
1189 +
1128 1190 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
1129 1191 return $bk_array;
1130 1192 }
1131 -
1132 - // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
1133 1193
1194 + // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
1195 +
1134 1196 // F R O M
1135 1197 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'from_offset' ] );
1136 -
1137 - if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
1138 - if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
1139 -
1198 +
1199 + if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
1200 + if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
1201 +
1140 1202 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
1141 -
1203 +
1142 1204 // </editor-fold>
1143 -
1144 1205
1145 - // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
1146 -
1147 - // 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
1148 1210 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'until_offset' ] );
1149 -
1150 - if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
1151 - if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
1152 -
1211 +
1212 + if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
1213 + if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
1214 +
1153 1215 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
1154 -
1216 +
1155 1217 // </editor-fold>
1156 -
1157 -
1218 +
1219 +
1158 1220 foreach ( $bk_array as $e_key => $ics_event ) {
1159 -
1221 +
1160 1222 $new_dates = array();
1161 -//debuge($ics_event[ '_BOOKING_DATES' ] );
1223 +//debuge($ics_event[ '_BOOKING_DATES' ] );
1162 1224 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
1163 -
1225 +
1164 1226 $ics_date_unix = strtotime( $ics_date );
1165 -//debuge( $d_key, $ics_date , array( $condition_from, $condition_until ) );
1166 - if ( ( $condition_from <= $ics_date_unix ) && ( $condition_until >= $ics_date_unix ) ) {
1167 -
1227 +//debuge( $d_key, $ics_date , array( $condition_from, $condition_until ) );
1228 + if ( ( $condition_from <= $ics_date_unix ) && ( $condition_until >= $ics_date_unix ) ) {
1229 +
1168 1230 $new_dates[] = $ics_date;
1169 1231 }
1170 1232 }
1171 -//debuge( $new_dates ); die;
1233 +//debuge( $new_dates ); die;
1172 1234 $bk_array[ $e_key ][ '_BOOKING_DATES' ] = $new_dates;
1173 1235 }
1174 -
1236 +
1175 1237 return $bk_array;
1176 1238 }
1177 1239
1178 1240
1179 1241 /** Skip events that does not fit to filter parameters: FROM - UNTIL
1180 - *
1242 + *
1181 1243 * @param array $bk_array - array of events
1182 1244 * @param array $shortcode
1183 1245 * @return array - trimmed array
1184 - */
1246 + */
1185 1247 function wpbm_clear_events_by_dates( $bk_array, $shortcode ) {
1186 1248
1187 1249 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
1188 1250 return $bk_array;
1189 1251 }
1190 -
1191 - // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
1192 1252
1253 + // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
1254 +
1193 1255 // F R O M
1194 1256 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'from_offset' ] );
1195 -
1196 - if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
1197 - if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
1198 -
1257 +
1258 + if ( $shortcode['from'] == 'any' ) $shortcode['from'] = 'any-start';
1259 + if ( $shortcode['from'] == 'week' ) $shortcode['from'] = 'week-start';
1260 +
1199 1261 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
1200 -
1262 +
1201 1263 // </editor-fold>
1202 -
1203 1264
1204 - // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
1205 -
1206 - // 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
1207 1269 $offset = wpbm_get_offset_unix_from_param( $shortcode[ 'until_offset' ] );
1208 -
1209 - if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
1210 - if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
1211 -
1270 +
1271 + if ( $shortcode['until'] == 'any' ) $shortcode['until'] = 'any-end';
1272 + if ( $shortcode['until'] == 'week' ) $shortcode['until'] = 'week-end';
1273 +
1212 1274 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
1213 -
1275 +
1214 1276 // </editor-fold>
1215 1277
1216 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
1217 1279 if ( ! isset( $shortcode['is_all_dates_in'] ) )
1218 - $is_all_dates_in_condition = true;
1219 - else
1220 - $is_all_dates_in_condition = (bool) $shortcode['is_all_dates_in'];
1221 -
1280 + $is_all_dates_in_condition = true;
1281 + else
1282 + $is_all_dates_in_condition = (bool) $shortcode['is_all_dates_in'];
1283 +
1222 1284 //debuge($condition_from, date_i18n('Y-m-d H:is',$condition_from));
1223 1285 //debuge($condition_until, date_i18n('Y-m-d H:is',$condition_until));
1224 1286 //debuge( (int) $is_all_dates_in_condition );
1225 1287 //die;
1226 1288 // Remove some events, that does not inside From | End time
1227 - $remove_events_keys = array();
1289 + $remove_events_keys = array();
1228 1290 foreach ( $bk_array as $e_key => $ics_event ) {
1229 1291 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
1230 1292 $ics_date = strtotime( $ics_date );
1231 -
1293 +
1232 1294 if ( $is_all_dates_in_condition ) {
1233 1295 // STRICT - ALL DATES
1234 1296 if ( ( $condition_from > $ics_date ) || ($condition_until < $ics_date ) ) {
1235 -
1297 +
1236 1298 $remove_events_keys[] = $e_key;
1237 1299 continue;
1238 1300 }
1239 1301 } else {
@@ -1238,9 +1300,9 @@
1238 1300 }
1239 1301 } else {
1240 1302 // AT LEAST 1 DATE
1241 1303 if ( ( $condition_from <= $ics_date ) && ( $condition_until >= $ics_date ) ) {
1242 -
1304 +
1243 1305 $remove_events_keys = array_diff( $remove_events_keys, array( $e_key ) ); // Remove values "$e_key" from array
1244 1306 break;
1245 1307 } else {
1246 1308 if ( ! in_array( $e_key, $remove_events_keys ) ) {
@@ -1246,18 +1308,18 @@
1246 1308 if ( ! in_array( $e_key, $remove_events_keys ) ) {
1247 1309 $remove_events_keys[] = $e_key;
1248 1310 }
1249 1311 }
1250 -
1312 +
1251 1313 }
1252 -
1314 +
1253 1315 }
1254 1316 }
1255 -
1317 +
1256 1318 // Remove them
1257 1319 $remove_events_keys = array_unique( $remove_events_keys );
1258 -
1259 -//debuge( $bk_array, $remove_events_keys);
1320 +
1321 +//debuge( $bk_array, $remove_events_keys);
1260 1322 foreach ( $remove_events_keys as $evnt_key ) {
1261 1323 unset( $bk_array[ $evnt_key ] );
1262 1324 }
1263 1325
@@ -1265,22 +1327,22 @@
1265 1327 }
1266 1328
1267 1329
1268 1330 /** Get time offset in seconds based on parameter
1269 - *
1331 + *
1270 1332 * @param string $offset_param 30s | 5m | 2h | 7d | just seconds
1271 1333 * @return int
1272 1334 */
1273 1335 function wpbm_get_offset_unix_from_param( $offset_param ) {
1274 -
1275 - $offset = 0;
1336 +
1337 + $offset = 0;
1276 1338 if ( ! empty( $offset_param ) ) {
1277 -
1339 +
1278 1340 $offset_type = substr( $offset_param, -1 );
1279 1341 $offset_value = substr( $offset_param, 0, -1 );
1280 -
1342 +
1281 1343 switch ( $offset_type ) {
1282 - case "s": // Seconds
1344 + case "s": // Seconds
1283 1345 $offset = intval( $offset_value );
1284 1346 break;
1285 1347 case "m": // Minutes
1286 1348 $offset = intval( $offset_value ) * 60 ;
@@ -1292,65 +1354,65 @@
1292 1354 $offset = intval( $offset_value ) * 86400 ;
1293 1355 break;
1294 1356 default:
1295 1357 $offset = intval( $offset_value );
1296 - }
1297 - }
1358 + }
1359 + }
1298 1360 return $offset;
1299 1361 }
1300 1362
1301 1363
1302 1364 /** Get Unix Time based on date parameter / condition and offset in seconds
1303 - *
1365 + *
1304 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'
1305 1367 * @param int $offset_unix - offset in seconds
1306 1368 * @return int - Unix time in seconds
1307 1369 */
1308 1370 function wpbm_get_time_unix_from_param_offset( $check_day, $offset_unix ) {
1309 -
1371 +
1310 1372 // $condition_end = strtotime ( $shortcode['until'] . ' +1 day - 1 second' );
1311 -
1373 +
1312 1374 $check_sql_day = explode( '-', $check_day );
1313 1375 if ( count( $check_sql_day ) == 3 ) {
1314 1376 $check_day = 'date';
1315 - }
1377 + }
1316 1378
1317 1379 switch ( $check_day ) {
1318 - // 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.
1319 1381 // Instead set to previous minute. Events in Google Calendar cannot be set to precision of seconds anyway
1320 1382 case 'now':
1321 -
1322 - //$time_unix = strtotime( date_i18n( 'Y-m-d H:i:s' ) ) + $offset_unix; // "Now" in "Timezone" from WordPress > Settings
1323 - $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 ;
1324 1386 break;
1325 1387 case 'today':
1326 - //$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
1327 1389 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), date_i18n( 'j' ), date_i18n( 'Y' ) ) + $offset_unix ;
1328 1390 break;
1329 1391 case 'week':
1330 1392 case 'week-start':
1331 -
1332 - $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
1333 - 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 ) )
1334 1396 $start_of_week = 0;
1335 -
1397 +
1336 1398 $start_day = date_i18n( 'w' ) - $start_of_week ;
1337 - if ( $start_day < 0 )
1399 + if ( $start_day < 0 )
1338 1400 $start_day = 7 + $start_day;
1339 1401
1340 1402 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), ( date_i18n( 'j' ) - $start_day ), date_i18n( 'Y' ) ) + $offset_unix ;
1341 1403 break;
1342 1404 case 'week-end':
1343 -
1405 +
1344 1406 $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
1345 - if ( empty( $start_of_week ) )
1407 + if ( empty( $start_of_week ) )
1346 1408 $start_of_week = 0;
1347 -
1409 +
1348 1410 $start_day = date_i18n( 'w' ) - $start_of_week ;
1349 - if ( $start_day < 0 )
1350 - $start_day = 7 + $start_day;
1411 + if ( $start_day < 0 )
1412 + $start_day = 7 + $start_day;
1351 1413 // minus 1 second -- prevent of events exactly == start Next period
1352 - $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;
1353 1415 break;
1354 1416 case 'month-start':
1355 1417 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), 1, date_i18n( 'Y' ) ) + $offset_unix ;
1356 1418 break;
@@ -1358,12 +1420,12 @@
1358 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
1359 1421 break;
1360 1422 case 'year-start':
1361 1423 $time_unix = mktime( 0, 0, 0, 1, 1, date_i18n( 'Y' ) ) + $offset_unix ;
1362 - break;
1424 + break;
1363 1425 case 'year-end':
1364 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
1365 - break;
1427 + break;
1366 1428 case 'date':
1367 1429
1368 1430 if ( intval( $check_sql_day[0] ) - intval( date('Y') ) > 15 ) { //FixIn m.2.0.1
1369 1431 $time_unix =2145916800;
@@ -1370,12 +1432,12 @@
1370 1432 } else {
1371 1433 $time_unix = mktime( 0, 0, 0, intval( $check_sql_day[1] ), intval( $check_sql_day[2] ), intval( $check_sql_day[0] ) );
1372 1434 }
1373 1435 break;
1374 - case 'any-start':
1436 + case 'any-start':
1375 1437 $time_unix = 0; // any - 1970-01-01 00:00
1376 1438 break;
1377 - case 'any-end':
1439 + case 'any-end':
1378 1440 $time_unix = 2145916800; //any - 2038-01-01 00:00
1379 1441 break;
1380 1442 default:
1381 1443 $time_unix = 2145916800; //any END - 2038-01-01 00:00
@@ -1380,9 +1442,9 @@
1380 1442 default:
1381 1443 $time_unix = 2145916800; //any END - 2038-01-01 00:00
1382 1444 }
1383 1445
1384 -//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));
1385 1447
1386 1448
1387 1449 return $time_unix;
1388 1450 }
@@ -1427,11 +1489,11 @@
1427 1489
1428 1490 foreach ( $res as $booking_obj ) {
1429 1491 $booking_id[] = $booking_obj->booking_id;
1430 1492 }
1431 -
1493 +
1432 1494 //debuge('$bookings_arr',$res);
1433 1495
1434 1496 }
1435 1497
1436 1498 return implode( ',', $booking_id );
1437 -}
1499 +}