Passed
Push — master ( ccb079...7906b4 )
by Paul
04:39
created

SiteReviewsWidget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Widgets;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode;
7
8
class SiteReviewsWidget extends Widget
9
{
10
    /**
11
     * @param array $instance
12
     * @return void
13
     */
14
    public function form($instance)
15
    {
16
        $this->widgetArgs = $this->shortcode()->normalizeAtts($instance);
17
        $terms = glsr(Database::class)->getTerms();
18
        $this->renderField('text', [
19
            'class' => 'widefat',
20
            'label' => __('Title', 'site-reviews'),
21
            'name' => 'title',
22
        ]);
23
        $this->renderField('number', [
24
            'class' => 'small-text',
25
            'default' => 10,
26
            'label' => __('How many reviews would you like to display?', 'site-reviews'),
27
            'max' => 100,
28
            'name' => 'display',
29
        ]);
30
        $this->renderField('select', [
31
            'label' => __('What is the minimum rating to display?', 'site-reviews'),
32
            'name' => 'rating',
33
            'options' => [
34
                '0' => sprintf(_n('%s star', '%s stars', 0, 'site-reviews'), 0),
35
                '1' => sprintf(_n('%s star', '%s stars', 1, 'site-reviews'), 1),
36
                '2' => sprintf(_n('%s star', '%s stars', 2, 'site-reviews'), 2),
37
                '3' => sprintf(_n('%s star', '%s stars', 3, 'site-reviews'), 3),
38
                '4' => sprintf(_n('%s star', '%s stars', 4, 'site-reviews'), 4),
39
                '5' => sprintf(_n('%s star', '%s stars', 5, 'site-reviews'), 5),
40
            ],
41
        ]);
42
        if (count(glsr()->reviewTypes) > 1) {
43
            $this->renderField('select', [
44
                'class' => 'widefat',
45
                'label' => __('Which type of review would you like to display?', 'site-reviews'),
46
                'name' => 'type',
47
                'options' => ['' => __('All Reviews', 'site-reviews')] + glsr()->reviewTypes,
48
            ]);
49
        }
50
        if (!empty($terms)) {
51
            $this->renderField('select', [
52
                'class' => 'widefat',
53
                'label' => __('Limit reviews to this category', 'site-reviews'),
54
                'name' => 'category',
55
                'options' => ['' => __('All Categories', 'site-reviews')] + $terms,
56
            ]);
57
        }
58
        $this->renderField('text', [
59
            'class' => 'widefat',
60
            'default' => '',
61
            'description' => sprintf(__("Separate multiple ID's with a comma. You may also enter %s to automatically represent the current page/post ID.", 'site-reviews'), '<code>post_id</code>'),
62
            'label' => __('Limit reviews to those assigned to this page/post ID', 'site-reviews'),
63
            'name' => 'assigned_to',
64
        ]);
65
        $this->renderField('text', [
66
            'class' => 'widefat',
67
            'label' => __('Enter any custom CSS classes here', 'site-reviews'),
68
            'name' => 'class',
69
        ]);
70
        $this->renderField('checkbox', [
71
            'name' => 'hide',
72
            'options' => $this->shortcode()->getHideOptions(),
73
        ]);
74
    }
75
76
    /**
77
     * @param array $newInstance
78
     * @param array $oldInstance
79
     * @return array
80
     */
81
    public function update($newInstance, $oldInstance)
82
    {
83
        if (!is_numeric($newInstance['display'])) {
84
            $newInstance['display'] = 10;
85
        }
86
        $newInstance['display'] = min(50, max(0, intval($newInstance['display'])));
87
        return parent::update($newInstance, $oldInstance);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function shortcode()
94
    {
95
        return glsr(SiteReviewsShortcode::class);
96
    }
97
}
98