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

FlatsomeShortcode::transformControl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Flatsome;
4
5
use GeminiLabs\SiteReviews\Integrations\Flatsome\Defaults\ControlDefaults;
6
use GeminiLabs\SiteReviews\Integrations\IntegrationShortcode;
7
8
abstract class FlatsomeShortcode
9
{
10
    use IntegrationShortcode;
11
12
    abstract public function icon(): string;
13
14
    public function options(): array
15
    {
16
        $controls = array_merge(
17
            $this->settingsConfig(),
18
            $this->styleConfig(),
19
            $this->globalConfig(),
20
        );
21
        $groups = [ // order is intentional
22
            'design' => esc_html_x('Design', 'admin-text', 'site-reviews'),
23
            'general' => esc_html_x('General', 'admin-text', 'site-reviews'),
24
            'schema' => esc_html_x('Schema', 'admin-text', 'site-reviews'),
25
            'display' => esc_html_x('Display', 'admin-text', 'site-reviews'),
26
            'filters' => esc_html_x('Filters', 'admin-text', 'site-reviews'),
27
            'hide' => esc_html_x('Hide', 'admin-text', 'site-reviews'),
28
            'text' => esc_html_x('Text', 'admin-text', 'site-reviews'),
29
            'advanced' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
30
        ];
31
        $options = [];
32
        foreach ($controls as $name => $args) {
33
            $control = $this->transformControl($name, $args);
34
            $group = $control['group'] ?? 'general';
35
            $groupHeading = $groups[$group] ?? ucfirst($group);
36
            if ('select' === $control['type'] && isset($control['options']) && empty($control['options'])) {
37
                continue;
38
            }
39
            if (!array_key_exists($group, $options)) {
40
                $options[$group] = [
41
                    'collapsed' => true,
42
                    'heading' => $groupHeading,
43
                    'options' => [],
44
                    'type' => 'group',
45
                ];
46
            }
47
            $options[$group]['options'][$control['name']] = $control;
48
        }
49
        $keyOrder = array_keys($groups); // order results by $groups order
50
        uksort($options, fn ($a, $b) => array_search($a, $keyOrder) <=> array_search($b, $keyOrder));
51
        return $options;
52
    }
53
54
    /**
55
     * Override the "add_ux_builder_shortcode" data with
56
     * the "ux_builder_shortcode_data_{$tag}" filter hook.
57
     */
58
    public function register(): void
59
    {
60
        add_action('ux_builder_setup', function () {
61
            add_ux_builder_shortcode($this->shortcodeInstance()->tag, [
0 ignored issues
show
Bug introduced by
The function add_ux_builder_shortcode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            add_ux_builder_shortcode($this->shortcodeInstance()->tag, [
Loading history...
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...
62
                'category' => glsr()->name,
63
                'name' => $this->shortcodeInstance()->name,
64
                'options' => $this->options(),
65
                'thumbnail' => $this->icon(),
66
                'wrap' => false,
67
            ]);
68
        });
69
    }
70
71
    /**
72
     * Disabled visibility for now because Flatsome is simply a fancy shortcode
73
     * wrapper and it doesn't look possible to intercept the shortcode render
74
     * as from Flatsome.
75
     */
76
    protected function globalConfig(): array
77
    {
78
        $global = [];
79
        // $visibility = get_template_directory().'/inc/builder/shortcodes/commons/visibility.php';
80
        // if (file_exists($visibility)) {
81
        //     $global['visibility'] = require $visibility;
82
        //     $global['visibility']['group'] = 'advanced';
83
        // }
84
        return $global;
85
    }
86
87
    protected function settingsConfig(): array
88
    {
89
        return $this->shortcodeInstance()->settings();
90
    }
91
92
    protected function styleConfig(): array
93
    {
94
        return [];
95
    }
96
97
    protected function transformControl(string $name, array $args): array
98
    {
99
        $control = glsr(ControlDefaults::class)->merge(
100
            wp_parse_args(compact('name'), $args)
101
        );
102
        if ('hide' === $name) {
103
            $control['description'] = esc_html_x('Flatsome does not support multiple checkboxes here so use the dropdown to select fields that you want to hide.', 'admin-text', 'site-reviews');
104
        }
105
        return $control;
106
    }
107
}
108