Instantly share code, notes, and snippets.
Created
March 22, 2010 01:06
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save bouchard/339702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Gcal Sidebar | |
Plugin URI: http://www.oriontechnologysolutions.com/gcal-sidebar/ | |
Description: Pulls a Google Calendar feed and displays it in your sidebar. | |
Version: 1.0 | |
Author: Orion Technology Solutions | |
Author URI: http://www.oriontechnologysolutions.com | |
*/ | |
/* Copyright 2010 Orion Technology Solutions (email : [email protected]) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
Thank you Justin Bennett fr providing the initial foundation this was built from. | |
*/ | |
class GcalSidebar extends WP_Widget { | |
/** constructor */ | |
function GcalSidebar() { | |
parent::WP_Widget(false, $name = 'GcalSidebar'); | |
} | |
/** @see WP_Widget::widget */ | |
function widget($args, $instance) { | |
extract($args); | |
$feed_id = $instance['feed_id']; | |
$feed_title = $instance['title']; | |
$title_url_option = $instance['title_url_option']; | |
$title = $this->get_title($feed_title, $feed_id, $title_url_option); | |
echo $before_widget; | |
echo $before_title . $title . $after_title; | |
$this->display_feed($feed_id, $instance); | |
echo $after_widget; | |
} | |
/** @see WP_Widget::update */ | |
function update($new_instance, $old_instance) { | |
return $new_instance; | |
} | |
/** @see WP_Widget::form */ | |
function form($instance) { | |
$feed_id = esc_attr($instance['feed_id']); | |
$title = esc_attr($instance['title']); | |
$max_results = esc_attr($instance['max_results']); | |
$rs_offset = esc_attr($instance['rs_offset']); | |
$tz_offset = esc_attr($instance['tz_offset']); | |
$static_url_option = esc_attr($instance['static_url_option']); | |
$static_url = esc_attr($instance['static_url']); | |
$title_url_option = esc_attr($instance['title_url_option']); | |
$title_url = esc_attr($instance['title_url']); | |
$map_link = esc_attr($instance['map_link']); | |
$pub_or_priv = esc_attr($instance['pub_or_priv']); | |
$priv_id = esc_attr($instance['priv_id']); | |
?> | |
<div> | |
<h4>Core</h4> | |
<p><label for="<?php echo $this->get_field_id('feed_id'); ?>"><?php _e('Feed ID:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('feed_id'); ?>" name="<?php echo $this->get_field_name('feed_id'); ?>" type="text" value="<?php echo $feed_id; ?>" /></label> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Calendar Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('title_url_option'); ?>"><?php _e('Title URL:'); ?> | |
<select name="<?php echo $this->get_field_name('title_url_option'); ?>" id="<?php echo $this->get_field_id('title_url_option'); ?>" > | |
<option value="0" <?php echo ($title_url_option == 0) ? 'selected="selected"' : ''; ?>>None</option> | |
<option value="1" <?php echo ($title_url_option == 1) ? 'selected="selected"' : ''; ?>>iCal</option> | |
<option value="2" <?php echo ($title_url_option == 2) ? 'selected="selected"' : ''; ?>>HTML</option> | |
<option value="3" <?php echo ($title_url_option == 3) ? 'selected="selected"' : ''; ?>>Specified</option> | |
</select></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('title_url'); ?>" name="<?php echo $this->get_field_name('title_url'); ?>" type="text" value="<?php echo $title_url; ?>" /> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('max_results'); ?>"><?php _e('Max Results:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('max_results'); ?>" name="<?php echo $this->get_field_name('max_results'); ?>" type="text" value="<?php echo $max_results; ?>" /></label></p> | |
</div> | |
<hr /> | |
<div> | |
<h4>Optional</h4> | |
<p><label for="<?php echo $this->get_field_id('map_link'); ?>"><?php _e('Display link to map:'); ?> | |
<select name="<?php echo $this->get_field_name('map_link'); ?>" id="<?php echo $this->get_field_id('map_link'); ?>" > | |
<option value="0" <?php echo ($map_link == 0) ? 'selected="selected"' : ''; ?>>No</option> | |
<option value="1" <?php echo ($map_link == 1) ? 'selected="selected"' : ''; ?>>Yes</option> | |
</select></label> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('static_url_option'); ?>"><?php _e('Single Event URL:'); ?> | |
<select name="<?php echo $this->get_field_name('static_url_option'); ?>" id="<?php echo $this->get_field_id('static_url_option'); ?>" > | |
<option value="0" <?php echo ($static_url_option == 0) ? 'selected="selected"' : ''; ?>>No</option> | |
<option value="1" <?php echo ($static_url_option == 1) ? 'selected="selected"' : ''; ?>>Yes</option> | |
</select></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('static_url'); ?>" name="<?php echo $this->get_field_name('static_url'); ?>" type="text" value="<?php echo $static_url; ?>" /> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('rs_offset'); ?>"><?php _e('Results Offset:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('rs_offset'); ?>" name="<?php echo $this->get_field_name('rs_offset'); ?>" type="text" value="<?php echo $rs_offset; ?>" /></label></p> | |
<p><label for="<?php echo $this->get_field_id('pub_or_priv'); ?>"><?php _e('Public or Private:'); ?> | |
<select name="<?php echo $this->get_field_name('pub_or_priv'); ?>" id="<?php echo $this->get_field_id('pub_or_priv'); ?>" > | |
<option value="0" <?php echo ($pub_or_priv == 0) ? 'selected="selected"' : ''; ?>>Public</option> | |
<option value="1" <?php echo ($pub_or_priv == 1) ? 'selected="selected"' : ''; ?>>Private</option> | |
</select></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('priv_id'); ?>" name="<?php echo $this->get_field_name('priv_id'); ?>" type="text" value="<?php echo $priv_id; ?>" /> | |
</p> | |
<p><label for="<?php echo $this->get_field_id('tz_offset'); ?>"><?php _e('Timezone Offset:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('tz_offset'); ?>" name="<?php echo $this->get_field_name('tz_offset'); ?>" type="text" value="<?php echo $tz_offset; ?>" /></label></p> | |
</div> | |
<?php | |
} | |
/** | |
* @function get_title | |
* @brief Generates the 'h2' header for the title of the calendar widget | |
* @param $feed_title | |
* $feed_title is the text displayed to the user as the title of the widget | |
* @param $gcal_sidebar_id | |
* $gcal_sidebar_id is the unique ID of the calendar. This is used to build proper | |
* URLs to link to depending on the value of $feed_title_url_option | |
* @param $feed_title_url_option | |
* $feed_title_url_option specifies what the title should link to, | |
* 0 is no link, 1 is ical, 2 is HTML, and 3 is a specified link | |
* | |
* gcal_sidebar_display feed does most of the heavy lifting. It is | |
* responsible for downloading the feed, parsing it, and | |
* generating the proper HTML. | |
* | |
*/ | |
function get_title($feed_title, $gcal_sidebar_id, $feed_title_url_option) { | |
switch($feed_title_url_option) { | |
case 0: | |
$title = $feed_title; | |
break; | |
case 1: | |
$feed_title_url = "http://www.google.com/calendar/ical/" . $gcal_sidebar_id . "/public/basic.ics"; | |
$title = "<a href='" . $feed_title_url . "'>" . $feed_title . "</a>"; | |
break; | |
case 2: | |
$feed_title_url = "http://www.google.com/calendar/embed?src=" . $gcal_sidebar_id; | |
$title = "<a href='" . $feed_title_url . "'>" . $feed_title . "</a>"; | |
break; | |
case 3: | |
$feed_title_url = get_option('gcal_sidebar_title_url'); | |
$title = "<a href='" . $feed_title_url . "'>" . $feed_title . "</a>"; | |
break; | |
} | |
return $title; | |
} | |
/** | |
* @function display_feed | |
* @brief Downloads the Google Calendar and converts to HTML | |
* @param $feed_id | |
* $feed_id is the unique ID for a gcal calendar. It uses this to build | |
* proper URLs for the HTML / ical / XML feeds. | |
* | |
* gcal_sidebar_display feed does most of the heavy lifting. It is | |
* responsible for downloading the feed, parsing it, and | |
* generating the proper HTML. | |
* | |
*/ | |
function display_feed( $feed_id, $instance) { | |
$tz_offset = esc_attr($instance['tz_offset']); | |
$max_results = esc_attr($instance['max_results']); | |
$rs_offset = esc_attr($instance['rs_offset']); | |
$map_url = "http://maps.google.com/maps"; | |
$map_link = esc_attr($instance['map_link']); | |
$pub_or_priv = esc_attr($instance['pub_or_priv']); | |
$priv_id = trim(esc_attr($instance['priv_id'])); | |
if($rs_offset == '') | |
$rs_offset = 0; | |
if($max_results == '') | |
$max_results = 4 + $rs_offset; | |
else | |
$max_results = $max_results + $rs_offset; | |
if ($pub_or_priv == 0) | |
$feed_url = "http://www.google.com/calendar/feeds/" . $feed_id . "/public/full?orderby=starttime&sortorder=ascending&futureevents=true&singleevents=true&max-results=" . $max_results; | |
else $feed_url = "http://www.google.com/calendar/feeds/" . $feed_id . "/private-" . $priv_id . "/full?orderby=starttime&sortorder=ascending&futureevents=true&singleevents=true&max-results=" . $max_results; | |
$xmlstr = wp_remote_fopen($feed_url); | |
$static_url = $instance['static_url_option']; | |
$xml = new SimpleXMLElement($xmlstr); | |
$gcal = $xml->children('http://schemas.google.com/gCal/2005'); | |
$ctz = $gcal->timezone->attributes()->value; | |
if ($tz_offset == '') { | |
$tz_offset = 0; | |
date_default_timezone_set ( $ctz ); | |
} | |
else if((-24 < $tz_offset) && ($tz_offset < 24)) { | |
$tz_offset = $tz_offset * 60 * 60; | |
} | |
echo '<ul id="events">'; | |
$iter = 0; | |
foreach($xml->entry as $entry) { | |
if($iter < $rs_offset) { | |
$iter++; | |
continue; | |
} | |
echo '<li class="event">'; | |
$gd = $entry->children('http://schemas.google.com/g/2005'); | |
if ( $static_url ) { | |
$event_link = get_option('gcal_sidebar_static_url'); | |
} | |
else { | |
$event_link = $entry->link->attributes()->href . "&ctz=" . $ctz; | |
} | |
if(isset($entry->content)) | |
$content = $entry->content; | |
else | |
$content = ""; | |
if(isset($gd->where)) | |
$where = $gd->where->attributes()->valueString; | |
else | |
$where = ""; | |
$tooltip = $where . " - " . $content; | |
if($pub_or_priv == 0) { | |
echo "<a style='display:inline' title='" . $tooltip . "' href='" . $event_link . "'>" . $entry->title . "</a>\n"; | |
} | |
else { | |
echo "<a style='display:inline' title='" . $tooltip . "'>" . $entry->title . "</a>\n"; | |
} | |
if($map_link == 1) { | |
$map_href = $map_url . "?view=map&iwloc=A&q=" . urlencode($where); | |
echo " --> "; | |
echo "<a style='display:inline' title='Map near $where' href='$map_href'>map</a>"; | |
} | |
$event_date = date("l, F j", strtotime($gd->when->attributes()->startTime) + $tz_offset); | |
$start = date("g:ia", strtotime($gd->when->attributes()->startTime) + $tz_offset); | |
$end = date("g:ia", strtotime($gd->when->attributes()->endTime) + $tz_offset); | |
if (date("g:ia", strtotime($gd->when->attributes()->startTime)) == "12:00am" && date("g:ia", strtotime($gd->when->attributes()->endTime)) == "12:00am") { | |
echo "<p class='event_time'>All Day on $event_date</p></li>"; | |
} else { | |
echo "<p class='event_time'>$event_date from $start to $end</p></li>"; | |
} | |
} | |
echo '</ul>'; | |
} | |
} // class GcalSidebar | |
add_action('widgets_init', create_function('', 'return register_widget("GcalSidebar");')); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment