Skip to content

Commit b47301d

Browse files
committed
Use settings when settings the cache expiration time & period.
1 parent 0a7d142 commit b47301d

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/RestApi/RestDispatch.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use function Dwnload\WpRestApi\Helpers\filter_var_bool;
66
use function Dwnload\WpRestApi\Helpers\filter_var_int;
7+
use Dwnload\WpRestApi\WpAdmin\Admin;
8+
use Dwnload\WpRestApi\WpAdmin\Settings;
79
use Dwnload\WpRestApi\WpRestApiCache;
810
use TheFrosty\WpUtilities\Plugin\HooksTrait;
911
use TheFrosty\WpUtilities\Plugin\WpHooksInterface;
@@ -29,6 +31,7 @@ class RestDispatch implements WpHooksInterface
2931
const CACHE_HEADER_DELETE = 'X-WP-API-Cache-Delete';
3032
const FILTER_API_GROUP = WpRestApiCache::FILTER_PREFIX . 'group';
3133
const FILTER_API_KEY = WpRestApiCache::FILTER_PREFIX . 'key';
34+
const FILTER_KEYS_NOT_ALLOWED = WpRestApiCache::FILTER_PREFIX . 'keys_not_allowed';
3235
const FILTER_ALLOWED_CACHE_STATUS = WpRestApiCache::FILTER_PREFIX . 'allowed_cache_status';
3336
const FILTER_CACHE_CONTROL_HEADERS = WpRestApiCache::FILTER_PREFIX . 'cache_control_headers';
3437
const FILTER_CACHE_EXPIRE = WpRestApiCache::FILTER_PREFIX . 'expire';
@@ -39,7 +42,7 @@ class RestDispatch implements WpHooksInterface
3942
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
4043
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';
4144

42-
const VERSION = '2.0.4';
45+
const VERSION = '1.1.0';
4346

4447
/**
4548
* Add class hooks.
@@ -60,7 +63,7 @@ public function addHooks()
6063
*
6164
* @return mixed Response
6265
*/
63-
public function preDispatch($result, WP_REST_Server $server, WP_REST_Request $request)
66+
protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request $request)
6467
{
6568
$request_uri = $this->getRequestUri();
6669
$group = $this->getCacheGroup();
@@ -151,7 +154,7 @@ public function preDispatch($result, WP_REST_Server $server, WP_REST_Request $re
151154
*
152155
* @return WP_REST_Response
153156
*/
154-
public function postDispatch($response, WP_REST_Server $server, WP_REST_Request $request) : WP_REST_Response
157+
protected function postDispatch($response, WP_REST_Server $server, WP_REST_Request $request) : WP_REST_Response
155158
{
156159
$request_uri = $this->getRequestUri();
157160
$key = $this->getCacheKey($request_uri, $server, $request);
@@ -197,11 +200,28 @@ protected function getCachedResult(
197200
string $group,
198201
bool $force = false
199202
) {
200-
$result = \wp_cache_get($key, $group, $force);
203+
$result = \wp_cache_get($this->cleanKey($key), $group, $force);
201204
if ($result === false) {
202205
$result = $this->dispatchRequest($server, $request);
203-
$expire = \absint(\apply_filters(self::FILTER_CACHE_EXPIRE, (MINUTE_IN_SECONDS * 10)));
204-
\wp_cache_set($key, $result, $group, $expire);
206+
$defaults = [
207+
Settings::EXPIRATION => [
208+
Settings::LENGTH => 10,
209+
Settings::PERIOD => MINUTE_IN_SECONDS,
210+
],
211+
];
212+
$options = \get_option(Admin::OPTION_KEY, $defaults);
213+
/**
214+
* Filter for cache expiration time.
215+
* @param int Expiration time.
216+
* @param array Array of settings from the expiration length and period.
217+
* @return int
218+
*/
219+
$expire = \apply_filters(
220+
self::FILTER_CACHE_EXPIRE,
221+
($options[Settings::EXPIRATION][Settings::PERIOD] * $options[Settings::EXPIRATION][Settings::LENGTH]),
222+
$options[Settings::EXPIRATION]
223+
);
224+
\wp_cache_set($this->cleanKey($key), $result, $group, \absint($expire));
205225

206226
return $result;
207227
}

0 commit comments

Comments
 (0)