PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | webp/class-wpo-webp-self-test.php +42 -38 3.2.5 → 4.7.0 View file →
@@ -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,67 +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 - WP_Optimize()->log('Checking if we can get webp image');
16 - $args = array(
17 - 'headers' => array(
18 - 'accept' => 'image/webp'
19 - )
20 - );
14 + const TEST_IMAGE_PATH = '/wpo/images/wpo_logo_small.png';
21 15
22 - $upload_dir = wp_upload_dir();
23 - $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';
24 23
25 - $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 + }
26 34
27 - if (is_wp_error($response)) return false;
28 - if (200 != $response['response']['code']) return false;
29 -
30 - $headers = wp_remote_retrieve_headers($response);
31 - if (is_a($headers, 'Requests_Utility_CaseInsensitiveDictionary')) {
32 - $headers = $headers->getAll();
33 - if (isset($headers['content-type']) && 'image/webp' == $headers['content-type']) {
34 - return true;
35 - }
36 - }
37 -
38 - 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']);
39 44 }
40 45
41 46 /**
42 - * Decided whether webp version is served or not
47 + * Determines whether a WebP version is served when requested with
48 + * an appropriate Accept header.
43 49 *
44 50 * @return bool
45 51 */
46 - public function is_webp_served() {
52 + public function is_webp_served(): bool {
47 53 $args = array(
48 54 'headers' => array(
49 - 'accept' => 'image/webp'
55 + 'accept' => self::WEBP_MIME_TYPE
50 56 )
51 57 );
52 58
53 59 $upload_dir = wp_upload_dir();
54 - $url = $upload_dir['baseurl']. '/wpo/images/wpo_logo_small.png';
60 + $url = $upload_dir['baseurl'] . self::TEST_IMAGE_PATH;
55 61
56 - $response = wp_remote_get($url, $args);
62 + $response = wp_remote_head($url, $args);
57 63
58 - if (is_wp_error($response)) return false;
59 - if (200 != $response['response']['code']) return false;
64 + if (is_wp_error($response) || 200 !== $response['response']['code']) {
65 + return false;
66 + }
60 67
61 68 $headers = wp_remote_retrieve_headers($response);
62 - if (is_a($headers, 'Requests_Utility_CaseInsensitiveDictionary')) {
69 + if (is_object($headers) && method_exists($headers, 'getAll')) {
63 70 $headers = $headers->getAll();
64 - if (isset($headers['content-type']) && 'image/webp' == $headers['content-type']) {
65 - return true;
66 - }
71 + return $this->has_webp_mime($headers) && $this->has_vary($headers);
67 72 }
68 -
69 73 return false;
70 74 }
71 75
72 76 /**
@@ -73,9 +77,9 @@
73 77 * Returns singleton instance
74 78 *
75 79 * @return WPO_WebP_Self_Test
76 80 */
77 - public static function get_instance() {
81 + public static function get_instance(): self {
78 82 static $_instance = null;
79 83 if (null === $_instance) {
80 84 $_instance = new self();
81 85 }