ssteele / wp-typesanity
Sanitize input by type when using WordPress
Requires (Dev)
- 10up/wp_mock: 0.3.*
- phpunit/phpunit: 6.*
This package is not auto-updated.
Last update: 2024-11-10 06:22:55 UTC
README
A WordPress plugin that sanitizes user input by unit type
Description
WP Typesanity delivers a consistent experience when dealing with the various base PHP unit types in the WordPress environment. All input is stringified and filtered through wp_kses
to remove markup and "evil scripts". You can pass a second parameter to sanitize
to specify an expected unit type. Strings, integers, and floats are verified and expected data is kept. Unexpected data is set to an empty string. Now you are free to develop as fast as you like knowing the unit types you're dealing with are what you expect. See table below for a breakdown of unit type expectations:
Setup
Navigate to your plugin via the terminal and issue:
composer require ssteele/wp-typesanity
Documentation
Sanitize user input:
$translator = new UserInput();
$_POST = [
'name' => 'Steve Steele',
'id' => 1,
'gpa' => 3.9,
];
$name = $translator->sanitize($_POST['name']); // sanitize name
Pass in optional parameter [s]tring, [i]nteger, or [f]loat to ensure proper data type:
$name = $translator->sanitize($_POST['name'], 's'); // sanitize name as string
$id = $translator->sanitize($_POST['id'], 'i'); // sanitize id as integer
$gpa = $translator->sanitize($_POST['gpa'], 'f'); // sanitize gpa as float
Bulk sanitize an array of user input:
$post = $translator->sanitize($_POST); // sanitize all post elements
$post = $translator->sanitize($_POST, 'i'); // sanitize all post elements as integer
$post = $translator->sanitize($_POST, ['s', 'i', 'f']); // sanitize all post elements (of known order) against respective types