my-calendar-widgets.php
32.5 KB
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
<?php
/**
* Construct widgets. Incorporate widget classes & supporting widget functions.
*
* @category Calendar
* @package My Calendar
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-calendar/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
include( dirname( __FILE__ ) . '/includes/widgets/class-my-calendar-simple-search.php' );
include( dirname( __FILE__ ) . '/includes/widgets/class-my-calendar-filters.php' );
include( dirname( __FILE__ ) . '/includes/widgets/class-my-calendar-today-widget.php' );
include( dirname( __FILE__ ) . '/includes/widgets/class-my-calendar-upcoming-widget.php' );
include( dirname( __FILE__ ) . '/includes/widgets/class-my-calendar-mini-widget.php' );
/**
* Generate the widget output for upcoming events.
*
* @param array $args Event selection arguments.
*
* @return String HTML output list.
*/
function my_calendar_upcoming_events( $args ) {
$language = isset( $args['language'] ) ? $args['language'] : '';
$switched = '';
if ( $language ) {
$locale = get_locale();
$switched = mc_switch_language( $locale, $language );
}
$before = ( isset( $args['before'] ) ) ? $args['before'] : 'default';
$after = ( isset( $args['after'] ) ) ? $args['after'] : 'default';
$type = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
$category = ( isset( $args['category'] ) ) ? $args['category'] : 'default';
$template = ( isset( $args['template'] ) ) ? $args['template'] : 'default';
$substitute = ( isset( $args['fallback'] ) ) ? $args['fallback'] : '';
$order = ( isset( $args['order'] ) ) ? $args['order'] : 'asc';
$skip = ( isset( $args['skip'] ) ) ? $args['skip'] : 0;
$show_today = ( isset( $args['show_today'] ) ) ? $args['show_today'] : 'yes';
$show_recurring = ( isset( $args['show_recurring'] ) ) ? $args['show_recurring'] : 'yes';
$author = ( isset( $args['author'] ) ) ? $args['author'] : 'default';
$host = ( isset( $args['host'] ) ) ? $args['host'] : 'default';
$ltype = ( isset( $args['ltype'] ) ) ? $args['ltype'] : '';
$lvalue = ( isset( $args['lvalue'] ) ) ? $args['lvalue'] : '';
$from = ( isset( $args['from'] ) ) ? $args['from'] : '';
$to = ( isset( $args['to'] ) ) ? $args['to'] : '';
$site = ( isset( $args['site'] ) ) ? $args['site'] : false;
if ( $site ) {
$site = ( 'global' === $site ) ? BLOG_ID_CURRENT_SITE : $site;
switch_to_blog( $site );
}
$hash = md5( implode( ',', $args ) );
$output = '';
$defaults = mc_widget_defaults();
$display_type = ( 'default' === $type ) ? $defaults['upcoming']['type'] : $type;
$display_type = ( '' === $display_type ) ? 'events' : $display_type;
// Get number of units we should go into the future.
$after = ( 'default' === $after ) ? $defaults['upcoming']['after'] : $after;
$after = ( '' === $after ) ? 10 : $after;
// Get number of units we should go into the past.
$before = ( 'default' === $before ) ? $defaults['upcoming']['before'] : $before;
$before = ( '' === $before ) ? 0 : $before;
$category = ( 'default' === $category ) ? '' : $category;
/**
* Pass a custom template to the upcoming events list. Template can either be a template key referencing a stored template or a template pattern using {} template tags.
*
* @hook mc_upcoming_events_template
*
* @param {string} $template Un-parsed template.
*
* @return {string} Template string.
*/
$template = apply_filters( 'mc_upcoming_events_template', $template );
// allow reference by file to external template.
if ( '' !== $template && mc_file_exists( $template ) ) {
$template = file_get_contents( mc_get_file( $template ) );
}
$template = ( ! $template || 'default' === $template ) ? $defaults['upcoming']['template'] : $template;
if ( mc_key_exists( $template ) ) {
$template = mc_get_custom_template( $template );
}
$no_event_text = ( '' === $substitute ) ? $defaults['upcoming']['text'] : $substitute;
$lang = ( $switched ) ? ' lang="' . esc_attr( $switched ) . '"' : '';
$header = "<ul id='upcoming-events-$hash' class='upcoming-events'$lang>";
$footer = '</ul>';
$display_events = ( 'events' === $display_type || 'event' === $display_type ) ? true : false;
if ( ! $display_events ) {
$temp_array = array();
if ( 'days' === $display_type ) {
$from = mc_date( 'Y-m-d', strtotime( "-$before days" ), false );
$to = mc_date( 'Y-m-d', strtotime( "+$after days" ), false );
}
if ( 'month' === $display_type ) {
$from = mc_date( 'Y-m-1' );
$to = mc_date( 'Y-m-t' );
}
if ( 'custom' === $display_type && '' !== $from && '' !== $to ) {
$from = mc_date( 'Y-m-d', strtotime( $from ), false );
$to = ( 'today' === $to ) ? current_time( 'Y-m-d' ) : mc_date( 'Y-m-d', strtotime( $to ), false );
}
/* Yes, this is crude. But there are only 12 possibilities, after all. */
if ( 'month+1' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+1 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+1 month' ), false );
}
if ( 'month+2' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+2 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+2 month' ), false );
}
if ( 'month+3' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+3 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+3 month' ), false );
}
if ( 'month+4' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+4 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+4 month' ), false );
}
if ( 'month+5' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+5 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+5 month' ), false );
}
if ( 'month+6' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+6 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+6 month' ), false );
}
if ( 'month+7' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+7 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+7 month' ), false );
}
if ( 'month+8' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+8 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+8 month' ), false );
}
if ( 'month+9' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+9 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+9 month' ), false );
}
if ( 'month+10' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+10 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+10 month' ), false );
}
if ( 'month+11' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+11 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+11 month' ), false );
}
if ( 'month+12' === $display_type ) {
$from = mc_date( 'Y-m-1', strtotime( '+12 month' ), false );
$to = mc_date( 'Y-m-t', strtotime( '+12 month' ), false );
}
if ( 'year' === $display_type ) {
$from = mc_date( 'Y-1-1' );
$to = mc_date( 'Y-12-31' );
}
/**
* Custom upcoming events date start value for upcoming events lists using date parameters.
*
* @hook mc_upcoming_date_from
*
* @param {string} $from Starting date for this list of upcoming events in Y-m-d format.
* @param {array} $args Associative array holding the arguments used to generate this list of upcoming events.
*
* @return {string} List starting date.
*/
$from = apply_filters( 'mc_upcoming_date_from', $from, $args );
/**
* Custom upcoming events date end value for upcoming events lists using date parameters.
*
* @hook mc_upcoming_date_to
*
* @param {string} $to Ending date for this list of upcoming events in Y-m-d format.
* @param {array} $args Associative array holding the arguments used to generate this list of upcoming events.
*
* @return {string} List ending date.
*/
$to = apply_filters( 'mc_upcoming_date_to', $to, $args );
$query = array(
'from' => $from,
'to' => $to,
'category' => $category,
'ltype' => $ltype,
'lvalue' => $lvalue,
'author' => $author,
'host' => $host,
'search' => '',
'source' => 'upcoming',
'site' => $site,
);
/**
* Modify the arguments used to generate upcoming events.
*
* @hook mc_upcoming_attributes
*
* @param {array} $query All arguments used to generate this list.
* @param {array} $args Subset of parameters used to generate this list's ID hash.
*
* @return {array} Array of event listing arguments.
*/
$query = apply_filters( 'mc_upcoming_attributes', $query, $args );
$event_array = my_calendar_events( $query );
if ( 0 !== count( $event_array ) ) {
foreach ( $event_array as $key => $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $k => $v ) {
if ( mc_private_event( $v ) ) {
// this event is private.
} else {
$temp_array[] = $v;
}
}
}
}
}
$i = 0;
$last_item = '';
$skips = array();
$omit = array();
foreach ( reverse_array( $temp_array, true, $order ) as $event ) {
$details = mc_create_tags( $event );
/**
* Draw a custom template for upcoming events. Returning any non-empty string short circuits other template settings.
*
* @hook mc_draw_upcoming_event
*
* @param {string} $item Empty string before event template is drawn.
* @param {array} $details Associative array of event template tags.
* @param {string} $template Template string passed from widget or shortcode.
* @param {array} $args Associative array holding the arguments used to generate this list of upcoming events.
*
* @return {string} Event template details.
*/
$item = apply_filters( 'mc_draw_upcoming_event', '', $details, $template, $args );
// if an event is a multidate group, only display first found.
if ( in_array( $event->event_group_id, $omit, true ) ) {
continue;
}
if ( '1' === $event->event_span ) {
$omit[] = $event->event_group_id;
}
if ( '' === $item ) {
$item = mc_draw_template( $details, $template, 'list', $event );
}
if ( $i < $skip && 0 !== $skip ) {
$i ++;
} else {
$today = current_time( 'Y-m-d H:i' );
$date = mc_date( 'Y-m-d H:i', strtotime( $details['dtstart'], false ) );
$classes = mc_event_classes( $event, 'upcoming' );
$prepend = "<li class='$classes'>";
$append = '</li>';
/**
* Opening elements for upcoming events list items. Default `<li class="$classes">`.
*
* @hook mc_event_upcoming_before
*
* @param {string} $append Template HTML closing tag.
* @param {string} $classes Space-separated list of classes for the event.
* @param {string} $date Event date in Y-m-d H:i:s format.
*
* @return {string} HTML following each event in upcoming events lists.
*/
$prepend = apply_filters( 'mc_event_upcoming_before', $prepend, $classes, $date );
/**
* Closing elements for upcoming events list items. Default `</li>`.
*
* @hook mc_event_upcoming_after
*
* @param {string} $append Template HTML closing tag.
* @param {string} $classes Space-separated list of classes for the event.
* @param {string} $date Event date in Y-m-d H:i:s format.
*
* @return {string} HTML following each event in upcoming events lists.
*/
$append = apply_filters( 'mc_event_upcoming_after', $append, $classes, $date );
// Recurring events should only appear once.
if ( ! in_array( $details['dateid'], $skips, true ) ) {
$output .= ( $item === $last_item ) ? '' : $prepend . $item . $append;
}
}
$skips[] = $details['dateid']; // Prevent the same event from showing more than once.
$last_item = $item;
}
} else {
$query = array(
'category' => $category,
'before' => $before,
'after' => $after,
'today' => $show_today,
'author' => $author,
'host' => $host,
'ltype' => $ltype,
'lvalue' => $lvalue,
'site' => $site,
);
$events = mc_get_all_events( $query );
$holidays = mc_get_all_holidays( $before, $after, $show_today );
$holiday_array = mc_set_date_array( $holidays );
if ( is_array( $events ) && ! empty( $events ) ) {
$event_array = mc_set_date_array( $events );
if ( is_array( $holidays ) && count( $holidays ) > 0 ) {
$event_array = mc_holiday_limit( $event_array, $holiday_array ); // if there are holidays, filter results.
}
}
if ( ! empty( $event_array ) ) {
$output .= mc_produce_upcoming_events( $event_array, $template, 'list', $order, $skip, $before, $after, $show_today, $show_recurring );
} else {
$output = '';
}
}
if ( '' !== $output ) {
/**
* Replace the list header for upcoming events lists. Default value `<ul id='upcoming-events-$hash' class='upcoming-events'$lang>`.
*
* @hook mc_upcoming_events_header
*
* @param {string} $header Existing upcoming events header HTML.
*
* @return {string} List header HTML.
*/
$header = apply_filters( 'mc_upcoming_events_header', $header );
/**
* Replace the list footer for upcoming events lists. Default value `</ul>`.
*
* @hook mc_upcoming_events_footer
*
* @param {string} $header Existing upcoming events footer HTML.
*
* @return {string} List header HTML.
*/
$footer = apply_filters( 'mc_upcoming_events_footer', $footer );
$output = $header . $output . $footer;
$return = mc_run_shortcodes( $output );
} else {
$return = '<div class="no-events-fallback upcoming-events">' . stripcslashes( $no_event_text ) . '</div>';
}
if ( $site ) {
restore_current_blog();
}
if ( $language ) {
mc_switch_language( $language, $locale );
}
return $return;
}
/**
* For a set of grouped events, get the total time spanned by the group of events.
*
* @param int $group_id Event Group ID.
*
* @return array beginning and ending dates
*/
function mc_span_time( $group_id ) {
$mcdb = mc_is_remote_db();
$group_id = (int) $group_id;
$dates = $mcdb->get_results( $mcdb->prepare( 'SELECT event_begin, event_time, event_end, event_endtime FROM ' . my_calendar_table() . ' WHERE event_group_id = %d ORDER BY event_begin ASC', $group_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$count = count( $dates );
$last = $count - 1;
$begin = $dates[0]->event_begin . ' ' . $dates[0]->event_time;
$end = $dates[ $last ]->event_end . ' ' . $dates[ $last ]->event_endtime;
return array( $begin, $end );
}
/**
* Generates the list of upcoming events when counting by events rather than a date pattern
*
* @param array $events (Array of events to analyze).
* @param string $template Custom template to use for display.
* @param string $type Usually 'list', but also RSS or export.
* @param string $order 'asc' or 'desc'.
* @param int $skip Number of events to skip over.
* @param int $before How many past events to show.
* @param int $after How many future events to show.
* @param string $show_today 'yes' (anything else is false); whether to include events happening today.
* @param string $show_recurring 'yes', show all recurring events. Else, first only.
* @param string $context Display context.
*
* @return string; HTML output of list
*/
function mc_produce_upcoming_events( $events, $template, $type = 'list', $order = 'asc', $skip = 0, $before = 0, $after = 3, $show_today = 'yes', $show_recurring = 'yes', $context = 'filters' ) {
// $events has +5 before and +5 after if those values are non-zero.
// $events equals array of events based on before/after queries. Nothing skipped, order is not set, holiday conflicts removed.
$output = array();
$near_events = array();
$temp_array = array();
$past = 0; // Number of events selected in the past.
$future = 0; // Number of events selected in the future.
uksort( $events, 'mc_timediff_cmp' ); // Sort all events by proximity to current date.
$count = count( $events );
$group = array();
$spans = array();
$occur = array();
$extra = 0;
$i = 0;
// Create near_events array.
$recurring_events = array();
$last_events = array();
$last_group = array();
if ( is_array( $events ) ) {
foreach ( $events as $k => $event ) {
if ( $i < $count ) {
if ( is_array( $event ) ) {
foreach ( $event as $e ) {
if ( ! mc_private_event( $e ) ) {
$beginning = $e->occur_begin;
$end = $e->occur_end;
// Store span time in an array to avoid repeating database query.
if ( '1' === $e->event_span && ( ! isset( $spans[ $e->occur_group_id ] ) ) ) {
// This is a multi-day event: treat each event as if it spanned the entire range of the group.
$span_time = mc_span_time( $e->occur_group_id );
$beginning = $span_time[0];
$end = $span_time[1];
$spans[ $e->occur_group_id ] = $span_time;
} elseif ( '1' === $e->event_span && ( isset( $spans[ $e->occur_group_id ] ) ) ) {
$span_time = $spans[ $e->occur_group_id ];
$beginning = $span_time[0];
$end = $span_time[1];
}
$current = current_time( 'Y-m-d H:i:00' );
if ( $e ) {
// If a multi-day event, show only once.
if ( '0' !== $e->occur_group_id && '1' === $e->event_span && in_array( $e->occur_group_id, $group, true ) || in_array( $e->occur_id, $occur, true ) ) {
$md = true;
} else {
$group[] = $e->occur_group_id;
$occur[] = $e->occur_id;
$md = false;
}
// end multi-day reduction.
if ( ! $md ) {
if ( in_array( $e->occur_event_id, $recurring_events, true ) ) {
$is_recurring = true;
} else {
$is_recurring = false;
$instances = mc_get_occurrences( $e->occur_event_id );
if ( count( $instances ) > 1 ) {
$recurring_events[] = $e->occur_event_id;
}
}
if ( ( 'yes' !== $show_recurring ) && $is_recurring ) {
continue;
}
$event_used = false;
if ( 'yes' === $show_today && my_calendar_date_equal( $beginning, $current ) ) {
/**
* Should today's events be counted towards total number of upcoming events. Default `yes`. Any value other than 'no' will be interpreted as 'yes'.
*
* @hook mc_include_today_in_total
*
* @param {string} $in_total Return 'no' to exclude today's events from event count. Default 'yes'.
*
* @return {string} 'yes' or 'no'.
*/
$in_total = apply_filters( 'mc_include_today_in_total', 'yes' ); // count todays events in total.
if ( 'yes' === $in_total ) {
$near_events[] = $e;
$event_used = true;
// Should today's events be counted as future or past? If more past events chosen, count as past.
if ( $before > $after ) {
$past ++;
} else {
$future ++;
}
} else {
$near_events[] = $e;
$event_used = true;
}
}
if ( $past <= $before && ( my_calendar_date_comp( $beginning, $current ) ) && ! $event_used ) {
$near_events[] = $e; // Split off another past event.
$event_used = true;
}
if ( $future <= $after && ( ! my_calendar_date_comp( $end, $current ) ) && ! $event_used ) {
$near_events[] = $e; // Split off another future event.
$event_used = true;
}
$event_added = false;
// If this event happened before the current date.
if ( my_calendar_date_comp( $beginning, $current ) ) {
$past ++;
$event_added = true;
}
// If this happened on the current date.
if ( my_calendar_date_equal( $beginning, $current ) && ! $event_added ) {
if ( 'yes' === $show_today ) {
$extra ++;
$event_added = true;
}
}
// If this did not end before the current date.
if ( ! my_calendar_date_comp( $end, $current ) && ! $event_added ) {
$future ++;
$event_added = true;
}
$last_events[] = $e->occur_id;
$last_group[] = $e->occur_group_id;
}
if ( $past > $before && $future > $after && 'yes' !== $show_today ) {
break;
}
}
}
}
}
}
}
}
$events = $near_events;
usort( $events, 'mc_datetime_cmp' ); // Sort split events by date.
if ( is_array( $events ) ) {
foreach ( array_keys( $events ) as $key ) {
$event =& $events[ $key ];
$temp_array[] = $event;
}
$i = 0;
$groups = array();
$skips = array();
foreach ( reverse_array( $temp_array, true, $order ) as $event ) {
$details = mc_create_tags( $event, $context );
if ( ! in_array( $details['group'], $groups, true ) ) {
// dtstart is already in current time zone.
if ( $i < $skip && 0 !== $skip ) {
$i ++;
} else {
if ( ! in_array( $details['dateid'], $skips, true ) ) {
$output[] = array(
'event' => $event,
'tags' => $details,
);
$skips[] = $details['dateid'];
}
}
if ( '1' === $details['event_span'] ) {
$groups[] = $details['group'];
}
}
}
}
// If more items than there should be (due to handling of current-day's events), pop off.
$intended = $before + $after + $extra;
$actual = count( $output );
if ( $actual > $intended ) {
for ( $i = 0; $i < ( $actual - $intended ); $i ++ ) {
array_pop( $output );
}
}
$html = '';
foreach ( $output as $out ) {
$data = array(
'event' => $event,
'tags' => $details,
'template' => $template,
'type' => $type,
);
$details = mc_load_template( 'upcoming', $data );
if ( ! $details ) {
$html .= mc_format_upcoming_event( $out, $template, $type );
}
}
return $html;
}
/**
* Format upcoming events output.
*
* @param array $event Array with event object and template tags array.
* @param string $template Template key or template structure.
* @param string $type Display type.
*
* @return string
*/
function mc_format_upcoming_event( $event, $template, $type ) {
$details = $event['tags'];
$event = $event['event'];
$today = current_time( 'Y-m-d' );
$date = mc_date( 'Y-m-d H:i:s', strtotime( $details['dtstart'] ), false );
$classes = mc_event_classes( $event, 'upcoming' );
if ( my_calendar_date_equal( $date, $today ) ) {
$classes .= ' today';
}
if ( '1' === $details['event_span'] ) {
$classes .= ' multiday';
}
if ( 'list' === $type ) {
$prepend = "\n<li class=\"$classes\">";
$append = "</li>\n";
} else {
$prepend = '';
$append = '';
}
/**
* Opening elements for today's events list items. Default `<li class="$classes">`.
*
* @hook mc_event_upcoming_before
*
* @param {string} $append Template HTML closing tag.
* @param {string} $classes Space-separated list of classes for the event.
* @param {string} $date Event date in Y-m-d H:i:s format.
*
* @return {string} HTML following each event in upcoming events lists.
*/
$prepend = apply_filters( 'mc_event_upcoming_before', $prepend, $classes, $date );
/**
* Closing elements for today's events list items. Default `</li>`.
*
* @hook mc_event_upcoming_after
*
* @param {string} $append Template HTML closing tag.
* @param {string} $classes Space-separated list of classes for the event.
* @param {string} $date Event date in Y-m-d H:i:s format.
*
* @return {string} HTML following each event in upcoming events lists.
*/
$append = apply_filters( 'mc_event_upcoming_after', $append, $classes, $date );
/**
* Draw a custom template for upcoming events. Returning any non-empty string short circuits other template settings.
*
* @hook mc_draw_upcoming_event
*
* @param {string} $item Empty string before event template is drawn.
* @param {array} $details Associative array of event template tags.
* @param {string} $template Template string passed from widget or shortcode.
* @param {array} $args Associative array holding the arguments used to generate this list of upcoming events.
*
* @return {string} Event template details.
*/
$item = apply_filters( 'mc_draw_upcoming_event', '', $details, $template, $type );
if ( '' === $item ) {
$item = mc_draw_template( $details, $template, $type, $event );
}
/**
* Filter upcoming events list item output.
*
* @hook mc_event_upcoming
*
* @param {string} $item List item HTML for upcoming event.
* @param {object} $event Event object.
*
* @return {array} Array of event listing arguments.
*/
$output = apply_filters( 'mc_event_upcoming', $prepend . $item . $append, $event );
return $output;
}
/**
* Process the Today's Events widget.
*
* @param array $args Event & output construction parameters.
*
* @return string HTML.
*/
function my_calendar_todays_events( $args ) {
$language = isset( $args['language'] ) ? $args['language'] : '';
$switched = '';
if ( $language ) {
$locale = get_locale();
$switched = mc_switch_language( $locale, $language );
}
$category = ( isset( $args['category'] ) ) ? $args['category'] : 'default';
$template = ( isset( $args['template'] ) ) ? $args['template'] : 'default';
$substitute = ( isset( $args['fallback'] ) ) ? $args['fallback'] : '';
$author = ( isset( $args['author'] ) ) ? $args['author'] : 'all';
$host = ( isset( $args['host'] ) ) ? $args['host'] : 'all';
$date = ( isset( $args['date'] ) ) ? $args['date'] : false;
$site = ( isset( $args['site'] ) ) ? $args['site'] : false;
if ( $site ) {
$site = ( 'global' === $site ) ? BLOG_ID_CURRENT_SITE : $site;
switch_to_blog( $site );
}
$params = array(
'category' => $category,
'template' => $template,
'substitute' => $substitute,
'author' => $author,
'host' => $host,
'date' => $date,
);
$hash = md5( implode( ',', $params ) );
$output = '';
// allow reference by file to external template.
if ( '' !== $template && mc_file_exists( $template ) ) {
$template = file_get_contents( mc_get_file( $template ) );
}
$defaults = mc_widget_defaults();
$template = ( ! $template || 'default' === $template ) ? $defaults['today']['template'] : $template;
if ( mc_key_exists( $template ) ) {
$template = mc_get_custom_template( $template );
}
$category = ( 'default' === $category ) ? $defaults['today']['category'] : $category;
$no_event_text = ( '' === $substitute ) ? $defaults['today']['text'] : $substitute;
if ( $date ) {
$from = mc_date( 'Y-m-d', strtotime( $date ), false );
$to = mc_date( 'Y-m-d', strtotime( $date ), false );
} else {
$from = current_time( 'Y-m-d' );
$to = current_time( 'Y-m-d' );
}
$args = array(
'from' => $from,
'to' => $to,
'category' => $category,
'ltype' => '',
'lvalue' => '',
'author' => $author,
'host' => $host,
'search' => '',
'source' => 'upcoming',
'site' => $site,
);
/**
* Modify the arguments used to generate today's events.
*
* @hook mc_today_attributes
*
* @param {array} $args All arguments used to generate this list.
* @param {array} $params Subset of parameters used to generate this list's ID hash.
*
* @return {array} Array of event listing arguments.
*/
$args = apply_filters( 'mc_today_attributes', $args, $params );
$events = my_calendar_events( $args );
$today = ( isset( $events[ $from ] ) ) ? $events[ $from ] : false;
$lang = ( $switched ) ? ' lang="' . esc_attr( $switched ) . '"' : '';
$header = "<ul id='todays-events-$hash' class='todays-events'$lang>";
$footer = '</ul>';
$groups = array();
$todays_events = array();
// quick loop through all events today to check for holidays.
if ( is_array( $today ) ) {
foreach ( $today as $e ) {
if ( ! mc_private_event( $e ) && ! in_array( $e->event_group_id, $groups, true ) ) {
$event_details = mc_create_tags( $e );
$ts = $e->ts_occur_begin;
$classes = mc_event_classes( $e, 'today' );
$data = array(
'event' => $e,
'tags' => $event_details,
'template' => $template,
'args' => $args,
);
$details = mc_load_template( 'today', $data );
if ( $details ) {
$todays_events[ $ts ][] = $details;
} else {
/**
* Modify the HTML preceding each list item in a list of today's events.
*
* @hook mc_todays_events_before
*
* @param {string} $item HTML string before each event.
* @param {string} $classes Space separated list of classes for this event.
* @param {string} $category Category argument passed to this list.
*
* @return {string} HTML preceding each event in today's events lists.
*/
$prepend = apply_filters( 'mc_todays_events_before', "<li class='$classes'>", $classes, $category );
/**
* Closing elements for today's events list items. Default `</li>`.
*
* @hook mc_todays_events_after
*
* @param {string} $item Template HTML closing tag.
*
* @return {string} HTML following each event in today's events lists.
*/
$append = apply_filters( 'mc_todays_events_after', '</li>' );
/**
* Draw a custom template for today's events. Returning any non-empty string short circuits other template settings.
*
* @hook mc_draw_todays_event
*
* @param {string} $item Empty string before event template is drawn.
* @param {array} $event_details Associative array of event template tags.
* @param {string} $template Template string passed from widget or shortcode.
* @param {array} $args Associative array holding the arguments used to generate this list of events.
*
* @return {string} Event output details.
*/
$item = apply_filters( 'mc_draw_todays_event', '', $event_details, $template, $args );
if ( '' === $item ) {
$item = mc_draw_template( $event_details, $template, 'list', $e );
}
$todays_events[ $ts ][] = $prepend . $item . $append;
}
}
}
/**
* Filter the array of events listed in today's events lists.
*
* @hook mc_event_today
*
* @param {array} $todays_events A multidimensional array of event items with today's date as a key with an array of formatted HTML on event templates on the current date.
* @param {array} $events Array of events without private events removed. Values are event objects.
*
* @return {array} A multidimensional array of event items with today's date as a key with an array of formatted HTML on event templates on the current date.
*/
$todays_events = apply_filters( 'mc_event_today', $todays_events, $events );
foreach ( $todays_events as $k => $t ) {
foreach ( $t as $now ) {
$output .= $now;
}
}
if ( 0 !== count( $events ) ) {
/**
* Replace the list header for today's events lists. Default value `<ul id='todays-events-$hash' class='todays-events'$lang>`.
*
* @hook mc_todays_events_header
*
* @param {string} $header Existing today's events header HTML.
*
* @return {string} List header HTML.
*/
$return = apply_filters( 'mc_todays_events_header', $header );
$return .= $output;
/**
* Replace the list footer for today's events lists. Default value `</ul>`.
*
* @hook mc_todays_events_header
*
* @param {string} $header Existing today's events header HTML.
*
* @return {string} List header HTML.
*/
$return .= apply_filters( 'mc_todays_events_footer', $footer );
} else {
$return = '<div class="no-events-fallback todays-events">' . stripcslashes( $no_event_text ) . '</div>';
}
} else {
$return = '<div class="no-events-fallback todays-events">' . stripcslashes( $no_event_text ) . '</div>';
}
if ( $site ) {
restore_current_blog();
}
$output = mc_run_shortcodes( $return );
if ( $language ) {
mc_switch_language( $language, $locale );
}
return $output;
}