Jake Vanderwerf
2025-11-10 e9967fa22781d922ba4eb8fb44fe72d200ac4b14
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
<?php
namespace JVBase\meta;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
class MetaRenderer
{
 
    /**
     * Rendering can be overridden in individual plugin/theme setups by creating a function
     *  A) for the named field via camelCased, no underscore function: BASE.Render{$name}Field($value, $config);
     *  B) for the entire field type via camelCased, no underscore function: BASE.Render{$type}Field($value, $config);
     * @param string $name
     * @param mixed $value
     * @param array $config
     *
     * @return mixed
     */
    public function render(string $name, mixed $value, array $config, bool $return = false):mixed
    {
        $type = array_map('ucfirst', explode('_', $config['type']));
        $type = implode('', $type);
        $method = 'render' . $type . 'Field';
        $methodFunction = str_replace('_', '', BASE).ucfirst($method);
        $nameFunction = str_replace('_', '', BASE).'Render'.ucfirst($name).'Field';
        $output = '';
        if (function_exists($nameFunction)) {
            $output = call_user_func($nameFunction, $value, $config);
        } elseif (function_exists($methodFunction)) {
            $output = call_user_func($methodFunction, $value, $config);
        } elseif (method_exists($this, $method)) {
            $output = $this->$method($name, $value, $config);
        }
 
        if ($return) {
            return $output;
        }
 
        echo $output;
        return true;
    }
 
    protected function renderTextField(string $name, string $value, array $field):string
    {
        return apply_filters('the_content', $value);
    }
 
    protected function renderTextareaField(string $name, string $value, array $field):string
    {
        return $this->renderTextField($name, $value, $field);
    }
    protected function renderNumberField(string $name, string $value, array $field):string
    {
        return $this->renderTextField($name, $value, $field);
    }
    protected function renderEmailField(string $name, string $value, array $field):string
    {
        $link = 'mailto:'.$value.'?subject='.rawurlencode('Contact from edmonton.ink').'&body='.rawurlencode('Hey,
        I found you on edmonton.ink, and I wanted to reach out!');
 
        return '<a href="'.$link.'" title="Send an Email">'.jvbIcon('envelope').'<span>Email</span></a>';
 
    }
    protected function renderUrlField(string $name, string $value, array $field):string
    {
//        jvbDump($value, 'URL Field:');
//        jvbDump($field);
        return '';
    }
    protected function renderImageField(string $name, string $value, array $field):string
    {
        if (!is_numeric($value)) {
            return '';
        }
        return jvbFormatImage($value, 'tiny', 'medium');
    }
    protected function renderGalleryField(string $name, array|null|false $value, array $field):string
    {
//        jvbDump($value, 'Gallery Field:');
//        jvbDump($field);
        return '';
    }
    protected function renderTaxonomyField($name, $value, $field):string
    {
        if ($value === '') {
            return '';
        }
        $value = explode(',', $value);
        $terms = get_terms([
            'taxonomy'  => jvbCheckBase($field['taxonomy']),
            'terms_in'  => $value
        ]);
        return jvbRenderTermList($terms, $field['label']);
    }
 
    protected function renderRepeaterField($name, $value, $field):string
    {
//        jvbDump($value, 'Repeater Field:');
//        jvbDump($field);
        return '';
    }
 
    protected function renderGroupField(string $name, array|bool $value, array $field):string
    {
        if (empty($value) || !$value || !isset($field['fields'])) {
            return '';
        }
 
        $output = '<div class="group-field ' . esc_attr($name) . '">';
 
        // Add field label if configured to show
        if (isset($field['show_label']) && $field['show_label'] && !empty($field['label'])) {
            $output .= '<h3 class="group-label">' . esc_html($field['label']) . '</h3>';
        }
 
        $output .= '<div class="group-content col">';
 
        foreach ($field['fields'] as $subfield_name => $subfield_config) {
            if (!isset($value[$subfield_name]) || empty($value[$subfield_name])) {
                continue;
            }
 
            $subfield_value = $value[$subfield_name];
 
            // Check if subfield has conditions that prevent display
            if (isset($subfield_config['condition'])) {
                $condition = $subfield_config['condition'];
                $condition_field = $condition['field'];
                $condition_value = $value[$condition_field] ?? '';
                $operator = $condition['operator'] ?? '==';
                $expected_value = $condition['value'];
 
                $condition_met = false;
                switch ($operator) {
                    case '==':
                        $condition_met = $condition_value == $expected_value;
                        break;
                    case '!=':
                        $condition_met = $condition_value != $expected_value;
                        break;
                    case 'in':
                        $condition_met = is_array($expected_value) && in_array($condition_value, $expected_value);
                        break;
                    case 'not_in':
                        $condition_met = is_array($expected_value) && !in_array($condition_value, $expected_value);
                        break;
                }
 
                if (!$condition_met) {
                    continue;
                }
            }
 
            // Render the subfield
            $subfield_output = $this->render($subfield_name, $subfield_value, $subfield_config, true);
 
            if (!empty($subfield_output)) {
                $output .= '<div class="group-item ' . esc_attr($subfield_name) . '">';
 
                // Add label if configured
                if (isset($subfield_config['show_label']) && $subfield_config['show_label'] && !empty($subfield_config['label'])) {
                    $output .= '<span class="subfield-label">' . esc_html($subfield_config['label']) . ':</span> ';
                }
 
                $output .= $subfield_output;
                $output .= '</div>';
            }
        }
 
        $output .= '</div></div>';
 
        return $output;
    }
    protected function renderLocationField(string $name, array|bool $value, array $field): string
    {
        if (!$value) {
            return '';
        }
        // Early return if no location data
        if (empty($value) || empty($value['lat']) || empty($value['lng'])) {
            return '';
        }
 
        $googleMaps = JVB()->connect('maps');
        if (!$googleMaps->isSetUp()) {
            error_log('Google Maps is not set up');
            // Fallback to text-only display
            return $this->renderLocationFallback($value, $field);
        }
 
        error_log('Google Maps: '.print_r($googleMaps, true));
        $lat = (float)$value['lat'];
        $lng = (float)$value['lng'];
        $address = $googleMaps->formatAddress($value, 'full');
 
        // Map display options (can be configured via field config)
        $map_options = array_merge([
            'height' => '200px',
            'zoom' => 14,
            'show_marker' => true,
            'show_info_window' => true,
            'interactive' => true
        ], $field['map_options'] ?? []);
 
        // Link options
        $link_options = array_merge([
            'show_icons' => true,
            'style' => 'buttons',
            'include' => ['google', 'apple']
        ], $field['link_options'] ?? []);
 
        ob_start();
        ?>
        <div class="jvb-location-display">
            <?php
            // Render the interactive map
            echo $googleMaps->renderDisplayMap($value, $map_options);
            ?>
 
            <div class="jvb-location-details">
                <?php if (!empty($address)): ?>
                    <div class="jvb-location-address">
                        <?= jvbIcon('map-pin'); ?>
                        <span><?= esc_html($address); ?></span>
                    </div>
                <?php endif; ?>
 
                <?php
                // Render map application links
                echo $googleMaps->renderMapLinks($lat, $lng, $address, $link_options);
                ?>
            </div>
        </div>
 
        <style>
            .jvb-location-display {
                margin: 1rem 0;
            }
 
            .jvb-location-map-display {
                margin-bottom: 1rem;
                border: 1px solid #e0e0e0;
            }
 
            .jvb-location-details {
                display: flex;
                flex-wrap: wrap;
                gap: 1rem;
                align-items: center;
                font-size: 0.95rem;
            }
 
            .jvb-location-address {
                display: flex;
                align-items: center;
                color: #1B1B1B;
                flex: 1;
                min-width: 200px;
            }
 
            .jvb-location-address svg {
                margin-right: 0.5rem;
                color: #FF0080;
                width: 18px;
                height: 18px;
            }
 
            .jvb-map-links {
                display: flex;
                gap: 0.75rem;
            }
 
            .jvb-map-links.button-style .map-link {
                display: inline-flex;
                align-items: center;
                padding: 0.4rem 0.75rem;
                border-radius: 6px;
                background-color: #1B1B1B;
                color: #EFEFEF;
                text-decoration: none;
                transition: all 0.2s ease;
                font-size: 0.9rem;
            }
 
            .jvb-map-links.button-style .map-link svg {
                margin-right: 0.35rem;
                width: 16px;
                height: 16px;
            }
 
            .jvb-map-links.button-style .map-link:hover {
                background-color: #FF0080;
                transform: translateY(-1px);
            }
 
            .jvb-map-links.text-style .map-link {
                color: #FF0080;
                text-decoration: none;
                font-weight: 500;
            }
 
            .jvb-map-links.text-style .map-link:hover {
                text-decoration: underline;
            }
 
            .map-info-window {
                padding: 8px 12px;
                font-weight: 500;
                color: #1B1B1B;
            }
 
            @media (max-width: 768px) {
                .jvb-location-details {
                    flex-direction: column;
                    align-items: flex-start;
                    gap: 0.75rem;
                }
 
                .jvb-location-address {
                    min-width: auto;
                }
            }
        </style>
        <?php
 
        return ob_get_clean();
    }
 
    protected function renderSetField(string $name, string $value, array $field):string
    {
        if ($value === '') {
            return '';
        }
        $value = explode(',', $value);
 
        $output = '<div class="set-field-display row">';
 
        if (!empty($field['label']) && ($field['show_label'] ?? false)) {
            $output .= '<span class="set-label">' . esc_html($field['label']) . ':</span> ';
        }
 
        $items = [];
        foreach ($value as $key) {
            if (isset($field['options'][$key])) {
                $items[] = '<span class="set-item">' . esc_html($field['options'][$key]) . '</span>';
            }
        }
 
        $separator = $field['separator'] ?? ', ';
        $output .= implode($separator, $items);
        $output .= '</div>';
 
        return $output;
    }
 
    protected function renderCheckboxField(string $name, string $value, array $field):string
    {
        // Checkbox fields are essentially the same as set fields for display
        return $this->renderSetField($name, $value, $field);
    }
 
    protected function renderRadioField(string $name, string $value, array $field):string
    {
        if (empty($value) || !isset($field['options'][$value])) {
            return '';
        }
 
        $display_value = $field['options'][$value];
 
        $output = '<div class="radio-field-display">';
 
        if (!empty($field['label']) && ($field['show_label'] ?? false)) {
            $output .= '<span class="radio-label">' . esc_html($field['label']) . ':</span> ';
        }
 
        $output .= '<span class="radio-value">' . esc_html($display_value) . '</span>';
        $output .= '</div>';
 
        return $output;
    }
 
    protected function renderSelectField(string $name, string $value, array $field):string
    {
        // Select fields display the same as radio fields
        return $this->renderRadioField($name, $value, $field);
    }
 
    protected function renderTrueFalseField(string $name, bool $value, array $field):string
    {
        $display_text = $value ? ($field['true_text'] ?? 'Yes') : ($field['false_text'] ?? 'No');
        $css_class = $value ? 'true-value' : 'false-value';
 
        $output = '<div class="true-false-field-display">';
 
        if (!empty($field['label']) && ($field['show_label'] ?? false)) {
            $output .= '<span class="true-false-label">' . esc_html($field['label']) . ':</span> ';
        }
 
        $output .= '<span class="' . $css_class . '">' . esc_html($display_text) . '</span>';
        $output .= '</div>';
 
        return $output;
    }
 
    protected function renderTimeField(string $name, string $value, array $field):string
    {
        if (empty($value)) {
            return '';
        }
 
        // Format time for display
        $display_format = $field['display_format'] ?? 'g:i A'; // Default: 12-hour format with AM/PM
 
        // Parse the time value
        $timestamp = strtotime($value);
        if ($timestamp === false) {
            return '';
        }
 
        $formatted_time = date($display_format, $timestamp);
 
        // Add icon if configured
        $show_icon = $field['show_icon'] ?? true;
        $icon_html = $show_icon ? jvbIcon('clock') : '';
 
        return sprintf(
            '<div class="time-field-display">%s<span class="time-value">%s</span></div>',
            $icon_html,
            esc_html($formatted_time)
        );
    }
 
    protected function renderDatetimeField(string $name, string $value, array $field):string
    {
        if (empty($value)) {
            return '';
        }
 
        // Parse datetime
        $date = DateTime::createFromFormat('Y-m-d H:i:s', $value);
        if (!$date) {
            // Try alternative formats
            $formats = ['Y-m-d\TH:i:s', 'Y-m-d\TH:i', 'Y-m-d H:i'];
            foreach ($formats as $format) {
                $date = DateTime::createFromFormat($format, $value);
                if ($date) break;
            }
        }
 
        if (!$date) {
            return '';
        }
 
        // Format for display
        $display_format = $field['display_format'] ?? 'F j, Y \a\t g:i A'; // Default: March 15, 2024 at 2:30 PM
        $formatted_datetime = $date->format($display_format);
 
        // Add icon if configured
        $show_icon = $field['show_icon'] ?? true;
        $icon_html = $show_icon ? jvbIcon('calendar') : '';
 
        return sprintf(
            '<div class="datetime-field-display">%s<span class="datetime-value">%s</span></div>',
            $icon_html,
            esc_html($formatted_datetime)
        );
    }
 
    protected function renderDateField(string $name, string $value, array $field):string
    {
        if (empty($value)) {
            return '';
        }
 
        $timestamp = strtotime($value);
        if ($timestamp === false) {
            return '';
        }
 
        // Format for display
        $display_format = $field['display_format'] ?? 'F j, Y'; // Default: March 15, 2024
        $formatted_date = date($display_format, $timestamp);
 
        // Add icon if configured
        $show_icon = $field['show_icon'] ?? true;
        $icon_html = $show_icon ? jvbIcon('calendar') : '';
 
        return sprintf(
            '<div class="date-field-display">%s<span class="date-value">%s</span></div>',
            $icon_html,
            esc_html($formatted_date)
        );
    }
}