Skip to content

Commit 2635dac

Browse files
committed
Remove bypass filter and add setting to bypass (disable).
1 parent 621eab9 commit 2635dac

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

CHANGELONG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## 1.4 2019-04-17
88
### Added
9-
- Bypass cache filter, use `RestDispatch::FILTER_CACHE_BYPASS`
9+
- Bypass cache setting added.
1010

1111
## 1.3.2 2019-02-14
1212
### Added

src/RestApi/RestDispatch.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ 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';
4039
const HEADER_CACHE_CONTROL = 'Cache-Control';
4140
const QUERY_CACHE_DELETE = 'rest_cache_delete';
4241
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
@@ -57,8 +56,11 @@ class RestDispatch implements WpHooksInterface
5756
*/
5857
public function addHooks()
5958
{
60-
$this->addFilter('rest_pre_dispatch', [$this, 'preDispatch'], 10, 3);
61-
$this->addFilter('rest_post_dispatch', [$this, 'postDispatch'], 10, 3);
59+
$options = $this->getOptions([]);
60+
if (!isset($options[Settings::BYPASS]) || $options[Settings::BYPASS] !== 'on') {
61+
$this->addFilter('rest_pre_dispatch', [$this, 'preDispatch'], 10, 3);
62+
$this->addFilter('rest_post_dispatch', [$this, 'postDispatch'], 10, 3);
63+
}
6264
}
6365

6466
/**
@@ -73,7 +75,7 @@ public function addHooks()
7375
*/
7476
protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request $request)
7577
{
76-
if ($result !== null || \apply_filters(self::FILTER_CACHE_BYPASS, false) === true) {
78+
if ($result !== null) {
7779
return $result;
7880
}
7981
$request_uri = $this->getRequestUri();
@@ -230,7 +232,7 @@ protected function getCachedResult(
230232
Settings::PERIOD => MINUTE_IN_SECONDS,
231233
],
232234
];
233-
$options = \get_option(Admin::OPTION_KEY, $defaults);
235+
$options = $this->getOptions($defaults);
234236
/**
235237
* Filter for cache expiration time.
236238
* @param int Expiration time.
@@ -367,4 +369,14 @@ private function isUserAuthenticated(WP_REST_Request $request) : bool
367369
*/
368370
return \apply_filters(self::FILTER_CACHE_VALIDATE_AUTH, false, $request) !== false;
369371
}
372+
373+
/**
374+
* Get the options.
375+
* @param mixed $defaults
376+
* @return mixed
377+
*/
378+
private function getOptions($defaults)
379+
{
380+
return \get_option(Admin::OPTION_KEY, $defaults);
381+
}
370382
}

src/WpAdmin/Admin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ protected function getOptions($key = null)
188188
{
189189
$options = \apply_filters(
190190
self::FILTER_CACHE_OPTIONS,
191-
\get_option(self::OPTION_KEY, $this->settings->getExpiration())
191+
\get_option(self::OPTION_KEY, $this->settings->getSettings())
192192
);
193193

194194
if (\is_string($key) && \array_key_exists($key, $options)) {
@@ -250,8 +250,9 @@ private function updateOptions(array $options) : bool
250250
{
251251
$this->settings->setLength(absint($options[Settings::EXPIRATION][Settings::LENGTH]));
252252
$this->settings->setPeriod(absint($options[Settings::EXPIRATION][Settings::PERIOD]));
253+
$this->settings->setBypass(!empty($options[Settings::BYPASS]) ? 'on' : 'off');
253254

254-
return \update_option(self::OPTION_KEY, $this->settings->getExpiration(), 'yes');
255+
return \update_option(self::OPTION_KEY, $this->settings->getSettings(), 'yes');
255256
}
256257

257258
/**

src/WpAdmin/Settings.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@
1111
class Settings extends BaseModel
1212
{
1313

14+
const BYPASS = 'bypass';
1415
const EXPIRATION = 'expiration';
1516
const LENGTH = 'length';
1617
const PERIOD = 'period';
1718

1819
/**
19-
* Settings expiration array.
20+
* Settings array.
2021
*
21-
* @var array $expiration
22+
* @var array $settings
2223
*/
23-
protected $expiration = [];
24+
protected $settings = [];
2425

2526
/**
2627
* Get's the expiration settings array.
2728
* @return array
2829
*/
29-
public function getExpiration() : array
30+
public function getSettings() : array
3031
{
31-
return $this->expiration;
32+
return $this->settings;
3233
}
3334

3435
/**
@@ -37,7 +38,7 @@ public function getExpiration() : array
3738
*/
3839
public function setLength(int $length)
3940
{
40-
$this->expiration[self::EXPIRATION][self::LENGTH] = $length;
41+
$this->settings[self::EXPIRATION][self::LENGTH] = $length;
4142
}
4243

4344
/**
@@ -46,6 +47,15 @@ public function setLength(int $length)
4647
*/
4748
public function setPeriod(int $period)
4849
{
49-
$this->expiration[self::EXPIRATION][self::PERIOD] = $period;
50+
$this->settings[self::EXPIRATION][self::PERIOD] = $period;
51+
}
52+
53+
/**
54+
* Sets the bypass value.
55+
* @param string $value
56+
*/
57+
public function setBypass(string $value)
58+
{
59+
$this->settings[self::BYPASS] = $value;
5060
}
5161
}

views/settings.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ class="button button-primary"><?php esc_html_e( 'empty cache', 'wp-rest-api-cach
5555
</select>
5656
</td>
5757
</tr>
58+
<tr>
59+
<th scope="row"><?php esc_html_e( 'Disable REST Cache', 'wp-rest-api-cache' ); ?></th>
60+
<td>
61+
<input type="checkbox"
62+
name="<?php printf( '%s[%s]', Admin::OPTION_KEY, Settings::BYPASS ); ?>"
63+
value="on"<?php checked($options[Settings::BYPASS], 'on'); ?>>
64+
65+
</td>
66+
</tr>
5867
<tr>
5968
<th scope="row">&nbsp;</th>
6069
<td><input type="submit" class="button button-primary"

0 commit comments

Comments
 (0)