Skip to content

Commit 9e5ee77

Browse files
authored
Merge pull request #2 from dwnload/develop
Version 1.1.0
2 parents 41caccd + 6f42206 commit 9e5ee77

File tree

7 files changed

+108
-34
lines changed

7 files changed

+108
-34
lines changed

CHANGELONG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ 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.1.0 - 2018-04-23
8+
### Updated
9+
- The admin settings page now works.
10+
- wp_cache_set expire from settings is used (if available).
11+
- Be sure to clean the URL queries after calls to avoid caching delete requests.
12+
- Make sure the cache flush button in the settings is invoked to show the URL.
13+
714
## 1.0.4 - 2018-04-19
815
### Updated
916
- `RestDispatch::preDispatch` should set the $request_uri from `CacheApiTrait::getRequestUri` and not use

src/RestApi/CacheApiTrait.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ protected function getCacheKey(
4444
}
4545
}
4646

47-
// Be sure to remove our added cache refresh & cache delete queries.
48-
$uri = \remove_query_arg([RestDispatch::QUERY_CACHE_DELETE, RestDispatch::QUERY_CACHE_REFRESH], $request_uri);
49-
return filter_var_string(\apply_filters(RestDispatch::FILTER_API_KEY, $uri, $server, $request));
47+
return filter_var_string(\apply_filters(RestDispatch::FILTER_API_KEY, $request_uri, $server, $request));
5048
}
5149

5250
/**
@@ -79,7 +77,23 @@ protected function wpCacheFlush() : bool
7977
*/
8078
protected function wpCacheDeleteByKey(string $key) : bool
8179
{
82-
return \wp_cache_delete($key, $this->getCacheGroup());
80+
return \wp_cache_delete($this->cleanKey($key), $this->getCacheGroup());
81+
}
82+
83+
/**
84+
* Clean the cache of query params that shouldn't be part of the string; like delete params.
85+
*
86+
* @param string $key
87+
* @return string
88+
*/
89+
protected function cleanKey(string $key) : string
90+
{
91+
$query_args = [
92+
RestDispatch::QUERY_CACHE_DELETE,
93+
RestDispatch::QUERY_CACHE_FORCE_DELETE,
94+
RestDispatch::QUERY_CACHE_REFRESH,
95+
];
96+
return \remove_query_arg($query_args, $key);
8397
}
8498

8599
/**

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
}

src/WpAdmin/Admin.php

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Admin implements WpHooksInterface
4242
public function __construct()
4343
{
4444
$this->settings = new Settings([
45-
Settings::LENGTH => 1,
46-
Settings::PERIOD => WEEK_IN_SECONDS,
45+
Settings::LENGTH => 10,
46+
Settings::PERIOD => MINUTE_IN_SECONDS,
4747
]);
4848
}
4949

@@ -52,15 +52,15 @@ public function __construct()
5252
*/
5353
public function addHooks()
5454
{
55-
if (\apply_filters(self::FILTER_SHOW_ADMIN, true)) {
56-
if (\apply_filters(self::FILTER_SHOW_ADMIN_MENU, true)) {
55+
if ($this->showAdmin()) {
56+
if ($this->showAdminMenu()) {
5757
$this->addAction('admin_menu', [$this, 'adminMenu']);
5858
} else {
5959
$this->addAction('admin_action_' . self::ADMIN_ACTION, [$this, 'adminAction']);
6060
$this->addAction('admin_notices', [$this, 'adminNotices']);
6161
}
6262

63-
if (\apply_filters(self::FILTER_SHOW_ADMIN_BAR_MENU, true)) {
63+
if ($this->showAdminMenuBar()) {
6464
$this->addAction('admin_bar_menu', [$this, 'adminBarMenu'], 999);
6565
}
6666
}
@@ -69,7 +69,7 @@ public function addHooks()
6969
/**
7070
* Hook into the WordPress admin menu.
7171
*/
72-
public function adminMenu()
72+
protected function adminMenu()
7373
{
7474
\add_submenu_page(
7575
'options-general.php',
@@ -86,7 +86,7 @@ public function adminMenu()
8686
*
8787
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar object.
8888
*/
89-
public function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
89+
protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
9090
{
9191
$args = [
9292
'id' => WpRestApiCache::ID,
@@ -105,7 +105,7 @@ public function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
105105
/**
106106
* Helper to check the request action.
107107
*/
108-
public function adminAction()
108+
protected function adminAction()
109109
{
110110
$this->requestCallback();
111111

@@ -127,7 +127,7 @@ public function adminAction()
127127
/**
128128
* Maybe add an admin notice.
129129
*/
130-
public function adminNotices()
130+
protected function adminNotices()
131131
{
132132
if (! empty($_REQUEST[self::NONCE_NAME]) &&
133133
\wp_verify_nonce($_REQUEST[self::NONCE_NAME], self::NONCE_ACTION) &&
@@ -142,10 +142,10 @@ public function adminNotices()
142142
/**
143143
* Render the admin settings page.
144144
*/
145-
public function renderPage()
145+
protected function renderPage()
146146
{
147147
$this->requestCallback();
148-
require_once \dirname(__FILE__) . '../../views/settings.php';
148+
require_once \dirname(__FILE__) . '/../../views/settings.php';
149149
}
150150

151151
/**
@@ -154,7 +154,7 @@ public function renderPage()
154154
* @param null|string $key Option key value.
155155
* @return mixed
156156
*/
157-
public function getOptions($key = null)
157+
protected function getOptions($key = null)
158158
{
159159
$options = \apply_filters(
160160
self::FILTER_CACHE_OPTIONS,
@@ -177,7 +177,7 @@ private function requestCallback()
177177
$message = \esc_html__('Nothing to see here.', 'wp-rest-api-cache');
178178

179179
if (! empty($_REQUEST[self::NONCE_NAME]) &&
180-
\wp_verify_nonce($_REQUEST[self::NONCE_NAME], 'rest_cache_options')
180+
\wp_verify_nonce($_REQUEST[self::NONCE_NAME], 'rest_cache_options') !== false
181181
) {
182182
if (! empty($_GET['rest_cache_empty']) &&
183183
filter_var_int($_GET['rest_cache_empty']) === 1
@@ -197,7 +197,7 @@ private function requestCallback()
197197
* @param \WP_User The current user.
198198
*/
199199
\do_action(self::ACTION_REQUEST_FLUSH_CACHE, $message, $type, \wp_get_current_user());
200-
} elseif (isset($_POST['rest_cache_options']) && ! empty($_POST['rest_cache_options'])) {
200+
} elseif (! empty($_POST[self::OPTION_KEY])) {
201201
if ($this->updateOptions($_POST['rest_cache_options'])) {
202202
$type = 'updated';
203203
$message = \esc_html__('The cache time has been updated', 'wp-rest-api-cache');
@@ -218,12 +218,10 @@ private function requestCallback()
218218
*/
219219
private function updateOptions(array $options) : bool
220220
{
221-
$options = \array_map(
222-
'sanitize_text_field',
223-
\apply_filters(self::FILTER_CACHE_UPDATE_OPTIONS, $options)
224-
);
221+
$this->settings->setLength(absint($options[Settings::EXPIRATION][Settings::LENGTH]));
222+
$this->settings->setPeriod(absint($options[Settings::EXPIRATION][Settings::PERIOD]));
225223

226-
return \update_option(self::OPTION_KEY, $options, 'yes');
224+
return \update_option(self::OPTION_KEY, $this->settings->getExpiration(), 'yes');
227225
}
228226

229227
/**
@@ -233,7 +231,7 @@ private function updateOptions(array $options) : bool
233231
*/
234232
private function getEmptyCacheUrl() : string
235233
{
236-
if (\apply_filters(self::FILTER_SHOW_ADMIN_MENU, true)) {
234+
if ($this->showAdminMenu()) {
237235
return \wp_nonce_url(
238236
\add_query_arg(
239237
[
@@ -259,4 +257,31 @@ private function getEmptyCacheUrl() : string
259257
self::NONCE_NAME
260258
);
261259
}
260+
261+
/**
262+
* Should the admin functions be shown?
263+
* @return bool
264+
*/
265+
private function showAdmin() : bool
266+
{
267+
return \apply_filters(self::FILTER_SHOW_ADMIN, true) === true;
268+
}
269+
270+
/**
271+
* Show the admin menu be shown?
272+
* @return bool
273+
*/
274+
private function showAdminMenu() : bool
275+
{
276+
return \apply_filters(self::FILTER_SHOW_ADMIN_MENU, true) === true;
277+
}
278+
279+
/**
280+
* Show the admin menu bar be shown?
281+
* @return bool
282+
*/
283+
private function showAdminMenuBar() : bool
284+
{
285+
return \apply_filters(self::FILTER_SHOW_ADMIN_BAR_MENU, true) === true;
286+
}
262287
}

src/WpAdmin/Settings.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ class Settings extends BaseModel
2020
*
2121
* @var array $expiration
2222
*/
23-
protected $expiration;
23+
protected $expiration = [];
2424

25+
/**
26+
* Get's the expiration settings array.
27+
* @return array
28+
*/
2529
public function getExpiration() : array
2630
{
2731
return $this->expiration;
2832
}
2933

3034
/**
35+
* Sets the expiration length.
3136
* @param int $length
3237
*/
3338
public function setLength(int $length)
@@ -36,6 +41,7 @@ public function setLength(int $length)
3641
}
3742

3843
/**
44+
* Sets the expiration period.
3945
* @param int $period
4046
*/
4147
public function setPeriod(int $period)

views/settings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
<div class="wrap">
2222
<h1><?php esc_html_e( 'WP REST API Cache', 'wp-rest-api-cache' ); ?></h1>
2323
<form action="" method="POST">
24-
<?php wp_nonce_field( Admin::OPTION_KEY, Admin::NONCE_NAME ); ?>
24+
<?php wp_nonce_field( 'rest_cache_options', Admin::NONCE_NAME ); ?>
2525
<table class="form-table">
2626
<tr>
2727
<th scope="row"><?php esc_html_e( 'Empty all cache', 'wp-rest-api-cache' ); ?></th>
28-
<td><a href="<?php echo esc_url( $cache_url ); ?>"
28+
<td><a href="<?php echo esc_url( $cache_url->invoke( $this ) ); ?>"
2929
class="button button-primary"><?php esc_html_e( 'empty cache', 'wp-rest-api-cache' ); ?></a></td>
3030
</tr>
3131
<tr>

wp-rest-api-cache.php

Lines changed: 3 additions & 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.0.2
7+
* Version: 1.1.0
88
* Requires at least: 4.9
99
* Tested up to: 4.9
1010
* Requires PHP: 7.0
@@ -14,10 +14,12 @@
1414
defined('ABSPATH') || exit;
1515

1616
use Dwnload\WpRestApi\RestApi\RestDispatch;
17+
use Dwnload\WpRestApi\WpAdmin\Admin;
1718
use TheFrosty\WpUtilities\Plugin\PluginFactory;
1819

1920
PluginFactory::create('rest-api-object-cache')
2021
->addOnHook(RestDispatch::class)
22+
->addOnHook(Admin::class)
2123
->initialize();
2224

2325
call_user_func_array(

0 commit comments

Comments
 (0)