activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
1 year ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
1 year ago
carts_helper.php
1 year ago
connector_helper.php
1 year ago
csv_helper.php
1 year ago
customer_helper.php
1 year ago
database_helper.php
1 year ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
1 year ago
encrypt_helper.php
1 year ago
events_helper.php
1 year ago
form_helper.php
1 year ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
1 year ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
1 year ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
1 year ago
order_intent_helper.php
1 year ago
orders_helper.php
1 year ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
1 year ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
1 year ago
shortcodes_helper.php
1 year ago
sms_helper.php
1 year ago
steps_helper.php
1 year ago
stripe_connect_helper.php
1 year ago
styles_helper.php
1 year ago
support_topics_helper.php
1 year ago
time_helper.php
1 year ago
timeline_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
1 year ago
version_specific_updates_helper.php
1 year ago
work_periods_helper.php
1 year ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
icalendar_helper.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ICS.php |
| 4 | * ======= |
| 5 | * Use this class to create an .ics file. |
| 6 | * |
| 7 | * Usage |
| 8 | * ----- |
| 9 | * Basic usage - generate ics file contents (see below for available properties): |
| 10 | * $ics = new ICS($props); |
| 11 | * $ics_file_contents = $ics->to_string(); |
| 12 | * |
| 13 | * Setting properties after instantiation |
| 14 | * $ics = new ICS(); |
| 15 | * $ics->set('summary', 'My awesome event'); |
| 16 | * |
| 17 | * You can also set multiple properties at the same time by using an array: |
| 18 | * $ics->set(array( |
| 19 | * 'dtstart' => 'now + 30 minutes', |
| 20 | * 'dtend' => 'now + 1 hour' |
| 21 | * )); |
| 22 | * |
| 23 | * Available properties |
| 24 | * -------------------- |
| 25 | * description |
| 26 | * String description of the event. |
| 27 | * dtend |
| 28 | * A date/time stamp designating the end of the event. You can use either a |
| 29 | * DateTime object or a PHP datetime format string (e.g. "now + 1 hour"). |
| 30 | * dtstart |
| 31 | * A date/time stamp designating the start of the event. You can use either a |
| 32 | * DateTime object or a PHP datetime format string (e.g. "now + 1 hour"). |
| 33 | * location |
| 34 | * String address or description of the location of the event. |
| 35 | * summary |
| 36 | * String short summary of the event - usually used as the title. |
| 37 | * url |
| 38 | * A url to attach to the the event. Make sure to add the protocol (http:// |
| 39 | * or https://). |
| 40 | */ |
| 41 | class ICS { |
| 42 | const DT_FORMAT = 'Ymd\THis'; |
| 43 | protected $properties = array(); |
| 44 | private $available_properties = array( |
| 45 | 'description', |
| 46 | 'dtend', |
| 47 | 'dtstart', |
| 48 | 'location', |
| 49 | 'summary', |
| 50 | 'url' |
| 51 | ); |
| 52 | public function __construct($props) { |
| 53 | $this->set($props); |
| 54 | } |
| 55 | public function set($key, $val = false) { |
| 56 | if (is_array($key)) { |
| 57 | foreach ($key as $k => $v) { |
| 58 | $this->set($k, $v); |
| 59 | } |
| 60 | } else { |
| 61 | if (in_array($key, $this->available_properties)) { |
| 62 | $this->properties[$key] = $this->sanitize_val($val, $key); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | public function to_string() { |
| 67 | $rows = $this->build_props(); |
| 68 | return implode("\r\n", $rows); |
| 69 | } |
| 70 | private function build_props() { |
| 71 | // Build ICS properties - add header |
| 72 | $ics_props = array( |
| 73 | 'BEGIN:VCALENDAR', |
| 74 | 'VERSION:2.0', |
| 75 | 'PRODID:-//hacksw/handcal//NONSGML v1.0//EN', |
| 76 | 'CALSCALE:GREGORIAN', |
| 77 | 'BEGIN:VEVENT' |
| 78 | ); |
| 79 | // Build ICS properties - add header |
| 80 | $props = array(); |
| 81 | foreach($this->properties as $k => $v) { |
| 82 | $props[strtoupper($k . ($k === 'url' ? ';VALUE=URI' : ''))] = $v; |
| 83 | } |
| 84 | // Set some default values |
| 85 | $props['DTSTAMP'] = $this->format_timestamp('now'); |
| 86 | $props['UID'] = uniqid(); |
| 87 | $props['X-WR-TIMEZONE'] = OsTimeHelper::get_wp_timezone_name(); |
| 88 | // Append properties |
| 89 | foreach ($props as $k => $v) { |
| 90 | $ics_props[] = "$k:$v"; |
| 91 | } |
| 92 | // Build ICS properties - add footer |
| 93 | $ics_props[] = 'END:VEVENT'; |
| 94 | $ics_props[] = 'END:VCALENDAR'; |
| 95 | return $ics_props; |
| 96 | } |
| 97 | private function sanitize_val($val, $key = false) { |
| 98 | switch($key) { |
| 99 | case 'dtend': |
| 100 | case 'dtstamp': |
| 101 | case 'dtstart': |
| 102 | $val = $this->format_timestamp($val); |
| 103 | break; |
| 104 | default: |
| 105 | $val = $this->escape_string($val); |
| 106 | } |
| 107 | return $val; |
| 108 | } |
| 109 | private function format_timestamp($timestamp) { |
| 110 | $dt = new OsWpDateTime($timestamp); |
| 111 | return $dt->format(self::DT_FORMAT); |
| 112 | } |
| 113 | private function escape_string($str) { |
| 114 | if(empty($str)) return ''; |
| 115 | return preg_replace('/([\,;])/','\\\$1', $str); |
| 116 | } |
| 117 | } |