-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to get a iCalendar format feed from any calendar #349
base: main
Are you sure you want to change the base?
Changes from all commits
44f5da2
a5703ad
407c104
5b86498
03d2450
42aba84
e1e13eb
f45a59a
bf9effa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,62 @@ function simcal_get_calendar( $object ) { | |
$objects = \SimpleCalendar\plugin()->objects; | ||
return $objects instanceof \SimpleCalendar\Objects ? $objects->get_calendar( $object ) : null; | ||
} | ||
/** | ||
* Formats strings to ical standard | ||
* | ||
* @param string $s | ||
* | ||
* @return string | ||
*/ | ||
function format_ical_string( $s ) { | ||
$r = wordwrap( | ||
preg_replace( | ||
array( '/,/', '/;/', '/[\r\n]/' ), | ||
array( '\,', '\;', '\n' ), | ||
$s | ||
), 73, "\n", TRUE | ||
); | ||
|
||
// Indent all lines but first: | ||
$r = preg_replace( '/\n/', "\n ", $r ); | ||
|
||
return $r; | ||
} | ||
|
||
|
||
/** | ||
* Compose and return a ics feed from the calendar | ||
* @param string|int|object|WP_Post $object | ||
* @return void | ||
* | ||
*/ | ||
function simcal_draw_calendar_ics($post) { | ||
$calendar = simcal_get_calendar($post); | ||
$events = $calendar->events; | ||
header('Content-Type:text/calendar'); | ||
$ics = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Wordpress/Simple-Calendar\r\n"; | ||
|
||
foreach($events as $eventarr) { | ||
$event = $eventarr[0]; | ||
if(strpos($ics, $event->uid) == false) { | ||
$ics .= "BEGIN:VEVENT\r\n"; | ||
$ics .="UID:" . $event->uid . "\r\n"; | ||
$ics .="DTSTAMP:" . $event->start_dt->format('Ymd\THis') . "\r\n"; | ||
$ics .= "ORGANIZER;CN=" . $event->source . ":MAILTO:".$event->source."\r\n"; | ||
$ics .= "DTSTART:" . $event->start_dt->format('Ymd\THis') . "\r\n"; | ||
$ics .= "DTEND:" . $event->end_dt->format('Ymd\THis') . "\r\n"; | ||
$ics.= "SUMMARY:" . format_ical_string($event->title) . "\r\n"; | ||
$ics .= "DESCRIPTION:" . format_ical_string($event->description) . "\r\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would nice to check if the description is present, if not we can skip adding the description. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for LOCATION and GEO. |
||
$ics .= "LOCATION:" . $event->start_location["name"] .';' . $event->start_location["address"] ."\r\n"; | ||
$ics .= "GEO:" . $event->start_location["lat"] . ';' . $event->start_location["lng"] . "\r\n"; | ||
$ics .= "END:VEVENT\r\n"; | ||
} | ||
|
||
} | ||
|
||
$ics .= 'END:VCALENDAR'; | ||
die($ics); | ||
} | ||
|
||
/** | ||
* Get a calendar view. | ||
|
@@ -140,7 +196,6 @@ function simcal_get_calendar_view( $id = 0, $name = '' ) { | |
function simcal_print_calendar( $object ) { | ||
|
||
$calendar = simcal_get_calendar( $object ); | ||
|
||
if ( $calendar instanceof \SimpleCalendar\Abstracts\Calendar ) { | ||
$calendar->html(); | ||
} | ||
|
@@ -287,11 +342,11 @@ function simcal_default_event_template() { | |
|
||
$content = '<strong>' . '[title]' . '</strong>'; | ||
$content .= "\n\n"; | ||
$content .= '[when]' . "\n"; | ||
$content .= '[when]' . "\r\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are these changes for? |
||
$content .= '[location]'; | ||
$content .= "\n"; | ||
$content .= "\r\n"; | ||
$content .= '<div>' . '[description]' . '</div>'; | ||
$content .= "\n" . '[link newwindow="yes"]' . __( 'See more details', 'google-calendar-events' ) . '[/link]'; | ||
$content .= "\r\n" . '[link newwindow="yes"]' . __( 'See more details', 'google-calendar-events' ) . '[/link]'; | ||
|
||
return apply_filters( 'simcal_default_event_template', $content ); | ||
} | ||
|
@@ -485,4 +540,4 @@ function simcal_delete_feed_transients( $id = '' ) { | |
} | ||
|
||
return delete_transient( '_simple-calendar_feed_ids' ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,13 @@ public function load_admin() { | |
} | ||
} | ||
|
||
public function simcal_ics_action() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix the formatting, a tab is missing. |
||
global $post; | ||
if(isset($post) && $post->post_type == "calendar" && isset($_GET["ics"]) && $_GET["ics"] == true) { | ||
simcal_draw_calendar_ics($post); | ||
} | ||
} | ||
|
||
/** | ||
* Init plugin when WordPress initializes. | ||
* | ||
|
@@ -198,6 +205,8 @@ public function init() { | |
|
||
// Upon init action hook. | ||
do_action( 'simcal_init' ); | ||
|
||
add_action('template_redirect',array($this, 'simcal_ics_action'), 10 ); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that the timings are not matching, maybe because of timezone
(purple is ICS export and yellow is Google calendar)