Skip to content

Commit

Permalink
Merge pull request #11 from dwnload/develop
Browse files Browse the repository at this point in the history
Version 1.4.0
  • Loading branch information
thefrosty committed Apr 17, 2019
2 parents afe637f + 2635dac commit 608098d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 21 deletions.
10 changes: 9 additions & 1 deletion CHANGELONG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.4 2019-04-17
### Added
- Bypass cache setting added.

## 1.3.2 2019-02-14
### Added
- Bypass cache when authorization headers present.

## 1.3.1 2019-02-06
### Updated
- Added additional check for object on `site_transient_update_plugins` check.
Expand Down Expand Up @@ -105,4 +113,4 @@ which fixes `addOnHook` not executing when omitting a priority parameter less th
### Added
- Forked [thefrosty/wp-rest-api-cache](https://github.com/thefrosty/wp-rest-api-cache/) which is a fork of
[airesvsg/wp-rest-api-cache](https://github.com/airesvsg/wp-rest-api-cache/).
- This CHANGELOG file.
- This CHANGELOG file.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dwnload/wp-rest-api-object-cache",
"description": "Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.",
"type": "wordpress-plugin",
"version": "1.3.1",
"version": "1.4.0",
"license": "MIT",
"authors": [
{
Expand All @@ -18,7 +18,6 @@
"php": ">=7.0.4"
},
"require-dev": {
"10up/wp_mock": "dev-dev",
"phpunit/phpunit": "6.*",
"squizlabs/php_codesniffer": "^3.2"
},
Expand Down
35 changes: 29 additions & 6 deletions src/RestApi/RestDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RestDispatch implements WpHooksInterface
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';

const VERSION = '1.2.2';
const VERSION = '1.4.0';

/**
* Has the current request been cached? Avoids the multi loop calls where
Expand All @@ -56,8 +56,11 @@ class RestDispatch implements WpHooksInterface
*/
public function addHooks()
{
$this->addFilter('rest_pre_dispatch', [$this, 'preDispatch'], 10, 3);
$this->addFilter('rest_post_dispatch', [$this, 'postDispatch'], 10, 3);
$options = $this->getOptions([]);
if (!isset($options[Settings::BYPASS]) || $options[Settings::BYPASS] !== 'on') {
$this->addFilter('rest_pre_dispatch', [$this, 'preDispatch'], 10, 3);
$this->addFilter('rest_post_dispatch', [$this, 'postDispatch'], 10, 3);
}
}

/**
Expand All @@ -72,13 +75,22 @@ public function addHooks()
*/
protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request $request)
{
if ($result !== null) {
return $result;
}
$request_uri = $this->getRequestUri();
$group = $this->getCacheGroup();
$key = $this->getCacheKey($request_uri, $server, $request);

// Return the result if it's a non-readable (GET) method or it's been cached.
/*
* Return the result if:
* It's a non-readable (GET) method.
* It's been cached already.
* The request has an authorization header.
*/
if ($request->get_method() !== WP_REST_Server::READABLE ||
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true)
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true) ||
! empty($request->get_header('authorization'))
) {
return $result;
}
Expand Down Expand Up @@ -220,7 +232,7 @@ protected function getCachedResult(
Settings::PERIOD => MINUTE_IN_SECONDS,
],
];
$options = \get_option(Admin::OPTION_KEY, $defaults);
$options = $this->getOptions($defaults);
/**
* Filter for cache expiration time.
* @param int Expiration time.
Expand All @@ -247,6 +259,7 @@ protected function getCachedResult(
* a cached request from an authenticated request happens before cache flush.
*/
if ($this->queryParamContextIsEdit($request) && ! $this->isUserAuthenticated($request)) {
\wp_cache_delete($this->cleanKey($key), $group);
return $this->dispatchRequest($server, $request);
}

Expand Down Expand Up @@ -356,4 +369,14 @@ private function isUserAuthenticated(WP_REST_Request $request) : bool
*/
return \apply_filters(self::FILTER_CACHE_VALIDATE_AUTH, false, $request) !== false;
}

/**
* Get the options.
* @param mixed $defaults
* @return mixed
*/
private function getOptions($defaults)
{
return \get_option(Admin::OPTION_KEY, $defaults);
}
}
16 changes: 12 additions & 4 deletions src/WpAdmin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,22 @@ protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar)

$wp_admin_bar->add_node([
'id' => WpRestApiCache::ID,
'title' => \esc_html__('REST API Cache', 'wp-rest-api-cache'),
'title' => \sprintf(
'<span class="ab-icon dashicons dashicons-shield" title="%s"></span><span class="ab-label">%s</span>',
\esc_attr__('REST API Cache', 'wp-rest-api-cache'),
\esc_html__('REST Cache', 'wp-rest-api-cache')
)
]);
$wp_admin_bar->add_menu([
'parent' => WpRestApiCache::ID,
'id' => self::MENU_ID,
'title' => \esc_html__('Empty all cache', 'wp-rest-api-cache'),
'href' => \esc_url($this->getEmptyCacheUrl()),
'meta' => [
'onclick' => 'return confirm("This will clear ALL cache, continue?")'
'onclick' => \sprintf(
'return confirm("%s")',
\esc_attr__('This will clear ALL cache, continue?', 'wp-rest-api-cache')
)
]
]);
}
Expand Down Expand Up @@ -181,7 +188,7 @@ protected function getOptions($key = null)
{
$options = \apply_filters(
self::FILTER_CACHE_OPTIONS,
\get_option(self::OPTION_KEY, $this->settings->getExpiration())
\get_option(self::OPTION_KEY, $this->settings->getSettings())
);

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

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

/**
Expand Down
24 changes: 17 additions & 7 deletions src/WpAdmin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@
class Settings extends BaseModel
{

const BYPASS = 'bypass';
const EXPIRATION = 'expiration';
const LENGTH = 'length';
const PERIOD = 'period';

/**
* Settings expiration array.
* Settings array.
*
* @var array $expiration
* @var array $settings
*/
protected $expiration = [];
protected $settings = [];

/**
* Get's the expiration settings array.
* @return array
*/
public function getExpiration() : array
public function getSettings() : array
{
return $this->expiration;
return $this->settings;
}

/**
Expand All @@ -37,7 +38,7 @@ public function getExpiration() : array
*/
public function setLength(int $length)
{
$this->expiration[self::EXPIRATION][self::LENGTH] = $length;
$this->settings[self::EXPIRATION][self::LENGTH] = $length;
}

/**
Expand All @@ -46,6 +47,15 @@ public function setLength(int $length)
*/
public function setPeriod(int $period)
{
$this->expiration[self::EXPIRATION][self::PERIOD] = $period;
$this->settings[self::EXPIRATION][self::PERIOD] = $period;
}

/**
* Sets the bypass value.
* @param string $value
*/
public function setBypass(string $value)
{
$this->settings[self::BYPASS] = $value;
}
}
9 changes: 9 additions & 0 deletions views/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class="button button-primary"><?php esc_html_e( 'empty cache', 'wp-rest-api-cach
</select>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Disable REST Cache', 'wp-rest-api-cache' ); ?></th>
<td>
<input type="checkbox"
name="<?php printf( '%s[%s]', Admin::OPTION_KEY, Settings::BYPASS ); ?>"
value="on"<?php checked($options[Settings::BYPASS], 'on'); ?>>

</td>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<td><input type="submit" class="button button-primary"
Expand Down
2 changes: 1 addition & 1 deletion wp-rest-api-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.
* Author: Austin Passy
* Author URI: http://github.com/thefrosty
* Version: 1.3.1
* Version: 1.4.0
* Requires at least: 4.9
* Tested up to: 4.9
* Requires PHP: 7.0
Expand Down

0 comments on commit 608098d

Please sign in to comment.