Jake Vanderwerf
21 hours ago f4be611c51473359e6d41780f0313c446079e9d3
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
<?php
namespace JVBase\base;
use JVBase\managers\Cache;
use JVBase\meta\Form;
use JVBase\meta\Meta;
use JVBase\registrar\Fields;
 
/**
 * Custom options for the site can be set here. They will need to be handled by the child plugin, but you can make use of the Meta.php this way
 */
 
$options = apply_filters('jvb_options', []);
define('JVB_OPTIONS', $options);
 
 
class Options {
    protected static Options $instance;
    protected array $resetFields = [];
    protected string $resetInterval = 'daily';
    protected Fields $fields;
    protected array $sections = [];
    protected static array $values = [];
 
    private function __construct(){
        $this->setFields();
        if (!empty($this->resetFields)) {
            add_action('jvbDailyReset', [$this, 'resetOptions']);
        }
    }
 
    public static function getInstance():Options {
        if (!isset(self::$instance)) {
            self::$instance = new self();
            do_action('jvb_define_options');
        }
        return self::$instance;
    }
 
    public function setFields():void
    {
        $this->fields = new Fields('options');
        if (Site::has('limit_hours')) {
            $this->fields->addField(
                'today_hours',
                [
                    'type'  => 'group',
                    'label' => 'Today\'s Hours',
                    'fields'    => [
                        'time_start'    => [
                            'type'  => 'time',
                            'label' => 'Open',
                        ],
                        'time_end'  => [
                            'type'  => 'time',
                            'label' => 'Closed',
                        ]
                    ]
                ]
            );
            $this->fields->addField(
                'open_to_public',
                [
                    'type'  => 'true_false',
                    'label' => 'Open to Public?'
                ]
            );
 
            if (Site::hasAnyIntegration(['facebook','instagram'])) {
                $this->fields->addField(
                    'post_to_social',
                    [
                        'type'  => 'true_false',
                        'label' => 'Post to Socials?',
                    ]
                );
            }
        }
 
        if (Site::hasIntegration('gmb') || Site::has('hours')) {
            $this->fields->addCommon('hours');
        }
    }
    public function fields():Fields
    {
        return $this->fields;
    }
 
    public function resetOptions():void
    {
        if (empty($this->resetFields)) {
            return;
        }
        foreach ($this->resetFields as $field) {
            self::delete($field);
        }
        Cache::for('options')->flush();
    }
 
    public static function get(string $fieldName):mixed
    {
        if (!array_key_exists($fieldName, self::$values)) {
            $meta = Meta::forOptions();
            self::$values[$fieldName] = $meta->get($fieldName);
        }
        return self::$values[$fieldName];
    }
    public static function delete(string $fieldName):void
    {
        if (array_key_exists($fieldName, self::$values)) {
            unset(self::$values[$fieldName]);
        }
        $meta = Meta::forOptions();
        $meta->delete($fieldName);
    }
    public static function set(string $fieldName, mixed $value):void
    {
        $meta = Meta::forOptions();
        $meta->set($fieldName, $value);
        self::$values = $value;
    }
 
    public static function render(array $fieldNames, array $options = [], bool $output = false):string
    {
        $meta = Meta::forOptions();
        $result = Form::renderFormFrom(
            $meta,
            'options',
            $options,
            $fieldNames
        );
        if ($output) {
            echo $result;
        }
        return $result;
    }
 
    public function getFields():array
    {
        return array_map(function ($field) {
            return $field->getConfig();
        },
            $this->fields->getFields()
        );
    }
}