Skip to content

Commit 95f7418

Browse files
committed
Add additional bypass options for skipping cache on auth requests and if filter present.
1 parent 0871af6 commit 95f7418

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/RestApi/RestDispatch.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ class RestDispatch implements WpHooksInterface
3636
const FILTER_CACHE_EXPIRE = WpRestApiCache::FILTER_PREFIX . 'expire';
3737
const FILTER_CACHE_HEADERS = WpRestApiCache::FILTER_PREFIX . 'headers';
3838
const FILTER_CACHE_SKIP = WpRestApiCache::FILTER_PREFIX . 'skip';
39+
const FILTER_CACHE_BYPASS = WpRestApiCache::FILTER_PREFIX . 'bypass';
3940
const HEADER_CACHE_CONTROL = 'Cache-Control';
4041
const QUERY_CACHE_DELETE = 'rest_cache_delete';
4142
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
4243
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';
4344

44-
const VERSION = '1.2.2';
45+
const VERSION = '1.4.0';
4546

4647
/**
4748
* Has the current request been cached? Avoids the multi loop calls where
@@ -72,13 +73,22 @@ public function addHooks()
7273
*/
7374
protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request $request)
7475
{
76+
if ( $result !== null || \apply_filters(self::FILTER_CACHE_BYPASS, false) === true ) {
77+
return $result;
78+
}
7579
$request_uri = $this->getRequestUri();
7680
$group = $this->getCacheGroup();
7781
$key = $this->getCacheKey($request_uri, $server, $request);
7882

79-
// Return the result if it's a non-readable (GET) method or it's been cached.
83+
/*
84+
* Return the result if:
85+
* It's a non-readable (GET) method.
86+
* It's been cached already.
87+
* The request has an authorization header.
88+
*/
8089
if ($request->get_method() !== WP_REST_Server::READABLE ||
81-
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true)
90+
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true) ||
91+
! empty($request->get_header('authorization'))
8292
) {
8393
return $result;
8494
}
@@ -247,6 +257,7 @@ protected function getCachedResult(
247257
* a cached request from an authenticated request happens before cache flush.
248258
*/
249259
if ($this->queryParamContextIsEdit($request) && ! $this->isUserAuthenticated($request)) {
260+
\wp_cache_delete($this->cleanKey($key), $group);
250261
return $this->dispatchRequest($server, $request);
251262
}
252263

0 commit comments

Comments
 (0)