Test Failed
Push — develop ( 55c79d...550055 )
by Paul
09:38
created

ElementorWidget::controlSections()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 28
rs 9.2888
ccs 0
cts 11
cp 0
cc 5
nc 7
nop 0
crap 30
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Elementor;
4
5
use Elementor\Controls_Manager;
6
use Elementor\Widget_Base;
7
use GeminiLabs\SiteReviews\Integrations\Elementor\Defaults\ControlDefaults;
8
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
9
10
abstract class ElementorWidget extends Widget_Base
11
{
12
    use IntegrationShortcode;
13
14
    protected bool $hide_if_all_fields_hidden = true;
15
16
    public function get_categories(): array
17
    {
18
        return [glsr()->id];
19
    }
20
21
    public function get_icon(): string
22
    {
23
        return 'eicon-star-o';
24
    }
25
26
    public function get_name(): string
27
    {
28
        return $this->shortcodeInstance()->tag;
0 ignored issues
show
Bug introduced by
Accessing tag on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
29
    }
30
31
    public function get_title(): string
32
    {
33
        return $this->shortcodeInstance()->name;
34
    }
35
36
    protected function controlGroups(): array
37
    {
38
        return [
39
            'general' => [
40
                'controls' => [],
41
                'label' => _x('General', 'admin-text', 'site-reviews'),
42
                'tab' => Controls_Manager::TAB_CONTENT,
43
            ],
44
            'advanced' => [
45
                'controls' => [],
46
                'label' => _x('Advanced', 'admin-text', 'site-reviews'),
47
                'tab' => Controls_Manager::TAB_CONTENT,
48
            ],
49
            'design' => [
50
                'controls' => [],
51
                'label' => _x('Design', 'admin-text', 'site-reviews'),
52
                'tab' => Controls_Manager::TAB_STYLE,
53
            ],
54
        ];
55
    }
56
57
    protected function controlHeadings(): array
58
    {
59
        return [
60
            'display' => esc_html_x('Display', 'admin-text', 'site-reviews'),
61
            'hide' => esc_html_x('Hide', 'admin-text', 'site-reviews'),
62
            'schema' => esc_html_x('Schema', 'admin-text', 'site-reviews'),
63
            'text' => esc_html_x('Text', 'admin-text', 'site-reviews'),
64
        ];
65
    }
66
67
    protected function controlSections(): array
68
    {
69
        $controls = array_merge(
70
            array_map(fn ($control) => wp_parse_args($control, ['group' => 'general']), $this->settingsConfig()),
71
            array_map(fn ($control) => wp_parse_args($control, ['group' => 'design']), $this->styleConfig()),
72
        );
73
        $groups = $this->controlGroups();
74
        $headings = $this->controlHeadings();
75
        foreach ($controls as $key => $control) {
76
            $group = $control['group'] ?? '';
77
            $heading = $group;
78
            if (!array_key_exists($group, $groups)) {
79
                $group = 'general';
80
            }
81
            if (!array_key_exists($heading, $headings)) {
82
                $groups[$group]['controls'][$key] = $this->transformControl($key, $control);
83
                continue;
84
            }
85
            if (!array_key_exists("separator_{$heading}", $groups[$group]['controls'])) {
86
                $groups[$group]['controls']["separator_{$heading}"] = [
87
                    'type' => Controls_Manager::HEADING,
88
                    'label' => $headings[$heading],
89
                    'separator' => 'before',
90
                ];
91
            }
92
            $groups[$group]['controls'][$key] = $this->transformControl($key, $control);
93
        }
94
        return $groups;
95
    }
96
97
    protected function register_controls(): void
98
    {
99
        $sections = $this->controlSections();
100
        $sections = glsr()->filterArray('elementor/register/controls', $sections, $this);
101
        foreach ($sections as $sectionId => $args) {
102
            $controls = array_filter($args['controls'] ?? []);
103
            if (empty($controls)) {
104
                continue;
105
            }
106
            unset($args['controls']);
107
            $this->start_controls_section($sectionId, $args);
108
            $this->registerControlsForSection($controls);
109
            $this->end_controls_section();
110
        }
111
    }
112
113
    protected function registerControlsForSection(array $controls): void
114
    {
115
        foreach ($controls as $controlId => $args) {
116
            if (Controls_Manager::SELECT2 === $args['type'] && empty($args['options'])) {
117
                continue; // skip select controls with empty options
118
            }
119
            if ($args['group_control_type'] ?? false) { // for grouped controls like typography
120
                $this->add_group_control($args['group_control_type'], $args);
121
                continue;
122
            }
123
            if ($args['is_responsive'] ?? false) { // for responsive controls
124
                $this->add_responsive_control($controlId, $args);
125
                continue;
126
            }
127
            $this->add_control($controlId, $args);
128
        }
129
    }
130
131
    protected function render(): void
132
    {
133
        $args = $this->get_settings_for_display();
134
        if ($this->hide_if_all_fields_hidden && !$this->shortcodeInstance()->hasVisibleFields($args)) {
135
            return;
136
        }
137
        $html = $this->shortcodeInstance()->build($args, 'elementor');
138
        $html = str_replace('class="glsr-fallback">', 'class="glsr-fallback" style="display:none;">', $html);
139
        echo $html;
140
    }
141
142
    protected function settingsConfig(): array
143
    {
144
        return $this->shortcodeInstance()->settings();
145
    }
146
147
    protected function styleConfig(): array
148
    {
149
        return [];
150
    }
151
152
    protected function transformControl(string $key, array $args): array
153
    {
154
        $args['name'] = $key;
155
        return glsr(ControlDefaults::class)->merge($args);
156
    }
157
}
158