Skip to content

Commit 608098d

Browse files
authored
Merge pull request #11 from dwnload/develop
Version 1.4.0
2 parents afe637f + 2635dac commit 608098d

File tree

7 files changed

+78
-21
lines changed

7 files changed

+78
-21
lines changed

CHANGELONG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 1.4 2019-04-17
8+
### Added
9+
- Bypass cache setting added.
10+
11+
## 1.3.2 2019-02-14
12+
### Added
13+
- Bypass cache when authorization headers present.
14+
715
## 1.3.1 2019-02-06
816
### Updated
917
- Added additional check for object on `site_transient_update_plugins` check.
@@ -105,4 +113,4 @@ which fixes `addOnHook` not executing when omitting a priority parameter less th
105113
### Added
106114
- Forked [thefrosty/wp-rest-api-cache](https://github.com/thefrosty/wp-rest-api-cache/) which is a fork of
107115
[airesvsg/wp-rest-api-cache](https://github.com/airesvsg/wp-rest-api-cache/).
108-
- This CHANGELOG file.
116+
- This CHANGELOG file.

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "dwnload/wp-rest-api-object-cache",
33
"description": "Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.",
44
"type": "wordpress-plugin",
5-
"version": "1.3.1",
5+
"version": "1.4.0",
66
"license": "MIT",
77
"authors": [
88
{
@@ -18,7 +18,6 @@
1818
"php": ">=7.0.4"
1919
},
2020
"require-dev": {
21-
"10up/wp_mock": "dev-dev",
2221
"phpunit/phpunit": "6.*",
2322
"squizlabs/php_codesniffer": "^3.2"
2423
},

src/RestApi/RestDispatch.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class RestDispatch implements WpHooksInterface
4141
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
4242
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';
4343

44-
const VERSION = '1.2.2';
44+
const VERSION = '1.4.0';
4545

4646
/**
4747
* Has the current request been cached? Avoids the multi loop calls where
@@ -56,8 +56,11 @@ class RestDispatch implements WpHooksInterface
5656
*/
5757
public function addHooks()
5858
{
59-
$this->addFilter('rest_pre_dispatch', [$this, 'preDispatch'], 10, 3);
60-
$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+
}
6164
}
6265

6366
/**
@@ -72,13 +75,22 @@ public function addHooks()
7275
*/
7376
protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request $request)
7477
{
78+
if ($result !== null) {
79+
return $result;
80+
}
7581
$request_uri = $this->getRequestUri();
7682
$group = $this->getCacheGroup();
7783
$key = $this->getCacheKey($request_uri, $server, $request);
7884

79-
// Return the result if it's a non-readable (GET) method or it's been cached.
85+
/*
86+
* Return the result if:
87+
* It's a non-readable (GET) method.
88+
* It's been cached already.
89+
* The request has an authorization header.
90+
*/
8091
if ($request->get_method() !== WP_REST_Server::READABLE ||
81-
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true)
92+
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true) ||
93+
! empty($request->get_header('authorization'))
8294
) {
8395
return $result;
8496
}
@@ -220,7 +232,7 @@ protected function getCachedResult(
220232
Settings::PERIOD => MINUTE_IN_SECONDS,
221233
],
222234
];
223-
$options = \get_option(Admin::OPTION_KEY, $defaults);
235+
$options = $this->getOptions($defaults);
224236
/**
225237
* Filter for cache expiration time.
226238
* @param int Expiration time.
@@ -247,6 +259,7 @@ protected function getCachedResult(
247259
* a cached request from an authenticated request happens before cache flush.
248260
*/
249261
if ($this->queryParamContextIsEdit($request) && ! $this->isUserAuthenticated($request)) {
262+
\wp_cache_delete($this->cleanKey($key), $group);
250263
return $this->dispatchRequest($server, $request);
251264
}
252265

@@ -356,4 +369,14 @@ private function isUserAuthenticated(WP_REST_Request $request) : bool
356369
*/
357370
return \apply_filters(self::FILTER_CACHE_VALIDATE_AUTH, false, $request) !== false;
358371
}
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+
}
359382
}

src/WpAdmin/Admin.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,22 @@ protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
118118

119119
$wp_admin_bar->add_node([
120120
'id' => WpRestApiCache::ID,
121-
'title' => \esc_html__('REST API Cache', 'wp-rest-api-cache'),
121+
'title' => \sprintf(
122+
'<span class="ab-icon dashicons dashicons-shield" title="%s"></span><span class="ab-label">%s</span>',
123+
\esc_attr__('REST API Cache', 'wp-rest-api-cache'),
124+
\esc_html__('REST Cache', 'wp-rest-api-cache')
125+
)
122126
]);
123127
$wp_admin_bar->add_menu([
124128
'parent' => WpRestApiCache::ID,
125129
'id' => self::MENU_ID,
126130
'title' => \esc_html__('Empty all cache', 'wp-rest-api-cache'),
127131
'href' => \esc_url($this->getEmptyCacheUrl()),
128132
'meta' => [
129-
'onclick' => 'return confirm("This will clear ALL cache, continue?")'
133+
'onclick' => \sprintf(
134+
'return confirm("%s")',
135+
\esc_attr__('This will clear ALL cache, continue?', 'wp-rest-api-cache')
136+
)
130137
]
131138
]);
132139
}
@@ -181,7 +188,7 @@ protected function getOptions($key = null)
181188
{
182189
$options = \apply_filters(
183190
self::FILTER_CACHE_OPTIONS,
184-
\get_option(self::OPTION_KEY, $this->settings->getExpiration())
191+
\get_option(self::OPTION_KEY, $this->settings->getSettings())
185192
);
186193

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

247-
return \update_option(self::OPTION_KEY, $this->settings->getExpiration(), 'yes');
255+
return \update_option(self::OPTION_KEY, $this->settings->getSettings(), 'yes');
248256
}
249257

250258
/**

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"

wp-rest-api-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Description: Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.
55
* Author: Austin Passy
66
* Author URI: http://github.com/thefrosty
7-
* Version: 1.3.1
7+
* Version: 1.4.0
88
* Requires at least: 4.9
99
* Tested up to: 4.9
1010
* Requires PHP: 7.0

0 commit comments

Comments
 (0)