Assets.php
1 day ago
Cache.php
3 weeks ago
Date.php
1 day ago
Debug.php
1 day ago
FeedReader.php
1 day ago
HTML.php
1 day ago
Helper.php
3 weeks ago
JSON.php
3 weeks ago
Nonce.php
3 weeks ago
Notice.php
1 day ago
Number.php
3 weeks ago
NumberConverter.php
1 day ago
Param.php
3 weeks ago
Sanitizing.php
1 day ago
Strip.php
3 weeks ago
Templates.php
3 weeks ago
User.php
3 weeks ago
Validating.php
3 weeks ago
WooCommerce.php
3 weeks ago
WordPress.php
1 day ago
Sanitizing.php
248 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | use enshrined\svgSanitize\Sanitizer; |
| 6 | |
| 7 | defined( 'ABSPATH' ) || exit; |
| 8 | |
| 9 | class Sanitizing { |
| 10 | /** |
| 11 | * Sanitize SVG |
| 12 | * |
| 13 | * @param string $svg SVG HTML |
| 14 | * |
| 15 | * @return string SVG HTML |
| 16 | */ |
| 17 | public static function svg( string $svg ): string { |
| 18 | $Sanitizer = new Sanitizer(); |
| 19 | $Sanitizer->removeXMLTag( true ); |
| 20 | $cleanSVG = $Sanitizer->sanitize( $svg ); |
| 21 | |
| 22 | return $cleanSVG ?: ''; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Clean scalar value |
| 27 | * |
| 28 | * @param mixed $value |
| 29 | * |
| 30 | * @return array|mixed|string |
| 31 | */ |
| 32 | public static function clean( $value ) { |
| 33 | if ( is_array( $value ) ) { |
| 34 | return array_map( array( '\WPParsidate\Helper\Sanitizing', 'clean' ), $value ); |
| 35 | } |
| 36 | |
| 37 | return is_scalar( $value ) ? sanitize_text_field( $value ) : $value; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * JSON to Array |
| 42 | * |
| 43 | * @param string $value JSON string |
| 44 | * |
| 45 | * @return array |
| 46 | * @throws \JsonException |
| 47 | */ |
| 48 | public static function jsonArray( string $value ): array { |
| 49 | $value = wp_unslash( htmlspecialchars_decode( $value ) ); |
| 50 | $value = str_replace( "'", '"', $value ); |
| 51 | if ( ! JSON::validate( $value ) ) { |
| 52 | return []; |
| 53 | } |
| 54 | |
| 55 | return self::array( JSON::decode( $value, true ) ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Object to array |
| 60 | * |
| 61 | * @param mixed $value |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | public static function array( $value ): array { |
| 66 | return (array) $value; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Convert to bool value |
| 71 | * |
| 72 | * @param mixed $value |
| 73 | * |
| 74 | * @return bool |
| 75 | */ |
| 76 | public static function bool( $value ): bool { |
| 77 | $value = $value ?? ''; |
| 78 | |
| 79 | return is_bool( $value ) ? $value : ( 'yes' === strtolower( $value ) || 1 === $value || 'true' === strtolower( $value ) || '1' === $value ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Converts a value to non-negative integer. |
| 84 | * |
| 85 | * @param mixed $value Data you wish to have converted to a non-negative integer. |
| 86 | * |
| 87 | * @return int A non-negative integer. |
| 88 | */ |
| 89 | public static function absint( $value ): int { |
| 90 | return absint( $value ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Converts a value to integer |
| 95 | * |
| 96 | * @param mixed $value Data you wish to have converted to an integer. |
| 97 | * |
| 98 | * @return int An integer |
| 99 | */ |
| 100 | public static function int( $value ): int { |
| 101 | return (int) $value; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Converts a value to float |
| 106 | * |
| 107 | * @param mixed $value Data you wish to have converted to an float. |
| 108 | * |
| 109 | * @return float An float |
| 110 | */ |
| 111 | public static function float( $value ): float { |
| 112 | return (float) $value; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sanitizes a string into a slug, which can be used in URLs or HTML attributes. |
| 117 | * |
| 118 | * @param string $value The string to be sanitized. |
| 119 | * |
| 120 | * @return string The sanitized string. |
| 121 | */ |
| 122 | public static function title( string $value ): string { |
| 123 | return sanitize_title( $value ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sanitizes a hex color. |
| 128 | * |
| 129 | * @param $value |
| 130 | * |
| 131 | * @return string |
| 132 | */ |
| 133 | public static function color( $value ): string { |
| 134 | return sanitize_hex_color( $value ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sanitizes a hex color without a hash. Use sanitize_hex_color() when possible. |
| 139 | * |
| 140 | * @param string $value The color value to sanitize. Can be with or without a #. |
| 141 | * |
| 142 | * @return string|null The sanitized hex color without the hash prefix, |
| 143 | * empty string if input is empty, or null if invalid. |
| 144 | */ |
| 145 | public static function colorNoHash( string $value ): string { |
| 146 | return sanitize_hex_color_no_hash( $value ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sanitizes a string from user input or from the database. |
| 151 | * |
| 152 | * @param string $value String to sanitize. |
| 153 | * |
| 154 | * @return string Sanitized string. |
| 155 | */ |
| 156 | public static function text( string $value ): string { |
| 157 | return sanitize_text_field( $value ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Strips out all characters that are not allowable in an email. |
| 162 | * |
| 163 | * @param string $value Email address to filter. |
| 164 | * |
| 165 | * @return string Filtered email address. |
| 166 | */ |
| 167 | public static function email( string $value ): string { |
| 168 | return sanitize_email( wp_unslash( $value ) ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Sanitizes a filename, replacing whitespace with dashes. |
| 173 | * |
| 174 | * @param string $value The filename to be sanitized. |
| 175 | * |
| 176 | * @return string The sanitized filename. |
| 177 | */ |
| 178 | public static function filename( string $value ): string { |
| 179 | return sanitize_file_name( $value ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sanitizes a URL for database or redirect usage. |
| 184 | * |
| 185 | * @param string $value The URL to be cleaned. |
| 186 | * |
| 187 | * @return string The cleaned URL after esc_url() is run with the 'db' context. |
| 188 | */ |
| 189 | public static function url( string $value ): string { |
| 190 | return sanitize_url( $value, array( 'http', 'https' ) ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sanitizes a multiline string from user input or from the database. |
| 195 | * |
| 196 | * @param string $value String to sanitize. |
| 197 | * |
| 198 | * @return string Sanitized string. |
| 199 | */ |
| 200 | public static function textarea( string $value ): string { |
| 201 | return sanitize_textarea_field( wp_unslash( $value ) ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sanitizes an HTML classname to ensure it only contains valid characters. |
| 206 | * |
| 207 | * @param string $value The classname to be sanitized. |
| 208 | * |
| 209 | * @return string The sanitized value. |
| 210 | */ |
| 211 | public static function class( string $value ): string { |
| 212 | return sanitize_html_class( $value ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Sanitizes a mime type |
| 217 | * |
| 218 | * @param string $value Mime type. |
| 219 | * |
| 220 | * @return string Sanitized mime type. |
| 221 | */ |
| 222 | public static function mimeType( string $value ): string { |
| 223 | return sanitize_mime_type( $value ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Ensures a string is a valid SQL 'order by' clause. |
| 228 | * |
| 229 | * @param string $value Order by clause to be validated. |
| 230 | * |
| 231 | * @return string|false Returns $orderby if valid, false otherwise. |
| 232 | */ |
| 233 | public static function sqlOrderBy( string $value ): string { |
| 234 | return sanitize_sql_orderby( $value ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Sanitizes a username, stripping out unsafe characters. |
| 239 | * |
| 240 | * @param string $value The username to be sanitized. |
| 241 | * |
| 242 | * @return string The sanitized username, after passing through filters. |
| 243 | */ |
| 244 | public static function username( string $value ): string { |
| 245 | return sanitize_user( $value ); |
| 246 | } |
| 247 | } |
| 248 |