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