PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.8.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.8.0
2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 All 78 releases
ablocks / includes / classes / sanitizer.php

sanitizer.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.8.0, at includes/classes/sanitizer.php

122 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Classes;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 class Sanitizer {
9 public static function sanitize_payload( $schema, $payload ) {
10 $sanitized_payload = array();
11 foreach ( $schema as $key => $type ) {
12 if ( isset( $payload[ $key ] ) ) {
13 switch ( $type ) {
14 case 'integer':
15 $sanitized_payload[ $key ] = (int) absint( sanitize_text_field( $payload[ $key ] ) );
16 break;
17 case 'float':
18 $sanitized_payload[ $key ] = (float) floatval( sanitize_text_field( $payload[ $key ] ) );
19 break;
20 case 'string':
21 $sanitized_payload[ $key ] = (string) sanitize_text_field( $payload[ $key ] );
22 break;
23 case 'url':
24 $sanitized_payload[ $key ] = esc_url_raw( $payload[ $key ] );
25 break;
26 case 'email':
27 $sanitized_payload[ $key ] = sanitize_email( $payload[ $key ] );
28 break;
29 case 'textarea':
30 $sanitized_payload[ $key ] = sanitize_textarea_field( $payload[ $key ] );
31 break;
32 case 'boolean':
33 $sanitized_payload[ $key ] = self::sanitize_checkbox_field( $payload[ $key ] );
34 break;
35 case 'array':
36 $sanitized_payload[ $key ] = self::sanitize_array_field( $payload[ $key ] );
37 break;
38 case 'json':
39 $sanitized_payload[ $key ] = self::sanitize_json_form_data( $payload[ $key ] );
40 break;
41 case 'post':
42 $sanitized_payload[ $key ] = wp_kses_post( $payload[ $key ] );
43 break;
44 default:
45 $sanitized_payload[ $key ] = sanitize_text_field( $payload[ $key ] );
46 break;
47 }//end switch
48 }//end if
49 }//end foreach
50 return $sanitized_payload;
51 }
52 public static function sanitize_json_form_data( $data, $schema = [] ) {
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 );
72 }
73 }
74 return $result;
75 }
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 }
93 }
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;
102 }
103
104 public static function sanitize_array_field( $array_data ) {
105 $array_data = is_array( $array_data ) ? $array_data : json_decode( stripslashes( $array_data ) );
106 $boolean = [ 'true', 'false', '1', '0' ];
107 if ( is_array( $array_data ) ) {
108 foreach ( $array_data as $key => &$value ) {
109 if ( is_array( $value ) ) {
110 $value = self::sanitize_array_field( $value );
111 } else {
112 $value = in_array( $value, $boolean, true ) || is_bool( $value ) ? self::sanitize_checkbox_field( $value ) : sanitize_text_field( $value );
113 }
114 }
115 }
116 return $array_data;
117 }
118 public static function sanitize_checkbox_field( $boolean ) {
119 return (bool) filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN );
120 }
121 }
122