|
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 $i = 1; |
|
55
|
|
|
while ( $i <= 10 ) { ?> |
|
56
|
|
|
<option <?php selected( $instance['count'], $i ) ?>><?php echo $i; ?></option> |
|
57
|
|
|
<?php $i++; } ?> |
|
58
|
|
|
<option value="0" <?php selected( $instance['count'], 0 ) ?>><?php _e( 'All' , 'jetpack' ) ?></option> |
|
59
|
|
|
</select> |
|
60
|
|
|
</p> |
|
61
|
|
|
<?php |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
function update( $new_instance, $old_instance ) { |
|
65
|
|
|
$instance['title'] = strip_tags( $new_instance['title'] ); |
|
|
|
|
|
|
66
|
|
|
$instance['feed-url'] = strip_tags( $new_instance['feed-url'] ); |
|
67
|
|
|
$instance['count'] = min( absint( $new_instance['count'] ), 10 ); // 10 or less |
|
68
|
|
|
return $instance; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function widget( $args, $instance ) { |
|
72
|
|
|
jetpack_require_lib( 'icalendar-reader' ); |
|
73
|
|
|
|
|
74
|
|
|
$ical = new iCalendarReader(); |
|
75
|
|
|
$events = $ical->get_events( $instance['feed-url'], $instance['count'] ); |
|
76
|
|
|
$events = $this->apply_timezone_offset( $events ); |
|
77
|
|
|
$ical->timezone = null; |
|
78
|
|
|
|
|
79
|
|
|
echo $args['before_widget']; |
|
80
|
|
|
if ( ! empty( $instance['title'] ) ) { |
|
81
|
|
|
echo $args['before_title']; |
|
82
|
|
|
echo esc_html( $instance['title'] ); |
|
83
|
|
|
echo $args['after_title']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ( ! $events ) : // nothing to display? |
|
87
|
|
|
?> |
|
88
|
|
|
<p><?php echo __( 'No upcoming events', 'jetpack' ) ?></p> |
|
89
|
|
|
<?php |
|
90
|
|
|
else : |
|
91
|
|
|
?> |
|
92
|
|
|
<ul class="upcoming-events"> |
|
93
|
|
|
<?php foreach ( $events as $event ) : ?> |
|
94
|
|
|
<li> |
|
95
|
|
|
<strong class="event-summary"><?php echo $ical->escape( stripslashes( $event['SUMMARY'] ) ); ?></strong> |
|
96
|
|
|
<span class="event-when"><?php echo $ical->formatted_date( $event ); ?></span> |
|
97
|
|
View Code Duplication |
<?php if ( ! empty( $event['LOCATION'] ) ) : ?> |
|
98
|
|
|
<span class="event-location"><?php echo $ical->escape( stripslashes( $event['LOCATION'] ) ); ?></span> |
|
99
|
|
|
<?php endif; ?> |
|
100
|
|
View Code Duplication |
<?php if ( ! empty( $event['DESCRIPTION'] ) ) : ?> |
|
101
|
|
|
<span class="event-description"><?php echo wp_trim_words( $ical->escape( stripcslashes( $event['DESCRIPTION'] ) ) ); ?></span> |
|
102
|
|
|
<?php endif; ?> |
|
103
|
|
|
</li> |
|
104
|
|
|
<?php endforeach; ?> |
|
105
|
|
|
</ul> |
|
106
|
|
|
<?php |
|
107
|
|
|
endif; |
|
108
|
|
|
|
|
109
|
|
|
echo $args['after_widget']; |
|
110
|
|
|
|
|
111
|
|
|
/** This action is documented in modules/widgets/gravatar-profile.php */ |
|
112
|
|
|
do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
function apply_timezone_offset( $events ) { |
|
116
|
|
|
if ( ! $events ) { |
|
117
|
|
|
return $events; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// get timezone offset from the timezone name. |
|
121
|
|
|
$timezone_name = get_option( 'timezone_string' ); |
|
122
|
|
|
if ( $timezone_name ) { |
|
123
|
|
|
$timezone = new DateTimeZone( $timezone_name ); |
|
124
|
|
|
$offset = $timezone->getOffset( new DateTime( 'now', new DateTimeZone( 'UTC' ) ) ); |
|
125
|
|
|
} else { |
|
126
|
|
|
// fallback - gmt_offset option |
|
127
|
|
|
$offset = get_option( 'gmt_offset' ) * 3600; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// generate a DateInterval object from the timezone offset |
|
131
|
|
|
$interval_string = sprintf( '%d minutes', $offset / 60 ); |
|
132
|
|
|
$interval = date_interval_create_from_date_string( $interval_string ); |
|
133
|
|
|
|
|
134
|
|
|
$offsetted_events = array(); |
|
135
|
|
|
|
|
136
|
|
|
foreach ( $events as $event ) { |
|
137
|
|
|
// Don't handle all-day events |
|
138
|
|
|
if ( 8 < strlen( $event['DTSTART'] ) ) { |
|
139
|
|
|
$start_time = new DateTime( $event['DTSTART'] ); |
|
140
|
|
|
$start_time->add( $interval ); |
|
141
|
|
|
$end_time = new DateTime( $event['DTEND'] ); |
|
142
|
|
|
$end_time->add( $interval ); |
|
143
|
|
|
|
|
144
|
|
|
$event['DTSTART'] = $start_time->format( 'YmdHis\Z' ); |
|
145
|
|
|
$event['DTEND'] = $end_time->format( 'YmdHis\Z' ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$offsetted_events[] = $event; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $offsetted_events; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
function upcoming_events_register_widgets() { |
|
156
|
|
|
register_widget( 'Jetpack_Upcoming_Events_Widget' ); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
add_action( 'widgets_init', 'upcoming_events_register_widgets' ); |
|
160
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.