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
booking-manager / core / wpbm-ics-listing.php

wpbm-ics-listing.php in Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar 2.1.21, at core/wpbm-ics-listing.php

266 lines 8.9 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 * @package ICS Listing functions
5 * @subpackage Import / Export ICS
6 * @category ICS
7 *
8 * @author wpdevelop
9 * @link https://oplugins.com/
10 * @email info@oplugins.com
11 *
12 * @modified 2017-03-01
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
16
17
18
19 // [booking-manager-listing url='https://server.com/feed.ics' from='2017-08-06' until='week' until_offset='4h' max=500]
20 // [booking-manager-listing url='https://server.com/feed.ics' from='today' from_offset='5d' until='year-end' until_offset='4h' max=500]
21 function wpbm_ics_get_listing( $attr ) {
22
23 // <editor-fold defaultstate="collapsed" desc=" Parse / validate parameters " >
24 /////////////////////////////////////////////////////////////////////
25 // Parse / validate parameters
26 /////////////////////////////////////////////////////////////////////
27 $defaults = array(
28 'url' => ''
29 , 'from' => 'any' //'today' // '00:00 today'
30 , 'from_offset' => ''
31 , 'until' => 'any' //'year-end' // '00:00 today'
32 , 'until_offset' => ''
33 , 'max' => ''
34 , 'is_all_dates_in' => false // 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
35 );
36 $shortcode = array();
37
38 foreach ( $attr as $param_name => $param_value ) {
39
40 switch ( $param_name) { // Validate Params
41
42 case 'url':
43 $shortcode[ $param_name ] = esc_url_raw( $param_value ); // $shortcode[ 'url' ]
44 break;
45
46 case 'from': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-start', 'any', 'date' = 2017-08-07
47 $shortcode[ $param_name ] = $param_value;
48 break;
49
50 case 'from_offset': // 5d, 10h, 5m, 30s -- if from: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-start' }
51 $shortcode[ $param_name ] = $param_value;
52 break;
53
54 case 'until': // 'now', 'today', 'week', 'month-start', 'month-end', 'year-end', 'any', 'date' = 2017-08-07
55 $shortcode[ $param_name ] = $param_value;
56 break;
57
58 case 'until_offset': // 5d, 10h, 5m, 30s -- if until: { 'now', 'today', 'week', 'month-start', 'month-end', 'year-end' }
59 $shortcode[ $param_name ] = $param_value;
60 break;
61
62 case 'max':
63 $shortcode[ $param_name ] = intval( $param_value );
64 break;
65
66 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
67 $shortcode[ $param_name ] = intval( $param_value );
68 break;
69
70 default:
71 $shortcode[ $param_name ] = $param_value;
72 break;
73 }
74 }
75 $shortcode = wp_parse_args( $shortcode, $defaults );
76 // </editor-fold>
77
78
79 if ( empty( $shortcode[ 'url' ] ) ) return '<strong>[WPBM Error]</strong> ' . __( 'No URL for .ics feed.', 'booking-manager' );
80
81
82 // <editor-fold defaultstate="collapsed" desc=" Parse ICS Feed " >
83 ///////////////////////////////////////////////////////////////
84 // Parse ICS Feed
85 ///////////////////////////////////////////////////////////////
86
87 $ics_array = wpbm_ics_file_to_array( $shortcode[ 'url' ] );
88
89 if ( is_wp_error( $ics_array ) ) {
90 $error_message = $ics_array->get_error_message();
91 return $error_message;
92 }
93
94 if ( $ics_array !== false ) {
95 if ( function_exists( 'wpbm_get_booking_fields_from_ics_array' ) ) {
96 $bk_array = wpbm_get_booking_fields_from_ics_array( $ics_array[ 'events' ] );
97 } else {
98 $bk_array = $ics_array[ 'events' ];
99 }
100 } else
101 $bk_array = array();
102 // </editor-fold>
103
104 // Sort Events
105 $bk_array = wpbm_sort_events_by( $bk_array );
106
107 // Filter events
108 $bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode );
109
110 // Filter events
111 $bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode );
112
113
114 /** array ( Array ( [_BOOKING_DATES] => Array
115 (
116 [0] => 2014-07-24 00:00:00
117 [1] => 2014-07-25 00:00:00
118 [2] => 2014-07-26 00:00:00
119 )
120
121 [_BOOKING_SUMMARY] => Reserve Room #1
122 [_BOOKING_DESCRIPTION] => Just description about this event!:)
123 [_BOOKING_LOCATION] => London
124 [_BOOKING_UID] => lnrjn92gsavrkkpodtbvm3plfs@google.com
125 [_BOOKING_MODIFIED] => 2014-07-20 07:21:43
126 )
127 , ....
128 * )
129 *
130 */
131
132
133 // Filter events
134 $bk_array = wpbm_remove_dates_from_event_not_in_condition( $bk_array, $shortcode );
135 // Re-Sort Events again, because we was removed some dates
136 $bk_array = wpbm_sort_events_by( $bk_array );
137
138
139 // <editor-fold defaultstate="collapsed" desc=" Listing Template " >
140 ///////////////////////////////////////////////////////////////
141 // Listing Template
142 ///////////////////////////////////////////////////////////////
143
144
145 $listing_template = '';
146 // Simple Events listing
147 foreach ( $bk_array as $evnt ) {
148
149 $echo_row = wpbm_ics_get_listing_row( $evnt );
150
151 $listing_template .= $echo_row;
152 }
153 // </editor-fold>
154
155
156 return $listing_template;
157 }
158
159
160
161 /** Get .ics Event Listing Row
162 *
163 * @param array $evnt
164 * @return string
165 */
166 function wpbm_ics_get_listing_row( $evnt ) {
167
168 /**
169 Array(
170 [_BOOKING_DATES] => Array
171 (
172 [0] => 2014-07-24 00:00:00
173 [1] => 2014-07-25 00:00:00
174 [2] => 2014-07-26 00:00:00
175 )
176
177 [_BOOKING_SUMMARY] => Reserve Room #1
178 [_BOOKING_DESCRIPTION] => Just description about this event!:)
179 [_BOOKING_LOCATION] => London
180 [_BOOKING_UID] => 9vxvxvv2gsavrkkpodtbvm3plfsvxcvxcvcxv@google.com
181 [_BOOKING_MODIFIED] => 2014-07-20 07:21:43
182 )
183 */
184
185 ///////////////////////////////////////////////////////////////
186 // Listing Template
187 ///////////////////////////////////////////////////////////////
188 $row_template = wpbm_sanitize_listing_template( get_wpbm_option( 'wpbm_listing_template' ) );
189
190 // Normilize
191 $replace_array = array();
192 foreach ( $evnt as $key => $value ) {
193 $key = str_replace( '_BOOKING_', '', $key );
194 if ( is_array( $value ) )
195 $value = implode( ',', $value );
196 $replace_array[ $key ] = $value;
197 }
198
199 //FixIn: 2.0.11.4
200 if (function_exists('wpbc_get_bookings_url')) {
201
202 $my_booking_id = wpbm_get_booking_id_by_UID( $replace_array['UID'] );
203 $replace_array['BOOKING_ID'] = $my_booking_id;
204
205 //FixIn: 2.0.11.5
206 $replace_array['BOOKING_LINK'] = htmlspecialchars_decode(
207 // '<a href="' .
208 esc_url( wpbc_get_bookings_url() . '&tab=vm_booking_listing&wh_booking_id=' . $my_booking_id )
209 // . '">' . __('here', 'booking') . '</a>'
210 );
211 }
212
213 //FixIn: 2.0.6.1 - Set timezone frrom Booking > Settings > Sync page for booking listing shortcode
214 $tzid = get_bk_option( 'booking_gcal_timezone' );
215 if ( ! empty( $tzid ) ) {
216 $dates_array = explode( ',', $replace_array['DATES'] );
217 foreach ( $dates_array as $dk => $day ) {
218 //$dates_array[ $dk ] = ZDateHelper::toLocalDateTime( get_gmt_from_date( $day ), $tzid );
219 //debuge($day);
220 $dates_array[ $dk ] = ZDateHelper::toLocalDateTime( $day, $tzid );
221 }
222 $replace_array['DATES'] = implode( ',', $dates_array );
223 }
224
225 if ( ! empty( $replace_array['DATES'] ) )
226 $replace_array['DATES'] = wpbm_get_dates_short_format( $replace_array['DATES'] );
227
228 $echo_row = wpbm_replace_shortcodes( $row_template, $replace_array );
229
230 return $echo_row;
231 }
232
233
234
235 /** Sort events by Check In days
236 *
237 * @param array $ics_events_arr
238 * @param string $sort_key - Default: '_BOOKING_DATES'
239 * @return array
240 */
241 function wpbm_sort_events_by( $ics_events_arr, $sort_key = '_BOOKING_DATES' ) {
242
243 // Get array with check in date
244 $check_in_arr = array();
245 foreach ( $ics_events_arr as $key => $event_arr) {
246 $event_dates = $event_arr[ $sort_key ];
247 sort( $event_dates );
248 $check_in_arr[ $key ] = $event_dates[ 0 ];
249 }
250
251 asort( $check_in_arr, SORT_STRING ); // Sort an array and maintain index association
252
253 // Sort inpur arr, by keys from previous check in array
254 $sorted_events_arr = array();
255 foreach ( $check_in_arr as $key => $event_check_in) {
256 $sorted_events_arr[] = $ics_events_arr[ $key ];
257 }
258
259 return $sorted_events_arr;
260 }
261
262
263
264 // TODO:
265 // - list events by days and not events ??? currently if (0) {...}
266