Skip to content

Instantly share code, notes, and snippets.

@samrae
Last active November 16, 2018 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samrae/87f43ef1e98a16b7d4199e6157794550 to your computer and use it in GitHub Desktop.
Save samrae/87f43ef1e98a16b7d4199e6157794550 to your computer and use it in GitHub Desktop.
/*
* eventer_save_events function
* This function is used to save event in Google, icalendar etc.
*/
if(!function_exists('eventer_save_events'))
{
function eventer_save_events()
{
//date_default_timezone_set('Antarctica/Troll');
$query_string = base64_decode($_SERVER['QUERY_STRING']);
parse_str($query_string);
if(isset($action) && isset($id) && isset($key) && $key == 'imic_save_event')
{
$custom_post = get_post($id);
$title = $custom_post->post_title;
$content = $custom_post->post_content;
$excerpt = $custom_post->post_excerpt; // Added by samrae.
if(!empty($excerpt)) { // Added by samrae. Makes the excerpt the content if it exists.
$content = $excerpt;
}
//$imic_event_address = get_post_meta($id, 'imic_event_address', true); // Removed by samrae. This does not work. I think it's code from another of IMIThemes' themes.
/* -- samrae added — Get correct address details. Taken and modified from event-metas.php -- */
$eventer_venue = get_the_terms($id, 'eventer-venue');
$elocation = '';
if(!is_wp_error($eventer_venue)&&!empty($eventer_venue))
{
foreach($eventer_venue as $venue)
{
$location_address = get_term_meta($venue->term_id, 'venue_address', true);
$location_coordinates = get_term_meta($venue->term_id, 'venue_coordinates', true);
$venue_name = $venue->name;
if($location_address!='')
{
$elocation = $location_address;
}
else
{
$elocation = $venue->name;
}
}
}
$imic_event_address = $elocation;
/* -- End samrae added -- */
$eventStartDate = get_post_meta($id, 'eventer_event_start_dt', true);
$eventEndDate = get_post_meta($id, 'eventer_event_end_dt', true);
$all_day = get_post_meta($id, 'eventer_event_all_day', true);
$eventer_timezone = eventer_get_settings( 'eventer_save_event' );
$random_name = substr(rand().rand().rand().rand(),0,20);
$start_dt_str = strtotime($eventStartDate);
$end_dt_str = strtotime($eventEndDate);
// $triggerOn_start = $edate.' '.date_i18n('H:i', $start_dt_str); // Removed by samrae. Assumes same start and end date.
// $triggerOn_end = $edate.' '.date_i18n('H:i', $end_dt_str); // Removed by samrae. Assumes same start and end date.
$triggerOn_start = date_i18n('Y-m-d H:i', $start_dt_str); // Added by samrae. Caters for different start and end dates.
$triggerOn_end = date_i18n('Y-m-d H:i', $end_dt_str); // Added by samrae. Caters for different start and end dates.
$event_url = get_permalink($id); // Added by samrae. Adds the event url to the .ics file.
switch($action)
{
case 'gcalendar' :
$google_save_url = 'https://www.google.com/calendar/render?action=TEMPLATE';
if($all_day)
{
$google_save_url .= '&dates='.date_i18n("Ymd",strtotime("$triggerOn_start"));
$google_save_url .= '/'.date_i18n("Ymd",strtotime("$triggerOn_end"));
}
else
{
if($eventer_timezone=="on")
{
$google_save_url .= '&dates='.date_i18n("Ymd\THis",strtotime("$triggerOn_start"));
$google_save_url .= '/'.date_i18n("Ymd\THis",strtotime("$triggerOn_end"));
}
else
{
$google_save_url .= '&dates='.date_i18n("Ymd\THis\Z",strtotime("$triggerOn_start"));
$google_save_url .= '/'.date_i18n("Ymd\THis\Z",strtotime("$triggerOn_end"));
}
}
$google_save_url .= '&location='.urlencode($imic_event_address);
$google_save_url .= '&text='.urlencode($title);
//$google_save_url .= '&ctz=Antarctica/Troll';
$google_save_url .= '&details='.urlencode($content);
wp_redirect($google_save_url); exit;
break;
case 'icalendar' :
ob_start();
header("Content-Type: text/calendar; charset=utf-8");
header("Content-Disposition: inline; filename=addto_calendar_".$random_name.".ics");
// $title = addslashes($title); // Removed by samrae. Doesn't work for iCal.
// $title = str_replace(array(",",":",";"),array("\,","\:","\;"),$title); // Removed by samrae. Doesn't work for iCal.
$paragraph_breaks = array('</p>','<br />','<br>','<hr />','<hr>','</h1>','</h2>','</h3>','</h4>','</h5>','</h6>','<ul>'); // Added by samrae
$content = str_replace($paragraph_breaks,"\\n\\n",$content); // Added by samrae. Replaces headings and line breaks with a string double line break.
$list_starts = array('<li>'); // Added by samrae
$content = str_replace($list_starts,"- ",$content); // Added by samrae. Replaces list start with a line break.
$list_ends = array('</li>','</ul>'); // Added by samrae.
$content = str_replace($list_ends,"\\n",$content); // Added by samrae. Replaces list end with a line break.
$content = wp_strip_all_tags($content); // Added by samrae. Strips html.
$content = preg_replace('`\[[^\]]*\]`','',$content); // Added by samrae. Removes shortcodes.
// $content = addslashes($content); // Removed by samrae. Stops line breaks being passed. Not needed.
// $content = str_replace(array(",",":",";"),array("\,","\:","\;"),$content); //Removed by samrae. Doesn't work for iCal.
$content = preg_replace('/\s+/',' ', $content);
$imic_event_address = addslashes($imic_event_address);
$imic_event_address = str_replace(array(",",":",";"),array("\,","\:","\;"),$imic_event_address);
echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID:Imitheme.com \n";
echo "BEGIN:VEVENT\n";
echo "UID:".date_i18n('Ymd').'T'.date_i18n('His').rand()."\n";
echo "DTSTAMP;TZID=UTC:".current_time()."\n";
if($eventer_timezone=="on")
{
echo "DTSTART:".date_i18n("Ymd\THis",strtotime("$triggerOn_start"))."\n";
echo "DTEND:".date_i18n("Ymd\THis",strtotime("$triggerOn_end"))."\n";
}
else
{
echo "DTSTART;TZID=UTC:".date_i18n("Ymd\THis",strtotime("$triggerOn_start"))."\n";
echo "DTEND;TZID=UTC:".date_i18n("Ymd\THis",strtotime("$triggerOn_end"))."\n";
}
echo "SUMMARY:$title\n";
echo "LOCATION:$imic_event_address\n";
echo "DESCRIPTION:$content\n";
echo "URL:$event_url\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
ob_flush();
exit;
break;
case 'outlook' :
ob_start();
header("Content-Type: text/calendar; charset=utf-8");
header("Content-Disposition: inline; filename=addto_calendar_".$random_name.".ics");
echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID:Imitheme.com\n";
echo "BEGIN:VEVENT\n";
echo "UID:".date_i18n('Ymd').'T'.date_i18n('His')."-".rand()."\n";
echo "DTSTAMP:".date_i18n('Ymd').'T'.date_i18n('His')."\n";
if($eventer_timezone=="on")
{
echo "DTSTART:".date_i18n("Ymd\THis",strtotime("$eventStartDate"))."\n";
echo "DTEND:".date("Ymd\THis",strtotime("$eventEndDate"))."\n";
}
else
{
echo "DTSTART:".date_i18n("Ymd\THis\Z",strtotime("$eventStartDate"))."\n";
echo "DTEND:".date("Ymd\THis\Z",strtotime("$eventEndDate"))."\n";
}
echo "SUMMARY:$title\n";
echo "LOCATION:$imic_event_address\n";
echo "DESCRIPTION: $content\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
ob_flush();
exit;
break;
case 'outlooklive' :
$outlooklive_url = 'https://bay03.calendar.live.com/calendar/calendar.aspx?rru=addevent';
$outlooklive_url .= '&summary='.urlencode($title);
$outlooklive_url .= '&location='.urlencode($imic_event_address);
$outlooklive_url .= '&description='.urlencode($content);
if($eventer_timezone=="on")
{
$outlooklive_url .= '&dtstart='.date_i18n("Ymd\THis",strtotime("$eventStartDate"));
$outlooklive_url .= '&dtend='.date_i18n("Ymd\THis",strtotime("$eventEndDate"));
}
else
{
$outlooklive_url .= '&dtstart='.date_i18n("Ymd\THis\Z",strtotime("$eventStartDate"));
$outlooklive_url .= '&dtend='.date_i18n("Ymd\THis\Z",strtotime("$eventEndDate"));
}
wp_redirect($outlooklive_url); exit;
break;
case 'yahoo' :
$yahoo_url = 'https://calendar.yahoo.com/?view=d&v=60&type=20';
$yahoo_url .= '&title='.urlencode($title);
$yahoo_url .= '&in_loc='.urlencode($imic_event_address);
$yahoo_url .= '&desc='.urlencode($content);
if($eventer_timezone=="on")
{
$yahoo_url .= '&st='.date_i18n("Ymd\THis",strtotime("$eventStartDate"));
$yahoo_url .= '&et='.date_i18n("Ymd\THis",strtotime("$eventEndDate"));
}
else
{
$yahoo_url .= '&st='.date_i18n("Ymd\THis\Z",strtotime("$eventStartDate"));
$yahoo_url .= '&et='.date_i18n("Ymd\THis\Z",strtotime("$eventEndDate"));
}
wp_redirect($yahoo_url); exit;
break;
}
}
}
}
@samrae
Copy link
Author

samrae commented Nov 16, 2018

Added:

  • Now checks for an excerpt. If an excerpt exists, that will be the description in the .ics file. If not, the content field is used (and cleaned up).
  • Permalink to the post now passed into the .ics (for iCalendar only—I can't test the others)

Known issues:

  • When using an excerpt for the Description, the Description ends up having no line breaks. I'm not sure why this is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment