-
Notifications
You must be signed in to change notification settings - Fork 286
/
class-options-sanitization.php
452 lines (415 loc) · 11.8 KB
/
class-options-sanitization.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* @package Options_Framework
* @author Devin Price <[email protected]>
* @license GPL-2.0+
* @link http://wptheming.com
* @copyright 2010-2016 WP Theming
*/
/**
* Sanitization for text input
*
* @link http://developer.wordpress.org/reference/functions/sanitize_text_field/
*/
add_filter( 'of_sanitize_text', 'sanitize_text_field' );
/**
* Sanitization for password input
*
* @link http://developer.wordpress.org/reference/functions/sanitize_text_field/
*/
add_filter( 'of_sanitize_password', 'sanitize_text_field' );
/**
* Sanitization for select input
*
* Validates that the selected option is a valid option.
*/
add_filter( 'of_sanitize_select', 'of_sanitize_enum', 10, 2 );
/**
* Sanitization for radio input
*
* Validates that the selected option is a valid option.
*/
add_filter( 'of_sanitize_radio', 'of_sanitize_enum', 10, 2 );
/**
* Sanitization for image selector
*
* Validates that the selected option is a valid option.
*/
add_filter( 'of_sanitize_images', 'of_sanitize_enum', 10, 2 );
/**
* Sanitization for textarea field
*
* @param $input string
* @return $output sanitized string
*/
function of_sanitize_textarea( $input ) {
global $allowedposttags;
$output = wp_kses( $input, $allowedposttags );
return $output;
}
add_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
/**
* Sanitization for checkbox input
*
* @param $input string (1 or empty) checkbox state
* @return $output '1' or false
*/
function of_sanitize_checkbox( $input ) {
if ( $input ) {
return '1';
}
return false;
}
add_filter( 'of_sanitize_checkbox', 'of_sanitize_checkbox' );
/**
* Sanitization for multicheck
*
* @param array of checkbox values
* @return array of sanitized values ('1' or false)
*/
function of_sanitize_multicheck( $input, $option ) {
$output = array();
if ( is_array( $input ) ) {
foreach( $option['options'] as $key => $value ) {
$output[$key] = false;
}
foreach( $input as $key => $value ) {
if ( array_key_exists( $key, $option['options'] ) && $value ) {
$output[$key] = '1';
}
}
}
return $output;
}
add_filter( 'of_sanitize_multicheck', 'of_sanitize_multicheck', 10, 2 );
/**
* File upload sanitization.
*
* Returns a sanitized filepath if it has a valid extension.
*
* @param string $input filepath
* @returns string $output filepath
*/
function of_sanitize_upload( $input ) {
$output = '';
$filetype = wp_check_filetype( $input );
if ( $filetype["ext"] ) {
$output = esc_url( $input );
}
return $output;
}
add_filter( 'of_sanitize_upload', 'of_sanitize_upload' );
/**
* Sanitization for editor input.
*
* Returns unfiltered HTML if user has permissions.
*
* @param string $input
* @returns string $output
*/
function of_sanitize_editor( $input ) {
if ( current_user_can( 'unfiltered_html' ) ) {
$output = $input;
}
else {
global $allowedposttags;
$output = wp_kses( $input, $allowedposttags );
}
return $output;
}
add_filter( 'of_sanitize_editor', 'of_sanitize_editor' );
/**
* Sanitization of input with allowed tags and wpautotop.
*
* Allows allowed tags in html input and ensures tags close properly.
*
* @param string $input
* @returns string $output
*/
function of_sanitize_allowedtags( $input ) {
global $allowedtags;
$output = wpautop( wp_kses( $input, $allowedtags ) );
return $output;
}
/**
* Sanitization of input with allowed post tags and wpautotop.
*
* Allows allowed post tags in html input and ensures tags close properly.
*
* @param string $input
* @returns string $output
*/
function of_sanitize_allowedposttags( $input ) {
global $allowedposttags;
$output = wpautop( wp_kses( $input, $allowedposttags) );
return $output;
}
/**
* Validates that the $input is one of the avilable choices
* for that specific option.
*
* @param string $input
* @returns string $output
*/
function of_sanitize_enum( $input, $option ) {
$output = '';
if ( array_key_exists( $input, $option['options'] ) ) {
$output = $input;
}
return $output;
}
/**
* Sanitization for background option.
*
* @returns array $output
*/
function of_sanitize_background( $input ) {
$output = wp_parse_args( $input, array(
'color' => '',
'image' => '',
'repeat' => 'repeat',
'position' => 'top center',
'attachment' => 'scroll'
) );
$output['color'] = apply_filters( 'of_sanitize_hex', $input['color'] );
$output['image'] = apply_filters( 'of_sanitize_upload', $input['image'] );
$output['repeat'] = apply_filters( 'of_background_repeat', $input['repeat'] );
$output['position'] = apply_filters( 'of_background_position', $input['position'] );
$output['attachment'] = apply_filters( 'of_background_attachment', $input['attachment'] );
return $output;
}
add_filter( 'of_sanitize_background', 'of_sanitize_background' );
/**
* Sanitization for background repeat
*
* @returns string $value if it is valid
*/
function of_sanitize_background_repeat( $value ) {
$recognized = of_recognized_background_repeat();
if ( array_key_exists( $value, $recognized ) ) {
return $value;
}
return apply_filters( 'of_default_background_repeat', current( $recognized ) );
}
add_filter( 'of_background_repeat', 'of_sanitize_background_repeat' );
/**
* Sanitization for background position
*
* @returns string $value if it is valid
*/
function of_sanitize_background_position( $value ) {
$recognized = of_recognized_background_position();
if ( array_key_exists( $value, $recognized ) ) {
return $value;
}
return apply_filters( 'of_default_background_position', current( $recognized ) );
}
add_filter( 'of_background_position', 'of_sanitize_background_position' );
/**
* Sanitization for background attachment
*
* @returns string $value if it is valid
*/
function of_sanitize_background_attachment( $value ) {
$recognized = of_recognized_background_attachment();
if ( array_key_exists( $value, $recognized ) ) {
return $value;
}
return apply_filters( 'of_default_background_attachment', current( $recognized ) );
}
add_filter( 'of_background_attachment', 'of_sanitize_background_attachment' );
/**
* Sanitization for typography option.
*/
function of_sanitize_typography( $input, $option ) {
$output = wp_parse_args( $input, array(
'size' => '',
'face' => '',
'style' => '',
'color' => ''
) );
if ( isset( $option['options']['faces'] ) && isset( $input['face'] ) ) {
if ( !( array_key_exists( $input['face'], $option['options']['faces'] ) ) ) {
$output['face'] = '';
}
}
else {
$output['face'] = apply_filters( 'of_font_face', $output['face'] );
}
$output['size'] = apply_filters( 'of_font_size', $output['size'] );
$output['style'] = apply_filters( 'of_font_style', $output['style'] );
$output['color'] = apply_filters( 'of_sanitize_color', $output['color'] );
return $output;
}
add_filter( 'of_sanitize_typography', 'of_sanitize_typography', 10, 2 );
/**
* Sanitization for font size
*/
function of_sanitize_font_size( $value ) {
$recognized = of_recognized_font_sizes();
$value_check = preg_replace('/px/','', $value);
if ( in_array( (int) $value_check, $recognized ) ) {
return $value;
}
return apply_filters( 'of_default_font_size', current( $recognized ) );
}
add_filter( 'of_font_size', 'of_sanitize_font_size' );
/**
* Sanitization for font style
*/
function of_sanitize_font_style( $value ) {
$recognized = of_recognized_font_styles();
if ( array_key_exists( $value, $recognized ) ) {
return $value;
}
return apply_filters( 'of_default_font_style', current( $recognized ) );
}
add_filter( 'of_font_style', 'of_sanitize_font_style' );
/**
* Sanitization for font face
*/
function of_sanitize_font_face( $value ) {
$recognized = of_recognized_font_faces();
if ( array_key_exists( $value, $recognized ) ) {
return $value;
}
return apply_filters( 'of_default_font_face', current( $recognized ) );
}
add_filter( 'of_font_face', 'of_sanitize_font_face' );
/**
* Get recognized background repeat settings
*
* @return array
*/
function of_recognized_background_repeat() {
$default = array(
'no-repeat' => __( 'No Repeat', 'options-framework' ),
'repeat-x' => __( 'Repeat Horizontally', 'options-framework' ),
'repeat-y' => __( 'Repeat Vertically', 'options-framework' ),
'repeat' => __( 'Repeat All', 'options-framework' ),
);
return apply_filters( 'of_recognized_background_repeat', $default );
}
/**
* Get recognized background positions
*
* @return array
*/
function of_recognized_background_position() {
$default = array(
'top left' => __( 'Top Left', 'options-framework' ),
'top center' => __( 'Top Center', 'options-framework' ),
'top right' => __( 'Top Right', 'options-framework' ),
'center left' => __( 'Middle Left', 'options-framework' ),
'center center' => __( 'Middle Center', 'options-framework' ),
'center right' => __( 'Middle Right', 'options-framework' ),
'bottom left' => __( 'Bottom Left', 'options-framework' ),
'bottom center' => __( 'Bottom Center', 'options-framework' ),
'bottom right' => __( 'Bottom Right', 'options-framework')
);
return apply_filters( 'of_recognized_background_position', $default );
}
/**
* Get recognized background attachment
*
* @return array
*/
function of_recognized_background_attachment() {
$default = array(
'scroll' => __( 'Scroll Normally', 'options-framework' ),
'fixed' => __( 'Fixed in Place', 'options-framework')
);
return apply_filters( 'of_recognized_background_attachment', $default );
}
/**
* Sanitize a color represented in hexidecimal notation.
*
* @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
* @param string The value that this function should return if it cannot be recognized as a color.
* @return string
*/
function of_sanitize_hex( $hex, $default = '' ) {
if ( of_validate_hex( $hex ) ) {
return $hex;
}
return $default;
}
add_filter( 'of_sanitize_color', 'of_sanitize_hex' );
/**
* Get recognized font sizes.
*
* Returns an indexed array of all recognized font sizes.
* Values are integers and represent a range of sizes from
* smallest to largest.
*
* @return array
*/
function of_recognized_font_sizes() {
$sizes = range( 9, 71 );
$sizes = apply_filters( 'of_recognized_font_sizes', $sizes );
$sizes = array_map( 'absint', $sizes );
return $sizes;
}
/**
* Get recognized font faces.
*
* Returns an array of all recognized font faces.
* Keys are intended to be stored in the database
* while values are ready for display in in html.
*
* @return array
*/
function of_recognized_font_faces() {
$default = array(
'arial' => 'Arial',
'verdana' => 'Verdana, Geneva',
'trebuchet' => 'Trebuchet',
'georgia' => 'Georgia',
'times' => 'Times New Roman',
'tahoma' => 'Tahoma, Geneva',
'palatino' => 'Palatino',
'helvetica' => 'Helvetica*'
);
return apply_filters( 'of_recognized_font_faces', $default );
}
/**
* Get recognized font styles.
*
* Returns an array of all recognized font styles.
* Keys are intended to be stored in the database
* while values are ready for display in in html.
*
* @return array
*/
function of_recognized_font_styles() {
$default = array(
'normal' => __( 'Normal', 'options-framework' ),
'italic' => __( 'Italic', 'options-framework' ),
'bold' => __( 'Bold', 'options-framework' ),
'bold italic' => __( 'Bold Italic', 'options-framework' )
);
return apply_filters( 'of_recognized_font_styles', $default );
}
/**
* Is a given string a color formatted in hexidecimal notation?
*
* @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
* @return bool
*/
function of_validate_hex( $hex ) {
$hex = trim( $hex );
/* Strip recognized prefixes. */
if ( 0 === strpos( $hex, '#' ) ) {
$hex = substr( $hex, 1 );
}
elseif ( 0 === strpos( $hex, '%23' ) ) {
$hex = substr( $hex, 3 );
}
/* Regex match. */
if ( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
return false;
}
else {
return true;
}
}