FieldGroupComposer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 7 1
A loadFieldGroups() 0 25 4
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
            'acf/init',
22
            ['Flynt\Features\Acf\FieldGroupComposer', 'loadFieldGroups']
23
        );
24
    }
25
26
    public static function loadFieldGroups()
27
    {
28
        // prevent this running more than once
29
        if (self::$fieldGroupsLoaded) {
30
            return;
31
        }
32
33
        // Load field groups from files after ACF initializes
34
        $dir = get_template_directory() . self::FIELD_GROUPS_DIR;
35
36
        if (!is_dir($dir)) {
37
            trigger_error("[ACF] Cannot load field groups: {$dir} is not a valid directory!", E_USER_WARNING);
38
            return;
39
        }
40
41
        FileLoader::iterateDir($dir, function ($file) {
42
            if ($file->getExtension() === 'json') {
43
                $filePath = $file->getPathname();
44
                $config = json_decode(file_get_contents($filePath), true);
45
                ACFComposer::registerFieldGroup($config);
46
            }
47
        });
48
49
        self::$fieldGroupsLoaded = true;
50
    }
51
}
52