sanitization.php
6.42 KB
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
<?php
/**
* Sanitize float number
* @param mixed
* @return float
*/
function et_sanitize_float_number( $number ) {
return floatval( $number );
}
/**
* Sanitize integer number
* @param mixed
* @return int
*/
function et_sanitize_int_number( $number ) {
return intval( $number );
}
/**
* Sanitize font style
* @param string
* @param string
*/
function et_sanitize_font_style( $styles ) {
// List of allowable style
$allowed_styles = array_keys( et_divi_font_style_choices() );
// Explodes styles into array
$styles_array = explode( '|', $styles );
// Get valid styles
$valid_styles = array_intersect( $allowed_styles, $styles_array );
// Return sanitized styles
return implode( "|", $valid_styles );
}
/**
* Sanitize choosen option based on options' key
* @param string
* @param array
* @return string|bool
*/
function et_sanitize_key_based_option( $choosen, $options, $default = false ) {
// Validate choosen option based on available options
if ( ! isset( $options[ $choosen ] ) ) {
return $default;
}
return $choosen;
}
/**
* Sanitize font choice
* @param string
* @return string|bool
*/
function et_sanitize_font_choices( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_builder_get_fonts() );
}
/**
* Sanitize color scheme
* @param string
* @return string|bool
*/
function et_sanitize_color_scheme( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_color_scheme_choices() );
}
/**
* Sanitize header style
* @param string
* @return string|bool
*/
function et_sanitize_header_style( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_header_style_choices() );
}
/**
* Sanitize dropdown animation
* @param string
* @return string|bool
*/
function et_sanitize_dropdown_animation( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_dropdown_animation_choices() );
}
/**
* Sanitize footer column
* @param string
* @return string|bool
*/
function et_sanitize_footer_column( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_footer_column_choices() );
}
/**
* Sanitize yes no choices
* @param string
* @return string|bool
*/
function et_sanitize_yes_no( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_yes_no_choices() );
}
/**
* Sanitize left or right choices
* @param string
* @return string|bool
*/
function et_sanitize_left_right( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_left_right_choices() );
}
/**
* Sanitize image animation choices
* @param string
* @return string|bool
*/
function et_sanitize_image_animation( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_image_animation_choices() );
}
/**
* Sanitize divider style choices
* @param string
* @return string|bool
*/
function et_sanitize_divider_style( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_divider_style_choices() );
}
/**
* Sanitize divider position choices
* @param string
* @return string|bool
*/
function et_sanitize_divider_position( $choosen ) {
return et_sanitize_key_based_option( $choosen, et_divi_divider_position_choices() );
}
/**
* Sanitize RGBA color
* @param string
* @return string|bool
*/
function et_sanitize_alpha_color( $color ) {
// Trim unneeded whitespace
$color = str_replace( ' ', '', $color );
// If this is hex color, validate and return it
if ( 1 === preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
return $color;
}
// If this is rgb, validate and return it
elseif ( 'rgb(' === substr( $color, 0, 4 ) ) {
sscanf( $color, 'rgb(%d,%d,%d)', $red, $green, $blue );
if ( ( $red >= 0 && $red <= 255 ) &&
( $green >= 0 && $green <= 255 ) &&
( $blue >= 0 && $blue <= 255 )
) {
return "rgb({$red},{$green},{$blue})";
}
}
// If this is rgba, validate and return it
elseif ( 'rgba(' === substr( $color, 0, 5 ) ) {
sscanf( $color, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
if ( ( $red >= 0 && $red <= 255 ) &&
( $green >= 0 && $green <= 255 ) &&
( $blue >= 0 && $blue <= 255 ) &&
$alpha >= 0 && $alpha <= 1
) {
return "rgba({$red},{$green},{$blue},{$alpha})";
}
}
return false;
}
/**
* Sanitize font icon picker
* @param string
* @return string|bool
*/
function et_sanitize_font_icon( $choosen ) {
// Convert symbols into strings
$symbols = array_map( 'et_sanitize_font_icon_convert_icon_to_string', et_pb_get_font_icon_symbols() );
// If choosen icon isn't found in symbol set, return false
if ( ! in_array( $choosen, $symbols ) ) {
return false;
}
return $choosen;
}
/**
* Convert font hex-code font icons into strings so it can be compared
* @param string
* @return string
*/
function et_sanitize_font_icon_convert_icon_to_string( $icon ) {
// Replace & with &. Otherwise, it'll incorrectly decoded
$icon = str_replace( '&', '&', $icon );
// Decode
return html_entity_decode( $icon );
}
/**
* Array of allowed html tags on short block
* @return array
*/
function et_allowed_html_tags_short_block() {
$allowed_tags = array(
'div' => array(
'class' => array(),
'id' => array(),
),
'span' => array(
'class' => array(),
'id' => array(),
),
'ol' => array(
'class' => array(),
'id' => array(),
),
'ul' => array(
'class' => array(),
'id' => array(),
),
'li' => array(
'class' => array(),
'id' => array(),
),
'p' => array(
'class' => array(),
'id' => array(),
),
'a' => array(
'href' => array(),
'class' => array(),
'id' => array(),
'rel' => array(),
'title' => array(),
'target' => array(),
),
'br' => array(),
'em' => array(),
'strong' => array(),
);
return apply_filters( 'et_allowed_html_tags_short_block', $allowed_tags );
}
/**
* Sanitize short block html input
* @return string
*/
function et_sanitize_html_input_text( $string ) {
return wp_kses( $string, et_allowed_html_tags_short_block() );
}
/**
* Sanitize background repeat value
* @return string
*/
function et_sanitize_background_repeat( $choosen ) {
return et_sanitize_key_based_option(
$choosen,
et_divi_background_repeat_choices(),
apply_filters( 'et_divi_background_repeat_default', 'repeat' )
);
}
/**
* Sanitize background attachment value
* @return string
*/
function et_sanitize_background_attachment( $choosen ) {
return et_sanitize_key_based_option(
$choosen,
et_divi_background_attachment_choices(),
apply_filters( 'et_sanitize_background_attachment_default', 'scroll' )
);
}