diff --git a/Request.php b/Request.php index 93374b9..30a07cb 100644 --- a/Request.php +++ b/Request.php @@ -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. * @@ -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 + */ + public static function fetch_get_arr() { + return self::fetch_var( $_GET, array() ); + } + + /** + * Fetch `$_POST` superglobal array. + * + * @return array + */ + public static function fetch_post_arr() { + return self::fetch_var( $_POST, array() ); + } + + /** + * Fetch `$_REQUEST` superglobal array. + * + * @return array + */ + public static function fetch_req_arr() { + return self::fetch_var( $_REQUEST, array() ); + } + + /** + * Fetch `$_SERVER` superglobal array. + * + * @return array + */ + public static function fetch_server_arr() { + return self::fetch_var( $_SERVER, array() ); + } + + /** + * Fetch `$_COOKIE` superglobal array. + * + * @return array + */ + public static function fetch_cookie_arr() { + return self::fetch_var( $_COOKIE, array() ); + } + + /** + * Fetch `$_FILES` superglobal array. + * + * @return array + */ + public static function fetch_files_arr() { + return self::fetch_var( $_FILES, array() ); } } diff --git a/composer.json b/composer.json index d77d2c3..be75097 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "XWP\\Helper\\Functions\\": "." }, "files": [ - "xwp-helper-fns.php" + "xwp-helper-fns.php", + "xwp-helper-fns-req.php" ] } } diff --git a/xwp-helper-fns-req.php b/xwp-helper-fns-req.php new file mode 100644 index 0000000..2e1a20d --- /dev/null +++ b/xwp-helper-fns-req.php @@ -0,0 +1,153 @@ + + */ + 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 + */ + 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 + */ + 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 + */ + 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 + */ + 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 + */ + function xwp_files_arr(): array { + return f\Request::fetch_files_arr(); + } +endif; diff --git a/xwp-helper-fns.php b/xwp-helper-fns.php index e0c4e5f..4b5a4f0 100644 --- a/xwp-helper-fns.php +++ b/xwp-helper-fns.php @@ -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. @@ -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' ) ) : @@ -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. @@ -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.