PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 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 All 80 releases
ablocks / includes / classes / sanitizer.php

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

94 lines 3.1 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 ) );
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 );
66 }
67 $results[] = $child_array;
68 } else {
69 $results[ sanitize_key( $key ) ] = sanitize_text_field( $value );
70 }
71 }
72 return $results;
73 }
74 return sanitize_text_field( $data );
75 }
76 public static function sanitize_array_field( $array_data ) {
77 $array_data = is_array( $array_data ) ? $array_data : json_decode( stripslashes( $array_data ) );
78 $boolean = [ 'true', 'false', '1', '0' ];
79 if ( is_array( $array_data ) ) {
80 foreach ( $array_data as $key => &$value ) {
81 if ( is_array( $value ) ) {
82 $value = self::sanitize_array_field( $value );
83 } else {
84 $value = in_array( $value, $boolean, true ) || is_bool( $value ) ? self::sanitize_checkbox_field( $value ) : sanitize_text_field( $value );
85 }
86 }
87 }
88 return $array_data;
89 }
90 public static function sanitize_checkbox_field( $boolean ) {
91 return (bool) filter_var( sanitize_text_field( $boolean ), FILTER_VALIDATE_BOOLEAN );
92 }
93 }
94