| 1 |
<?php |
| 2 |
|
| 3 |
class Jetpack_Upcoming_Events_Widget extends WP_Widget { |
| 4 |
function __construct() { |
| 5 |
parent::__construct( |
| 6 |
'upcoming_events_widget', |
| 7 |
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 8 |
apply_filters( 'jetpack_widget_name', __( 'Upcoming Events', 'jetpack' ) ), |
| 9 |
array( |
| 10 |
'description' => __( 'Display upcoming events from an iCalendar feed.', 'jetpack' ), |
| 11 |
'customize_selective_refresh' => true, |
| 12 |
) |
| 13 |
); |
| 14 |
if ( is_active_widget( false, false, $this->id_base ) ) { |
| 15 |
add_action( 'wp_head', array( $this, 'css' ) ); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
function css() { |
| 20 |
?> |
| 21 |
<style type="text/css"> |
| 22 |
.upcoming-events li { |
| 23 |
margin-bottom: 10px; |
| 24 |
} |
| 25 |
.upcoming-events li span { |
| 26 |
display: block; |
| 27 |
} |
| 28 |
</style> |
| 29 |
<?php |
| 30 |
} |
| 31 |
|
| 32 |
function form( $instance ) { |
| 33 |
$defaults = array( |
| 34 |
'title' => __( 'Upcoming Events', 'jetpack' ), |
| 35 |
'feed-url' => '', |
| 36 |
'count' => 3, |
| 37 |
); |
| 38 |
$instance = array_merge( $defaults, (array) $instance ); |
| 39 |
?> |
| 40 |
|
| 41 |
<p> |
| 42 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label> |
| 43 |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
| 44 |
</p> |
| 45 |
|
| 46 |
<p> |
| 47 |
<label for="<?php echo $this->get_field_id( 'feed-url' ); ?>"><?php _e( 'iCalendar Feed URL:', 'jetpack' ); ?></label> |
| 48 |
<input class="widefat" id="<?php echo $this->get_field_id( 'feed-url' ); ?>" name="<?php echo $this->get_field_name( 'feed-url' ); ?>" type="text" value="<?php echo esc_attr( $instance['feed-url'] ); ?>" /> |
| 49 |
</p> |
| 50 |
|
| 51 |
<p> |
| 52 |
<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Items to show:', 'jetpack' ); ?></label> |
| 53 |
<select id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"> |
| 54 |
<?php |
| 55 |
$i = 1; |
| 56 |
while ( $i <= 10 ) { |
| 57 |
?> |
| 58 |
<option <?php selected( $instance['count'], $i ); ?>><?php echo $i; ?></option> |
| 59 |
<?php $i++; } ?> |
| 60 |
<option value="0" <?php selected( $instance['count'], 0 ); ?>><?php _e( 'All', 'jetpack' ); ?></option> |
| 61 |
</select> |
| 62 |
</p> |
| 63 |
<?php |
| 64 |
} |
| 65 |
|
| 66 |
function update( $new_instance, $old_instance ) { |
| 67 |
$instance['title'] = strip_tags( $new_instance['title'] ); |
| 68 |
$instance['feed-url'] = strip_tags( $new_instance['feed-url'] ); |
| 69 |
$instance['count'] = min( absint( $new_instance['count'] ), 10 ); // 10 or less |
| 70 |
return $instance; |
| 71 |
} |
| 72 |
|
| 73 |
function widget( $args, $instance ) { |
| 74 |
jetpack_require_lib( 'icalendar-reader' ); |
| 75 |
|
| 76 |
$ical = new iCalendarReader(); |
| 77 |
$events = $ical->get_events( $instance['feed-url'], $instance['count'] ); |
| 78 |
$events = $this->apply_timezone_offset( $events ); |
| 79 |
$ical->timezone = null; |
| 80 |
|
| 81 |
echo $args['before_widget']; |
| 82 |
if ( ! empty( $instance['title'] ) ) { |
| 83 |
echo $args['before_title']; |
| 84 |
echo esc_html( $instance['title'] ); |
| 85 |
echo $args['after_title']; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! $events ) : // nothing to display? |
| 89 |
?> |
| 90 |
<p><?php echo __( 'No upcoming events', 'jetpack' ); ?></p> |
| 91 |
<?php |
| 92 |
else : |
| 93 |
?> |
| 94 |
<ul class="upcoming-events"> |
| 95 |
<?php foreach ( $events as $event ) : ?> |
| 96 |
<li> |
| 97 |
<strong class="event-summary"><?php echo $ical->escape( stripslashes( $event['SUMMARY'] ) ); ?></strong> |
| 98 |
<span class="event-when"><?php echo $ical->formatted_date( $event ); ?></span> |
| 99 |
<?php if ( ! empty( $event['LOCATION'] ) ) : ?> |
| 100 |
<span class="event-location"><?php echo $ical->escape( stripslashes( $event['LOCATION'] ) ); ?></span> |
| 101 |
<?php endif; ?> |
| 102 |
<?php if ( ! empty( $event['DESCRIPTION'] ) ) : ?> |
| 103 |
<span class="event-description"><?php echo wp_trim_words( $ical->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); ?></span> |
| 104 |
<?php endif; ?> |
| 105 |
</li> |
| 106 |
<?php endforeach; ?> |
| 107 |
</ul> |
| 108 |
<?php |
| 109 |
endif; |
| 110 |
|
| 111 |
echo $args['after_widget']; |
| 112 |
|
| 113 |
/** This action is documented in modules/widgets/gravatar-profile.php */ |
| 114 |
do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' ); |
| 115 |
} |
| 116 |
|
| 117 |
// Left this function here for backward compatibility |
| 118 |
// just incase a site using jetpack is also using this function |
| 119 |
function apply_timezone_offset( $events ) { |
| 120 |
jetpack_require_lib( 'icalendar-reader' ); |
| 121 |
|
| 122 |
$ical = new iCalendarReader(); |
| 123 |
return $ical->apply_timezone_offset( $events ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
function upcoming_events_register_widgets() { |
| 128 |
register_widget( 'Jetpack_Upcoming_Events_Widget' ); |
| 129 |
} |
| 130 |
|
| 131 |
add_action( 'widgets_init', 'upcoming_events_register_widgets' ); |
| 132 |
|