-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathsanitize-functions.php
146 lines (128 loc) · 3.29 KB
/
sanitize-functions.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
<?php
/**
*
* Sanitize functions.
*
* Author: Andrei Baicus <[email protected]>
* Created on: 20/08/2018
*
* @package Neve\Globals
*/
/**
* Function to sanitize alpha color.
*
* @param string $value Hex or RGBA color.
*
* @return string
*/
function neve_sanitize_colors( $value ) {
// Is this an rgba color or a hex?
$mode = ( false === strpos( $value, 'rgba' ) ) ? 'hex' : 'rgba';
if ( 'rgba' === $mode ) {
return neve_sanitize_rgba( $value );
} else {
return sanitize_hex_color( $value );
}
}
/**
* Sanitize rgba color.
*
* @param string $value Color in rgba format.
*
* @return string
*/
function neve_sanitize_rgba( $value ) {
$red = 'rgba(0,0,0,0)';
$green = 'rgba(0,0,0,0)';
$blue = 'rgba(0,0,0,0)';
$alpha = 'rgba(0,0,0,0)'; // If empty or an array return transparent
if ( empty( $value ) || is_array( $value ) ) {
return '';
}
// By now we know the string is formatted as an rgba color so we need to further sanitize it.
$value = str_replace( ' ', '', $value );
sscanf( $value, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')';
}
/**
* Sanitize checkbox output.
*
* @param bool $value value to be sanitized.
*
* @return string
*/
function neve_sanitize_checkbox( $value ) {
return isset( $value ) && true === (bool) $value;
}
/**
* Check if a string is in json format
*
* @param string $string Input.
*
* @since 1.1.38
* @return bool
*/
function neve_is_json( $string ) {
return is_string( $string ) && is_array( json_decode( $string, true ) );
}
/**
* Sanitize values for range inputs.
*
* @param string $input Control input.
*
* @return float
*/
function neve_sanitize_range_value( $input ) {
if ( ! neve_is_json( $input ) ) {
return floatval( $input );
}
$range_value = json_decode( $input, true );
$range_value['desktop'] = ! empty( $range_value['desktop'] ) || $range_value['desktop'] === '0' ? floatval( $range_value['desktop'] ) : '';
$range_value['tablet'] = ! empty( $range_value['tablet'] ) || $range_value['tablet'] === '0' ? floatval( $range_value['tablet'] ) : '';
$range_value['mobile'] = ! empty( $range_value['mobile'] ) || $range_value['mobile'] === '0' ? floatval( $range_value['mobile'] ) : '';
return json_encode( $range_value );
}
/**
* Sanitize font weight values.
*
* @param string $value font-weight value.
*
* @return string
*/
function neve_sanitize_font_weight( $value ) {
$allowed = array( '100', '200', '300', '400', '500', '600', '700', '800', '900' );
if ( ! in_array( (string) $value, $allowed, true ) ) {
return '300';
}
return $value;
}
/**
* Sanitize font weight values.
*
* @param string $value font-weight value.
*
* @return string
*/
function neve_sanitize_text_transform( $value ) {
$allowed = array( 'none', 'capitalize', 'uppercase', 'lowercase' );
if ( ! in_array( $value, $allowed, true ) ) {
return 'none';
}
return $value;
}
/**
* Sanitize the background control.
*
* @param array $value input value.
*
* @return WP_Error | array
*/
function neve_sanitize_background( $value ) {
if ( ! is_array( $value ) ) {
return new WP_Error();
}
if ( ! isset( $value['type'] ) || ! in_array( $value['type'], array( 'image', 'color' ), true ) ) {
return new WP_Error();
}
return $value;
}