Passed
Push — master ( 6b35fa...ccb079 )
by Paul
10:24 queued 05:13
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\Application;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode;
8
9
class SiteReviewsWidget extends Widget
10
{
11
    public function __construct()
12
    {
13
        $idBase = Application::ID.'_site-reviews';
14
        $name = __('Recent Reviews', 'site-reviews');
15
        $widgetOptions = [
16
            'class' => 'glsr-widget glsr-widget-site-reviews',
17
            'description' => __('Site Reviews: Display your recent reviews.', 'site-reviews'),
18
        ];
19
        parent::__construct($idBase, $name, $widgetOptions);
20
    }
21
22
    /**
23
     * @param array $instance
24
     * @return void
25
     */
26
    public function form($instance)
27
    {
28
        $this->widgetArgs = glsr(SiteReviewsShortcode::class)->normalizeAtts($instance);
29
        $terms = glsr(Database::class)->getTerms();
30
        $this->renderField('text', [
31
            'class' => 'widefat',
32
            'label' => __('Title', 'site-reviews'),
33
            'name' => 'title',
34
        ]);
35
        $this->renderField('number', [
36
            'class' => 'small-text',
37
            'default' => 10,
38
            'label' => __('How many reviews would you like to display?', 'site-reviews'),
39
            'max' => 100,
40
            'name' => 'display',
41
        ]);
42
        $this->renderField('select', [
43
            'label' => __('What is the minimum rating to display?', 'site-reviews'),
44
            'name' => 'rating',
45
            'options' => [
46
                '5' => sprintf(_n('%s star', '%s stars', 5, 'site-reviews'), 5),
47
                '4' => sprintf(_n('%s star', '%s stars', 4, 'site-reviews'), 4),
48
                '3' => sprintf(_n('%s star', '%s stars', 3, 'site-reviews'), 3),
49
                '2' => sprintf(_n('%s star', '%s stars', 2, 'site-reviews'), 2),
50
                '1' => sprintf(_n('%s star', '%s stars', 1, 'site-reviews'), 1),
51
            ],
52
        ]);
53
        if (count(glsr()->reviewTypes) > 1) {
54
            $this->renderField('select', [
55
                'class' => 'widefat',
56
                'label' => __('Which type of review would you like to display?', 'site-reviews'),
57
                'name' => 'type',
58
                'options' => ['' => __('All Reviews', 'site-reviews')] + glsr()->reviewTypes,
59
            ]);
60
        }
61
        if (!empty($terms)) {
62
            $this->renderField('select', [
63
                'class' => 'widefat',
64
                'label' => __('Limit reviews to this category', 'site-reviews'),
65
                'name' => 'category',
66
                'options' => ['' => __('All Categories', 'site-reviews')] + $terms,
67
            ]);
68
        }
69
        $this->renderField('text', [
70
            'class' => 'widefat',
71
            'default' => '',
72
            '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>'),
73
            'label' => __('Limit reviews to those assigned to this page/post ID', 'site-reviews'),
74
            'name' => 'assigned_to',
75
        ]);
76
        $this->renderField('text', [
77
            'class' => 'widefat',
78
            'label' => __('Enter any custom CSS classes here', 'site-reviews'),
79
            'name' => 'class',
80
        ]);
81
        $this->renderField('checkbox', [
82
            'name' => 'hide',
83
            'options' => glsr(SiteReviewsShortcode::class)->getHideOptions(),
84
        ]);
85
    }
86
87
    /**
88
     * @param array $newInstance
89
     * @param array $oldInstance
90
     * @return array
91
     */
92
    public function update($newInstance, $oldInstance)
93
    {
94
        if (!is_numeric($newInstance['display'])) {
95
            $newInstance['display'] = 10;
96
        }
97
        $newInstance['display'] = min(50, max(0, intval($newInstance['display'])));
98
        return parent::update($newInstance, $oldInstance);
99
    }
100
101
    /**
102
     * @param array $args
103
     * @param array $instance
104
     * @return void
105
     */
106
    public function widget($args, $instance)
107
    {
108
        echo glsr(SiteReviewsShortcode::class)->build($instance, $args, 'widget');
109
    }
110
}
111