PluginProbe
Virtue/Ascend/Pinnacle Toolkit / 3.1
Virtue/Ascend/Pinnacle Toolkit v3.1
4.9.12.2 trunk 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.7 All 48 releases
virtue-toolkit / cmb / helpers / cmb_Meta_Box_Sanitize.php

cmb_Meta_Box_Sanitize.php in Virtue/Ascend/Pinnacle Toolkit 3.1, at cmb/helpers/cmb_Meta_Box_Sanitize.php

347 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * CMB field validation
5 * @since 0.0.4
6 */
7 class cmb_Meta_Box_Sanitize {
8
9 /**
10 * A CMB field object
11 * @var cmb_Meta_Box_field object
12 */
13 public $field;
14
15 /**
16 * Field's $_POST value
17 * @var mixed
18 */
19 public $value;
20
21 /**
22 * Setup our class vars
23 * @since 1.1.0
24 * @param object $field A CMB field object
25 * @param mixed $value Field value
26 */
27 public function __construct( $field, $value ) {
28 $this->field = $field;
29 $this->value = $value;
30 $this->object_id = cmb_Meta_Box::get_object_id();
31 $this->object_type = cmb_Meta_Box::get_object_type();
32 }
33
34 /**
35 * Catchall method if field's 'sanitization_cb' is NOT defined, or field type does not have a corresponding validation method
36 * @since 1.0.0
37 * @param string $name Non-existent method name
38 * @param array $arguments All arguments passed to the method
39 */
40 public function __call( $name, $arguments ) {
41 list( $value ) = $arguments;
42 return $this->default_sanitization( $value );
43 }
44
45 /**
46 * Default fallback sanitization method. Applies filters.
47 * @since 1.0.2
48 * @param mixed $value Meta value
49 */
50 public function default_sanitization( $value ) {
51
52 // Allow field type validation via filter
53 $updated = apply_filters( 'cmb_validate_'. $this->field->type(), null, $value, $this->object_id, $this->field->args(), $this );
54
55 if ( null !== $updated )
56 return $updated;
57
58 switch ( $this->field->type() ) {
59 case 'wysiwyg':
60 // $value = wp_kses( $value );
61 // break;
62 case 'textarea_small':
63 return $this->textarea( $value );
64 case 'taxonomy_select':
65 case 'taxonomy_radio':
66 case 'taxonomy_multicheck':
67 if ( $this->field->args( 'taxonomy' ) ) {
68 return wp_set_object_terms( $this->object_id, $value, $this->field->args( 'taxonomy' ) );
69 }
70 case 'multicheck':
71 case 'file_list':
72 case 'oembed':
73 // no filtering
74 return $value;
75 default:
76 // Handle repeatable fields array
77 // We'll fallback to 'sanitize_text_field'
78 return is_array( $value ) ? array_map( 'sanitize_text_field', $value ) : call_user_func( 'sanitize_text_field', $value );
79 }
80 }
81
82 /**
83 * Simple checkbox validation
84 * @since 1.0.1
85 * @param mixed $val 'on' or false
86 * @return mixed 'on' or false
87 */
88 public function checkbox( $value ) {
89 return $value === 'on' ? 'on' : false;
90 }
91
92 /**
93 * Validate url in a meta value
94 * @since 1.0.1
95 * @param string $value Meta value
96 * @return string Empty string or escaped url
97 */
98 public function text_url( $value ) {
99 $protocols = $this->field->args( 'protocols' );
100 // for repeatable
101 if ( is_array( $value ) ) {
102 foreach ( $value as $key => $val ) {
103 $value[ $key ] = $val ? esc_url_raw( $val, $protocols ) : $this->field->args( 'default' );
104 }
105 } else {
106 $value = $value ? esc_url_raw( $value, $protocols ) : $this->field->args( 'default' );
107 }
108
109 return $value;
110 }
111
112 public function colorpicker( $value ) {
113 // for repeatable
114 if ( is_array( $value ) ) {
115 $check = $value;
116 $value = array();
117 foreach ( $check as $key => $val ) {
118 if ( $val && '#' != $val ) {
119 $value[ $key ] = esc_attr( $val );
120 }
121 }
122 } else {
123 $value = ! $value || '#' == $value ? '' : esc_attr( $value );
124 }
125 return $value;
126 }
127
128 /**
129 * Validate email in a meta value
130 * @since 1.0.1
131 * @param string $value Meta value
132 * @return string Empty string or validated email
133 */
134 public function text_email( $value ) {
135 // for repeatable
136 if ( is_array( $value ) ) {
137 foreach ( $value as $key => $val ) {
138 $val = trim( $val );
139 $value[ $key ] = is_email( $val ) ? $val : '';
140 }
141 } else {
142 $value = trim( $value );
143 $value = is_email( $value ) ? $value : '';
144 }
145
146 return $value;
147 }
148
149 /**
150 * Validate money in a meta value
151 * @since 1.0.1
152 * @param string $value Meta value
153 * @return string Empty string or validated money value
154 */
155 public function text_money( $value ) {
156
157 global $wp_locale;
158
159 $search = array( $wp_locale->number_format['thousands_sep'], $wp_locale->number_format['decimal_point'] );
160 $replace = array( '', '.' );
161
162 // for repeatable
163 if ( is_array( $value ) ) {
164 foreach ( $value as $key => $val ) {
165 $value[ $key ] = number_format_i18n( (float) str_ireplace( $search, $replace, $val ), 2 );
166 }
167 } else {
168 $value = number_format_i18n( (float) str_ireplace( $search, $replace, $value ), 2 );
169 }
170
171 return $value;
172 }
173
174 /**
175 * Converts text date to timestamp
176 * @since 1.0.2
177 * @param string $value Meta value
178 * @return string Timestring
179 */
180 public function text_date_timestamp( $value ) {
181 return is_array( $value ) ? array_map( 'strtotime', $value ) : strtotime( $value );
182 }
183
184 /**
185 * Datetime to timestamp
186 * @since 1.0.1
187 * @param string $value Meta value
188 * @return string Timestring
189 */
190 public function text_datetime_timestamp( $value, $repeat = false ) {
191
192 $test = is_array( $value ) ? array_filter( $value ) : '';
193 if ( empty( $test ) )
194 return '';
195
196 if ( $repeat_value = $this->_check_repeat( $value, __FUNCTION__, $repeat ) )
197 return $repeat_value;
198
199 $value = strtotime( $value['date'] .' '. $value['time'] );
200
201 if ( $tz_offset = $this->field->field_timezone_offset() )
202 $value += $tz_offset;
203
204 return $value;
205 }
206
207 /**
208 * Datetime to imestamp with timezone
209 * @since 1.0.1
210 * @param string $value Meta value
211 * @return string Timestring
212 */
213 public function text_datetime_timestamp_timezone( $value, $repeat = false ) {
214
215 $test = is_array( $value ) ? array_filter( $value ) : '';
216 if ( empty( $test ) )
217 return '';
218
219 if ( $repeat_value = $this->_check_repeat( $value, __FUNCTION__, $repeat ) )
220 return $repeat_value;
221
222 $tzstring = null;
223
224 if ( is_array( $value ) && array_key_exists( 'timezone', $value ) )
225 $tzstring = $value['timezone'];
226
227 if ( empty( $tzstring ) )
228 $tzstring = cmb_Meta_Box::timezone_string();
229
230 $offset = cmb_Meta_Box::timezone_offset( $tzstring, true );
231
232 if ( substr( $tzstring, 0, 3 ) === 'UTC' )
233 $tzstring = timezone_name_from_abbr( '', $offset, 0 );
234
235 $value = new DateTime( $value['date'] .' '. $value['time'], new DateTimeZone( $tzstring ) );
236 $value = serialize( $value );
237
238 return $value;
239 }
240
241 /**
242 * Sanitize textareas and wysiwyg fields
243 * @since 1.0.1
244 * @param string $value Meta value
245 * @return string Sanitized data
246 */
247 public function textarea( $value ) {
248 return is_array( $value ) ? array_map( 'wp_kses_post', $value ) : wp_kses_post( $value );
249 }
250
251 /**
252 * Sanitize code textareas
253 * @since 1.0.2
254 * @param string $value Meta value
255 * @return string Sanitized data
256 */
257 public function textarea_code( $value, $repeat = false ) {
258 if ( $repeat_value = $this->_check_repeat( $value, __FUNCTION__, $repeat ) )
259 return $repeat_value;
260
261 return htmlspecialchars_decode( stripslashes( $value ) );
262 }
263
264 /**
265 * Peforms saving of `file` attachement's ID
266 * @since 1.1.0
267 * @param string $value File url
268 */
269 public function _save_file_id( $value ) {
270 $group = $this->field->group;
271 $args = $this->field->args();
272 $args['id'] = $args['_id'] . '_id';
273
274 unset( $args['_id'], $args['_name'] );
275 // And get new field object
276 $field = new cmb_Meta_Box_field( $args, $group );
277 $id_key = $field->_id();
278 $id_val_old = $field->escaped_value( 'absint' );
279
280 if ( $group ) {
281 // Check group $_POST data
282 $i = $group->index;
283 $base_id = $group->_id();
284 $id_val = isset( $_POST[ $base_id ][ $i ][ $id_key ] ) ? absint( $_POST[ $base_id ][ $i ][ $id_key ] ) : 0;
285
286 } else {
287 // Check standard $_POST data
288 $id_val = isset( $_POST[ $field->id() ] ) ? $_POST[ $field->id() ] : null;
289
290 }
291
292 // If there is no ID saved yet, try to get it from the url
293 if ( $value && ! $id_val ) {
294 $id_val = cmb_Meta_Box::image_id_from_url( $value );
295 }
296
297 if ( $group ) {
298 return array(
299 'attach_id' => $id_val,
300 'field_id' => $id_key
301 );
302 }
303
304 if ( $id_val && $id_val != $id_val_old ) {
305 return $field->update_data( $id_val );
306 } elseif ( empty( $id_val ) && $id_val_old ) {
307 return $field->remove_data( $old );
308 }
309 }
310
311 /**
312 * Handles saving of attachment post ID and sanitizing file url
313 * @since 1.1.0
314 * @param string $value File url
315 * @return string Sanitized url
316 */
317 public function file( $value ) {
318 // If NOT specified to NOT save the file ID
319 if ( $this->field->args( 'save_id' ) ) {
320 $id_value = $this->_save_file_id( $value );
321 }
322 $clean = $this->text_url( $value );
323
324 // Return an array with url/id if saving a group field
325 return $this->field->group ? array_merge( array( 'url' => $clean), $id_value ) : $clean;
326 }
327
328 /**
329 * If repeating, loop through and re-apply sanitization method
330 * @since 1.1.0
331 * @param mixed $value Meta value
332 * @param string $method Class method
333 * @param bool $repeat Whether repeating or not
334 * @return mixed Sanitized value
335 */
336 public function _check_repeat( $value, $method, $repeat ) {
337 if ( $repeat || ! $this->field->args( 'repeatable' ) )
338 return;
339 $new_value = array();
340 foreach ( $value as $iterator => $val ) {
341 $new_value[] = $this->$method( $val, true );
342 }
343 return $new_value;
344 }
345
346 }
347