-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmoa-calendar.php
688 lines (588 loc) · 19.2 KB
/
cmoa-calendar.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<?php
error_reporting(E_ERROR);
date_default_timezone_set('America/New_York');
class CMOA_Calendar {
protected $_timezone = 'America/New_York';
function __construct() {
global $ai1ec_registry;
$this->ai1ec_registry = $ai1ec_registry;
}
function get_calendar_timezone() {
return $this->_timezone;
}
/*
* calendar_dates
*
* This function will calculate the range of dates to be shown on the calendar
* based on the current date.
*
* @type function
* @date 03/21/16
* @since 1.0.0
*
* @param $date (DateTime from which to generate calendar)
* @return [[
* "date"=>string('Y-m-d'),
* "events" => [[id, post_id, start, end, post_content, post_title,
* event_start, event_end, venue, country, address,
* city, province, postal_code ]]
* ]] (array of objects containing date and events)
*/
function calendar_dates($date = null)
{
if (!isset($date)) $date = new DateTime('', new DateTimeZone($this->_timezone));
$first_day = clone $date;
$first_day->modify('first day of this month')->modify('last Sunday');
$last_day = clone $date;
$last_day->modify('last day of this month')->modify('next Sunday')->modify("-1 second");
return $this->generate_event_list($first_day, $last_day);
}
/*
* calendar_range
*
* This function provides the events occuring over a date range.
*
* @type function
* @date 03/21/16
* @since 1.0.0
*
* @param $start, $end (DateTime from which to generate calendar)
* @return [[
* "date"=>string('Y-m-d'),
* "events" => [[id, post_id, start, end, post_content, post_title,
* event_start, event_end, venue, country, address,
* city, province, postal_code ]]
* ]] (array of objects containing date and events)
*/
function calendar_range($start, $end) {
$start->setTime(0,0,0);
$end->setTime(23,59,59);
return $this->generate_event_list($start, $end);
}
/*
* events_in_category
*
* This function provides the events occuring in a particular category
*
* @type function
* @date 04/12/16
* @since 1.0.0
*
* @param $slug (category slug)
* @return [
* "events" => [[id, post_id, start, end, post_content, post_title,
* event_start, event_end, venue, country, address,
* city, province, postal_code ]]
* ] (array of events)
*/
function events_in_category($slug) {
$start_date = new DateTime('now');
$end_date = new DateTime('+1 year');
$category = get_term_by('slug', $slug, 'events_categories');
$filters['cat_ids'][] = $category->term_id;
$events = $this->get_event_between_dates($start_date->format('U'), $end_date->format('U'), $filters);
$unique_events = $this->unique_events($events);
return $this->format_events($unique_events);
}
/*
* generate_event_list
*
* This function will generate an array of events over a specified period
*
* @type function
* @date 03/22/16
* @since 1.0.0
*
* @param first_day (DateTime), last_day (DateTime)
* @return [[
* "date"=>string('Y-m-d'),
* "events" => [[id, post_id, start, end, post_content, post_title,
* event_start, event_end, venue, country, address,
* city, province, postal_code ]]
* ]] (array of objects containing date and events)
*/
private function generate_event_list($first_day, $last_day) {
$period = new DatePeriod(
$first_day,
new DateInterval('P1D'),
$last_day
);
$events = $this->get_event_between_dates($first_day->getTimestamp(), $last_day->getTimestamp());
$event_list = [];
foreach ($period as $value) {
// The start and end of `this` day
$day_start = $value->getTimestamp();
$day_end = $day_start + 60 * 60 * 24 - 1;
$es = array_filter($events, function($event) use ($day_start, $day_end) {
// The start and end of the `event`
$start = $event->get('start')->format('U');
$end = $event->get('end')->format('U');
// If the event starts on this day, include it
if ($start >= $day_start && $start <= $day_end) return true;
// If the event ends on this day, include it
// this is causing duplicate events to appear in the list... do we need it?
// if ($end <= $day_end && $end >= $day_start) return true;
// If the event runs through this day, include it
if ($start <= $day_start && $end >= $day_end) return true;
return false;
});
$es = array_values($es);
if (!empty($es)) $es = $this->format_events($es);
array_push($event_list, ["date" => $value->format('Y-m-d'), "events" => $es]);
}
return $event_list;
}
/*
* get_event_between_dates
*
* This function will query the ai1ec events between two timestamps
*
* @type function
* @date 03/22/16
* @since 1.0.0
*
* @param start (timestamp), end (timestamp)
* @return query results [[post_id, post_content, post_title, start, end]]
*/
public function get_event_between_dates($start = 0, $end = 9999999999, $filters = []) {
$start_time = new Ai1ec_Date_Time($this->ai1ec_registry, $start, $this->get_calendar_timezone());
$end_time = new Ai1ec_Date_Time($this->ai1ec_registry, $end, $this->get_calendar_timezone());
$search = $this->ai1ec_registry->get('model.search');
$events = $search->get_events_between(
$start_time,
$end_time,
$filters, // array of category/tag ids
true
);
$visible_events = array_filter($events, function($event) {
return !get_field('hidden', $event->get('post_id'));
});
return $visible_events;
}
/*
* format_events
*
* This function accepts an array of events and returns with desired structure
*
* @type function
* @date 03/22/16
* @since 1.0.0
*
* @param $events (array of events)
* @return events array with desired structure
*/
function format_events($events) {
$event_list = [];
$tz = new DateTimeZone($this->_timezone);
foreach ($events as $event) {
$post = $event->get('post');
$event_instances = $this->event_instances($post->ID);
$created_date = NULL;
if (isset($post->post_date)) {
$created_date = str_replace(" ", "T", $post->post_date);
}
$excerpt = get_the_excerpt($post);
if(empty($excerpt)) {
$excerpt = get_field('excerpt', $post);
}
// yowza, double reduce with a foreach!
$sponsor_list = array_reduce(get_field('sponsorship_sections', $post->ID), function($sponsor_arr, $sponsorship_section) {
$sponsors = array_reduce($sponsorship_section['sponsor_layout'], function($section_arr, $layout) {
if($layout['acf_fc_layout'] == 'sponsor_text') {
$section_arr[] = ['description' => $layout['sponsorship_details']];
}
elseif($layout['acf_fc_layout'] == 'sponsor_logos') {
foreach($layout['sponsors'] as $logo) {
list($src, $width, $height) = wp_get_attachment_image_src($logo['image'], 'large');
$section_arr[] = [
'name' => $logo['sponsor_name'],
'image' => $src,
'website' => $logo['website']
];
}
}
return $section_arr;
}, []);
$section = [
'headline' => $sponsorship_section['sponsorship_headline'],
'sponsor_list' => $sponsors
];
$sponsor_arr[] = $section;
return $sponsor_arr;
}, []);
// should we exclude some of these fields to decrease the payload size?
$e = [
'id' => $post->ID,
'categories' => wp_get_post_terms($post->ID, 'events_categories'),
'tags' => wp_get_post_terms($post->ID, 'events_tags'),
'name' => $post->post_title,
'url' => get_permalink($post),
'excerpt' => $excerpt,
'description' => $post->post_content,
'start' => $event->get('start')->format('c'),
'end' => $event->get('end')->format('c'),
'start_date' => DateTime::createFromFormat("U", $event_instances[0]->start)->setTimezone($tz)->format('c'),
'end_date' => DateTime::createFromFormat("U", end($event_instances)->end)->setTimezone($tz)->format('c'),
'all_day' => $event->get('allday'),
'created_date' => $created_date,
'duration' => $this->friendly_time_between($event->get('start')->format('U'), $event->get('end')->format('U')),
'locations' => wp_get_post_terms($post->ID, 'locations'),
'address' => $event->get('address'),
'status' => $event->get('event_status') ?: 'confirmed',
'recurrence' => $event->get('recurrence_rules'),
'images' => [
'banner' => get_field('banner_image', $post),
'featured' => wp_get_attachment_image_src(get_post_thumbnail_id($post), 'large'),
],
'sponsors' => $sponsor_list,
'building_location' => $event->get('venue'),
'ticketing' => [
'availability' => get_field('ticket_availability', $post),
'cost' => $event->get('ticket_cost'),
'url' => $event->get('ticket_url')
],
'registration' => get_field('registration_information')
];
$event_list[] = $e;
}
usort($event_list, array('self', 'sort_events_by_hour'));
/**
* Filters the list of events, for other sites to add additional fields.
*
* @since 1.2.5
*
* @param $event_list Formatted array of event data.
*/
apply_filters( 'carnegie_format_events_for_rest_api', $event_list );
return $event_list;
}
/*
* sort_events_by_hour
*
* This function sorts an array of events by formatting the start date to hours
*
* @type function
* @date 03/25/16
* @since 1.0.0
*
* @param $e1, $e2 (associative arrays)
* @return 0 if events are on the same hour, -1 if the $e1 is first, 1 if $e2 is first
*/
private static function sort_events_by_hour($e1, $e2) {
$e1_date = DateTime::createFromFormat("Y-m-d\TH:i:sP", $e1['start_date']);
$e2_date = DateTime::createFromFormat("Y-m-d\TH:i:sP", $e2['start_date']);
if ($e1_date->format('YmdG') == $e2_date->format('YmdG')) {
return 0;
}
return ($e1_date->format('YmdG') < $e2_date->format('YmdG')) ? -1 : 1;
}
/*
* friendly_time_between
*
* This function returns the time between two dates in a readable format
*
* @type function
* @date 03/21/16
* @since 1.0.0
*
* @param $start, $end (timestamps)
* @return (string)
*/
private function friendly_time_between($start, $end) {
if (!isset($start) || !isset($end)) return "0 seconds";
$day = 60 * 60 * 24;
$hour = 60 * 60;
$minute = 60;
$time = $end - $start;
$days = floor($time / $day);
$time -= $days * $day;
$hours = floor($time / $hour);
$time -= $hours * $hour;
$minutes = floor($time / $minute);
$time -= $minutes * $minute;
$seconds = $time;
$duration = "";
if ($days > 0) {
$duration .= "$days";
$duration .= ($days > 1) ? " days " : " day ";
}
if ($hours > 0) {
$duration .= "$hours";
$duration .= ($hours > 1) ? " hours " : " hour ";
}
if ($minutes > 0) {
$duration .= "$minutes";
$duration .= ($minutes > 1) ? " minutes " : " minute ";
}
if ($seconds > 0) {
$duration .= "$seconds";
$duration .= ($secounds > 1) ? " seconds " : " second ";
}
return trim($duration);
}
/*
* calendar_categories
*
* This function returns all of the events_categories taxonomy terms
*
* @type function
* @date 08/25/16
* @since 1.0.1
*
* @return (array)
*/
public function calendar_categories() {
$categories = get_terms(['taxonomy' => 'events_categories', 'hide_empty' => false]);
$visible_categories = array_filter($categories, function($category) {
return !get_field('hidden', $category);
});
array_walk($visible_categories, function(&$category, $key) {
$category->name = wp_specialchars_decode($category->name);
});
return array_values($visible_categories);
}
/*
* event_instances
*
* This function returns all event instances for a given post
*
* @type function
* @date 08/25/16
* @since 1.0.0
*
* @param $post_id (int)
* @return (array)
*/
public function event_instances($post_id) {
global $wpdb;
$instances_table_name = $wpdb->prefix . 'ai1ec_event_instances';
$query = "SELECT id, start, end ".
"FROM {$instances_table_name} " .
"WHERE post_id = %d";
$args = array(
$post_id
);
$results = $wpdb->get_results($wpdb->prepare($query, $args));
return $results;
}
/*
* all_upcoming_events
*
* This function returns all upcoming events
*
* @type function
* @date 08/25/16
* @since 1.0.0
*
* @param $ai1ec_registry (All-in-One Event Calendar plugin registry object)
* @return (array)
*/
public function all_upcoming_events($filters = []) {
$start_date = new DateTime('now');
$end_date = new DateTime('+1 year');
return $this->get_event_between_dates($start_date->format('U'), $end_date->format('U'), $filters);
}
/*
* unique_events
*
* This function returns unique events for a given post
*
* @type function
* @date 09/13/16
* @since 1.0.0
*
* @param $events (Array of All-in-One Event objects)
* @return (array)
*/
public function unique_events($events) {
$unique_events = array_reduce($events, function($arr, $event) {
if(!isset($arr[$event->get('post_id')])) {
$arr[$event->get('post_id')] = $event;
}
return $arr;
}, []);
return array_values($unique_events);
}
/*
* related_events
*
* This function returns related events for a given event object
*
* @type function
* @date 12/09/16
* @since 1.2.0
* @version 2.0.0
*
* @param $post (Post object)
* @return (array)
*/
public function related_events($post, $include_expired=false, $sort_by_date=true) {
$related = get_field('related_items', $post) ?: [];
$related_events = array_filter($related, function($related_event) {
return get_post_type($related_event) == 'ai1ec_event' && 'publish' == $related_event->post_status;
});
$cmoa_events = array_map(function($event) {
$cmoa_event = new CMOA_Event($event);
return $cmoa_event;
}, $related_events);
if($sort_by_date) {
usort($cmoa_events, function($event1, $event2) {
if ($event1->start_date_iso() == $event2->start_date_iso()) {
return 0;
}
return ($event1->start_date_iso() < $event2->start_date_iso()) ? -1 : 1;
});
}
if(!$include_expired) {
$now = new DateTime();
$cmoa_events = array_filter($cmoa_events, function($cmoa_event) use ($now) {
return $cmoa_event->last_event_end()->format('U') > $now->format('U');
});
}
return $cmoa_events;
}
/*
* related_exhibitions
*
* This function returns related exhibitions for a given event object
*
* @type function
* @date 12/09/16
* @since 1.2.0
* @version 4.0.0
*
* @param $post (Post object)
* @return (array)
*/
public function related_exhibitions($post, $sort_by_date=true) {
$related = get_field('related_items', $post) ?: [];
$related_exhibitions = array_filter($related, function($related_exhibition) {
return get_post_type($related_exhibition) == 'exhibition' && 'publish' == $related_exhibition->post_status;
});
$related_exhibitions = array_filter($related_exhibitions, function($related_exhibition) {
return get_field('start_date', $related_exhibition->ID) || get_field('lead_message', $related_exhibition->ID);
});
if($sort_by_date) {
usort($related_exhibitions, function($ex1, $ex2) {
$start_date1 = DateTime::createFromFormat('Ymd', get_field('start_date', $ex1));
$start_date2 = DateTime::createFromFormat('Ymd', get_field('start_date', $ex2));
if ($start_date1 == $start_date2) {
return 0;
}
return ($start_date1 < $start_date2) ? -1 : 1;
});
}
return $related_exhibitions;
}
}
/* Routes */
/*
* calendar_dates_api
*
* This function is called from the API route and passes the year and month
* for the desired calendar.
*
* @type function
* @date 03/21/16
* @since 1.0.0
*
* @param $args (year and month parsed from URL params)
* @return JSON object representing event
*/
function calendar_dates_api($args) {
$cal = new CMOA_Calendar();
$year = (isset($args['year'])) ? $args['year'] : date("Y");
$month = (isset($args['month'])) ? $args['month'] : date("m");
$date = new DateTime("{$year}-{$month}-01", new DateTimeZone($cal->get_calendar_timezone()));
return $cal->calendar_dates($date);
}
add_action('rest_api_init', function () {
register_rest_route('events/v1', '/year/(?P<year>\d+)/month/(?P<month>\d+)', array(
'methods' => 'GET',
'callback' => 'calendar_dates_api'
));
});
/*
* calendar_range_api
*
* This function is called from the API route and passes the start and end dates
* in Ymd format for calendar events.
*
* @type function
* @date 03/21/16
* @since 1.0.0
*
* @param $args (start and end parsed from URL params in Ymd format)
* @return JSON object representing event
*/
function calendar_range_api($args) {
$cal = new CMOA_Calendar();
$start = (DateTime::createFromFormat('Ymd', $args['start'])) ?: new DateTime("now", new DateTimeZone($cal->get_calendar_timezone()));
$end = (DateTime::createFromFormat('Ymd', $args['end'])) ?: new DateTime("+30 days", new DateTimeZone($cal->get_calendar_timezone()));
return $cal->calendar_range($start, $end);
}
add_action('rest_api_init', function () {
register_rest_route('events/v1', '/start/(?P<start>\d+)/end/(?P<end>\d+)', array(
'methods' => 'GET',
'callback' => 'calendar_range_api'
));
});
/*
* calendar_upcoming_api
*
* This function is called from the API route and passes the current date to
* show all upcoming events.
*
* @type function
* @date 02/2/17
* @since 1.2.5
*
* @return JSON object representing event
*/
function calendar_upcoming_api() {
$cal = new CMOA_Calendar();
$events = $cal->all_upcoming_events();
// Filter down to unique events
$unique_events = $cal->unique_events($events);
// Format events
$formatted_events = $cal->format_events($unique_events);
return $formatted_events;
}
add_action('rest_api_init', function () {
register_rest_route('events/v1', '/upcoming/', array(
'methods' => 'GET',
'callback' => 'calendar_upcoming_api'
));
});
/*
* calendar_categories_api
*
* This function is called from the API route and returns the current event categories
*
* @type function
* @date 07/27/16
* @since 1.0.0
*
* @return JSON object representing categories
*/
function calendar_categories_api() {
$cal = new CMOA_Calendar();
return $cal->calendar_categories();
}
add_action('rest_api_init', function () {
register_rest_route('events/v1', '/categories/', array(
'methods' => 'GET',
'callback' => 'calendar_categories_api'
));
});
function calendar_category_events_api($args) {
$cal = new CMOA_Calendar();
return $cal->events_in_category($args['slug']);
}
add_action('rest_api_init', function () {
register_rest_route('events/v1', '/categories/(?P<slug>[\w-]+)', array(
'methods' => 'GET',
'callback' => 'calendar_category_events_api'
));
});
?>