Completed
Push — constructionplanless_gutenberg ( 97fb4a )
by Dominik
01:32
created

functions.php ➔ renderBlockCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\AcfGutenberg;
4
5
use Flynt;
6
use Flynt\Utils\Asset;
7
use ACFComposer\ACFComposer;
8
use Timber\Timber;
9
10
function registerGutenbergComponents($components, $postTypes = null)
11
{
12
    foreach ($components as $component) {
13
        $config = apply_filters($component, null);
14
        acf_register_block([
15
            'name' => $config['name'],
16
            'title' => $config['label'],
17
            'render_callback' => renderBlockCallback($config['name']),
18
            'category' => 'flynt',
19
            'mode' => 'edit',
20
            'post_types' => $postTypes,
21
            'align' => 'full',
22
            // 'supports' => [
23
            //     'align' => 'wide',
24
            // ],
25
            // 'icon' => 'admin-comments',
26
            // 'keywords' => array( 'testimonial', 'quote' ),
27
        ]);
28
        $fieldGroup = [
29
            'name' => $config['name'],
30
            'title' => $config['label'],
31
            'fields' => [
32
                $component . '/SubFields',
33
            ],
34
            'location' => [
35
                [
36
                    [
37
                        'param' => 'block',
38
                        'operator' => '==',
39
                        'value' => 'acf/' . strtolower($config['name']),
40
                    ],
41
                ],
42
            ],
43
        ];
44
45
        add_action('enqueue_block_editor_assets', function () use ($config) {
46
            $name = ucfirst($config['name']);
47
            Asset::enqueue([
48
                'type' => 'style',
49
                'name' => "Flynt/Components/{$name}",
50
                'path' => "Components/{$name}/style.css",
51
                'dependencies' => ['wp-edit-blocks'],
52
            ]);
53
        });
54
55
        ACFComposer::registerFieldGroup($fieldGroup);
56
    }
57
};
58
59
function renderBlockCallback($componentName)
60
{
61
    return function ($block, $content, $isPreview, $postId) use ($componentName) {
0 ignored issues
show
Unused Code introduced by
The parameter $block 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...
Unused Code introduced by
The parameter $content 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...
Unused Code introduced by
The parameter $isPreview 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...
Unused Code introduced by
The parameter $postId 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...
62
        $html = Timber::compile_string('{{ renderComponent(name, data)}}', [
63
            'name' => ucfirst($componentName),
64
            'data' => get_fields(),
65
        ]);
66
        echo $html;
67
    };
68
};
69
70
add_filter('block_categories', function ($categories, $post) {
0 ignored issues
show
Unused Code introduced by
The parameter $post 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...
71
    return array_merge(
72
        [
73
            [
74
                'slug' => 'flynt',
75
                'title' => __('Flynt Components', 'flynt'),
76
                // 'icon'  => 'welcome-view-site',
77
            ],
78
        ],
79
        $categories
80
    );
81
}, 10, 2);
82
83
add_theme_support('align-wide');
84
add_theme_support('align-full');
85
86
add_action('enqueue_block_editor_assets', function () {
87
    Asset::enqueue([
88
        'name' => 'Flynt/assets',
89
        'path' => 'assets/style.css',
90
        'type' => 'style',
91
        'dependencies' => ['wp-edit-blocks'],
92
    ]);
93
    Asset::enqueue([
94
        'type' => 'script',
95
        'name' => 'Flynt/Features/AcfGutenberg',
96
        'path' => 'Features/AcfGutenberg/admin.js',
97
        'dependencies' => ['wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-api'],
98
        // 'dependencies' => ['wp-blocks', 'wp-element'],
99
    ]);
100
    Asset::enqueue([
101
        'name' => 'lazysizes',
102
        'type' => 'script',
103
        'path' => 'vendor/lazysizes.js'
104
    ]);
105
});
106