PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / sanitizer.php

sanitizer.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/classes/sanitizer.php

123 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Classes;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class Sanitizer {
10 public static function sanitize_payload( array $schema, array $payload ): array {
11 $sanitized_payload = [];
12
13 foreach ( $schema as $key => $type ) {
14 if ( array_key_exists( $key, $payload ) ) {
15 if ( is_array( $type ) ) {
16 if ( is_string( $payload[ $key ] ) ) {
17 $payload[ $key ] = json_decode( $payload[ $key ], true );
18 }
19
20 if ( ! is_array( $payload[ $key ] ) ) {
21 continue;
22 }
23
24 $sanitized_payload[ $key ] = self::sanitize_payload( $type, $payload[ $key ] );
25 } else {
26 $value = is_null( $payload[ $key ] ) ? '' : $payload[ $key ];
27 if ( str_contains( $type, '|' ) ) {
28 list( $data_type, $type ) = explode( '|', $type, 2 );
29 if ( 'array' === $data_type ) {
30 $value = json_decode( wp_unslash( $value ), true );
31 }
32 if ( 'json' === $data_type ) {
33 $value = json_decode( wp_unslash( $value ) );
34 }
35 }
36
37 $sanitize_value = function ( $value ) use ( $type ) {
38 switch ( strtolower( $type ) ) {
39 case 'id':
40 return absint( sanitize_text_field( $value ) );
41 case 'integer':
42 return intval( sanitize_text_field( $value ) );
43 case 'float':
44 return floatval( sanitize_text_field( $value ) );
45 case 'url':
46 return esc_url_raw( $value );
47 case 'boolean':
48 return (bool) filter_var( sanitize_text_field( $value ), FILTER_VALIDATE_BOOLEAN );
49 case 'post':
50 return wp_kses_post( $value );
51 case 'email':
52 return sanitize_email( $value );
53 case 'string':
54 return sanitize_text_field( $value );
55 default:
56 if ( is_array( $value ) ) {
57 return wp_kses_post_deep( $value );
58 }
59
60 return wp_kses_post( trim( stripslashes( $value ) ) );
61 }
62 };
63
64 if ( is_array( $value ) || is_object( $value ) ) {
65 $sanitized_payload[ $key ] = map_deep( $value, $sanitize_value );
66 } else {
67 $sanitized_payload[ $key ] = $sanitize_value( $value );
68 }
69 }
70 }
71 }
72
73 return $sanitized_payload;
74 }
75
76 public static function sanitize_json_form_data( $data, $schema = [] ) {
77 $data = is_array( $data ) ? $data : json_decode( stripslashes( $data ) );
78 if ( is_array( $data ) ) {
79 $results = [];
80 $has_schema = count( $schema );
81 foreach ( $data as $key => $value ) {
82 if ( $has_schema && ! isset( $schema[ $key ] ) ) {
83 continue;
84 }
85 if ( is_array( $value ) || is_object( $value ) ) {
86 $value = (array) $value;
87 $child_array = [];
88 foreach ( $value as $child_key => $child_value ) {
89 $child_array[ sanitize_key( $child_key ) ] = sanitize_text_field( $child_value );
90 }
91 $results[] = $child_array;
92 } else {
93 $results[ sanitize_key( $key ) ] = sanitize_text_field( $value );
94 }
95 }
96
97 return $results;
98 }
99
100 return sanitize_text_field( $data );
101 }
102
103 public static function sanitize_array_field( $array_data ) {
104 $array_data = is_array( $array_data ) ? $array_data : json_decode( stripslashes( $array_data ) );
105 $boolean = [ 'true', 'false', '1', '0' ];
106 if ( is_array( $array_data ) ) {
107 foreach ( $array_data as $key => &$value ) {
108 if ( is_array( $value ) ) {
109 $value = self::sanitize_array_field( $value );
110 } else {
111 $value = in_array( $value, $boolean, true ) || is_bool( $value ) ? self::sanitize_checkbox_field( $value ) : sanitize_text_field( $value );
112 }
113 }
114 }
115
116 return $array_data;
117 }
118
119 public static function sanitize_checkbox_field( $boolean ) {
120 return (bool) filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN );
121 }
122 }
123