Test Failed
Push — develop ( c07ba6...801573 )
by Paul
08:42
created

ElementorWidget::get_control_sections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 9.7
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Elementor;
4
5
use Elementor\Controls_Manager;
6
use Elementor\Widget_Base;
7
use GeminiLabs\SiteReviews\Database;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
11
12
abstract class ElementorWidget extends Widget_Base
13
{
14
    use IntegrationShortcode;
15
16
    /**
17
     * @return array
18
     */
19
    public function get_categories()
20
    {
21
        return [glsr()->id];
22
    }
23
24
    /**
25
     * @return string
26
     */
27
    public function get_icon()
28
    {
29
        return 'eicon-star-o';
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function get_name()
36
    {
37
        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...
38
    }
39
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']; // @compat
49
        $settings['id'] = $settings['shortcode_id']; // @compat
50
        if (!empty($settings['assigned_posts_custom'])) {
51
            $settings['assigned_posts'] = $settings['assigned_posts_custom'];
52
        }
53
        $hide = [];
54
        foreach ($settings as $key => $value) {
55
            if (str_starts_with($key, 'hide-') && !empty($value)) {
56
                $hide[] = Str::removePrefix($key, 'hide-');
57
            }
58
        }
59
        $settings['hide'] = array_filter($hide);
60
        $settings = glsr()->filterArray('elementor/display/settings', $settings, $this);
61
        if ($settingKey) {
62
            return Arr::get($settings, $settingKey);
63
        }
64
        return $settings;
65
    }
66
67
    public function get_title()
68
    {
69
        return $this->shortcodeInstance()->name;
70
    }
71
72
    protected function assigned_posts_options(): array
73
    {
74
        return [ // order is intentional
75
            'custom' => _x('Specific Post ID', 'admin-text', 'site-reviews'),
76
            'post_id' => _x('The Current Page', 'admin-text', 'site-reviews'),
77
            'parent_id' => _x('The Parent Page', 'admin-text', 'site-reviews'),
78
        ];
79
    }
80
81
    protected function assigned_terms_options(): array
82
    {
83
        return glsr(Database::class)->terms();
84
    }
85
86
    protected function assigned_users_options(): array
87
    {
88
        return [ // order is intentional
89
            'custom' => _x('Specific User ID', 'admin-text', 'site-reviews'),
90
            'user_id' => _x('The Logged-in user', 'admin-text', 'site-reviews'),
91
            'author_id' => _x('The Page author', 'admin-text', 'site-reviews'),
92
            'profile_id' => _x('The Profile user (BuddyPress/Ultimate Member)', 'admin-text', 'site-reviews'),
93
        ];
94
    }
95
96
    protected function hide_if_all_fields_hidden(): bool
97
    {
98
        return false;
99
    }
100
101
    protected function get_control_sections(): array
102
    {
103
        return [
104
            'settings' => [
105
                'controls' => $this->settings_basic(),
106
                'label' => _x('Settings', 'admin-text', 'site-reviews'),
107
                'tab' => Controls_Manager::TAB_CONTENT,
108
            ],
109
            'advanced' => [
110
                'controls' => $this->settings_advanced(),
111
                'label' => _x('Advanced', 'admin-text', 'site-reviews'),
112
                'tab' => Controls_Manager::TAB_CONTENT,
113
            ],
114
            'style_rating' => [
115
                'controls' => $this->settings_rating(),
116
                'label' => _x('Rating', 'admin-text', 'site-reviews'),
117
                'tab' => Controls_Manager::TAB_STYLE,
118
            ],
119
            'style_layout' => [
120
                'controls' => $this->settings_layout(),
121
                'label' => _x('Layout', 'admin-text', 'site-reviews'),
122
                'tab' => Controls_Manager::TAB_STYLE,
123
            ],
124
        ];
125
    }
126
127
    protected function insert_hide_controls(array $controls): array
128
    {
129
        $hideOptions = $this->shortcodeInstance()->options('hide');
130
        foreach ($hideOptions as $key => $label) {
131
            $separator = $key === key(array_slice($hideOptions, 0, 1)) ? 'before' : 'default';
132
            $controls["hide-{$key}"] = [
133
                'label' => $label,
134
                'separator' => $separator,
135
                'return_value' => '1',
136
                'type' => Controls_Manager::SWITCHER,
137
            ];
138
        }
139
        return $controls;
140
    }
141
142
    protected function get_type_control(): array
143
    {
144
        if ($options = $this->shortcodeInstance()->options('type')) {
145
            return [
146
                'default' => 'local',
147
                'label' => _x('Limit the Type of Reviews', 'admin-text', 'site-reviews'),
148
                'label_block' => true,
149
                'options' => $options,
150
                'type' => Controls_Manager::SELECT,
151
            ];
152
        }
153
        return [];
154
    }
155
156
    /**
157
     * @return void
158
     */
159
    protected function register_controls()
160
    {
161
        $sections = $this->get_control_sections();
162
        $sections = glsr()->filterArray('elementor/register/controls', $sections, $this);
163
        foreach ($sections as $key => $args) {
164
            $controls = array_filter($args['controls'] ?? []);
165
            if (empty($controls)) {
166
                continue;
167
            }
168
            $this->start_controls_section($key, $args);
169
            $this->register_controls_for_section($controls);
170
            $this->end_controls_section();
171
        }
172
    }
173
174
    protected function register_controls_for_section(array $controls): void
175
    {
176
        foreach ($controls as $key => $args) {
177
            if ($args['group_control_type'] ?? false) {
178
                $args['name'] = $key;
179
                $this->add_group_control($args['group_control_type'], $args);
180
                continue;
181
            }
182
            if ($args['is_responsive'] ?? false) {
183
                $this->add_responsive_control($key, $args);
184
                continue;
185
            }
186
            $this->add_control($key, $args);
187
        }
188
    }
189
190
    /**
191
     * @return void
192
     */
193
    protected function render()
194
    {
195
        $args = $this->get_settings_for_display();
196
        if ($this->hide_if_all_fields_hidden() && !$this->shortcodeInstance()->hasVisibleFields($args)) {
197
            return;
198
        }
199
        $html = $this->shortcodeInstance()->build($args, 'elementor');
200
        $html = str_replace('class="glsr-fallback">', 'class="glsr-fallback" style="display:none;">', $html);
201
        echo $html;
202
    }
203
204
    protected function set_custom_size_unit(array $units): array
205
    {
206
        if (version_compare(\ELEMENTOR_VERSION, '3.10.0', '>=')) {
207
            $units[] = 'custom';
208
        }
209
        return $units;
210
    }
211
212
    protected function settings_advanced(): array
213
    {
214
        return [
215
            'shortcode_id' => [
216
                'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'),
217
                'label_block' => true,
218
                'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'),
219
                'type' => Controls_Manager::TEXT,
220
            ],
221
            'shortcode_class' => [
222
                'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
223
                'label_block' => true,
224
                'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'),
225
                'type' => Controls_Manager::TEXT,
226
            ],
227
        ];
228
    }
229
230
    protected function settings_basic(): array
231
    {
232
        return [];
233
    }
234
235
    protected function settings_layout(): array
236
    {
237
        return [];
238
    }
239
240
    protected function settings_rating(): array
241
    {
242
        return [];
243
    }
244
}
245