| @@ -49,31 +49,59 @@ | ||
| 49 | 49 | }//end foreach |
| 50 | 50 | return $sanitized_payload; |
| 51 | 51 | } |
| 52 | 52 | public static function sanitize_json_form_data( $data, $schema = [] ) { |
| 53 | - $data = is_array( $data ) ? $data : json_decode( stripslashes( $data ) ); | |
| 54 | - if ( is_array( $data ) ) { | |
| 55 | - $results = []; | |
| 56 | - $has_schema = count( $schema ); | |
| 57 | - foreach ( $data as $key => $value ) { | |
| 58 | - if ( $has_schema && ! isset( $schema[ $key ] ) ) { | |
| 59 | - continue; | |
| 60 | - } | |
| 61 | - if ( is_array( $value ) || is_object( $value ) ) { | |
| 62 | - $value = (array) $value; | |
| 63 | - $child_array = []; | |
| 64 | - foreach ( $value as $child_key => $child_value ) { | |
| 65 | - $child_array[ sanitize_key( $child_key ) ] = sanitize_text_field( $child_value ); | |
| 53 | + $data = is_array( $data ) ? $data : json_decode( stripslashes( $data ), true ); | |
| 54 | + | |
| 55 | + if ( ! is_array( $data ) ) { | |
| 56 | + return self::cast_value_type( sanitize_text_field( $data ) ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + $sanitize_recursive = function ( $item, $child_schema = [] ) use ( &$sanitize_recursive ) { | |
| 60 | + if ( is_array( $item ) ) { | |
| 61 | + $result = []; | |
| 62 | + foreach ( $item as $key => $value ) { | |
| 63 | + $sanitized_key = sanitize_text_field( $key ); | |
| 64 | + | |
| 65 | + if ( is_array( $value ) || is_object( $value ) ) { | |
| 66 | + // Recursively sanitize nested arrays/objects | |
| 67 | + $result[ $sanitized_key ] = $sanitize_recursive( (array) $value, $child_schema[ $sanitized_key ]['schema'] ?? [] ); | |
| 68 | + } else { | |
| 69 | + // sanitize value and auto-cast type | |
| 70 | + $clean_value = sanitize_text_field( $value ); | |
| 71 | + $result[ $sanitized_key ] = self::cast_value_type( $clean_value ); | |
| 66 | 72 | } |
| 67 | - $results[] = $child_array; | |
| 68 | - } else { | |
| 69 | - $results[ sanitize_key( $key ) ] = sanitize_text_field( $value ); | |
| 70 | 73 | } |
| 74 | + return $result; | |
| 71 | 75 | } |
| 72 | - return $results; | |
| 76 | + | |
| 77 | + return self::cast_value_type( sanitize_text_field( $item ) ); | |
| 78 | + }; | |
| 79 | + | |
| 80 | + return $sanitize_recursive( $data, $schema ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + private static function cast_value_type( $value ) { | |
| 84 | + // Check for boolean | |
| 85 | + if ( is_string( $value ) ) { | |
| 86 | + $lower = strtolower( $value ); | |
| 87 | + if ( $lower === 'true' ) { | |
| 88 | + return true; | |
| 89 | + } | |
| 90 | + if ( $lower === 'false' ) { | |
| 91 | + return false; | |
| 92 | + } | |
| 73 | 93 | } |
| 74 | - return sanitize_text_field( $data ); | |
| 94 | + | |
| 95 | + // Check for numeric | |
| 96 | + if ( is_numeric( $value ) ) { | |
| 97 | + return strpos( $value, '.' ) !== false ? (float) $value : (int) $value; | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Default: string | |
| 101 | + return (string) $value; | |
| 75 | 102 | } |
| 103 | + | |
| 76 | 104 | public static function sanitize_array_field( $array_data ) { |
| 77 | 105 | $array_data = is_array( $array_data ) ? $array_data : json_decode( stripslashes( $array_data ) ); |
| 78 | 106 | $boolean = [ 'true', 'false', '1', '0' ]; |
| 79 | 107 | if ( is_array( $array_data ) ) { |