Jake Vanderwerf
2026-03-03 772462eeca3002a1d52508aeba485aab2b4742ad
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
<?php
 
if (!defined('ABSPATH')) {
    exit;
}
 
use JVBase\managers\Cache;
use JVBase\meta\Form;
use JVBase\registrar\Registrar;
 
/**
 * For whatever reason, after much testing, it seems that
 *  good ol' WP resets post_parent if you call wp_update_post
 *  without explicitly setting the post_parent
 * This is a wrapper that grabs old data and merges it with
 *   what we're trying to update - reducing repetition
 * @param array $postArr as in wp_update_post
 * @param array $allowOverride an array of keys that are allowed to be overridden
 * @return int|WP_Error
 */
function jvb_update_post(array $postArr, array $allowOverride = []) {
    if (empty($postArr['ID'])) {
        return new WP_Error('missing_id', 'Post ID is required');
    }
 
    $old = get_post($postArr['ID'], ARRAY_A);
    if (!$old) {
        return new WP_Error('invalid_id', 'Post not found');
    }
    /**
     * WARNING: You won't want to override fields like:
     * guid
     * filter
     * ancestors
     * post_category
     * tags_input
     * to_ping
     * pinged
    **/
    $preserveFields = [
        'post_parent', 'menu_order',
        'post_status', 'post_password', 'comment_status', 'ping_status',
        'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt',
        'post_name', 'post_title', 'post_excerpt', 'post_content',
        'post_author'
    ];
    // Remove fields we explicitly want to override
    $preserveFields = array_diff($preserveFields, $allowOverride);
 
    // Keep only preserved fields from old post
    $old = array_intersect_key($old, array_flip($preserveFields));
 
    // Merge old → new (new wins)
    $merged = array_merge($old, $postArr);
    $merged['ID'] = (int)$postArr['ID'];
 
    return wp_update_post($merged, true);
}
 
/**
 * @deprecated use CRUDManager.php or CRUDSkeleton.php
 * Outputs the blocks of a CRUD management in backend
 * Mainly used in news.php so far
 * @param string $content
 * @param array|null $statusFilters
 * @param array|null $bulkEdit
 *
 * @return string
 */
function jvbCrudManagement(
    string $content,
    array|null $statusFilters = null,
    array|null $bulkEdit = null
):string {
    $statusFilters    = ($statusFilters) ?: [
        [
            'id'    => 'all',
            'icon'  => 'all',
            'label' => 'Everything',
        ],
        [
            'id'    => 'publish',
            'icon'  => 'show',
            'label' => 'Live',
        ],
        [
            'id'    => 'draft',
            'icon'  => 'hide',
            'label' => 'Hidden',
        ],
        [
            'id'    => 'trash',
            'icon'  => 'delete',
            'label' => 'Scrapped',
        ]
    ];
    $permission = ($content === 'news') ? 'update' : $content;
    $permission = JVB_CONTENT[$content]['plural']??$content.'s';
 
    $canPublish = current_user_can("publish_{$permission}");
    $out = '<div class="filters">';
    if (!empty($statusFilters)) {
        $out .= '<div class="status">';
        foreach ($statusFilters as $filter) {
            $disabled = ($filter['id'] === 'publish' && !$canPublish) ? ' disabled' : '';
            $status = esc_attr($filter['id']);
            $title = esc_html($filter['label']);
            $out .= sprintf(
                '<input type="radio"
                   name="status"
                   value="%s"
                   id="set-%s"%s>
                <label for="set-%s">
                    %s
                    <span>%s</span>
                </label>',
                $status,
                $status,
                $disabled,
                $status,
                jvbIcon($filter['icon']),
                $title
            );
        }
        if (!$canPublish) {
            $out .= '<p class="description">Your account needs to be verified before you can publish '.$content.'.</p>';
        }
        $out .= '</div>';
    }
 
    $bulkActions = jvbGetBulkActions($content);
 
    if (!empty($bulkActions)) {
        $out .= '<div class="bulk-controls">
                <div class="bulk-select">
                    <input type="checkbox" id="select-all" class="select-all">
                    <label for="select-all">Select All<span class="selected-count"></span></label>
                </div>
                <div class="bulk-actions" hidden>
                    <select class="bulk-action-select">
                        <option value="">Bulk Actions...</option>';
        foreach ($bulkActions as $status => $control) {
            $disabled = ($control['disabled']) ? ' disabled' : '';
            $out .= sprintf(
                '<option value="%s"%s>%s</option>',
                esc_attr($status),
                $disabled,
                esc_html($control['label'])
            );
        }
        $out .= sprintf(
            '</select>
                <button type="button" class="apply-bulk">Apply</button>
                <button type="button" class="cancel-bulk">
                        %s
                        Clear
                    </button>
                </div>
            </div>',
            jvbIcon('x', ['title'=>'Cancel'])
        );
    }
    $out .= '</div>';
 
    return $out;
}
/**
 * Outputs available actions
 * mainly used by news.php
 * @param string $content
 *
 * @return array|array[]
 */
function jvbGetBulkActions(string $content):array
{
 
    $permission = ($content === 'news') ? 'update' : $content;
    $permission = JVB_CONTENT[$content]['plural']??$content.'s';
    $bulkActions = [
        'publish' => [
            'icon'        => 'show',
            'label'        => 'Show',
            'disabled'    => true,
        ],
        'edit'    => [
            'icon'        => 'edit',
            'label'        => 'Edit',
            'disabled'    => true,
        ],
        'draft'    => [
            'icon'        => 'hide',
            'label'        => 'Hide',
            'disabled'    => true,
        ],
        'trash'    => [
            'icon'        => 'delete',
            'label'        => 'Scrap',
            'disabled'    => true,
        ]
    ];
    if (current_user_can("publish_{$permission}")) {
        $bulkActions['publish']['disabled'] = false;
    }
    if (current_user_can("edit_{$permission}")) {
        $bulkActions['edit']['disabled'] = false;
        $bulkActions['draft']['disabled'] = false;
    }
    if (current_user_can("delete_{$permission}")) {
        $bulkActions['trash']['disabled'] = false;
    }
 
    return $bulkActions;
}
 
/**
 * Outputs the date filters for a content type
 * @param string $content
 *
 * @return string
 */
function jvbRenderDateFilter(string $content):string
{
    $cache = Cache::for('date_filter')->connect('post', true);
    $check = $cache->get($content);
    if ($check) {
        return $check;
    }
 
    $postType = (str_starts_with($content, BASE)) ? $content : BASE. $content;
    // Get available months
    global $wpdb;
    $months = $wpdb->get_results("
    SELECT DISTINCT
        YEAR(post_date) as year,
        MONTH(post_date) as month
    FROM $wpdb->posts
    WHERE post_type = '{$postType}'
    ORDER BY post_date DESC
");
 
    // Quick filters
    $out = '<div>
        <label for="filter-date">'.
           jvbIcon('calendar', ['title'=>'Date']).
           '<span class="screen-reader-text">Filter by Date</span>
        </label>
        <select id="filter-date" class="date-filter" name="date-filter">
            <option value="">[ Date ]</option>
            <option value="today">Today</option>
            <option value="week">Past Week</option>
            <option value="month">Past Month</option>
            <option value="year">Past Year</option>
            <option value="custom">Custom Range...</option>
        </select>
    </div>';
 
    // Custom date range
    $out .= '<dialog class="date-range" >
        <div class="wrap">
            <div class="custom-range row">
                <label class="col">
                    <span>From</span>
                    <input type="date" class="date-start">
                </label>
                <label class="col">
                    <span>To</span>
                    <input type="date" class="date-end">
                </label>
            </div>
            <div class="month-picker">
                <label>
                    <span>Or select month</span>
                    <select class="month-select">
                        <option value="">&emsp; . . . &emsp;</option>';
 
    foreach ($months as $date) {
        $month_name = date('F Y', mktime(0, 0, 0, $date->month, 1, $date->year));
        $value = $date->year . '-' . str_pad($date->month, 2, '0', STR_PAD_LEFT);
        $out .= sprintf(
            '<option value="%s">%s</option>',
            esc_attr($value),
            esc_html($month_name)
        );
    }
 
    $out .= '</select>
                </label>
            </div>
        </div>
    </dialog>';
 
    $cache->set($content, $out);
 
    return $out;
}
 
 
/**
 * Renders sections based on what was set in the Content Registry
 * @param object $handler
 * @param int $ID
 * @param string $contentType
 * @param string $postType
 * @param bool $prefix
 *
 * @return void
 */
function jvbRenderSections(
    int $ID,
    string $contentType = 'post',
    string $postType = '',
    bool $prefix = false
):void {
    switch ($contentType) {
        case 'post':
            $settings = JVB_CONTENT;
            break;
        case 'term':
            $settings = JVB_TAXONOMY;
            break;
        case 'user':
            $settings = JVB_USER;
            break;
        default:
            return;
    }
    $sections = $settings[$postType]['sections']??[];
    if (empty($sections)) {
        return;
    }
 
    echo '<div class="container">';
    $nav = '<nav class="tabs row start" role="tablist">';
    $i = 1;
    foreach ($sections as $slug => $section) {
        $nav .= '<button type="button" class="tab';
 
        $ariaActive = 'false';
        if ($i === 1) {
            $nav .= ' active';
            $ariaActive = 'true';
        }
        $tabName = ($prefix) ? $ID.'-'.$slug : $slug;
        $nav .= '" data-tab="'.$tabName.'" role="tab" aria-selected="'.$ariaActive.'">
            <h2>'.jvbIcon($section['icon']).$section['label'].'</h2></button>';
        $i++;
    }
    $nav .= '</nav>';
    echo $nav;
 
    $fields = Registrar::getFieldsFor($postType);
    ?>
    <form class="jvb-form" id="bio" data-form-id="bio-<?=$ID?>" data-save="bio"
          data-object-id="<?=$ID?>" data-content-type="<?=$postType?>">
        <?php
        $i = 0;
        foreach ($sections as $slug => $section) {
            $tabName = ($prefix) ? $ID.'-'.$slug : $slug;
 
            $class = ($i == 0) ? ' active' : '';
            ?>
            <section id="<?= $slug ?>" class="tab-content<?=$class?>" data-tab="<?=$tabName?>" role="tabpanel">
                <?php if (!empty($section['title']) && !$prefix) : ?>
                    <h2><?= esc_html($section['title']); ?></h2>
                <?php endif; ?>
 
                <?php if (!empty($section['description'])) : ?>
                    <p class="section-description">
                        <?= wp_kses_post($section['description']); ?>
                    </p>
                <?php endif; ?>
 
 
                <?php
                $sectionFields = array_filter($fields, function ($f) use ($slug) {
                    return array_key_exists('section', $f) && $f['section'] == $slug;
                });
                foreach ($sectionFields as $field => $config) : ?>
                    <?php
                    if ($config['type'] == 'callback') {
                        $callback = $config['callback'];
                        echo $callback($ID);
                    } else {
                        if (array_key_exists('role', $config)) {
                            $user = get_userdata($ID);
                            if (in_array($config['role'], $user->roles)) {
                                echo Form::render($field, null, $config);
                            }
                        } else {
                            echo Form::render($field, null, $config);
                        }
                    }
                    ?>
                <?php endforeach; ?>
            </section>
            <?php
            $i++;
        }
        ?>
    </form>
    </div>
    <?php
}