/
sanitizer.php
279 lines (251 loc) · 8.75 KB
/
sanitizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Sanitize field value before saving.
*
* @package Meta Box
*/
/**
* Sanitize class.
*/
class RWMB_Sanitizer {
/**
* Register hook to sanitize field value.
*/
public function init() {
add_filter( 'rwmb_sanitize', array( $this, 'sanitize' ), 10, 4 );
}
/**
* Sanitize a field value.
*
* @param mixed $value The submitted new value.
* @param array $field The field settings.
* @param mixed $old_value The old field value in the database.
* @param int $object_id The object ID.
*/
public function sanitize( $value, $field, $old_value = null, $object_id = null ) {
// Allow developers to bypass the sanitization.
if ( 'none' === $field['sanitize_callback'] ) {
return $value;
}
$callback = $this->get_callback( $field );
return is_callable( $callback ) ? call_user_func( $callback, $value, $field, $old_value, $object_id ) : $value;
}
/**
* Get sanitize callback for a field.
*
* @param array $field Field settings.
* @return callable
*/
private function get_callback( $field ) {
// User-defined callback.
if ( is_callable( $field['sanitize_callback'] ) ) {
return $field['sanitize_callback'];
}
$callbacks = array(
'autocomplete' => array( $this, 'sanitize_choice' ),
'background' => array( $this, 'sanitize_background' ),
'button_group' => array( $this, 'sanitize_choice' ),
'checkbox' => array( $this, 'sanitize_checkbox' ),
'checkbox_list' => array( $this, 'sanitize_choice' ),
'color' => array( $this, 'sanitize_color' ),
'date' => array( $this, 'sanitize_datetime' ),
'datetime' => array( $this, 'sanitize_datetime' ),
'email' => 'sanitize_email',
'fieldset_text' => array( $this, 'sanitize_text' ),
'file' => array( $this, 'sanitize_file' ),
'file_advanced' => array( $this, 'sanitize_object' ),
'file_input' => array( $this, 'sanitize_url' ),
'file_upload' => array( $this, 'sanitize_object' ),
'hidden' => 'sanitize_text_field',
'image' => array( $this, 'sanitize_file' ),
'image_advanced' => array( $this, 'sanitize_object' ),
'image_select' => array( $this, 'sanitize_choice' ),
'image_upload' => array( $this, 'sanitize_object' ),
'key_value' => array( $this, 'sanitize_text' ),
'map' => array( $this, 'sanitize_map' ),
'number' => array( $this, 'sanitize_number' ),
'oembed' => array( $this, 'sanitize_url' ),
'osm' => array( $this, 'sanitize_map' ),
'password' => 'sanitize_text_field',
'post' => array( $this, 'sanitize_object' ),
'radio' => array( $this, 'sanitize_choice' ),
'range' => array( $this, 'sanitize_number' ),
'select' => array( $this, 'sanitize_choice' ),
'select_advanced' => array( $this, 'sanitize_choice' ),
'sidebar' => array( $this, 'sanitize_text' ),
'single_image' => 'absint',
'slider' => array( $this, 'sanitize_slider' ),
'switch' => array( $this, 'sanitize_checkbox' ),
'taxonomy' => array( $this, 'sanitize_object' ),
'taxonomy_advanced' => array( $this, 'sanitize_taxonomy_advanced' ),
'text' => 'sanitize_text_field',
'text_list' => array( $this, 'sanitize_text' ),
'textarea' => 'wp_kses_post',
'time' => 'sanitize_text_field',
'url' => array( $this, 'sanitize_url' ),
'user' => array( $this, 'sanitize_object' ),
'video' => array( $this, 'sanitize_object' ),
'wysiwyg' => 'wp_kses_post',
);
$type = $field['type'];
return isset( $callbacks[ $type ] ) ? $callbacks[ $type ] : null;
}
/**
* Set the value of checkbox to 1 or 0 instead of 'checked' and empty string.
* This prevents using default value once the checkbox has been unchecked.
*
* @link https://github.com/rilwis/meta-box/issues/6
* @param string $value Checkbox value.
* @return int
*/
private function sanitize_checkbox( $value ) {
return (int) ! empty( $value );
}
/**
* Sanitize numeric value.
*
* @param string $value The number value.
* @return string
*/
private function sanitize_number( $value ) {
return is_numeric( $value ) ? $value : '';
}
/**
* Sanitize color value.
*
* @param string $value The color value.
* @return string
*/
private function sanitize_color( $value ) {
if ( false === strpos( $value, 'rgba' ) ) {
return sanitize_hex_color( $value );
}
// rgba value.
$red = '';
$green = '';
$blue = '';
$alpha = '';
sscanf( $value, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')';
}
/**
* Sanitize value for a choice field.
*
* @param string|array $value The submitted value.
* @param array $field The field settings.
* @return string|array
*/
private function sanitize_choice( $value, $field ) {
$options = RWMB_Choice_Field::transform_options( $field['options'] );
$options = wp_list_pluck( $options, 'value' );
return is_array( $value ) ? array_intersect( $value, $options ) : ( in_array( $value, $options ) ? $value : '' );
}
/**
* Sanitize object & media field.
*
* @param int|array $value The submitted value.
* @return int|array
*/
private function sanitize_object( $value ) {
return is_array( $value ) ? array_filter( array_map( 'absint', $value ) ) : ( $value ? absint( $value ) : '' );
}
/**
* Sanitize background field.
*
* @param array $value The submitted value.
* @return array
*/
private function sanitize_background( $value ) {
$value = wp_parse_args(
$value,
array(
'color' => '',
'image' => '',
'repeat' => '',
'attachment' => '',
'position' => '',
'size' => '',
)
);
$value['color'] = $this->sanitize_color( $value['color'] );
$value['image'] = esc_url_raw( $value['image'] );
$value['repeat'] = in_array( $value['repeat'], array( 'no-repeat', 'repeat', 'repeat-x', 'repeat-y', 'inherit' ), true ) ? $value['repeat'] : '';
$value['position'] = in_array( $value['position'], array( 'top left', 'top center', 'top right', 'center left', 'center center', 'center right', 'bottom left', 'bottom center', 'bottom right' ), true ) ? $value['position'] : '';
$value['attachment'] = in_array( $value['attachment'], array( 'fixed', 'scroll', 'inherit' ), true ) ? $value['attachment'] : '';
$value['size'] = in_array( $value['size'], array( 'inherit', 'cover', 'contain' ), true ) ? $value['size'] : '';
return $value;
}
/**
* Sanitize text field.
*
* @param string|array $value The submitted value.
* @return string|array
*/
private function sanitize_text( $value ) {
return is_array( $value ) ? array_map( __METHOD__, $value ) : sanitize_text_field( $value );
}
/**
* Sanitize file, image field.
*
* @param array $value The submitted value.
* @param array $field The field settings.
* @return array
*/
private function sanitize_file( $value, $field ) {
return $field['upload_dir'] ? array_map( 'esc_url_raw', $value ) : $this->sanitize_object( $value );
}
/**
* Sanitize slider field.
*
* @param mixed $value The submitted value.
* @param array $field The field settings.
* @return string|int|float
*/
private function sanitize_slider( $value, $field ) {
return true === $field['js_options']['range'] ? sanitize_text_field( $value ) : $this->sanitize_number( $value );
}
/**
* Sanitize datetime field.
*
* @param mixed $value The submitted value.
* @param array $field The field settings.
* @return float|string
*/
private function sanitize_datetime( $value, $field ) {
return $field['timestamp'] ? floor( abs( (float) $value ) ) : sanitize_text_field( $value );
}
/**
* Sanitize map field.
*
* @param mixed $value The submitted value.
* @return string
*/
private function sanitize_map( $value ) {
$value = sanitize_text_field( $value );
list( $latitude, $longitude, $zoom ) = explode( ',', $value . ',,' );
$latitude = (float) $latitude;
$longitude = (float) $longitude;
$zoom = (int) $zoom;
return "$latitude,$longitude,$zoom";
}
/**
* Sanitize taxonomy advanced field.
*
* @param mixed $value The submitted value.
* @return string
*/
private function sanitize_taxonomy_advanced( $value ) {
$value = RWMB_Helpers_Array::from_csv( $value );
$value = array_filter( array_map( 'absint', $value ) );
return implode( ',', $value );
}
/**
* Sanitize URL field.
*
* @param string $value The submitted value.
* @return string
*/
private function sanitize_url( $value ) {
return esc_url_raw( $value );
}
}