Jake Vanderwerf
2026-05-01 48721c85ebcfa973ee81719d2467ca80e4253dc9
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
<?php
namespace JVBase\registrar\config;
 
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
final class Section extends Config{
    protected string $title;
    protected string $slug;
    protected string $description = '';
    protected string $icon = '';
    protected array $fields = [];
    protected Registrar $registrar;
 
    public function __construct(string $title, Registrar $registrar) {
        $this->title = $title;
        $this->slug = sanitize_title($title);
        $this->registrar = $registrar;
    }
 
    public function setTitle(string $title): self {
        $this->title = $title;
        return $this;
    }
    public function getTitle():string
    {
        return $this->title;
    }
 
    public function setDescription(string $description):self
    {
        $this->description = $description;
        return $this;
    }
    public function getDescription():string|array
    {
        return $this->description;
    }
 
    public function setIcon(string $icon):self
    {
        $this->icon = $icon;
        return $this;
    }
 
    protected function checkFields(string|array $fields):string|array
    {
        $allFields =  $this->registrar->getFields();
        if (is_array($fields)){
            foreach ($fields as $index =>$fieldName){
                if (!array_key_exists($fieldName, $allFields)){
                    error_log('Attempted to add '.$fieldName.' to section. No field defined.');
                    unset($fields[$index]);
                }
            }
        } else {
            if (!array_key_exists($fields, $allFields)){
                error_log('Attempted to add '.$fields.' to section. No field defined.');
                return '';
            }
        }
        return $fields;
    }
 
 
 
    public function setFields(array $fields):self
    {
        $fields = $this->checkFields($fields);
        $this->fields = $fields;
        return $this;
    }
    public function addField(string $field):self
    {
        $field = $this->checkFields($field);
        if ($field !== ''){
            $this->fields[] = $field;
        }
        return $this;
    }
 
    public function getFields():array
    {
        return $this->fields;
    }
 
    /**
     * If the user has defined a 'section' key for this section for specific fields, they can autogenerate the section with this
     * @return $this
     */
    public function generateFields():self
    {
        $this->fields = array_keys(array_filter(Registrar::getInstance($this->slug)->getFields(), function($field){
            return array_key_exists('section', $field) && $field['section'] === $this->slug;
        }));
        return $this;
 
    }
 
    public function getConfig(): array
    {
        return [
            'slug'          => $this->slug,
            'title'         => $this->title,
            'description'   => $this->description,
            'icon'          => $this->icon,
            'fields'        => $this->fields,
        ];
    }
}