$v ) { $out[ is_int( $k ) ? $k : self::key( $k ) ] = self::deep( $v, $mode ); } return $out; } if ( is_bool( $value ) || is_int( $value ) || is_float( $value ) || null === $value ) { return $value; } if ( ! is_scalar( $value ) ) { return ''; } $value = (string) $value; switch ( $mode ) { case 'html': return wp_kses_post( $value ); case 'key': return sanitize_key( $value ); default: return sanitize_textarea_field( $value ); } } /** Array keys: letters, numbers, underscore, dash and dot only. */ public static function key( $key ) { return preg_replace( '/[^A-Za-z0-9_\-\.]/', '', (string) $key ); } /** * Decode a JSON string and sanitise every value in it. * * @return array Empty array when the JSON is invalid. */ public static function json( $raw, $mode = 'text' ) { $d = is_string( $raw ) ? json_decode( $raw, true ) : null; return is_array( $d ) ? self::deep( $d, $mode ) : array(); } /** * Read, unslash, decode and sanitise a JSON string from $_POST. * The caller must verify the nonce and capability first. */ public static function post_json( $key, $mode = 'text' ) { if ( ! isset( $_POST[ $key ] ) || ! is_string( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- verified by the caller. return array(); } return self::json( wp_unslash( $_POST[ $key ] ), $mode ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised recursively by json(). } /** Read, unslash and sanitise an array from $_POST. The caller must verify the nonce and capability first. */ public static function post_array( $key, $mode = 'text' ) { if ( ! isset( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- verified by the caller. return array(); } return (array) self::deep( wp_unslash( $_POST[ $key ] ), $mode ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised recursively by deep(). } /** A Y-m-d date filter from the query string, or $default. Only read with a valid view nonce (see Request). */ public static function get_date( $key, $default = '' ) { return Request::filter( $key, 'date', $default ); } }