activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
1 week ago
auth_helper.php
6 days ago
blocks_helper.php
3 weeks ago
booking_helper.php
4 days ago
bricks_helper.php
3 months ago
bundles_helper.php
4 days ago
calendar_helper.php
3 days ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
1 month ago
customer_import_helper.php
1 month ago
database_helper.php
1 week ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 weeks ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 weeks ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
2 months ago
orders_helper.php
12 hours ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
12 hours ago
plugin_version_update_helper.php
2 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
razorpay_connect_helper.php
1 week ago
replacer_helper.php
3 months ago
resource_helper.php
4 days ago
roles_helper.php
3 weeks ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
1 week ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 weeks ago
sms_helper.php
3 months ago
steps_helper.php
12 hours ago
stripe_connect_helper.php
1 week ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
2 months ago
timeline_helper.php
2 months ago
transaction_helper.php
1 month ago
transaction_intent_helper.php
3 months ago
util_helper.php
1 month ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
1 month ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
icalendar_helper.php
120 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 ) ) { |
| 115 | return ''; |
| 116 | } |
| 117 | return preg_replace( '/([\,;])/', '\\\$1', $str ); |
| 118 | } |
| 119 | } |
| 120 |