| 1 |
<?php |
| 2 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 3 |
|
| 4 |
if (!trait_exists('WP_Optimize_HTTP_Error_Codes_Trait')) : |
| 5 |
trait WP_Optimize_HTTP_Error_Codes_Trait { |
| 6 |
|
| 7 |
/** |
| 8 |
* List of HTTP semantic codes taken from |
| 9 |
* https://en.wikipedia.org/wiki/List_of_HTTP_status_codes |
| 10 |
* This list includes non-default HTTP codes that are not part of RFC 9110 but might appear on the wild |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
private $http_codes; |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize `$this->http_codes` table |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
private function set_codes() { |
| 22 |
$this->http_codes = array( |
| 23 |
100 => __('Continue', 'wp-optimize'), |
| 24 |
101 => __('Switching Protocols', 'wp-optimize'), |
| 25 |
102 => __('Processing (WebDAV; RFC 2518)', 'wp-optimize'), |
| 26 |
103 => __('Early Hints (RFC 8297)', 'wp-optimize'), |
| 27 |
200 => __('OK', 'wp-optimize'), |
| 28 |
201 => __('Created', 'wp-optimize'), |
| 29 |
202 => __('Accepted', 'wp-optimize'), |
| 30 |
203 => __('Non-Authoritative Information (since HTTP/1.1)', 'wp-optimize'), |
| 31 |
204 => __('No Content', 'wp-optimize'), |
| 32 |
205 => __('Reset Content', 'wp-optimize'), |
| 33 |
206 => __('Partial Content', 'wp-optimize'), |
| 34 |
207 => __('Multi-Status (WebDAV; RFC 4918)', 'wp-optimize'), |
| 35 |
208 => __('Already Reported (WebDAV; RFC 5842)', 'wp-optimize'), |
| 36 |
226 => __('IM Used (RFC 3229)', 'wp-optimize'), |
| 37 |
300 => __('Multiple Choices', 'wp-optimize'), |
| 38 |
301 => __('Moved Permanently', 'wp-optimize'), |
| 39 |
302 => __('Found (Previously "Moved temporarily")', 'wp-optimize'), |
| 40 |
303 => __('See Other (since HTTP/1.1)', 'wp-optimize'), |
| 41 |
304 => __('Not Modified', 'wp-optimize'), |
| 42 |
305 => __('Use Proxy (since HTTP/1.1)', 'wp-optimize'), |
| 43 |
306 => __('Switch Proxy', 'wp-optimize'), |
| 44 |
307 => __('Temporary Redirect (since HTTP/1.1)', 'wp-optimize'), |
| 45 |
308 => __('Permanent Redirect', 'wp-optimize'), |
| 46 |
400 => __('Bad Request', 'wp-optimize'), |
| 47 |
401 => __('Unauthorized', 'wp-optimize'), |
| 48 |
402 => __('Payment Required', 'wp-optimize'), |
| 49 |
403 => __('Forbidden', 'wp-optimize'), |
| 50 |
404 => __('Not Found', 'wp-optimize'), |
| 51 |
405 => __('Method Not Allowed', 'wp-optimize'), |
| 52 |
406 => __('Not Acceptable', 'wp-optimize'), |
| 53 |
407 => __('Proxy Authentication Required', 'wp-optimize'), |
| 54 |
408 => __('Request Timeout', 'wp-optimize'), |
| 55 |
409 => __('Conflict', 'wp-optimize'), |
| 56 |
410 => __('Gone', 'wp-optimize'), |
| 57 |
411 => __('Length Required', 'wp-optimize'), |
| 58 |
412 => __('Precondition Failed', 'wp-optimize'), |
| 59 |
413 => __('Payload Too Large', 'wp-optimize'), |
| 60 |
414 => __('URI Too Long', 'wp-optimize'), |
| 61 |
415 => __('Unsupported Media Type', 'wp-optimize'), |
| 62 |
416 => __('Range Not Satisfiable', 'wp-optimize'), |
| 63 |
417 => __('Expectation Failed', 'wp-optimize'), |
| 64 |
418 => __('I\'m a teapot (RFC 2324, RFC 7168)', 'wp-optimize'), |
| 65 |
421 => __('Misdirected Request', 'wp-optimize'), |
| 66 |
422 => __('Unprocessable Entity', 'wp-optimize'), |
| 67 |
423 => __('Locked (WebDAV; RFC 4918)', 'wp-optimize'), |
| 68 |
424 => __('Failed Dependency (WebDAV; RFC 4918)', 'wp-optimize'), |
| 69 |
425 => __('Too Early (RFC 8470)', 'wp-optimize'), |
| 70 |
426 => __('Upgrade Required', 'wp-optimize'), |
| 71 |
428 => __('Precondition Required (RFC 6585)', 'wp-optimize'), |
| 72 |
429 => __('Too Many Requests (RFC 6585)', 'wp-optimize'), |
| 73 |
431 => __('Request Header Fields Too Large (RFC 6585)', 'wp-optimize'), |
| 74 |
451 => __('Unavailable For Legal Reasons (RFC 7725)', 'wp-optimize'), |
| 75 |
500 => __('Internal Server Error', 'wp-optimize'), |
| 76 |
501 => __('Not Implemented', 'wp-optimize'), |
| 77 |
502 => __('Bad Gateway', 'wp-optimize'), |
| 78 |
503 => __('Service Unavailable', 'wp-optimize'), |
| 79 |
504 => __('Gateway Timeout', 'wp-optimize'), |
| 80 |
505 => __('HTTP Version Not Supported', 'wp-optimize'), |
| 81 |
506 => __('Variant Also Negotiates (RFC 2295)', 'wp-optimize'), |
| 82 |
507 => __('Insufficient Storage (WebDAV; RFC 4918)', 'wp-optimize'), |
| 83 |
508 => __('Loop Detected (WebDAV; RFC 5842)', 'wp-optimize'), |
| 84 |
510 => __('Not Extended (RFC 2774)', 'wp-optimize'), |
| 85 |
511 => __('Network Authentication Required (RFC 6585)', 'wp-optimize'), |
| 86 |
218 => __('This is fine (Apache HTTP Server)', 'wp-optimize'), |
| 87 |
419 => __('Page Expired (Laravel Framework)', 'wp-optimize'), |
| 88 |
420 => __('Method Failure (Spring Framework)', 'wp-optimize'), |
| 89 |
430 => __('Request Header Fields Too Large (Shopify)', 'wp-optimize'), |
| 90 |
450 => __('Blocked by Windows Parental Controls (Microsoft)', 'wp-optimize'), |
| 91 |
498 => __('Invalid Token (Esri)', 'wp-optimize'), |
| 92 |
509 => __('Bandwidth Limit Exceeded (Apache Web Server/cPanel)', 'wp-optimize'), |
| 93 |
529 => __('Site is overloaded', 'wp-optimize'), |
| 94 |
530 => __('Site is frozen', 'wp-optimize'), |
| 95 |
598 => __('(Informal convention) Network read timeout error', 'wp-optimize'), |
| 96 |
599 => __('Network Connect Timeout Error', 'wp-optimize'), |
| 97 |
440 => __('IIS - Login Time-out', 'wp-optimize'), |
| 98 |
449 => __('IIS - Retry With', 'wp-optimize'), |
| 99 |
444 => __('Nginx - No Response', 'wp-optimize'), |
| 100 |
494 => __('Nginx - Request header too large', 'wp-optimize'), |
| 101 |
495 => __('Nginx - SSL Certificate Error', 'wp-optimize'), |
| 102 |
496 => __('Nginx - SSL Certificate Required', 'wp-optimize'), |
| 103 |
497 => __('Nginx - HTTP Request Sent to HTTPS Port', 'wp-optimize'), |
| 104 |
499 => __('Nginx - Client Closed Request', 'wp-optimize'), |
| 105 |
520 => __('Cloudflare - Web Server Returned an Unknown Error', 'wp-optimize'), |
| 106 |
521 => __('Cloudflare - Web Server Is Down', 'wp-optimize'), |
| 107 |
522 => __('Cloudflare - Connection Timed Out', 'wp-optimize'), |
| 108 |
523 => __('Cloudflare - Origin Is Unreachable', 'wp-optimize'), |
| 109 |
524 => __('Cloudflare - A Timeout Occurred', 'wp-optimize'), |
| 110 |
525 => __('Cloudflare - SSL Handshake Failed', 'wp-optimize'), |
| 111 |
526 => __('Cloudflare - Invalid SSL Certificate', 'wp-optimize'), |
| 112 |
527 => __('Cloudflare - Railgun Error', 'wp-optimize'), |
| 113 |
460 => __('AWS Load Balancer - Connection Closed', 'wp-optimize'), |
| 114 |
463 => __('AWS Load Balancer - Too Many Redirects', 'wp-optimize'), |
| 115 |
464 => __('AWS Load Balancer - Incompatible Protocols', 'wp-optimize'), |
| 116 |
561 => __('AWS Load Balancer - Unauthorized', 'wp-optimize'), |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the current theme's style.css headers |
| 122 |
* |
| 123 |
* @return array|WP_Error |
| 124 |
*/ |
| 125 |
protected function get_stylesheet_headers() { |
| 126 |
static $headers; |
| 127 |
if (isset($headers)) return $headers; |
| 128 |
|
| 129 |
$style = get_template_directory_uri() . '/style.css'; |
| 130 |
|
| 131 |
/** |
| 132 |
* Filters wp_remote_get parameters, when checking if browser cache is enabled. |
| 133 |
* |
| 134 |
* @param array $request_params Default parameters |
| 135 |
*/ |
| 136 |
$request_params = apply_filters('wpoptimize_get_stylesheet_headers_args', array('timeout' => 10)); |
| 137 |
|
| 138 |
// trying to load style.css. |
| 139 |
$response = wp_remote_get($style, $request_params); |
| 140 |
|
| 141 |
if (is_wp_error($response)) return $response; |
| 142 |
|
| 143 |
$response_code = wp_remote_retrieve_response_code($response); |
| 144 |
if (200 != $response_code) { |
| 145 |
$link = sprintf('<a href="%s" target="_blank">%s</a>', $style, $style); |
| 146 |
|
| 147 |
if (401 == $response_code) { |
| 148 |
$error = '<b>' . esc_html__('401 Unauthorized', 'wp-optimize') . '</b>'; |
| 149 |
$error_string = sprintf(esc_html__('The server responded with a %s error.', 'wp-optimize'), $error) . ' ' |
| 150 |
. esc_html__('This usually happens when the site requires authentication to access it.', 'wp-optimize') . ' ' |
| 151 |
. sprintf(esc_html__('Please temporarily disable authentication for the following URL: %s and retry.', 'wp-optimize'), $link); |
| 152 |
} elseif (404 == $response_code) { |
| 153 |
$error = '<b>' . esc_html__('404 File Not Found', 'wp-optmize') . '</b>'; |
| 154 |
$error_string = sprintf(esc_html__('The server responded with a %s error', 'wp-optimize'), $error) . ' ' |
| 155 |
. sprintf(esc_html__('Please temporarily restore the file under URL: %s, or just create an empty file and try again.', 'wp-optimize'), $link); |
| 156 |
} else { |
| 157 |
$error = '<b>' . $response_code . ' ' . $this->get_http_code_label($response_code) . '</b>'; |
| 158 |
$error_string = sprintf(esc_html__('The server responded with a %s error whilst trying to open the URL: %s.', 'wp-optimize'), $error, $link) . ' ' |
| 159 |
. esc_html__('Please contact your website administrator to resolve this.', 'wp-optimize') . ' ' . esc_html__('Once the issue is addressed, try again.', 'wp-optimize'); |
| 160 |
} |
| 161 |
|
| 162 |
return new WP_Error('unauthorized', $error_string); |
| 163 |
} |
| 164 |
|
| 165 |
$headers = wp_remote_retrieve_headers($response); |
| 166 |
|
| 167 |
if (is_object($headers) && method_exists($headers, 'getAll')) { |
| 168 |
$headers = $headers->getAll(); |
| 169 |
} |
| 170 |
|
| 171 |
return is_array($headers) ? $headers : array(); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Return label for HTTP code |
| 176 |
* |
| 177 |
* @param int $code The HTTP code |
| 178 |
* @return string |
| 179 |
*/ |
| 180 |
private function get_http_code_label($code) { |
| 181 |
if (!isset($this->http_codes)) { |
| 182 |
$this->set_codes(); |
| 183 |
} |
| 184 |
|
| 185 |
return $code . " " . !empty($this->http_codes[$code]) ? $this->http_codes[$code] : esc_html__("HTTP Code", "wp-optimize"); |
| 186 |
} |
| 187 |
} |
| 188 |
endif; |
| 189 |
|