Passed
Push — develop ( f66d95...f9d2ce )
by Paul
13:45
created

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