Skip to content

Commit 9675c19

Browse files
committed
feat(Functions): Added request data utils
1 parent 92731d8 commit 9675c19

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

Request.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

xwp-helper-fns.php

+67
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,70 @@ function xwp_remove_hook_callbacks(
156156
return f\Hook_Remover::remove_callbacks( $classname, $target_hook, $method, $priority );
157157
}
158158
endif;
159+
160+
161+
162+
if ( ! function_exists( 'xwp_clean' ) ) :
163+
/**
164+
* Clean variables using sanitize_text_field. Arrays are cleaned recursively.
165+
* Non-scalar values are ignored.
166+
*
167+
* @param string|array $input Data to sanitize.
168+
* @return string|array
169+
*/
170+
function xwp_clean( $input ) {
171+
return f\Request::clean( $input );
172+
}
173+
endif;
174+
175+
if ( ! function_exists( 'xwp_uclean' ) ) :
176+
/**
177+
* Unslash then clean variables using sanitize_text_field. Arrays are cleaned recursively.
178+
* Non-scalar values are ignored.
179+
*
180+
* @param string|array $input Data to sanitize.
181+
* @return string|array
182+
*/
183+
function xwp_uclean( $input ) {
184+
return f\Request::uclean( $input );
185+
}
186+
endif;
187+
188+
if ( ! function_exists( 'xwp_fetch_get_var' ) ) :
189+
/**
190+
* Get an item of `GET` data if set, otherwise return a default value.
191+
*
192+
* @param string $key GET key.
193+
* @param string $def Default value.
194+
* @return mixed Value sanitized by xwp_uclean.
195+
*/
196+
function xwp_fetch_get_var( $key, $def = null ) {
197+
return f\Request::fetch_get_var( $key, $def );
198+
}
199+
endif;
200+
201+
if ( ! function_exists( 'xwp_fetch_post_var' ) ) :
202+
/**
203+
* Get an item of `POST` data if set, otherwise return a default value.
204+
*
205+
* @param string $key POST key.
206+
* @param string $def Default value.
207+
* @return mixed Value sanitized by xwp_uclean.
208+
*/
209+
function xwp_fetch_post_var( $key, $def = null ) {
210+
return f\Request::fetch_post_var( $key, $def );
211+
}
212+
endif;
213+
214+
if ( ! function_exists( 'xwp_fetch_req_var' ) ) :
215+
/**
216+
* Get an item of `REQUEST`data if set, otherwise return a default value.
217+
*
218+
* @param string $key REQUEST key.
219+
* @param string $def Default value.
220+
* @return mixed Value sanitized by xwp_uclean.
221+
*/
222+
function xwp_fetch_req_var( $key, $def = null ) {
223+
return f\Request::fetch_req_var( $key, $def );
224+
}
225+
endif;

0 commit comments

Comments
 (0)