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
LiteSpeedCacheService.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Cache; |
| 4 | |
| 5 | /** |
| 6 | * LiteSpeed Cache Service. |
| 7 | */ |
| 8 | class LiteSpeedCacheService extends CacheService { |
| 9 | /** |
| 10 | * Bootstrap the service. |
| 11 | * |
| 12 | * @return void |
| 13 | */ |
| 14 | public function bootstrap() { |
| 15 | // Early return if LiteSpeed Cache plugin is not active. |
| 16 | if ( ! $this->isCachePluginActive() ) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Use LiteSpeed's finalize hook for cache control decisions. |
| 21 | add_action( 'litespeed_control_finalize', [ $this, 'onControlFinalize' ] ); |
| 22 | |
| 23 | // Add vary cookies for SureCart sessions. |
| 24 | add_filter( 'litespeed_vary_cookies', [ $this, 'addVaryCookies' ] ); |
| 25 | |
| 26 | // Exclude critical WordPress scripts from JS defer. |
| 27 | add_filter( 'litespeed_optm_js_defer_exc', [ $this, 'excludeScriptsFromDefer' ] ); |
| 28 | |
| 29 | // Disable cache for SureCart REST API requests. |
| 30 | add_action( 'rest_api_init', [ $this, 'maybeDisableCacheForRestApi' ], 1 ); |
| 31 | |
| 32 | // Purge cache when product stock is adjusted. |
| 33 | add_action( 'surecart/product_stock_adjusted', [ $this, 'purgeProductCacheOnStockAdjustment' ] ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Check if LiteSpeed Cache plugin is active. |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | protected function isCachePluginActive(): bool { |
| 42 | return has_action( 'litespeed_control_set_nocache' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Disable cache for the current page. |
| 47 | * |
| 48 | * @param string $reason Reason for disabling cache. |
| 49 | * @return void |
| 50 | */ |
| 51 | protected function disableCache( string $reason ): void { |
| 52 | do_action( 'litespeed_control_set_nocache', $reason ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Handle cache control during LiteSpeed's finalize phase. |
| 57 | * |
| 58 | * @param string|false $esi_id ESI block ID if this is an ESI request. |
| 59 | * @return void |
| 60 | */ |
| 61 | public function onControlFinalize( $esi_id = false ) { |
| 62 | // Only proceed if the page is currently marked as cacheable. |
| 63 | if ( ! apply_filters( 'litespeed_control_cacheable', false ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if ( $this->isCustomerDashboardPage() ) { |
| 68 | $this->disableCacheWithBrowserHeaders( 'SureCart customer dashboard' ); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if ( $this->isCheckoutPage() ) { |
| 73 | $this->disableCacheWithBrowserHeaders( 'SureCart checkout page' ); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | if ( $this->hasCheckoutFormBlock() ) { |
| 78 | $this->disableCacheWithBrowserHeaders( 'SureCart checkout form block' ); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if ( $this->isBuyPage() ) { |
| 83 | $this->disableCacheWithBrowserHeaders( 'SureCart buy page' ); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Add SureCart session cookies to LiteSpeed Cache vary cookies. |
| 90 | * |
| 91 | * @param array $cookies Existing vary cookies. |
| 92 | * @return array |
| 93 | */ |
| 94 | public function addVaryCookies( $cookies ) { |
| 95 | if ( ! is_array( $cookies ) ) { |
| 96 | $cookies = []; |
| 97 | } |
| 98 | |
| 99 | return array_merge( $cookies, $this->getVaryCookies() ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Exclude critical scripts from JS defer. |
| 104 | * |
| 105 | * @param array $excludes Existing excluded scripts. |
| 106 | * @return array |
| 107 | */ |
| 108 | public function excludeScriptsFromDefer( $excludes ) { |
| 109 | if ( ! is_array( $excludes ) ) { |
| 110 | $excludes = []; |
| 111 | } |
| 112 | |
| 113 | return array_merge( $excludes, $this->getJsDeferExcludes() ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Purge product cache when stock is adjusted. |
| 118 | * |
| 119 | * @param \SureCart\Models\Product $product The product model. |
| 120 | * @return void |
| 121 | */ |
| 122 | public function purgeProductCacheOnStockAdjustment( $product ) { |
| 123 | // Only purge if the action is available. |
| 124 | if ( ! has_action( 'litespeed_purge_post' ) ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if ( empty( $product ) ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // Get the WordPress post ID for the product. |
| 133 | $post_id = $product->metadata->wp_id ?? null; |
| 134 | |
| 135 | if ( ! empty( $post_id ) ) { |
| 136 | do_action( 'litespeed_purge_post', $post_id ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Action fired after purging cache for a product on stock adjustment. |
| 141 | * |
| 142 | * @param \SureCart\Models\Product $product The product model. |
| 143 | */ |
| 144 | do_action( 'surecart/cache/purged_product', $product ); |
| 145 | } |
| 146 | } |
| 147 |