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