PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.0
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / WordPress / Cache / CacheService.php
CacheService.php
240 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return bool
146 */
147 protected function isCustomerDashboardPage(): bool {
148 return \SureCart::pages()->isCustomerDashboardPageByUrl();
149 }
150
151 /**
152 * Check if the current page is the checkout page.
153 *
154 * @return bool
155 */
156 protected function isCheckoutPage(): bool {
157 $checkout_page_id = \SureCart::pages()->getId( 'checkout' );
158
159 if ( empty( $checkout_page_id ) ) {
160 return false;
161 }
162
163 return is_page( $checkout_page_id );
164 }
165
166 /**
167 * Check if the current page has a checkout form block.
168 *
169 * @return bool
170 */
171 protected function hasCheckoutFormBlock(): bool {
172 $post = get_post();
173
174 if ( ! $post ) {
175 return false;
176 }
177
178 return has_block( 'surecart/checkout-form', $post ) || has_block( 'surecart/form', $post );
179 }
180
181 /**
182 * Check if the current page is a buy page.
183 *
184 * @return bool
185 */
186 protected function isBuyPage(): bool {
187 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
188 return isset( $_GET['sc-buy'] ) || ! empty( get_query_var( 'sc-buy' ) );
189 }
190
191 /**
192 * Get SureCart vary cookies.
193 *
194 * @return array
195 */
196 protected function getVaryCookies(): array {
197 $cookies = [
198 'sc_checkout_id',
199 'sc_customer_id',
200 'sc_order_id',
201 ];
202
203 /**
204 * Filter the SureCart cookies used for cache variation.
205 *
206 * @param array $cookies Array of cookie names.
207 */
208 return apply_filters( 'surecart/cache/vary_cookies', $cookies );
209 }
210
211 /**
212 * Get core WordPress scripts that should be excluded from JS defer.
213 *
214 * @return array
215 */
216 protected function getJsDeferExcludes(): array {
217 $scripts = [
218 'wp-api-fetch',
219 'wp-a11y',
220 'wp-i18n',
221 'wp-url',
222 'dom-ready',
223 'wp-hooks',
224 'api-fetch',
225 'a11y.min.js',
226 'i18n.min.js',
227 'url.min.js',
228 'dom-ready.min.js',
229 'hooks.min.js',
230 ];
231
232 /**
233 * Filter the scripts excluded from JS defer.
234 *
235 * @param array $scripts Array of script patterns to exclude.
236 */
237 return apply_filters( 'surecart/cache/js_defer_excludes', $scripts );
238 }
239 }
240