Test Failed
Push — develop ( 8b2a32...47c531 )
by Paul
08:13
created

FlatsomeShortcode   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 34
c 1
b 0
f 1
dl 0
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A uxShortcode() 0 3 1
A typeOptions() 0 12 2
A renderShortcode() 0 14 5
A register() 0 10 1
A hideOptions() 0 10 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Flatsome;
4
5
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
8
abstract class FlatsomeShortcode
9
{
10
    abstract public function options(): array;
11
12
    /**
13
     * Override the "add_ux_builder_shortcode" data with
14
     * the "ux_builder_shortcode_data_{$tag}" filter hook.
15
     */
16
    public function register(): void
17
    {
18
        add_shortcode($this->uxShortcode(), [$this, 'renderShortcode']);
19
        add_action('ux_builder_setup', function () {
20
            add_ux_builder_shortcode($this->uxShortcode(), [
1 ignored issue
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

20
            /** @scrutinizer ignore-call */ 
21
            add_ux_builder_shortcode($this->uxShortcode(), [
Loading history...
21
                'category' => glsr()->name,
22
                'name' => $this->name(),
23
                'options' => $this->options(),
24
                'thumbnail' => $this->icon(),
25
                'wrap' => false,
26
            ]);
27
        });
28
    }
29
30
    /**
31
     * @param array|string $atts
32
     */
33
    public function renderShortcode($atts = []): string
34
    {
35
        $args = wp_parse_args($atts);
36
        $hide = [];
37
        foreach ($atts as $key => $value) {
38
            if (str_starts_with((string) $key, 'hide_') && Cast::toBool($value)) {
39
                $hide[] = substr($key, 5);
40
            }
41
        }
42
        $args['hide'] = array_filter($hide);
43
        if (!empty($args['visibility'])) {
44
            $args['class'] = ($args['class'] ?? '').' '.$args['visibility'];
45
        }
46
        return $this->shortcode()->build($args, 'flatsome');
47
    }
48
49
    abstract protected function icon(): string;
50
51
    abstract protected function name(): string;
52
53
    protected function hideOptions(): array
54
    {
55
        $options = [];
56
        foreach ($this->shortcode()->getHideOptions() as $key => $label) {
57
            $options["hide_{$key}"] = [
58
                'heading' => $label,
59
                'type' => 'checkbox',
60
            ];
61
        }
62
        return $options;
63
    }
64
65
    abstract protected function shortcode(): ShortcodeContract;
66
67
    protected function typeOptions(): array
68
    {
69
        $types = glsr()->retrieveAs('array', 'review_types', []);
70
        if (2 > count($types)) {
71
            return [];
72
        }
73
        return [
74
            'type' => 'select',
75
            'heading' => esc_html_x('Limit Reviews by Type', 'admin-text', 'site-reviews'),
76
            'full_width' => true,
77
            'default' => 'local',
78
            'options' => $types,
79
        ];
80
    }
81
82
    protected function uxShortcode(): string
83
    {
84
        return "ux_{$this->shortcode()->shortcode}";
85
    }
86
}
87