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