Test Failed
Push — develop ( c0d6f9...325b33 )
by Paul
08:37
created

BricksElement   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 77
c 1
b 0
f 0
dl 0
loc 158
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 4
A registerElement() 0 7 1
A set_control_groups() 0 12 2
A __construct() 0 7 1
A setNumberControl() 0 8 1
A get_label() 0 3 1
A setCheckboxControl() 0 13 3
A elementConfig() 0 3 1
A render() 0 5 1
A get_keywords() 0 3 1
A set_controls() 0 22 5
A setSelectControl() 0 15 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Bricks;
4
5
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
6
use GeminiLabs\SiteReviews\Helper;
7
8
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...
9
{
10
    public $nestable = false;
11
    public $scripts = ['GLSR_init'];
12
13
    /**
14
     * @var ShortcodeContract
15
     */
16
    protected $elShortcode;
17
18
    public function __construct($element = null)
19
    {
20
        $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...
21
        $this->elShortcode = static::shortcode();
22
        $this->icon = "ti-{$this->elShortcode->tag}";
0 ignored issues
show
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...
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...
23
        $this->name = $this->elShortcode->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...
24
        parent::__construct($element);
25
    }
26
27
    public function elementConfig(): array
28
    {
29
        return $this->elShortcode->settings();
30
    }
31
32
    public function get_keywords()
33
    {
34
        return ['review', 'reviews', 'site reviews'];
35
    }
36
37
    public function get_label()
38
    {
39
        return $this->elShortcode->name;
40
    }
41
42
    public function load()
43
    {
44
        parent::load();
45
        $groups = array_map(fn ($args) => $args['group'] ?? '', $this->controls);
46
        $groups = array_filter($groups, fn ($value) => !str_starts_with($value, '_'));
47
        $groups = array_values(array_filter(array_unique($groups)));
48
        foreach ($this->control_groups as $key => $value) {
49
            if (str_starts_with($key, '_')) {
50
                continue;
51
            }
52
            if (in_array($key, $groups)) {
53
                continue;
54
            }
55
            unset($this->control_groups[$key]);
56
        }
57
    }
58
59
    public static function registerElement(): void
60
    {
61
        $reflection = new \ReflectionClass(static::class);
62
        $file = $reflection->getFileName();
63
        $name = static::shortcode()->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...
64
        $className = $reflection->getName();
65
        \Bricks\Elements::register_element($file, $name, $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...
66
    }
67
68
    public function render()
69
    {
70
        echo "<{$this->get_tag()} {$this->render_attributes('_root')}>";
71
        echo $this->elShortcode->build($this->settings, 'bricks');
72
        echo "</{$this->get_tag()}>";
73
    }
74
75
    /**
76
     * @see https://academy.bricksbuilder.io/article/filter-bricks-elements-element_name-control_groups/
77
     */
78
    public function set_control_groups()
79
    {
80
        $groups = [ // order is intentional
81
            'display' => esc_html_x('Display', 'admin-text', 'site-reviews'),
82
            'hide' => esc_html_x('Hide', 'admin-text', 'site-reviews'),
83
            'schema' => esc_html_x('Schema', 'admin-text', 'site-reviews'),
84
            'advanced' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
85
        ];
86
        foreach ($groups as $key => $title) {
87
            $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...
88
                'tab' => 'content',
89
                'title' => $title,
90
            ];
91
        }
92
    }
93
94
    /**
95
     * @see https://academy.bricksbuilder.io/article/filter-bricks-elements-element_name-controls
96
     */
97
    public function set_controls()
98
    {
99
        foreach ($this->elementConfig() as $key => $args) {
100
            $args = wp_parse_args($args, [
101
                'group' => '',
102
                // 'hasDynamicData' => false,
103
                // 'hasVariables' => true,
104
                'tab' => 'content',
105
                'type' => 'text',
106
            ]);
107
            if (!array_key_exists($args['group'], $this->control_groups)) {
108
                $args['group'] = '';
109
            }
110
            $method = Helper::buildMethodName('set', $args['type'], 'control');
111
            if (!method_exists($this, $method)) {
112
                $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...
113
                continue;
114
            }
115
            call_user_func([$this, $method], $key, $args);
116
        }
117
        if (count(glsr()->retrieveAs('array', 'review_types', [])) < 2) {
118
            unset($this->controls['type']);
119
        }
120
    }
121
122
    abstract public static function shortcode(): ShortcodeContract;
123
124
    protected function setCheckboxControl(string $key, array $args): void
125
    {
126
        if (empty($args['options'])) {
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
            return;
129
        }
130
        foreach ($args['options'] as $value => $label) {
131
            $this->controls["{$key}_{$value}"] = [
132
                'default' => false,
133
                'group' => $args['group'],
134
                'label' => $label,
135
                'tab' => 'content',
136
                'type' => 'checkbox',
137
            ];
138
        }
139
    }
140
141
    protected function setNumberControl(string $key, array $args): void
142
    {
143
        $control = [
144
            // 'small' => true,
145
            'type' => 'slider',
146
            'units' => false,
147
        ];
148
        $this->controls[$key] = wp_parse_args($control, $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...
149
    }
150
151
    protected function setSelectControl(string $key, array $args): void
152
    {
153
        if (!in_array($key, ['assigned_posts', 'assigned_terms', 'assigned_users', 'post_id'])) {
154
            $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...
155
            return;
156
        }
157
        $control = [
158
            'type' => 'select',
159
            'searchable' => true,
160
            'optionsAjax' => ['action' => "bricks_glsr_{$key}"],
161
        ];
162
        if ('post_id' !== $key) {
163
            $control['multiple'] = true;
164
        }
165
        $this->controls[$key] = wp_parse_args($control, $args);
166
    }
167
}
168