PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.3.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.3.0
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / framework / src / WPFluent / Support / Sanitizer.php

Sanitizer.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.3.0, at vendor/wpfluent/framework/src/WPFluent/Support/Sanitizer.php

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