Test Failed
Push — main ( 6435b1...db043e )
by Paul
16:23 queued 07:02
created

ElementorWidget   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 34
eloc 101
dl 0
loc 232
ccs 0
cts 96
cp 0
rs 9.68
c 3
b 0
f 1

19 Methods

Rating   Name   Duplication   Size   Complexity  
A get_categories() 0 3 1
A assigned_terms_options() 0 3 1
A get_settings_for_display() 0 20 6
A get_shortcode_instance() 0 6 2
A get_name() 0 3 1
A get_icon() 0 3 1
A settings_layout() 0 3 1
A get_review_types() 0 13 2
A settings_rating() 0 3 1
A render() 0 10 3
A settings_basic() 0 3 1
A register_controls() 0 12 3
A hide_if_all_fields_hidden() 0 3 1
A set_custom_size_unit() 0 6 2
A register_controls_for_section() 0 13 4
A settings_advanced() 0 14 1
A assigned_posts_options() 0 6 1
A get_control_sections() 0 22 1
A assigned_users_options() 0 7 1
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
11
abstract class ElementorWidget extends Widget_Base
12
{
13
    /**
14
     * @var \GeminiLabs\SiteReviews\Shortcodes\Shortcode|callable
15
     */
16
    private $_shortcode_instance;
17
18
    /**
19
     * @return array
20
     */
21
    public function get_categories()
22
    {
23
        return [glsr()->id];
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public function get_icon()
30
    {
31
        return 'eicon-star-o';
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function get_name()
38
    {
39
        return $this->get_shortcode_instance()->shortcode;
40
    }
41
42
    /**
43
     * @param string $settingKey
44
     *
45
     * @return mixed
46
     */
47
    public function get_settings_for_display($settingKey = null)
48
    {
49
        $settings = parent::get_settings_for_display();
50
        $settings['class'] = $settings['shortcode_class']; // @compat
51
        $settings['id'] = $settings['shortcode_id']; // @compat
52
        if (!empty($settings['assigned_posts_custom'])) {
53
            $settings['assigned_posts'] = $settings['assigned_posts_custom'];
54
        }
55
        $hide = [];
56
        foreach ($settings as $key => $value) {
57
            if (str_starts_with($key, 'hide-') && !empty($value)) {
58
                $hide[] = Str::removePrefix($key, 'hide-');
59
            }
60
        }
61
        $settings['hide'] = array_filter($hide);
62
        $settings = glsr()->filterArray('elementor/display/settings', $settings, $this);
63
        if ($settingKey) {
64
            return Arr::get($settings, $settingKey);
65
        }
66
        return $settings;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    abstract public function get_shortcode();
73
74
    /**
75
     * @return \GeminiLabs\SiteReviews\Shortcodes\Shortcode
76
     */
77
    public function get_shortcode_instance()
78
    {
79
        if (is_null($this->_shortcode_instance)) { // @phpstan-ignore-line
80
            $this->_shortcode_instance = glsr($this->get_shortcode());
81
        }
82
        return $this->_shortcode_instance;
83
    }
84
85
    protected function assigned_posts_options(): array
86
    {
87
        return [ // order is intentional
88
            'custom' => _x('Specific Post ID', 'admin-text', 'site-reviews'),
89
            'post_id' => _x('The Current Page', 'admin-text', 'site-reviews'),
90
            'parent_id' => _x('The Parent Page', 'admin-text', 'site-reviews'),
91
        ];
92
    }
93
94
    protected function assigned_terms_options(): array
95
    {
96
        return glsr(Database::class)->terms();
97
    }
98
99
    protected function assigned_users_options(): array
100
    {
101
        return [ // order is intentional
102
            'custom' => _x('Specific User ID', 'admin-text', 'site-reviews'),
103
            'user_id' => _x('The Logged-in user', 'admin-text', 'site-reviews'),
104
            'author_id' => _x('The Page author', 'admin-text', 'site-reviews'),
105
            'profile_id' => _x('The Profile user (BuddyPress/Ultimate Member)', 'admin-text', 'site-reviews'),
106
        ];
107
    }
108
109
    protected function hide_if_all_fields_hidden(): bool
110
    {
111
        return false;
112
    }
113
114
    protected function get_control_sections(): array
115
    {
116
        return [
117
            'settings' => [
118
                'controls' => $this->settings_basic(),
119
                'label' => _x('Settings', 'admin-text', 'site-reviews'),
120
                'tab' => Controls_Manager::TAB_CONTENT,
121
            ],
122
            'advanced' => [
123
                'controls' => $this->settings_advanced(),
124
                'label' => _x('Advanced', 'admin-text', 'site-reviews'),
125
                'tab' => Controls_Manager::TAB_CONTENT,
126
            ],
127
            'style_rating' => [
128
                'controls' => $this->settings_rating(),
129
                'label' => _x('Rating', 'admin-text', 'site-reviews'),
130
                'tab' => Controls_Manager::TAB_STYLE,
131
            ],
132
            'style_layout' => [
133
                'controls' => $this->settings_layout(),
134
                'label' => _x('Layout', 'admin-text', 'site-reviews'),
135
                'tab' => Controls_Manager::TAB_STYLE,
136
            ],
137
        ];
138
    }
139
140
    protected function get_review_types(): array
141
    {
142
        $types = glsr()->retrieveAs('array', 'review_types', []);
143
        if (count($types) > 2) {
144
            return [
145
                'default' => 'local',
146
                'label' => _x('Limit the Type of Reviews', 'admin-text', 'site-reviews'),
147
                'label_block' => true,
148
                'options' => $types,
149
                'type' => Controls_Manager::SELECT,
150
            ];
151
        }
152
        return [];
153
    }
154
155
    /**
156
     * @return void
157
     */
158
    protected function register_controls()
159
    {
160
        $sections = $this->get_control_sections();
161
        $sections = glsr()->filterArray('elementor/register/controls', $sections, $this);
162
        foreach ($sections as $key => $args) {
163
            $controls = array_filter($args['controls'] ?? []);
164
            if (empty($controls)) {
165
                continue;
166
            }
167
            $this->start_controls_section($key, $args);
168
            $this->register_controls_for_section($controls);
169
            $this->end_controls_section();
170
        }
171
    }
172
173
    protected function register_controls_for_section(array $controls): void
174
    {
175
        foreach ($controls as $key => $args) {
176
            if ($args['group_control_type'] ?? false) {
177
                $args['name'] = $key;
178
                $this->add_group_control($args['group_control_type'], $args);
179
                continue;
180
            }
181
            if ($args['is_responsive'] ?? false) {
182
                $this->add_responsive_control($key, $args);
183
                continue;
184
            }
185
            $this->add_control($key, $args);
186
        }
187
    }
188
189
    /**
190
     * @return void
191
     */
192
    protected function render()
193
    {
194
        $args = $this->get_settings_for_display();
195
        $shortcode = $this->get_shortcode_instance();
196
        if ($this->hide_if_all_fields_hidden() && !$shortcode->hasVisibleFields($args)) {
197
            return;
198
        }
199
        $html = $shortcode->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