BricksElement   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
eloc 95
c 1
b 0
f 0
dl 0
loc 189
rs 9.84

16 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 4
A registerElement() 0 6 1
A set_control_groups() 0 11 2
A __construct() 0 6 1
A get_label() 0 3 1
A add_actions() 0 4 1
A elementConfig() 0 3 1
A render() 0 15 3
A get_keywords() 0 3 1
A set_controls() 0 20 4
A designConfig() 0 3 1
A setNumberControl() 0 12 2
A styledSetting() 0 5 2
A setCheckboxControl() 0 13 3
A styledClasses() 0 6 2
A setSelectControl() 0 15 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Bricks;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
8
9
abstract class BricksElement extends \Bricks\Element
0 ignored issues
show
Bug introduced by
The type Bricks\Element was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
{
11
    use IntegrationShortcode;
12
13
    public $nestable = false;
14
    public $scripts = ['GLSR_init'];
15
16
    public function __construct($element = null)
17
    {
18
        $this->category = glsr()->id;
0 ignored issues
show
Bug Best Practice introduced by
The property category does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->icon = "ti-{$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...
Bug Best Practice introduced by
The property icon does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->name = $this->shortcodeInstance()->tag;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        parent::__construct($element);
22
    }
23
24
    public function add_actions()
25
    {
26
        add_action('wp_enqueue_scripts', function () {
27
            wp_add_inline_style('bricks-frontend', ".brxe-{$this->name} {width: 100%}");
28
        });
29
    }
30
31
    public function designConfig(): array
32
    {
33
        return [];
34
    }
35
36
    public function elementConfig(): array
37
    {
38
        return $this->shortcodeInstance()->settings();
39
    }
40
41
    public function get_keywords()
42
    {
43
        return ['review', 'reviews', 'site reviews'];
44
    }
45
46
    public function get_label()
47
    {
48
        return $this->shortcodeInstance()->name;
49
    }
50
51
    public function load()
52
    {
53
        parent::load();
54
        $groups = array_map(fn ($args) => $args['group'] ?? '', $this->controls);
55
        $groups = array_filter($groups, fn ($value) => !str_starts_with($value, '_'));
56
        $groups = array_values(array_filter(array_unique($groups)));
57
        foreach ($this->control_groups as $key => $value) {
58
            if (str_starts_with($key, '_')) {
59
                continue;
60
            }
61
            if (in_array($key, $groups)) {
62
                continue;
63
            }
64
            unset($this->control_groups[$key]);
65
        }
66
    }
67
68
    public static function registerElement(): void
69
    {
70
        $reflection = new \ReflectionClass(static::class);
71
        $file = $reflection->getFileName();
72
        $className = $reflection->getName();
73
        \Bricks\Elements::register_element($file, '', $className);
0 ignored issues
show
Bug introduced by
The type Bricks\Elements was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
    }
75
76
    public function render()
77
    {
78
        $buttonClasses = ['bricks-button'];
79
        if ($buttonStyle = $this->styledSetting('style_button_preset')) {
80
            $buttonClasses[] = "bricks-background-{$buttonStyle}";
81
        }
82
        if ($buttonSize = $this->styledSetting('style_button_size')) {
83
            $buttonClasses[] = $buttonSize;
84
        }
85
        $buttonClasses = implode(' ', $buttonClasses);
86
        $html = $this->shortcodeInstance()->build($this->settings, 'bricks');
87
        $html = str_replace('glsr-button', "glsr-button {$buttonClasses}", $html);
88
        echo "<{$this->get_tag()} {$this->render_attributes('_root')}>";
89
        echo $html;
90
        echo "</{$this->get_tag()}>";
91
    }
92
93
    /**
94
     * @see https://academy.bricksbuilder.io/article/filter-bricks-elements-element_name-control_groups/
95
     */
96
    public function set_control_groups()
97
    {
98
        $groups = [ // order is intentional
99
            'general' => esc_html_x('General', 'admin-text', 'site-reviews'),
100
            'design' => esc_html_x('Design', 'admin-text', 'site-reviews'),
101
            'advanced' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
102
        ];
103
        foreach ($groups as $key => $title) {
104
            $this->control_groups[$key] = [
0 ignored issues
show
Bug Best Practice introduced by
The property control_groups does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
105
                'tab' => 'content',
106
                'title' => $title,
107
            ];
108
        }
109
    }
110
111
    /**
112
     * @see https://academy.bricksbuilder.io/article/filter-bricks-elements-element_name-controls
113
     */
114
    public function set_controls()
115
    {
116
        $this->controls ??= [];
117
        $this->control_groups ??= [];
118
        $config = array_merge($this->elementConfig(), $this->designConfig());
119
        foreach ($config as $key => $args) {
120
            $args = wp_parse_args($args, [
121
                'group' => 'general',
122
                'tab' => 'content',
123
                'type' => 'text',
124
            ]);
125
            $method = Helper::buildMethodName('set', $args['type'], 'control');
126
            if (!method_exists($this, $method)) {
127
                $this->controls[$key] = $args;
0 ignored issues
show
Bug Best Practice introduced by
The property controls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
128
                continue;
129
            }
130
            call_user_func([$this, $method], $key, $args);
131
        }
132
        if (count(glsr()->retrieveAs('array', 'review_types', [])) < 2) {
133
            unset($this->controls['type']);
134
        }
135
    }
136
137
    public function styledClasses(array $classes = []): array
138
    {
139
        if ($align = $this->styledSetting('style_align')) {
140
            $classes[] = "items-justified-{$align}";
141
        }
142
        return $classes;
143
    }
144
145
    public function styledSetting(string $key): string
146
    {
147
        $value = $this->settings[$key] ?? '';
148
        $value = $value ?: $this->theme_styles[$key] ?? '';
149
        return Cast::toString($value);
150
    }
151
152
    protected function setCheckboxControl(string $key, array $args): void
153
    {
154
        if (empty($args['options'])) {
155
            $this->controls[$key] = $args;
0 ignored issues
show
Bug Best Practice introduced by
The property controls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
156
            return;
157
        }
158
        foreach ($args['options'] as $value => $label) {
159
            $this->controls["{$key}_{$value}"] = [
160
                'default' => false,
161
                'group' => $args['group'],
162
                'label' => $label,
163
                'tab' => 'content',
164
                'type' => 'checkbox',
165
            ];
166
        }
167
    }
168
169
    protected function setNumberControl(string $key, array $args): void
170
    {
171
        if (isset($args['css'])) {
172
            $this->controls[$key] = $args;
0 ignored issues
show
Bug Best Practice introduced by
The property controls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
173
            return;
174
        }
175
        $control = [
176
            // 'small' => true,
177
            'type' => 'slider',
178
            'units' => false,
179
        ];
180
        $this->controls[$key] = wp_parse_args($control, $args);
181
    }
182
183
    protected function setSelectControl(string $key, array $args): void
184
    {
185
        if (!in_array($key, ['assigned_posts', 'assigned_terms', 'assigned_users', 'author', 'post_id'])) {
186
            $this->controls[$key] = $args;
0 ignored issues
show
Bug Best Practice introduced by
The property controls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
187
            return;
188
        }
189
        $control = [
190
            'type' => 'select',
191
            'searchable' => true,
192
            'optionsAjax' => ['action' => "bricks_glsr_{$key}"],
193
        ];
194
        if (in_array($key, ['assigned_posts', 'assigned_terms', 'assigned_users'])) {
195
            $control['multiple'] = true;
196
        }
197
        $this->controls[$key] = wp_parse_args($control, $args);
198
    }
199
}
200