Passed
Push — develop ( 39ad60...0ca1de )
by Paul
14:09
created

BricksElement::styledClasses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Bricks;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
9
10
abstract class BricksElement extends \Bricks\Element
11
{
12
    use IntegrationShortcode;
13
14
    public $category;
15
    public $controls;
16
    public $control_groups;
17
    public $icon;
18
    public $name;
19
    public $nestable = false;
20
    public $scripts = ['GLSR_init'];
21
22
    public function __construct($element = null)
23
    {
24
        $this->category = glsr()->id;
25
        $this->icon = "ti-{$this->shortcodeInstance()->tag}";
26
        $this->name = $this->shortcodeInstance()->tag;
27
        parent::__construct($element);
28
    }
29
30
    public function add_actions()
31
    {
32
        add_action('wp_enqueue_scripts', function () {
33
            wp_add_inline_style('bricks-frontend', ".brxe-{$this->name} {width: 100%}");
34
        });
35
    }
36
37
    public function designConfig(): array
38
    {
39
        return [];
40
    }
41
42
    public function elementConfig(): array
43
    {
44
        return $this->shortcodeInstance()->settings();
45
    }
46
47
    public function get_keywords()
48
    {
49
        return ['review', 'reviews', 'site reviews'];
50
    }
51
52
    public function get_label()
53
    {
54
        return $this->shortcodeInstance()->name;
55
    }
56
57
    public function load()
58
    {
59
        parent::load();
60
        $this->control_groups = glsr()->filterArray('bricks/element/control_groups', $this->control_groups, $this);
61
        $this->controls = glsr()->filterArray('bricks/element/controls', $this->controls, $this);
62
        $groups = array_map(fn ($args) => $args['group'] ?? '', $this->controls);
63
        $groups = array_filter($groups, fn ($value) => !str_starts_with($value, '_'));
64
        $groups = array_values(array_filter(array_unique($groups)));
65
        foreach ($this->control_groups as $key => $value) {
66
            if (str_starts_with($key, '_')) {
67
                continue;
68
            }
69
            if (in_array($key, $groups)) {
70
                continue;
71
            }
72
            unset($this->control_groups[$key]);
73
        }
74
    }
75
76
    public static function registerElement(): void
77
    {
78
        $reflection = new \ReflectionClass(static::class);
79
        $file = $reflection->getFileName();
80
        $className = $reflection->getName();
81
        \Bricks\Elements::register_element($file, '', $className);
82
    }
83
84
    public function render()
85
    {
86
        $buttonClasses = ['glsr-button', 'bricks-button'];
87
        if ($buttonStyle = Cast::toString($this->styledSetting('style_button_preset'))) {
88
            $buttonClasses[] = "bricks-background-{$buttonStyle}";
89
        }
90
        if ($buttonSize = Cast::toString($this->styledSetting('style_button_size'))) {
91
            $buttonClasses[] = $buttonSize;
92
        }
93
        $buttonClasses = implode(' ', $buttonClasses);
94
        $html = $this->shortcodeInstance()->build($this->settings, 'bricks');
95
        $html = str_replace('glsr-button', $buttonClasses, $html);
96
        echo "<{$this->get_tag()} {$this->render_attributes('_root')}>";
97
        echo $html;
98
        echo "</{$this->get_tag()}>";
99
    }
100
101
    public function set_control_groups()
102
    {
103
        $this->control_groups['general'] = [
104
            'tab' => 'content',
105
            'title' => esc_html_x('General', 'admin-text', 'site-reviews'),
106
        ];
107
        $this->control_groups['advanced'] = [
108
            'tab' => 'content',
109
            'title' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
110
        ];
111
        $this->control_groups['design'] = [
112
            'tab' => 'content',
113
            'title' => esc_html_x('Design', 'admin-text', 'site-reviews'),
114
        ];
115
    }
116
117
    public function set_controls()
118
    {
119
        $this->controls ??= [];
120
        $this->control_groups ??= [];
121
        $config = array_merge($this->elementConfig(), $this->designConfig());
122
        foreach ($config as $key => $args) {
123
            $args = wp_parse_args($args, [
124
                'group' => 'general',
125
                'tab' => 'content',
126
                'type' => 'text',
127
            ]);
128
            $method = Helper::buildMethodName('set', $args['type'], 'control');
129
            if (!method_exists($this, $method)) {
130
                $this->controls[$key] = $args;
131
                continue;
132
            }
133
            call_user_func([$this, $method], $key, $args);
134
        }
135
        if (count(glsr()->retrieveAs('array', 'review_types', [])) < 2) {
136
            unset($this->controls['type']);
137
        }
138
        if (isset($this->controls['_typography'])) {
139
            $this->controls['_typography']['exclude'] ??= [];
140
            $this->controls['_typography']['exclude'][] = 'text-align'; // because rerender doesn't work
141
        }
142
    }
143
144
    /**
145
     * @return mixed
146
     */
147
    public function styledSetting(string $path)
148
    {
149
        $value = Arr::get($this->settings, $path, '');
150
        $value = $value ?: Arr::get($this->theme_styles, $path, '');
151
        return $value;
152
    }
153
154
    protected function setCheckboxControl(string $key, array $args): void
155
    {
156
        if (empty($args['options'])) {
157
            $this->controls[$key] = $args;
158
            return;
159
        }
160
        foreach ($args['options'] as $value => $label) {
161
            $this->controls["{$key}_{$value}"] = [
162
                'default' => false,
163
                'group' => $args['group'],
164
                'label' => $label,
165
                'tab' => 'content',
166
                'type' => 'checkbox',
167
            ];
168
        }
169
    }
170
171
    protected function setNumberControl(string $key, array $args): void
172
    {
173
        if (isset($args['css'])) {
174
            $this->controls[$key] = $args;
175
            return;
176
        }
177
        $control = [
178
            // 'small' => true,
179
            'type' => 'slider',
180
            'units' => false,
181
        ];
182
        $this->controls[$key] = wp_parse_args($control, $args);
183
    }
184
185
    protected function setSelectControl(string $key, array $args): void
186
    {
187
        if (!in_array($key, ['assigned_posts', 'assigned_terms', 'assigned_users', 'author', 'post_id'])) {
188
            $this->controls[$key] = $args;
189
            return;
190
        }
191
        $control = [
192
            'type' => 'select',
193
            'searchable' => true,
194
            'optionsAjax' => ['action' => "bricks_glsr_{$key}"],
195
        ];
196
        if (in_array($key, ['assigned_posts', 'assigned_terms', 'assigned_users'])) {
197
            $control['multiple'] = true;
198
        }
199
        $this->controls[$key] = wp_parse_args($control, $args);
200
    }
201
}
202