Test Failed
Push — develop ( 1707fa...c6b569 )
by Paul
07: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 function __construct()
16
    {
17
        $className = (new \ReflectionClass($this))->getShortName();
18
        $className = str_replace('Widget', '', $className);
19
        $baseId = glsr()->prefix.Str::dashCase($className);
20
        parent::__construct($baseId, $this->shortcode()->name, [
21
            'description' => sprintf('%s: %s', glsr()->name, $this->shortcode()->description),
22
            'name' => $this->shortcode()->name,
23
            'show_instance_in_rest' => true,
24
        ]);
25
    }
26
27
    /**
28
     * @param array $instance
29
     *
30
     * @return string
31
     */
32
    public function form($instance)
33
    {
34
        $instance = $this->normalizeInstance($instance);
35
        $notice = _x('This is a legacy widget with limited options, consider switching to the shortcode or block.', 'admin-text', 'site-reviews');
36
        echo glsr(WidgetBuilder::class)->div([
37
            'class' => 'notice notice-alt notice-warning inline',
38
            'style' => 'margin:1em 0;',
39
            'text' => glsr(WidgetBuilder::class)->p($notice),
40
        ]);
41
        $config = wp_parse_args($this->widgetConfig(), [
42
            'title' => [
43
                'label' => _x('Widget Title', 'admin-text', 'site-reviews'),
44
                'type' => 'text',
45
            ],
46
        ]);
47
        foreach ($config as $name => $args) {
48
            if ('type' === $name && empty($args['options'])) {
49
                continue;
50
            }
51
            $this->renderField($name, $args, $instance);
52
        }
53
        return '';
54
    }
55
56
    /**
57
     * @param array $oldInstance
58
     *
59
     * @return array
60
     */
61
    public function update($instance, $oldInstance)
62
    {
63
        return $this->normalizeInstance($instance);
64
    }
65
66
    /**
67
     * @param array $args
68
     * @param array $instance
69
     *
70
     * @return void
71
     */
72
    public function widget($args, $instance)
73
    {
74
        $shortcode = $this->shortcode();
75
        $args = $this->normalizeArgs($args);
76
        $html = $shortcode->build($instance, 'widget');
77
        $title = !empty($shortcode->args['title'])
78
            ? $args->before_title.$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 fieldTypeOptions(): array
93
    {
94
        $types = glsr()->retrieveAs('array', 'review_types');
95
        if (count($types) > 1) {
96
            return $types;
97
        }
98
        return [];
99
    }
100
101
    /**
102
     * @param array|string $args
103
     */
104
    protected function normalizeArgs($args): Arguments
105
    {
106
        $args = wp_parse_args($args, [
107
            'before_widget' => '',
108
            'after_widget' => '',
109
            'before_title' => '<h2 class="glsr-title">',
110
            'after_title' => '</h2>',
111
        ]);
112
        $args = glsr()->filterArray('widget/args', $args, $this->shortcode()->shortcode);
113
        return glsr()->args($args);
114
    }
115
116
    protected function normalizeInstance(array $instance): array
117
    {
118
        $pairs = array_fill_keys(array_keys($instance), '');
119
        $title = $instance['title'] ?? '';
120
        $instance = $this->shortcode()->defaults()->merge($instance);
0 ignored issues
show
Bug introduced by
The method defaults() does not exist on GeminiLabs\SiteReviews\Contracts\ShortcodeContract. Since it exists in all sub-types, consider adding an abstract or default implementation to GeminiLabs\SiteReviews\Contracts\ShortcodeContract. ( Ignorable by Annotation )

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

120
        $instance = $this->shortcode()->/** @scrutinizer ignore-call */ defaults()->merge($instance);
Loading history...
121
        $instance['title'] = sanitize_text_field($title);
122
        return shortcode_atts($pairs, $instance);
123
    }
124
125
    protected function renderField($name, array $args, array $instance = []): void
126
    {
127
        if (isset($args['name']) && !isset($args['type'])) {
128
            $args['type'] = $name; // @todo remove in v8.0
129
        }
130
        $field = new WidgetField(wp_parse_args($args, compact('name')));
131
        if (!$field->isValid()) {
132
            return;
133
        }
134
        $value = Arr::get($instance, $field->original_name);
135
        if ('' !== $value) {
136
            $field->value = $value;
137
        }
138
        $field->id = $this->get_field_id($field->name);
139
        $field->name = $this->get_field_name($field->name);
140
        $field->render();
141
    }
142
143
    abstract protected function shortcode(): ShortcodeContract;
144
145
    protected function widgetConfig(): array
146
    {
147
        return [];
148
    }
149
}
150