Completed
Pull Request — master (#195)
by Dominik
03:10 queued 25s
created

FieldLoader::addFilters()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 5
nop 3
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\Acf;
4
5
use RecursiveDirectoryIterator;
6
use Flynt\Utils\ArrayHelpers;
7
use Flynt\Utils\StringHelpers;
8
use Flynt\Utils\FileLoader;
9
use Flynt\ComponentManager;
10
11
class FieldLoader
12
{
13
    const FILTER_NAMESPACES = [
14
        'component' => 'Flynt/Components',
15
        'customPostType' => 'Flynt/CustomPostTypes',
16
        'feature' => 'Flynt/Features'
17
    ];
18
19
    public static function setup()
20
    {
21
        add_action(
22
            'Flynt/registerComponent',
23
            ['Flynt\Features\Acf\FieldLoader', 'loadComponentFields'],
24
            11
25
        );
26
27
        add_action(
28
            'Flynt/Features/CustomPostTypes/Register',
29
            ['Flynt\Features\Acf\FieldLoader', 'loadCustomPostTypeFields'],
30
            10,
31
            2
32
        );
33
34
        add_action(
35
            'Flynt/registerFeature',
36
            ['Flynt\Features\Acf\FieldLoader', 'loadFeatureFields'],
37
            10,
38
            3
39
        );
40
41
    }
42
43
    public static function loadComponentFields($name)
44
    {
45
        // load fields.json if it exists
46
        $componentManager = ComponentManager::getInstance();
47
        $filePath = $componentManager->getComponentFilePath($name, 'fields.json');
48
49
        // make sure naming convention is kept
50
        $name = ucfirst($name);
51
52
        // add filters
53
        self::addFilters('component', $name, $filePath);
54
    }
55
56
    public static function loadCustomPostTypeFields($name, $customPostType)
57
    {
58
        $filePath = $customPostType['dir'] . '/fields.json';
59
60
        $name = StringHelpers::kebapCaseToCamelCase($name);
61
62
        self::addFilters('customPostType', $name, $filePath);
63
    }
64
65
    public static function loadFeatureFields($name, $options, $dir)
66
    {
67
        $filePath = $dir . '/fields.json';
68
69
        $name = StringHelpers::removePrefix('flynt', StringHelpers::kebapCaseToCamelCase($name));
70
71
        self::addFilters('feature', $name, $filePath);
72
    }
73
74
    public static function addFilters($type, $name, $filePath)
75
    {
76
        if (false === $filePath || !file_exists($filePath)) {
77
            return;
78
        }
79
80
        $fields = json_decode(file_get_contents($filePath), true);
81
82
        foreach ($fields as $groupKey => $groupValue) {
83
            $groupKey = ucfirst($groupKey);
84
            $filterNamespace = self::FILTER_NAMESPACES[$type];
85
            $filterName = "{$filterNamespace}/{$name}/Fields/{$groupKey}";
86
87
            add_filter($filterName, function ($config) use ($groupValue) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
                return $groupValue;
89
            });
90
            if (ArrayHelpers::isAssoc($groupValue) && array_key_exists('sub_fields', $groupValue)) {
91
                $filterName .= '/SubFields';
92
                $subFields = $groupValue['sub_fields'];
93
94
                add_filter($filterName, function ($subFieldsconfig) use ($subFields) {
0 ignored issues
show
Unused Code introduced by
The parameter $subFieldsconfig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
                    return $subFields;
96
                });
97
                self::addFilterForSubFields($filterName, $subFields);
98
            } elseif (is_array($groupValue)) {
99
                self::addFilterForSubFields($filterName, $groupValue);
100
            }
101
        }
102
    }
103
104
    protected static function addFilterForSubFields($parentFilterName, $subFields)
105
    {
106
        foreach ($subFields as $subField) {
107
            if (is_string($subField)) {
108
                continue;
109
            }
110
            if (!array_key_exists('name', $subField)) {
111
                trigger_error('[ACF] Name is missing in Sub Field while adding Filter: ' . $parentFilterName, E_USER_WARNING);
112
                continue;
113
            }
114
            $subFieldName = ucfirst($subField['name']);
115
            $subFilterName = $parentFilterName . "/{$subFieldName}";
116
117
            add_filter($subFilterName, function ($subFieldConfig) use ($subField) {
0 ignored issues
show
Unused Code introduced by
The parameter $subFieldConfig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
                return $subField;
119
            });
120
        }
121
    }
122
}
123