<?php
/*
Plugin Name: Google Event Cal
Plugin URI: http://calendar.google.com
Description: Widget that displays events from a public google calendar.
Author: Kyle Ellman
Version: 1
Author URI: http://kyleellman.com
*/
function google_event_cal_init()
{
// Check to see required Widget API functions are defined...
if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
return; // ...and if not, exit from the script.
function google_event_cal($args)
{
extract($args);
$options = get_option('google_event_cal');
$gec_title = $options['gec_title'];
$gec_cal_address = $options['gec_cal_address'];
$gec_num_of_events = $options['gec_num_of_events'];
$gec_format = $options['gec_format'];
$gec_events = eventList($gec_cal_address, $gec_format);
echo '<li id="'.$gec_title.'" class="widget widget_'.$gec_title.'" >';
echo '<h2 class="widgettitle">'.$gec_title.'</h2>';
echo '<ul>';
for($num = 0; $num < count($gec_events) && $num < $gec_num_of_events; $num++)
echo '<li>'.$gec_events[$num].'</li>';
echo '</ul>';
echo '</li>';
}
function google_event_cal_control()
{
$options = get_option('google_event_cal');
if ( !is_array($options) )
$options = array('gec_title'=>'' ,'gec_cal_address'=>'','gec_num_of_events'=>'');
if ( $_POST['gec_submit'] )
{
$options['gec_title'] = strip_tags(stripslashes($_POST['gec_title']));
$options['gec_cal_address'] = $_POST['gec_cal_address'];
$gec_num_of_events = (int) $_POST['gec_num_of_events'];
if ( $gec_num_of_events > 25) $gec_num_of_events = 25;
if ( $gec_num_of_events < 1) $gec_num_of_events = 1;
$options['gec_num_of_events'] = $gec_num_of_events;
$options['gec_format'] = $_POST['gec_format'];
update_option('google_event_cal', $options);
}
$gec_title = htmlspecialchars($options['gec_title'], ENT_QUOTES);
$gec_cal_address = $options['gec_cal_address'];
$gec_num_of_events = $options['gec_num_of_events'];
$gec_format = $options['gec_format'];
?>
<style type="text/css">
.gec label, .gec input, .gec select {display: block;}
.gec input, .gec select { margin: 0 0 8px 10px;}
</style>
<?php
echo '<div class="gec"><label for="gec_title">Title:</label> <input id="gec_title" name="gec_title" type="text" value="'.$gec_title.'" /></div>';
echo '<div class="gec"><label for="gec_cal_address">Google Calendar Address*:</label> <input id="gec_cal_address" name="gec_cal_address" type="text" value="'.$gec_cal_address.'" /></div>';
?><div class="gec"><label for="gec_num_of_events">Number of events:</label> <select id="gec_num_of_events" name="gec_num_of_events" value="<?php echo $gec_num_of_events; ?>"><?php for ( $i = 1; $i <= 25; ++$i ) echo "<option value='$i' ".($gec_num_of_events==$i ? "selected='selected'" : '').">$i</option>"; ?></select></div>
<div class="gec"><label for="gec_format">Date Format:</label> <select id="gec_format" name="gec_format" value="<?php echo $gec_format; ?>"><option value="m/d"><?php echo date('m/d'); ?></option><option value="m/d/Y"><?php echo date('m/d/Y'); ?></option><option value="M j"><?php echo date('M j'); ?></option><option value="M j, Y"><?php echo date('M j, Y'); ?></option><option value="D M j"><?php echo date('D M j'); ?></option><option value="D M j, Y"><?php echo date('D M j, Y'); ?></option></select></div>
<?php
echo '<input type="hidden" id="gec_submit" name="gec_submit" value="1" />';
echo '<div style="padding-top:15px;">*The Google Calendar Address can be found under an individual calendar\'s settings. The calendar must be set to public and the public ICAL address must be used.</div';
}
register_sidebar_widget('Google Event Cal', 'google_event_cal');
register_widget_control('Google Event Cal', 'google_event_cal_control', 250, 300);
}
add_action('plugins_loaded', 'google_event_cal_init');
function eventList($icalFile, $dateFormat)
{
$theFile = getPage($icalFile);
//event name
$event_name = extractTag('SUMMARY', $theFile);
for($i = 0; $i < count($event_name); $i++)
{
$event_name[$i] = substr($event_name[$i], 1, strlen($event_name[$i]) - 1);
}
//event link
$event_link = extractTag('DESCRIPTION', $theFile);
for($i = 0; $i < count($event_link); $i++)
{
$event_link[$i] = substr($event_link[$i], 1, strlen($event_link[$i]) - 1);
$description = explode(' ', $event_link[$i]);
for($j = 0; $j < count($description[$j]); $j++)
{
if(substr($description[$j], 0, 4) == 'http')
{
$link = explode('\n', $description[$j]);
$event_link[$i] = $link[0];
break;
}
else
$event_link[$i] = '';
}
if(substr($description[$j], 0, 4) != 'http')
$event_link[$i] = '';
}
//tag to not display
$no_display = extractTag('DESCRIPTION', $theFile);
for($i = 0; $i < count($no_display); $i++)
{
if(strstr(strtolower($no_display[$i]), 'no-wp'))
$no_display[$i] = true;
else
$no_display[$i] = false;
}
//event start date
$event_start = extractTag('DTSTART', $theFile);
//event end date
$event_end = extractTag('DTEND', $theFile);
//standardize start date
for($i = 0; $i < count($event_start); $i++)
{
$date = explode(':', $event_start[$i]);
$event_start[$i] = substr($date[count($date) - 1], 0, 8);
}
//sort events by start date
for($i = count($event_start) - 1; $i > 0; $i--)
{
for($j = 1; $j <= $i; $j++)
{
if($event_start[$j-1] < $event_start[$j])
{
//event name
swap($event_name[$j-1], $event_name[$j]);
//event link
swap($event_link[$j-1], $event_link[$j]);
//tag to not display
swap($no_display[$j-1], $no_display[$j]);
//event start
swap($event_start[$j-1], $event_start[$j]);
//event end
swap($event_end[$j-1], $event_end[$j]);
}
}
}
//construct list of events
$j = 0;
for($i = 0; $i < count($event_name); $i++)
{
if($event_start[$i] >= date('Ymd'))
{
if(!$no_display[$i])
{
if($event_link[$j] == '')
{
$event_list[$j] = event_date($event_start[$i], $event_end[$i], $dateFormat).' - '.$event_name[$i];
}
else
{
$event_list[$j] = event_date($event_start[$i], $event_end[$i], $dateFormat).' - <a href="'.$event_link[$i].'">'.$event_name[$i].'</a>';
}
$j++;
}
}
else
break;
}
//reverse the array so it displays in the correct order
$event_list = array_reverse($event_list);
return $event_list;
}
//format the date
function event_date($start, $end, $dateFormat)
{
$endRaw = $end;
$startStamp = strtotime(substr($start, 6, 2).'-'.substr($start, 4, 2).'-'.substr($start, 0, 4));
$end = explode(':', $endRaw);
$end = substr($end[count($end) - 1], 0, 8);
$endStamp = strtotime(substr($end, 6, 2).'-'.substr($end, 4, 2).'-'.substr($end, 0, 4));
if(substr($endRaw, 0, 11) == ';VALUE=DATE')
{
$end--;
}
if($start == $end)
{
return date($dateFormat, $startStamp);
}
return date($dateFormat, $startStamp).'-'.date($dateFormat, $endStamp);
}
//Extract values of tags in the ical address
function extractTag($tag, $icalFile)
{
$taglength = strlen($tag);
$endPos = strpos($icalFile, 'BEGIN:VEVENT', 0);
for($num = 0; $num < substr_count($icalFile, 'BEGIN:VEVENT', 'BEGIN:VEVENT'); $num++)
{
//$Pos = strpos($icalFile, '<' . $tag, $endPos) + $taglength;
//$endPos = strpos($icalFile, '</' . $tag . '>', $Pos);
$Pos = strpos($icalFile, $tag, $endPos) + $taglength;
$endPos = strpos($icalFile, '
', $Pos);
$data[$num] = substr($icalFile, $Pos, ($endPos - $Pos));
//clean tag data
//$bracketpos = strpos($data[$num], '>');
//$data[$num] = substr($data[$num], $bracketpos + 1);
}
return $data;
}
function getPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$returned = curl_exec($ch);
curl_close ($ch);
return $returned;
}
function swap(&$a, &$b)
{
$temp = $a;
$a = $b;
$b = $temp;
}
?>