-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathResolveConfig.php
More file actions
272 lines (251 loc) · 10.2 KB
/
Copy pathResolveConfig.php
File metadata and controls
272 lines (251 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace ACFComposer;
use Exception;
class ResolveConfig
{
/**
* Validates and resolves a configuration for a local field group.
*
* @param array $config Configuration array for the local field group.
*
* @return array Resolved field group configuration.
*/
public static function forFieldGroup($config)
{
$output = static::validateConfig($config, ['name', 'title', 'fields', 'location']);
$keySuffix = $output['name'];
$output['key'] = "group_{$keySuffix}";
$output['fields'] = array_reduce($config['fields'], function ($carry, $fieldConfig) use ($keySuffix) {
$fields = static::forField($fieldConfig, [$keySuffix]);
static::pushSingleOrMultiple($carry, $fields);
return $carry;
}, []);
$output['location'] = array_map([static::class, 'mapLocation'], $output['location']);
return $output;
}
/**
* Validates a location the configuration for a field group location.
*
* @param array $config Configuration array for a location of a field group.
*
* @return array Valid config.
*/
public static function forLocation($config)
{
return static::validateConfig($config, ['param', 'operator', 'value']);
}
/**
* Validates and resolves a field configuration.
*
* @param array|string $config Configuration array for a any kind of field.
* @param array $parentKeys Previously used keys of all parent fields.
*
* @return array Resolved config for a field.
*/
public static function forField($config, $parentKeys = [])
{
return static::forEntity($config, ['name', 'label', 'type'], $parentKeys);
}
/**
* Validates and resolves a layout configuration of a flexible content field.
*
* @param array|string $config Configuration array for the local field group.
* @param array $parentKeys Previously used keys of all parent fields.
*
* @return array Resolved config for a layout of a flexible content field.
*/
public static function forLayout($config, $parentKeys = [])
{
return static::forEntity($config, ['name', 'label'], $parentKeys);
}
/**
* Validates and resolves configuration for a field, subfield, or layout. Applies prefix through filter arguments.
*
* @param array|string $config Configuration array for the nested entity.
* @param array $requiredAttributes Required attributes.
* @param array $parentKeys Previously used keys of all parent fields.
* @param string $prefix Optional prefix for named field based on filter arguments.
*
* @return array Resolved config.
*/
protected static function forEntity($config, $requiredAttributes, $parentKeys = [], $prefix = null)
{
if (is_string($config)) {
$filterName = $config;
$filterParts = explode('#', $filterName);
if (isset($filterParts[1])) {
$prefix = $filterParts[1];
$config = apply_filters($filterParts[0], null, $prefix);
if (!static::isAssoc($config)) {
$config = array_map(function ($singleConfig) use ($prefix) {
$singleConfig['name'] = $prefix . '_' . $singleConfig['name'];
return $singleConfig;
}, $config);
} else {
$config['name'] = $prefix . '_' . $config['name'];
}
} else {
$config = apply_filters($filterName, null);
}
if (is_null($config)) {
trigger_error("ACFComposer: Filter {$filterName} does not exist!", E_USER_WARNING);
return [];
}
}
if (!static::isAssoc($config)) {
return array_map(function ($singleConfig) use ($requiredAttributes, $parentKeys, $prefix) {
return static::forEntity($singleConfig, $requiredAttributes, $parentKeys, $prefix);
}, $config);
}
$output = static::validateConfig($config, $requiredAttributes);
$parentKeysIncludingPrefix = isset($prefix) ? array_merge($parentKeys, [$prefix]) : $parentKeys;
$output = static::forConditionalLogic($output, $parentKeysIncludingPrefix);
array_push($parentKeys, $output['name']);
$keySuffix = implode('_', $parentKeys);
$output['key'] = "field_{$keySuffix}";
$output = apply_filters('ACFComposer/resolveEntity', $output);
$output = apply_filters("ACFComposer/resolveEntity?name={$output['name']}", $output);
$output = apply_filters("ACFComposer/resolveEntity?key={$output['key']}", $output);
$output = static::forNestedEntities($output, $parentKeys);
return $output;
}
/**
* Validates and resolves configuration for subfields and layouts.
*
* @param array $config Configuration array for the nested entity.
* @param array $parentKeys Previously used keys of all parent fields.
*
* @return array Resolved config.
*/
protected static function forNestedEntities($config, $parentKeys)
{
if (array_key_exists('sub_fields', $config)) {
$config['sub_fields'] = array_reduce($config['sub_fields'], function ($output, $subField) use ($parentKeys) {
$fields = static::forField($subField, $parentKeys);
if (!static::isAssoc($fields)) {
foreach ($fields as $field) {
array_push($output, $field);
}
} else {
array_push($output, $fields);
}
return $output;
}, []);
}
if (array_key_exists('layouts', $config)) {
$config['layouts'] = array_reduce($config['layouts'], function ($output, $layout) use ($parentKeys) {
$subLayouts = static::forLayout($layout, $parentKeys);
if (!static::isAssoc($subLayouts)) {
foreach ($subLayouts as $subLayout) {
array_push($output, $subLayout);
}
} else {
array_push($output, $subLayouts);
}
return $output;
}, []);
}
return $config;
}
/**
* Validates a configuration array based on given required attributes.
*
* Usually the field key has to be provided for conditional logic to work. Since all keys are generated automatically by this plugin, you can instead provide a 'relative path' to a field by it's name.
*
* @param array $config Configuration array.
* @param array $requiredAttributes Required Attributes.
*
* @throws Exception if a required attribute is not present.
* @throws Exception if the `key` attribute is not present.
*
* @return array Given $config.
*/
protected static function validateConfig($config, $requiredAttributes = [])
{
array_walk($requiredAttributes, function ($key) use ($config) {
if (!array_key_exists($key, $config)) {
throw new Exception("Field config needs to contain a \'{$key}\' property.");
}
});
if (array_key_exists('key', $config)) {
throw new Exception('Field config must not contain a \'key\' property.');
}
return $config;
}
/**
* Maps location configurations to their resolved config arrays.
*
* @param array $locationArray All locations for a field group.
*
* @return array Resolved locations array.
*/
protected static function mapLocation($locationArray)
{
return array_map([static::class, 'forLocation'], $locationArray);
}
/**
* Resolves a field's conditional logic attribute.
*
* Usually the field key has to be provided for conditional logic to work. Since all keys are generated automatically by this plugin, you can instead provide a 'relative path' to a field by it's name.
*
* @param array $config Configuration array for the conditional logic attribute.
* @param array $parentKeys Previously used keys of all parent fields.
*
* @return array Resolved conditional logic attribute.
*/
protected static function forConditionalLogic($config, $parentKeys)
{
if (array_key_exists('conditional_logic', $config)) {
$config['conditional_logic'] = array_map(function ($conditionGroup) use ($parentKeys) {
return array_map(function ($condition) use ($parentKeys) {
if (array_key_exists('fieldPath', $condition)) {
$conditionalField = $condition['fieldPath'];
while (substr($conditionalField, 0, 3) === '../') {
$conditionalField = substr($conditionalField, 3);
array_pop($parentKeys);
}
array_push($parentKeys, $conditionalField);
$keySuffix = implode('_', $parentKeys);
$condition['field'] = "field_{$keySuffix}";
unset($condition['fieldPath']);
}
return $condition;
}, $conditionGroup);
}, $config['conditional_logic']);
}
return $config;
}
/**
* Checks whether or not a given array is associative.
*
* @param array $arr Array to check.
*
* @return boolean
*/
protected static function isAssoc(array $arr)
{
if (array() === $arr) {
return false;
}
return array_keys($arr) !== range(0, count($arr) - 1);
}
/**
* Adds a single or multiple elements to an array.
*
* @param array &$arr Array to add to.
* @param array $fields Single or multiple associative arrays to add to $arr.
*
* @return boolean
*/
protected static function pushSingleOrMultiple(array &$carry, array $fields)
{
if (!static::isAssoc($fields)) {
foreach ($fields as $field) {
static::pushSingleOrMultiple($carry, $field);
}
} else {
array_push($carry, $fields);
}
return $carry;
}
}