1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Builds configurations for an ACF Field |
7
|
|
|
* @method FieldBuilder addField(string $name, string $type, array $args = []) |
8
|
|
|
* @method FieldBuilder addChoiceField(string $name, string $type, array $args = []) |
9
|
|
|
* @method FieldBuilder addText(string $name, array $args = []) |
10
|
|
|
* @method FieldBuilder addTextarea(string $name, array $args = []) |
11
|
|
|
* @method FieldBuilder addNumber(string $name, array $args = []) |
12
|
|
|
* @method FieldBuilder addEmail(string $name, array $args = []) |
13
|
|
|
* @method FieldBuilder addUrl(string $name, array $args = []) |
14
|
|
|
* @method FieldBuilder addPassword(string $name, array $args = []) |
15
|
|
|
* @method FieldBuilder addWysiwyg(string $name, array $args = []) |
16
|
|
|
* @method FieldBuilder addOembed(string $name, array $args = []) |
17
|
|
|
* @method FieldBuilder addImage(string $name, array $args = []) |
18
|
|
|
* @method FieldBuilder addFile(string $name, array $args = []) |
19
|
|
|
* @method FieldBuilder addGallery(string $name, array $args = []) |
20
|
|
|
* @method FieldBuilder addTrueFalse(string $name, array $args = []) |
21
|
|
|
* @method FieldBuilder addSelect(string $name, array $args = []) |
22
|
|
|
* @method FieldBuilder addRadio(string $name, array $args = []) |
23
|
|
|
* @method FieldBuilder addCheckbox(string $name, array $args = []) |
24
|
|
|
* @method FieldBuilder addPostObject(string $name, array $args = []) |
25
|
|
|
* @method FieldBuilder addPageLink(string $name, array $args = []) |
26
|
|
|
* @method FieldBuilder addTaxonomy(string $name, array $args = []) |
27
|
|
|
* @method FieldBuilder addUser(string $name, array $args = []) |
28
|
|
|
* @method FieldBuilder addDatePicker(string $name, array $args = []) |
29
|
|
|
* @method FieldBuilder addTimePicker(string $name, array $args = []) |
30
|
|
|
* @method FieldBuilder addDateTimePicker(string $name, array $args = []) |
31
|
|
|
* @method FieldBuilder addColorPicker(string $name, array $args = []) |
32
|
|
|
* @method FieldBuilder addGoogleMap(string $name, array $args = []) |
33
|
|
|
* @method FieldBuilder addLink(string $name, array $args = []) |
34
|
|
|
* @method FieldBuilder addTab(string $label, array $args = []) |
35
|
|
|
* @method FieldBuilder addRange(string $name, array $args = []) |
36
|
|
|
* @method FieldBuilder addMessage(string $label, string $message, array $args = []) |
37
|
|
|
* @method GroupBuilder addGroup(string $name, array $args = []) |
38
|
|
|
* @method GroupBuilder endGroup() |
39
|
|
|
* @method RepeaterBuilder addRepeater(string $name, array $args = []) |
40
|
|
|
* @method Builder endRepeater() |
41
|
|
|
* @method FlexibleContentBuilder addFlexibleContent(string $name, array $args = []) |
42
|
|
|
* @method FieldsBuilder addLayout(string|FieldsBuilder $layout, array $args = []) |
43
|
|
|
* @method LocationBuilder setLocation(string $param, string $operator, string $value)* |
44
|
|
|
*/ |
45
|
|
|
class FieldBuilder extends ParentDelegationBuilder implements NamedBuilder |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* Field Type |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $type; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Additional Field Configuration |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $config; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name Field Name, conventionally 'snake_case'. |
61
|
|
|
* @param string $type Field Type. |
62
|
|
|
* @param array $config Additional Field Configuration. |
63
|
|
|
*/ |
64
|
|
|
public function __construct($name, $type, $config = []) |
65
|
|
|
{ |
66
|
|
|
$this->config = [ |
67
|
|
|
'name' => $name, |
68
|
|
|
'label' => $this->generateLabel($name), |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$this->type = $type; |
72
|
|
|
$this->setKey($name); |
73
|
|
|
$this->updateConfig($config); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function getConfig() |
80
|
|
|
{ |
81
|
|
|
return $this->config; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set a config key -> value pair |
86
|
|
|
* @param string $key |
87
|
|
|
* @param mixed $value |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setConfig($key, $value) |
91
|
|
|
{ |
92
|
|
|
return $this->updateConfig([$key => $value]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update multiple config values using and array of key -> value pairs. |
97
|
|
|
* @param array $config |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function updateConfig($config) |
101
|
|
|
{ |
102
|
|
|
$this->config = array_merge($this->config, $config); |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getName() |
110
|
|
|
{ |
111
|
|
|
return $this->config['name']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getKey() |
118
|
|
|
{ |
119
|
|
|
return $this->config['key']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getLabel() |
126
|
|
|
{ |
127
|
|
|
return $this->config['label']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Will prepend `field_` if missing. |
132
|
|
|
* @param string $key |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function setKey($key) |
136
|
|
|
{ |
137
|
|
|
if (!preg_match('/^field_/', $key)) { |
138
|
|
|
$key = 'field_'.$key; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->setConfig('key', $key); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Will set field required. |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function setRequired() |
149
|
|
|
{ |
150
|
|
|
return $this->setConfig('required', 1); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Will set field unrequired. |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setUnrequired() |
158
|
|
|
{ |
159
|
|
|
return $this->setConfig('required', 0); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Will set field's label. |
164
|
|
|
* @param string $label |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function setLabel($label) |
168
|
|
|
{ |
169
|
|
|
return $this->setConfig('label', $label); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Will set field's instructions. |
174
|
|
|
* @param string $instructions |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
|
public function setInstructions($instructions) |
178
|
|
|
{ |
179
|
|
|
return $this->setConfig('instructions', $instructions); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Will set field's defaultValue. |
184
|
|
|
* @param string $defaultValue |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setDefaultValue($defaultValue) |
188
|
|
|
{ |
189
|
|
|
return $this->setConfig('default_value', $defaultValue); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Add a conditional logic statement that will determine if the last added |
194
|
|
|
* field will display or not. You can add `or` or `and` calls after |
195
|
|
|
* to build complex logic. Any other function call will return you to the |
196
|
|
|
* parentContext. |
197
|
|
|
* @param string $name Dependent field name |
198
|
|
|
* (choice type: radio, checkbox, select, trueFalse) |
199
|
|
|
* @param string $operator ==, != |
200
|
|
|
* @param string $value 1 or choice value |
201
|
|
|
* @return ConditionalBuilder |
202
|
|
|
*/ |
203
|
|
|
public function conditional($name, $operator, $value) |
204
|
|
|
{ |
205
|
|
|
$conditionalBuilder = new ConditionalBuilder($name, $operator, $value); |
206
|
|
|
$conditionalBuilder->setParentContext($this); |
207
|
|
|
|
208
|
|
|
$this->setConfig('conditional_logic', $conditionalBuilder); |
209
|
|
|
|
210
|
|
|
return $conditionalBuilder; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Set Wrapper container tag attributes |
215
|
|
|
* |
216
|
|
|
* @param array $config |
217
|
|
|
* |
218
|
|
|
* @return FieldBuilder |
219
|
|
|
*/ |
220
|
|
|
public function setWrapper($config) |
221
|
|
|
{ |
222
|
|
|
return $this->setConfig('wrapper', $config); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get Wrapper container tag attributes |
227
|
|
|
* |
228
|
|
|
* @return array|mixed |
229
|
|
|
*/ |
230
|
|
|
public function getWrapper() |
231
|
|
|
{ |
232
|
|
|
if (isset($this->config['wrapper'])) { |
233
|
|
|
return $this->config['wrapper']; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return []; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Set width of a Wrapper container |
241
|
|
|
* |
242
|
|
|
* @param string $width Width of a container in % or px. |
243
|
|
|
* |
244
|
|
|
* @return FieldBuilder |
245
|
|
|
*/ |
246
|
|
|
public function setWidth($width) |
247
|
|
|
{ |
248
|
|
|
$wrapper = $this->getWrapper(); |
249
|
|
|
$wrapper['width'] = $width; |
250
|
|
|
|
251
|
|
|
return $this->setWrapper($wrapper); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set specified Attr of a Wrapper container |
256
|
|
|
* |
257
|
|
|
* @param string $name Attribute name, ex. 'class'. |
258
|
|
|
* @param string|null $value Attribute value, ex. 'my-class'. |
259
|
|
|
* |
260
|
|
|
* @return FieldBuilder |
261
|
|
|
*/ |
262
|
|
|
public function setAttr($name, $value = null) |
263
|
|
|
{ |
264
|
|
|
$wrapper = $this->getWrapper(); |
265
|
|
|
|
266
|
|
|
// set attribute. |
267
|
|
|
$wrapper[$name] = $value; |
268
|
|
|
|
269
|
|
|
return $this->setWrapper($wrapper); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Set Class and/or ID attribute of a Wrapper container |
274
|
|
|
* use CSS-like selector string to specify css or id |
275
|
|
|
* example: #my-id.foo-class.bar-class |
276
|
|
|
* |
277
|
|
|
* @param string $css_selector |
278
|
|
|
* |
279
|
|
|
* @return FieldBuilder |
280
|
|
|
*/ |
281
|
|
|
public function setSelector($css_selector) |
282
|
|
|
{ |
283
|
|
|
// if # is the first sign - we start with ID. |
284
|
|
|
if (0 === strpos($css_selector, '#')) { |
285
|
|
|
$css_selector .= '.'; // prevent empty second part. |
286
|
|
|
list($id, $class) = explode('.', $css_selector, 2); |
287
|
|
|
} else { |
288
|
|
|
$css_selector .= '#'; // prevent empty second part. |
289
|
|
|
list($class, $id) = explode('#', $css_selector, 2); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$id = trim($id, '#'); |
293
|
|
|
$class = trim($class, '.'); |
294
|
|
|
|
295
|
|
|
if (! empty($id)) { |
296
|
|
|
$this->setAttr('id', $id); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (! empty($class)) { |
300
|
|
|
$class = str_replace('.', ' ', $class); |
301
|
|
|
$this->setAttr('class', $class); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Build the field configuration array |
309
|
|
|
* @return array Field configuration array |
310
|
|
|
*/ |
311
|
|
|
public function build() |
312
|
|
|
{ |
313
|
|
|
$config = array_merge([ |
314
|
|
|
'type' => $this->type, |
315
|
|
|
], $this->getConfig()); |
316
|
|
|
|
317
|
|
|
foreach ($config as $key => $value) { |
318
|
|
|
if ($value instanceof Builder) { |
319
|
|
|
$config[$key] = $value->build(); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $config; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Create a field label based on the field's name. Generates title case. |
328
|
|
|
* @param string $name |
329
|
|
|
* @return string label |
330
|
|
|
*/ |
331
|
|
|
protected function generateLabel($name) |
332
|
|
|
{ |
333
|
|
|
return ucwords(str_replace("_", " ", $name)); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Generates a snaked cased name. |
338
|
|
|
* @param string $name |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
|
|
protected function generateName($name) |
342
|
|
|
{ |
343
|
|
|
return strtolower(str_replace(" ", "_", $name)); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|