1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
9
|
|
|
|
10
|
|
|
class Field |
11
|
|
|
{ |
12
|
|
|
const MULTI_FIELD_TYPES = ['radio', 'checkbox']; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public $field; |
18
|
|
|
|
19
|
|
|
public function __construct( array $field = [] ) |
20
|
|
|
{ |
21
|
|
|
$this->field = wp_parse_args( $field, [ |
22
|
|
|
'is_hidden' => false, |
23
|
|
|
'is_multi' => false, |
24
|
|
|
'is_valid' => true, |
25
|
|
|
'path' => '', |
26
|
|
|
]); |
27
|
|
|
$this->normalize(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function build() |
34
|
|
|
{ |
35
|
|
|
if( !$this->field['is_valid'] )return; |
36
|
|
|
if( $this->field['is_multi'] ) { |
37
|
|
|
return $this->buildMultiField(); |
38
|
|
|
} |
39
|
|
|
$this->field['data-depends'] = $this->getFieldDependsOn(); |
40
|
|
|
return glsr( Template::class )->build( 'partials/settings/form-table-row', [ |
41
|
|
|
'context' => [ |
42
|
|
|
'class' => $this->getFieldClass(), |
43
|
|
|
'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ), |
44
|
|
|
'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ), |
45
|
|
|
], |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function render() |
53
|
|
|
{ |
54
|
|
|
echo $this->build(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function buildMultiField() |
61
|
|
|
{ |
62
|
|
|
return glsr( Template::class )->build( 'partials/settings/form-table-row-multiple', [ |
63
|
|
|
'context' => [ |
64
|
|
|
'class' => $this->getFieldClass(), |
65
|
|
|
'depends_on' => $this->getFieldDependsOn(), |
66
|
|
|
'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ), |
67
|
|
|
'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ), |
68
|
|
|
'legend' => $this->field['legend'], |
69
|
|
|
], |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
protected function getFieldClass() |
77
|
|
|
{ |
78
|
|
|
return $this->field['is_hidden'] |
79
|
|
|
? 'hidden' |
80
|
|
|
: ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getFieldDefault() |
87
|
|
|
{ |
88
|
|
|
return isset( $this->field['default'] ) |
89
|
|
|
? $this->field['default'] |
90
|
|
|
: ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function getFieldDependsOn() |
97
|
|
|
{ |
98
|
|
|
return !empty( $this->field['depends_on'] ) |
99
|
|
|
? $this->field['depends_on'] |
100
|
|
|
: ''; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $path |
105
|
|
|
* @param string $expectedValue |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function isFieldHidden( $path, $expectedValue ) |
109
|
|
|
{ |
110
|
|
|
$optionValue = glsr( OptionManager::class )->get( |
111
|
|
|
$path, |
112
|
|
|
glsr( Helper::class )->getPathValue( $path, glsr()->defaults ) |
113
|
|
|
); |
114
|
|
|
if( is_array( $expectedValue )) { |
|
|
|
|
115
|
|
|
return !in_array( $optionValue, $expectedValue ); |
116
|
|
|
} |
117
|
|
|
return $optionValue != $expectedValue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
protected function isFieldValid() |
124
|
|
|
{ |
125
|
|
|
$isValid = true; |
126
|
|
|
$missingValues = []; |
127
|
|
|
$requiredValues = [ |
128
|
|
|
'label', 'name', 'type', |
129
|
|
|
]; |
130
|
|
|
foreach( $requiredValues as $value ) { |
131
|
|
|
if( isset( $this->field[$value] ))continue; |
132
|
|
|
$missingValues[] = $value; |
133
|
|
|
$isValid = $this->field['is_valid'] = false; |
134
|
|
|
} |
135
|
|
|
if( !empty( $missingValues )) { |
136
|
|
|
glsr_log() |
137
|
|
|
->warning( 'Field is missing: '.implode( ', ', $missingValues )) |
138
|
|
|
->info( $this->field ); |
139
|
|
|
} |
140
|
|
|
return $isValid; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
protected function normalize() |
147
|
|
|
{ |
148
|
|
|
if( !$this->isFieldValid() )return; |
149
|
|
|
$field = $this->field; |
150
|
|
|
foreach( $field as $key => $value ) { |
151
|
|
|
$methodName = glsr( Helper::class )->buildMethodName( $key, 'normalize' ); |
152
|
|
|
if( !method_exists( $this, $methodName ))continue; |
153
|
|
|
$this->$methodName(); |
154
|
|
|
} |
155
|
|
|
$this->normalizeFieldId(); |
156
|
|
|
$this->normalizeFieldType(); |
157
|
|
|
$this->normalizeFieldValue(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
protected function normalizeDependsOn() |
164
|
|
|
{ |
165
|
|
|
if( empty( $this->field['depends_on'] ) || !is_array( $this->field['depends_on'] ))return; |
166
|
|
|
$path = key( $this->field['depends_on'] ); |
167
|
|
|
$value = $this->field['depends_on'][$path]; |
168
|
|
|
$this->field['depends_on'] = json_encode([ |
169
|
|
|
'name' => glsr( Helper::class )->convertPathToName( $path, OptionManager::databaseKey() ), |
170
|
|
|
'value' => $value, |
171
|
|
|
], JSON_HEX_APOS|JSON_HEX_QUOT ); |
172
|
|
|
$this->field['is_hidden'] = $this->isFieldHidden( $path, $value ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function normalizeFieldId() |
179
|
|
|
{ |
180
|
|
|
if( isset( $this->field['id'] ))return; |
181
|
|
|
$this->field['id'] = glsr( Helper::class )->convertNameToId( $this->field['name'] ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
protected function normalizeFieldType() |
188
|
|
|
{ |
189
|
|
|
$className = glsr( Helper::class )->buildClassName( $this->field['type'], 'Modules\Html\Fields' ); |
190
|
|
|
if( class_exists( $className )) { |
191
|
|
|
$this->field = array_merge( $this->field, glsr( $className )->defaults() ); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
if( in_array( $this->field['type'], static::MULTI_FIELD_TYPES )) { |
194
|
|
|
$this->field['is_multi'] = true; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
protected function normalizeFieldValue() |
202
|
|
|
{ |
203
|
|
|
if( isset( $this->field['value'] ))return; |
204
|
|
|
$this->field['value'] = glsr( OptionManager::class )->get( |
205
|
|
|
$this->field['path'], |
206
|
|
|
$this->getFieldDefault() |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
protected function normalizeLabel() |
214
|
|
|
{ |
215
|
|
|
$this->field['legend'] = $this->field['label']; |
216
|
|
|
unset( $this->field['label'] ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return void |
221
|
|
|
*/ |
222
|
|
|
protected function normalizeName() |
223
|
|
|
{ |
224
|
|
|
$this->field['path'] = $this->field['name']; |
225
|
|
|
$this->field['name'] = glsr( Helper::class )->convertPathToName( |
226
|
|
|
$this->field['name'], |
227
|
|
|
OptionManager::databaseKey() |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|