|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
9
|
|
|
|
|
10
|
|
|
class Sanitizer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $sanitizers; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $values; |
|
21
|
|
|
|
|
22
|
38 |
|
public function __construct(array $values = [], array $sanitizers = []) |
|
23
|
|
|
{ |
|
24
|
38 |
|
$this->sanitizers = $this->buildSanitizers(Arr::consolidate($sanitizers)); |
|
25
|
38 |
|
$this->values = Arr::consolidate($values); |
|
26
|
38 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return array|bool|string |
|
30
|
|
|
*/ |
|
31
|
38 |
|
public function run() |
|
32
|
|
|
{ |
|
33
|
38 |
|
$result = $this->values; |
|
34
|
38 |
|
foreach ($this->values as $key => $value) { |
|
35
|
38 |
|
if (array_key_exists($key, $this->sanitizers)) { |
|
36
|
38 |
|
$result[$key] = call_user_func([$this, $this->sanitizers[$key]], $value); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
38 |
|
return $result; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param mixed $value |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
17 |
|
public function sanitizeArray($value) |
|
47
|
|
|
{ |
|
48
|
17 |
|
return Arr::consolidate($value); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param mixed $value |
|
53
|
|
|
* @return int[] |
|
54
|
|
|
*/ |
|
55
|
25 |
|
public function sanitizeArrayInt($value) |
|
56
|
|
|
{ |
|
57
|
25 |
|
return Arr::uniqueInt(Cast::toArray($value)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
* @return string[] |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function sanitizeArrayString($value) |
|
65
|
|
|
{ |
|
66
|
1 |
|
return array_filter(Cast::toArray($value), 'is_string'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param mixed $value |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
16 |
|
public function sanitizeBool($value) |
|
74
|
|
|
{ |
|
75
|
16 |
|
return Cast::toBool($value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* If date is invalid then return an empty string. |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
16 |
|
public function sanitizeDate($value) |
|
84
|
|
|
{ |
|
85
|
16 |
|
$date = strtotime(Cast::toString($value)); |
|
86
|
16 |
|
if (false !== $date) { |
|
87
|
1 |
|
return wp_date('Y-m-d H:i:s', $date); |
|
88
|
|
|
} |
|
89
|
16 |
|
return ''; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param mixed $value |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
24 |
|
public function sanitizeEmail($value) |
|
97
|
|
|
{ |
|
98
|
24 |
|
return sanitize_email(Cast::toString($value)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
7 |
|
public function sanitizeId($value) |
|
106
|
|
|
{ |
|
107
|
7 |
|
require_once ABSPATH.WPINC.'/pluggable.php'; |
|
108
|
7 |
|
$value = $this->sanitizeSlug($value); |
|
109
|
7 |
|
if (empty($value)) { |
|
110
|
7 |
|
$value = glsr()->prefix.substr(wp_hash(serialize($this->values), 'nonce'), -12, 8); |
|
111
|
|
|
} |
|
112
|
7 |
|
return $value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param mixed $value |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
16 |
|
public function sanitizeInt($value) |
|
120
|
|
|
{ |
|
121
|
16 |
|
return Cast::toInt($value); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* This allows lowercase alphannumeric, dash, and underscore characters |
|
126
|
|
|
* @param mixed $value |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
17 |
|
public function sanitizeKey($value) |
|
130
|
|
|
{ |
|
131
|
17 |
|
return Str::snakeCase(sanitize_key($this->sanitizeText($value))); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* This allows lowercase alpha and underscore characters |
|
136
|
|
|
* @param mixed $value |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function sanitizeName($value) |
|
140
|
|
|
{ |
|
141
|
|
|
$value = Str::snakeCase($this->sanitizeText($value)); |
|
142
|
|
|
return preg_replace('/[^a-z_]/', '', $value); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param mixed $value |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
8 |
|
public function sanitizeSlug($value) |
|
150
|
|
|
{ |
|
151
|
8 |
|
return sanitize_title($this->sanitizeText($value)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param mixed $value |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
28 |
|
public function sanitizeText($value) |
|
159
|
|
|
{ |
|
160
|
28 |
|
return sanitize_text_field(Cast::toString($value)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param mixed $value |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function sanitizeTextHtml($value) |
|
168
|
|
|
{ |
|
169
|
|
|
global $allowedposttags; |
|
170
|
|
|
$allowedHtml = [ |
|
171
|
|
|
'a' => glsr_get($allowedposttags, 'a'), |
|
172
|
|
|
'em' => glsr_get($allowedposttags, 'em'), |
|
173
|
|
|
'strong' => glsr_get($allowedposttags, 'strong'), |
|
174
|
|
|
]; |
|
175
|
|
|
$allowedHtml = glsr()->filterString('sanitize/allowed-html-tags', $allowedHtml, $allowedposttags); |
|
176
|
|
|
return trim(wp_kses(Cast::toString($value), $allowedHtml)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param mixed $value |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
16 |
|
public function sanitizeTextMultiline($value) |
|
184
|
|
|
{ |
|
185
|
16 |
|
return sanitize_textarea_field(Cast::toString($value)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param mixed $value |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
16 |
|
public function sanitizeUrl($value) |
|
193
|
|
|
{ |
|
194
|
16 |
|
$url = Cast::toString($value); |
|
195
|
16 |
|
if (!Str::startsWith('http://, https://', $url)) { |
|
196
|
16 |
|
$url = Str::prefix($url, 'https://'); |
|
197
|
|
|
} |
|
198
|
16 |
|
$url = wp_http_validate_url($url); |
|
199
|
16 |
|
return esc_url_raw(Cast::toString($url)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param mixed $value |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
15 |
|
public function sanitizeUserEmail($value) |
|
207
|
|
|
{ |
|
208
|
15 |
|
$user = wp_get_current_user(); |
|
209
|
15 |
|
$value = Cast::toString($value); |
|
210
|
15 |
|
if ($user->exists() && !glsr()->retrieveAs('bool', 'import', false)) { |
|
211
|
2 |
|
return Helper::ifEmpty($value, $user->user_email); |
|
212
|
|
|
} |
|
213
|
15 |
|
return sanitize_email($value); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param mixed $value |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
15 |
|
public function sanitizeUserName($value) |
|
221
|
|
|
{ |
|
222
|
15 |
|
$user = wp_get_current_user(); |
|
223
|
15 |
|
$value = Cast::toString($value); |
|
224
|
15 |
|
if ($user->exists() && !glsr()->retrieveAs('bool', 'import', false)) { |
|
225
|
2 |
|
return Helper::ifEmpty($value, $user->display_name); |
|
226
|
|
|
} |
|
227
|
15 |
|
return sanitize_text_field($value); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @return array |
|
232
|
|
|
*/ |
|
233
|
38 |
|
protected function buildSanitizers(array $sanitizers) |
|
234
|
|
|
{ |
|
235
|
38 |
|
foreach ($sanitizers as $key => &$type) { |
|
236
|
38 |
|
$method = Helper::buildMethodName($type, 'sanitize'); |
|
237
|
38 |
|
$type = method_exists($this, $method) |
|
238
|
38 |
|
? $method |
|
239
|
38 |
|
: 'sanitizeText'; |
|
240
|
|
|
} |
|
241
|
38 |
|
return $sanitizers; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|