Jake Vanderwerf
2026-01-11 181938ea32cf2c11d10d56018df22586adff7860
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
<?php
namespace JVBase\managers;
 
use JVBase\ui\CRUDSkeleton;
use JVBase\utility\Features;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * WordPress CRUD Manager
 * Configures CRUDSkeleton for WordPress post types
 */
class CRUD {
    protected CRUDSkeleton $skeleton;
    protected CacheManager $cache;
    protected array $config;
    protected string $content;
    protected array $taxonomies = [];
    protected int $user_id;
    protected ?string $type = null;
    protected ?array $constant = null;
 
    public function __construct(string $content) {
        if (array_key_exists($content, JVB_CONTENT)) {
            $this->type = 'post';
            $this->constant = JVB_CONTENT;
        } elseif (array_key_exists($content, JVB_TAXONOMY)) {
            $this->type = 'term';
            $this->constant = JVB_TAXONOMY;
        } elseif (array_key_exists($content, JVB_USER)) {
            $this->type = 'user';
            $this->constant = JVB_USER;
        } else {
            return;
        }
 
        $this->user_id = get_current_user_id();
        $this->config = $this->constant[$content];
        $this->content = $content;
        $this->cache = CacheManager::for($content);
 
        $this->cache->clear();
 
        // Create and configure skeleton
        $this->skeleton = new CRUDSkeleton();
        $this->configure();
    }
 
    /**
     * Configure CRUDSkeleton from WordPress config
     */
    protected function configure(): void {
        // Basic info
        $this->skeleton
            ->content($this->content, $this->config['singular'], $this->config['plural'])
            ->title(
                'Your ' . $this->config['plural'],
                $this->config['page_description'] ?? ''
            );
 
        // Initialize meta
        $this->skeleton->initMeta($this->type, $this->content);
 
        $this->skeleton->addSearch();
 
        // Timeline if applicable
        if (Features::forContent($this->content)->has('is_timeline')) {
            $this->skeleton->setTimeline();
        }
 
        // Fields and sections
        $this->skeleton->setFields($this->config['fields']);
 
        $sections = array_key_exists('sections', $this->config) ? $this->config['sections'] : [];
        foreach ($sections as $id => $config) {
            $this->skeleton->addSection($id, $config);
        }
 
        // Taxonomies
        $this->initTaxonomies();
 
        // Statuses
        if (Features::forContent($this->content)->has('is_calendar')) {
            $this->skeleton->setCalendar();
        }
 
        $this->skeleton->setDefaultStatus();
 
        // Views
        $this->skeleton
            ->addViews()
            ->defaultView('grid');
        $this->skeleton->addItemActions();
 
        // Filters
        $this->skeleton->addDateFilter();
        $this->skeleton->addCustomDateRange($this->addDateRanges());
        if (!empty($this->taxonomies)) {
            $this->skeleton->addTaxonomyFilter(array_keys($this->taxonomies), 'user');
        }
 
        // Capabilities
        $this->skeleton->addCapabilities(['view', 'edit', 'create', 'delete']);
 
        $plural = strtolower($this->config['plural'] ?? $this->content . 's');
        $canPublish = jvbUserIsVerified() && user_can($this->user_id, "publish_{$plural}");
        $this->skeleton->userCanPublish($canPublish);
 
        // Bulk actions
        $this->skeleton->addBulkActions(['edit', 'publish', 'draft', 'trash']);
 
        // Uploader
        $this->setupUploader();
 
        // Sticky fields
        $stuck = ['post_title', 'term_name'];
        if ($this->skeleton->get('isTimeline')) {
            $stuck[] = 'post_thumbnail';
        }
        $this->skeleton->stickFields($stuck);
 
        // Hook for create button
        add_filter('jvbAdditionalActions', [$this, 'createItem']);
    }
 
    /**
     * Setup uploader configuration
     */
    protected function setupUploader(): void {
        $isSingleImage = jvbCheck('single_image', $this->config);
 
        $config = [
            'type' => 'upload',
            'subtype' => 'image',
            'mode' => $isSingleImage ? 'direct' : 'selection',
            'create_new' => true,
            'label' => $this->config['upload_title'] ?? 'Bulk Upload ' . $this->config['plural'],
            'content' => $this->content,
            'singular' => $this->config['singular'],
            'plural' => $this->config['plural'],
            'multiple' => true,
            'destination' => $isSingleImage ? 'post' : 'post_group'
        ];
 
        if (!$isSingleImage) {
            $config['upload_text'] = '<p>Drag images into groups. Each group becomes its own ' . $this->config['singular'] . '.</p>
                <p>You can also select multiple images and click the "Add to Group" button.</p>
                <p>If a ' . $this->config['singular'] . ' has multiple images, you can select the ' . jvbDashIcon('star') . ' to set an image as the main one.</p>
                <p>Images left ungrouped will become individual ' . $this->config['plural'] . '</p>
                <p>Once finished, click the \'Save Changes\' button to send to server for processing.</p>';
        } else {
            $config['description'] = 'Each image will become its own ' . $this->config['singular'] . '.';
        }
 
        $this->skeleton->addUploader($config);
    }
 
    /**
     * Initialize taxonomies from WordPress config
     */
    protected function initTaxonomies(): void {
        $this->taxonomies = array_filter(JVB_TAXONOMY, function ($config) {
            return in_array($this->content, $config['for_content']);
        });
    }
 
    /**
     * Get statuses - calendar or standard
     */
    protected function getStatuses(): array {
        return array_key_exists('is_calendar', $this->config) ?
            [
                'all' => [
                    'icon' => 'calendar',
                    'label' => 'Everything',
                ],
                'future' => [
                    'label' => 'Upcoming',
                    'icon' => 'clock-clockwise',
                ],
                'past' => [
                    'label' => 'Past',
                    'icon' => 'clock-counter-clockwise',
                ],
                'repeat' => [
                    'label' => 'Recurring',
                    'icon' => 'repeat',
                ],
                'draft' => [
                    'icon' => 'eye-closed',
                    'label' => 'Hidden',
                ],
                'trash' => [
                    'label' => 'Scrapped',
                    'icon' => 'trash',
                ],
            ] :
            [
                'all' => [
                    'icon' => 'infinity',
                    'label' => 'Everything',
                ],
                'publish' => [
                    'icon' => 'eye',
                    'label' => 'Live',
                ],
                'draft' => [
                    'icon' => 'eye-closed',
                    'label' => 'Hidden',
                ],
                'trash' => [
                    'label' => 'Scrapped',
                    'icon' => 'trash',
                ],
            ];
    }
 
    /**
     * Add create button to dashboard actions
     */
    public function createItem(array $actions): array {
        $actions[] = [
            'button' => '<button type="button" class="create-item row" title="Create New ' . $this->config['singular'] . '">'
                . jvbDashIcon('plus-square')
                . '<span class="screen-reader-text">Create New ' . $this->config['singular'] . '</span></button>',
            'content' => '', // Modal is rendered by skeleton
        ];
 
        return $actions;
    }
 
    protected function addDateRanges():array
    {
        return $this->cache->remember(
            'dateRanges',
            function() {
                $postType = jvbCheckBase($this->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}'
            AND post_author = '{$this->user_id}'
            ORDER BY post_date DESC
        ");
                $ranges = [];
                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);
                    $ranges[$value] = $month_name;
                }
                return $ranges;
            }
        );
    }
 
    /**
     * Render the interface
     */
    public function render(): void {
        $this->skeleton->render();
    }
 
    /**
     * Get the skeleton instance for further customization
     */
    public function getSkeleton(): CRUDSkeleton {
        return $this->skeleton;
    }
}