Completed
Push — componentlibrary ( 34b329...2e7aeb )
by
unknown
01:30
created

functions.php ➔ parseComponentReadme()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Components\ListComponents;
4
5
use Flynt;
6
use Flynt\ComponentManager;
7
use Flynt\Utils\Asset;
8
9
add_filter('Flynt/addComponentData?name=ListComponents', function ($data) {
10
    if (!empty($data['componentBlocks'])) {
11
        $data['componentBlocks'] = array_map(function ($block) {
12
            $componentPaths = explode('/', $block['component']);
13
            $block['component'] = implode('/', array_slice($componentPaths, count($componentPaths)-3, 3));
14
15
            if (file_exists(Asset::requirePath($block['component'] . 'screenshot.png'))) {
16
                $src = Asset::requireUrl($block['component'] . 'screenshot.png');
17
                list($width, $height) = getimagesize(Asset::requirePath($block['component'] . 'screenshot.png'));
18
19
                $block['componentScreenshot'] = [
20
                    'src' => $src,
21
                    'aspect' => $width / $height
22
                ];
23
            }
24
25
            $readme = Asset::requirePath($block['component'] . 'README.md');
26
27
            if (file_exists($readme)) {
28
                $block['readme'] = parseComponentReadme(file_get_contents($readme));
29
            }
30
31
            return $block;
32
        }, $data['componentBlocks']);
33
    }
34
35
    return $data;
36
});
37
38
39
add_filter('acf/load_field/name=component', function ($field) {
40
    $componentManager = ComponentManager::getInstance();
41
    $field['choices'] = array_flip($componentManager->getAll());
42
    return $field;
43
});
44
45
function parseComponentReadme($file)
46
{
47
    $content = [];
48
    $fields = preg_split('!\n---\s*\n*!', $file);
49
    foreach ($fields as $field) {
50
        $pos = strpos($field, ':');
51
        $key = str_replace(['-', ' '], '_', strtolower(trim(substr($field, 0, $pos))));
52
        if (empty($key)) {
53
            continue;
54
        }
55
        $content[$key] = trim(substr($field, $pos + 1));
56
        if ($key === 'text') {
57
            $content['html'] = $content[$key];
58
        }
59
    }
60
    return $content;
61
}
62
63
Flynt\registerFields('ListComponents', [
64
    'layout' => [
65
        'name' => 'listComponents',
66
        'label' => 'List: Components',
67
        'sub_fields' => [
68
            [
69
                'label' => 'Component Blocks',
70
                'name' => 'componentBlocks',
71
                'type' => 'repeater',
72
                'collapsed' => 0,
73
                'min' => 1,
74
                'layout' => 'block',
75
                'button_label' => 'Add Component Block',
76
                'sub_fields' => [
77
                    [
78
                        'label' => 'Component',
79
                        'name' => 'component',
80
                        'type' => 'select',
81
                        'ui' => 1,
82
                        'ajax' => 0,
83
                        'choices' => []
84
                    ],
85
                    [
86
                        'label' => 'Calls To Action',
87
                        'name' => 'ctas',
88
                        'type' => 'repeater',
89
                        'collapsed' => 0,
90
                        'layout' => 'table',
91
                        'button_label' => 'Add Call To Action',
92
                        'sub_fields' => [
93
                            [
94
                                'label' => 'Link',
95
                                'name' => 'link',
96
                                'type' => 'link',
97
                                'return_format' => 'array',
98
                                'required' => 1,
99
                                'wrapper' => [
100
                                    'width' => 70
101
                                ]
102
                            ],
103
                            [
104
                                'label' => 'Button Type',
105
                                'name' => 'buttonType',
106
                                'type' => 'button_group',
107
                                'choices' => [
108
                                    'primary' => 'Primary',
109
                                    'secondary' => 'Secondary'
110
                                ],
111
                                'wrapper' => [
112
                                    'width' => 30
113
                                ],
114
                                'ui' => 1
115
                            ]
116
                        ]
117
                    ]
118
                ]
119
            ]
120
        ]
121
    ]
122
]);
123