forked from CMB2/CMB2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMB2_Sanitize.php
376 lines (324 loc) · 9.97 KB
/
CMB2_Sanitize.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* CMB field validation
* @since 0.0.4
* @method string _id()
*/
class CMB2_Sanitize {
/**
* A CMB field object
* @var CMB2_Field object
*/
public $field;
/**
* Field's $_POST value
* @var mixed
*/
public $value;
/**
* Setup our class vars
* @since 1.1.0
* @param CMB2_Field $field A CMB field object
* @param mixed $value Field value
*/
public function __construct( CMB2_Field $field, $value ) {
$this->field = $field;
$this->value = stripslashes_deep( $value ); // get rid of those evil magic quotes
}
/**
* Catchall method if field's 'sanitization_cb' is NOT defined, or field type does not have a corresponding validation method
* @since 1.0.0
* @param string $name Non-existent method name
* @param array $arguments All arguments passed to the method
*/
public function __call( $name, $arguments ) {
list( $value ) = $arguments;
return $this->default_sanitization( $value );
}
/**
* Default fallback sanitization method. Applies filters.
* @since 1.0.2
* @param mixed $value Meta value
*/
public function default_sanitization( $value ) {
/**
* Filter the value before it is saved.
*
* The dynamic portion of the hook name, $this->field->type(), refers to the field type.
*
* Passing a non-null value to the filter will short-circuit saving
* the field value, saving the passed value instead.
*
* @param bool|mixed $override_value Sanitization/Validation override value to return.
* Default false to skip it.
* @param mixed $value The value to be saved to this field.
* @param int $object_id The ID of the object where the value will be saved
* @param array $field_args The current field's arguments
* @param object $sanitizer This `CMB2_Sanitize` object
*/
$override_value = apply_filters( "cmb2_sanitize_{$this->field->type()}", null, $value, $this->field->object_id, $this->field->args(), $this );
/**
* DEPRECATED. See documentation above.
*/
$override_value = apply_filters( "cmb2_validate_{$this->field->type()}", $override_value, $value, $this->field->object_id, $this->field->args(), $this );
if ( null !== $override_value ) {
return $override_value;
}
switch ( $this->field->type() ) {
case 'wysiwyg':
// $value = wp_kses( $value );
// break;
case 'textarea_small':
return $this->textarea( $value );
case 'taxonomy_select':
case 'taxonomy_radio':
case 'taxonomy_multicheck':
if ( $this->field->args( 'taxonomy' ) ) {
return wp_set_object_terms( $this->field->object_id, $value, $this->field->args( 'taxonomy' ) );
}
case 'multicheck':
case 'file_list':
case 'oembed':
// no filtering
return $value;
default:
// Handle repeatable fields array
// We'll fallback to 'sanitize_text_field'
return is_array( $value ) ? array_map( 'sanitize_text_field', $value ) : call_user_func( 'sanitize_text_field', $value );
}
}
/**
* Simple checkbox validation
* @since 1.0.1
* @param mixed $value 'on' or false
* @return string|false 'on' or false
*/
public function checkbox( $value ) {
return $value === 'on' ? 'on' : false;
}
/**
* Validate url in a meta value
* @since 1.0.1
* @param string $value Meta value
* @return string Empty string or escaped url
*/
public function text_url( $value ) {
$protocols = $this->field->args( 'protocols' );
// for repeatable
if ( is_array( $value ) ) {
foreach ( $value as $key => $val ) {
$value[ $key ] = $val ? esc_url_raw( $val, $protocols ) : $this->field->args( 'default' );
}
} else {
$value = $value ? esc_url_raw( $value, $protocols ) : $this->field->args( 'default' );
}
return $value;
}
public function colorpicker( $value ) {
// for repeatable
if ( is_array( $value ) ) {
$check = $value;
$value = array();
foreach ( $check as $key => $val ) {
if ( $val && '#' != $val ) {
$value[ $key ] = esc_attr( $val );
}
}
} else {
$value = ! $value || '#' == $value ? '' : esc_attr( $value );
}
return $value;
}
/**
* Validate email in a meta value
* @since 1.0.1
* @param string $value Meta value
* @return string Empty string or sanitized email
*/
public function text_email( $value ) {
// for repeatable
if ( is_array( $value ) ) {
foreach ( $value as $key => $val ) {
$val = trim( $val );
$value[ $key ] = is_email( $val ) ? $val : '';
}
} else {
$value = trim( $value );
$value = is_email( $value ) ? $value : '';
}
return $value;
}
/**
* Validate money in a meta value
* @since 1.0.1
* @param string $value Meta value
* @return string Empty string or sanitized money value
*/
public function text_money( $value ) {
global $wp_locale;
$search = array( $wp_locale->number_format['thousands_sep'], $wp_locale->number_format['decimal_point'] );
$replace = array( '', '.' );
// for repeatable
if ( is_array( $value ) ) {
foreach ( $value as $key => $val ) {
$value[ $key ] = number_format_i18n( (float) str_ireplace( $search, $replace, $val ), 2 );
}
} else {
$value = number_format_i18n( (float) str_ireplace( $search, $replace, $value ), 2 );
}
return $value;
}
/**
* Converts text date to timestamp
* @since 1.0.2
* @param string $value Meta value
* @return string Timestring
*/
public function text_date_timestamp( $value ) {
return is_array( $value ) ? array_map( 'strtotime', $value ) : strtotime( $value );
}
/**
* Datetime to timestamp
* @since 1.0.1
* @param string $value Meta value
* @return string Timestring
*/
public function text_datetime_timestamp( $value, $repeat = false ) {
$test = is_array( $value ) ? array_filter( $value ) : '';
if ( empty( $test ) ) {
return '';
}
if ( $repeat_value = $this->_check_repeat( $value, __FUNCTION__, $repeat ) ) {
return $repeat_value;
}
$value = strtotime( $value['date'] . ' ' . $value['time'] );
if ( $tz_offset = $this->field->field_timezone_offset() ) {
$value += $tz_offset;
}
return $value;
}
/**
* Datetime to imestamp with timezone
* @since 1.0.1
* @param string $value Meta value
* @return string Timestring
*/
public function text_datetime_timestamp_timezone( $value, $repeat = false ) {
$test = is_array( $value ) ? array_filter( $value ) : '';
if ( empty( $test ) ) {
return '';
}
if ( $repeat_value = $this->_check_repeat( $value, __FUNCTION__, $repeat ) ) {
return $repeat_value;
}
$tzstring = null;
if ( is_array( $value ) && array_key_exists( 'timezone', $value ) ) {
$tzstring = $value['timezone'];
}
if ( empty( $tzstring ) ) {
$tzstring = cmb2_utils()->timezone_string();
}
$offset = cmb2_utils()->timezone_offset( $tzstring, true );
if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
$tzstring = timezone_name_from_abbr( '', $offset, 0 );
}
$value = new DateTime( $value['date'] . ' ' . $value['time'], new DateTimeZone( $tzstring ) );
$value = serialize( $value );
return $value;
}
/**
* Sanitize textareas and wysiwyg fields
* @since 1.0.1
* @param string $value Meta value
* @return string Sanitized data
*/
public function textarea( $value ) {
return is_array( $value ) ? array_map( 'wp_kses_post', $value ) : wp_kses_post( $value );
}
/**
* Sanitize code textareas
* @since 1.0.2
* @param string $value Meta value
* @return string Sanitized data
*/
public function textarea_code( $value, $repeat = false ) {
if ( $repeat_value = $this->_check_repeat( $value, __FUNCTION__, $repeat ) ) {
return $repeat_value;
}
return htmlspecialchars_decode( stripslashes( $value ) );
}
/**
* Peforms saving of `file` attachement's ID
* @since 1.1.0
* @param string $value File url
*/
public function _save_file_id( $value ) {
$group = $this->field->group;
$args = $this->field->args();
$args['id'] = $args['_id'] . '_id';
unset( $args['_id'], $args['_name'] );
// And get new field object
$field = new CMB2_Field( array(
'field_args' => $args,
'group_field' => $group,
'object_id' => $this->field->object_id,
'object_type' => $this->field->object_type,
) );
$id_key = $field->_id();
$id_val_old = $field->escaped_value( 'absint' );
if ( $group ) {
// Check group $_POST data
$i = $group->index;
$base_id = $group->_id();
$id_val = isset( $_POST[ $base_id ][ $i ][ $id_key ] ) ? absint( $_POST[ $base_id ][ $i ][ $id_key ] ) : 0;
} else {
// Check standard $_POST data
$id_val = isset( $_POST[ $field->id() ] ) ? $_POST[ $field->id() ] : null;
}
// If there is no ID saved yet, try to get it from the url
if ( $value && ! $id_val ) {
$id_val = cmb2_utils()->image_id_from_url( $value );
}
if ( $group ) {
return array(
'attach_id' => $id_val,
'field_id' => $id_key,
);
}
if ( $id_val && $id_val != $id_val_old ) {
return $field->update_data( $id_val );
} elseif ( empty( $id_val ) && $id_val_old ) {
return $field->remove_data( $id_val_old );
}
}
/**
* Handles saving of attachment post ID and sanitizing file url
* @since 1.1.0
* @param string $value File url
* @return string Sanitized url
*/
public function file( $value ) {
$id_value = $this->_save_file_id( $value );
$clean = $this->text_url( $value );
// Return an array with url/id if saving a group field
return $this->field->group ? array_merge( array( 'url' => $clean), $id_value ) : $clean;
}
/**
* If repeating, loop through and re-apply sanitization method
* @since 1.1.0
* @param mixed $value Meta value
* @param string $method Class method
* @param bool $repeat Whether repeating or not
* @return mixed Sanitized value
*/
public function _check_repeat( $value, $method, $repeat ) {
if ( $repeat || ! $this->field->args( 'repeatable' ) ) {
return;
}
$new_value = array();
foreach ( $value as $iterator => $val ) {
$new_value[] = $this->$method( $val, true );
}
return $new_value;
}
}