|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
|
|
7
|
|
|
class ReviewLimitsValidator extends ValidatorAbstract |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
public function filterSqlClauseOperator() |
|
13
|
|
|
{ |
|
14
|
|
|
return 'AND'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function isValid() |
|
21
|
|
|
{ |
|
22
|
1 |
|
$method = Helper::buildMethodName(glsr_get_option('submissions.limit'), 'validateBy'); |
|
23
|
1 |
|
return method_exists($this, $method) |
|
24
|
1 |
|
? call_user_func([$this, $method]) |
|
25
|
1 |
|
: true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function performValidation() |
|
32
|
|
|
{ |
|
33
|
1 |
|
if (!$this->isValid()) { |
|
34
|
1 |
|
$this->setErrors(__('You have already submitted a review.', 'site-reviews')); |
|
35
|
|
|
} |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $value |
|
40
|
|
|
* @param string $whitelist |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
1 |
|
protected function isWhitelisted($value, $whitelist) |
|
44
|
|
|
{ |
|
45
|
1 |
|
if (empty($whitelist)) { |
|
46
|
1 |
|
return false; |
|
47
|
|
|
} |
|
48
|
1 |
|
$values = array_filter(array_map('trim', explode("\n", $whitelist))); |
|
49
|
1 |
|
return in_array($value, $values); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
1 |
|
protected function normalizeArgs(array $args) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$assignments = glsr_get_option('submissions.limit_assignments', ['assigned_posts'], 'array'); // assigned_posts is the default |
|
58
|
1 |
|
if (in_array('assigned_posts', $assignments)) { |
|
59
|
1 |
|
$args['assigned_posts'] = $this->request->assigned_posts; |
|
60
|
|
|
} |
|
61
|
1 |
|
if (in_array('assigned_terms', $assignments)) { |
|
62
|
|
|
$args['assigned_terms'] = $this->request->assigned_terms; |
|
63
|
|
|
} |
|
64
|
1 |
|
if (in_array('assigned_users', $assignments)) { |
|
65
|
|
|
$args['assigned_users'] = $this->request->assigned_users; |
|
66
|
|
|
} |
|
67
|
1 |
|
$args['status'] = 'all'; |
|
68
|
1 |
|
return $args; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
1 |
|
protected function validateByEmail() |
|
75
|
|
|
{ |
|
76
|
1 |
|
glsr_log()->debug('Email is: '.$this->request->email); |
|
77
|
1 |
|
return $this->validateLimit('email', $this->request->email, [ |
|
78
|
1 |
|
'email' => $this->request->email, |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
1 |
|
protected function validateByIpAddress() |
|
86
|
|
|
{ |
|
87
|
1 |
|
glsr_log()->debug('IP Address is: '.$this->request->ip_address); |
|
88
|
1 |
|
return $this->validateLimit('ip_address', $this->request->ip_address, [ |
|
89
|
1 |
|
'ip_address' => $this->request->ip_address, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
1 |
|
protected function validateByUsername() |
|
97
|
|
|
{ |
|
98
|
1 |
|
$user = wp_get_current_user(); |
|
99
|
1 |
|
if (!$user->exists()) { |
|
100
|
1 |
|
return true; |
|
101
|
|
|
} |
|
102
|
1 |
|
glsr_log()->debug('Username is: '.$user->user_login); |
|
103
|
1 |
|
return $this->validateLimit('username', $user->user_login, [ |
|
104
|
1 |
|
'user__in' => $user->ID, |
|
105
|
|
|
]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $key |
|
110
|
|
|
* @param string $value |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
1 |
|
protected function validateLimit($key, $value, array $args) |
|
114
|
|
|
{ |
|
115
|
1 |
|
if (empty($value) |
|
116
|
1 |
|
|| $this->isWhitelisted($value, glsr_get_option('submissions.limit_whitelist.'.$key))) { |
|
117
|
1 |
|
return true; |
|
118
|
|
|
} |
|
119
|
1 |
|
add_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator'], 20); |
|
120
|
1 |
|
$reviews = glsr_get_reviews($this->normalizeArgs($args)); |
|
121
|
1 |
|
remove_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator'], 20); |
|
122
|
1 |
|
$result = 0 === $reviews->total; |
|
123
|
1 |
|
return glsr()->filterBool('validate/review-limits', $result, $reviews, $this->request, $key); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|