Jake Vanderwerf
2025-12-21 3aada9949d51024a92a8b5c6cb70d12f9c3cac16
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
<?php
namespace JVBase\ui;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Tabs UI component with fluent interface
 *
 * Usage:
 * $tabs = new Tabs();
 * $tabs->addTab('tab-slug')
 *      ->title('Tab Title')
 *      ->icon('iconName')
 *      ->description('Description text')
 *      ->content($content);
 * echo $tabs->render();
 */
class Tabs {
    private array $tabs = [];
    private int $counter = 0;
 
    /**
     * Add a new tab and return it for chaining
     *
     * @param string $slug Unique identifier for the tab
     * @return Tab
     */
    public function addTab(string $slug = ''): Tab {
        if (empty($slug)) {
            $slug = 'tab-' . ++$this->counter;
        }
 
        $tab = new Tab($slug);
        $this->tabs[$slug] = $tab;
        return $tab;
    }
 
    /**
     * Render all tabs as HTML
     *
     * @param bool $return Whether to return or echo the output
     * @return string
     */
    public function render(bool $return = true): string {
        if (empty($this->tabs)) {
            return '';
        }
 
        $header = '<nav class="tabs row start" role="tablist">';
        $content = '';
        $i = 0;
 
        foreach ($this->tabs as $slug => $tab) {
            if (!$tab->hasContent()) {
                error_log('No content for tab: ' . $slug);
                continue;
            }
 
            // Header
            $active = ($i === 0) ? ' active' : '';
            $selected = ($i === 0) ? 'true' : 'false';
            $hidden = $tab->isHidden() ? ' hidden' : '';
 
            $header .= '<button type="button" class="button tab' . $active . '" data-tab="' . $slug . '" role="tab" aria-selected="' . $selected . '"' . $hidden . '>
                <h2 class="row">';
 
            if ($tab->getIcon()) {
                $header .= jvbIcon($tab->getIcon());
            }
 
            $header .= $tab->getTitle() . '</h2>
            </button>';
 
            // Content
            $ariaHidden = ($i === 0) ? 'false' : 'true';
            $content .= '<div class="tab-content' . $active . '" data-tab="' . $slug . '" role="tabpanel" aria-hidden="' . $ariaHidden . '"';
 
            if ($i !== 0) {
                $content .= ' hidden';
            }
 
            $content .= '>
                <h2>' . $tab->getTitle() . '</h2>';
 
            // Description
            if ($tab->getDescription()) {
                $description = $tab->getDescription();
                if (!is_array($description)) {
                    $content .= apply_filters('the_content', $description);
                } else {
                    $content .= implode('', array_map(function ($paragraph) {
                        return apply_filters('the_content', $paragraph);
                    }, $description));
                }
            }
 
            $content .= $tab->getContent() . '
            </div>';
            $i++;
        }
 
        $header .= '</nav>';
        $out = $header . $content;
 
        if ($return) {
            return $out;
        }
 
        echo $out;
        return $out;
    }
}
 
/**
 * Individual tab with fluent interface for configuration
 */
class Tab {
    private string $slug;
    private string $title = '';
    private ?string $icon = null;
    private string|array|null $description = null;
    private string $content = '';
    private bool $hidden = false;
 
    public function __construct(string $slug) {
        $this->slug = $slug;
    }
 
    /**
     * Set the tab title
     *
     * @param string $title
     * @return self
     */
    public function title(string $title): self {
        $this->title = $title;
        return $this;
    }
 
    /**
     * Set the tab icon
     *
     * @param string $icon Icon name (used with jvbIcon())
     * @return self
     */
    public function icon(string $icon): self {
        $this->icon = $icon;
        return $this;
    }
 
    /**
     * Set the tab description (can be string or array)
     *
     * @param string|array $description
     * @return self
     */
    public function description(string|array $description): self {
        $this->description = $description;
        return $this;
    }
 
    /**
     * Set the tab content
     *
     * @param string $content
     * @return self
     */
    public function content(string $content): self {
        $this->content = $content;
        return $this;
    }
 
    /**
     * Mark tab as hidden
     *
     * @param bool $hidden
     * @return self
     */
    public function hidden(bool $hidden = true): self {
        $this->hidden = $hidden;
        return $this;
    }
 
    // Getters
    public function getTitle(): string {
        return $this->title;
    }
 
    public function getIcon(): ?string {
        return $this->icon;
    }
 
    public function getDescription(): string|array|null {
        return $this->description;
    }
 
    public function getContent(): string {
        return $this->content;
    }
 
    public function isHidden(): bool {
        return $this->hidden;
    }
 
    public function hasContent(): bool {
        return !empty($this->content);
    }
}