Jake Vanderwerf
2026-02-04 2127b1bdd73ecd2423e443992da4b442f5a3c1a3
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
<?php
namespace JVBase\registry;
 
if (!defined('ABSPATH')) {
    exit;
}
 
use JVBase\meta\Form;
use JVBase\meta\Meta;
 
class OptionsRegistry
{
    private array $fields;
 
    public function __construct(array $fields)
    {
        $this->fields = $fields;
        add_action('init', [$this, 'registerOptions'], 10);
        add_action('admin_init', [$this, 'registerSettings']);
    }
 
    /**
     * Register options
     */
    public function registerOptions(): void
    {
        $meta = Meta::forOptions('options');
        foreach ($this->fields as $field_name => $field) {
            if (in_array($field_name, ['common', 'fields'])) {
                continue;
            }
 
            // Register the option
            $option_name = BASE . $field_name;
 
 
            // Add default value if not exists
            if (get_option($option_name) === false) {
                add_option($option_name, $field['default'] ??'');
            }
 
//
//          // Register for REST API
//          register_setting('jvb_options', $option_name, [
//              'type'              => $this->getFieldType($field['type']),
//              'description'       => $field['description'] ?? '',
//              'sanitize_callback' => $meta->getSanitizeCallback($field),
//              'show_in_rest'      => $field['show_in_rest'] ?? true,
//              'default'           => $field['default'] ?? '',
//          ]);
        }
    }
 
    /**
     * Register settings for admin
     */
    public function registerSettings(): void
    {
        // Register settings page
        add_settings_section(
            'jvb_options_section',
            __('Site Options', 'jvb'),
            null,
            'jvb_options'
        );
 
        foreach ($this->fields as $field_name => $field) {
            if (in_array($field_name, ['common', 'fields'])) {
                continue;
            }
 
            add_settings_field(
                BASE . $field_name,
                $field['label'] ?? $field_name,
                [$this, 'renderField'],
                'jvb_options',
                'jvb_options_section',
                ['field' => $field, 'name' => $field_name]
            );
        }
    }
 
    /**
     * Render field in admin
     */
    public function renderField(array $args): void
    {
        $field = $args['field'];
        $name = $args['name'];
        $value = get_option(BASE . $name);
 
        echo Form::render($name, $value, $field);
    }
 
    private function getFieldType(string $type): string
    {
        return match ($type) {
            'text', 'textarea', 'url', 'email' => 'string',
            'number' => 'number',
            'true_false', 'checkbox' => 'boolean',
            'location', 'group', 'repeater' => 'array',
            default => 'string'
        };
    }
}