Test Failed
Push — develop ( 11959d...0f24e8 )
by Paul
09:06
created

ControlDefaults::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Elementor\Defaults;
4
5
use Elementor\Controls_Manager;
6
use GeminiLabs\SiteReviews\Defaults\DefaultsAbstract;
7
8
class ControlDefaults extends DefaultsAbstract
9
{
10
    protected function defaults(): array
11
    {
12
        return [
13
            'description' => '',
14
            'label' => '',
15
            'name' => '',
16
            'type' => 'text',
17
        ];
18
    }
19
20
    /**
21
     * Normalize provided values, this always runs first.
22
     *
23
     * Elementor throws a JS error when removing a widget from the page if it
24
     * has a control with "id" as the name. To fix this, we need to transform
25
     * "id" to "shortcode_id" and "class" to "shortcode_class" (just in case).
26
     */
27
    protected function normalize(array $values = []): array
28
    {
29
        $name = $values['name'] ?? '';
30
        if (in_array($name, ['class', 'id'])) {
31
            $values['name'] = "shortcode_{$name}";
32
        }
33
        return $values;
34
    }
35
36
    /**
37
     * Finalize provided values, this always runs last.
38
     */
39
    protected function finalize(array $values = []): array
40
    {
41
        $types = [
42
            'checkbox' => Controls_Manager::SWITCHER,
43
            'number' => Controls_Manager::NUMBER,
44
            'radio' => Controls_Manager::CHOOSE,
45
            'select' => Controls_Manager::SELECT2,
46
            'text' => Controls_Manager::TEXT,
47
            'textarea' => Controls_Manager::TEXTAREA,
48
        ];
49
        if (array_key_exists($values['type'], $types)) {
50
            $values['type'] = $types[$values['type']];
51
        }
52
        if (Controls_Manager::SWITCHER === $values['type'] && !empty($values['options'])) {
53
            $values['type'] = 'multi_switcher';
54
        }
55
        if (!in_array($values['type'], [
56
            Controls_Manager::CHOOSE,
57
            Controls_Manager::COLOR,
58
            Controls_Manager::NUMBER,
59
            Controls_Manager::SWITCHER,
60
        ]) && !isset($values['label_block'])) {
61
            $values['label_block'] = true;
62
        }
63
        if (Controls_Manager::SELECT2 === $values['type'] && !empty($values['placeholder'])) {
64
            $values['select2options'] ??= [];
65
            $values['select2options']['placeholder'] = $values['placeholder'];
66
        }
67
        if (Controls_Manager::SELECT2 === $values['type'] && !isset($values['options'])) {
68
            $values['type'] = 'select2_ajax';
69
        }
70
        return $values;
71
    }
72
}
73