Passed
Push — develop ( 49b51d...3839fb )
by Paul
26:28
created

ElementorWidget::controlSections()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 22
c 1
b 1
f 0
dl 0
loc 30
ccs 0
cts 25
cp 0
rs 9.2568
cc 5
nc 7
nop 0
crap 30
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Elementor\Widgets;
4
5
use Elementor\Controls_Manager;
6
use Elementor\Widget_Base;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Integrations\Elementor\Defaults\ControlDefaults;
9
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
10
use GeminiLabs\SiteReviews\License;
11
12
abstract class ElementorWidget extends Widget_Base
13
{
14
    use IntegrationShortcode;
15
16
    protected bool $hide_if_all_fields_hidden = true;
17
18
    public function get_categories(): array
19
    {
20
        return [glsr()->id];
21
    }
22
23
    public function get_icon(): string
24
    {
25
        return 'eicon-star-o';
26
    }
27
28
    public function get_name(): string
29
    {
30
        return $this->shortcodeInstance()->tag;
31
    }
32
33
    /**
34
     * Elementor throws a JS error when removing a widget from the page if it
35
     * has a control with "id" as the name. To fix this, we transformed "id"
36
     * to "shortcode_id" and "class" to "shortcode_class" (just in case).
37
     *
38
     * When the widget is displayed, we need to convert the setting keys back
39
     * to their original names.
40
     *
41
     * @param string $settingKey
42
     *
43
     * @return mixed
44
     */
45
    public function get_settings_for_display($settingKey = null)
46
    {
47
        $settings = parent::get_settings_for_display();
48
        $settings['class'] = $settings['shortcode_class']; // because Elementor throws a JS error for these
49
        $settings['id'] = $settings['shortcode_id']; // because Elementor throws a JS error for these
50
        $settings = glsr()->filterArray('elementor/display/settings', $settings, $this);
51
        if ($settingKey) {
52
            return Arr::get($settings, $settingKey);
53
        }
54
        return $settings;
55
    }
56
57
    public function get_title(): string
58
    {
59
        return $this->shortcodeInstance()->name;
60
    }
61
62
    protected function controlGroups(): array
63
    {
64
        return [
65
            'general' => [
66
                'controls' => [],
67
                'label' => _x('General', 'admin-text', 'site-reviews'),
68
                'tab' => Controls_Manager::TAB_CONTENT,
69
            ],
70
            'advanced' => [
71
                'controls' => [],
72
                'label' => _x('Advanced', 'admin-text', 'site-reviews'),
73
                'tab' => Controls_Manager::TAB_CONTENT,
74
            ],
75
            'design' => [
76
                'controls' => [],
77
                'label' => _x('Design', 'admin-text', 'site-reviews'),
78
                'tab' => Controls_Manager::TAB_STYLE,
79
            ],
80
        ];
81
    }
82
83
    protected function controlHeadings(): array
84
    {
85
        return [
86
            'display' => esc_html_x('Display', 'admin-text', 'site-reviews'),
87
            'hide' => esc_html_x('Hide', 'admin-text', 'site-reviews'),
88
            'schema' => esc_html_x('Schema', 'admin-text', 'site-reviews'),
89
            'text' => esc_html_x('Text', 'admin-text', 'site-reviews'),
90
        ];
91
    }
92
93
    protected function controlSections(): array
94
    {
95
        $controls = array_merge(
96
            array_map(fn ($control) => wp_parse_args($control, ['group' => 'general']), $this->settingsConfig()),
97
            array_map(fn ($control) => wp_parse_args($control, ['group' => 'design']), $this->styleConfig()),
98
        );
99
        $controls = glsr()->filterArray('elementor/controls', $controls, $this);
100
        $groups = $this->controlGroups();
101
        $headings = $this->controlHeadings();
102
        foreach ($controls as $key => $control) {
103
            $group = $control['group'] ?? '';
104
            $heading = $group;
105
            if (!array_key_exists($group, $groups)) {
106
                $group = 'general';
107
            }
108
            $control = $this->transformControl($key, $control);
109
            if (!array_key_exists($heading, $headings)) {
110
                $groups[$group]['controls'][$control['name']] = $control;
111
                continue;
112
            }
113
            if (!array_key_exists("separator_{$heading}", $groups[$group]['controls'])) {
114
                $groups[$group]['controls']["separator_{$heading}"] = [
115
                    'type' => Controls_Manager::HEADING,
116
                    'label' => $headings[$heading],
117
                    'separator' => 'before',
118
                ];
119
            }
120
            $groups[$group]['controls'][$control['name']] = $control;
121
        }
122
        return $groups;
123
    }
124
125
    protected function get_upsale_data(): array
126
    {
127
        $data = [
128
            'condition' => !glsr(License::class)->isPremium(),
129
            'description' => esc_html_x('Upgrade to Site Reviews Premium and get a bunch of additional features.', 'admin-text', 'site-reviews'),
130
            'image' => glsr()->url('assets/images/premium.svg'),
131
            'image_alt' => esc_attr_x('Upgrade', 'admin-text', 'site-reviews'),
132
            'upgrade_text' => esc_html_x('Upgrade Now', 'admin-text', 'site-reviews'),
133
            'upgrade_url' => glsr_premium_url('site-reviews-premium'),
134
        ];
135
        return glsr()->filterArray('elementor/upsale_data', $data, $this);
136
    }
137
138
    protected function register_controls(): void
139
    {
140
        foreach ($this->controlSections() as $sectionId => $args) {
141
            $controls = array_filter($args['controls'] ?? []);
142
            if (empty($controls)) {
143
                continue;
144
            }
145
            unset($args['controls']);
146
            $this->start_controls_section($sectionId, $args);
147
            $this->registerControlsForSection($controls);
148
            $this->end_controls_section();
149
        }
150
    }
151
152
    protected function registerControlsForSection(array $controls): void
153
    {
154
        $groupTypes = Controls_Manager::get_groups_names();
155
        foreach ($controls as $controlId => $args) {
156
            if (!isset($args['type'])) {
157
                continue;
158
            }
159
            if (Controls_Manager::SELECT2 === $args['type'] && empty($args['options'])) {
160
                continue; // skip select controls with empty options
161
            }
162
            if (in_array($args['type'], $groupTypes)) {
163
                $this->add_group_control($args['type'], $args);
164
                continue;
165
            }
166
            if ($args['is_responsive'] ?? false) { // for responsive controls
167
                $this->add_responsive_control($controlId, $args);
168
                continue;
169
            }
170
            $this->add_control($controlId, $args);
171
        }
172
    }
173
174
    protected function render(): void
175
    {
176
        $args = $this->get_settings_for_display();
177
        if ($this->hide_if_all_fields_hidden && !$this->shortcodeInstance()->hasVisibleFields($args)) {
178
            return;
179
        }
180
        $html = $this->shortcodeInstance()->build($args, 'elementor');
181
        $html = str_replace('class="glsr-fallback">', 'class="glsr-fallback" style="display:none;">', $html);
182
        echo $html;
183
    }
184
185
    protected function settingsConfig(): array
186
    {
187
        return $this->shortcodeInstance()->settings();
188
    }
189
190
    protected function styleConfig(): array
191
    {
192
        return [];
193
    }
194
195
    protected function transformControl(string $name, array $args): array
196
    {
197
        return glsr(ControlDefaults::class)->merge(
198
            wp_parse_args(compact('name'), $args)
199
        );
200
    }
201
}
202