Completed
Push — fieldLoaderTest ( 57c9f6 )
by Wahiba
03:02
created

FieldLoader::loadCustomPostTypeFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
rs 9.4285
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
    public static function loadComponentFields($name)
43
    {
44
        // load fields.json if it exists
45
        $componentManager = ComponentManager::getInstance();
46
        $filePath = $componentManager->getComponentFilePath($name, 'fields.json');
47
48
        // make sure naming convention is kept
49
        $name = ucfirst($name);
50
51
        // add filters
52
        self::addFilters('component', $name, $filePath);
53
    }
54
55
    public static function loadCustomPostTypeFields($name, $customPostType)
56
    {
57
        $filePath = $customPostType['dir'] . '/fields.json';
58
59
        $name = StringHelpers::kebapCaseToCamelCase($name);
60
61
        self::addFilters('customPostType', $name, $filePath);
62
    }
63
64
    public static function loadFeatureFields($name, $options, $dir)
65
    {
66
        $filePath = $dir . '/fields.json';
67
68
        $name = StringHelpers::removePrefix('flynt', StringHelpers::kebapCaseToCamelCase($name));
69
70
        self::addFilters('feature', $name, $filePath);
71
    }
72
73
    public static function addFilters($type, $name, $filePath)
74
    {
75
        if (false === $filePath || !file_exists($filePath)) {
76
            return;
77
        }
78
79
        $fields = json_decode(file_get_contents($filePath), true);
80
81
        foreach ($fields as $groupKey => $groupValue) {
82
            $groupKey = ucfirst($groupKey);
83
            $filterNamespace = self::FILTER_NAMESPACES[$type];
84
            $filterName = "{$filterNamespace}/{$name}/Fields/{$groupKey}";
85
86
            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...
87
                return $groupValue;
88
            });
89
            if (ArrayHelpers::isAssoc($groupValue) && array_key_exists('sub_fields', $groupValue)) {
90
                $filterName .= '/SubFields';
91
                $subFields = $groupValue['sub_fields'];
92
93
                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...
94
                    return $subFields;
95
                });
96
                self::addFilterForSubFields($filterName, $subFields);
97
            } elseif (is_array($groupValue)) {
98
                self::addFilterForSubFields($filterName, $groupValue);
99
            }
100
        }
101
    }
102
103
    protected static function addFilterForSubFields($parentFilterName, $subFields)
104
    {
105
        foreach ($subFields as $subField) {
106
            if (is_string($subField)) {
107
                continue;
108
            }
109
            if (!array_key_exists('name', $subField)) {
110
                trigger_error('[ACF] Name is missing in Sub Field while adding Filter: ' . $parentFilterName, E_USER_WARNING);
111
                continue;
112
            }
113
            $subFieldName = ucfirst($subField['name']);
114
            $subFilterName = $parentFilterName . "/{$subFieldName}";
115
116
            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...
117
                return $subField;
118
            });
119
        }
120
    }
121
}
122