Jake Vanderwerf
2026-09-16 a239ef33ec489ce6d0f25ac2f256df0ac54a7e50
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
<?php
namespace JVBase\blocks;
 
 
use DateTime;
use JVBase\helpers\Events;
use JVBase\meta\Meta;
use JVBase\utility\Image;
use WP_Block;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class EventBlock {
    public function __construct()
    {
        add_action('init', [$this, 'registerBlock']);
    }
 
    public function registerBlock():void
    {
        register_block_type(
            JVB_DIR . '/build/event',
            [
                'render_callback' => [$this, 'render']
            ]
        );
    }
 
    public function render(array $attributes, string $content, WP_Block $block):string
    {
 
        if (!is_singular(BASE.'event')) {
            return '';
        }
 
        $meta = Meta::forPost(get_the_ID());
        $values = $meta->getAll();
 
        $out = '<section class="event-block">';
        $out .= Events::tags($values);
 
        if (!empty($values['post_thumbnail'])) {
            $out .= jvbFormatImage($values['post_thumbnail']);
        }
        $out .= '<header class="row x-btw">';
            if (!empty($values['post_title'])) {
                $out .= sprintf(
                    '<h1 id="%s">%s</h1>',
                    sanitize_title($values['post_title']),
                    $values['post_title']
                );
            }
            $out .= Events::calendarDay($values['date_start']??'');
 
        $out .= '</header>';
 
        $out .= '<div class="meta">';
            $out .= sprintf(
                '<ul class="time term-list row left"><li class="title">%s</li>',
                jvbIcon('clock')
            );
                $out .= sprintf(
                    '<li>%s</li>',
                    Events::formatDay($values['date_start']??'', $values['time_start']??'', $values['time_end']??'')
                );
                foreach ($values['schedule']??[] as $date) {
                    $out .= sprintf(
                        '<li>%s</li>',
                        Events::formatDay($date['date'], $date['time_start'], $date['time_end'], $date['notes'])
                    );
                }
            $out .= '</ul>';
 
                $out .= Events::location($values);
 
            $out .= $this->price($values['cost']??'', $values['ticket_url']??'');
            $out .= $this->participants($values['max_participants']??'');
 
            $out .= Events::terms($values);
 
            $out .= $this->rsvp($values['rsvp_required']??'', $values['rsvp_limit']??'', $values['rsvp_deadline']);
 
        $out .= '</div>';
        $out .= $this->gallery($values['gallery']??'');
        $out .= $this->moreInfo($values['external_url']??'');
 
        $out .= '</section>';
        return $out;
    }
    public function renderGallery():void
    {
        jvbRenderGallery();
    }
 
 
    protected function price(string $price, string $url):string
    {
        $out = '';
        if (!empty($price) || $price !== '0') {
            $out = jvbCurrency($price);
        }
        if (!empty($url)) {
            $out .= sprintf(
                '<br><a href="%s">%sPurchase Tickets</a>',
                esc_url($url),
                jvbIcon('ticket')
            );
        }
        return empty($out) ? $out : '<p>'.$out.'</p>';
    }
 
    protected function participants(string $num):string
    {
        $out = '';
        if (!empty($num) || $num !== '0') {
            $out .= sprintf(
                '<p>Maximum %d participants</p>',
                $num
            );
        }
        return $out;
    }
 
    protected function moreInfo(string $url):string
    {
        $out = '';
        if (!empty($url)) {
            $out .= sprintf(
                '<p>%s<a href="%s" target="_blank">Learn More</a></p>',
                jvbIcon('link'),
                esc_url($url)
            );
        }
        return $out;
    }
 
    protected function rsvp(string $mustRSVP, string $limit, string $deadline):string
    {
        $out = '';
        if (!empty($mustRSVP) && $mustRSVP === '1') {
            $out = '<p class="rsvp">RSVP Required';
            if (!empty($limit) && $limit !== '0') {
                $out .= sprintf(
                    '<br>Maximum %d Seats',
                    $limit
                );
            }
            if (!empty($deadline)) {
                $out .= sprintf(
                    '<br>RSVP Deadline: <time datetime="%s">%s</time>',
                    date('c', $deadline),
                    date('F j, Y', $deadline)
                );
            }
            $out .= '</p>';
        }
        return $out;
    }
 
    protected function gallery(string $gallery):string
    {
        $out = '';
        if (!empty($gallery)) {
            add_action('wp_footer', [$this, 'renderGallery']);
            $out = '<div class="gallery row left">';
            $gallery = array_map('absint', explode(',', $gallery));
            foreach ($gallery as $id) {
                $caption = wp_get_attachment_caption($id);
                $out .= sprintf(
                    '<figure>%s<figcaption>%s</figcaption></figure>',
                    jvbFormatImage($id,'tiny','medium',true,'event'),
                    empty($caption) ? '' : $caption
                );
            }
            $out .= '</div>';
        }
        return $out;
    }
}