Passed
Push — develop ( 0ca1de...49b51d )
by Paul
18:10
created

BricksElement::setButtonClass()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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