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