1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
|
8
|
|
|
class ReviewLimits |
9
|
|
|
{ |
10
|
|
|
protected $request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @return array |
14
|
|
|
* @filter site-reviews/get/reviews/query |
15
|
|
|
*/ |
16
|
|
|
public function filterReviewsQuery(array $parameters, array $args) |
17
|
|
|
{ |
18
|
|
|
if ($authorId = get_current_user_id()) { |
19
|
|
|
$parameters['author'] = $authorId; |
20
|
|
|
} |
21
|
|
|
$parameters['post_status'] = ['pending', 'publish']; |
22
|
|
|
return apply_filters('site-reviews/reviews-limits/query', $parameters, $args); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
1 |
|
public function hasReachedLimit(array $request = []) |
29
|
|
|
{ |
30
|
1 |
|
$this->request = $request; |
31
|
1 |
|
$method = glsr(Helper::class)->buildMethodName( |
32
|
1 |
|
glsr(OptionManager::class)->get('settings.submissions.limit'), 'validateBy' |
33
|
|
|
); |
34
|
1 |
|
return method_exists($this, $method) |
35
|
|
|
? !call_user_func([$this, $method]) |
36
|
1 |
|
: false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $value |
41
|
|
|
* @param string $whitelist |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function isWhitelisted($value, $whitelist) |
45
|
|
|
{ |
46
|
|
|
if (empty($whitelist)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
return in_array($value, array_filter(explode("\n", $whitelist), 'trim')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $whitelistName |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function getWhitelist($whitelistName) |
57
|
|
|
{ |
58
|
|
|
return glsr(OptionManager::class)->get('settings.submissions.limit_whitelist.'.$whitelistName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
protected function validate($key, $value, $addMetaQuery = true) |
65
|
|
|
{ |
66
|
|
|
if ($this->isWhitelisted($value, $this->getWhitelist($key))) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
add_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5, 2); |
70
|
|
|
$args = ['assigned_to' => glsr_get($this->request, 'assign_to')]; |
71
|
|
|
if ($addMetaQuery) { |
72
|
|
|
$args[$key] = $value; |
73
|
|
|
} |
74
|
|
|
$reviews = glsr_get_reviews($args); |
75
|
|
|
remove_filter('site-reviews/get/reviews/query', [$this, 'filterReviewsQuery'], 5); |
76
|
|
|
return 0 === count($reviews); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function validateByEmail() |
83
|
|
|
{ |
84
|
|
|
glsr_log()->debug('Email is: '.glsr_get($this->request, 'email')); |
85
|
|
|
return $this->validate('email', glsr_get($this->request, 'email')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
protected function validateByIpAddress() |
92
|
|
|
{ |
93
|
|
|
glsr_log()->debug('IP Address is: '.glsr_get($this->request, 'ip_address')); |
94
|
|
|
return $this->validate('ip_address', glsr_get($this->request, 'ip_address')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
protected function validateByUsername() |
101
|
|
|
{ |
102
|
|
|
$user = wp_get_current_user(); |
103
|
|
|
if (!$user->exists()) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
glsr_log()->debug('Username is: '.$user->user_login); |
107
|
|
|
return $this->validate('username', $user->user_login, false); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|