Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 361 lines (302 sloc) 9.014 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 filt...
authored
9 function of_sanitize_textarea($input) {
987c0b0 @devinsays Replace with for textarea and info.
authored
10 global $allowedposttags;
11 $output = wp_kses( $input, $allowedposttags);
1f0d363 @devinsays Change for default sanitization on textarea. Example of how to use filt...
authored
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
987c0b0 @devinsays Replace with for textarea and info.
authored
19 add_filter( 'of_sanitize_info', 'of_sanitize_allowedposttags' );
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
987c0b0 @devinsays Replace with for textarea and info.
authored
79 /* Allowed Tags */
80
81 function of_sanitize_allowedtags($input) {
82 global $allowedtags;
83 $output = wpautop(wp_kses( $input, $allowedtags));
84 return $output;
85 }
86
87 add_filter( 'of_sanitize_info', 'of_sanitize_allowedtags' );
88
89 /* Allowed Post Tags */
90
91 function of_sanitize_allowedposttags($input) {
92 global $allowedposttags;
93 $output = wpautop(wp_kses( $input, $allowedposttags));
94 return $output;
95 }
96
97 add_filter( 'of_sanitize_info', 'of_sanitize_allowedposttags' );
98
99
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
100 /* Check that the key value sent is valid */
101
102 function of_sanitize_enum( $input, $option ) {
103 $output = '';
104 if ( array_key_exists( $input, $option['options'] ) ) {
105 $output = $input;
106 }
107 return $output;
108 }
109
aeed826 @devinsays Validation filters. Improved sanitization.
authored
110 /* Background */
111
112 function of_sanitize_background( $input ) {
113 $output = wp_parse_args( $input, array(
114 'color' => '',
81377fc @devinsays Simplified filters. Hat tip @_mfields.
authored
115 'image' => '',
aeed826 @devinsays Validation filters. Improved sanitization.
authored
116 'repeat' => 'repeat',
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
117 'position' => 'top center',
118 'attachment' => 'scroll'
aeed826 @devinsays Validation filters. Improved sanitization.
authored
119 ) );
120
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
121 $output['color'] = apply_filters( 'of_sanitize_hex', $input['color'] );
122 $output['image'] = apply_filters( 'of_sanitize_upload', $input['image'] );
123 $output['repeat'] = apply_filters( 'of_background_repeat', $input['repeat'] );
124 $output['position'] = apply_filters( 'of_background_position', $input['position'] );
125 $output['attachment'] = apply_filters( 'of_background_attachment', $input['attachment'] );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
126
127 return $output;
128 }
129 add_filter( 'of_sanitize_background', 'of_sanitize_background' );
130
131 function of_sanitize_background_repeat( $value ) {
132 $recognized = of_recognized_background_repeat();
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
133 if ( array_key_exists( $value, $recognized ) ) {
134 return $value;
aeed826 @devinsays Validation filters. Improved sanitization.
authored
135 }
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
136 return apply_filters( 'of_default_background_repeat', current( $recognized ) );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
137 }
138 add_filter( 'of_background_repeat', 'of_sanitize_background_repeat' );
139
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
140 function of_sanitize_background_position( $value ) {
141 $recognized = of_recognized_background_position();
142 if ( array_key_exists( $value, $recognized ) ) {
143 return $value;
144 }
145 return apply_filters( 'of_default_background_position', current( $recognized ) );
146 }
147 add_filter( 'of_background_position', 'of_sanitize_background_position' );
148
149 function of_sanitize_background_attachment( $value ) {
150 $recognized = of_recognized_background_attachment();
151 if ( array_key_exists( $value, $recognized ) ) {
152 return $value;
153 }
154 return apply_filters( 'of_default_background_attachment', current( $recognized ) );
155 }
156 add_filter( 'of_background_attachment', 'of_sanitize_background_attachment' );
157
158
aeed826 @devinsays Validation filters. Improved sanitization.
authored
159 /* Typography */
160
161 function of_sanitize_typography( $input ) {
162 $output = wp_parse_args( $input, array(
163 'size' => '',
164 'face' => '',
165 'style' => '',
166 'color' => ''
167 ) );
168
169 $output['size'] = apply_filters( 'of_font_size', $output['size'] );
170 $output['face'] = apply_filters( 'of_font_face', $output['face'] );
171 $output['style'] = apply_filters( 'of_font_style', $output['style'] );
172 $output['color'] = apply_filters( 'of_color', $output['color'] );
173
174 return $output;
175 }
176 add_filter( 'of_sanitize_typography', 'of_sanitize_typography' );
177
178
179 function of_sanitize_font_size( $value ) {
180 $recognized = of_recognized_font_sizes();
cd5dd55 @devinsays Preg replace for font size. Hat tip @rejithomas.
authored
181 $value = preg_replace('/px/','', $value);
aeed826 @devinsays Validation filters. Improved sanitization.
authored
182 if ( in_array( (int) $value, $recognized ) ) {
183 return (int) $value;
184 }
185 return (int) apply_filters( 'of_default_font_size', $recognized );
186 }
187 add_filter( 'of_font_face', 'of_sanitize_font_face' );
188
189
190 function of_sanitize_font_style( $value ) {
191 $recognized = of_recognized_font_styles();
192 if ( array_key_exists( $value, $recognized ) ) {
193 return $value;
194 }
195 return apply_filters( 'of_default_font_style', current( $recognized ) );
196 }
197 add_filter( 'of_font_style', 'of_sanitize_font_style' );
198
199
200 function of_sanitize_font_face( $value ) {
201 $recognized = of_recognized_font_faces();
202 if ( array_key_exists( $value, $recognized ) ) {
203 return $value;
204 }
205 return apply_filters( 'of_default_font_face', current( $recognized ) );
206 }
207 add_filter( 'of_font_face', 'of_sanitize_font_face' );
208
209 /**
210 * Get recognized background repeat settings
211 *
212 * @return array
213 *
214 */
215 function of_recognized_background_repeat() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
216 $default = array(
72a6340 @devinsays Updated background repeat array. Updated version because of new filters...
authored
217 'no-repeat' => 'No Repeat',
218 'repeat-x' => 'Repeat Horizontally',
219 'repeat-y' => 'Repeat Vertically',
220 'repeat' => 'Repeat All',
aeed826 @devinsays Validation filters. Improved sanitization.
authored
221 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
222 return apply_filters( 'of_recognized_background_repeat', $default );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
223 }
224
225 /**
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
226 * Get recognized background positions
227 *
228 * @return array
229 *
230 */
231 function of_recognized_background_position() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
232 $default = array(
40d7337 @mfields whitespace
mfields authored
233 'top left' => 'Top Left',
234 'top center' => 'Top Center',
235 'top right' => 'Top Right',
236 'center left' => 'Middle Left',
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
237 'center center' => 'Middle Center',
40d7337 @mfields whitespace
mfields authored
238 'center right' => 'Middle Right',
239 'bottom left' => 'Bottom Left',
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
240 'bottom center' => 'Bottom Center',
40d7337 @mfields whitespace
mfields authored
241 'bottom right' => 'Bottom Right'
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
242 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
243 return apply_filters( 'of_recognized_background_position', $default );
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
244 }
245
246 /**
247 * Get recognized background attachment
248 *
249 * @return array
250 *
251 */
252 function of_recognized_background_attachment() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
253 $default = array(
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
254 'scroll' => 'Scroll Normally',
40d7337 @mfields whitespace
mfields authored
255 'fixed' => 'Fixed in Place'
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
256 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
257 return apply_filters( 'of_recognized_background_attachment', $default );
528a9a4 @devinsays Updates to background option. Saves as an array.
authored
258 }
259
260 /**
aeed826 @devinsays Validation filters. Improved sanitization.
authored
261 * Sanitize a color represented in hexidecimal notation.
262 *
263 * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
264 * @param string The value that this function should return if it cannot be recognized as a color.
265 * @return string
266 *
267 */
268
269 function of_sanitize_hex( $hex, $default = '' ) {
270 if ( of_validate_hex( $hex ) ) {
271 return $hex;
272 }
273 return $default;
274 }
275
276 /**
277 * Get recognized font sizes.
278 *
279 * Returns an indexed array of all recognized font sizes.
280 * Values are integers and represent a range of sizes from
281 * smallest to largest.
282 *
283 * @return array
284 */
285
286 function of_recognized_font_sizes() {
287 $sizes = range( 9, 71 );
288 $sizes = apply_filters( 'of_recognized_font_sizes', $sizes );
289 $sizes = array_map( 'absint', $sizes );
290 return $sizes;
291 }
292
293 /**
294 * Get recognized font faces.
295 *
296 * Returns an array of all recognized font faces.
297 * Keys are intended to be stored in the database
298 * while values are ready for display in in html.
299 *
300 * @return array
301 *
302 */
303 function of_recognized_font_faces() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
304 $default = array(
aeed826 @devinsays Validation filters. Improved sanitization.
authored
305 'arial' => 'Arial',
306 'verdana' => 'Verdana, Geneva',
307 'trebuchet' => 'Trebuchet',
308 'georgia' => 'Georgia',
309 'times' => 'Times New Roman',
310 'tahoma' => 'Tahoma, Geneva',
311 'palatino' => 'Palatino',
312 'helvetica' => 'Helvetica*'
313 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
314 return apply_filters( 'of_recognized_font_faces', $default );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
315 }
316
317 /**
318 * Get recognized font styles.
319 *
320 * Returns an array of all recognized font styles.
321 * Keys are intended to be stored in the database
322 * while values are ready for display in in html.
323 *
324 * @return array
325 *
326 */
327 function of_recognized_font_styles() {
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
328 $default = array(
aeed826 @devinsays Validation filters. Improved sanitization.
authored
329 'normal' => 'Normal',
330 'italic' => 'Italic',
331 'bold' => 'Bold',
332 'bold italic' => 'Bold Italic'
333 );
56b9d92 @mfields Allow themes to override 'recognized' arrays.
mfields authored
334 return apply_filters( 'of_recognized_font_styles', $default );
aeed826 @devinsays Validation filters. Improved sanitization.
authored
335 }
336
337 /**
338 * Is a given string a color formatted in hexidecimal notation?
339 *
340 * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
341 * @return bool
342 *
343 */
344
345 function of_validate_hex( $hex ) {
346 $hex = trim( $hex );
347 /* Strip recognized prefixes. */
348 if ( 0 === strpos( $hex, '#' ) ) {
349 $hex = substr( $hex, 1 );
350 }
351 elseif ( 0 === strpos( $hex, '%23' ) ) {
352 $hex = substr( $hex, 3 );
353 }
354 /* Regex match. */
355 if ( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
356 return false;
357 }
358 else {
359 return true;
360 }
361 }
Something went wrong with that request. Please try again.