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