VcShortcode::vcTransformControl()   C
last analyzed

Complexity

Conditions 12
Paths 32

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 31
rs 6.9666
cc 12
nc 32
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\WPBakery;
4
5
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
6
use GeminiLabs\SiteReviews\Integrations\WPBakery\Defaults\ControlDefaults;
7
8
abstract class VcShortcode extends \WPBakeryShortCode
9
{
10
    /**
11
     * Override the "vc_map" data with the
12
     * "vc_element_settings_filter" ($settings, $shortcode) filter hook.
13
     */
14
    public static function vcRegister(array $args = []): void
15
    {
16
        vc_map(wp_parse_args($args, [
17
            'base' => static::vcShortcode()->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...
18
            'category' => glsr()->name,
19
            'description' => static::vcShortcode()->description,
20
            'icon' => static::vcShortcodeIcon(),
21
            'name' => static::vcShortcode()->name,
22
            'params' => static::vcShortcodeSettings(),
23
            'php_class_name' => static::class,
24
            'show_settings_on_create' => false,
25
        ]));
26
    }
27
28
    public static function vcShortcode(): ShortcodeContract
29
    {
30
        return glsr(static::vcShortcodeClass());
31
    }
32
33
    abstract public static function vcShortcodeClass(): string;
34
35
    abstract public static function vcShortcodeIcon(): string;
36
37
    /**
38
     * Override attributes for a setting with the 
39
     * "vc_mapper_attribute" ($setting, $shortcode) filter hook.
40
     */
41
    public static function vcShortcodeSettings(): array
42
    {
43
        $groups = [
44
            'general' => [
45
                'controls' => [],
46
                'title' => _x('General', 'admin-text', 'site-reviews'),
47
            ],
48
            'design' => [
49
                'controls' => [],
50
                'title' => _x('Design', 'admin-text', 'site-reviews'),
51
            ],
52
            'advanced' => [
53
                'controls' => [],
54
                'title' => _x('Advanced', 'admin-text', 'site-reviews'),
55
            ],
56
        ];
57
        $config = array_merge(static::vcSettingsConfig(), static::vcStyleConfig());
58
        foreach ($config as $name => $args) {
59
            $control = static::vcTransformControl($name, $args);
60
            if ('dropdown' === $control['type'] && empty($control['value'])) {
61
                continue;
62
            }
63
            $group =  $groups[$control['group']]['title'];
64
            $groups[$control['group']]['controls'][$name] = wp_parse_args(compact('group'), $control);
65
        }
66
        $controls = array_merge(...array_column($groups, 'controls'));
67
        $controls = array_values(array_filter($controls));
68
        return $controls;
69
    }
70
71
    protected static function vcSettingsConfig(): array
72
    {
73
        return static::vcShortcode()->settings();
74
    }
75
76
    protected static function vcStyleConfig(): array
77
    {
78
        return [];
79
    }
80
81
    protected static function vcTransformControl(string $name, array $args): array
82
    {
83
        $control = glsr(ControlDefaults::class)->merge(
84
            wp_parse_args(compact('name'), $args)
85
        );
86
        if ('hide' === $name) {
87
            $control['heading'] = _x('Hide', 'admin-text', 'site-reviews');
88
        }
89
        // Handle dropdown type: add placeholder as first option if present
90
        if ('dropdown' === $control['type']
91
            && !empty($control['options'])
92
            && !isset($control['options'][''])
93
            && !empty($control['placeholder'])) {
94
            $control['options'] = ['' => $control['placeholder']] + $control['options'];
95
        }
96
        // Set value to options for dropdown/checkbox if value is not set
97
        if (isset($control['options']) && !isset($control['value'])) {
98
            $control['value'] = $control['options'];
99
        }
100
        // Flip value array for dropdown/checkbox if options exist
101
        if (in_array($control['type'], ['checkbox', 'dropdown']) && !empty($control['options'])) {
102
            $control['value'] = array_flip($control['value']);
103
        }
104
        // Set default value for checkbox if not set
105
        if ('checkbox' === $control['type'] && !isset($control['value'])) {
106
            $control['value'] = [
107
                esc_html_x('Yes', 'admin-text', 'site-reviews') => 'true',
108
            ];
109
        }
110
        unset($control['options']);
111
        return $control;
112
    }
113
}
114