| @@ -12,8 +12,13 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | if (!defined('WPO_CACHE_FILES_DIR')) define('WPO_CACHE_FILES_DIR', untrailingslashit(WP_CONTENT_DIR).'/cache/wpo-cache'); |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | + * Minimum Firefox version for WebP support | |
| 17 | + */ | |
| 18 | +if (!defined('WPO_MIN_FIREFOX_VERSION_FOR_WEBP')) define('WPO_MIN_FIREFOX_VERSION_FOR_WEBP', '65.0.0'); | |
| 19 | + | |
| 20 | +/** | |
| 16 | 21 | * Holds utility functions used by file based cache |
| 17 | 22 | */ |
| 18 | 23 | |
| 19 | 24 | /** |
| @@ -89,10 +94,14 @@ | ||
| 89 | 94 | $no_cache_because[] = __('Comments are opened and the visitor saved his information.', 'wp-optimize'); |
| 90 | 95 | } |
| 91 | 96 | } |
| 92 | 97 | |
| 98 | + if (wpo_restricted_cache_page_with_cart_items()) { | |
| 99 | + $no_cache_because[] = __('User has items in WooCommerce cart.', 'wp-optimize'); | |
| 100 | + } | |
| 101 | + | |
| 93 | 102 | $can_cache_page = true; |
| 94 | - | |
| 103 | + | |
| 95 | 104 | if (defined('DONOTCACHEPAGE') && DONOTCACHEPAGE) { |
| 96 | 105 | $can_cache_page = false; |
| 97 | 106 | } |
| 98 | 107 | |
| @@ -443,10 +452,10 @@ | ||
| 443 | 452 | |
| 444 | 453 | if (wpo_cache_mobile_caching_enabled() && wpo_is_mobile()) { |
| 445 | 454 | $filename = 'mobile.' . $filename; |
| 446 | 455 | } |
| 447 | - | |
| 448 | - if (wpo_webp_images_enabled() && !wpo_is_using_webp_images_redirection() && wpo_is_using_alter_html()) { | |
| 456 | + | |
| 457 | + if (wpo_webp_images_enabled() && !wpo_is_using_webp_images_redirection() && wpo_is_browser_supports_webp()) { | |
| 449 | 458 | $filename = $filename . '.webp'; |
| 450 | 459 | } |
| 451 | 460 | |
| 452 | 461 | $cookies = wpo_cache_cookies(); |
| @@ -726,15 +735,27 @@ | ||
| 726 | 735 | } |
| 727 | 736 | endif; |
| 728 | 737 | |
| 729 | 738 | /** |
| 730 | - * Check whether webp images using alter html method or not | |
| 739 | + * Check whether the requesting browser supports WebP images | |
| 731 | 740 | * |
| 732 | 741 | * @return bool |
| 733 | 742 | */ |
| 734 | -if (!function_exists('wpo_is_using_alter_html')) : | |
| 735 | - function wpo_is_using_alter_html() { | |
| 736 | - return (isset($_SERVER['HTTP_ACCEPT']) && false !== strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Executes before WP fully loads, only doing string comparison | |
| 743 | +if (!function_exists('wpo_is_browser_supports_webp')) : | |
| 744 | + function wpo_is_browser_supports_webp() { | |
| 745 | + // Direct Accept header check (works for image sub-resource requests) | |
| 746 | + if (isset($_SERVER['HTTP_ACCEPT']) && false !== strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Executes before WP fully loads, only doing string comparison | |
| 747 | + return true; | |
| 748 | + } | |
| 749 | + | |
| 750 | + // Fallback for older Firefox versions, which support WebP but don't send 'image/webp' in the Accept header. | |
| 751 | + $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Executes before WP fully loads, only extracting a version number via regex for a version_compare, not used for output or storage | |
| 752 | + | |
| 753 | + if (!empty($user_agent) && preg_match('/Firefox\/([\d\.]+[a-z\d]*)/', $user_agent, $matches)) { | |
| 754 | + return version_compare(WPO_MIN_FIREFOX_VERSION_FOR_WEBP, $matches[1], '<='); | |
| 755 | + } | |
| 756 | + | |
| 757 | + return false; | |
| 737 | 758 | } |
| 738 | 759 | endif; |
| 739 | 760 | |
| 740 | 761 | /** |
| @@ -828,9 +849,11 @@ | ||
| 828 | 849 | |
| 829 | 850 | if ($use_gzip) $path .= '.gz'; |
| 830 | 851 | |
| 831 | 852 | $modified_time = file_exists($path) ? (int) filemtime($path) : time(); |
| 832 | - | |
| 853 | + | |
| 854 | + $modified_time = apply_filters('wpo_cache_modified_time', $modified_time, $path); | |
| 855 | + | |
| 833 | 856 | // Cache has expired, purge and exit. |
| 834 | 857 | if (!empty($GLOBALS['wpo_cache_config']['page_cache_length'])) { |
| 835 | 858 | if (time() > ($GLOBALS['wpo_cache_config']['page_cache_length'] + $modified_time)) { |
| 836 | 859 | wpo_delete_files($path); |
| @@ -1020,8 +1043,12 @@ | ||
| 1020 | 1043 | } |
| 1021 | 1044 | } |
| 1022 | 1045 | } |
| 1023 | 1046 | |
| 1047 | + if (wpo_restricted_cache_page_with_cart_items()) { | |
| 1048 | + $no_cache_because[] = 'User has items in WooCommerce cart.'; | |
| 1049 | + } | |
| 1050 | + | |
| 1024 | 1051 | $restricted_page_type_cache = wpo_restricted_cache_page_type(''); |
| 1025 | 1052 | if (!empty($restricted_page_type_cache)) { |
| 1026 | 1053 | $no_cache_because[] = $restricted_page_type_cache; |
| 1027 | 1054 | } |
| @@ -1062,8 +1089,19 @@ | ||
| 1062 | 1089 | if (!empty($no_cache_because)) return $no_cache_because; |
| 1063 | 1090 | |
| 1064 | 1091 | return true; |
| 1065 | 1092 | } |
| 1093 | +endif; | |
| 1094 | + | |
| 1095 | +/** | |
| 1096 | + * Checks if the current request has WooCommerce cart items. | |
| 1097 | + * | |
| 1098 | + * @return bool Returns true if the user has items in the WooCommerce cart, false otherwise. | |
| 1099 | + */ | |
| 1100 | +if (!function_exists('wpo_restricted_cache_page_with_cart_items')) : | |
| 1101 | + function wpo_restricted_cache_page_with_cart_items(): bool { | |
| 1102 | + return !empty($_COOKIE['woocommerce_items_in_cart']) || !empty($_COOKIE['woocommerce_cart_hash']); | |
| 1103 | + } | |
| 1066 | 1104 | endif; |
| 1067 | 1105 | |
| 1068 | 1106 | /** |
| 1069 | 1107 | * Checks if the current request is a cacheable sitemap request |