Test Failed
Push — develop ( 6c74c3...4ba53a )
by Paul
08:25
created

ControlDefaults::finalizeSelect()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 27
rs 9.0111
cc 6
nc 7
nop 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Flatsome\Defaults;
4
5
use GeminiLabs\SiteReviews\Defaults\DefaultsAbstract;
6
use GeminiLabs\SiteReviews\Helper;
7
8
class ControlDefaults extends DefaultsAbstract
9
{
10
    /**
11
     * The keys that should be mapped to other keys.
12
     * Keys are mapped before the values are normalized and sanitized.
13
     * Note: Mapped keys should not be included in the defaults!
14
     */
15
    public array $mapped = [
16
        'label' => 'heading',
17
    ];
18
19
    /**
20
     * The values that should be sanitized.
21
     * This is done after $casts and before $enums.
22
     */
23
    public array $sanitize = [
24
        'description' => 'text', // Flatsome does not support HTML in descriptions
25
    ];
26
27
    protected function defaults(): array
28
    {
29
        return [
30
            'description' => '',
31
            'label' => '',
32
            'type' => 'text',
33
        ];
34
    }
35
36
    /**
37
     * Finalize provided values, this always runs last.
38
     */
39
    protected function finalize(array $values = []): array
40
    {
41
        $types = [
42
            'number' => 'slider',
43
            'text' => 'textfield',
44
        ];
45
        if (array_key_exists($values['type'], $types)) {
46
            $values['type'] = $types[$values['type']];
47
        }
48
        $values['full_width'] ??= true;
49
        $method = Helper::buildMethodName('finalize', $values['type']);
50
        if (method_exists($this, $method)) {
51
            return call_user_func([$this, $method], $values);
52
        }
53
        return $values;
54
    }
55
56
    protected function finalizeCheckbox(array $values = []): array
57
    {
58
        if (!empty($values['options'])) {
59
            $values['type'] = 'select';
60
            $values['config'] = [
61
                'multiple' => true,
62
                'options' => $values['options'],
63
                'placeholder' => $values['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews'),
64
                'sortable' => false,
65
            ];
66
            unset($values['options']);
67
        } elseif (!isset($values['options'])) {
68
            $values['type'] = 'radio-buttons';
69
            $values['options'] = [
70
                '' => ['title' => _x('No', 'admin-text', 'site-reviews')],
71
                'true' => ['title' => _x('Yes', 'admin-text', 'site-reviews')],
72
            ];
73
        }
74
        return $values;
75
    }
76
77
    protected function finalizeSelect(array $values = []): array
78
    {
79
        $values['config'] = [
80
            'sortable' => false,
81
        ];
82
        if (!empty($values['options'])) {
83
            if (!isset($values['options'][''])) {
84
                $placeholder = $values['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
85
                $values['options'] = ['' => $placeholder] + $values['options'];
86
            }
87
        } elseif (!isset($values['options'])) {
88
            if ('assigned_terms' === $values['name']) {
89
                $values['config']['termSelect'] = [
90
                    'taxonomies' => glsr()->taxonomy,
91
                ];
92
            } else {
93
                $values['config']['postSelect'] = glsr()->prefix.$values['name'];
94
            }
95
            if (str_starts_with($values['name'], 'assigned_')) {
96
                $values['multiple'] = true;
97
            }
98
            $values['config']['multiple'] = $values['multiple'] ?? false;
99
            $values['config']['placeholder'] = $values['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
100
            unset($values['multiple']);
101
            unset($values['placeholder']);
102
        }
103
        return $values;
104
    }
105
}
106