1 | <?php |
||
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() |
||
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) |
||
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.