Passed
Push — develop ( f6f5f6...710bfb )
by Paul
14:13
created

FlatsomeShortcode::styleConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Flatsome;
4
5
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
6
7
abstract class FlatsomeShortcode
8
{
9
    use IntegrationShortcode;
10
11
    abstract public function icon(): string;
12
13
    public function options(): array
14
    {
15
        $controls = array_merge(
16
            $this->settingsConfig(),
17
            $this->styleConfig(),
18
            $this->globalConfig(),
19
        );
20
        $controls = glsr()->filterArray('flatsome/controls', $controls, $this->shortcodeInstance());
21
        $groups = [ // order is intentional
22
            'general' => esc_html_x('General', 'admin-text', 'site-reviews'),
23
            'schema' => esc_html_x('Schema', 'admin-text', 'site-reviews'),
24
            'display' => esc_html_x('Display', 'admin-text', 'site-reviews'),
25
            'filters' => esc_html_x('Filters', 'admin-text', 'site-reviews'),
26
            'hide' => esc_html_x('Hide', 'admin-text', 'site-reviews'),
27
            'text' => esc_html_x('Text', 'admin-text', 'site-reviews'),
28
            'advanced' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
29
            'design' => esc_html_x('Design', 'admin-text', 'site-reviews'),
30
        ];
31
        $groupedcontrols = [];
32
        foreach ($controls as $name => $args) {
33
            $transformer = new Transformer($name, $args, $this->shortcodeInstance()->tag);
34
            $control = $transformer->control();
35
            if (empty($control)) {
36
                continue;
37
            }
38
            $group = $control['group'];
39
            $groupHeading = $groups[$group] ?? ucfirst($group);
40
            if (!array_key_exists($group, $groupedcontrols)) {
41
                $groupedcontrols[$group] = [
42
                    'collapsed' => true,
43
                    'heading' => $groupHeading,
44
                    'options' => [],
45
                    'type' => 'group',
46
                ];
47
            }
48
            $groupedcontrols[$group]['options'][$control['name']] = $control;
49
        }
50
        return array_combine( // we need to prefix the group names in order to target them with CSS
51
            array_map(fn ($k) => glsr()->prefix.$k, array_keys($groupedcontrols)),
52
            $groupedcontrols
53
        );
54
    }
55
56
    /**
57
     * Override the "add_ux_builder_shortcode" data with
58
     * the "ux_builder_shortcode_data_{$tag}" filter hook.
59
     */
60
    public function register(): void
61
    {
62
        add_action('ux_builder_setup', function () {
63
            if (!function_exists('add_ux_builder_shortcode')) {
64
                return;
65
            }
66
            $shortcode = $this->shortcodeInstance()->tag;
67
            add_ux_builder_shortcode($shortcode, [
68
                'category' => glsr()->name,
69
                'name' => $this->shortcodeInstance()->name,
70
                'options' => $this->options(),
71
                'scripts' => $this->scripts(),
72
                'styles' => $this->styles(),
73
                'thumbnail' => $this->icon(),
74
                'wrap' => false,
75
            ]);
76
        });
77
    }
78
79
    /**
80
     * Disabled visibility for now because Flatsome is simply a fancy shortcode
81
     * wrapper and it doesn't seem possible to intercept the shortcode rendering
82
     * from Flatsome.
83
     */
84
    protected function globalConfig(): array
85
    {
86
        return [];
87
        // $global = [];
88
        // $visibility = get_template_directory().'/inc/builder/shortcodes/commons/visibility.php';
89
        // if (file_exists($visibility)) {
90
        //     $global['visibility'] = require $visibility;
91
        //     $global['visibility']['group'] = 'advanced';
92
        // }
93
        // return $global;
94
    }
95
96
    protected function scripts(): array
97
    {
98
        return [];
99
    }
100
101
    protected function settingsConfig(): array
102
    {
103
        $hidden = [
104
            'from' => [ // used to set the "from" attribute
105
                'type' => 'textfield',
106
                'default' => 'flatsome',
107
                'group' => 'hidden',
108
                'save_when_default' => true,
109
                'value' => 'flatsome',
110
            ],
111
        ];
112
        return array_merge($hidden, $this->shortcodeInstance()->settings());
113
    }
114
115
    protected function styleConfig(): array
116
    {
117
        return [];
118
    }
119
120
    protected function styles(): array
121
    {
122
        return [];
123
    }
124
}
125