| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (!defined('WPO_VERSION')) die('No direct access allowed'); | |
| 3 | +if (!defined('ABSPATH')) die('No direct access allowed'); | |
| 4 | 4 | |
| 5 | 5 | if (!class_exists('WPO_WebP_Self_Test')) : |
| 6 | 6 | |
| 7 | 7 | class WPO_WebP_Self_Test { |
| @@ -6,55 +6,70 @@ | ||
| 6 | 6 | |
| 7 | 7 | class WPO_WebP_Self_Test { |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Determines whether content type header has webp mime or not | |
| 10 | + * Relative path to the test image used for WebP serving verification. | |
| 11 | 11 | * |
| 12 | - * @param array $headers An array of headers | |
| 12 | + * @var string | |
| 13 | + */ | |
| 14 | + const TEST_IMAGE_PATH = '/wpo/images/wpo_logo_small.png'; | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * The MIME type for WebP images, used as the Accept header value | |
| 18 | + * and for content-type verification. | |
| 13 | 19 | * |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + const WEBP_MIME_TYPE = 'image/webp'; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Determines whether the content-type header indicates a WebP image. | |
| 26 | + * | |
| 27 | + * @param array<string, string> $headers An array of response headers | |
| 28 | + * | |
| 14 | 29 | * @return bool |
| 15 | 30 | */ |
| 16 | - private function has_webp_mime($headers) { | |
| 17 | - return isset($headers['content-type']) && 0 === strcasecmp('image/webp', $headers['content-type']); | |
| 31 | + private function has_webp_mime($headers): bool { | |
| 32 | + return isset($headers['content-type']) && 0 === strcasecmp(self::WEBP_MIME_TYPE, $headers['content-type']); | |
| 18 | 33 | } |
| 19 | 34 | |
| 20 | 35 | /** |
| 21 | - * Determines whether headers has `vary` header or not | |
| 36 | + * Determines whether the Vary header includes Accept. | |
| 22 | 37 | * |
| 23 | - * @param array $headers An array of headers | |
| 38 | + * @param array<string, string> $headers An array of response headers | |
| 24 | 39 | * |
| 25 | 40 | * @return bool |
| 26 | 41 | */ |
| 27 | - private function has_vary($headers) { | |
| 42 | + private function has_vary($headers): bool { | |
| 28 | 43 | return isset($headers['vary']) && preg_match('/accept/i', $headers['vary']); |
| 29 | 44 | } |
| 30 | 45 | |
| 31 | 46 | /** |
| 32 | - * Decided whether webp version is served or not | |
| 47 | + * Determines whether a WebP version is served when requested with | |
| 48 | + * an appropriate Accept header. | |
| 33 | 49 | * |
| 34 | 50 | * @return bool |
| 35 | 51 | */ |
| 36 | - public function is_webp_served() { | |
| 52 | + public function is_webp_served(): bool { | |
| 37 | 53 | $args = array( |
| 38 | 54 | 'headers' => array( |
| 39 | - 'accept' => 'image/webp' | |
| 55 | + 'accept' => self::WEBP_MIME_TYPE | |
| 40 | 56 | ) |
| 41 | 57 | ); |
| 42 | 58 | |
| 43 | 59 | $upload_dir = wp_upload_dir(); |
| 44 | - $url = $upload_dir['baseurl']. '/wpo/images/wpo_logo_small.png'; | |
| 60 | + $url = $upload_dir['baseurl'] . self::TEST_IMAGE_PATH; | |
| 45 | 61 | |
| 46 | 62 | $response = wp_remote_head($url, $args); |
| 47 | 63 | |
| 48 | - if (is_wp_error($response)) return false; | |
| 49 | - if (200 != $response['response']['code']) return false; | |
| 64 | + if (is_wp_error($response) || 200 !== $response['response']['code']) { | |
| 65 | + return false; | |
| 66 | + } | |
| 50 | 67 | |
| 51 | 68 | $headers = wp_remote_retrieve_headers($response); |
| 52 | - if (method_exists($headers, 'getAll')) { | |
| 69 | + if (is_object($headers) && method_exists($headers, 'getAll')) { | |
| 53 | 70 | $headers = $headers->getAll(); |
| 54 | - if ($this->has_webp_mime($headers) && $this->has_vary($headers)) { | |
| 55 | - return true; | |
| 56 | - } | |
| 71 | + return $this->has_webp_mime($headers) && $this->has_vary($headers); | |
| 57 | 72 | } |
| 58 | 73 | return false; |
| 59 | 74 | } |
| 60 | 75 | |
| @@ -62,9 +77,9 @@ | ||
| 62 | 77 | * Returns singleton instance |
| 63 | 78 | * |
| 64 | 79 | * @return WPO_WebP_Self_Test |
| 65 | 80 | */ |
| 66 | - public static function get_instance() { | |
| 81 | + public static function get_instance(): self { | |
| 67 | 82 | static $_instance = null; |
| 68 | 83 | if (null === $_instance) { |
| 69 | 84 | $_instance = new self(); |
| 70 | 85 | } |