Jake Vanderwerf
2026-02-09 83985b1f1534d70cca59edb01627638c890116cb
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
<?php
namespace JVBase\managers\SEO;
 
use JVBase\managers\AdminPages;
use JVBase\meta\Form;
use JVBase\ui\Tabs;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Admin interface for SEO configuration
 *
 * Provides UI for configuring meta tags and schema for content types.
 * Now includes live schema preview functionality.
 *
 */
class SEOAdminPage
{
    private ConfigManager $config;
    private SchemaBuilder $registry;
 
    public function __construct()
    {
        $this->registry = SchemaBuilder::getInstance();
 
 
        // Add to JVB dashboard
        add_filter('jvbDashboardPage', [$this, 'addDashboardSection'], 20, 2);
        add_action('admin_enqueue_scripts', [$this, 'enqueueScripts']);
    }
 
    public function enqueueScripts():void
    {
        global $_GET;
        if (array_key_exists('page', $_GET) && $_GET['page'] === BASE.'seo') {
            wp_enqueue_script('jvb-form');
            wp_enqueue_script('jvb-schema');
        }
    }
 
    public static function addSubpage():void
    {
        $subpage = [
            'page_title' => 'SEO Settings',
            'menu_title' => 'SEO',
            'capability' => 'manage_options',
            'menu_slug' => BASE . 'seo',
            'callback' => [self::class, 'renderAdminPageStatic']
        ];
        AdminPages::addSubPage(BASE.'seo', $subpage);
    }
 
    public static function renderAdminPageStatic():void
    {
        JVB()->seoAdmin()->renderAdminPage();
    }
 
    /**
     * Add section to JVB dashboard
     */
    public function addDashboardSection(string $content, string $page): string
    {
        if ($page !== 'SEO') {
            return $content;
        }
        ob_start();
        $this->renderAdminPage();
        return ob_get_clean();
    }
 
    /**
     * Render admin page
     */
    public function renderAdminPage(bool $outputScripts = true): void
    {
        ?>
        <div class="wrap jvb-seo-admin">
            <h1><?= jvbDashIcon('magnifying-glass'); ?> SEO Configuration</h1>
 
            <?php
            $tabs = new Tabs();
 
            $tabs->addTab('main')
                ->title('Website & Business')
                ->icon('storefront')
                ->content($this->renderMain());
 
            $tabs->addTab('content')
                ->title('Content')
                ->icon('note')
                ->content($this->renderConfig('content'));
 
            $tabs->addTab('taxonomies')
                ->title('Taxonomies')
                ->icon('tag')
                ->content($this->renderConfig('taxonomy'));
 
            echo $tabs->render();
            ?>
        </div>
 
        <?php
        $this->renderTemplates();
        if ($outputScripts) {
            $this->renderStyles();
        }
    }
 
    protected function renderForm(string $key, ?string $type = null, string $configType = 'schema'):string
    {
        if (!in_array($configType, ['meta', 'schema', 'archive'])) {
            return '';
        }
 
        $this->config = ConfigManager::for($key);
        // Setup archive if needed
        if ($configType === 'archive') {
            $this->config->setupArchive();
            $config = $this->config->archive();
        } elseif ($configType === 'schema') {
            $config = $this->config->schema();
        } else { // meta
            $config = $this->config->meta();
        }
 
        if (!$type) {
            $type = (array_key_exists('type', $config)) ? $config['type'] : 'WebPage';
        }
        $fields = ($configType === 'meta') ? $this->registry->getMetaFields() : $this->registry->getFieldsForType($type);
        $registry = $this->registry->getTypeDefinition($type);
        ob_start();
        ?>
        <form data-save="seo" data-content="<?=$key?>">
            <input type="hidden" name="context" value="<?=$key?>">
            <fieldset>
                <legend><?= $this->registry->getTypeDefinition($type)['label']??ucwords($key) ?></legend>
                <?php
                $exclude = ['creator'];
                foreach ($fields as $index => $fieldName) {
                    if (in_array($fieldName, $exclude) ) {
                        continue;
                    }
                    if ($index === 0 && $fieldName !== 'type') {
                        echo '<div class="seo-'.$type.'">';
                    }
                    $fieldConfig = $this->registry->getFieldDefinition($fieldName);
                    if (!$fieldConfig) {
                        continue;
                    }
                    echo Form::render($fieldName, $config[$fieldName]??'', $fieldConfig);
                    if ($index === 0 && $fieldName === 'type') {
                        echo '<div class="seo-'.$type.'">';
                    }
                }
                ?>
            </div>
            </fieldset>
            <div class="row nowrap">
                <button type="button" data-action="reset" style="width:max-content"><?= jvbDashIcon('arrow-counter-clockwise')?> Reset</button>
                <button type="submit"><?=jvbDashIcon('floppy-disk') ?> Save <?=$registry['label']??ucwords($key)?></button>
            </div>
        </form>
        <?php
        return ob_get_clean();
    }
 
    protected function renderMain():string
    {
        $business = ConfigManager::for('organization');
        $savedBusiness = $business->schema()['type'] ?? 'Organization';
 
        $tabs = new Tabs();
 
        $tabs->addTab('website')
            ->title('WebSite Schema')
            ->icon('globe-simple')
            ->description('This is the main definition for your website')
            ->content($this->renderForm('website', 'WebSite'));
 
        $tabs->addTab('organization')
            ->title('Organization Schema')
            ->icon('storefront')
            ->description('Define your organization or local business here.')
            ->content($this->renderForm('organization', $savedBusiness));
 
        return $tabs->render();
    }
 
    protected function renderConfig(string $type):string
    {
        $types = ['meta', 'schema'];
 
        switch ($type) {
            case 'content':
                $config = JVB_CONTENT;
                $types[] = 'archive';
                break;
            case 'taxonomy':
            case 'taxonomies':
                $config = JVB_TAXONOMY;
                break;
            case 'user':
                $config = JVB_USER;
                break;
            default:
                error_log('[SEOAdminPage]:renderConfig --- no config found for '.$type);
                return '';
        }
 
        $mainTabs = new Tabs();
 
        foreach ($config as $c => $opt) {
            $subTabs = new Tabs();
 
            foreach ($types as $t) {
                $tab = $subTabs->addTab($c.'_'.$t);
 
                switch ($t) {
                    case 'meta':
                        $tab->title('Meta')
                            ->icon('folders')
                            ->description('The title and description are used when a link is shared to social media and a preview shows, or in the search engine result for this page.')
                            ->content($this->renderForm($c, null, $t));
                        break;
 
                    case 'schema':
                        $tab->title('Schema')
                            ->icon('robot')
                            ->description('Defining the schema helps search engines understand what the content of this page is about.')
                            ->content($this->renderForm($c, null, $t));
                        break;
 
                    case 'archive':
                        $tab->title('Archive')
                            ->icon('hard-drives')
                            ->description('The archive is similar to the per-post schema for this content, but is generally a CollectionPage of some sort.')
                            ->content($this->renderForm($c, null, $t));
                        break;
                }
            }
 
            $mainTabs->addTab($c)
                ->title($opt['plural'])
                ->icon($opt['icon'])
                ->content($subTabs->render());
        }
 
        return $mainTabs->render();
    }
 
    /**
     * Render admin styles
     */
    private function renderStyles(): void
    {
        jvbInlineStyles('forms');
    }
 
    protected function renderTemplates():void
    {
        $types = array_keys($this->registry->schemaTypes);
        foreach ($types as $type) {
            ?>
            <template class="seo-<?=$type?>">
                <div class="seo-<?=$type?>">
                    <?php
                    $fields = $this->registry->getFieldsForType($type);
                    foreach ($fields as $fieldName) {
                        $config = $this->registry->getFieldDefinition($fieldName);
                        if (!$config) {
                            continue;
                        }
                        echo Form::render($fieldName, '', $config);
                    }
                    ?>
                </div>
            </template>
            <?php
        }
    }
}