Jake Vanderwerf
8 days ago 90f5c3069f23f63036ab0178ff9ab69effcfb2ea
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
<?php
 
namespace JVBase\helpers;
use DateTime;
use Exception;
 
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
 
/**
 * Helper class that renders fields based on the presets from MakeCalendarType.php
 */
class Events {
    public static function tags(array $fields):string
    {
        $out = '<div class="event tags row right">';
        $get = ['is_free' => 'Free', 'rsvp_required' => 'Must RSVP', 'public' => 'Private Event'];
        foreach ($get as $k => $g) {
            $check = !empty($fields[$k]) && $fields[$k] === '1';
            if ($k === 'public') {
                $check = !$check;
            }
            $icon = match ($k) {
                'is_free'   => jvbIcon('check-circle'),
                'rsvp_required'   => jvbIcon('envelope'),
                'public'   => jvbIcon('lock-key'),
            };
            if ($check) {
                $out .= sprintf(
                    '<div class="tag">%s%s</div>',
                    $icon,
                    $g
                );
            }
        }
        $out .= '</div>';
        return $out;
    }
 
    /**
     * @param string $date
     *
     * @return string
     */
    public static function calendarDay(string $date):string
    {
 
        if (empty($date)) {
            return '';
        }
        try {
            $date = new DateTime($date, wp_timezone());
            $weekday = $date->format('D');
            $num = $date->format('d');
            $month = $date->format('M');
 
            return sprintf(
                '<div class="col calendar">
                        <span class="weekday">%s</span>
                        <span class="month">%s</span>
                        <span class="day">%d</span>
                    </div>',
                $weekday,
                $month,
                $num
            );
        } catch (Exception $e){
            return '';
        }
    }
 
    public static function formatDay(string $date, string $start, string $end, string $notes = ''):string
    {
        $out = '';
        if (!empty($date)) {
            $out .= sprintf(
                '<time datetime="%s">%s</time><br>',
                date('c', strtotime($date)),
                date('F j, Y', strtotime($date))
            );
        }
        $doit = false;
 
        if (!empty($start)) {
            $doit = true;
            $d = strtotime($date.' '.$start);
            $out .= sprintf(
                '<time datetime="%s">%s</time>',
                date('c', $d),
                date('g:ia', $d)
            );
        }
        if (!empty($end)) {
            if ($doit) {
                $out .= ' - ';
            }
            $d = strtotime($date.' '.$end);
            $out .= sprintf(
                '<time datetime="%s">%s</time>',
                date('c', $d),
                date('g:ia', $d)
            );
 
        }
        if (!empty($notes)) {
            $out .= wpautop($notes);
        }
 
        return $out;
    }
 
    public static function getMonthName(int $num):string
    {
        return match($num) {
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December',
        };
    }
 
    public static function location(array $fields):string
    {
        $out = '';
        if (!array_key_exists('location', $fields)) {
            return '';
        }
        if (!empty($fields['location']['address']??'')) {
            $out .= sprintf(
                '<ul class="location term-list row left">
                    <li class="title">%sLocation</li>
                    <li>%s</li>
                    <li>%s</li>
                    %s
                    </ul>',
                jvbIcon('map-pin'),
                self::renderLocationString($fields['location']),
                jvbLocationLinks($fields['location']),
                empty($location['location_notes']??'') ? '' : '<li>'.$fields['location_notes'].'</li>',
            );
        }
        return $out;
    }
        public static function renderLocationString(array $location):string
        {
            if (!array_key_exists('address', $location) ||
                !array_key_exists('street', $location) ||
                !array_key_exists('city', $location) ||
                !array_key_exists('province', $location) ||
                !array_key_exists('country', $location)) {
                return '';
            }
 
            return sprintf(
                '<p class="location">%s%s<br>%s, %s %s</p>',
                $location['address'],
                $location['street'],
                $location['city'],
                $location['province'],
                $location['country'] === 'CA' ? '' : $location['country']
            );
        }
    public static function terms(array $fields, bool $showTaxonomy = true):string
    {
        $out = '';
        $get = ['city', 'event_category'];
        foreach ($get as $g) {
            if (!empty($fields[$g])) {
                $out .= jvbMetaTermList($fields[$g], $g, true, $showTaxonomy);
            }
        }
        return $out;
    }
}