Skip to content

Commit

Permalink
feat(Functions): Added request data utils
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Aug 11, 2024
1 parent 92731d8 commit 9675c19
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php //phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput, SlevomatCodingStandard.Operators.SpreadOperatorSpacing.IncorrectSpacesAfterOperator

namespace XWP\Helper\Functions;

/**
* Request helper class.
*/
final class Request {
/**
* Clean input data.
*
* @param string|array $input The input data.
* @return string|array The cleaned input data.
*/
public static function clean( $input ) {
return match ( true ) {
\is_array( $input ) => \array_map( self::clean( ... ), $input ),
\is_scalar( $input ) => \sanitize_text_field( $input ),
default => $input,
};
}

/**
* Unslash and clean input data.
*
* @param string|array $input The input data.
* @return string|array The cleaned input data.
*/
public static function uclean( $input ) {
return self::clean( \wp_unslash( $input ) );
}

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

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

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

/**
* 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 );
}
}
67 changes: 67 additions & 0 deletions xwp-helper-fns.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,70 @@ function xwp_remove_hook_callbacks(
return f\Hook_Remover::remove_callbacks( $classname, $target_hook, $method, $priority );
}
endif;



if ( ! function_exists( 'xwp_clean' ) ) :
/**
* Clean variables using sanitize_text_field. Arrays are cleaned recursively.
* Non-scalar values are ignored.
*
* @param string|array $input Data to sanitize.
* @return string|array
*/
function xwp_clean( $input ) {
return f\Request::clean( $input );
}
endif;

if ( ! function_exists( 'xwp_uclean' ) ) :
/**
* Unslash then clean variables using sanitize_text_field. Arrays are cleaned recursively.
* Non-scalar values are ignored.
*
* @param string|array $input Data to sanitize.
* @return string|array
*/
function xwp_uclean( $input ) {
return f\Request::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;

0 comments on commit 9675c19

Please sign in to comment.