Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LiveWhale Calendar Widget
<?php
/*
Plugin Name: LiveWhale Calendar Widget (TAMU)
Description: WordPress widget to display events from a LiveWhale calendar feed.
Author: Stephanie Leary
Version: 1.0
Author URI: http://stephanieleary.com
*/
// initialize all custom widgets
function scl_widgets_init() {
register_widget( 'SCL_LiveWhale_Calendar_Widget' );
}
add_action( 'widgets_init', 'scl_widgets_init', 99 );
class SCL_LiveWhale_Calendar_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array( 'description' => esc_html__( 'Add a calendar from LiveWhale.', 'tees' ) );
parent::__construct( 'livewhale_calendar', esc_html__( 'Calendar (LiveWhale)', 'tees'), $widget_ops );
}
public function widget( $args, $instance ) {
if ( empty( $instance['calendar_id'] ) )
return;
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $args['before_widget'];
if ( !empty( $instance['title'] ) )
echo $args['before_title'] . $instance['title'] . $args['after_title'];
$extra_args = '';
$transient_name = 'lwc2'; // LWCal + widget ID, will add options to this name below. "lwc" for "LiveWhale Calendar" because the max length on these is 64 characters.
$find = array( ' ',',','&' );
$replace = array( '_','','_' );
if ( !empty( $instance['groups'] ) ) {
$extra_args .= '/group/' . rawurlencode( trim( $instance['groups'] ) );
$transient_name .= '_group_'.str_replace( $find, $replace, strtolower( trim( $instance['groups'] ) ));
}
else {
$transient_name .= '_allgroups';
}
if ( $instance['max_events'] ) {
$extra_args .= '/max/' . trim( $instance['max_events'] );
$transient_name .= '_max' . trim( $instance['max_events'] );
}
if ( $instance['tags'] ) {
$events_tags = explode( ',', $instance['tags'] );
$transient_name .= '_tags';
foreach( $events_tags as $tag ){
$extra_args .= '/tag/' . rawurlencode( trim( $tag ) );
$transient_name .= '_' . str_replace( $find, $replace, trim( $tag ) );
}
}
$transient_name = substr( $transient_name, 0, 64 ); // transient names limited to 64 characters, so let's not go over.
// Check the transient, and if it's expired/empty, refresh it:
$events_transient = get_transient( $transient_name );
if ( empty( $events_transient ) ) {
$url = 'http://calendar.tamu.edu/live/widget/' . $instance['calendar_id'] . $extra_args . '?external_widget=1';
$response = wp_remote_retrieve_body( wp_remote_get( $url ) );
if ( !is_wp_error( $response ) ) {
echo wp_kses( $response );
set_transient( $transient_name, $response, HOUR_IN_SECONDS );
}
}
else {
echo wp_kses( $events_transient );
}
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ) {
$instance = array();
if ( ! empty( $new_instance['calendar_id'] ) ) {
$instance['calendar_id'] = absint( $new_instance['calendar_id'] );
}
if ( ! empty( $new_instance['title'] ) ) {
$instance['title'] = sanitize_text_field( stripslashes( $new_instance['title'] ) );
}
if ( ! empty( $new_instance['groups'] ) ) {
$instance['groups'] = sanitize_text_field( stripslashes( $new_instance['groups'] ) );
}
if ( ! empty( $new_instance['tags'] ) ) {
$instance['tags'] = sanitize_text_field( stripslashes( $new_instance['tags'] ) );
}
$instance['max_events'] = absint( $new_instance['max_events'] );
return $instance;
}
public function form( $instance ) {
$id = isset( $instance['calendar_id'] ) ? $instance['calendar_id'] : '';
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$tags = isset( $instance['tags'] ) ? $instance['tags'] : '';
$groups = isset( $instance['groups'] ) ? $instance['groups'] : '';
$max = isset( $instance['max_events'] ) ? $instance['max_events'] : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'tees' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'calendar_id' ); ?>"><?php esc_html_e( 'Calendar ID:', 'tees' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'calendar_id' ); ?>" name="<?php echo $this->get_field_name( 'calendar_id' ); ?>" value="<?php echo esc_attr( $id ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'groups' ); ?>"><?php esc_html_e( 'Groups:', 'tees' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'groups' ); ?>" name="<?php echo $this->get_field_name( 'groups' ); ?>" value="<?php echo esc_attr( $groups ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'tags' ); ?>"><?php esc_html_e( 'Tags:', 'tees' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'tags' ); ?>" name="<?php echo $this->get_field_name( 'tags' ); ?>" value="<?php echo esc_attr( $tags ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'max_events' ); ?>"><?php esc_html_e( 'Number of Events to Display:', 'tees' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'max_events' ); ?>" name="<?php echo $this->get_field_name( 'max_events' ); ?>" value="<?php echo esc_attr( $max ); ?>"/>
</p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.