Skip to content

Commit db8597b

Browse files
authored
Merge pull request #3 from dwnload/develop
Add new methods with filters to re-check requests containing `?contex…
2 parents a564df7 + 498a769 commit db8597b

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

CHANGELONG.md

Lines changed: 8 additions & 0 deletions
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.2.0 - 2018-04-25
8+
### Added
9+
- Added new method `RestDispatch::queryParamContextIsEdit`
10+
- Added new method `RestDispatch::isUserAuthenticated`.
11+
12+
### Updated
13+
- `RestDispatch::isUserAuthenticated` uses a new filter `RestDispatch::FILTER_CACHE_VALIDATE_AUTH` to re-check requests containing `?context=edit` to avoid race conditions where a non-auth request returns results from cache.
14+
715
## 1.1.1 - 2018-04-23
816
### Updated
917
- Version bump for packagist.

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ To install this package, edit your `composer.json` file:
1515
```js
1616
{
1717
"require": {
18-
"dwnload/wp-rest-api-object-cache": "^1.1.1"
18+
"dwnload/wp-rest-api-object-cache": "^1.2.0"
1919
}
2020
}
2121
```
@@ -71,6 +71,7 @@ Filters
7171
| Dwnload\WpRestApi\WpAdmin\Admin::FILTER_SHOW_ADMIN_MENU | boolean **$show** |
7272
| Dwnload\WpRestApi\WpAdmin\Admin::FILTER_SHOW_ADMIN_BAR_MENU | boolean **$show** |
7373
| Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_ALLOWED_CACHE_STATUS | array **$status** HTTP Header statuses (defaults to `array( 200 )` |
74+
| Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_CACHE_VALIDATE_AUTH | boolean **$authenticated**<br>WP_REST_Request $request |
7475

7576
How to use filters
7677
----
@@ -110,6 +111,21 @@ add_filter( Admin::FILTER_CACHE_OPTIONS, function( array $options ) : array {
110111
} );
111112
```
112113

114+
**Validating user auth when `?context=edit`**
115+
116+
```php
117+
use Dwnload\WpRestApi\RestApi\RestDispatch;
118+
add_filter( RestDispatch::FILTER_CACHE_VALIDATE_AUTH, function( bool $auth, WP_REST_Request $request ) : bool {
119+
// If you are running the Basic Auth plugin.
120+
if ( $GLOBALS['wp_json_basic_auth_error'] === true ) {
121+
$authorized = true;
122+
}
123+
// Otherwise, maybe do some additional logic on the request for current user...
124+
125+
return $authorized;
126+
}, 10, 2 );
127+
```
128+
113129
**Skipping cache**
114130

115131
```php
@@ -146,3 +162,13 @@ add_action( 'save_post', function( $post_id ) {
146162
}
147163
} );
148164
```
165+
166+
**Maybe better to use `transition_post_status`**
167+
168+
```php
169+
add_action( 'transition_post_status', function( string $new_status, string $old_status, \WP_Post $post ) {
170+
if ( 'publish' === $new_status || 'publish' === $old_status ) {
171+
\wp_cache_flush();
172+
}
173+
}, 99, 3 );
174+
```

composer.json

Lines changed: 1 addition & 1 deletion
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.1.1",
5+
"version": "1.2.0",
66
"license": "MIT",
77
"authors": [
88
{

src/RestApi/RestDispatch.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class RestDispatch implements WpHooksInterface
3333
const FILTER_API_KEY = WpRestApiCache::FILTER_PREFIX . 'key';
3434
const FILTER_KEYS_NOT_ALLOWED = WpRestApiCache::FILTER_PREFIX . 'keys_not_allowed';
3535
const FILTER_ALLOWED_CACHE_STATUS = WpRestApiCache::FILTER_PREFIX . 'allowed_cache_status';
36+
const FILTER_CACHE_VALIDATE_AUTH = WpRestApiCache::FILTER_PREFIX . 'validate_auth';
3637
const FILTER_CACHE_CONTROL_HEADERS = WpRestApiCache::FILTER_PREFIX . 'cache_control_headers';
3738
const FILTER_CACHE_EXPIRE = WpRestApiCache::FILTER_PREFIX . 'expire';
3839
const FILTER_CACHE_HEADERS = WpRestApiCache::FILTER_PREFIX . 'headers';
@@ -42,7 +43,7 @@ class RestDispatch implements WpHooksInterface
4243
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
4344
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';
4445

45-
const VERSION = '1.1.0';
46+
const VERSION = '1.2.0';
4647

4748
/**
4849
* Add class hooks.
@@ -226,6 +227,14 @@ protected function getCachedResult(
226227
return $result;
227228
}
228229

230+
/*
231+
* Attempt to validate the user if `?context=edit` to avoid returning results for non-auth'd requests after
232+
* a cached request from an authenticated request happens before cache flush.
233+
*/
234+
if ($this->queryParamContextIsEdit($request) && ! $this->isUserAuthenticated($request)) {
235+
return $this->dispatchRequest($server, $request);
236+
}
237+
229238
return $result;
230239
}
231240

@@ -301,4 +310,37 @@ private function validateQueryParam(WP_REST_Request $request, string $key) : boo
301310
return \array_key_exists($key, $request->get_query_params()) &&
302311
filter_var_int($request->get_query_params()[$key]) === 1;
303312
}
313+
314+
/**
315+
* Validate the HTTP query param.
316+
*
317+
* @param WP_REST_Request $request
318+
*
319+
* @return bool
320+
*/
321+
private function queryParamContextIsEdit(WP_REST_Request $request) : bool
322+
{
323+
return (
324+
array_key_exists('context', $request->get_query_params()) &&
325+
$request->get_query_params()['context'] === 'edit'
326+
);
327+
}
328+
329+
/**
330+
* Apply a filter to allow user auth checks based on the $request headers.
331+
* A great example here is to use the Basic Auth plugin and check for the global `$wp_json_basic_auth_error`
332+
* is equal to true to validate the current request is an authenticated user.
333+
*
334+
* @param WP_REST_Request $request
335+
*
336+
* @return bool
337+
*/
338+
private function isUserAuthenticated(WP_REST_Request $request) : bool
339+
{
340+
/**
341+
* @param bool $authenticated Defaults to false, user needs to be authenticated.
342+
* @param WP_REST_Request $request
343+
*/
344+
return \apply_filters(self::FILTER_CACHE_VALIDATE_AUTH, false, $request) !== false;
345+
}
304346
}

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.1.1
7+
* Version: 1.2.0
88
* Requires at least: 4.9
99
* Tested up to: 4.9
1010
* Requires PHP: 7.0

0 commit comments

Comments
 (0)