Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 340 lines (288 sloc) 8.539 kB
aeed826 @devinsays Validation filters. Improved sanitization.
authored
1 <?php
2
3 /* Text */
4
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
5 add_filter( 'of_sanitize_text', 'sanitize_text_field' );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
6
7 /* Textarea */
8
1f0d363 @devinsays Change for default sanitization on textarea. Example of how to use f…
authored
9 function of_sanitize_textarea($input) {
10 global $allowedtags;
11 $output = wp_kses( $input, $allowedtags);
12 return $output;
13 }
14
15 add_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
fa7dbd5 @devinsays Run info description through a sanitization filter.
authored
16
17 /* Info */
18
19 add_filter( 'of_sanitize_info', 'of_sanitize_textarea' );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
20
21 /* Select */
22
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
23 add_filter( 'of_sanitize_select', 'of_sanitize_enum', 10, 2);
aeed826 @devinsays Validation filters. Improved sanitization.
authored
24
25 /* Radio */
26
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
27 add_filter( 'of_sanitize_radio', 'of_sanitize_enum', 10, 2);
aeed826 @devinsays Validation filters. Improved sanitization.
authored
28
29 /* Images */
30
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
31 add_filter( 'of_sanitize_images', 'of_sanitize_enum', 10, 2);
aeed826 @devinsays Validation filters. Improved sanitization.
authored
32
33 /* Checkbox */
34
35 function of_sanitize_checkbox( $input ) {
36 if ( $input ) {
37 $output = "1";
38 } else {
39 $output = "0";
40 }
41 return $output;
42 }
43 add_filter( 'of_sanitize_checkbox', 'of_sanitize_checkbox' );
44
45 /* Multicheck */
46
47 function of_sanitize_multicheck( $input, $option ) {
48 $output = '';
49 if ( is_array( $input ) ) {
0c36163 @devinsays Ensure that multicheck always returns a value.
authored
50 foreach( $option['options'] as $key => $value ) {
51 $output[$key] = "0";
52 }
aeed826 @devinsays Validation filters. Improved sanitization.
authored
53 foreach( $input as $key => $value ) {
0c36163 @devinsays Ensure that multicheck always returns a value.
authored
54 if ( array_key_exists( $key, $option['options'] ) && $value ) {
55 $output[$key] = "1";
aeed826 @devinsays Validation filters. Improved sanitization.
authored
56 }
57 }
58 }
59 return $output;
60 }
61 add_filter( 'of_sanitize_multicheck', 'of_sanitize_multicheck', 10, 2 );
62
63 /* Color Picker */
64
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
65 add_filter( 'of_sanitize_color', 'of_sanitize_hex' );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
66
67 /* Uploader */
68
69 function of_sanitize_upload( $input ) {
70 $output = '';
71 $filetype = wp_check_filetype($input);
72 if ( $filetype["ext"] ) {
73 $output = $input;
74 }
75 return $output;
76 }
77 add_filter( 'of_sanitize_upload', 'of_sanitize_upload' );
78
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
79 /* Check that the key value sent is valid */
80
81 function of_sanitize_enum( $input, $option ) {
82 $output = '';
83 if ( array_key_exists( $input, $option['options'] ) ) {
84 $output = $input;
85 }
86 return $output;
87 }
88
aeed826 @devinsays Validation filters. Improved sanitization.
authored
89 /* Background */
90
91 function of_sanitize_background( $input ) {
92 $output = wp_parse_args( $input, array(
93 'color' => '',
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
94 'image' => '',
aeed826 @devinsays Validation filters. Improved sanitization.
authored
95 'repeat' => 'repeat',
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
96 'position' => 'top center',
97 'attachment' => 'scroll'
aeed826 @devinsays Validation filters. Improved sanitization.
authored
98 ) );
99
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
100 $output['color'] = apply_filters( 'of_sanitize_hex', $input['color'] );
101 $output['image'] = apply_filters( 'of_sanitize_upload', $input['image'] );
102 $output['repeat'] = apply_filters( 'of_background_repeat', $input['repeat'] );
103 $output['position'] = apply_filters( 'of_background_position', $input['position'] );
104 $output['attachment'] = apply_filters( 'of_background_attachment', $input['attachment'] );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
105
106 return $output;
107 }
108 add_filter( 'of_sanitize_background', 'of_sanitize_background' );
109
110 function of_sanitize_background_repeat( $value ) {
111 $recognized = of_recognized_background_repeat();
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
112 if ( array_key_exists( $value, $recognized ) ) {
113 return $value;
aeed826 @devinsays Validation filters. Improved sanitization.
authored
114 }
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
115 return apply_filters( 'of_default_background_repeat', current( $recognized ) );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
116 }
117 add_filter( 'of_background_repeat', 'of_sanitize_background_repeat' );
118
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
119 function of_sanitize_background_position( $value ) {
120 $recognized = of_recognized_background_position();
121 if ( array_key_exists( $value, $recognized ) ) {
122 return $value;
123 }
124 return apply_filters( 'of_default_background_position', current( $recognized ) );
125 }
126 add_filter( 'of_background_position', 'of_sanitize_background_position' );
127
128 function of_sanitize_background_attachment( $value ) {
129 $recognized = of_recognized_background_attachment();
130 if ( array_key_exists( $value, $recognized ) ) {
131 return $value;
132 }
133 return apply_filters( 'of_default_background_attachment', current( $recognized ) );
134 }
135 add_filter( 'of_background_attachment', 'of_sanitize_background_attachment' );
136
137
aeed826 @devinsays Validation filters. Improved sanitization.
authored
138 /* Typography */
139
140 function of_sanitize_typography( $input ) {
141 $output = wp_parse_args( $input, array(
142 'size' => '',
143 'face' => '',
144 'style' => '',
145 'color' => ''
146 ) );
147
148 $output['size'] = apply_filters( 'of_font_size', $output['size'] );
149 $output['face'] = apply_filters( 'of_font_face', $output['face'] );
150 $output['style'] = apply_filters( 'of_font_style', $output['style'] );
151 $output['color'] = apply_filters( 'of_color', $output['color'] );
152
153 return $output;
154 }
155 add_filter( 'of_sanitize_typography', 'of_sanitize_typography' );
156
157
158 function of_sanitize_font_size( $value ) {
159 $recognized = of_recognized_font_sizes();
cd5dd55 @devinsays Preg replace for font size. Hat tip @rejithomas.
authored
160 $value = preg_replace('/px/','', $value);
aeed826 @devinsays Validation filters. Improved sanitization.
authored
161 if ( in_array( (int) $value, $recognized ) ) {
162 return (int) $value;
163 }
164 return (int) apply_filters( 'of_default_font_size', $recognized );
165 }
166 add_filter( 'of_font_face', 'of_sanitize_font_face' );
167
168
169 function of_sanitize_font_style( $value ) {
170 $recognized = of_recognized_font_styles();
171 if ( array_key_exists( $value, $recognized ) ) {
172 return $value;
173 }
174 return apply_filters( 'of_default_font_style', current( $recognized ) );
175 }
176 add_filter( 'of_font_style', 'of_sanitize_font_style' );
177
178
179 function of_sanitize_font_face( $value ) {
180 $recognized = of_recognized_font_faces();
181 if ( array_key_exists( $value, $recognized ) ) {
182 return $value;
183 }
184 return apply_filters( 'of_default_font_face', current( $recognized ) );
185 }
186 add_filter( 'of_font_face', 'of_sanitize_font_face' );
187
188 /**
189 * Get recognized background repeat settings
190 *
191 * @return array
192 *
193 */
194 function of_recognized_background_repeat() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
195 $default = array(
72a6340 @devinsays Updated background repeat array. Updated version because of new filt…
authored
196 'no-repeat' => 'No Repeat',
197 'repeat-x' => 'Repeat Horizontally',
198 'repeat-y' => 'Repeat Vertically',
199 'repeat' => 'Repeat All',
aeed826 @devinsays Validation filters. Improved sanitization.
authored
200 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
201 return apply_filters( 'of_recognized_background_repeat', $default );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
202 }
203
204 /**
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
205 * Get recognized background positions
206 *
207 * @return array
208 *
209 */
210 function of_recognized_background_position() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
211 $default = array(
40d7337 @mfields whitespace
mfields authored
212 'top left' => 'Top Left',
213 'top center' => 'Top Center',
214 'top right' => 'Top Right',
215 'center left' => 'Middle Left',
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
216 'center center' => 'Middle Center',
40d7337 @mfields whitespace
mfields authored
217 'center right' => 'Middle Right',
218 'bottom left' => 'Bottom Left',
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
219 'bottom center' => 'Bottom Center',
40d7337 @mfields whitespace
mfields authored
220 'bottom right' => 'Bottom Right'
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
221 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
222 return apply_filters( 'of_recognized_background_position', $default );
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
223 }
224
225 /**
226 * Get recognized background attachment
227 *
228 * @return array
229 *
230 */
231 function of_recognized_background_attachment() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
232 $default = array(
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
233 'scroll' => 'Scroll Normally',
40d7337 @mfields whitespace
mfields authored
234 'fixed' => 'Fixed in Place'
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
235 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
236 return apply_filters( 'of_recognized_background_attachment', $default );
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
237 }
238
239 /**
aeed826 @devinsays Validation filters. Improved sanitization.
authored
240 * Sanitize a color represented in hexidecimal notation.
241 *
242 * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
243 * @param string The value that this function should return if it cannot be recognized as a color.
244 * @return string
245 *
246 */
247
248 function of_sanitize_hex( $hex, $default = '' ) {
249 if ( of_validate_hex( $hex ) ) {
250 return $hex;
251 }
252 return $default;
253 }
254
255 /**
256 * Get recognized font sizes.
257 *
258 * Returns an indexed array of all recognized font sizes.
259 * Values are integers and represent a range of sizes from
260 * smallest to largest.
261 *
262 * @return array
263 */
264
265 function of_recognized_font_sizes() {
266 $sizes = range( 9, 71 );
267 $sizes = apply_filters( 'of_recognized_font_sizes', $sizes );
268 $sizes = array_map( 'absint', $sizes );
269 return $sizes;
270 }
271
272 /**
273 * Get recognized font faces.
274 *
275 * Returns an array of all recognized font faces.
276 * Keys are intended to be stored in the database
277 * while values are ready for display in in html.
278 *
279 * @return array
280 *
281 */
282 function of_recognized_font_faces() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
283 $default = array(
aeed826 @devinsays Validation filters. Improved sanitization.
authored
284 'arial' => 'Arial',
285 'verdana' => 'Verdana, Geneva',
286 'trebuchet' => 'Trebuchet',
287 'georgia' => 'Georgia',
288 'times' => 'Times New Roman',
289 'tahoma' => 'Tahoma, Geneva',
290 'palatino' => 'Palatino',
291 'helvetica' => 'Helvetica*'
292 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
293 return apply_filters( 'of_recognized_font_faces', $default );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
294 }
295
296 /**
297 * Get recognized font styles.
298 *
299 * Returns an array of all recognized font styles.
300 * Keys are intended to be stored in the database
301 * while values are ready for display in in html.
302 *
303 * @return array
304 *
305 */
306 function of_recognized_font_styles() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
307 $default = array(
aeed826 @devinsays Validation filters. Improved sanitization.
authored
308 'normal' => 'Normal',
309 'italic' => 'Italic',
310 'bold' => 'Bold',
311 'bold italic' => 'Bold Italic'
312 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
313 return apply_filters( 'of_recognized_font_styles', $default );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
314 }
315
316 /**
317 * Is a given string a color formatted in hexidecimal notation?
318 *
319 * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
320 * @return bool
321 *
322 */
323
324 function of_validate_hex( $hex ) {
325 $hex = trim( $hex );
326 /* Strip recognized prefixes. */
327 if ( 0 === strpos( $hex, '#' ) ) {
328 $hex = substr( $hex, 1 );
329 }
330 elseif ( 0 === strpos( $hex, '%23' ) ) {
331 $hex = substr( $hex, 3 );
332 }
333 /* Regex match. */
334 if ( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
335 return false;
336 }
337 else {
338 return true;
339 }
340 }
Something went wrong with that request. Please try again.