CacheService.php
2 weeks ago
CacheServiceProvider.php
2 weeks ago
DoNotCachePageService.php
2 weeks ago
LiteSpeedCacheService.php
4 months ago
W3TotalCacheService.php
4 months ago
WpFastestCacheService.php
4 months ago
CacheService.php
254 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Cache; |
| 4 | |
| 5 | /** |
| 6 | * Abstract Cache Service. |
| 7 | */ |
| 8 | abstract class CacheService { |
| 9 | /** |
| 10 | * Bootstrap the service. |
| 11 | * |
| 12 | * @return void |
| 13 | */ |
| 14 | public function bootstrap() { |
| 15 | // Early return if the cache plugin is not active. |
| 16 | if ( ! $this->isCachePluginActive() ) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Disable cache for SureCart dynamic pages. |
| 21 | add_action( 'wp', [ $this, 'maybeDisableCache' ] ); |
| 22 | |
| 23 | // Disable cache for SureCart REST API requests. |
| 24 | add_action( 'rest_api_init', [ $this, 'maybeDisableCacheForRestApi' ], 1 ); |
| 25 | |
| 26 | // Purge cache when product stock is adjusted. |
| 27 | add_action( 'surecart/product_stock_adjusted', [ $this, 'purgeProductCacheOnStockAdjustment' ] ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Disable cache for the current page. |
| 32 | * |
| 33 | * @param string $reason Reason for disabling cache. |
| 34 | * @return void |
| 35 | */ |
| 36 | abstract protected function disableCache( string $reason ): void; |
| 37 | |
| 38 | /** |
| 39 | * Check if the cache plugin is active. |
| 40 | * |
| 41 | * @return bool |
| 42 | */ |
| 43 | abstract protected function isCachePluginActive(): bool; |
| 44 | |
| 45 | /** |
| 46 | * Check if the current page should be excluded from cache. |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public function shouldExcludeFromCache(): bool { |
| 51 | return $this->isCustomerDashboardPage() |
| 52 | || $this->isCheckoutPage() |
| 53 | || $this->hasCheckoutFormBlock() |
| 54 | || $this->isBuyPage(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Maybe disable cache for SureCart dynamic pages. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function maybeDisableCache() { |
| 63 | if ( $this->isCustomerDashboardPage() ) { |
| 64 | $this->disableCacheWithBrowserHeaders( 'SureCart customer dashboard' ); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if ( $this->isCheckoutPage() ) { |
| 69 | $this->disableCacheWithBrowserHeaders( 'SureCart checkout page' ); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if ( $this->hasCheckoutFormBlock() ) { |
| 74 | $this->disableCacheWithBrowserHeaders( 'SureCart checkout form block' ); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | if ( $this->isBuyPage() ) { |
| 79 | $this->disableCacheWithBrowserHeaders( 'SureCart buy page' ); |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Disable both server-side and browser caching. |
| 86 | * |
| 87 | * @param string $reason Reason for disabling cache. |
| 88 | * @return void |
| 89 | */ |
| 90 | protected function disableCacheWithBrowserHeaders( string $reason ): void { |
| 91 | // Disable server-side caching via the cache plugin. |
| 92 | $this->disableCache( $reason ); |
| 93 | |
| 94 | // Disable browser caching by sending no-cache headers. |
| 95 | if ( ! headers_sent() ) { |
| 96 | nocache_headers(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Maybe disable cache for SureCart REST API requests. |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | public function maybeDisableCacheForRestApi() { |
| 106 | if ( $this->isSureCartRestRequest() ) { |
| 107 | $this->disableCacheWithBrowserHeaders( 'SureCart REST API request' ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check if the current request is a SureCart REST API request. |
| 113 | * |
| 114 | * @return bool |
| 115 | */ |
| 116 | protected function isSureCartRestRequest(): bool { |
| 117 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 118 | |
| 119 | if ( strpos( $request_uri, '/surecart/' ) !== false && strpos( $request_uri, 'wp-json' ) !== false ) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 124 | $rest_route = isset( $_GET['rest_route'] ) ? sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ) : ''; |
| 125 | if ( strpos( $rest_route, '/surecart/' ) !== false ) { |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Purge product cache when stock is adjusted. |
| 134 | * |
| 135 | * @param \SureCart\Models\Product $product The product model. |
| 136 | * @return void |
| 137 | */ |
| 138 | public function purgeProductCacheOnStockAdjustment( $product ) { |
| 139 | // Override in child classes if needed. |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Check if the current page is the customer dashboard page. |
| 144 | * |
| 145 | * Uses is_page() instead of a URL comparison so dashboard subviews |
| 146 | * with query args (?action=index&model=invoice) are also matched. |
| 147 | * |
| 148 | * @return bool |
| 149 | */ |
| 150 | protected function isCustomerDashboardPage(): bool { |
| 151 | $dashboard_page_id = \SureCart::pages()->getId( 'dashboard' ); |
| 152 | |
| 153 | if ( empty( $dashboard_page_id ) ) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | return is_page( $dashboard_page_id ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check if the current page is the checkout page. |
| 162 | * |
| 163 | * @return bool |
| 164 | */ |
| 165 | protected function isCheckoutPage(): bool { |
| 166 | $checkout_page_id = \SureCart::pages()->getId( 'checkout' ); |
| 167 | |
| 168 | if ( empty( $checkout_page_id ) ) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return is_page( $checkout_page_id ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Check if the current page has a checkout form block. |
| 177 | * |
| 178 | * @return bool |
| 179 | */ |
| 180 | protected function hasCheckoutFormBlock(): bool { |
| 181 | $post = get_post(); |
| 182 | |
| 183 | if ( ! $post ) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | return has_block( 'surecart/checkout-form', $post ) || has_block( 'surecart/form', $post ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Check if the current page is a buy page. |
| 192 | * |
| 193 | * Pretty buy permalinks (/buy/{product-slug}/) rewrite to the |
| 194 | * sc_checkout_product_id query var; sc-buy covers legacy buy links. |
| 195 | * |
| 196 | * @return bool |
| 197 | */ |
| 198 | protected function isBuyPage(): bool { |
| 199 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 200 | return isset( $_GET['sc-buy'] ) |
| 201 | || ! empty( get_query_var( 'sc-buy' ) ) |
| 202 | || ! empty( get_query_var( 'sc_checkout_product_id' ) ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get SureCart vary cookies. |
| 207 | * |
| 208 | * @return array |
| 209 | */ |
| 210 | protected function getVaryCookies(): array { |
| 211 | $cookies = [ |
| 212 | 'sc_checkout_id', |
| 213 | 'sc_customer_id', |
| 214 | 'sc_order_id', |
| 215 | ]; |
| 216 | |
| 217 | /** |
| 218 | * Filter the SureCart cookies used for cache variation. |
| 219 | * |
| 220 | * @param array $cookies Array of cookie names. |
| 221 | */ |
| 222 | return apply_filters( 'surecart/cache/vary_cookies', $cookies ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get core WordPress scripts that should be excluded from JS defer. |
| 227 | * |
| 228 | * @return array |
| 229 | */ |
| 230 | protected function getJsDeferExcludes(): array { |
| 231 | $scripts = [ |
| 232 | 'wp-api-fetch', |
| 233 | 'wp-a11y', |
| 234 | 'wp-i18n', |
| 235 | 'wp-url', |
| 236 | 'dom-ready', |
| 237 | 'wp-hooks', |
| 238 | 'api-fetch', |
| 239 | 'a11y.min.js', |
| 240 | 'i18n.min.js', |
| 241 | 'url.min.js', |
| 242 | 'dom-ready.min.js', |
| 243 | 'hooks.min.js', |
| 244 | ]; |
| 245 | |
| 246 | /** |
| 247 | * Filter the scripts excluded from JS defer. |
| 248 | * |
| 249 | * @param array $scripts Array of script patterns to exclude. |
| 250 | */ |
| 251 | return apply_filters( 'surecart/cache/js_defer_excludes', $scripts ); |
| 252 | } |
| 253 | } |
| 254 |