Skip to content

Commit 951890a

Browse files
committed
feat(Functions): Added fetchers
1 parent 2612690 commit 951890a

File tree

4 files changed

+235
-62
lines changed

4 files changed

+235
-62
lines changed

Request.php

+80-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public static function uclean( $input ) {
3030
return self::clean( \wp_unslash( $input ) );
3131
}
3232

33+
/**
34+
* Fetch, unslash, and clean a variable.
35+
*
36+
* @param string $val Request variable.
37+
* @param mixed $def The default value.
38+
* @return mixed The fetched value.
39+
*/
40+
private static function fetch_var( &$val, $def = null ) {
41+
return self::uclean( $val ?? $def );
42+
}
43+
3344
/**
3445
* Fetch a variable from the $_GET superglobal.
3546
*
@@ -75,13 +86,78 @@ public static function fetch_server_var( $key, $def = null ) {
7586
}
7687

7788
/**
78-
* Fetch, unslash, and clean a variable.
89+
* Fetch a variable from the $_COOKIE superglobal.
7990
*
80-
* @param string $val Request variable.
91+
* @param string $key The key to fetch.
8192
* @param mixed $def The default value.
8293
* @return mixed The fetched value.
8394
*/
84-
private static function fetch_var( &$val, $def = null ) {
85-
return self::uclean( $val ?? $def );
95+
public static function fetch_cookie_var( $key, $def = null ) {
96+
return self::fetch_var( $_COOKIE[ $key ], $def );
97+
}
98+
99+
/**
100+
* Fetch a variable from the $_FILES superglobal.
101+
*
102+
* @param string $key The key to fetch.
103+
* @param mixed $def The default value.
104+
* @return mixed The fetched value.
105+
*/
106+
public static function fetch_files_var( $key, $def = null ) {
107+
return self::fetch_var( $_FILES[ $key ], $def );
108+
}
109+
110+
/**
111+
* Fetch `$_GET` superglobal array.
112+
*
113+
* @return array<string, mixed>
114+
*/
115+
public static function fetch_get_arr() {
116+
return self::fetch_var( $_GET, array() );
117+
}
118+
119+
/**
120+
* Fetch `$_POST` superglobal array.
121+
*
122+
* @return array<string, mixed>
123+
*/
124+
public static function fetch_post_arr() {
125+
return self::fetch_var( $_POST, array() );
126+
}
127+
128+
/**
129+
* Fetch `$_REQUEST` superglobal array.
130+
*
131+
* @return array<string, mixed>
132+
*/
133+
public static function fetch_req_arr() {
134+
return self::fetch_var( $_REQUEST, array() );
135+
}
136+
137+
/**
138+
* Fetch `$_SERVER` superglobal array.
139+
*
140+
* @return array<string, mixed>
141+
*/
142+
public static function fetch_server_arr() {
143+
return self::fetch_var( $_SERVER, array() );
144+
}
145+
146+
/**
147+
* Fetch `$_COOKIE` superglobal array.
148+
*
149+
* @return array<string, mixed>
150+
*/
151+
public static function fetch_cookie_arr() {
152+
return self::fetch_var( $_COOKIE, array() );
153+
}
154+
155+
/**
156+
* Fetch `$_FILES` superglobal array.
157+
*
158+
* @return array<string, mixed>
159+
*/
160+
public static function fetch_files_arr() {
161+
return self::fetch_var( $_FILES, array() );
86162
}
87163
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"XWP\\Helper\\Functions\\": "."
3232
},
3333
"files": [
34-
"xwp-helper-fns.php"
34+
"xwp-helper-fns.php",
35+
"xwp-helper-fns-req.php"
3536
]
3637
}
3738
}

xwp-helper-fns-req.php

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* Request helper functions definition file.
4+
*
5+
* @package eXtended WordPress
6+
* @subpackage Helper\Functions
7+
*/
8+
9+
use XWP\Helper\Functions as f;
10+
11+
if ( ! function_exists( 'xwp_fetch_get_var' ) ) :
12+
/**
13+
* Get an item of `GET` data if set, otherwise return a default value.
14+
*
15+
* @param string $key GET key.
16+
* @param string $def Default value.
17+
* @return mixed Value sanitized by xwp_uclean.
18+
*/
19+
function xwp_fetch_get_var( $key, $def = null ) {
20+
return f\Request::fetch_get_var( $key, $def );
21+
}
22+
endif;
23+
24+
if ( ! function_exists( 'xwp_fetch_post_var' ) ) :
25+
/**
26+
* Get an item of `POST` data if set, otherwise return a default value.
27+
*
28+
* @param string $key POST key.
29+
* @param string $def Default value.
30+
* @return mixed Value sanitized by xwp_uclean.
31+
*/
32+
function xwp_fetch_post_var( $key, $def = null ) {
33+
return f\Request::fetch_post_var( $key, $def );
34+
}
35+
endif;
36+
37+
if ( ! function_exists( 'xwp_fetch_req_var' ) ) :
38+
/**
39+
* Get an item of `REQUEST`data if set, otherwise return a default value.
40+
*
41+
* @param string $key REQUEST key.
42+
* @param string $def Default value.
43+
* @return mixed Value sanitized by xwp_uclean.
44+
*/
45+
function xwp_fetch_req_var( $key, $def = null ) {
46+
return f\Request::fetch_req_var( $key, $def );
47+
}
48+
endif;
49+
50+
if ( ! function_exists( 'xwp_fetch_server_var' ) ) :
51+
/**
52+
* Get an item of `SERVER` data if set, otherwise return a default value.
53+
*
54+
* @param string $key SERVER key.
55+
* @param string $def Default value.
56+
* @return mixed Value sanitized by xwp_uclean.
57+
*/
58+
function xwp_fetch_server_var( $key, $def = null ) {
59+
return f\Request::fetch_server_var( $key, $def );
60+
}
61+
endif;
62+
63+
if ( ! function_exists( 'xwp_fetch_cookie_var' ) ) :
64+
/**
65+
* Get an item of `COOKIE` data if set, otherwise return a default value.
66+
*
67+
* @param string $key COOKIE key.
68+
* @param string $def Default value.
69+
* @return mixed Value sanitized by xwp_uclean.
70+
*/
71+
function xwp_fetch_cookie_var( $key, $def = null ) {
72+
return f\Request::fetch_cookie_var( $key, $def );
73+
}
74+
endif;
75+
76+
if ( ! function_exists( 'xwp_fetch_files_var' ) ) :
77+
/**
78+
* Get an item of `FILES` data if set, otherwise return a default value.
79+
*
80+
* @param string $key FILES key.
81+
* @param string $def Default value.
82+
* @return mixed Value sanitized by xwp_uclean.
83+
*/
84+
function xwp_fetch_files_var( $key, $def = null ) {
85+
return f\Request::fetch_files_var( $key, $def );
86+
}
87+
endif;
88+
89+
if ( ! function_exists( 'xwp_get_arr' ) ) :
90+
/**
91+
* Get the unslashed and cleaned $_GET array.
92+
*
93+
* @return array<string, mixed>
94+
*/
95+
function xwp_get_arr(): array {
96+
return f\Request::fetch_get_arr();
97+
}
98+
endif;
99+
100+
if ( ! function_exists( 'xwp_post_arr' ) ) :
101+
/**
102+
* Get the unslashed and cleaned $_POST array.
103+
*
104+
* @return array<string, mixed>
105+
*/
106+
function xwp_post_arr(): array {
107+
return f\Request::fetch_post_arr();
108+
}
109+
endif;
110+
111+
if ( ! function_exists( 'xwp_req_arr' ) ) :
112+
/**
113+
* Get the unslashed and cleaned $_REQUEST array.
114+
*
115+
* @return array<string, mixed>
116+
*/
117+
function xwp_req_arr(): array {
118+
return f\Request::fetch_req_arr();
119+
}
120+
endif;
121+
122+
if ( ! function_exists( 'xwp_server_arr' ) ) :
123+
/**
124+
* Get the unslashed and cleaned $_SERVER array.
125+
*
126+
* @return array<string, mixed>
127+
*/
128+
function xwp_server_arr(): array {
129+
return f\Request::fetch_server_arr();
130+
}
131+
endif;
132+
133+
if ( ! function_exists( 'xwp_cookie_arr' ) ) :
134+
/**
135+
* Get the unslashed and cleaned $_COOKIE array.
136+
*
137+
* @return array<string, mixed>
138+
*/
139+
function xwp_cookie_arr(): array {
140+
return f\Request::fetch_cookie_arr();
141+
}
142+
endif;
143+
144+
if ( ! function_exists( 'xwp_files_arr' ) ) :
145+
/**
146+
* Get the unslashed and cleaned $_FILES array.
147+
*
148+
* @return array<string, mixed>
149+
*/
150+
function xwp_files_arr(): array {
151+
return f\Request::fetch_files_arr();
152+
}
153+
endif;

xwp-helper-fns.php

-57
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ function wp_array_flatmap( callable $callback, array $input_array ) {
4444
}
4545
endif;
4646

47-
4847
if ( ! function_exists( 'wp_array_flatmap_assoc' ) ) :
4948
/**
5049
* Flatten and map an associative array of arrays.
@@ -62,7 +61,6 @@ function wp_array_flatmap( callable $callback, array $input_array ) {
6261
function wp_array_flatmap_assoc( callable $callback, array $input, string $key, bool $unkey = true ) {
6362
return f\Array_Extra::flatmap_assoc( $callback, $input, $key, $unkey );
6463
}
65-
6664
endif;
6765

6866
if ( ! function_exists( 'wp_array_diff_assoc' ) ) :
@@ -157,8 +155,6 @@ function xwp_remove_hook_callbacks(
157155
}
158156
endif;
159157

160-
161-
162158
if ( ! function_exists( 'xwp_clean' ) ) :
163159
/**
164160
* Clean variables using sanitize_text_field. Arrays are cleaned recursively.
@@ -185,59 +181,6 @@ function xwp_uclean( $input ) {
185181
}
186182
endif;
187183

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;
226-
227-
if ( ! function_exists( 'xwp_fetch_server_var' ) ) :
228-
/**
229-
* Get an item of `SERVER` data if set, otherwise return a default value.
230-
*
231-
* @param string $key SERVER key.
232-
* @param string $def Default value.
233-
* @return mixed Value sanitized by xwp_uclean.
234-
*/
235-
function xwp_fetch_server_var( $key, $def = null ) {
236-
return f\Request::fetch_server_var( $key, $def );
237-
}
238-
endif;
239-
240-
241184
if ( ! function_exists( 'xwp_format_term_name' ) ) :
242185
/**
243186
* Format a term name with term parents.

0 commit comments

Comments
 (0)