Test Failed
Push — develop ( fe7dfd...f4a85b )
by Paul
08:21
created

Widget::widgetOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 1
b 0
f 0
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\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Modules\Html\WidgetBuilder;
10
11
abstract class Widget extends \WP_Widget
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $mapped = [ // @compat 4.0
17
        'assign_to' => 'assigned_posts',
18
        'assigned_to' => 'assigned_posts',
19
        'category' => 'assigned_terms',
20
        'per_page' => 'display',
21
        'user' => 'assigned_users',
22
    ];
23
24
    /**
25
     * @var array
26
     */
27
    protected $widgetArgs;
28
29
    public function __construct()
30
    {
31
        $className = (new \ReflectionClass($this))->getShortName();
32
        $className = str_replace('Widget', '', $className);
33
        $baseId = glsr()->prefix.Str::dashCase($className);
34
        parent::__construct($baseId, $this->shortcode()->name, [
0 ignored issues
show
Bug introduced by
Accessing name on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
35
            'description' => sprintf('%s: %s', glsr()->name, $this->shortcode()->description),
0 ignored issues
show
Bug introduced by
Accessing description on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
36
            'name' => $this->shortcode()->name,
37
            'show_instance_in_rest' => true,
38
        ]);
39
    }
40
41
    /**
42
     * @param array $args
43
     * @param array $instance
44
     *
45
     * @return void
46
     */
47
    public function widget($args, $instance)
48
    {
49
        $shortcode = $this->shortcode();
50
        $args = $this->normalizeArgs($args);
51
        $html = $shortcode->build($instance, 'widget');
52
        $title = !empty($shortcode->args['title'])
53
            ? $args->before_title.$shortcode->args['title'].$args->after_title
54
            : '';
55
        echo $args->before_widget.$title.$html.$args->after_widget;
56
    }
57
58
    /**
59
     * @param string $key
60
     *
61
     * @return mixed
62
     */
63
    protected function mapped($key)
64
    {
65
        $key = Arr::get($this->mapped, $key, $key);
66
        return Arr::get($this->widgetArgs, $key);
67
    }
68
69
    /**
70
     * @param array|string $args
71
     */
72
    protected function normalizeArgs($args): Arguments
73
    {
74
        $args = wp_parse_args($args, [
75
            'before_widget' => '',
76
            'after_widget' => '',
77
            'before_title' => '<h2 class="glsr-title">',
78
            'after_title' => '</h2>',
79
        ]);
80
        $args = glsr()->filterArray('widget/args', $args, $this->shortcode()->shortcode);
81
        return glsr()->args($args);
82
    }
83
84
    protected function normalizeFieldAttributes(string $tag, array $args): array
85
    {
86
        if (empty($args['value'])) {
87
            $args['value'] = $this->mapped($args['name']);
88
        }
89
        if (empty($this->mapped('options')) && in_array($tag, ['checkbox', 'radio'])) {
90
            $args['checked'] = in_array($args['value'], (array) $this->mapped($args['name']));
91
        }
92
        $args['id'] = $this->get_field_id($args['name']);
93
        $args['name'] = $this->get_field_name($args['name']);
94
        return $args;
95
    }
96
97
    protected function renderField(string $tag, array $args = []): void
98
    {
99
        $args = $this->normalizeFieldAttributes($tag, $args);
100
        echo glsr(WidgetBuilder::class)->p([
101
            'text' => glsr(WidgetBuilder::class)->$tag($args),
102
        ]);
103
    }
104
105
    abstract protected function shortcode(): ShortcodeContract;
106
}
107