Passed
Push — develop ( 3839fb...f9b986 )
by Paul
19:59
created

ElementorWidget::get_icon()   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\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 controlGroupsForContent(): array
63
    {
64
        return [
65
            'advanced' => _x('Advanced', 'admin-text', 'site-reviews'),
66
            'display' => _x('Display', 'admin-text', 'site-reviews'),
67
            'general' => _x('General', 'admin-text', 'site-reviews'),
68
            'hide' => _x('Hide', 'admin-text', 'site-reviews'),
69
            'schema' => _x('Schema', 'admin-text', 'site-reviews'),
70
            'text' => _x('Text', 'admin-text', 'site-reviews'),
71
        ];
72
    }
73
74
    protected function controlGroupsForStyle(): array
75
    {
76
        return [
77
            'design' => _x('Design', 'admin-text', 'site-reviews'),
78
        ];
79
    }
80
81
    protected function controlSections(): array
82
    {
83
        $controls = array_merge(
84
            array_map(
85
                fn ($control) => wp_parse_args($control, [
86
                    'group' => 'general',
87
                    'tab' => Controls_Manager::TAB_CONTENT,
88
                ]),
89
                $this->settingsConfig()
90
            ),
91
            array_map(
92
                fn ($control) => wp_parse_args($control, [
93
                    'group' => 'design',
94
                    'tab' => Controls_Manager::TAB_STYLE,
95
                ]),
96
                $this->styleConfig()
97
            ),
98
        );
99
        $groups = [
100
            Controls_Manager::TAB_CONTENT => $this->controlGroupsForContent(),
101
            Controls_Manager::TAB_STYLE => $this->controlGroupsForStyle(),
102
        ];
103
        $controls = glsr()->filterArray('elementor/controls', $controls, $this);
104
        $groupsLabels = glsr()->filterArray('elementor/groups', $groups, $this);
105
        $sections = [];
106
        foreach ($controls as $name => $control) {
107
            $tab = $control['tab'] ?? '';
108
            $group = $control['group'] ?? '';
109
            $section = "section_{$group}";
110
            if (!isset($groupsLabels[$tab][$group])) {
111
                continue;
112
            }
113
            $sections[$section] ??= [
114
                'controls' => [],
115
                'label' => $groupsLabels[$tab][$group],
116
                'tab' => $tab,
117
            ];
118
            $transformedControl = $this->transformControl($name, $control);
119
            $sections[$section]['controls'][$transformedControl['name']] = $transformedControl;
120
        }
121
        return $sections;
122
    }
123
124
    protected function get_upsale_data(): array
125
    {
126
        $data = [
127
            'condition' => !glsr(License::class)->isPremium(),
128
            'description' => esc_html_x('Upgrade to Site Reviews Premium and get a bunch of additional features.', 'admin-text', 'site-reviews'),
129
            'image' => glsr()->url('assets/images/premium.svg'),
130
            'image_alt' => esc_attr_x('Upgrade', 'admin-text', 'site-reviews'),
131
            'upgrade_text' => esc_html_x('Upgrade Now', 'admin-text', 'site-reviews'),
132
            'upgrade_url' => glsr_premium_url('site-reviews-premium'),
133
        ];
134
        return glsr()->filterArray('elementor/upsale_data', $data, $this);
135
    }
136
137
    protected function register_controls(): void
138
    {
139
        foreach ($this->controlSections() as $sectionId => $args) {
140
            $controls = array_filter($args['controls'] ?? []);
141
            if (empty($controls)) {
142
                continue;
143
            }
144
            unset($args['controls']);
145
            $this->start_controls_section($sectionId, $args);
146
            $this->registerControlsForSection($controls);
147
            $this->end_controls_section();
148
        }
149
    }
150
151
    protected function registerControlsForSection(array $controls): void
152
    {
153
        $groupTypes = Controls_Manager::get_groups_names();
154
        foreach ($controls as $controlId => $args) {
155
            if (!isset($args['type'])) {
156
                continue; // controls must have a type
157
            }
158
            if (Controls_Manager::SELECT2 === $args['type'] && empty($args['options'])) {
159
                continue; // skip select controls with empty options
160
            }
161
            if (in_array($args['type'], $groupTypes)) {
162
                $this->add_group_control($args['type'], $args);
163
                continue; // this is a grouped control
164
            }
165
            if ($args['is_responsive'] ?? false) { // for responsive controls
166
                $this->add_responsive_control($controlId, $args);
167
                continue; // this is a responsive control
168
            }
169
            $this->add_control($controlId, $args);
170
        }
171
    }
172
173
    protected function render(): void
174
    {
175
        $args = $this->get_settings_for_display();
176
        if ($this->hide_if_all_fields_hidden && !$this->shortcodeInstance()->hasVisibleFields($args)) {
177
            return;
178
        }
179
        $html = $this->shortcodeInstance()->build($args, 'elementor');
180
        $html = str_replace('class="glsr-fallback">', 'class="glsr-fallback" style="display:none;">', $html);
181
        echo $html;
182
    }
183
184
    protected function settingsConfig(): array
185
    {
186
        return $this->shortcodeInstance()->settings();
187
    }
188
189
    protected function styleConfig(): array
190
    {
191
        return [];
192
    }
193
194
    protected function transformControl(string $name, array $args): array
195
    {
196
        unset($args['tab']); // only section controls should have the tab set
197
        return glsr(ControlDefaults::class)->merge(
198
            wp_parse_args(compact('name'), $args)
199
        );
200
    }
201
}
202