Test Failed
Push — develop ( db5b9c...b55dd4 )
by Paul
17:52
created

Widget::widgetConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Widgets;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Database;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Modules\Html\WidgetBuilder;
11
use GeminiLabs\SiteReviews\Modules\Html\WidgetField;
12
13
abstract class Widget extends \WP_Widget
14
{
15
    public ShortcodeContract $shortcode;
16
17
    public function __construct()
18
    {
19
        $this->shortcode = $this->wShortcode();
20
        $baseId = glsr()->prefix.Str::dashCase($this->shortcode->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...
21
        parent::__construct($baseId, $this->shortcode->name, [
22
            'description' => sprintf('%s: %s', glsr()->name, $this->shortcode->description),
23
            'name' => $this->shortcode->name,
24
            'show_instance_in_rest' => true,
25
        ]);
26
    }
27
28
    /**
29
     * @param array $instance
30
     *
31
     * @return string
32
     */
33
    public function form($instance)
34
    {
35
        $instance = $this->normalizeInstance(wp_parse_args($instance));
36
        $notice = _x('This is a legacy widget with limited options, consider switching to the shortcode or block.', 'admin-text', 'site-reviews');
37
        echo glsr(WidgetBuilder::class)->div([
38
            'class' => 'notice notice-alt notice-warning inline',
39
            'style' => 'margin:1em 0;',
40
            'text' => glsr(WidgetBuilder::class)->p($notice),
41
        ]);
42
        $config = wp_parse_args($this->wConfig(), [
43
            'title' => [
44
                'label' => _x('Widget Title', 'admin-text', 'site-reviews'),
45
                'type' => 'text',
46
            ],
47
        ]);
48
        foreach ($config as $name => $args) {
49
            if ('type' === $name && empty($args['options'])) {
50
                continue;
51
            }
52
            $this->renderField($name, $args, $instance);
53
        }
54
        return '';
55
    }
56
57
    /**
58
     * @param array $oldInstance
59
     *
60
     * @return array
61
     */
62
    public function update($instance, $oldInstance)
63
    {
64
        return $this->normalizeInstance(wp_parse_args($instance));
65
    }
66
67
    /**
68
     * @param array $args
69
     * @param array $instance
70
     *
71
     * @return void
72
     */
73
    public function widget($args, $instance)
74
    {
75
        $args = $this->normalizeArgs(wp_parse_args($args));
76
        $html = $this->shortcode->build($instance, 'widget');
77
        $title = !empty($this->shortcode->args['title'])
78
            ? $args->before_title.$this->shortcode->args['title'].$args->after_title
79
            : '';
80
        echo $args->before_widget.$title.$html.$args->after_widget;
81
    }
82
83
    protected function fieldAssignedTermsOptions(): array
84
    {
85
        return Arr::prepend(
86
            glsr(Database::class)->terms(),
87
            esc_html_x('— Select —', 'admin-text', 'site-reviews'),
88
            ''
89
        );
90
    }
91
92
    protected function normalizeArgs(array $args): Arguments
93
    {
94
        $args = wp_parse_args($args, [
95
            'before_widget' => '',
96
            'after_widget' => '',
97
            'before_title' => '<h2 class="glsr-title">',
98
            'after_title' => '</h2>',
99
        ]);
100
        $args = glsr()->filterArray('widget/args', $args, $this->shortcode->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...
101
        return glsr()->args($args);
102
    }
103
104
    protected function normalizeInstance(array $instance): array
105
    {
106
        $atts = $this->shortcode->defaults()->unguardedMerge($instance);
107
        $pairs = array_fill_keys(array_keys($instance), '');
108
        return shortcode_atts($pairs, $atts);
109
    }
110
111
    protected function renderField(string $name, array $args, array $instance = []): void
112
    {
113
        if (isset($args['name']) && !isset($args['type'])) {
114
            $args['type'] = $name; // @todo remove in v8.0
115
        }
116
        $field = new WidgetField(wp_parse_args($args, compact('name')));
117
        if (!$field->isValid()) {
118
            return;
119
        }
120
        $value = Arr::get($instance, $field->original_name);
121
        if ('' !== $value) {
122
            $field->value = $value;
123
        }
124
        $field->id = $this->get_field_id($field->name);
125
        $field->name = $this->get_field_name($field->name);
126
        $field->render();
127
    }
128
129
    abstract protected function wConfig(): array;
130
131
    abstract protected function wShortcode(): ShortcodeContract;
132
}
133