Skip to content

Commit c0aedf5

Browse files
authored
Merge pull request #8 from dwnload/develop
Version 1.3
2 parents 6454c5a + 7192335 commit c0aedf5

File tree

9 files changed

+100
-66
lines changed

9 files changed

+100
-66
lines changed

CHANGELONG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ 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.3.0 - 2018-07-27
8+
### Updated
9+
- Removed the `helper.php` file.
10+
- Updated all the functions that were using the helper functions.
11+
- Update [thefrosty/wp-utilities](https://github.com/thefrosty/wp-utilities) to 1.2.2.
12+
- Fix save settings on admin page, (POST array key was incorrect).
13+
- Add confirm to clear all cache button on settings page.
14+
- Only load the Admin class in the admin.
15+
16+
### Changed
17+
- Added a new capability (`manage_wp_rest_api_cache`) to view the settings page and/or admin bar which
18+
is (mapped to `delete_users`).
19+
- The `Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_CACHE_EXPIRE` filters expire sanitize function was changed from
20+
`absint` to `inval` function to allow for zero and negative numbers.
21+
- Pass `is_admin_bar_showing()` into FILTER_SHOW_ADMIN_BAR_MENU.
22+
23+
### Added
24+
- Added `wpCacheReplace()` to the `CacheApiTrait`.
25+
726
## 1.2.3 - 2018-05-30
827
### Updated
928
- Added permission check (`delete_users`) before adding admin bar node.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Enable object caching for WordPress' REST API. Aids in increased response times
1212

1313
To install this package, edit your `composer.json` file:
1414

15-
```js
15+
```json
1616
{
1717
"require": {
18-
"dwnload/wp-rest-api-object-cache": "^1.2.0"
18+
"dwnload/wp-rest-api-object-cache": "^1.3.0"
1919
}
2020
}
2121
```

composer.json

Lines changed: 3 additions & 4 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.2.3",
5+
"version": "1.3.0",
66
"license": "MIT",
77
"authors": [
88
{
@@ -14,7 +14,7 @@
1414
],
1515
"require": {
1616
"composer/installers": "~1.0",
17-
"thefrosty/wp-utilities": "^1.1.3",
17+
"thefrosty/wp-utilities": "^1.2.2",
1818
"php": ">=7.0.4"
1919
},
2020
"require-dev": {
@@ -30,8 +30,7 @@
3030
"autoload": {
3131
"psr-4": {
3232
"Dwnload\\WpRestApi\\": "src"
33-
},
34-
"files": ["helpers.php"]
33+
}
3534
},
3635
"autoload-dev": {
3736
"psr-4": {

helpers.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/RestApi/CacheApiTrait.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Dwnload\WpRestApi\RestApi;
44

5-
use function Dwnload\WpRestApi\Helpers\filter_var_string;
65
use WP_REST_Request;
76
use WP_REST_Server;
87

@@ -44,7 +43,7 @@ protected function getCacheKey(
4443
}
4544
}
4645

47-
return filter_var_string(\apply_filters(RestDispatch::FILTER_API_KEY, $request_uri, $server, $request));
46+
return $this->sanitize(\apply_filters(RestDispatch::FILTER_API_KEY, $request_uri, $server, $request));
4847
}
4948

5049
/**
@@ -54,7 +53,7 @@ protected function getCacheKey(
5453
*/
5554
protected function getCacheGroup() : string
5655
{
57-
return filter_var_string(\apply_filters(RestDispatch::FILTER_API_GROUP, RestDispatch::CACHE_GROUP));
56+
return $this->sanitize(\apply_filters(RestDispatch::FILTER_API_GROUP, RestDispatch::CACHE_GROUP));
5857
}
5958

6059
/**
@@ -68,11 +67,23 @@ protected function wpCacheFlush() : bool
6867
return \wp_cache_flush();
6968
}
7069

70+
/**
71+
* Empty all cache.
72+
*
73+
* @uses wp_cache_replace()
74+
* @param string $key The key under which the value is stored.
75+
* @return bool Returns TRUE on success or FALSE on failure.
76+
*/
77+
protected function wpCacheReplace(string $key) : bool
78+
{
79+
return \wp_cache_replace($this->cleanKey($key), false, $this->getCacheGroup(), -1);
80+
}
81+
7182
/**
7283
* Empty all cache.
7384
*
7485
* @uses wp_cache_delete()
75-
* @param string $key The key under which to store the value.
86+
* @param string $key The key under which the value is stored.
7687
* @return bool Returns TRUE on success or FALSE on failure.
7788
*/
7889
protected function wpCacheDeleteByKey(string $key) : bool
@@ -104,6 +115,16 @@ protected function cleanKey(string $key) : string
104115
*/
105116
protected function getRequestUri() : string
106117
{
107-
return filter_var_string(wp_unslash($_SERVER['REQUEST_URI']));
118+
return $this->sanitize(\wp_unslash($_SERVER['REQUEST_URI']));
119+
}
120+
121+
/**
122+
* Sanitize incoming variables as a string value.
123+
* @param mixed $variable
124+
* @return string|false
125+
*/
126+
private function sanitize($variable)
127+
{
128+
return \filter_var($variable, FILTER_SANITIZE_STRING);
108129
}
109130
}

src/RestApi/RestDispatch.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Dwnload\WpRestApi\RestApi;
44

5-
use function Dwnload\WpRestApi\Helpers\filter_var_bool;
6-
use function Dwnload\WpRestApi\Helpers\filter_var_int;
75
use Dwnload\WpRestApi\WpAdmin\Admin;
86
use Dwnload\WpRestApi\WpAdmin\Settings;
97
use Dwnload\WpRestApi\WpRestApiCache;
@@ -105,7 +103,7 @@ protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request
105103
}
106104

107105
// Cache is refreshed (cached below).
108-
$refresh = filter_var_bool($request->get_param(self::QUERY_CACHE_REFRESH));
106+
$refresh = \filter_var($request->get_param(self::QUERY_CACHE_REFRESH), FILTER_VALIDATE_BOOLEAN);
109107
if ($refresh) {
110108
$server->send_header(
111109
self::CACHE_HEADER,
@@ -128,8 +126,9 @@ protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request
128126
);
129127
}
130128

131-
$skip = filter_var_bool(
132-
\apply_filters(self::FILTER_CACHE_SKIP, WP_DEBUG, $request_uri, $server, $request)
129+
$skip = \filter_var(
130+
\apply_filters(self::FILTER_CACHE_SKIP, WP_DEBUG, $request_uri, $server, $request),
131+
FILTER_VALIDATE_BOOLEAN
133132
);
134133
if ($skip) {
135134
$server->send_header(
@@ -237,7 +236,7 @@ protected function getCachedResult(
237236
$this->cleanKey($key),
238237
$result,
239238
$group,
240-
\absint($expire)
239+
\intval($expire)
241240
);
242241

243242
return $result;
@@ -324,7 +323,7 @@ private function dispatchShutdownAction(string $key)
324323
private function validateQueryParam(WP_REST_Request $request, string $key) : bool
325324
{
326325
return \array_key_exists($key, $request->get_query_params()) &&
327-
filter_var_int($request->get_query_params()[$key]) === 1;
326+
\filter_var($request->get_query_params()[$key], FILTER_VALIDATE_INT) === 1;
328327
}
329328

330329
/**
@@ -336,10 +335,8 @@ private function validateQueryParam(WP_REST_Request $request, string $key) : boo
336335
*/
337336
private function queryParamContextIsEdit(WP_REST_Request $request) : bool
338337
{
339-
return (
340-
array_key_exists('context', $request->get_query_params()) &&
341-
$request->get_query_params()['context'] === 'edit'
342-
);
338+
return \array_key_exists('context', $request->get_query_params()) &&
339+
$request->get_query_params()['context'] === 'edit';
343340
}
344341

345342
/**

src/WpAdmin/Admin.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Dwnload\WpRestApi\WpAdmin;
44

5-
use function Dwnload\WpRestApi\Helpers\filter_var_int;
65
use Dwnload\WpRestApi\RestApi\CacheApiTrait;
76
use Dwnload\WpRestApi\RestApi\RestDispatch;
87
use Dwnload\WpRestApi\WpRestApiCache;
@@ -21,6 +20,7 @@ class Admin implements WpHooksInterface
2120

2221
const ACTION_REQUEST_FLUSH_CACHE = WpRestApiCache::FILTER_PREFIX . 'request_flush_cache';
2322
const ADMIN_ACTION = WpRestApiCache::FILTER_PREFIX . 'flush';
23+
const CAPABILITY = 'manage_wp_rest_api_cache';
2424
const FILTER_SHOW_ADMIN = WpRestApiCache::FILTER_PREFIX . 'show_admin';
2525
const FILTER_SHOW_ADMIN_BAR_MENU = WpRestApiCache::FILTER_PREFIX . 'show_admin_bar_menu';
2626
const FILTER_SHOW_ADMIN_MENU = WpRestApiCache::FILTER_PREFIX . 'show_admin_menu';
@@ -59,11 +59,33 @@ public function addHooks()
5959
$this->addAction('admin_action_' . self::ADMIN_ACTION, [$this, 'adminAction']);
6060
$this->addAction('admin_notices', [$this, 'adminNotices']);
6161
}
62-
6362
if ($this->showAdminMenuBar()) {
6463
$this->addAction('admin_bar_menu', [$this, 'adminBarMenu'], 999);
6564
}
65+
if ($this->showAdminMenu() || $this->showAdminMenuBar()) {
66+
$this->addFilter('map_meta_cap', [$this, 'mapMetaCap'], 10, 2);
67+
}
68+
}
69+
}
70+
71+
72+
/**
73+
* Map `self::CAPABILITY` capability.
74+
*
75+
* @param array $caps Returns the user's actual capabilities.
76+
* @param string $cap Capability name.
77+
* @return array
78+
*/
79+
protected function mapMetaCap(array $caps, string $cap) : array
80+
{
81+
// Map single-site cap check to 'manage_options'
82+
if ($cap === self::CAPABILITY) {
83+
if (! \is_multisite()) {
84+
$caps = ['delete_users'];
85+
}
6686
}
87+
88+
return $caps;
6789
}
6890

6991
/**
@@ -75,7 +97,7 @@ protected function adminMenu()
7597
'options-general.php',
7698
\esc_html__('WP REST API Cache', 'wp-rest-api-cache'),
7799
\esc_html__('REST API Cache', 'wp-rest-api-cache'),
78-
'delete_users',
100+
self::CAPABILITY,
79101
self::MENU_SLUG,
80102
function () {
81103
$this->renderPage();
@@ -90,7 +112,7 @@ function () {
90112
*/
91113
protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
92114
{
93-
if (! is_user_logged_in() || ! current_user_can('delete_users') || ! is_admin_bar_showing()) {
115+
if (! \is_user_logged_in() || ! \current_user_can(self::CAPABILITY) || ! \is_admin_bar_showing()) {
94116
return;
95117
}
96118

@@ -103,6 +125,9 @@ protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar)
103125
'id' => self::MENU_ID,
104126
'title' => \esc_html__('Empty all cache', 'wp-rest-api-cache'),
105127
'href' => \esc_url($this->getEmptyCacheUrl()),
128+
'meta' => [
129+
'onclick' => 'return confirm("This will clear ALL cache, continue?")'
130+
]
106131
]);
107132
}
108133

@@ -130,7 +155,7 @@ protected function adminAction()
130155
protected function adminNotices()
131156
{
132157
if (! empty($_GET[self::NOTICE]) &&
133-
filter_var_int($_GET[self::NOTICE]) === 1
158+
\filter_var($_GET[self::NOTICE], FILTER_VALIDATE_INT) === 1
134159
) {
135160
$message = \esc_html__('The cache has been successfully cleared.', 'wp-rest-api-cache');
136161
echo "<div class='notice updated is-dismissible'><p>{$message}</p></div>"; // PHPCS: XSS OK.
@@ -178,7 +203,7 @@ private function requestCallback()
178203
\wp_verify_nonce($_REQUEST[self::NONCE_NAME], 'rest_cache_options') !== false
179204
) {
180205
if (! empty($_GET['rest_cache_empty']) &&
181-
filter_var_int($_GET['rest_cache_empty']) === 1
206+
\filter_var($_GET['rest_cache_empty'], FILTER_VALIDATE_INT) === 1
182207
) {
183208
if ($this->wpCacheFlush()) {
184209
$type = 'updated';
@@ -196,7 +221,7 @@ private function requestCallback()
196221
*/
197222
\do_action(self::ACTION_REQUEST_FLUSH_CACHE, $message, $type, \wp_get_current_user());
198223
} elseif (! empty($_POST[self::OPTION_KEY])) {
199-
if ($this->updateOptions($_POST['rest_cache_options'])) {
224+
if ($this->updateOptions($_POST[self::OPTION_KEY])) {
200225
$type = 'updated';
201226
$message = \esc_html__('The cache time has been updated', 'wp-rest-api-cache');
202227
} else {
@@ -280,6 +305,6 @@ private function showAdminMenu() : bool
280305
*/
281306
private function showAdminMenuBar() : bool
282307
{
283-
return \apply_filters(self::FILTER_SHOW_ADMIN_BAR_MENU, true) === true;
308+
return \apply_filters(self::FILTER_SHOW_ADMIN_BAR_MENU, \is_admin_bar_showing()) === true;
284309
}
285310
}

views/settings.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
<tr>
2727
<th scope="row"><?php esc_html_e( 'Empty all cache', 'wp-rest-api-cache' ); ?></th>
2828
<td><a href="<?php echo esc_url( $cache_url->invoke( $this ) ); ?>"
29+
onclick="return confirm('This will clear ALL cache, continue?')"
2930
class="button button-primary"><?php esc_html_e( 'empty cache', 'wp-rest-api-cache' ); ?></a></td>
3031
</tr>
3132
<tr>
3233
<th scope="row"><?php esc_html_e( 'Cache time', 'wp-rest-api-cache' ); ?></th>
3334
<td>
34-
<input type="number" id="fld-cache-time" min="1" style="width: 70px;"
35+
<input type="number" min="1" style="width: 70px;"
3536
name="<?php printf( '%s[%s][%s]', Admin::OPTION_KEY, Settings::EXPIRATION, Settings::LENGTH ); ?>"
3637
value="<?php echo absint( $options[ Settings::EXPIRATION ][ Settings::LENGTH ] ); ?>">
3738
<?php $period = absint( $options[ Settings::EXPIRATION ][ Settings::PERIOD ] ); ?>

wp-rest-api-cache.php

Lines changed: 7 additions & 5 deletions
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.2.3
7+
* Version: 1.3.0
88
* Requires at least: 4.9
99
* Tested up to: 4.9
1010
* Requires PHP: 7.0
@@ -17,10 +17,12 @@
1717
use Dwnload\WpRestApi\WpAdmin\Admin;
1818
use TheFrosty\WpUtilities\Plugin\PluginFactory;
1919

20-
PluginFactory::create('rest-api-object-cache')
21-
->addOnHook(RestDispatch::class)
22-
->addOnHook(Admin::class)
23-
->initialize();
20+
$plugin = PluginFactory::create('rest-api-object-cache');
21+
$plugin->addOnHook(RestDispatch::class, 'rest_api_init')->initialize();
22+
23+
if (is_admin()) {
24+
$plugin->add(new Admin())->initialize();
25+
}
2426

2527
call_user_func_array(
2628
function ($filter) {

0 commit comments

Comments
 (0)