Skip to content

Commit

Permalink
feat(Functions): Added fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Sep 7, 2024
1 parent 2612690 commit 951890a
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 62 deletions.
84 changes: 80 additions & 4 deletions Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public static function uclean( $input ) {
return self::clean( \wp_unslash( $input ) );
}

/**
* Fetch, unslash, and clean a variable.
*
* @param string $val Request variable.
* @param mixed $def The default value.
* @return mixed The fetched value.
*/
private static function fetch_var( &$val, $def = null ) {
return self::uclean( $val ?? $def );
}

/**
* Fetch a variable from the $_GET superglobal.
*
Expand Down Expand Up @@ -75,13 +86,78 @@ public static function fetch_server_var( $key, $def = null ) {
}

/**
* Fetch, unslash, and clean a variable.
* Fetch a variable from the $_COOKIE superglobal.
*
* @param string $val Request variable.
* @param string $key The key to fetch.
* @param mixed $def The default value.
* @return mixed The fetched value.
*/
private static function fetch_var( &$val, $def = null ) {
return self::uclean( $val ?? $def );
public static function fetch_cookie_var( $key, $def = null ) {
return self::fetch_var( $_COOKIE[ $key ], $def );
}

/**
* Fetch a variable from the $_FILES superglobal.
*
* @param string $key The key to fetch.
* @param mixed $def The default value.
* @return mixed The fetched value.
*/
public static function fetch_files_var( $key, $def = null ) {
return self::fetch_var( $_FILES[ $key ], $def );
}

/**
* Fetch `$_GET` superglobal array.
*
* @return array<string, mixed>
*/
public static function fetch_get_arr() {
return self::fetch_var( $_GET, array() );
}

/**
* Fetch `$_POST` superglobal array.
*
* @return array<string, mixed>
*/
public static function fetch_post_arr() {
return self::fetch_var( $_POST, array() );
}

/**
* Fetch `$_REQUEST` superglobal array.
*
* @return array<string, mixed>
*/
public static function fetch_req_arr() {
return self::fetch_var( $_REQUEST, array() );
}

/**
* Fetch `$_SERVER` superglobal array.
*
* @return array<string, mixed>
*/
public static function fetch_server_arr() {
return self::fetch_var( $_SERVER, array() );
}

/**
* Fetch `$_COOKIE` superglobal array.
*
* @return array<string, mixed>
*/
public static function fetch_cookie_arr() {
return self::fetch_var( $_COOKIE, array() );
}

/**
* Fetch `$_FILES` superglobal array.
*
* @return array<string, mixed>
*/
public static function fetch_files_arr() {
return self::fetch_var( $_FILES, array() );
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"XWP\\Helper\\Functions\\": "."
},
"files": [
"xwp-helper-fns.php"
"xwp-helper-fns.php",
"xwp-helper-fns-req.php"
]
}
}
153 changes: 153 additions & 0 deletions xwp-helper-fns-req.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Request helper functions definition file.
*
* @package eXtended WordPress
* @subpackage Helper\Functions
*/

use XWP\Helper\Functions as f;

if ( ! function_exists( 'xwp_fetch_get_var' ) ) :
/**
* Get an item of `GET` data if set, otherwise return a default value.
*
* @param string $key GET key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_get_var( $key, $def = null ) {
return f\Request::fetch_get_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_post_var' ) ) :
/**
* Get an item of `POST` data if set, otherwise return a default value.
*
* @param string $key POST key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_post_var( $key, $def = null ) {
return f\Request::fetch_post_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_req_var' ) ) :
/**
* Get an item of `REQUEST`data if set, otherwise return a default value.
*
* @param string $key REQUEST key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_req_var( $key, $def = null ) {
return f\Request::fetch_req_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_server_var' ) ) :
/**
* Get an item of `SERVER` data if set, otherwise return a default value.
*
* @param string $key SERVER key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_server_var( $key, $def = null ) {
return f\Request::fetch_server_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_cookie_var' ) ) :
/**
* Get an item of `COOKIE` data if set, otherwise return a default value.
*
* @param string $key COOKIE key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_cookie_var( $key, $def = null ) {
return f\Request::fetch_cookie_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_files_var' ) ) :
/**
* Get an item of `FILES` data if set, otherwise return a default value.
*
* @param string $key FILES key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_files_var( $key, $def = null ) {
return f\Request::fetch_files_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_get_arr' ) ) :
/**
* Get the unslashed and cleaned $_GET array.
*
* @return array<string, mixed>
*/
function xwp_get_arr(): array {
return f\Request::fetch_get_arr();
}
endif;

if ( ! function_exists( 'xwp_post_arr' ) ) :
/**
* Get the unslashed and cleaned $_POST array.
*
* @return array<string, mixed>
*/
function xwp_post_arr(): array {
return f\Request::fetch_post_arr();
}
endif;

if ( ! function_exists( 'xwp_req_arr' ) ) :
/**
* Get the unslashed and cleaned $_REQUEST array.
*
* @return array<string, mixed>
*/
function xwp_req_arr(): array {
return f\Request::fetch_req_arr();
}
endif;

if ( ! function_exists( 'xwp_server_arr' ) ) :
/**
* Get the unslashed and cleaned $_SERVER array.
*
* @return array<string, mixed>
*/
function xwp_server_arr(): array {
return f\Request::fetch_server_arr();
}
endif;

if ( ! function_exists( 'xwp_cookie_arr' ) ) :
/**
* Get the unslashed and cleaned $_COOKIE array.
*
* @return array<string, mixed>
*/
function xwp_cookie_arr(): array {
return f\Request::fetch_cookie_arr();
}
endif;

if ( ! function_exists( 'xwp_files_arr' ) ) :
/**
* Get the unslashed and cleaned $_FILES array.
*
* @return array<string, mixed>
*/
function xwp_files_arr(): array {
return f\Request::fetch_files_arr();
}
endif;
57 changes: 0 additions & 57 deletions xwp-helper-fns.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function wp_array_flatmap( callable $callback, array $input_array ) {
}
endif;


if ( ! function_exists( 'wp_array_flatmap_assoc' ) ) :
/**
* Flatten and map an associative array of arrays.
Expand All @@ -62,7 +61,6 @@ function wp_array_flatmap( callable $callback, array $input_array ) {
function wp_array_flatmap_assoc( callable $callback, array $input, string $key, bool $unkey = true ) {
return f\Array_Extra::flatmap_assoc( $callback, $input, $key, $unkey );
}

endif;

if ( ! function_exists( 'wp_array_diff_assoc' ) ) :
Expand Down Expand Up @@ -157,8 +155,6 @@ function xwp_remove_hook_callbacks(
}
endif;



if ( ! function_exists( 'xwp_clean' ) ) :
/**
* Clean variables using sanitize_text_field. Arrays are cleaned recursively.
Expand All @@ -185,59 +181,6 @@ function xwp_uclean( $input ) {
}
endif;

if ( ! function_exists( 'xwp_fetch_get_var' ) ) :
/**
* Get an item of `GET` data if set, otherwise return a default value.
*
* @param string $key GET key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_get_var( $key, $def = null ) {
return f\Request::fetch_get_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_post_var' ) ) :
/**
* Get an item of `POST` data if set, otherwise return a default value.
*
* @param string $key POST key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_post_var( $key, $def = null ) {
return f\Request::fetch_post_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_req_var' ) ) :
/**
* Get an item of `REQUEST`data if set, otherwise return a default value.
*
* @param string $key REQUEST key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_req_var( $key, $def = null ) {
return f\Request::fetch_req_var( $key, $def );
}
endif;

if ( ! function_exists( 'xwp_fetch_server_var' ) ) :
/**
* Get an item of `SERVER` data if set, otherwise return a default value.
*
* @param string $key SERVER key.
* @param string $def Default value.
* @return mixed Value sanitized by xwp_uclean.
*/
function xwp_fetch_server_var( $key, $def = null ) {
return f\Request::fetch_server_var( $key, $def );
}
endif;


if ( ! function_exists( 'xwp_format_term_name' ) ) :
/**
* Format a term name with term parents.
Expand Down

0 comments on commit 951890a

Please sign in to comment.