diff --git a/CHANGELONG.md b/CHANGELONG.md index 24c5ea5..fc59d22 100755 --- a/CHANGELONG.md +++ b/CHANGELONG.md @@ -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. @@ -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. \ No newline at end of file +- This CHANGELOG file. diff --git a/composer.json b/composer.json index 19b0046..f324426 100755 --- a/composer.json +++ b/composer.json @@ -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": [ { @@ -18,7 +18,6 @@ "php": ">=7.0.4" }, "require-dev": { - "10up/wp_mock": "dev-dev", "phpunit/phpunit": "6.*", "squizlabs/php_codesniffer": "^3.2" }, diff --git a/src/RestApi/RestDispatch.php b/src/RestApi/RestDispatch.php index 8c6fb41..1f94a4e 100644 --- a/src/RestApi/RestDispatch.php +++ b/src/RestApi/RestDispatch.php @@ -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 @@ -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); + } } /** @@ -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; } @@ -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. @@ -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); } @@ -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); + } } diff --git a/src/WpAdmin/Admin.php b/src/WpAdmin/Admin.php index 547851c..bb8c9b6 100644 --- a/src/WpAdmin/Admin.php +++ b/src/WpAdmin/Admin.php @@ -118,7 +118,11 @@ 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( + '%s', + \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, @@ -126,7 +130,10 @@ protected function adminBarMenu(WP_Admin_Bar $wp_admin_bar) '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') + ) ] ]); } @@ -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)) { @@ -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'); } /** diff --git a/src/WpAdmin/Settings.php b/src/WpAdmin/Settings.php index c83d132..acbc377 100644 --- a/src/WpAdmin/Settings.php +++ b/src/WpAdmin/Settings.php @@ -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; } /** @@ -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; } /** @@ -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; } } diff --git a/views/settings.php b/views/settings.php index 948a495..56d8b56 100644 --- a/views/settings.php +++ b/views/settings.php @@ -55,6 +55,15 @@ class="button button-primary"> + + + + > + + +