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 |
|
return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
1 |
|
protected function validateByEmail() |
55
|
|
|
{ |
56
|
1 |
|
glsr_log()->debug('Email is: '.$this->request->email); |
57
|
1 |
|
return $this->validateLimit('email', $this->request->email, [ |
58
|
1 |
|
'email' => $this->request->email, |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
1 |
|
protected function validateByIpAddress() |
66
|
|
|
{ |
67
|
1 |
|
glsr_log()->debug('IP Address is: '.$this->request->ip_address); |
68
|
1 |
|
return $this->validateLimit('ip_address', $this->request->ip_address, [ |
69
|
1 |
|
'ip_address' => $this->request->ip_address, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
1 |
|
protected function validateByUsername() |
77
|
|
|
{ |
78
|
1 |
|
$user = wp_get_current_user(); |
|
|
|
|
79
|
1 |
|
if (!$user->exists()) { |
80
|
1 |
|
return true; |
81
|
|
|
} |
82
|
1 |
|
glsr_log()->debug('Username is: '.$user->user_login); |
83
|
1 |
|
return $this->validateLimit('username', $user->user_login, [ |
84
|
1 |
|
'status' => 'all', |
85
|
1 |
|
'author_id' => $user->ID, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $key |
91
|
|
|
* @param string $value |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
1 |
|
protected function validateLimit($key, $value, array $queryArgs) |
95
|
|
|
{ |
96
|
1 |
|
if (empty($value) |
97
|
1 |
|
|| $this->isWhitelisted($value, glsr_get_option('submissions.limit_whitelist.'.$key))) { |
98
|
1 |
|
return true; |
99
|
|
|
} |
100
|
1 |
|
$queryArgs['assigned_posts'] = $this->request->assigned_posts; |
101
|
1 |
|
add_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator']); |
|
|
|
|
102
|
1 |
|
$reviews = glsr_get_reviews($queryArgs); |
103
|
1 |
|
remove_filter('query/sql/clause/operator', [$this, 'filterSqlClauseOperator']); |
|
|
|
|
104
|
1 |
|
$result = 0 === $reviews->total; |
105
|
1 |
|
return glsr()->filterBool('validate/review-limits', $result, $reviews, $this->request, $key); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|