Jake Vanderwerf
5 hours ago 56a9a1ccf764ff7a6af8f8a2292cb07443cb4aa7
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
<?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 left" 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 .= sprintf(
                '<button type="button" class="button tab%s" data-tab="%s" role="tab" aria-selected="%s"%s>',
                $active,
                $slug,
                $selected,
                $hidden
            );
 
            if ($tab->getIcon()) {
                $header .= jvbIcon($tab->getIcon());
            }
 
            $header .= sprintf('%s
                    </button>',
                $tab->getTitle()
            );
 
            // Content
            $ariaHidden = ($i === 0) ? 'false' : 'true';
            $content .= sprintf(
                '<div class="tab-content%s" data-tab="%s" role="tabpanel" aria-hidden="%s"',
                $active,
                $slug,
                $ariaHidden
            );
 
            if ($i !== 0) {
                $content .= ' hidden';
            }
 
            $content .= sprintf('>
                <h2>%s</h2>',
                $tab->getTitle(),
            );
 
            // Description
            if ($tab->getDescription()) {
                $description = $tab->getDescription();
                if (!is_array($description)) {
                    $content .= jvb_filter_content( $description);
                } else {
                    $content .= implode('', array_map(function ($paragraph) {
                        return jvb_filter_content( $paragraph);
                    }, $description));
                }
            }
 
            $content .= sprintf('%s
            </div>',
                $tab->getContent());
            $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);
    }
}