$type ) { if ( isset( $payload[ $key ] ) ) { // "enum:a|b|c": one of the listed values. Anything else is not // stored, so the setting keeps its current or default value. if ( is_string( $type ) && 0 === strpos( $type, 'enum:' ) ) { $value = is_scalar( $payload[ $key ] ) ? sanitize_text_field( (string) $payload[ $key ] ) : ''; if ( in_array( $value, explode( '|', substr( $type, 5 ) ), true ) ) { $sanitized_payload[ $key ] = $value; } continue; } switch ( $type ) { case 'int_list': // A list of IDs: positive integers only. $list = is_array( $payload[ $key ] ) ? $payload[ $key ] : array(); $sanitized_payload[ $key ] = array_values( array_unique( array_filter( array_map( 'absint', array_filter( $list, 'is_scalar' ) ) ) ) ); break; case 'key_list': // A list of keys (for example role names). $list = is_array( $payload[ $key ] ) ? $payload[ $key ] : array(); $sanitized_payload[ $key ] = array_values( array_unique( array_filter( array_map( 'sanitize_key', array_filter( $list, 'is_scalar' ) ) ) ) ); break; case 'integer': $sanitized_payload[ $key ] = (int) absint( sanitize_text_field( $payload[ $key ] ) ); break; case 'float': $sanitized_payload[ $key ] = (float) floatval( sanitize_text_field( $payload[ $key ] ) ); break; case 'string': $sanitized_payload[ $key ] = (string) sanitize_text_field( $payload[ $key ] ); break; case 'url': $sanitized_payload[ $key ] = esc_url_raw( $payload[ $key ] ); break; case 'key': $sanitized_payload[ $key ] = sanitize_key( is_scalar( $payload[ $key ] ) ? (string) $payload[ $key ] : '' ); break; case 'boolean': $sanitized_payload[ $key ] = self::sanitize_checkbox_field( $payload[ $key ] ); break; case 'array': $sanitized_payload[ $key ] = self::sanitize_array_field( $payload[ $key ] ); break; case 'json': $sanitized_payload[ $key ] = self::sanitize_json_form_data( $payload[ $key ] ); break; case 'post': $sanitized_payload[ $key ] = wp_kses_post( $payload[ $key ] ); break; case 'textarea': $sanitized_payload[ $key ] = sanitize_textarea_field( is_scalar( $payload[ $key ] ) ? (string) $payload[ $key ] : '' ); break; case 'json_string': $sanitized_payload[ $key ] = self::sanitize_json_string( $payload[ $key ] ); break; default: $sanitized_payload[ $key ] = sanitize_text_field( $payload[ $key ] ); break; }//end switch }//end if }//end foreach return $sanitized_payload; } /** * A JSON document sent as one field (the form builder, StoreEngine prices, * course import data). It is decoded, cleaned like a request payload (keys * reduced to safe characters, invalid UTF-8 and control characters removed * at every depth) and encoded again for the callback, which applies its own * per-field typing. Anything that is not valid JSON becomes an empty string. * * @param mixed $value Raw field value. * @return string */ public static function sanitize_json_string( $value ) { if ( is_array( $value ) ) { $decoded = $value; } else { $decoded = is_string( $value ) ? json_decode( $value, true ) : null; if ( null === $decoded && 'null' !== trim( (string) $value ) ) { return ''; } } $encoded = wp_json_encode( self::sanitize_request_payload( $decoded ) ); return false === $encoded ? '' : $encoded; } /** * Read only the named fields of the current POST request, unslashed. The * dispatchers pass the result straight to sanitize_payload() with the * action's declared schema, so each field is typed before any callback. * * @param string[] $keys Declared field names. * @return array */ public static function declared_request_fields( array $keys ) { $fields = array(); foreach ( $keys as $key ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- the dispatcher has verified the nonce. if ( isset( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- typed by sanitize_payload() with the declared schema. $fields[ $key ] = wp_unslash( $_POST[ $key ] ); } } return $fields; } public static function sanitize_json_form_data( $data, $schema = [] ) { $data = is_array( $data ) ? $data : json_decode( $data ); if ( is_array( $data ) ) { $results = []; $has_schema = count( $schema ); foreach ( $data as $key => $value ) { if ( $has_schema && ! isset( $schema[ $key ] ) ) { continue; } if ( is_array( $value ) || is_object( $value ) ) { $value = (array) $value; $child_array = []; foreach ( $value as $child_key => $child_value ) { $child_array[ sanitize_key( $child_key ) ] = sanitize_text_field( $child_value ); } $results[] = $child_array; } else { $results[ sanitize_key( $key ) ] = sanitize_text_field( $value ); } } return $results; } return sanitize_text_field( $data ); } /** * Sanitize an array, or a JSON string holding one. Every key is cleaned * and every leaf value becomes a boolean or a sanitize_text_field() * string. JSON objects are decoded as arrays, and input that does not * decode to an array is returned as an empty array, so nothing reaches * the caller unsanitized. * * @param mixed $array_data Array or JSON string. * @param int $depth Current depth. * @return array */ public static function sanitize_array_field( $array_data, $depth = 0 ) { if ( is_object( $array_data ) ) { $array_data = json_decode( wp_json_encode( $array_data ), true ); } elseif ( ! is_array( $array_data ) ) { $array_data = is_string( $array_data ) ? json_decode( $array_data, true ) : null; } if ( ! is_array( $array_data ) || $depth > 10 ) { return array(); } $boolean = [ 'true', 'false', '1', '0' ]; $clean = array(); foreach ( $array_data as $key => $value ) { $key = is_int( $key ) ? $key : self::sanitize_payload_key( $key ); if ( '' === $key ) { continue; } if ( is_array( $value ) || is_object( $value ) ) { $clean[ $key ] = self::sanitize_array_field( $value, $depth + 1 ); } elseif ( is_bool( $value ) || in_array( $value, $boolean, true ) ) { $clean[ $key ] = self::sanitize_checkbox_field( $value ); } elseif ( is_int( $value ) || is_float( $value ) ) { $clean[ $key ] = $value; } else { $clean[ $key ] = sanitize_text_field( is_scalar( $value ) ? (string) $value : '' ); } } return $clean; } public static function sanitize_checkbox_field( $boolean ) { return (bool) filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN ); } /** * First-pass sanitizing of a whole request payload, applied by the AJAX * and admin-post dispatchers before any callback sees it. * * - Every array key, at any depth, is reduced to letters, digits, "_" and * "-"; entries whose key ends up empty are dropped. Callbacks therefore * never receive (or store) an unsanitized key. * - Every string value has invalid UTF-8 and control characters (other * than tab and line breaks) removed. * * This step never changes legitimate data, so rich text, JSON and * passwords survive it intact; each callback then applies its own typed * sanitizing ({@see self::sanitize_payload()}) to the fields it uses. * * @param mixed $data Unslashed request data. * @param int $depth Current depth (guards against deeply nested input). * @return mixed */ public static function sanitize_request_payload( $data, $depth = 0 ) { if ( is_array( $data ) ) { if ( $depth >= 10 ) { return array(); } $clean = array(); foreach ( $data as $key => $value ) { $key = is_int( $key ) ? $key : self::sanitize_payload_key( $key ); if ( '' === $key ) { continue; } $clean[ $key ] = self::sanitize_request_payload( $value, $depth + 1 ); } return $clean; } if ( is_string( $data ) ) { $data = wp_check_invalid_utf8( $data, true ); return (string) preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $data ); } return is_scalar( $data ) ? $data : null; } /** * Sanitize one imported CSV row before it is stored or handed to an * import filter: keys through sanitize_key(), values through * wp_kses_post() (keeps formatting, drops script/style and handlers). * * @param array $row Row keyed by CSV header. * @return array */ public static function sanitize_import_row( $row ) { $clean = array(); foreach ( (array) $row as $key => $value ) { $key = sanitize_key( (string) $key ); if ( '' === $key ) { continue; } $clean[ $key ] = is_scalar( $value ) ? self::sanitize_import_value( (string) $value ) : ''; } return $clean; } /** * One imported CSV cell: a JSON cell is decoded, every string in it is * filtered with wp_kses_post() (keys cleaned) and it is re-encoded, so * it stays valid JSON; any other cell is filtered with wp_kses_post(). * * @param string $value Cell value. * @return string */ public static function sanitize_import_value( $value ) { $trimmed = ltrim( $value ); if ( '' !== $trimmed && ( '{' === $trimmed[0] || '[' === $trimmed[0] ) ) { $decoded = json_decode( $value, true ); if ( is_array( $decoded ) ) { $walk = static function ( $data ) use ( &$walk ) { $out = array(); foreach ( $data as $k => $v ) { $k = is_int( $k ) ? $k : self::sanitize_payload_key( $k ); if ( '' === $k ) { continue; } if ( is_array( $v ) ) { $out[ $k ] = $walk( $v ); } elseif ( is_string( $v ) ) { $out[ $k ] = self::kses_import_html( $v ); } else { $out[ $k ] = is_scalar( $v ) || null === $v ? $v : null; } } return $out; }; return (string) wp_json_encode( $walk( $decoded ) ); } } return self::kses_import_html( $value ); } /** * wp_kses_post() after removing