Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
65 changes: 60 additions & 5 deletions includes/functions/shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Member

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

Screenshot 2023-03-20 at 8 37 03 AM

(purple is ICS export and yellow is Google calendar)

$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";
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -287,11 +342,11 @@ function simcal_default_event_template() {

$content = '<strong>' . '[title]' . '</strong>';
$content .= "\n\n";
$content .= '[when]' . "\n";
$content .= '[when]' . "\r\n";
Copy link
Member

Choose a reason for hiding this comment

The 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 );
}
Expand Down Expand Up @@ -485,4 +540,4 @@ function simcal_delete_feed_transients( $id = '' ) {
}

return delete_transient( '_simple-calendar_feed_ids' );
}
}
9 changes: 9 additions & 0 deletions includes/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ public function load_admin() {
}
}

public function simcal_ics_action() {
Copy link
Member

Choose a reason for hiding this comment

The 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.
*
Expand All @@ -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 );
}

/**
Expand Down