@@ 93-138 (lines=46) @@ | ||
90 | return null; |
|
91 | } |
|
92 | ||
93 | static function validate( $data, $type = null ) { |
|
94 | if ( is_null( $data ) ) { |
|
95 | return $data; |
|
96 | } |
|
97 | switch( $type ) { |
|
98 | case 'bool': |
|
99 | return boolval( $data ); |
|
100 | case 'url': |
|
101 | return esc_url( $data ); |
|
102 | case 'on': |
|
103 | return ( 'on' == $data ? true : false ); |
|
104 | break; |
|
105 | case 'closed': |
|
106 | return ( 'closed' != $data ? true : false ); |
|
107 | case 'string': |
|
108 | return strval( $data ); |
|
109 | case 'int': |
|
110 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
|
111 | case 'float': |
|
112 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
|
113 | case 'array': |
|
114 | return ( is_array( $data ) ? $data : array() ); |
|
115 | case 'rtrim-slash': |
|
116 | return strval( rtrim( $data, '/' ) ); |
|
117 | } |
|
118 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
|
119 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
|
120 | } elseif ( is_array( $type ) ) { |
|
121 | // Is the array associative? |
|
122 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
|
123 | foreach ( $type as $item => $check ) { |
|
124 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
|
125 | } |
|
126 | return $data; |
|
127 | } else { |
|
128 | // check if the value exists in the array if not return the first value. |
|
129 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
|
130 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
|
131 | } |
|
132 | } |
|
133 | // Don't check for validity here |
|
134 | if ( 'no-validation' == $type ) { |
|
135 | return $data; |
|
136 | } |
|
137 | return null; |
|
138 | } |
|
139 | ||
140 | static function validate_option( $data, $type = null ) { |
|
141 | if ( is_null( $data ) ) { |
|
@@ 140-185 (lines=46) @@ | ||
137 | return null; |
|
138 | } |
|
139 | ||
140 | static function validate_option( $data, $type = null ) { |
|
141 | if ( is_null( $data ) ) { |
|
142 | return $data; |
|
143 | } |
|
144 | switch( $type ) { |
|
145 | case 'bool': |
|
146 | return boolval( $data ); |
|
147 | case 'url': |
|
148 | return esc_url( $data ); |
|
149 | case 'on': |
|
150 | return ( 'on' == $data ? true : false ); |
|
151 | break; |
|
152 | case 'closed': |
|
153 | return ( 'closed' != $data ? true : false ); |
|
154 | case 'string': |
|
155 | return strval( $data ); |
|
156 | case 'int': |
|
157 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
|
158 | case 'float': |
|
159 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
|
160 | case 'array': |
|
161 | return ( is_array( $data ) ? $data : array() ); |
|
162 | case 'rtrim-slash': |
|
163 | return strval( rtrim( $data, '/' ) ); |
|
164 | } |
|
165 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
|
166 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
|
167 | } elseif ( is_array( $type ) ) { |
|
168 | // Is the array associative? |
|
169 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
|
170 | foreach ( $type as $item => $check ) { |
|
171 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
|
172 | } |
|
173 | return $data; |
|
174 | } else { |
|
175 | // check if the value exists in the array if not return the first value. |
|
176 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
|
177 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
|
178 | } |
|
179 | } |
|
180 | // Don't check for validity here |
|
181 | if ( 'no-validation' == $type ) { |
|
182 | return $data; |
|
183 | } |
|
184 | return null; |
|
185 | } |
|
186 | } |
|
187 |