PluginProbe
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar / 2.0.2
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar v2.0.2
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 2.0.22 All 55 releases
booking-manager / core / wpbc / wpbm-bc-import.php

wpbm-bc-import.php in Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar 2.0.2, at core/wpbc/wpbm-bc-import.php

678 lines 25.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @description Booking Calendar integration - Import bookings from ICS events
5 *
6 * @author wpdevelop
7 * @link http://oplugins.com/
8 * @email info@oplugins.com
9 *
10 * @modified 2017-06-28
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15
16 ////////////////////////////////////////////////////////////////////////////////////////////
17 // I M P O R T
18 ////////////////////////////////////////////////////////////////////////////////////////////
19
20 /** Import ICS feed and create bookings .in Booking Calendar
21 *
22 * @param array $attr = array(
23 'url' => ''
24 , 'resource_id' => 1
25 , 'import_conditions' => '' | 'if_dates_free' // maybe rename ??? if_dates_booked_then_not_import
26 )
27 * @return int|bool number of imported bookings or FALSE if error
28 */
29 function wpbm_ics_import_start( $attr ) {
30
31 // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
32 /////////////////////////////////////////////////////////////////////
33 // Parse / validate parameters
34 /////////////////////////////////////////////////////////////////////
35 $defaults = array(
36 'url' => ''
37 , 'resource_id' => 1
38 , 'import_conditions' => ''
39 , 'from' => 'any' // 'today' // '00:00 today'
40 , 'from_offset' => ''
41 , 'until' => 'any' // 'year-end'
42 , 'until_offset' => ''
43 , 'max' => ''
44 , '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
45 );
46 $shortcode = array();
47
48 foreach ( $attr as $param_name => $param_value ) {
49
50 switch ( $param_name) { // Validate Params
51
52 case 'url':
53 $shortcode[ $param_name ] = esc_url_raw( $param_value ); // $shortcode[ 'url' ]
54 break;
55
56 case 'import_conditions':
57 $shortcode[ $param_name ] = esc_attr( $param_value ); // $shortcode[ 'resource_id' ]
58 break;
59
60 case 'resource_id':
61 $shortcode[ $param_name ] = intval($param_value ); // $shortcode[ 'resource_id' ]
62 break;
63
64 case 'from': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-start', 'any', 'date' = 2017-08-07
65 $shortcode[ $param_name ] = $param_value;
66 break;
67
68 case 'from_offset': // 5d, 10h, 5m, 30s -- if from: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-start' }
69 $shortcode[ $param_name ] = $param_value;
70 break;
71
72 case 'until': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-end', 'any', 'date' = 2017-08-07
73 $shortcode[ $param_name ] = $param_value;
74 break;
75
76 case 'until_offset': // 5d, 10h, 5m, 30s -- if until: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-end' }
77 $shortcode[ $param_name ] = $param_value;
78 break;
79
80 case 'max':
81 $shortcode[ $param_name ] = intval( $param_value );
82 break;
83
84 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 );
86 break;
87
88
89 default:
90 $shortcode[ $param_name ] = $param_value;
91 break;
92 }
93 }
94 $shortcode = wp_parse_args( $shortcode, $defaults );
95 // </editor-fold>
96
97
98 // <editor-fold defaultstate="collapsed" desc=" Notice - Import Parameters | Error No URL" >
99
100 do_action( 'wpbc_show_debug', array( 'Import Parameters' , $shortcode ) ); // S_Y_S_T_E_M L_O_G
101
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
104 return false;
105 }
106 // </editor-fold>
107
108
109 ///////////////////////////////////////////////////////////////////// - Get, Parse ICS Feed
110
111 $ics_array = wpbm_ics_file_to_array( $shortcode[ 'url' ] );
112
113 // <editor-fold defaultstate="collapsed" desc=" Notice - feed contain N events | Error Importing " >
114 do_action( 'wpbc_show_debug', array( 'Imported data' , $ics_array) ); // S_Y_S_T_E_M L_O_G
115
116 // If Error
117 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
121 return false;
122 }
123
124 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 );
127 // </editor-fold>
128
129 $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
135 do_action( 'wpbc_show_debug', array( 'Imported Events', $bk_array ) ); // S_Y_S_T_E_M L_O_G
136
137
138 ///////////////////////////////////////////////////////////////////// - Check if these EVENTS was imported before or NOT
139
140 $bk_array = wpbm_clear_events_from_exist_bookings( $bk_array );
141
142
143 // <editor-fold defaultstate="collapsed" desc=" Notice - if events was imported previously | Already ALL Imported " >
144 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
146 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> '
150 . 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;
153 }
154 // </editor-fold>
155
156
157 ///////////////////////////////////////////////////////////////////// - Skip events that does not fit to filter parameters: FROM - UNTIL
158
159 // $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
160
161
162 // Sort Events
163 $bk_array = wpbm_sort_events_by( $bk_array );
164
165 // Filter By Dates - "From - Until"
166 $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
167
168 // Filter events by - "Max"
169 $bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode );
170
171
172 // <editor-fold defaultstate="collapsed" desc=" Notice - Creation of N bookings " >
173 do_action( 'wpbc_admin_show_top_notice' // N_O_T_I_C_E in H_E_A_D_E_R
174 , sprintf( __( 'Imported of %s bookings', 'booking-manager' ), '<strong>' . count( $bk_array ) . '</strong>' )
175 , 'info', 3000 );
176
177 do_action( 'wpbc_show_debug', array( 'Create bookings after filtering', $bk_array ) ); // S_Y_S_T_E_M L_O_G
178 // </editor-fold>
179
180 ///////////////////////////////////////////////////////////////////// - Loop events > C r e a t e B o o k i n g s
181
182 // Get assigning fields for SUMMARY, DESCRIPTION, LOCATION
183 $assigned_fields_arr = WPBM_create_bookings_from_events::get_assigned_form_fields();
184
185 $booking_added_num = 0;
186
187 foreach ( $bk_array as $ics_event) {
188
189 $booking_data = array();
190
191 $dates_formats = array_fill( 0, count( $ics_event[ '_BOOKING_DATES' ] ), "Y-m-d" ); // Array ( [0] => Y-m-d [1] => Y-m-d ... )
192 $booking_dates_unix = array_map( 'strtotime', $ics_event[ '_BOOKING_DATES' ] ); // Array ( [0] => 1498262400 [1] => 1498348800 )
193 $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
195 // Tempoary save our dates
196 WPBM_create_bookings_from_events::set_ics_dates( $ics_event[ '_BOOKING_DATES' ] );
197
198 $booking = array(
199 'dates' => $simple_booking_dates // array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
200 , 'data' => array()
201 , 'resource_id' => $shortcode[ 'resource_id' ]
202 );
203
204
205 $bk_data = array();
206 foreach ( $assigned_fields_arr as $assigned_field ) {
207
208 switch ( $assigned_field[ 'ics_field_name' ] ) {
209
210 case 'title':
211 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_SUMMARY' ] ) );
212 break;
213
214 case 'description':
215 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_DESCRIPTION' ] ) );
216 break;
217
218 case 'where':
219 $bk_data [ $assigned_field[ 'name' ] ]= array( 'type' => $assigned_field[ 'type' ], 'value' => trim( $ics_event[ '_BOOKING_LOCATION' ] ) );
220 break;
221
222 default:
223 break;
224 }
225 }
226
227 // 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' );
233 $bk_data [ 'email' ] = array( 'value' => $email, 'type' => 'email' );
234
235 // Optional Start and End times
236 $start_time = substr( $ics_event[ '_BOOKING_DATES' ][ 0 ], 11, 5 );
237 $end_time = $ics_event[ '_BOOKING_DATES' ][ ( count( $ics_event[ '_BOOKING_DATES' ] ) - 1 ) ];
238 $end_time = substr( $end_time, 11, 5 );
239
240 if ( ( $start_time !== '00:00' ) || ( $end_time !== '00:00' ) ) {
241 $bk_data [ 'rangetime' ] = array( 'value' => $start_time . ' - ' . $end_time, 'type' => 'select-one' );
242 }
243
244 $booking['data'] = $bk_data;
245
246 $additional_params = array( 'sync_gid' => $ics_event[ '_BOOKING_UID' ] );
247
248 /**
249 // $ics_event[ '_BOOKING_DATES' ]; // array ( [0] => 2014-09-16 05:00:01 [1] => 2014-09-16 12:00:02 )
250 // $ics_event[ '_BOOKING_SUMMARY' ]; // Event (timezone Pacific GMT-07:00)
251 // $ics_event[ '_BOOKING_DESCRIPTION' ]; // 8/7/2017 1:00pm TO 3:30pm 8/8/2017 (GMT-07:00) Pacific Time
252 // $ics_event[ '_BOOKING_LOCATION' ]; // Pacific Coast Highway, Pacific Coast Hwy, Los Angeles, CA, USA
253 // $ics_event[ '_BOOKING_UID' ]; // 5t3ogfsb3tqj09po7fiou6hh60@google.com
254 // $ics_event[ '_BOOKING_MODIFIED' ]; // 2017-06-28 10:12:35
255 */
256
257 add_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10, 5 );
258
259 // 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 );
261
262 // Check dates availability and process
263 // 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
267 $is_dates_booked = false;
268
269 if ( ! $is_dates_booked ) {
270
271 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ] , $additional_params );
272 $booking_added_num++;
273
274 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 } else {
276
277 do_action( 'wpbc_show_debug', // S_Y_S_T_E_M L_O_G
278 sprintf ( 'Event was not create becausse dates %s already booked in booking resource ID = %d'
279 , implode( ', ', $booking[ 'dates' ] ) , $booking[ 'resource_id' ] ) );
280 }
281
282 remove_filter( 'wpbc_get_insert_sql_for_dates', 'wpbm_get_insert_sql_for_dates', 10 );
283 remove_filter( 'wpbc_is_reupdate_dates_to_child_resources', 'wpbm_is_reupdate_dates_to_child_resources', 10 );
284
285 // Remove previously saved dates to our 'Static' class.
286 WPBM_create_bookings_from_events::erase_ics_dates();
287 }
288
289 return $booking_added_num;
290 }
291 add_action( 'wpbm_ics_import_start', 'wpbm_ics_import_start', 10, 1 );
292
293
294 ////////////////////////////////////////////////////////////////////////////////////////////
295 // Booking Calendar support functions
296 ////////////////////////////////////////////////////////////////////////////////////////////
297
298
299 /** Clear array of Events from events that already exist in Booking table
300 * Check UID and GUID sync paramaters
301 *
302 * @param array $bk_array - array of events
303 * @return array - trimmed array
304 */
305 function wpbm_clear_events_from_exist_bookings( $bk_array ) {
306
307 $bk_uid = $bk_guid = array();
308 // GET array of UID for imported bookings
309 foreach ( $bk_array as $ics_key => $ics_event) {
310
311 $bk_uid[ $ics_key ] = $ics_event[ '_BOOKING_UID' ];
312
313 if ( strpos( $ics_event[ '_BOOKING_UID' ], '@google.com') !== false ) {
314 // 15ig8t0i739kajgjc8ekc386dt@google.com - ID of event from ICS
315 // 15ig8t0i739kajgjc8ekc386dt_20170815 - ID of event during import from Google Calendar (probabaly created by ID before @ + first date)
316
317 $bk_guid[ $ics_key ] = str_replace( '@google.com'
318 , date_i18n( '_Ymd', strtotime( $ics_event[ '_BOOKING_DATES' ][0] ) )
319 , $ics_event[ '_BOOKING_UID' ]
320 );
321 }
322 }
323 $bookings_check_uid = array_merge( $bk_uid,$bk_guid );
324
325 $bookings_exit_uid = wpbm_get_exist_bookings_gid( $bookings_check_uid );
326
327 // Remove events which already exist
328 foreach ( $bk_uid as $ics_key => $ics_uid ) {
329
330 // If exist UID remove it from event
331 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
332 unset( $bk_array[ $ics_key ] );
333 }
334
335 // If we are having such GUID and it exist, then remove ir
336 if ( isset( $bk_guid[ $ics_key ] ) ) {
337 $ics_gid = $bk_guid[ $ics_key ];
338 if ( in_array( $ics_uid, $bookings_exit_uid ) ) {
339 unset( $bk_array[ $ics_key ] );
340 }
341 }
342 }
343 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
344
345 return $bk_array;
346 }
347
348
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 ) {
356
357 $sql_sync_gid = implode( "','", $uid_arr );
358
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 /** Trim number of events in array
375 *
376 * @param array $bk_array
377 * @param array $shortcode
378 * @return array
379 */
380 function wpbm_clear_events_by_count( $bk_array, $shortcode ) {
381
382 if ( empty( $shortcode['max'] ) ) {
383 return $bk_array;
384 } else {
385 $max = intval( $shortcode['max'] );
386 }
387
388 $bk_count= count( $bk_array );
389 if ( $max > $bk_count )
390 return $bk_array;
391
392 $cnt = 0;
393 $events_arr = array();
394
395 foreach ( $bk_array as $ev_key => $ev_arr ) {
396
397 $events_arr[] = $ev_arr;
398
399 $cnt++;
400 if ( $cnt >= $max )
401 break;
402 }
403
404 return $events_arr;
405 }
406
407
408
409
410 /** Remove ONLY dates from event(s) that does not fit to filter parameters: FROM - UNTIL -- All events will exist here
411 *
412 * @param array $bk_array - array of events
413 * @param array $shortcode
414 * @return array - trimmed array
415 */
416 function wpbm_remove_dates_from_event_not_in_condition( $bk_array, $shortcode ) {
417
418 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
419 return $bk_array;
420 }
421
422 // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
423
424 // F R O M
425 $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
430 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
431
432 // </editor-fold>
433
434
435 // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
436
437 // U N T I L
438 $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
443 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
444
445 // </editor-fold>
446
447
448 foreach ( $bk_array as $e_key => $ics_event ) {
449
450 $new_dates = array();
451 //debuge($ics_event[ '_BOOKING_DATES' ] );
452 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
453
454 $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
458 $new_dates[] = $ics_date;
459 }
460 }
461 //debuge( $new_dates ); die;
462 $bk_array[ $e_key ][ '_BOOKING_DATES' ] = $new_dates;
463 }
464
465 return $bk_array;
466 }
467
468
469 /** Skip events that does not fit to filter parameters: FROM - UNTIL
470 *
471 * @param array $bk_array - array of events
472 * @param array $shortcode
473 * @return array - trimmed array
474 */
475 function wpbm_clear_events_by_dates( $bk_array, $shortcode ) {
476
477 if ( ( ! isset( $shortcode['from'] ) ) && ( ! isset( $shortcode['until'] ) ) ) {
478 return $bk_array;
479 }
480
481 // <editor-fold defaultstate="collapsed" desc=" F R O M C o n d " >
482
483 // F R O M
484 $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
489 $condition_from = wpbm_get_time_unix_from_param_offset( $shortcode['from'], $offset );
490
491 // </editor-fold>
492
493
494 // <editor-fold defaultstate="collapsed" desc=" U N T I L C o n d " >
495
496 // U N T I L
497 $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
502 $condition_until = wpbm_get_time_unix_from_param_offset( $shortcode['until'], $offset );
503
504 // </editor-fold>
505
506 // 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 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
512 //debuge($condition_from, date_i18n('Y-m-d H:is',$condition_from));
513 //debuge($condition_until, date_i18n('Y-m-d H:is',$condition_until));
514 //debuge( (int) $is_all_dates_in_condition );
515 //die;
516 // Remove some events, that does not inside From | End time
517 $remove_events_keys = array();
518 foreach ( $bk_array as $e_key => $ics_event ) {
519 foreach ( $ics_event[ '_BOOKING_DATES' ] as $d_key => $ics_date ) {
520 $ics_date = strtotime( $ics_date );
521
522 if ( $is_all_dates_in_condition ) {
523 // STRICT - ALL DATES
524 if ( ( $condition_from > $ics_date ) || ($condition_until < $ics_date ) ) {
525
526 $remove_events_keys[] = $e_key;
527 continue;
528 }
529 } else {
530 // AT LEAST 1 DATE
531 if ( ( $condition_from <= $ics_date ) && ( $condition_until >= $ics_date ) ) {
532
533 $remove_events_keys = array_diff( $remove_events_keys, array( $e_key ) ); // Remove values "$e_key" from array
534 break;
535 } else {
536 if ( ! in_array( $e_key, $remove_events_keys ) ) {
537 $remove_events_keys[] = $e_key;
538 }
539 }
540
541 }
542
543 }
544 }
545
546 // Remove them
547 $remove_events_keys = array_unique( $remove_events_keys );
548
549 //debuge( $bk_array, $remove_events_keys);
550 foreach ( $remove_events_keys as $evnt_key ) {
551 unset( $bk_array[ $evnt_key ] );
552 }
553
554 return $bk_array;
555 }
556
557
558 /** Get time offset in seconds based on parameter
559 *
560 * @param string $offset_param 30s | 5m | 2h | 7d | just seconds
561 * @return int
562 */
563 function wpbm_get_offset_unix_from_param( $offset_param ) {
564
565 $offset = 0;
566 if ( ! empty( $offset_param ) ) {
567
568 $offset_type = substr( $offset_param, -1 );
569 $offset_value = substr( $offset_param, 0, -1 );
570
571 switch ( $offset_type ) {
572 case "s": // Seconds
573 $offset = intval( $offset_value );
574 break;
575 case "m": // Minutes
576 $offset = intval( $offset_value ) * 60 ;
577 break;
578 case "h": // Hours
579 $offset = intval( $offset_value ) * 3600 ;
580 break;
581 case "d": // Days
582 $offset = intval( $offset_value ) * 86400 ;
583 break;
584 default:
585 $offset = intval( $offset_value );
586 }
587 }
588 return $offset;
589 }
590
591
592 /** Get Unix Time based on date parameter / condition and offset in seconds
593 *
594 * @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 * @param int $offset_unix - offset in seconds
596 * @return int - Unix time in seconds
597 */
598 function wpbm_get_time_unix_from_param_offset( $check_day, $offset_unix ) {
599
600 // $condition_end = strtotime ( $shortcode['until'] . ' +1 day - 1 second' );
601
602 $check_sql_day = explode( '-', $check_day );
603 if ( count( $check_sql_day ) == 3 ) {
604 $check_day = 'date';
605 }
606
607 switch ( $check_day ) {
608 // Don't just use time() for 'now', as this will effectively make cache duration 1 second.
609 // Instead set to previous minute. Events in Google Calendar cannot be set to precision of seconds anyway
610 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 ;
614 break;
615 case 'today':
616 //$time_unix = strtotime( date_i18n( 'Y-m-d 00:00:00' ) ) + $offset_unix; // "Today 00:00" in "Timezone" from WordPress > Settings
617 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), date_i18n( 'j' ), date_i18n( 'Y' ) ) + $offset_unix ;
618 break;
619 case 'week':
620 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 ) )
624 $start_of_week = 0;
625
626 $start_day = date_i18n( 'w' ) - $start_of_week ;
627 if ( $start_day < 0 )
628 $start_day = 7 + $start_day;
629
630 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), ( date_i18n( 'j' ) - $start_day ), date_i18n( 'Y' ) ) + $offset_unix ;
631 break;
632 case 'week-end':
633
634 $start_of_week = get_wpbm_option( 'wpbm_start_day_weeek' ); //get_option( 'start_of_week' );
635 if ( empty( $start_of_week ) )
636 $start_of_week = 0;
637
638 $start_day = date_i18n( 'w' ) - $start_of_week ;
639 if ( $start_day < 0 )
640 $start_day = 7 + $start_day;
641 // 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;
643 break;
644 case 'month-start':
645 $time_unix = mktime( 0, 0, 0, date_i18n( 'm' ), 1, date_i18n( 'Y' ) ) + $offset_unix ;
646 break;
647 case 'month-end':
648 $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 break;
650 case 'year-start':
651 $time_unix = mktime( 0, 0, 0, 1, 1, date_i18n( 'Y' ) ) + $offset_unix ;
652 break;
653 case 'year-end':
654 $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;
656 case 'date':
657
658 if ( intval( $check_sql_day[0] ) - intval( date('Y') ) > 15 ) { //FixIn m.2.0.1
659 $time_unix =2145916800;
660 } else {
661 $time_unix = mktime( 0, 0, 0, intval( $check_sql_day[1] ), intval( $check_sql_day[2] ), intval( $check_sql_day[0] ) );
662 }
663 break;
664 case 'any-start':
665 $time_unix = 0; // any - 1970-01-01 00:00
666 break;
667 case 'any-end':
668 $time_unix = 2145916800; //any - 2038-01-01 00:00
669 break;
670 default:
671 $time_unix = 2145916800; //any END - 2038-01-01 00:00
672 }
673
674 //debuge($time_unix, date_i18n('Y-m-d H:i:s (D)',$time_unix));
675
676
677 return $time_unix;
678 }