Skip to content

Instantly share code, notes, and snippets.

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 stephenharris/1a2c7f024f15b278e121 to your computer and use it in GitHub Desktop.
Save stephenharris/1a2c7f024f15b278e121 to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: Event Organiser iCal feed work around
* Version: 1.0
* License: GNU General Public License, v2 (or newer)
* License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Description: Implements a work around for feeds with invalid DTSTART/DTEND properties.
*/
/**
* This plug-in provides a fix for when an iCal feed contains incorrectly formatted DTSTART
* or DTEND properties for its events.
*
* Specifically this is for use with feeds where the VALUE modifier is not set correctly. For all
* day events, the start date should (for example) be given as
*
* DTSART;VALUE=DATE:20153101
*
* and NOT
*
* DTSART:20153101
*
* This will typically result in a error: "[Line X] Invalid datetime “20153101″. Date expected in YYYYMMDDTHHiissZ or YYYYMMDDTHHiiss format.
* This is intended as workaround, and you should encourage the source of the iCal feed to correct the mistake.
**/
/**
* IMPORTANT: Requires Event Organiser 2.10.0 (or better)
**/
add_filter( 'eventorganiser_pre_ical_property_dtstart', 'my_alter_parsed_date', 10, 5 );
add_filter( 'eventorganiser_pre_ical_property_dtend', 'my_alter_parsed_date', 10, 5 );
function my_alter_parsed_date( $skip, $value, $modifier, $ical_parser, $property ){
$meta = false;
$date_tz = false;
//Parse any modifiers
if( !empty( $modifiers ) ):
foreach( $modifiers as $modifier ):
if ( stristr( $modifier, 'TZID' ) ){
$date_tz = $ical_parser->parse_timezone( substr( $modifier, 5 ) );
}elseif( stristr( $modifier, 'VALUE' ) ){
$meta = substr( $modifier, 6 );
}
endforeach;
endif;
//Set timezone
if( empty( $date_tz ) )
$date_tz = $ical_parser->calendar_timezone;
//Parse date / date-time of property
if( $meta === 'DATE' ):
$date = $ical_parser->parse_ical_date( $value );
$allday = 1;
else:
try{
$date = $ical_parser->parse_ical_datetime( $value, $date_tz );
$allday = 0;
}catch( Exception $e ){
$date = $ical_parser->parse_ical_date( $value );
$allday = 1;
}
endif;
//Set date / date-time of property
if( !empty( $date ) ){
switch( $property ):
case'DTSTART':
$ical_parser->current_event['start'] = $date;
$ical_parser->current_event['all_day'] = $allday;
break;
case 'DTEND':
if( $allday == 1 )
$date->modify('-1 second');
$ical_parser->current_event['end'] = $date;
break;
endswitch;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment