|
| 1 | +<?php //phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput, SlevomatCodingStandard.Operators.SpreadOperatorSpacing.IncorrectSpacesAfterOperator |
| 2 | + |
| 3 | +namespace XWP\Helper\Functions; |
| 4 | + |
| 5 | +/** |
| 6 | + * Request helper class. |
| 7 | + */ |
| 8 | +final class Request { |
| 9 | + /** |
| 10 | + * Clean input data. |
| 11 | + * |
| 12 | + * @param string|array $input The input data. |
| 13 | + * @return string|array The cleaned input data. |
| 14 | + */ |
| 15 | + public static function clean( $input ) { |
| 16 | + return match ( true ) { |
| 17 | + \is_array( $input ) => \array_map( self::clean( ... ), $input ), |
| 18 | + \is_scalar( $input ) => \sanitize_text_field( $input ), |
| 19 | + default => $input, |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Unslash and clean input data. |
| 25 | + * |
| 26 | + * @param string|array $input The input data. |
| 27 | + * @return string|array The cleaned input data. |
| 28 | + */ |
| 29 | + public static function uclean( $input ) { |
| 30 | + return self::clean( \wp_unslash( $input ) ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Fetch a variable from the $_GET superglobal. |
| 35 | + * |
| 36 | + * @param string $key The key to fetch. |
| 37 | + * @param mixed $def The default value. |
| 38 | + * @return mixed The fetched value. |
| 39 | + */ |
| 40 | + public static function fetch_get_var( $key, $def = null ) { |
| 41 | + return self::fetch_var( $_GET[ $key ], $def ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Fetch a variable from the $_POST superglobal. |
| 46 | + * |
| 47 | + * @param string $key The key to fetch. |
| 48 | + * @param mixed $def The default value. |
| 49 | + * @return mixed The fetched value. |
| 50 | + */ |
| 51 | + public static function fetch_post_var( $key, $def = null ) { |
| 52 | + return self::fetch_var( $_POST[ $key ], $def ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Fetch a variable from the $_REQUEST superglobal. |
| 57 | + * |
| 58 | + * @param string $key The key to fetch. |
| 59 | + * @param mixed $def The default value. |
| 60 | + * @return mixed The fetched value. |
| 61 | + */ |
| 62 | + public static function fetch_req_var( $key, $def = null ) { |
| 63 | + return self::fetch_var( $_REQUEST[ $key ], $def ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Fetch, unslash, and clean a variable. |
| 68 | + * |
| 69 | + * @param string $val Request variable. |
| 70 | + * @param mixed $def The default value. |
| 71 | + * @return mixed The fetched value. |
| 72 | + */ |
| 73 | + private static function fetch_var( &$val, $def = null ) { |
| 74 | + return self::uclean( $val ?? $def ); |
| 75 | + } |
| 76 | +} |
0 commit comments