PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Support / Sanitizer.php

Sanitizer.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.0, at vendor/wpfluent/framework/src/WPFluent/Support/Sanitizer.php

587 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Support;
4
5 use Closure;
6 use InvalidArgumentException;
7 use FluentCommunity\Framework\Support\DateTime;
8
9 class Sanitizer
10 {
11 /**
12 * Sanitize an email address.
13 *
14 * @param string $arg
15 * @return string
16 */
17 public static function sanitizeEmail($arg)
18 {
19 return sanitize_email($arg);
20 }
21
22 /**
23 * Sanitize a file name.
24 *
25 * @param string $arg
26 * @return string
27 */
28 public static function sanitizeFileName($arg)
29 {
30 return sanitize_file_name($arg);
31 }
32
33 /**
34 * Sanitize an HTML class.
35 *
36 * @param string $arg
37 * @return string
38 */
39 public static function sanitizeHtmlClass($arg)
40 {
41 return sanitize_html_class($arg);
42 }
43
44 /**
45 * Sanitize a key.
46 *
47 * @param string $arg
48 * @return string
49 */
50 public static function sanitizeKey($arg)
51 {
52 return sanitize_key($arg);
53 }
54
55 /**
56 * Sanitize meta data.
57 *
58 * @param string $metaKey Meta key.
59 * @param mixed $metaValue Meta value to sanitize.
60 * @param string $objectType Type of object the meta is registered to
61 * (e.g., 'post', 'term', 'user').
62 * @param string $objectSubtype Optional. Subtype of the object type (e.g., custom post type). Default ''.
63 *
64 * @return mixed Sanitized meta value.
65 */
66 public static function sanitizeMeta(
67 $metaKey,
68 $metaValue,
69 $objectType = 'post',
70 $objectSubtype = ''
71 )
72 {
73 return sanitize_meta($metaKey, $metaValue, $objectType, $objectSubtype);
74 }
75
76 /**
77 * Sanitize a mime type.
78 *
79 * @param string $arg
80 * @return string
81 */
82 public static function sanitizeMimeType($arg)
83 {
84 return sanitize_mime_type($arg);
85 }
86
87 /**
88 * Sanitize an option value.
89 *
90 * @param string $option The option name.
91 * @param mixed $value The option value to sanitize.
92 * @return mixed
93 */
94 public static function sanitizeOption(string $option, $value)
95 {
96 return sanitize_option($option, $value);
97 }
98
99 /**
100 * Sanitize an SQL ORDER BY clause.
101 *
102 * @param string $arg
103 * @return string
104 */
105 public static function sanitizeSqlOrderby($arg)
106 {
107 return sanitize_sql_orderby($arg);
108 }
109
110 /**
111 * Sanitize an integer.
112 *
113 * @param string $arg
114 * @return integer
115 */
116 public static function sanitizeInt($arg)
117 {
118 return intval($arg);
119 }
120
121 /**
122 * Sanitize an float.
123 *
124 * @param string $arg
125 * @return float
126 */
127 public static function sanitizeFloat($arg)
128 {
129 return floatval($arg);
130 }
131
132 /**
133 * Sanitize a text field.
134 *
135 * @param string $arg
136 * @return string
137 */
138 public static function sanitizeTextField($arg)
139 {
140 return sanitize_text_field($arg);
141 }
142
143 /**
144 * Sanitize a text field.
145 *
146 * @param string $arg
147 * @return string
148 */
149 public static function sanitizeText($arg)
150 {
151 return static::sanitizeTextField($arg);
152 }
153
154 /**
155 * Sanitize a title.
156 *
157 * @param string $arg
158 * @return string
159 */
160 public static function sanitizeTitle($arg)
161 {
162 return sanitize_title($arg);
163 }
164
165 /**
166 * Sanitize a title for a query.
167 *
168 * @param string $arg
169 * @return string
170 */
171 public static function sanitizeTitleForQuery($arg)
172 {
173 return sanitize_title_for_query($arg);
174 }
175
176 /**
177 * Sanitize a title with dashes.
178 *
179 * @param string $arg
180 * @return string
181 */
182 public static function sanitizeTitleWithDashes($arg)
183 {
184 return sanitize_title_with_dashes($arg);
185 }
186
187 /**
188 * Sanitize a username.
189 *
190 * @param string $arg
191 * @return string
192 */
193 public static function sanitizeUser($arg)
194 {
195 return sanitize_user($arg);
196 }
197
198 /**
199 * Filter content through kses for posts.
200 *
201 * @param string $arg
202 * @return string
203 */
204 public static function wpFilterPostKses($arg)
205 {
206 return wp_filter_post_kses($arg);
207 }
208
209 /**
210 * Filter content through kses for no HTML.
211 *
212 * @param string $arg
213 * @return string
214 */
215 public static function wpFilterNohtmlKses($arg)
216 {
217 return wp_filter_nohtml_kses($arg);
218 }
219
220 /**
221 * Escape HTML attribute.
222 *
223 * @param string $arg
224 * @return string
225 */
226 public static function escAttr($arg)
227 {
228 return esc_attr($arg);
229 }
230
231 /**
232 * Escape HTML.
233 *
234 * @param string $arg
235 * @return string
236 */
237 public static function escHtml($arg)
238 {
239 return esc_html($arg);
240 }
241
242 /**
243 * Escape JavaScript.
244 *
245 * @param string $arg
246 * @return string
247 */
248 public static function escJs($arg)
249 {
250 return esc_js($arg);
251 }
252
253 /**
254 * Escape textarea content.
255 *
256 * @param string $arg
257 * @return string
258 */
259 public static function escTextarea($arg)
260 {
261 return esc_textarea($arg);
262 }
263
264 /**
265 * Escape URL.
266 *
267 * @param string $arg
268 * @return string
269 */
270 public static function escUrl($arg)
271 {
272 return esc_url($arg);
273 }
274
275 /**
276 * Escape URL (raw).
277 *
278 * @param string $arg
279 * @return string
280 */
281 public static function escUrlRaw($arg)
282 {
283 return esc_url_raw($arg);
284 }
285
286 /**
287 * Escape XML.
288 *
289 * @param string $arg
290 * @return string
291 */
292 public static function escXml($arg)
293 {
294 return esc_xml($arg);
295 }
296
297 /**
298 * Sanitize content with KSES.
299 *
300 * @param string $string Content to sanitize.
301 * @param array|string $allowedHtml Allowed HTML tags. Can be an array of
302 * tags/attributes,or a context string accepted by wp_kses_allowed_html().
303 * Defaults to 'post'.
304 *
305 * @return string Sanitized content with only allowed HTML.
306 */
307 public static function kses($string, $allowedHtml = 'post')
308 {
309 if (is_string($allowedHtml)) {
310 $allowedHtml = wp_kses_allowed_html($allowedHtml);
311 }
312
313 return wp_kses($string, $allowedHtml);
314 }
315
316 /**
317 * Kses post content.
318 *
319 * @param string $arg
320 * @return string
321 */
322 public static function ksesPost($arg)
323 {
324 return wp_kses_post($arg);
325 }
326
327 /**
328 * Kses data.
329 *
330 * @param string $arg
331 * @return string
332 */
333 public static function ksesData($arg)
334 {
335 return wp_kses_data($arg);
336 }
337
338 /**
339 * Escape HTML with translation.
340 *
341 * @param string $arg
342 * @return string
343 */
344 public static function escHtml__($arg)
345 {
346 return esc_html__($arg);
347 }
348
349 /**
350 * Escape attribute with translation.
351 *
352 * @param string $arg
353 * @return string
354 */
355 public static function escAttr__($arg)
356 {
357 return esc_attr__($arg);
358 }
359
360 /**
361 * Escape HTML and echo.
362 *
363 * @param string $arg
364 * @return void
365 */
366 public static function escHtmlE($arg)
367 {
368 esc_html_e($arg);
369 }
370
371 /**
372 * Escape attribute and echo.
373 *
374 * @param string $arg
375 * @return void
376 */
377 public static function escAttrE($arg)
378 {
379 esc_attr_e($arg);
380 }
381
382 /**
383 * Escape HTML with translation context.
384 *
385 * @param string $text Text to escape and translate.
386 * @param string $context Context information for translators.
387 * @param string $domain Optional. Text domain. Default 'default'.
388 * @return string
389 */
390 public static function escHtmlX($text, $context, $domain = 'default')
391 {
392 return esc_html_x($text, $context, $domain);
393 }
394
395 /**
396 * Escape attribute with translation context.
397 *
398 * @param string $text Text to escape and translate.
399 * @param string $context Context information for translators.
400 * @param string $domain Optional. Text domain. Default 'default'.
401 * @return string
402 */
403 public static function escAttrX($text, $context, $domain = 'default')
404 {
405 return esc_attr_x($text, $context, $domain);
406 }
407
408 /**
409 * Returns a DateTime object.
410 *
411 * @param string $value
412 * @param string|null $format
413 * @param string|null|\DateTimeZone $tz
414 * @return \FluentCommunity\Framework\Support\DateTime|null
415 */
416 public static function sanitizeDate($value, $format, $tz)
417 {
418 $result = is_null($format)
419 ? DateTime::parse($value, $tz)
420 : DateTime::createFromFormat($format, $value, $tz);
421
422 return $result ?: null;
423 }
424
425 /**
426 * Sanitize data based on given rules.
427 *
428 * @param array $data
429 * @param array $rules
430 * @return array
431 */
432 public static function sanitize($data = [], $rules = [])
433 {
434 $result = [];
435 $expandedRules = [];
436
437 // Expand rules first (handle wildcards)
438 foreach ($rules as $key => $callbacks) {
439 if (!$callbacks) {
440 continue;
441 }
442
443 $callbacks = is_array($callbacks) ? $callbacks : [$callbacks];
444
445 if (str_contains($key, '*')) {
446 $expandedRules = array_merge(
447 $expandedRules,
448 static::substituteWildcardKeys(
449 [$key => $callbacks], $key, $data
450 )
451 );
452 } else {
453 $expandedRules[$key] = $callbacks;
454 }
455 }
456
457 // Apply sanitization
458 foreach ($expandedRules as $k => $callbacks) {
459 $callbacks = static::mayBeFixCallbacks($callbacks);
460
461 if (($value = Arr::get($data, $k)) !== null) {
462
463 foreach ($callbacks as $cb) {
464 if ($cb = static::getCallback($cb)) {
465 $value = $cb($value);
466 }
467 }
468
469 Arr::Set($data, $k, $value);
470 }
471 }
472
473 return $data;
474 }
475
476 public static function substituteWildcardKeys($array, $field, $data)
477 {
478 $callback = $array[$field];
479
480 unset($array[$field]);
481
482 $expand = function (
483 $data,
484 $segments,
485 $prefix = ''
486 ) use (
487 &$expand,
488 $callback,
489 &$array
490 ) {
491 $segment = array_shift($segments);
492
493 if ($segment === null) {
494 $array[$prefix] = $callback;
495 return;
496 }
497
498 if ($segment === '*') {
499 if (!is_array($data)) return;
500
501 foreach ($data as $key => $child) {
502 $newPrefix = $prefix === '' ? $key : $prefix . '.' . $key;
503 $expand($child, $segments, $newPrefix);
504 }
505 } else {
506 if (is_array($data) && array_key_exists($segment, $data)) {
507 $newPrefix = $prefix === '' ? $segment : $prefix . '.' . $segment;
508 $expand($data[$segment], $segments, $newPrefix);
509 }
510 }
511 };
512
513 $segments = explode('.', $field);
514 $expand($data, $segments);
515
516 return $array;
517 }
518
519 /**
520 * Check and fix if callbacks are given
521 * as: callback1|callback2\callback3.
522 *
523 * @param array|string $callbacks
524 * @return array
525 */
526 public static function mayBeFixCallbacks($callbacks)
527 {
528 $normalized = [];
529
530 foreach ((array)$callbacks as $cb) {
531 if (is_string($cb)) {
532 $normalized = array_merge($normalized, explode('|', $cb));
533 } elseif (is_callable($cb)) {
534 $normalized[] = $cb;
535 }
536 }
537
538 return $normalized;
539 }
540
541 /**
542 * Get the callback function.
543 *
544 * @param callable|string $callback
545 * @return callable|null
546 * @throws \InvalidArgumentException If the rule resolves to nothing callable.
547 */
548 protected static function getCallback($callback)
549 {
550 if ($callback) {
551 if ($callback instanceof Closure) return $callback;
552
553 if ($cb = static::methodExists($callback)) {
554 $callback = $cb;
555 } elseif (!is_callable($callback)) {
556 throw new InvalidArgumentException(sprintf(
557 'Unknown sanitizer rule: %s.',
558 is_string($callback) ? $callback : gettype($callback)
559 ));
560 }
561 }
562
563 return $callback;
564 }
565
566 /**
567 * Check if the method exists.
568 *
569 * @param string $method
570 * @return callable|null
571 */
572 protected static function methodExists($method)
573 {
574 $suffix = '';
575
576 if (Str::endsWith($method, '__')) {
577 $suffix = '__';
578 }
579
580 $method = Str::camel($method) . $suffix;
581
582 if (method_exists(static::class, $method)) {
583 return [static::class, $method];
584 }
585 }
586 }
587