1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\DefaultsManager; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Translation; |
9
|
|
|
|
10
|
|
|
class Settings |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public $settings; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $id |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function buildFields($id) |
22
|
|
|
{ |
23
|
|
|
$this->settings = glsr(DefaultsManager::class)->settings(); |
24
|
|
|
$method = glsr(Helper::class)->buildMethodName($id, 'getTemplateDataFor'); |
25
|
|
|
$data = !method_exists($this, $method) |
26
|
|
|
? $this->getTemplateData($id) |
27
|
|
|
: $this->$method($id); |
28
|
|
|
return glsr(Template::class)->build('pages/settings/'.$id, $data); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
protected function getFieldDefault(array $field) |
35
|
|
|
{ |
36
|
|
|
return glsr_get($field, 'default'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
protected function getFieldNameForDependsOn($path) |
43
|
|
|
{ |
44
|
|
|
$fieldName = glsr(Helper::class)->convertPathToName($path, OptionManager::databaseKey()); |
45
|
|
|
return $this->isMultiDependency($path) |
46
|
|
|
? $fieldName.'[]' |
47
|
|
|
: $fieldName; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
protected function getSettingFields($path) |
54
|
|
|
{ |
55
|
|
|
return array_filter($this->settings, function ($key) use ($path) { |
56
|
|
|
return glsr(Helper::class)->startsWith($path, $key); |
57
|
|
|
}, ARRAY_FILTER_USE_KEY); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
protected function getSettingRows(array $fields) |
64
|
|
|
{ |
65
|
|
|
$rows = ''; |
66
|
|
|
foreach ($fields as $name => $field) { |
67
|
|
|
$field = wp_parse_args($field, [ |
68
|
|
|
'is_setting' => true, |
69
|
|
|
'name' => $name, |
70
|
|
|
]); |
71
|
|
|
$rows.= new Field($this->normalize($field)); |
72
|
|
|
} |
73
|
|
|
return $rows; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $id |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getTemplateData($id) |
81
|
|
|
{ |
82
|
|
|
$fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
83
|
|
|
return [ |
84
|
|
|
'context' => [ |
85
|
|
|
'rows' => $this->getSettingRows($fields), |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $id |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
protected function getTemplateDataForAddons($id) |
95
|
|
|
{ |
96
|
|
|
$fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
97
|
|
|
$settings = glsr(Helper::class)->convertDotNotationArray($fields); |
98
|
|
|
$settingKeys = array_keys($settings['settings']['addons']); |
99
|
|
|
$results = []; |
100
|
|
|
foreach ($settingKeys as $key) { |
101
|
|
|
$addonFields = array_filter($fields, function ($path) use ($key) { |
102
|
|
|
return glsr(Helper::class)->startsWith('settings.addons.'.$key, $path); |
103
|
|
|
}, ARRAY_FILTER_USE_KEY); |
104
|
|
|
$results[$key] = $this->getSettingRows($addonFields); |
105
|
|
|
} |
106
|
|
|
ksort($results); |
107
|
|
|
return [ |
108
|
|
|
'settings' => $results, |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $id |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function getTemplateDataForLicenses($id) |
117
|
|
|
{ |
118
|
|
|
$fields = $this->getSettingFields($this->normalizeSettingPath($id)); |
119
|
|
|
ksort($fields); |
120
|
|
|
return [ |
121
|
|
|
'context' => [ |
122
|
|
|
'rows' => $this->getSettingRows($fields), |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
protected function getTemplateDataForTranslations() |
131
|
|
|
{ |
132
|
|
|
$translations = glsr(Translation::class)->renderAll(); |
133
|
|
|
$class = empty($translations) |
134
|
|
|
? 'glsr-hidden' |
135
|
|
|
: ''; |
136
|
|
|
return [ |
137
|
|
|
'context' => [ |
138
|
|
|
'class' => $class, |
139
|
|
|
'database_key' => OptionManager::databaseKey(), |
140
|
|
|
'translations' => $translations, |
141
|
|
|
], |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $path |
147
|
|
|
* @param string|array $expectedValue |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
protected function isFieldHidden($path, $expectedValue) |
151
|
|
|
{ |
152
|
|
|
$optionValue = glsr(OptionManager::class)->get( |
153
|
|
|
$path, |
154
|
|
|
glsr(Helper::class)->dataGet(glsr()->defaults, $path) |
155
|
|
|
); |
156
|
|
|
if (is_array($expectedValue)) { |
157
|
|
|
return is_array($optionValue) |
158
|
|
|
? 0 === count(array_intersect($optionValue, $expectedValue)) |
159
|
|
|
: !in_array($optionValue, $expectedValue); |
160
|
|
|
} |
161
|
|
|
return $optionValue != $expectedValue; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
protected function isMultiDependency($path) |
168
|
|
|
{ |
169
|
|
|
if (isset($this->settings[$path])) { |
170
|
|
|
$field = $this->settings[$path]; |
171
|
|
|
return ('checkbox' == $field['type'] && !empty($field['options'])) |
172
|
|
|
|| !empty($field['multiple']); |
173
|
|
|
} |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
protected function normalize(array $field) |
181
|
|
|
{ |
182
|
|
|
$field = $this->normalizeDependsOn($field); |
183
|
|
|
$field = $this->normalizeLabelAndLegend($field); |
184
|
|
|
$field = $this->normalizeValue($field); |
185
|
|
|
return $field; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function normalizeDependsOn(array $field) |
192
|
|
|
{ |
193
|
|
|
if (!empty($field['depends_on']) && is_array($field['depends_on'])) { |
194
|
|
|
$isFieldHidden = false; |
195
|
|
|
$conditions = []; |
196
|
|
|
foreach ($field['depends_on'] as $path => $value) { |
197
|
|
|
$conditions[] = [ |
198
|
|
|
'name' => $this->getFieldNameForDependsOn($path), |
199
|
|
|
'value' => $value, |
200
|
|
|
]; |
201
|
|
|
if ($this->isFieldHidden($path, $value)) { |
202
|
|
|
$isFieldHidden = true; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
$field['data-depends'] = json_encode($conditions, JSON_HEX_APOS | JSON_HEX_QUOT); |
206
|
|
|
$field['is_hidden'] = $isFieldHidden; |
207
|
|
|
} |
208
|
|
|
return $field; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
|
|
protected function normalizeLabelAndLegend(array $field) |
215
|
|
|
{ |
216
|
|
|
if (!empty($field['label'])) { |
217
|
|
|
$field['legend'] = $field['label']; |
218
|
|
|
unset($field['label']); |
219
|
|
|
} else { |
220
|
|
|
$field['is_valid'] = false; |
221
|
|
|
glsr_log()->warning('Setting field is missing a label')->debug($field); |
222
|
|
|
} |
223
|
|
|
return $field; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
protected function normalizeValue(array $field) |
230
|
|
|
{ |
231
|
|
|
if (!isset($field['value'])) { |
232
|
|
|
$field['value'] = glsr(OptionManager::class)->get( |
233
|
|
|
$field['name'], |
234
|
|
|
$this->getFieldDefault($field) |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
return $field; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
protected function normalizeSettingPath($path) |
244
|
|
|
{ |
245
|
|
|
return glsr(Helper::class)->prefix('settings.', rtrim($path, '.')); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|