Jake Vanderwerf
10 days ago de317675a8069b747cb253ba3e2b5dc394ca36ef
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
<?php
namespace JVBase\integrations;
 
use JVBase\base\Options;
use JVBase\meta\Form;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
use JVBase\ui\Tabs;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Handles the getting and setting of default values for service fields.
 */
trait SyncDefaults {
    use _Base;
    protected array $allContent;
    protected array $enabledTaxonomies;
    protected array $enabledContent;
    protected array $enabledUsers;
    public function enabledContentTypes():array
    {
        //Build an array of post type/taxonomy slugs => integration content type
        if (!isset($this->allContent)) {
            $content = Registrar::withIntegration($this->service_name);
            $this->allContent = [];
            foreach ($content as $c) {
                $registrar = Registrar::getInstance($c);
                if ($registrar){
                    $this->allContent[$c] = $registrar->getIntegration($this->service_name)->getContentType();
                }
            }
        }
        return $this->allContent;
    }
    public function enabledContent():array
    {
        if (!isset($this->enabledContent)) {
            $content = Registrar::withIntegration($this->service_name, 'post');
            $this->enabledContent = [];
            foreach ($content as $c) {
                $registrar = Registrar::getInstance($c);
                if ($registrar){
                    $this->enabledContent[$c] = $registrar->getIntegration($this->service_name)->getContentType();
                }
            }
        }
        return $this->enabledContent;
    }
    public function enabledTaxonomies():array
    {
        if (!isset($this->enabledTaxonomies)) {
            $content = Registrar::withIntegration($this->service_name, 'term');
            $this->enabledTaxonomies = [];
            foreach ($content as $c) {
                $registrar = Registrar::getInstance($c);
                if ($registrar){
                    $this->enabledTaxonomies[$c] = $registrar->getIntegration($this->service_name)->getContentType();
                }
            }
        }
        return $this->enabledTaxonomies;
    }
    public function enabledUsers():array
    {
        if (!isset($this->enabledUsers)) {
            $content = Registrar::withIntegration($this->service_name, 'user');
            $this->enabledUsers = [];
            foreach ($content as $c) {
                $registrar = Registrar::getInstance($c);
                if ($registrar){
                    $this->enabledUsers[$c] = $registrar->getIntegration($this->service_name)->getContentType();
                }
            }
        }
        return $this->enabledUsers;
    }
 
    public function renderDefaults():void
    {
        $tabs = new Tabs();
 
        $types = [
            'enabledContent' => [
                'title' => 'Content',
                'slug'  => 'content',
                'icon'  => 'note-blank',
                'description'   => 'Set default values for the meta for different content types here.'
            ],
            'enabledTaxonomies' => [
                'title' => 'Taxonomies',
                'slug'  => 'taxonomies',
                'icon'  => 'folder',
                'description'   => 'Set default values for the meta for different taxonomies here.'
            ],
            'enabledUsers' => [
                'title' => 'Users',
                'slug'  => 'users',
                'icon'  => 'user',
                'description'   => 'Set default values for the meta for different user types here.'
            ]
        ];
 
        foreach ($types as $method => $options) {
            $registered = $this->$method();
            if (empty($registered)) {
                continue;
            }
            $role = !is_null($this->userID) ? jvbUserRole($this->userID) : '';
            $userRegistrar = null;
            if (!empty($role)) {
                $userRegistrar = Registrar::getInstance($role);
            }
            $allowed = is_null($this->userID) ?
                array_keys($registered) :
                array_filter(array_keys($registered), function ($type) use ($userRegistrar) {
                    return in_array($type, $userRegistrar->getCreatable());
                });
            if (empty($allowed)) {
                continue;
            }
 
            $tab = $tabs->addTab($options['slug'])
                ->icon($options['icon'])
                ->title($options['title'])
                ->description($options['description'])
                ->content($this->getContentTypeContent($allowed));
        }
        $tabs->render(false);
    }
    public function getContentTypeContent(array $content):string
    {
        $tabs = new Tabs();
        foreach ($content as $c) {
            $registrar = Registrar::getInstance($c);
            $additionalFields = $registrar->getIntegrationFields($this->service_name);
            $fields = $additionalFields->getIntegrationFields();
            if (empty($fields)) {
                continue;
            }
            $form = '';
            foreach ($fields as $name => $config) {
                $form .= Form::render(
                    $this->getFieldName($name, $c),
                    $this->getDefaultValue($name, $c, $this->userID),
                    $config
                );
            }
 
            $tab = $tabs->addTab($c)
                ->icon($registrar->getIcon())
                ->title($registrar->getPlural())
                ->description('Set default values for the meta for '.$registrar->getPlural().' here.')
                ->content($form);
        }
        return $tabs->render();
    }
 
    protected function getFieldName(string $fieldName, string $type):string
    {
        return $type.'_defaults_'.$fieldName;
    }
 
    public function getDefaultValue(string $fieldName, string $type, ?int $userID = null):mixed
    {
        $name = $this->getFieldName($fieldName, $type);
        if (is_null($userID)) {
            return Options::get($name);
        } else {
            return Meta::forUser($userID)->get($name);
        }
    }
    public function setDefaultMetaValue(string $fieldName, mixed $value, string $type, ?int $userID = null):void
    {
        $name = $this->getFieldName($fieldName, $type);
        if (is_null($userID)) {
            Options::set($name, $value);
        } else {
            Meta::forUser($userID)->set($name, $value);
        }
    }
}