forked from whitkirkchurch/include-churchsuite-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude-churchsuite-events.php
256 lines (214 loc) · 7.94 KB
/
include-churchsuite-events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/*
Plugin Name: Include ChurchSuite Events
Plugin URI: https://github.com/whitkirkchurch/include-churchsuite-events
Description: Gets a list of events from a ChurchSuite site, and includes it as part of a post or page.
Version: 1.1
Author: St Mary's Church, Whitkirk
Author URI: https://whitkirkchurch.org.uk
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
function cs_events_shortcode($atts = [])
{
if (isset($atts['site'])) {
$site_id = $atts['site'];
$base_url =
'https://' . $site_id . '.churchsuite.co.uk/embed/calendar/json';
unset($atts['site']);
} else {
return 'Missing "site" parameter!';
}
if (isset($atts['link_titles'])) {
$link_titles = (bool) $atts['link_titles'];
unset($atts['link_titles']);
} else {
$link_titles = false;
}
if (isset($atts['show_years'])) {
$show_years = $atts['show_years'];
unset($atts['show_years']);
} else {
$show_years = false;
}
if (isset($atts['show_date'])) {
$show_date = $atts['show_date'];
unset($atts['show_date']);
} else {
$show_date = true;
}
if (isset($atts['show_end_times'])) {
$show_end_times = (bool) $atts['show_end_times'];
unset($atts['show_end_times']);
} else {
$show_end_times = false;
}
if (isset($atts['show_locations'])) {
$show_locations = (bool) $atts['show_locations'];
unset($atts['show_locations']);
} else {
$show_locations = false;
}
if (isset($atts['show_descriptions'])) {
$show_descriptions = (bool) $atts['show_descriptions'];
unset($atts['show_descriptions']);
} else {
$show_descriptions = true;
}
try {
$params = [];
foreach ($atts as $attribute => $value) {
$params[$attribute] = $value;
}
$params_string = http_build_query($params);
$query_url = $base_url . '?' . $params_string;
$json = file_get_contents($query_url);
$data = json_decode($json);
$output = '<div class="cs_events--dateblock">';
$last_date = null;
date_default_timezone_set('Europe/London');
// This is where most of the magic happens
foreach ($data as $event) {
// Build the event URL, we use this a couple of times
$event_url =
'https://' .
$site_id .
'.churchsuite.co.uk/events/' .
$event->identifier;
// Build the object for the JSON-LD representation
$json_ld = [
'@context' => 'http://www.schema.org',
'@type' => 'Event',
'name' => $event->name,
'url' => $event_url,
'description' => $event->description,
'startDate' => date(
DATE_ISO8601,
strtotime($event->datetime_start)
),
'endDate' => date(
DATE_ISO8601,
strtotime($event->datetime_end)
),
];
// Tack on the image, if we have one
if (isset($event->images->lg)) {
$json_ld['image'] = $event->images->lg->url;
}
// Set attendance mode
if ($event->location->type == 'online') {
$json_ld['eventAttendanceMode'] =
'https://schema.org/OnlineEventAttendanceMode';
$json_ld['location'] = [
'@type' => 'VirtualLocation',
'url' => $event->location->url,
];
} else {
$json_ld['eventAttendanceMode'] =
'https://schema.org/OfflineEventAttendanceMode';
$json_ld['location'] = [
'@type' => 'Place',
'name' => $event->location->name,
'address' => [
'@type' => 'PostalAddress',
'postalCode' => $event->location->address,
],
];
}
// Flag cancelled events
if ($event->status == 'cancelled') {
$json_ld['eventStatus'] = 'https://schema.org/EventCancelled';
} else {
$json_ld['eventStatus'] = 'https://schema.org/EventScheduled';
}
// And output it!
$output .=
'<script type="application/ld+json">' .
json_encode($json_ld) .
'</script>';
// Turn the time into an actual object
$start_time = strtotime($event->datetime_start);
$date = date('Y-m-d', $start_time);
// Make sure we only show the date once per day
if ($date != $last_date && $show_date) {
$last_date = $date;
$output .= '</div><div class="cs_events--dateblock">';
$output .=
'<h3 class="cs_events--date">' . date('l j F', $start_time);
if (
$show_years and
($show_years == 'always' or
$show_years == 'different' and
date('Y', $start_time) != date('Y'))
) {
$output .= ' ' . date('Y', $start_time);
}
$output .= '</h3>';
}
$output .= '<div class="cs_events--event">';
if (isset($event->images->thumb) && $event->description != '') {
if ($link_titles == true) {
$output .=
'<a href="https://' .
$site_id .
'.churchsuite.co.uk/events/' .
$event->identifier .
'">';
}
$output .=
'<img class="cs_events--event--image hidden-xs hidden-sm" src="' .
$event->images->thumb->url .
'">';
if ($link_titles == true) {
$output .= '</a>';
}
}
// Output the event title
$output .= '<h4 class="cs_events--event--title">';
if ($event->status == 'cancelled') {
$output .= '<span style="text-decoration:line-through">';
}
if ($link_titles == true) {
$output .= '<a href="' . $event_url . '">';
}
$output .=
'<span class="cs_events--event--time">' .
date('g.i a', $start_time);
if ($show_end_times) {
$output .=
' – ' .
date('g.i a', strtotime($event->datetime_end));
}
$output .=
'</span><span class="cs_events--event--name">' .
$event->name .
'</span>';
if ($link_titles == true) {
$output .= '</a>';
}
if ($event->status == 'cancelled') {
$output .= '</span>';
}
$output .= '</h4>';
if ($event->status == 'cancelled') {
$output .=
'<p><strong>This event has been cancelled.</strong></p>';
}
if ($show_locations && $event->location->name) {
$output .=
'<p><i>' .
htmlspecialchars_decode($event->location->name) .
'</i></p>';
}
if ($show_descriptions and $event->description != '') {
$output .= htmlspecialchars_decode($event->description);
}
$output .= '<div style="clear:both"></div></div>';
}
$output .= '</div>';
return $output;
} catch (Exception $e) {
return $e->getMessage();
}
}
add_shortcode('churchsuite_events', 'cs_events_shortcode');