-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsanitizes.php
168 lines (144 loc) · 3.6 KB
/
sanitizes.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
<?php
/**
* Sanitization Functions.
*
* This file demonstrates how to define sanitization callback functions for various data types.
*
* @package Suru/Libs
*/
/**
* Sanitizes a simple text string.
*
* @param mixed $value The string to sanitize.
* @return string
*/
function wplibs_sanitize_text( $value ) {
return sanitize_text_field( wp_unslash( $value ) );
}
/**
* Sanitizes content that could contain HTML.
*
* @param mixed $value The HTML string to sanitize.
* @return string
*/
function wplibs_sanitize_html( $value ) {
return wp_filter_post_kses( $value );
}
/**
* Sanitizes content that could not contain HTML.
*
* @param string $nohtml The no-HTML content to sanitize.
* @return string
*/
function wplibs_sanitize_nohtml( $nohtml ) {
return wp_filter_nohtml_kses( $nohtml );
}
/**
* Sanitizes for checkbox or toggle.
*
* @param mixed $value The checkbox value.
* @return string
*/
function wplibs_sanitize_checkbox( $value ) {
return in_array( $value, [ 'on', 'yes', 'true', true ] ) ? '1' : '0';
}
/**
* Sanitizes a color value with support bold hex & rgba.
*
* @param string $color The value to sanitize.
* @return string
*/
function wplibs_sanitize_color( $color ) {
$color = trim( $color );
if ( empty( $color ) || ! is_string( $color ) ) {
return '';
}
if ( 'transparent' === $color ) {
return $color;
}
// Given a color name, return the hex.
static $color_names;
if ( ! $color_names ) {
$color_names = json_decode(
file_get_contents( __DIR__ . '/src/Resources/color-names.json' ), true
);
}
if ( is_array( $color_names ) && array_key_exists( $color, $color_names ) ) {
return $color_names[ $color ];
}
if ( false !== strpos( $color, '#' ) ) {
return sanitize_hex_color( $color );
}
if ( false !== strpos( $color, 'rgba(' ) ) {
return wplibs_sanitize_rgba_color( $color );
}
return '';
}
/**
* Sanitizes an RGBA color value.
*
* @param string $color The RGBA color value to sanitize.
* @return string
*/
function wplibs_sanitize_rgba_color( $color ) {
// Trim unneeded whitespace.
$color = trim( str_replace( ' ', '', $color ) );
/* @noinspection PhpUndefinedVariableInspection */
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 '';
}
/**
* Sanitizes emails address.
*
* @param string $email The email to sanitize.
* @return string
*/
function wplibs_sanitize_emails( $email ) {
if ( ! is_array( $email ) ) {
$email = array_map( 'trim', explode( ',', $email ) );
}
return implode( ', ', array_filter( $email, 'is_email' ) );
}
/**
* Sanitizes comma-separated list of IDs.
*
* @param string $list The value to sanitize.
* @return string
*/
function wplibs_sanitize_ids( $list ) {
return implode( ', ', wp_parse_id_list( $list ) );
}
/**
* Sanitization callback for 'url' type text inputs.
*
* @param string $url URL to sanitize.
* @return string
*/
function wplibs_sanitize_url( $url ) {
return esc_url_raw( $url );
}
/**
* Sanitization callback for 'css' type textarea inputs.
*
* @param string $css CSS to sanitize.
* @return string
*/
function wplibs_sanitize_css( $css ) {
return trim( wp_strip_all_tags( $css ) );
}
/**
* Sanitization callback for 'dropdown-pages' type controls.
*
* @param int $page_id Page ID.
* @return int|null
*/
function wplibs_sanitize_dropdown_pages( $page_id ) {
$page_id = absint( $page_id );
return 'publish' === get_post_status( $page_id ) ? $page_id : null;
}