Passed
Push — master ( 197c27...f2cc15 )
by Paul
22:24 queued 07:48
created

Widget   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
eloc 64
c 0
b 0
f 0
dl 0
loc 180
ccs 0
cts 118
cp 0
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A get_review_types() 0 13 2
A assigned_posts_options() 0 6 1
A register_controls() 0 9 3
A get_icon() 0 3 1
A settings_advanced() 0 13 1
A render() 0 5 1
A get_categories() 0 3 1
A settings_basic() 0 3 1
A get_shortcode_instance() 0 6 2
A assigned_users_options() 0 8 1
A get_name() 0 3 1
A get_settings_for_display() 0 16 5
A assigned_terms_options() 0 3 1
A register_shortcode_options() 0 10 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Elementor;
4
5
use Elementor\Widget_Base;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
9
abstract class Widget extends Widget_Base
10
{
11
    /**
12
     * @var \GeminiLabs\SiteReviews\Shortcodes\Shortcode|callable
13
     */
14
    private $_shortcode_instance = null;
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->get_shortcode_instance()->shortcode;
38
    }
39
40
    /**
41
     * @param string $setting_key
42
     * @return mixed
43
     */
44
    public function get_settings_for_display($setting_key = null)
45
    {
46
        $settings = parent::get_settings_for_display();
47
        if (!empty($settings['assigned_posts_custom'])) {
48
            $settings['assigned_posts'] = $settings['assigned_posts_custom'];
49
        }
50
        $hide = [];
51
        foreach ($settings as $key => $value) {
52
            if (Str::startsWith('hide-', $key) && !empty($value)) {
53
                $hide[] = Str::removePrefix($key, 'hide-');
54
            }
55
        }
56
        $settings['class'] = $settings['shortcode_class']; // @compat
57
        $settings['hide'] = array_filter($hide);
58
        $settings['id'] = $settings['shortcode_id']; // @compat
59
        return $settings;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    abstract public function get_shortcode();
66
67
    /**
68
     * @return \GeminiLabs\SiteReviews\Shortcodes\Shortcode
69
     */
70
    public function get_shortcode_instance()
71
    {
72
        if (is_null($this->_shortcode_instance)) {
73
            $this->_shortcode_instance = glsr($this->get_shortcode());
74
        }
75
        return $this->_shortcode_instance;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    protected function assigned_posts_options()
82
    {
83
        return [
84
            'custom' => _x('Assign to multiple Post IDs', 'admin-text', 'site-reviews'),
85
            'post_id' => _x('The Current Page', 'admin-text', 'site-reviews').' (post_id)',
86
            'parent_id' => _x('The Parent Page', 'admin-text', 'site-reviews').' (parent_id)',
87
        ];
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    protected function assigned_terms_options()
94
    {
95
        return glsr(Database::class)->terms();
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    protected function assigned_users_options()
102
    {
103
        $options = [
104
            'author_id' => _x('The Page author', 'admin-text', 'site-reviews').' (author_id)',
105
            'profile_id' => _x('The Profile user (BuddyPress/Ultimate Member)', 'admin-text', 'site-reviews').' (profile_id)',
106
            'user_id' => _x('The Logged-in user', 'admin-text', 'site-reviews').' (user_id)',
107
        ];
108
        return $options + glsr(Database::class)->users();
109
    }
110
111
    protected function get_review_types()
112
    {
113
        $types = glsr()->retrieveAs('array', 'review_types', []);
114
        if (count($types) > 2) {
115
            return [
116
                'default' => 'local',
117
                'label' => _x('Limit the Type of Reviews', 'admin-text', 'site-reviews'),
118
                'label_block' => true,
119
                'options' => $types,
120
                'type' => \Elementor\Controls_Manager::SELECT,
121
            ];
122
        }
123
        return [];
124
    }
125
126
    /**
127
     * @return void
128
     */
129
    protected function register_controls()
130
    {
131
        $settings = array_filter(glsr()->filterArray('integration/elementor/settings', $this->settings_basic(), $this->get_name()));
132
        if (!empty($settings)) {
133
            $this->register_shortcode_options($settings, 'settings', _x('Settings', 'admin-text', 'site-reviews'));
134
        }
135
        $advanced = array_filter(glsr()->filterArray('integration/elementor/advanced', $this->settings_advanced(), $this->get_name()));
136
        if (!empty($advanced)) {
137
            $this->register_shortcode_options($advanced, 'advanced', _x('Advanced', 'admin-text', 'site-reviews'));
138
        }
139
    }
140
141
    /**
142
     * @return void
143
     */
144
    protected function register_shortcode_options($options, $tabKey, $tabLabel)
145
    {
146
        $this->start_controls_section($tabKey, [
147
            'label' => $tabLabel,
148
            'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
149
        ]);
150
        foreach ($options as $key => $settings) {
151
            $this->add_control($key, $settings);
152
        }
153
        $this->end_controls_section();
154
    }
155
156
    protected function render()
157
    {
158
        $shortcode = $this->get_shortcode_instance()->build($this->get_settings_for_display());
159
        $shortcode = str_replace('class="glsr-fallback">', 'class="glsr-fallback" style="display:none;">', $shortcode);
160
        echo $shortcode;
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    protected function settings_advanced()
167
    {
168
        return [
169
            'shortcode_id' => [
170
                'label_block' => true,
171
                'label' => _x('Custom ID', 'admin-text', 'site-reviews'),
172
                'type' => \Elementor\Controls_Manager::TEXT,
173
            ],
174
            'shortcode_class' => [
175
                'description' => _x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
176
                'label_block' => true,
177
                'label' => _x('Additional CSS classes', 'admin-text', 'site-reviews'),
178
                'type' => \Elementor\Controls_Manager::TEXT,
179
            ],
180
        ];
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    protected function settings_basic()
187
    {
188
        return [];
189
    }
190
}
191