1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Features\Acf; |
4
|
|
|
|
5
|
|
|
use Flynt\Utils\ArrayHelpers; |
6
|
|
|
use Flynt\Utils\StringHelpers; |
7
|
|
|
use Flynt\ComponentManager; |
8
|
|
|
|
9
|
|
|
class FieldLoader |
10
|
|
|
{ |
11
|
|
|
const FILTER_NAMESPACES = [ |
12
|
|
|
'component' => 'Flynt/Components', |
13
|
|
|
'customPostType' => 'Flynt/CustomPostTypes', |
14
|
|
|
'feature' => 'Flynt/Features' |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
public static function setup() |
18
|
|
|
{ |
19
|
|
|
add_action( |
20
|
|
|
'Flynt/registerComponent', |
21
|
|
|
['Flynt\Features\Acf\FieldLoader', 'loadComponentFields'], |
22
|
|
|
11 |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
add_action( |
26
|
|
|
'Flynt/Features/CustomPostTypes/Register', |
27
|
|
|
['Flynt\Features\Acf\FieldLoader', 'loadCustomPostTypeFields'], |
28
|
|
|
10, |
29
|
|
|
2 |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
add_action( |
33
|
|
|
'Flynt/registerFeature', |
34
|
|
|
['Flynt\Features\Acf\FieldLoader', 'loadFeatureFields'], |
35
|
|
|
10, |
36
|
|
|
3 |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function loadComponentFields($name) |
41
|
|
|
{ |
42
|
|
|
// load fields.json if it exists |
43
|
|
|
$componentManager = ComponentManager::getInstance(); |
44
|
|
|
$filePath = $componentManager->getComponentFilePath($name, 'fields.json'); |
45
|
|
|
|
46
|
|
|
// add filters |
47
|
|
|
self::addFilters('component', $name, $filePath); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function loadCustomPostTypeFields($name, $customPostType) |
51
|
|
|
{ |
52
|
|
|
$filePath = $customPostType['dir'] . '/fields.json'; |
53
|
|
|
|
54
|
|
|
$name = StringHelpers::kebapCaseToCamelCase($name); |
55
|
|
|
|
56
|
|
|
self::addFilters('customPostType', $name, $filePath); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function loadFeatureFields($name, $options, $dir) |
60
|
|
|
{ |
61
|
|
|
$filePath = $dir . '/fields.json'; |
62
|
|
|
|
63
|
|
|
self::addFilters('feature', $name, $filePath); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function addFilters($category, $name, $filePath) |
67
|
|
|
{ |
68
|
|
|
if (false === $filePath || !file_exists($filePath)) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// make sure naming convention is kept |
73
|
|
|
$name = ucfirst($name); |
74
|
|
|
|
75
|
|
|
$fields = json_decode(file_get_contents($filePath), true); |
76
|
|
|
|
77
|
|
|
foreach ($fields as $groupKey => $groupValue) { |
78
|
|
|
$groupKey = ucfirst($groupKey); |
79
|
|
|
$filterNamespace = self::FILTER_NAMESPACES[$category]; |
80
|
|
|
$filterName = "{$filterNamespace}/{$name}/Fields/{$groupKey}"; |
81
|
|
|
|
82
|
|
|
add_filter($filterName, function ($config) use ($groupValue) { |
|
|
|
|
83
|
|
|
return $groupValue; |
84
|
|
|
}); |
85
|
|
|
if (ArrayHelpers::isAssoc($groupValue) && array_key_exists('sub_fields', $groupValue)) { |
86
|
|
|
$filterName .= '/SubFields'; |
87
|
|
|
$subFields = $groupValue['sub_fields']; |
88
|
|
|
|
89
|
|
|
add_filter($filterName, function ($subFieldsconfig) use ($subFields) { |
|
|
|
|
90
|
|
|
return $subFields; |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
self::addFilterForSubFields($filterName, $subFields); |
94
|
|
|
} elseif (is_array($groupValue)) { |
95
|
|
|
self::addFilterForSubFields($filterName, $groupValue); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected static function addFilterForSubFields($parentFilterName, $subFields) |
101
|
|
|
{ |
102
|
|
|
foreach ($subFields as $subField) { |
103
|
|
|
if (is_string($subField)) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
if (!array_key_exists('name', $subField)) { |
107
|
|
|
trigger_error('[ACF] Name is missing in Sub Field while adding Filter: ' . $parentFilterName, E_USER_WARNING); |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
$subFieldName = ucfirst($subField['name']); |
111
|
|
|
$subFilterName = "{$parentFilterName}/{$subFieldName}"; |
112
|
|
|
|
113
|
|
|
add_filter($subFilterName, function ($subFieldConfig) use ($subField) { |
|
|
|
|
114
|
|
|
return $subField; |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.