1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A class for helping render common component items. |
9
|
|
|
* |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
*/ |
12
|
|
|
class AUI_Component_Helper { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A component helper for generating a input name. |
16
|
|
|
* |
17
|
|
|
* @param $text |
18
|
|
|
* @param $multiple bool If the name is set to be multiple but no brackets found then we add some. |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public static function name( $text, $multiple = false ) { |
23
|
|
|
$output = ''; |
24
|
|
|
|
25
|
|
|
if ( $text ) { |
26
|
|
|
$is_multiple = strpos( $text, '[' ) === false && $multiple ? '[]' : ''; |
27
|
|
|
$output = ' name="' . esc_attr( $text ) . $is_multiple . '" '; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $output; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A component helper for generating a item id. |
35
|
|
|
* |
36
|
|
|
* @param $text string The text to be used as the value. |
37
|
|
|
* |
38
|
|
|
* @return string The sanitized item. |
39
|
|
|
*/ |
40
|
|
|
public static function id( $text ) { |
41
|
|
|
$output = ''; |
42
|
|
|
|
43
|
|
|
if ( $text ) { |
44
|
|
|
$output = ' id="' . sanitize_html_class( $text ) . '" '; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $output; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* A component helper for generating a item title. |
52
|
|
|
* |
53
|
|
|
* @param $text string The text to be used as the value. |
54
|
|
|
* |
55
|
|
|
* @return string The sanitized item. |
56
|
|
|
*/ |
57
|
|
|
public static function title( $text ) { |
58
|
|
|
$output = ''; |
59
|
|
|
|
60
|
|
|
if ( $text ) { |
61
|
|
|
$output = ' title="' . esc_attr( $text ) . '" '; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $output; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* A component helper for generating a item value. |
69
|
|
|
* |
70
|
|
|
* @param $text string The text to be used as the value. |
71
|
|
|
* |
72
|
|
|
* @return string The sanitized item. |
73
|
|
|
*/ |
74
|
|
|
public static function value( $text ) { |
75
|
|
|
$output = ''; |
76
|
|
|
|
77
|
|
|
if ( $text !== null && $text !== false ) { |
78
|
|
|
$output = ' value="' . esc_attr( wp_unslash( $text ) ) . '" '; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $output; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* A component helper for generating a item class attribute. |
86
|
|
|
* |
87
|
|
|
* @param $text string The text to be used as the value. |
88
|
|
|
* |
89
|
|
|
* @return string The sanitized item. |
90
|
|
|
*/ |
91
|
|
|
public static function class_attr( $text ) { |
92
|
|
|
$output = ''; |
93
|
|
|
|
94
|
|
|
if ( $text ) { |
95
|
|
|
$classes = self::esc_classes( $text ); |
96
|
|
|
if ( ! empty( $classes ) ) { |
97
|
|
|
$output = ' class="' . $classes . '" '; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $output; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Escape a string of classes. |
106
|
|
|
* |
107
|
|
|
* @param $text |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public static function esc_classes( $text ) { |
112
|
|
|
$output = ''; |
113
|
|
|
|
114
|
|
|
if ( $text ) { |
115
|
|
|
$classes = explode( " ", $text ); |
116
|
|
|
$classes = array_map( "trim", $classes ); |
117
|
|
|
$classes = array_map( "sanitize_html_class", $classes ); |
118
|
|
|
if ( ! empty( $classes ) ) { |
119
|
|
|
$output = implode( " ", $classes ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $output; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $args |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public static function data_attributes( $args ) { |
133
|
|
|
$output = ''; |
134
|
|
|
|
135
|
|
|
if ( ! empty( $args ) ) { |
136
|
|
|
|
137
|
|
|
foreach ( $args as $key => $val ) { |
138
|
|
|
if ( substr( $key, 0, 5 ) === "data-" ) { |
139
|
|
|
$output .= ' ' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '" '; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $output; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $args |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public static function aria_attributes( $args ) { |
153
|
|
|
$output = ''; |
154
|
|
|
|
155
|
|
|
if ( ! empty( $args ) ) { |
156
|
|
|
|
157
|
|
|
foreach ( $args as $key => $val ) { |
158
|
|
|
if ( substr( $key, 0, 5 ) === "aria-" ) { |
159
|
|
|
$output .= ' ' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '" '; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $output; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Build a font awesome icon from a class. |
169
|
|
|
* |
170
|
|
|
* @param $class |
171
|
|
|
* @param bool $space_after |
172
|
|
|
* @param array $extra_attributes An array of extra attributes. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public static function icon( $class, $space_after = false, $extra_attributes = array() ) { |
177
|
|
|
$output = ''; |
178
|
|
|
|
179
|
|
|
if ( $class ) { |
180
|
|
|
$classes = self::esc_classes( $class ); |
181
|
|
|
if ( ! empty( $classes ) ) { |
182
|
|
|
$output = '<i class="' . $classes . '" '; |
183
|
|
|
// extra attributes |
184
|
|
|
if ( ! empty( $extra_attributes ) ) { |
185
|
|
|
$output .= AUI_Component_Helper::extra_attributes( $extra_attributes ); |
186
|
|
|
} |
187
|
|
|
$output .= '></i>'; |
188
|
|
|
if ( $space_after ) { |
189
|
|
|
$output .= " "; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $output; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param $args |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public static function extra_attributes( $args ) { |
203
|
|
|
$output = ''; |
204
|
|
|
|
205
|
|
|
if ( ! empty( $args ) ) { |
206
|
|
|
|
207
|
|
|
if ( is_array( $args ) ) { |
208
|
|
|
foreach ( $args as $key => $val ) { |
209
|
|
|
$output .= ' ' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '" '; |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
|
|
$output .= ' ' . $args . ' '; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $output; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param $args |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public static function help_text( $text ) { |
226
|
|
|
$output = ''; |
227
|
|
|
|
228
|
|
|
if ( $text ) { |
229
|
|
|
$output .= '<small class="form-text text-muted d-block">' . wp_kses_post( $text ) . '</small>'; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
return $output; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Replace element require context with JS. |
238
|
|
|
* |
239
|
|
|
* @param $input |
240
|
|
|
* |
241
|
|
|
* @return string|void |
242
|
|
|
*/ |
243
|
|
|
public static function element_require( $input ) { |
244
|
|
|
|
245
|
|
|
$input = str_replace( "'", '"', $input );// we only want double quotes |
246
|
|
|
|
247
|
|
|
$output = esc_attr( str_replace( array( "[%", "%]", "%:checked]" ), array( |
248
|
|
|
"jQuery(form).find('[data-argument=\"", |
249
|
|
|
"\"]').find('input,select,textarea').val()", |
250
|
|
|
"\"]').find('input:checked').val()", |
251
|
|
|
), $input ) ); |
252
|
|
|
|
253
|
|
|
if ( $output ) { |
254
|
|
|
$output = ' data-element-require="' . $output . '" '; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $output; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Navigates through an array, object, or scalar, and removes slashes from the values. |
262
|
|
|
* |
263
|
|
|
* @since 0.1.41 |
264
|
|
|
* |
265
|
|
|
* @param mixed $value The value to be stripped. |
266
|
|
|
* @param array $input Input Field. |
267
|
|
|
* |
268
|
|
|
* @return mixed Stripped value. |
269
|
|
|
*/ |
270
|
|
|
public static function sanitize_html_field( $value, $input = array() ) { |
271
|
|
|
$original = $value; |
272
|
|
|
|
273
|
|
|
if ( is_array( $value ) ) { |
274
|
|
|
foreach ( $value as $index => $item ) { |
275
|
|
|
$value[ $index ] = self::_sanitize_html_field( $value, $input ); |
276
|
|
|
} |
277
|
|
|
} elseif ( is_object( $value ) ) { |
278
|
|
|
$object_vars = get_object_vars( $value ); |
279
|
|
|
|
280
|
|
|
foreach ( $object_vars as $property_name => $property_value ) { |
281
|
|
|
$value->$property_name = self::_sanitize_html_field( $property_value, $input ); |
282
|
|
|
} |
283
|
|
|
} else { |
284
|
|
|
$value = self::_sanitize_html_field( $value, $input ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Filters content and keeps only allowable HTML elements. |
289
|
|
|
* |
290
|
|
|
* @since 0.1.41 |
291
|
|
|
* |
292
|
|
|
* @param string|array $value Content to filter through kses. |
293
|
|
|
* @param string|array $value Original content without filter. |
294
|
|
|
* @param array $input Input Field. |
295
|
|
|
*/ |
296
|
|
|
return apply_filters( 'ayecode_ui_sanitize_html_field', $value, $original, $input ); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Filters content and keeps only allowable HTML elements. |
301
|
|
|
* |
302
|
|
|
* This function makes sure that only the allowed HTML element names, attribute |
303
|
|
|
* names and attribute values plus only sane HTML entities will occur in |
304
|
|
|
* $string. You have to remove any slashes from PHP's magic quotes before you |
305
|
|
|
* call this function. |
306
|
|
|
* |
307
|
|
|
* The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news', |
308
|
|
|
* 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This |
309
|
|
|
* covers all common link protocols, except for 'javascript' which should not |
310
|
|
|
* be allowed for untrusted users. |
311
|
|
|
* |
312
|
|
|
* @since 0.1.41 |
313
|
|
|
* |
314
|
|
|
* @param string|array $value Content to filter through kses. |
315
|
|
|
* @param array $input Input Field. |
316
|
|
|
* |
317
|
|
|
* @return string Filtered content with only allowed HTML elements. |
318
|
|
|
*/ |
319
|
|
|
public static function _sanitize_html_field( $value, $input = array() ) { |
320
|
|
|
if ( $value === '' ) { |
321
|
|
|
return $value; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
$allowed_html = self::kses_allowed_html( 'post', $input ); |
325
|
|
|
|
326
|
|
|
if ( ! is_array( $allowed_html ) ) { |
|
|
|
|
327
|
|
|
$allowed_html = wp_kses_allowed_html( 'post' ); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$filtered = trim( wp_unslash( $value ) ); |
|
|
|
|
331
|
|
|
$filtered = wp_kses( $filtered, $allowed_html ); |
332
|
|
|
$filtered = balanceTags( $filtered ); // Balances tags |
333
|
|
|
|
334
|
|
|
return $filtered; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Returns an array of allowed HTML tags and attributes for a given context. |
339
|
|
|
* |
340
|
|
|
* @since 0.1.41 |
341
|
|
|
* |
342
|
|
|
* @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
343
|
|
|
* 'strip', 'data', 'entities', or the name of a field filter such as |
344
|
|
|
* 'pre_user_description'. |
345
|
|
|
* @param array $input Input. |
346
|
|
|
* |
347
|
|
|
* @return array Array of allowed HTML tags and their allowed attributes. |
348
|
|
|
*/ |
349
|
|
|
public static function kses_allowed_html( $context = 'post', $input = array() ) { |
350
|
|
|
$allowed_html = wp_kses_allowed_html( $context ); |
351
|
|
|
|
352
|
|
|
if ( is_array( $allowed_html ) ) { |
|
|
|
|
353
|
|
|
// <iframe> |
354
|
|
|
if ( ! isset( $allowed_html['iframe'] ) && $context == 'post' ) { |
355
|
|
|
$allowed_html['iframe'] = array( |
356
|
|
|
'class' => true, |
357
|
|
|
'id' => true, |
358
|
|
|
'src' => true, |
359
|
|
|
'width' => true, |
360
|
|
|
'height' => true, |
361
|
|
|
'frameborder' => true, |
362
|
|
|
'marginwidth' => true, |
363
|
|
|
'marginheight' => true, |
364
|
|
|
'scrolling' => true, |
365
|
|
|
'style' => true, |
366
|
|
|
'title' => true, |
367
|
|
|
'allow' => true, |
368
|
|
|
'allowfullscreen' => true, |
369
|
|
|
'data-*' => true, |
370
|
|
|
); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Filters the allowed html tags. |
376
|
|
|
* |
377
|
|
|
* @since 0.1.41 |
378
|
|
|
* |
379
|
|
|
* @param array[]|string $allowed_html Allowed html tags. |
380
|
|
|
* @param @param string|array $context The context for which to retrieve tags. |
381
|
|
|
* @param array $input Input field. |
382
|
|
|
*/ |
383
|
|
|
return apply_filters( 'ayecode_ui_kses_allowed_html', $allowed_html, $context, $input ); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public static function get_column_class( $label_number = 2, $type = 'label' ) { |
387
|
|
|
|
388
|
|
|
$class = ''; |
389
|
|
|
|
390
|
|
|
// set default if empty |
391
|
|
|
if( $label_number === '' ){ |
392
|
|
|
$label_number = 2; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
if ( $label_number && $label_number < 12 && $label_number > 0 ) { |
396
|
|
|
if ( $type == 'label' ) { |
397
|
|
|
$class = 'col-sm-' . absint( $label_number ); |
398
|
|
|
} elseif ( $type == 'input' ) { |
399
|
|
|
$class = 'col-sm-' . ( 12 - absint( $label_number ) ); |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return $class; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Sanitizes a multiline string from user input or from the database. |
408
|
|
|
* |
409
|
|
|
* Emulate the WP native sanitize_textarea_field function in a %%variable%% safe way. |
410
|
|
|
* |
411
|
|
|
* @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php for the original |
412
|
|
|
* |
413
|
|
|
* @since 0.1.66 |
414
|
|
|
* |
415
|
|
|
* @param string $str String to sanitize. |
416
|
|
|
* @return string Sanitized string. |
417
|
|
|
*/ |
418
|
|
|
public static function sanitize_textarea_field( $str ) { |
419
|
|
|
$filtered = self::_sanitize_text_fields( $str, true ); |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Filters a sanitized textarea field string. |
423
|
|
|
* |
424
|
|
|
* @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php |
425
|
|
|
* |
426
|
|
|
* @param string $filtered The sanitized string. |
427
|
|
|
* @param string $str The string prior to being sanitized. |
428
|
|
|
*/ |
429
|
|
|
return apply_filters( 'sanitize_textarea_field', $filtered, $str ); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Internal helper function to sanitize a string from user input or from the db. |
434
|
|
|
* |
435
|
|
|
* @since 0.1.66 |
436
|
|
|
* @access private |
437
|
|
|
* |
438
|
|
|
* @param string $str String to sanitize. |
439
|
|
|
* @param bool $keep_newlines Optional. Whether to keep newlines. Default: false. |
440
|
|
|
* @return string Sanitized string. |
441
|
|
|
*/ |
442
|
|
|
public static function _sanitize_text_fields( $str, $keep_newlines = false ) { |
443
|
|
|
if ( is_object( $str ) || is_array( $str ) ) { |
|
|
|
|
444
|
|
|
return ''; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
$str = (string) $str; |
448
|
|
|
|
449
|
|
|
$filtered = wp_check_invalid_utf8( $str ); |
450
|
|
|
|
451
|
|
|
if ( strpos( $filtered, '<' ) !== false ) { |
452
|
|
|
$filtered = wp_pre_kses_less_than( $filtered ); |
453
|
|
|
// This will strip extra whitespace for us. |
454
|
|
|
$filtered = wp_strip_all_tags( $filtered, false ); |
455
|
|
|
|
456
|
|
|
// Use HTML entities in a special case to make sure no later |
457
|
|
|
// newline stripping stage could lead to a functional tag. |
458
|
|
|
$filtered = str_replace( "<\n", "<\n", $filtered ); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
if ( ! $keep_newlines ) { |
462
|
|
|
$filtered = preg_replace( '/[\r\n\t ]+/', ' ', $filtered ); |
463
|
|
|
} |
464
|
|
|
$filtered = trim( $filtered ); |
465
|
|
|
|
466
|
|
|
$found = false; |
467
|
|
|
while ( preg_match( '`[^%](%[a-f0-9]{2})`i', $filtered, $match ) ) { |
468
|
|
|
$filtered = str_replace( $match[1], '', $filtered ); |
469
|
|
|
$found = true; |
470
|
|
|
} |
471
|
|
|
unset( $match ); |
472
|
|
|
|
473
|
|
|
if ( $found ) { |
474
|
|
|
// Strip out the whitespace that may now exist after removing the octets. |
475
|
|
|
$filtered = trim( preg_replace( '` +`', ' ', $filtered ) ); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return $filtered; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Sanitize FontAwesome icon. |
483
|
|
|
* |
484
|
|
|
* @param string $icon Icon string. |
485
|
|
|
* @return string Sanitized icon. |
486
|
|
|
*/ |
487
|
|
|
public static function sanitize_fa_icon( $icon ) { |
488
|
|
|
if ( ! is_scalar( $icon ) ) { |
|
|
|
|
489
|
|
|
return ""; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$pattern = '/[^0-9a-zA-Z\-_ ]/'; |
493
|
|
|
|
494
|
|
|
$sanitized_icon = preg_replace( $pattern, '', trim( $icon ) ); |
495
|
|
|
|
496
|
|
|
return apply_filters( 'ayecode_ui_sanitize_fa_icon', $sanitized_icon, $icon ); |
497
|
|
|
} |
498
|
|
|
} |