| 1173 | if ( ! function_exists( 'check_http_status_code' ) ) : |
| 1174 | /** |
| 1175 | * Verifies an HTTP status code is within a specific range |
| 1176 | * |
| 1177 | * @since 5.2.2 |
| 1178 | * |
| 1179 | * @param int $status HTTP status code. |
| 1180 | * @param string|array $range HTTP status code range or array of status code ranges such as 1XX, 2XX, etc. |
| 1181 | * Default 3XX. |
| 1182 | * @param bool $die Optional. Whether to die if the HTTP status code is not within specified range(s). |
| 1183 | * Default true. |
| 1184 | * @return false|int False if the code is out of range. Returns HTTP status code if within specified range(s). |
| 1185 | */ |
| 1186 | function check_http_status_code( $status = -1, $range = '3XX', $die = true ) { |
| 1187 | if ( -1 == $status ) { |
| 1188 | _doing_it_wrong( __FUNCTION__, __( 'You should specify a HTTP status code to be verified by using the first parameter.' ), '5.2' ); |
| 1189 | } |
| 1190 | |
| 1191 | $is_valid = false; // Assume $status is invalid |
| 1192 | $valid_range = false; // Assume $range is invalid |
| 1193 | |
| 1194 | $valid_codes = array ( |
| 1195 | '1XX' => array ( 100, 101, 102, 103, ), // Information responses |
| 1196 | '2XX' => array ( 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, ), // Successful responses |
| 1197 | '3XX' => array ( 300, 301, 302, 303, 304, 305, 306, 307, 308, ), // Redirection messages |
| 1198 | '4XX' => array ( 400, 401, 402, 403, 404, 404, 405, 406, 407, 408, 409, 410, // Client error responses |
| 1199 | 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, |
| 1200 | 425, 426, 428, 429, 431, 440, 444, 449, 451, 494, 495, 496, |
| 1201 | 497, 499, ), |
| 1202 | '5XX' => array ( 500, 501, 502, 503, 504, 505, 506, 507, 508, 501, 511, ), // Server error responses |
| 1203 | ); |
| 1204 | |
| 1205 | if ( is_array( $range ) ) { |
| 1206 | foreach ( $range as $r ) { |
| 1207 | if ( array_key_exists( $r, $valid_codes ) ) { |
| 1208 | $valid_range = true; |
| 1209 | if ( in_array( $status, $valid_codes[$r] ) ) { |
| 1210 | $is_valid = $status; |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | } else { |
| 1215 | if ( array_key_exists( $range, $valid_codes ) ) { |
| 1216 | $valid_range = true; |
| 1217 | if ( in_array( $status, $valid_codes[$range] ) ) { |
| 1218 | $is_valid = $status; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | if ( false === $valid_range ) { |
| 1224 | _doing_it_wrong( __FUNCTION__, __( 'You should specify a valid HTTP status code range to be verified by using the second parameter.' ), '5.2' ); |
| 1225 | } |
| 1226 | |
| 1227 | if ( $die && false === $is_valid ) { |
| 1228 | wp_die( __( 'HTTP status code out of range.' ) ); |
| 1229 | } |
| 1230 | |
| 1231 | return $is_valid; |
| 1232 | } |
| 1233 | endif; |
| 1234 | |