PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.2
Elementor Website Builder – more than just a page builder v4.1.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / app / modules / onboarding / validation / base-validator.php

base-validator.php in Elementor Website Builder – more than just a page builder 4.1.2, at app/modules/onboarding/validation/base-validator.php

166 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\Onboarding\Validation;
4
5 use WP_Error;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 abstract class Base_Validator {
12
13 protected array $errors = [];
14
15 abstract protected function get_rules(): array;
16
17 public function validate( array $params ) {
18 if ( ! is_array( $params ) ) {
19 return new WP_Error( 'invalid_params', 'Parameters must be an array.', [ 'status' => 400 ] );
20 }
21
22 $this->errors = [];
23 $validated = [];
24
25 foreach ( $this->get_rules() as $field => $rule ) {
26 if ( ! array_key_exists( $field, $params ) ) {
27 continue;
28 }
29
30 $result = $this->validate_field( $field, $params[ $field ], $rule );
31
32 if ( is_wp_error( $result ) ) {
33 return $result;
34 }
35
36 $validated[ $field ] = $result;
37 }
38
39 return $validated;
40 }
41
42 protected function validate_field( string $field, $value, array $rule ) {
43 $type = $rule['type'] ?? 'string';
44 $nullable = $rule['nullable'] ?? false;
45
46 if ( null === $value ) {
47 if ( $nullable ) {
48 return null;
49 }
50
51 return $this->error( $field, "{$field} cannot be null." );
52 }
53
54 switch ( $type ) {
55 case 'string':
56 return $this->validate_string( $field, $value );
57 case 'int':
58 return $this->validate_int( $field, $value );
59 case 'bool':
60 return $this->validate_bool( $field, $value );
61 case 'array':
62 return $this->validate_array( $field, $value, $rule );
63 case 'string_array':
64 return $this->validate_string_array( $field, $value );
65 case 'custom_data':
66 return $this->validate_custom_data( $field, $value );
67 default:
68 return $value;
69 }
70 }
71
72 protected function validate_string( string $field, $value ) {
73 if ( ! is_string( $value ) ) {
74 return $this->error( $field, "{$field} must be a string." );
75 }
76
77 return sanitize_text_field( $value );
78 }
79
80 protected function validate_int( string $field, $value ) {
81 if ( ! is_numeric( $value ) ) {
82 return $this->error( $field, "{$field} must be a number." );
83 }
84
85 return (int) $value;
86 }
87
88 protected function validate_bool( string $field, $value ) {
89 if ( ! is_bool( $value ) ) {
90 return $this->error( $field, "{$field} must be a boolean." );
91 }
92
93 return $value;
94 }
95
96 protected function validate_array( string $field, $value, array $rule ) {
97 if ( ! is_array( $value ) ) {
98 return $this->error( $field, "{$field} must be an array." );
99 }
100
101 $allowed = $rule['allowed'] ?? null;
102
103 if ( $allowed && ! in_array( $value, $allowed, true ) ) {
104 return $this->error( $field, "{$field} contains invalid value." );
105 }
106
107 return $value;
108 }
109
110 protected function validate_string_array( string $field, $value ) {
111 if ( ! is_array( $value ) ) {
112 return $this->error( $field, "{$field} must be an array." );
113 }
114
115 return array_values(
116 array_filter(
117 array_map(
118 static function ( $item ) {
119 return is_string( $item ) ? sanitize_text_field( $item ) : null;
120 },
121 $value
122 ),
123 static function ( $item ) {
124 return null !== $item;
125 }
126 )
127 );
128 }
129
130 protected function validate_custom_data( string $field, $value ) {
131 if ( ! is_array( $value ) ) {
132 return $this->error( $field, "{$field} must be an array." );
133 }
134
135 return $this->sanitize_recursive( $value );
136 }
137
138 protected function sanitize_recursive( array $data ): array {
139 $sanitized = [];
140
141 foreach ( $data as $key => $value ) {
142 $safe_key = sanitize_key( $key );
143
144 if ( is_string( $value ) ) {
145 $sanitized[ $safe_key ] = sanitize_text_field( $value );
146 } elseif ( is_array( $value ) ) {
147 $sanitized[ $safe_key ] = $this->sanitize_recursive( $value );
148 } elseif ( is_numeric( $value ) || is_bool( $value ) || null === $value ) {
149 $sanitized[ $safe_key ] = $value;
150 } else {
151 $sanitized[ $safe_key ] = null;
152 }
153 }
154
155 return $sanitized;
156 }
157
158 protected function error( string $field, string $message ): WP_Error {
159 return new WP_Error(
160 'invalid_' . $field,
161 $message,
162 [ 'status' => 400 ]
163 );
164 }
165 }
166