Completed
Push — master ( 237036...650d4b )
by Dominik
02:47
created

FieldGroupComposer::addFieldFilters()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 5
nop 1
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\Acf;
4
5
use RecursiveDirectoryIterator;
6
use ACFComposer\ACFComposer;
7
use Flynt\Utils\ArrayHelpers;
8
use Flynt\Utils\FileLoader;
9
use Flynt\ComponentManager;
10
11
class FieldGroupComposer
12
{
13
    const FILTER_NAMESPACE = 'Flynt/Components';
14
    const FIELD_GROUPS_DIR = '/config/fieldGroups';
15
16
    protected static $fieldGroupsLoaded = false;
17
18
    public static function setup()
19
    {
20
        add_action(
21
            'Flynt/registerComponent',
22
            ['Flynt\Features\Acf\FieldGroupComposer', 'addFieldFilters'],
23
            11
24
        );
25
26
        add_action(
27
            'acf/init',
28
            ['Flynt\Features\Acf\FieldGroupComposer', 'loadFieldGroups']
29
        );
30
    }
31
32
    public static function loadFieldGroups()
33
    {
34
        // prevent this running more than once
35
        if (self::$fieldGroupsLoaded) {
36
            return;
37
        }
38
39
        // Load field groups from files after ACF initializes
40
        $dir = get_template_directory() . self::FIELD_GROUPS_DIR;
41
42
        if (!is_dir($dir)) {
43
            trigger_error("[ACF] Cannot load field groups: {$dir} is not a valid directory!", E_USER_WARNING);
44
            return;
45
        }
46
47
        FileLoader::iterateDir($dir, function ($file) {
48
            if ($file->getExtension() === 'json') {
49
                $filePath = $file->getPathname();
50
                $config = json_decode(file_get_contents($filePath), true);
51
                ACFComposer::registerFieldGroup($config);
52
            }
53
        });
54
55
        self::$fieldGroupsLoaded = true;
56
    }
57
58
    public static function addFieldFilters($componentName)
59
    {
60
        // load fields.json if it exists
61
        $componentManager = ComponentManager::getInstance();
62
        $filePath = $componentManager->getComponentFilePath($componentName, 'fields.json');
63
64
        if (false === $filePath) {
65
            return;
66
        }
67
68
        $fields = json_decode(file_get_contents($filePath), true);
69
70
        // make sure naming convention is kept
71
        $componentName = ucfirst($componentName);
72
73
        // add filters
74
        foreach ($fields as $groupKey => $groupValue) {
75
            $groupKey = ucfirst($groupKey);
76
            $filterName = self::FILTER_NAMESPACE . "/{$componentName}/Fields/{$groupKey}";
77
78
            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...
79
                return $groupValue;
80
            });
81
            if (ArrayHelpers::isAssoc($groupValue) && array_key_exists('sub_fields', $groupValue)) {
82
                $filterName .= '/SubFields';
83
                $subFields = $groupValue['sub_fields'];
84
85
                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...
86
                    return $subFields;
87
                });
88
                self::addFilterForSubFields($filterName, $subFields);
89
            } elseif (is_array($groupValue)) {
90
                self::addFilterForSubFields($filterName, $groupValue);
91
            }
92
        }
93
    }
94
95
    protected static function addFilterForSubFields($parentFilterName, $subFields)
96
    {
97
        foreach ($subFields as $subField) {
98
            if (is_string($subField)) {
99
                continue;
100
            }
101
            if (!array_key_exists('name', $subField)) {
102
                trigger_error('[ACF] Name is missing in Sub Field while adding Filter: ' . $parentFilterName, E_USER_WARNING);
103
                continue;
104
            }
105
            $subFieldName = ucfirst($subField['name']);
106
            $subFilterName = $parentFilterName . "/{$subFieldName}";
107
108
            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...
109
                return $subField;
110
            });
111
        }
112
    }
113
}
114