| 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 http://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 |
if ( is_wp_error( $ics_array ) ) { |
| 89 |
$error_message = $ics_array->get_error_message(); |
| 90 |
return $error_message; |
| 91 |
} |
| 92 |
|
| 93 |
if ( $ics_array !== false ) { |
| 94 |
if ( function_exists( 'wpbm_get_booking_fields_from_ics_array' ) ) { |
| 95 |
$bk_array = wpbm_get_booking_fields_from_ics_array( $ics_array[ 'events' ] ); |
| 96 |
} else { |
| 97 |
$bk_array = $ics_array[ 'events' ]; |
| 98 |
} |
| 99 |
} else |
| 100 |
$bk_array = array(); |
| 101 |
// </editor-fold> |
| 102 |
|
| 103 |
// Sort Events |
| 104 |
$bk_array = wpbm_sort_events_by( $bk_array ); |
| 105 |
|
| 106 |
// Filter events |
| 107 |
$bk_array = wpbm_clear_events_by_dates( $bk_array, $shortcode ); |
| 108 |
|
| 109 |
// Filter events |
| 110 |
$bk_array = wpbm_clear_events_by_count( $bk_array, $shortcode ); |
| 111 |
|
| 112 |
|
| 113 |
/** array ( Array ( [_BOOKING_DATES] => Array |
| 114 |
( |
| 115 |
[0] => 2014-07-24 00:00:00 |
| 116 |
[1] => 2014-07-25 00:00:00 |
| 117 |
[2] => 2014-07-26 00:00:00 |
| 118 |
) |
| 119 |
|
| 120 |
[_BOOKING_SUMMARY] => Reserve Room #1 |
| 121 |
[_BOOKING_DESCRIPTION] => Just description about this event!:) |
| 122 |
[_BOOKING_LOCATION] => London |
| 123 |
[_BOOKING_UID] => lnrjn92gsavrkkpodtbvm3plfs@google.com |
| 124 |
[_BOOKING_MODIFIED] => 2014-07-20 07:21:43 |
| 125 |
) |
| 126 |
, .... |
| 127 |
* ) |
| 128 |
* |
| 129 |
*/ |
| 130 |
|
| 131 |
|
| 132 |
// Filter events |
| 133 |
$bk_array = wpbm_remove_dates_from_event_not_in_condition( $bk_array, $shortcode ); |
| 134 |
// Re-Sort Events again, because we was removed some dates |
| 135 |
$bk_array = wpbm_sort_events_by( $bk_array ); |
| 136 |
|
| 137 |
|
| 138 |
// <editor-fold defaultstate="collapsed" desc=" Listing Template " > |
| 139 |
/////////////////////////////////////////////////////////////// |
| 140 |
// Listing Template |
| 141 |
/////////////////////////////////////////////////////////////// |
| 142 |
|
| 143 |
|
| 144 |
$listing_template = ''; |
| 145 |
// Simple Events listing |
| 146 |
foreach ( $bk_array as $evnt ) { |
| 147 |
|
| 148 |
$echo_row = wpbm_ics_get_listing_row( $evnt ); |
| 149 |
|
| 150 |
$listing_template .= $echo_row; |
| 151 |
} |
| 152 |
// </editor-fold> |
| 153 |
|
| 154 |
|
| 155 |
return $listing_template; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
/** Get .ics Event Listing Row |
| 161 |
* |
| 162 |
* @param array $evnt |
| 163 |
* @return string |
| 164 |
*/ |
| 165 |
function wpbm_ics_get_listing_row( $evnt ) { |
| 166 |
|
| 167 |
/** |
| 168 |
Array( |
| 169 |
[_BOOKING_DATES] => Array |
| 170 |
( |
| 171 |
[0] => 2014-07-24 00:00:00 |
| 172 |
[1] => 2014-07-25 00:00:00 |
| 173 |
[2] => 2014-07-26 00:00:00 |
| 174 |
) |
| 175 |
|
| 176 |
[_BOOKING_SUMMARY] => Reserve Room #1 |
| 177 |
[_BOOKING_DESCRIPTION] => Just description about this event!:) |
| 178 |
[_BOOKING_LOCATION] => London |
| 179 |
[_BOOKING_UID] => 9vxvxvv2gsavrkkpodtbvm3plfsvxcvxcvcxv@google.com |
| 180 |
[_BOOKING_MODIFIED] => 2014-07-20 07:21:43 |
| 181 |
) |
| 182 |
*/ |
| 183 |
|
| 184 |
/////////////////////////////////////////////////////////////// |
| 185 |
// Listing Template |
| 186 |
/////////////////////////////////////////////////////////////// |
| 187 |
$row_template = get_wpbm_option( 'wpbm_listing_template' ); |
| 188 |
|
| 189 |
// Normilize |
| 190 |
$replace_array = array(); |
| 191 |
foreach ( $evnt as $key => $value ) { |
| 192 |
$key = str_replace( '_BOOKING_', '', $key ); |
| 193 |
if ( is_array( $value ) ) |
| 194 |
$value = implode( ',', $value ); |
| 195 |
$replace_array[ $key ] = $value; |
| 196 |
} |
| 197 |
|
| 198 |
if ( ! empty( $replace_array['DATES'] ) ) |
| 199 |
$replace_array['DATES'] = wpbm_get_dates_short_format( $replace_array['DATES'] ); |
| 200 |
|
| 201 |
$echo_row = wpbm_replace_shortcodes( $row_template, $replace_array ); |
| 202 |
|
| 203 |
return $echo_row; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
/** Sort events by Check In days |
| 209 |
* |
| 210 |
* @param array $ics_events_arr |
| 211 |
* @param string $sort_key - Default: '_BOOKING_DATES' |
| 212 |
* @return array |
| 213 |
*/ |
| 214 |
function wpbm_sort_events_by( $ics_events_arr, $sort_key = '_BOOKING_DATES' ) { |
| 215 |
|
| 216 |
// Get array with check in date |
| 217 |
$check_in_arr = array(); |
| 218 |
foreach ( $ics_events_arr as $key => $event_arr) { |
| 219 |
$event_dates = $event_arr[ $sort_key ]; |
| 220 |
sort( $event_dates ); |
| 221 |
$check_in_arr[ $key ] = $event_dates[ 0 ]; |
| 222 |
} |
| 223 |
|
| 224 |
asort( $check_in_arr, SORT_STRING ); // Sort an array and maintain index association |
| 225 |
|
| 226 |
// Sort inpur arr, by keys from previous check in array |
| 227 |
$sorted_events_arr = array(); |
| 228 |
foreach ( $check_in_arr as $key => $event_check_in) { |
| 229 |
$sorted_events_arr[] = $ics_events_arr[ $key ]; |
| 230 |
} |
| 231 |
|
| 232 |
return $sorted_events_arr; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
// TODO: |
| 238 |
// - list events by days and not events ??? currently if (0) {...} |