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