Jake Vanderwerf
2026-01-04 afdc1b396e3e88146006cd707a15b9da4843e1c2
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
/**
 * JVBase SEO Admin Interface
 *
 * Handles:
 * - Tab navigation
 * - Dynamic schema field loading
 * - Save/reset functionality
 * - Repeater field management
 */
(function($) {
    'use strict';
 
    const SEOAdmin = {
        config: window.jvbSeoConfig || {},
 
        init() {
            this.bindTabs();
            this.bindSchemaTypeChange();
            this.bindSaveButtons();
            this.bindResetButtons();
            this.bindRepeaterFields();
            this.bindImageSelectors();
        },
 
        /**
         * Tab navigation
         */
        bindTabs() {
            $('.jvb-seo-tabs .tab-btn').on('click', function() {
                const tab = $(this).data('tab');
 
                // Update active tab button
                $('.jvb-seo-tabs .tab-btn').removeClass('active');
                $(this).addClass('active');
 
                // Show corresponding content
                $('.tab-content').removeClass('active');
                $(`.tab-content[data-tab="${tab}"]`).addClass('active');
            });
        },
 
        /**
         * Dynamic schema field loading when type changes
         */
        bindSchemaTypeChange() {
            $(document).on('change', '.schema-type-select', async function() {
                const $select = $(this);
                const type = $select.val();
                const $fieldset = $select.closest('.jvb-seo-fieldset');
                const $schemaFields = $fieldset.find('.schema-fields');
 
                if (!type) {
                    $schemaFields.hide().empty();
                    return;
                }
 
                // Fetch fields for this schema type
                try {
                    const response = await fetch(
                        `${SEOAdmin.config.restUrl}schema-fields/${type}`,
                        {
                            headers: {
                                'X-WP-Nonce': SEOAdmin.config.nonce
                            }
                        }
                    );
 
                    const data = await response.json();
 
                    if (data.fields) {
                        SEOAdmin.renderSchemaFields($schemaFields, data.fields);
                        $schemaFields.show();
                    }
                } catch (error) {
                    console.error('Failed to load schema fields:', error);
                }
            });
        },
 
        /**
         * Render schema field mappings
         */
        renderSchemaFields($container, fields) {
            $container.empty();
 
            Object.entries(fields).forEach(([key, config]) => {
                const $field = $(`
                    <div class="form-field schema-field-mapping" data-field="${key}">
                        <label>${config.label || key}</label>
                        <input type="text" name="schema_fields[${key}]"
                               value="" class="regular-text template-field"
                               placeholder="{{field_name}}">
                        ${config.description ? `<span class="description">${config.description}</span>` : ''}
                    </div>
                `);
                $container.append($field);
            });
        },
 
        /**
         * Save button handlers
         */
        bindSaveButtons() {
            // Save site config
            $('#save-site-config').on('click', async function() {
                const $btn = $(this);
                $btn.prop('disabled', true).text('Saving...');
 
                const data = SEOAdmin.collectSiteFormData();
 
                try {
                    const response = await fetch(`${SEOAdmin.config.restUrl}site`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-WP-Nonce': SEOAdmin.config.nonce
                        },
                        body: JSON.stringify(data)
                    });
 
                    const result = await response.json();
 
                    if (result.success) {
                        SEOAdmin.showNotice('Settings saved successfully.', 'success');
                    } else {
                        SEOAdmin.showNotice(result.message || 'Failed to save.', 'error');
                    }
                } catch (error) {
                    SEOAdmin.showNotice('Network error. Please try again.', 'error');
                } finally {
                    $btn.prop('disabled', false).text('Save Site Settings');
                }
            });
 
            // Save content type config
            $(document).on('click', '.save-content-type', async function() {
                const $btn = $(this);
                const $container = $btn.closest('.jvb-seo-content-type');
                const type = $container.data('type');
                const objectType = $container.data('object-type');
 
                $btn.prop('disabled', true).text('Saving...');
 
                const data = SEOAdmin.collectContentTypeFormData($container);
 
                try {
                    const response = await fetch(
                        `${SEOAdmin.config.restUrl}content/${objectType}/${type}`,
                        {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                                'X-WP-Nonce': SEOAdmin.config.nonce
                            },
                            body: JSON.stringify(data)
                        }
                    );
 
                    const result = await response.json();
 
                    if (result.success) {
                        SEOAdmin.showNotice('Settings saved.', 'success');
                    } else {
                        SEOAdmin.showNotice(result.message || 'Failed to save.', 'error');
                    }
                } catch (error) {
                    SEOAdmin.showNotice('Network error.', 'error');
                } finally {
                    $btn.prop('disabled', false).text('Save');
                }
            });
        },
 
        /**
         * Reset button handlers
         */
        bindResetButtons() {
            $(document).on('click', '.reset-to-defaults', async function() {
                if (!confirm('Reset to default settings? This cannot be undone.')) {
                    return;
                }
 
                const $btn = $(this);
                const $container = $btn.closest('.jvb-seo-content-type');
                const type = $container.data('type');
                const objectType = $container.data('object-type');
 
                $btn.prop('disabled', true).text('Resetting...');
 
                try {
                    const response = await fetch(
                        `${SEOAdmin.config.restUrl}content/${objectType}/${type}/reset`,
                        {
                            method: 'POST',
                            headers: {
                                'X-WP-Nonce': SEOAdmin.config.nonce
                            }
                        }
                    );
 
                    const result = await response.json();
 
                    if (result.success) {
                        // Reload page to show defaults
                        location.reload();
                    } else {
                        SEOAdmin.showNotice(result.message || 'Failed to reset.', 'error');
                    }
                } catch (error) {
                    SEOAdmin.showNotice('Network error.', 'error');
                } finally {
                    $btn.prop('disabled', false).text('Reset to Defaults');
                }
            });
        },
 
        /**
         * Repeater field management
         */
        bindRepeaterFields() {
            // Add row
            $(document).on('click', '.repeater-field .add-row', function() {
                const $repeater = $(this).closest('.repeater-field');
                const fieldName = $repeater.data('field');
 
                const $row = $(`
                    <div class="repeater-row">
                        <input type="url" name="${fieldName}[]" value="" class="regular-text">
                        <button type="button" class="button remove-row">×</button>
                    </div>
                `);
 
                $repeater.find('.add-row').before($row);
            });
 
            // Remove row
            $(document).on('click', '.repeater-field .remove-row', function() {
                $(this).closest('.repeater-row').remove();
            });
        },
 
        /**
         * WordPress Media Library image selector
         */
        bindImageSelectors() {
            $(document).on('click', '.select-image', function(e) {
                e.preventDefault();
 
                const $button = $(this);
                const $field = $button.siblings('input[type="hidden"]');
                const $preview = $button.siblings('.image-preview');
 
                const frame = wp.media({
                    title: 'Select Image',
                    button: { text: 'Use Image' },
                    multiple: false
                });
 
                frame.on('select', function() {
                    const attachment = frame.state().get('selection').first().toJSON();
                    $field.val(attachment.id);
                    $preview.html(`<img src="${attachment.sizes?.thumbnail?.url || attachment.url}" style="max-height:50px;">`);
                });
 
                frame.open();
            });
        },
 
        /**
         * Collect site form data
         */
        collectSiteFormData() {
            const $form = $('[data-tab="site"]');
            const data = {};
 
            // Single fields
            $form.find('input[name], textarea[name], select[name]').each(function() {
                const name = $(this).attr('name');
                if (!name.endsWith('[]')) {
                    data[name] = $(this).val();
                }
            });
 
            // Repeater fields (sameAs)
            $form.find('[name="organization_sameas[]"]').each(function() {
                if (!data.organization_sameas) {
                    data.organization_sameas = [];
                }
                const val = $(this).val();
                if (val) {
                    data.organization_sameas.push(val);
                }
            });
 
            return data;
        },
 
        /**
         * Collect content type form data
         */
        collectContentTypeFormData($container) {
            const data = {
                meta_title: $container.find('[name="meta_title"]').val(),
                meta_description: $container.find('[name="meta_description"]').val(),
                schema_type: $container.find('[name="schema_type"]').val(),
                schema_fields: {}
            };
 
            // Collect schema field mappings
            $container.find('[name^="schema_fields"]').each(function() {
                const match = $(this).attr('name').match(/schema_fields\[(\w+)\]/);
                if (match) {
                    data.schema_fields[match[1]] = $(this).val();
                }
            });
 
            return data;
        },
 
        /**
         * Show notice message
         */
        showNotice(message, type = 'info') {
            const $notice = $(`
                <div class="notice notice-${type} is-dismissible">
                    <p>${message}</p>
                </div>
            `);
 
            // Remove existing notices
            $('.jvb-seo-admin .notice').remove();
 
            // Add new notice
            $('.jvb-seo-admin').prepend($notice);
 
            // Auto-dismiss after 5 seconds
            setTimeout(() => $notice.fadeOut(), 5000);
        }
    };
 
    // Initialize when DOM is ready
    $(document).ready(() => SEOAdmin.init());
 
})(jQuery);