1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Widgets; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
7
|
|
|
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode; |
8
|
|
|
|
9
|
|
|
class SiteReviewsWidget extends Widget |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param array $instance |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public function form($instance) |
16
|
|
|
{ |
17
|
|
|
$this->widgetArgs = $this->shortcode()->normalizeAtts($instance)->toArray(); |
18
|
|
|
$terms = glsr(Database::class)->terms(); |
19
|
|
|
$this->renderField('text', [ |
20
|
|
|
'label' => _x('Title', 'admin-text', 'site-reviews'), |
21
|
|
|
'name' => 'title', |
22
|
|
|
]); |
23
|
|
|
$this->renderField('number', [ |
24
|
|
|
'default' => 10, |
25
|
|
|
'label' => _x('How many reviews would you like to display?', 'admin-text', 'site-reviews'), |
26
|
|
|
'max' => 100, |
27
|
|
|
'name' => 'display', |
28
|
|
|
]); |
29
|
|
|
$this->renderField('select', [ |
30
|
|
|
'label' => _x('What is the minimum rating to display?', 'admin-text', 'site-reviews'), |
31
|
|
|
'name' => 'rating', |
32
|
|
|
'options' => [ |
33
|
|
|
'0' => esc_attr(sprintf(_nx('%s star', '%s stars', 0, 'admin-text', 'site-reviews'), 0)), |
34
|
|
|
'1' => esc_attr(sprintf(_nx('%s star', '%s stars', 1, 'admin-text', 'site-reviews'), 1)), |
35
|
|
|
'2' => esc_attr(sprintf(_nx('%s star', '%s stars', 2, 'admin-text', 'site-reviews'), 2)), |
36
|
|
|
'3' => esc_attr(sprintf(_nx('%s star', '%s stars', 3, 'admin-text', 'site-reviews'), 3)), |
37
|
|
|
'4' => esc_attr(sprintf(_nx('%s star', '%s stars', 4, 'admin-text', 'site-reviews'), 4)), |
38
|
|
|
'5' => esc_attr(sprintf(_nx('%s star', '%s stars', 5, 'admin-text', 'site-reviews'), 5)), |
39
|
|
|
], |
40
|
|
|
]); |
41
|
|
|
if (count($reviewTypes = glsr()->retrieveAs('array', 'review_types')) > 1) { |
42
|
|
|
$this->renderField('select', [ |
43
|
|
|
'label' => _x('Which type of review would you like to display?', 'admin-text', 'site-reviews'), |
44
|
|
|
'name' => 'type', |
45
|
|
|
'options' => Arr::prepend($reviewTypes, _x('All Reviews', 'admin-text', 'site-reviews'), ''), |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
if (!empty($terms)) { |
49
|
|
|
$this->renderField('select', [ |
50
|
|
|
'label' => _x('Limit reviews to this category', 'admin-text', 'site-reviews'), |
51
|
|
|
'name' => 'assigned_terms', |
52
|
|
|
'options' => Arr::prepend($terms, _x('Do not assign a category', 'admin-text', 'site-reviews'), ''), |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
$this->renderField('text', [ |
56
|
|
|
'default' => '', |
57
|
|
|
'description' => sprintf(_x("You may also enter %s to use the Post ID of the current page.", 'admin-text', 'site-reviews'), '<code>post_id</code>'), |
58
|
|
|
'label' => _x('Limit reviews to those assigned to a Post ID', 'admin-text', 'site-reviews'), |
59
|
|
|
'name' => 'assigned_posts', |
60
|
|
|
]); |
61
|
|
|
$this->renderField('text', [ |
62
|
|
|
'default' => '', |
63
|
|
|
'description' => sprintf(esc_html_x("You may also enter %s to use the ID of the logged-in user.", 'admin-text', 'site-reviews'), '<code>user_id</code>'), |
64
|
|
|
'label' => _x('Limit reviews to those assigned to a User ID', 'admin-text', 'site-reviews'), |
65
|
|
|
'name' => 'assigned_users', |
66
|
|
|
]); |
67
|
|
|
$this->renderField('text', [ |
68
|
|
|
'label' => _x('Enter any custom CSS classes here', 'admin-text', 'site-reviews'), |
69
|
|
|
'name' => 'class', |
70
|
|
|
]); |
71
|
|
|
$this->renderField('checkbox', [ |
72
|
|
|
'name' => 'hide', |
73
|
|
|
'options' => $this->shortcode()->getHideOptions(), |
74
|
|
|
]); |
75
|
|
|
return ''; // WP_Widget::form should return a string |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $newInstance |
80
|
|
|
* @param array $oldInstance |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function update($newInstance, $oldInstance) |
84
|
|
|
{ |
85
|
|
|
if (!is_numeric($newInstance['display'])) { |
86
|
|
|
$newInstance['display'] = 10; |
87
|
|
|
} |
88
|
|
|
$newInstance['display'] = min(50, max(0, intval($newInstance['display']))); |
89
|
|
|
return parent::update($newInstance, $oldInstance); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
protected function shortcode() |
96
|
|
|
{ |
97
|
|
|
return glsr(SiteReviewsShortcode::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
protected function widgetDescription() |
104
|
|
|
{ |
105
|
|
|
return _x('Site Reviews: Display your recent reviews.', 'admin-text', 'site-reviews'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
protected function widgetName() |
112
|
|
|
{ |
113
|
|
|
return _x('Recent Reviews', 'admin-text', 'site-reviews'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|