-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
eme-calendar.php
729 lines (667 loc) · 28.2 KB
/
eme-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
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function eme_get_calendar_shortcode( $atts ) {
eme_enqueue_frontend();
$atts = shortcode_atts(
[
'category' => 0,
'notcategory' => 0,
'full' => 0,
'month' => '',
'year' => '',
'echo' => 0,
'long_events' => 0,
'ignore_filter' => 0,
'author' => '',
'contact_person' => '',
'location_id' => '',
'template_id' => 0,
'holiday_id' => 0,
'htmltable' => 1,
'htmldiv' => 0,
'weekdays' => '',
],
$atts
);
$echo = filter_var($atts['echo'], FILTER_VALIDATE_BOOLEAN);
$full = filter_var($atts['full'], FILTER_VALIDATE_BOOLEAN);
$long_events = filter_var($atts['long_events'], FILTER_VALIDATE_BOOLEAN);
$month = $atts['month'];
$year = intval($atts['year']);
$category = $atts['category'];
$notcategory = $atts['notcategory'];
$author = $atts['author'];
$contact_person = $atts['contact_person'];
$location_id = $atts['location_id'];
$template_id = $atts['template_id'];
$holiday_id = $atts['holiday_id'];
$htmltable = $atts['htmltable'];
$htmldiv = $atts['htmldiv'];
$weekdays = $atts['weekdays'];
$ignore_filter = $atts['ignore_filter'];
// Allow specific months/years for the calendar display
if (isset($_GET['calmonth']) && $_GET['calmonth'] != '') {
$month = eme_sanitize_request($_GET['calmonth']);
}
if ($month != 'this_month' && $month != 'next_month') {
$month = intval($month);
}
if ($month == 'this_month') {
$eme_date_obj_tmp = new ExpressiveDate('now', EME_TIMEZONE);
$eme_date_obj_tmp->startOfMonth();
$month = $eme_date_obj_tmp->format('m');
}
if (isset($_GET['calyear']) && $_GET['calyear'] != '') {
$year = intval($_GET['calyear']);
}
if (!empty($year) && empty($month)) {
$month = 1;
}
$location_id_arr = [];
if (!$ignore_filter && isset($_REQUEST['eme_eventAction']) && eme_sanitize_request($_REQUEST['eme_eventAction']) == 'filter') {
if (!empty($_REQUEST['eme_scope_filter'])) {
$scope = eme_sanitize_request($_REQUEST['eme_scope_filter']);
if (preg_match('/^([0-9]{4})-([0-9]{2})-[0-9]{2}--[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $scope, $matches)) {
$year = $matches[1];
$month = $matches[2];
}
}
if (!empty($_REQUEST['eme_author_filter']) && intval($_REQUEST['eme_author_filter']) > 0) {
$author = intval($_REQUEST['eme_author_filter']);
}
if (!empty($_REQUEST['eme_contact_filter']) && intval($_REQUEST['eme_contact_filter']) > 0) {
$contact_person = intval($_REQUEST['eme_contact_filter']);
}
if (!empty($_REQUEST['eme_loc_filter'])) {
if (is_array($_REQUEST['eme_loc_filter'])) {
$arr = eme_array_remove_empty_elements(eme_sanitize_request($_REQUEST['eme_loc_filter']));
if (!empty($arr)) {
$location_id_arr = $arr;
}
} else {
$location_id_arr[] = eme_sanitize_request($_REQUEST['eme_loc_filter']);
}
}
if (!empty($_REQUEST['eme_city_filter'])) {
$cities = eme_sanitize_request($_REQUEST['eme_city_filter']);
$tmp_ids = eme_get_city_location_ids($cities);
if (empty($location_id_arr)) {
$location_id_arr = $tmp_ids;
} else {
$location_id_arr = array_intersect($location_id_arr, $tmp_ids);
}
if (empty($location_id_arr)) {
$location_id = -1;
}
}
if (!empty($_REQUEST['eme_country_filter'])) {
$countries = eme_sanitize_request($_REQUEST['eme_country_filter']);
$tmp_ids = eme_get_country_location_ids($countries);
if (empty($location_id_arr)) {
$location_id_arr = $tmp_ids;
} else {
$location_id_arr = array_intersect($location_id_arr, $tmp_ids);
}
if (empty($location_id_arr)) {
$location_id = -1;
}
}
if (!empty($_REQUEST['eme_cat_filter'])) {
if (is_array($_REQUEST['eme_cat_filter'])) {
$arr = eme_array_remove_empty_elements(eme_sanitize_request($_REQUEST['eme_cat_filter']));
if (!empty($arr)) {
$category = join(',', $arr);
}
} else {
$category = eme_sanitize_request($_REQUEST['eme_cat_filter']);
}
}
}
if ($location_id != -1 && !empty($location_id_arr)) {
$location_id = join(',', $location_id_arr);
}
$result = eme_get_calendar(
full: $full,
month: $month,
year: $year,
do_echo: $echo,
long_events: $long_events,
category: $category,
author: $author,
contact_person: $contact_person,
location_id: $location_id,
notcategory: $notcategory,
template_id: $template_id,
weekdays: $weekdays,
holiday_id: $holiday_id,
htmltable: $htmltable,
htmldiv: $htmldiv
);
return $result;
}
function eme_get_calendar( $category=0, $notcategory=0, $full=0, $month='', $year='', $do_echo=0, $long_events=0, $author='', $contact_person='', $location_id='', $template_id=0, $holiday_id=0, $htmltable=1, $htmldiv=0, $weekdays='' ) {
global $wp_locale;
$do_echo = filter_var( $do_echo, FILTER_VALIDATE_BOOLEAN );
$full = filter_var( $full, FILTER_VALIDATE_BOOLEAN );
$long_events = filter_var( $long_events, FILTER_VALIDATE_BOOLEAN );
if ( ! empty( $weekdays ) ) {
$weekday_arr = explode( ',', $weekdays );
} else {
$weekday_arr = [];
}
// this comes from global WordPress preferences
$start_of_week = get_option( 'start_of_week' );
// allow month=next_month
if ( $month == 'next_month' ) {
$eme_date_obj_tmp = new ExpressiveDate( 'now', EME_TIMEZONE );
$eme_date_obj_tmp->startOfMonth()->addOneMonth();
$month = $eme_date_obj_tmp->format( 'm' );
$year = $eme_date_obj_tmp->format( 'Y' );
}
$eme_date_obj = new ExpressiveDate( 'now', EME_TIMEZONE );
if ( get_option( 'eme_use_client_clock' ) && !empty( $_COOKIE['eme_client_time'] ) ) {
try {
$client_timeinfo = eme_sanitize_request( json_decode( $_COOKIE['eme_client_time'], true ) );
if ( ! is_array( $client_timeinfo ) ) {
$client_timeinfo = [];
}
} catch ( Exception $error ) {
$client_timeinfo = [];
}
// avoid php warnings
if ( ! isset( $client_timeinfo['eme_client_unixtime'] ) ) {
$client_timeinfo['eme_client_mday'] = (int) $eme_date_obj->format( 'j' );
}
if ( ! isset( $client_timeinfo['eme_client_unixtime'] ) ) {
$client_timeinfo['eme_client_month'] = (int) $eme_date_obj->format( 'n' );
}
if ( ! isset( $client_timeinfo['eme_client_unixtime'] ) ) {
$client_timeinfo['eme_client_fullyear'] = (int) $eme_date_obj->format( 'Y' );
}
// these come from client unless their clock is wrong
$iNowDay = sprintf( '%02d', $client_timeinfo['eme_client_mday'] );
$iNowMonth = sprintf( '%02d', $client_timeinfo['eme_client_month'] );
$iNowYear = sprintf( '%04d', $client_timeinfo['eme_client_fullyear'] );
// some checks
if ( abs( $eme_date_obj->format( 'j' ) - $iNowDay ) > 30 ) {
$iNowDay = 0;
}
if ( abs( $eme_date_obj->format( 'n' ) - $iNowMonth ) > 11 ) {
$iNowMonth = 0;
}
if ( abs( $eme_date_obj->format( 'Y' ) - $iNowYear ) > 1 ) {
$iNowYear = 0;
}
if ( empty( $iNowDay ) || empty( $iNowMonth ) || empty( $iNowYear ) ) {
[$iNowYear, $iNowMonth, $iNowDay] = explode( '-', $eme_date_obj->getDate() );
}
} else {
// Get current year, month and day
[$iNowYear, $iNowMonth, $iNowDay] = explode( '-', $eme_date_obj->getDate() );
}
$iSelectedYear = $year;
$iSelectedMonth = $month;
if ( empty( $iSelectedMonth ) ) {
$iSelectedMonth = $iNowMonth;
}
if ( empty( $iSelectedYear ) ) {
$iSelectedYear = $iNowYear;
}
$iSelectedMonth = sprintf( '%02d', $iSelectedMonth );
$lang = eme_detect_lang();
// Get name and number of days of specified month
$eme_date_obj->setDay( 1 );
$eme_date_obj->setMonth( $iSelectedMonth );
$eme_date_obj->setYear( $iSelectedYear );
$iDaysInMonth = (int) $eme_date_obj->getDaysInMonth();
$fullMonthName = $wp_locale->get_month( $eme_date_obj->format( 'm' ) );
$shortMonthName = $wp_locale->get_month_abbrev( $fullMonthName );
// Get friendly month name, but since DateTime::format doesn't respect the locale, we need eme_localized_date
if ( $full ) {
$sMonthName = $fullMonthName;
} else {
$sMonthName = $shortMonthName;
}
// take into account some locale info: some always best show full month name, some show month after year, some have a year suffix
$showMonthAfterYear = 0;
$yearSuffix = '';
switch ( $lang ) {
case 'hu':
$showMonthAfterYear = 1;
break;
case 'ja':
$showMonthAfterYear = 1;
$sMonthName = $fullMonthName;
$yearSuffix = '年';
break;
case 'ko':
$showMonthAfterYear = 1;
$sMonthName = $fullMonthName;
$yearSuffix = '년';
break;
case 'zh':
$showMonthAfterYear = 1;
$sMonthName = $fullMonthName;
$yearSuffix = '年';
break;
}
if ( $showMonthAfterYear ) {
$cal_datestring = "$iSelectedYear$yearSuffix $sMonthName";
} else {
$cal_datestring = "$sMonthName $iSelectedYear$yearSuffix";
}
if ( $full && has_filter( 'eme_cal_full_yearmonth' ) ) {
$cal_datestring = apply_filters( 'eme_cal_full_yearmonth', $cal_datestring, $iSelectedMonth, $iSelectedYear );
}
if ( ! $full && has_filter( 'eme_cal_small_yearmonth' ) ) {
$cal_datestring = apply_filters( 'eme_cal_small_yearmonth', $cal_datestring, $iSelectedMonth, $iSelectedYear );
}
// Get previous year and month
$iPrevYear = $iSelectedYear;
$iPrevMonth = $iSelectedMonth - 1;
if ( $iPrevMonth <= 0 ) {
--$iPrevYear;
$iPrevMonth = 12; // set to December
}
$iPrevMonth = sprintf( '%02d', $iPrevMonth );
// Get next year and month
$iNextYear = $iSelectedYear;
$iNextMonth = $iSelectedMonth + 1;
if ( $iNextMonth > 12 ) {
++$iNextYear;
$iNextMonth = 1;
}
$iNextMonth = sprintf( '%02d', $iNextMonth );
// Get number of days of previous month
$eme_date_obj2 = new ExpressiveDate( 'now', EME_TIMEZONE );
$eme_date_obj2->setDay( 1 );
$eme_date_obj2->setMonth( $iPrevMonth );
$eme_date_obj2->setYear( $iPrevYear );
$iPrevDaysInMonth = (int) $eme_date_obj2->getDaysInMonth();
// Get numeric representation of the day of the week of the first day of specified (current) month
// remember: first day of week is a Sunday
// if you want the day of the week to begin on Monday: start_of_week=1, Tuesday: start_of_week=2, etc ...
// So, if e.g. the month starts on a Sunday and start_of_week=1 (Monday), then $iFirstDayDow is 6
$iFirstDayDow = (int) $eme_date_obj->getDayOfWeekAsNumeric() - $start_of_week;
if ( $iFirstDayDow < 0 ) {
$iFirstDayDow += 7;
}
// On what day the previous month begins
if ( $iFirstDayDow > 0 ) {
$iPrevShowFrom = $iPrevDaysInMonth - $iFirstDayDow + 1;
} else {
$iPrevShowFrom = $iPrevDaysInMonth;
}
// we'll look for events in the requested month and 7 days before and after
$calbegin = "$iPrevYear-$iPrevMonth-$iPrevShowFrom";
$calend = "$iNextYear-$iNextMonth-07";
$events = eme_get_events( scope: "$calbegin--$calend", location_id: $location_id, category: $category, author: $author, contact_person: $contact_person, notcategory: $notcategory );
$eventful_days = [];
if ( $events ) {
// go through the events and slot them into the right d-m index
foreach ( $events as $event ) {
if ( $event['event_status'] == EME_EVENT_STATUS_PRIVATE && ! is_user_logged_in() ) {
continue;
}
$eme_date_obj_end = new ExpressiveDate( $event['event_end'], EME_TIMEZONE );
$eme_date_obj_now = new ExpressiveDate( 'now', EME_TIMEZONE );
// when hiding past events, we hide those which end date is lower than today
if ( get_option( 'eme_cal_hide_past_events' ) && $eme_date_obj_end < $eme_date_obj_now ) {
continue;
}
// if $long_events is set then show a date as eventful if there is an multi-day event which runs during that day
if ( $long_events ) {
$eme_date_obj_tmp = new ExpressiveDate( $event['event_start'], EME_TIMEZONE );
// just to be safe we need a reasonable end datetime too
if ( $eme_date_obj_end < $eme_date_obj_tmp ) {
$eme_date_obj_end = $eme_date_obj_tmp->copy();
}
// we'll start at the beginning of the first day the event begins, so when we add one day (in the while loop below)
// the last day is taken also into account if the end time would be smaller than the start time
$eme_date_obj_tmp->startOfDay();
while ( $eme_date_obj_tmp <= $eme_date_obj_end ) {
$event_eventful_date = $eme_date_obj_tmp->getDate();
if ( isset( $eventful_days[ $event_eventful_date ] ) && is_array( $eventful_days[ $event_eventful_date ] ) ) {
$eventful_days[ $event_eventful_date ][] = $event;
} else {
$eventful_days[ $event_eventful_date ] = [ $event ];
}
$eme_date_obj_tmp->addOneDay();
}
} else {
//Only show events on the day that they start
$event_start_date = eme_get_date_from_dt( $event['event_start'] );
if ( isset( $eventful_days[ $event_start_date ] ) && is_array( $eventful_days[ $event_start_date ] ) ) {
$eventful_days[ $event_start_date ][] = $event;
} else {
$eventful_days[ $event_start_date ] = [ $event ];
}
}
}
}
// we found all the events for the wanted days, now get them in the correct format with a good link
if ( $template_id ) {
$event_format = eme_get_template_format( $template_id );
} else {
$event_format = get_option( 'eme_full_calendar_event_format' );
}
$event_title_format = get_option( 'eme_small_calendar_event_title_format' );
$event_title_separator_format = get_option( 'eme_small_calendar_event_title_separator' );
$cells = [];
$holidays = [];
$holiday_titles = [];
if ( $holiday_id ) {
$holidays = eme_get_holiday_listinfo( intval($holiday_id) );
if ( $holidays ) {
foreach ( $holidays as $day_key => $info ) {
if ( ! empty( $info['name'] ) ) {
$holiday_title = trim( eme_esc_html( $info['name'] ) );
$eme_holiday_class = 'eme-cal-holidays';
if ( empty( $info['class'] ) ) {
$class = $eme_holiday_class;
} else {
$class = sanitize_html_class( $info['class'] );
}
if ( empty( $info['link'] ) ) {
$holiday_text = $info['name'];
} else {
$holiday_text = "<a href='" . $info['link'] . "' target='_blank'>" . $info['name'] . '</a>';
}
// if there's an event that day, the day-number is a link and will be set later on
// otherwise we set the day-number
if ( isset( $eventful_days[ $day_key ] ) ) {
$holiday_titles[ $day_key ] = $holiday_title;
$cells[ $day_key ] = "<br><span class='$class'>$holiday_text</span><br>";
} else {
$event_date = explode( '-', $day_key );
$event_day = ltrim( $event_date[2], '0' );
if ( ! empty( $info['link'] ) ) {
$event_day = "<a href='" . $info['link'] . "' target='_blank'>" . $event_day . '</a>';
}
$cells[ $day_key ] = "<span class='$eme_holiday_class' title='$holiday_title'>$event_day</span>";
if ( $full ) {
$cells[ $day_key ] .= "<br /><span class='$class'>$holiday_text</span><br>";
}
}
}
}
}
}
foreach ( $eventful_days as $day_key => $events ) {
// Set the date into the key
$events_titles = [];
if ( isset( $holiday_titles[ $day_key ] ) ) {
$events_titles[] = $holiday_titles[ $day_key ];
}
foreach ( $events as $event ) {
$event_title = eme_replace_event_placeholders( $event_title_format, $event, 'html', $lang, 0 );
$event_title = eme_replace_calendar_placeholders( $event_title, $event, $day_key, 'html', $lang );
$events_titles[] = $event_title;
}
$link_title = implode( $event_title_separator_format, $events_titles );
$cal_day_link = eme_calendar_day_url( $day_key );
// Let's add the possible options
// template_id is not being used per event
if ( ! empty( $location_id ) ) {
$cal_day_link = add_query_arg( [ 'location_id' => $location_id ], $cal_day_link );
}
if ( ! empty( $category ) ) {
$cal_day_link = add_query_arg( [ 'category' => $category ], $cal_day_link );
}
if ( ! empty( $notcategory ) ) {
$cal_day_link = add_query_arg( [ 'notcategory' => $notcategory ], $cal_day_link );
}
// the hash char and everything following it in a GET is not getting through a browser request, so we replace "#_MYSELF" with just "_MYSELF" here
$author = str_replace( '#_MYSELF', '_MYSELF', $author );
$contact_person = str_replace( '#_MYSELF', '_MYSELF', $contact_person );
if ( ! empty( $author ) ) {
$cal_day_link = add_query_arg( [ 'author' => $author ], $cal_day_link );
}
if ( ! empty( $contact_person ) ) {
$cal_day_link = add_query_arg( [ 'contact_person' => $contact_person ], $cal_day_link );
}
$event_date = explode( '-', $day_key );
$event_day = ltrim( $event_date[2], '0' );
// there might already be something in the cell if there's a holiday
if ( isset( $cells[ $day_key ] ) ) {
$holiday_info = $cells[ $day_key ];
} else {
$holiday_info = '';
}
// if there is a specific class filter for the urls, do it
$class = '';
if ( has_filter( 'eme_calday_url_class_filter' ) ) {
$class = apply_filters( 'eme_calday_url_class_filter', $class );
}
if ( ! empty( $class ) ) {
$class = "class='$class'";
}
$cells[ $day_key ] = "<span class='span-eme-calday span-eme-calday-$event_day'><a title='$link_title' href='$cal_day_link' $class>$event_day</a></span>";
if ( $full ) {
$cells[ $day_key ] .= "$holiday_info<ul class='eme-calendar-day-event'>";
foreach ( $events as $event ) {
$cal_day_content = eme_replace_event_placeholders( $event_format, $event, 'html', $lang, 0 );
$cal_day_content = eme_replace_calendar_placeholders( $cal_day_content, $event, $day_key, 'html', $lang );
$cells[ $day_key ] .= $cal_day_content;
}
$cells[ $day_key ] .= '</ul>';
}
}
// If previous month
$isPreviousMonth = ( $iFirstDayDow > 0 );
// Initial day on the calendar
$iCalendarDay = ( $isPreviousMonth ) ? $iPrevShowFrom : 1;
$isNextMonth = false;
$sCalTblRows = '';
$sCalDivRows = '';
// Generate rows for the calendar
for ( $i = 0; $i < 6; $i++ ) { // 6-weeks range
if ( $isNextMonth ) {
continue;
}
$sCalTblRows .= '<tr>';
$sCalDivRows .= "<div class='emeDivTableRow'>";
for ( $j = 0; $j < 7; $j++ ) { // 7 days a week
// we need the calendar day with 2 digits for the planned events
$iCalendarDay_padded = sprintf( '%02d', $iCalendarDay );
if ( $isPreviousMonth ) {
$calstring = "$iPrevYear-$iPrevMonth-$iCalendarDay_padded";
} elseif ( $isNextMonth ) {
$calstring = "$iNextYear-$iNextMonth-$iCalendarDay_padded";
} else {
$calstring = "$iSelectedYear-$iSelectedMonth-$iCalendarDay_padded";
}
// each day in the calendar has the name of the day as a class by default
$eme_date_obj = new ExpressiveDate( $calstring, EME_TIMEZONE );
$sClass = $eme_date_obj->format( 'D' );
if ( isset( $holidays[ $calstring ] ) ) {
$sClass .= ' holiday';
}
if ( isset( $cells[ $calstring ] ) ) {
if ( $isPreviousMonth ) {
$sClass .= " eventful-pre event-day-$iCalendarDay";
} elseif ( $isNextMonth ) {
$sClass .= " eventful-post event-day-$iCalendarDay";
} elseif ( $calstring == "$iNowYear-$iNowMonth-$iNowDay" ) {
$sClass .= " eventful-today event-day-$iCalendarDay";
} else {
$sClass .= " eventful event-day-$iCalendarDay";
}
$sCalTblCell = "<td class='$sClass'>" . $cells[ $calstring ] . "</td>\n";
$sCalDivCell = "<div class='emeDivTableCell $sClass'>" . $cells[ $calstring ] . "</div>\n";
} else {
if ( $isPreviousMonth ) {
$sClass .= ' eventless-pre';
} elseif ( $isNextMonth ) {
$sClass .= ' eventless-post';
} elseif ( $calstring == "$iNowYear-$iNowMonth-$iNowDay" ) {
$sClass .= ' eventless-today';
} else {
$sClass .= ' eventless';
}
$sCalTblCell = "<td class='$sClass'><span class='span-eme-calday span-eme-calday-$iCalendarDay'>$iCalendarDay</span></td>\n";
$sCalDivCell = "<div class='emeDivTableCell $sClass'>$iCalendarDay</div>\n";
}
// only show wanted columns
if ( count( $weekday_arr ) ) {
$day_of_week = $eme_date_obj->getDayOfWeekAsNumeric();
if ( eme_is_numeric_array( $weekday_arr ) && in_array( $day_of_week, $weekday_arr ) ) {
$sCalTblRows .= $sCalTblCell;
$sCalDivRows .= $sCalDivCell;
}
} else {
$sCalTblRows .= $sCalTblCell;
$sCalDivRows .= $sCalDivCell;
}
// Next day
++$iCalendarDay;
if ( $isPreviousMonth && $iCalendarDay > $iPrevDaysInMonth ) {
$isPreviousMonth = false;
$iCalendarDay = 1;
}
if ( ! $isPreviousMonth && ! $isNextMonth && $iCalendarDay > $iDaysInMonth ) {
$isNextMonth = true;
$iCalendarDay = 1;
}
}
$sCalTblRows .= "</tr>\n";
$sCalDivRows .= "</div>\n";
}
$weekday_names = [ __( 'Sunday' ), __( 'Monday' ), __( 'Tuesday' ), __( 'Wednesday' ), __( 'Thursday' ), __( 'Friday' ), __( 'Saturday' ) ];
$weekday_header_class = [ 'Sun_header', 'Mon_header', 'Tue_header', 'Wed_header', 'Thu_header', 'Fri_header', 'Sat_header' ];
$sCalTblDayNames = '';
$sCalDivDayNames = '';
// respect the beginning of the week offset
for ( $i = $start_of_week; $i < $start_of_week + 7; $i++ ) {
$j = $i;
if ( $j >= 7 ) {
$j -= 7;
}
// only show wanted columns
if ( ! empty( $weekday_arr ) ) {
if ( ! eme_is_numeric_array( $weekday_arr ) || ! in_array( $j, $weekday_arr ) ) {
continue;
}
}
if ( $full ) {
$sCalTblDayNames .= "<td class='" . $weekday_header_class[ $j ] . "'>" . $wp_locale->get_weekday_abbrev( $weekday_names[ $j ] ) . '</td>';
$sCalDivDayNames .= "<div class='emeDivTableHead " . $weekday_header_class[ $j ] . "'>" . $wp_locale->get_weekday_abbrev( $weekday_names[ $j ] ) . '</div>';
} else {
$sCalTblDayNames .= "<td class='" . $weekday_header_class[ $j ] . "'>" . $wp_locale->get_weekday_initial( $weekday_names[ $j ] ) . '</td>';
$sCalDivDayNames .= "<div class='emeDivTableHead " . $weekday_header_class[ $j ] . "'>" . $wp_locale->get_weekday_initial( $weekday_names[ $j ] ) . '</div>';
}
}
// the real links are created via jquery when clicking on the prev-month or next-month class-links
$random = ( rand( 100, 200 ) );
$cal_div_id = "eme-calendar-$random";
$previous_link = "<a class='prev-month eme-cal-prev-month' href='#' data-full='$full' data-htmltable='$htmltable' data-htmldiv='$htmldiv' data-long_events='$long_events' data-month='$iPrevMonth' data-year='$iPrevYear' data-category='$category' data-author='$author' data-contact_person='$contact_person' data-location_id='$location_id' data-notcategory='$notcategory' data-template_id='$template_id' data-holiday_id='$holiday_id' data-weekdays='$weekdays' data-language='$lang' data-calendar_divid='$cal_div_id'><<</a>";
$next_link = "<a class='next-month eme-cal-next-month' href=\"#\" data-full='$full' data-htmltable='$htmltable' data-htmldiv='$htmldiv' data-long_events='$long_events' data-month='$iNextMonth' data-year='$iNextYear' data-category='$category' data-author='$author' data-contact_person='$contact_person' data-location_id='$location_id' data-notcategory='$notcategory' data-template_id='$template_id' data-holiday_id='$holiday_id' data-weekdays='$weekdays' data-language='$lang' data-calendar_divid='$cal_div_id'>>></a>";
$full ? $class = 'eme-calendar-full' : $class = 'eme-calendar';
$calendar = "<div class='$class' id='$cal_div_id'>";
if ( count( $weekday_arr ) ) {
$colspan = count( $weekday_arr );
} else {
$colspan = 7;
}
if ( $full ) {
$fullclass = 'fullcalendar';
} else {
$fullclass = 'smallcalendar';
}
$CalTblHead = "<th class='month_name' colspan='$colspan'>$previous_link $cal_datestring $next_link</th>\n";
$CalDivHead = "<div class='emeDivTableHead month_name'>$previous_link $cal_datestring $next_link</div>\n";
// Build the heading portion of the calendar table
if ( $htmltable && ! $htmldiv ) {
$calendar .= "<table class='eme-calendar-table $fullclass'>\n" .
'<thead><tr>' . $CalTblHead . "</tr></thead>\n" .
"<tr class='days-names'>" . $sCalTblDayNames . "</tr>\n";
$calendar .= $sCalTblRows;
$calendar .= "</table>\n";
} else {
$calendar .= "<div class='emeDivTable $fullclass'>\n" .
"<div class='emeDivTableHeading'><div class='emeDivTableRow'>" . $CalDivHead . "</div></div>\n" .
"</div><div class='emeDivTable $fullclass'>\n" .
"<div class='emeDivTableRow days-names'>" . $sCalDivDayNames . "</div>\n";
$calendar .= $sCalDivRows;
$calendar .= "</div>\n";
}
$calendar .= '</div>';
if ( $do_echo ) {
echo $calendar;
} else {
return $calendar;
}
}
// for both logged in and not logged in users
add_action( 'wp_ajax_eme_calendar', 'eme_calendar_ajax' );
add_action( 'wp_ajax_nopriv_eme_calendar', 'eme_calendar_ajax' );
function eme_calendar_ajax() {
check_ajax_referer( 'eme_frontend', 'eme_frontend_nonce' );
( ! empty( $_POST['full'] ) ) ? $full = 1 : $full = 0;
( ! empty( $_POST['htmltable'] ) ) ? $htmltable = 1 : $htmltable = 0;
( ! empty( $_POST['htmldiv'] ) ) ? $htmldiv = 1 : $htmldiv = 0;
( ! empty( $_POST['long_events'] ) ) ? $long_events = 1 : $long_events = 0;
( ! empty( $_POST['category'] ) ) ? $category = eme_sanitize_request( $_POST['category'] ) : $category = 0; // this can be a string like 0+1 or 0,3
( ! empty( $_POST['notcategory'] ) ) ? $notcategory = eme_sanitize_request( $_POST['notcategory'] ) : $notcategory = 0; // this can be a string like 0+1 or 0,3
( ! empty( $_POST['calmonth'] ) ) ? $month = eme_sanitize_request( $_POST['calmonth'] ) : $month = ''; // this can be a number or text (like next_month)
( ! empty( $_POST['calyear'] ) ) ? $year = intval( $_POST['calyear'] ) : $year = '';
( ! empty( $_POST['author'] ) ) ? $author = eme_sanitize_request( $_POST['author'] ) : $author = ''; // can be a name of a person
( ! empty( $_POST['contact_person'] ) ) ? $contact_person = eme_sanitize_request( $_POST['contact_person'] ) : $contact_person = ''; // can be a name of a person
( ! empty( $_POST['location_id'] ) ) ? $location_id = eme_sanitize_request( $_POST['location_id'] ) : $location_id = ''; // this can be a string like 0+1 or 0,3
( ! empty( $_POST['template_id'] ) ) ? $template_id = intval( $_POST['template_id'] ) : $template_id = '';
( ! empty( $_POST['holiday_id'] ) ) ? $holiday_id = intval( $_POST['holiday_id'] ) : $holiday_id = '';
( ! empty( $_POST['weekdays'] ) ) ? $weekdays = eme_sanitize_request( $_POST['weekdays'] ) : $weekdays = ''; // this can be a string like 0,2,3,5
echo eme_get_calendar( full: $full, month: $month, year: $year, long_events: $long_events, category: $category, author: $author, contact_person: $contact_person, location_id: $location_id, notcategory: $notcategory, template_id: $template_id, weekdays: $weekdays, holiday_id: $holiday_id, htmltable: $htmltable, htmldiv: $htmldiv );
wp_die();
}
function eme_replace_calendar_placeholders( $format, $event, $cal_day, $target = 'html', $lang = '' ) {
// replace EME language tags as early as possible
$format = eme_translate_string_nowptrans( $format );
if ( $target == 'htmlmail' || $target == 'html_nohtml2br' ) {
$target = 'html';
}
if ( has_filter( 'eme_cal_format_prefilter' ) ) {
$format = apply_filters( 'eme_cal_format_prefilter', $format, $event, $cal_day );
}
$event_start_date = eme_get_date_from_dt( $event['event_start'] ); // Just the YYYY-MM-DD part
$event_end_date = eme_get_date_from_dt( $event['event_end'] );
$needle_offset = 0;
preg_match_all( '/#(ESC|URL)?@?_?[A-Za-z0-9_]+(\{(?>[^{}]+|(?2))*\})*+/', $format, $placeholders, PREG_OFFSET_CAPTURE );
foreach ( $placeholders[0] as $orig_result ) {
$result = $orig_result[0];
$orig_result_needle = $orig_result[1] - $needle_offset;
$orig_result_length = strlen( $orig_result[0] );
$replacement = '';
$found = 1;
if ( preg_match( '/#_IS_START_DAY/', $result ) ) {
if ( $cal_day == $event_start_date ) {
$replacement = 1;
} else {
$replacement = 0;
}
} elseif ( preg_match( '/#_IS_END_DAY/', $result ) ) {
if ( $cal_day == $event_end_date ) {
$replacement = 1;
} else {
$replacement = 0;
}
} elseif ( preg_match( '/#_IS_NOT_START_OR_END_DAY|#_IS_NOT_START_OR_END_DATE/', $result ) ) {
if ( $cal_day != $event_start_date && $cal_day != $event_end_date ) {
$replacement = 1;
} else {
$replacement = 0;
}
} else {
$found = 0;
}
if ( $found ) {
$format = substr_replace( $format, $replacement, $orig_result_needle, $orig_result_length );
$needle_offset += $orig_result_length - strlen( $replacement );
}
}
// now, replace any language tags found in the format itself
$format = eme_translate( $format, $lang );
return do_shortcode( $format );
}