@@ -41,7 +41,7 @@ class RestDispatch implements WpHooksInterface
4141 const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete ' ;
4242 const QUERY_CACHE_REFRESH = 'rest_cache_refresh ' ;
4343
44- const VERSION = '1.2.2 ' ;
44+ const VERSION = '1.4.0 ' ;
4545
4646 /**
4747 * Has the current request been cached? Avoids the multi loop calls where
@@ -56,8 +56,11 @@ class RestDispatch implements WpHooksInterface
5656 */
5757 public function addHooks ()
5858 {
59- $ this ->addFilter ('rest_pre_dispatch ' , [$ this , 'preDispatch ' ], 10 , 3 );
60- $ this ->addFilter ('rest_post_dispatch ' , [$ this , 'postDispatch ' ], 10 , 3 );
59+ $ options = $ this ->getOptions ([]);
60+ if (!isset ($ options [Settings::BYPASS ]) || $ options [Settings::BYPASS ] !== 'on ' ) {
61+ $ this ->addFilter ('rest_pre_dispatch ' , [$ this , 'preDispatch ' ], 10 , 3 );
62+ $ this ->addFilter ('rest_post_dispatch ' , [$ this , 'postDispatch ' ], 10 , 3 );
63+ }
6164 }
6265
6366 /**
@@ -72,13 +75,22 @@ public function addHooks()
7275 */
7376 protected function preDispatch ($ result , WP_REST_Server $ server , WP_REST_Request $ request )
7477 {
78+ if ($ result !== null ) {
79+ return $ result ;
80+ }
7581 $ request_uri = $ this ->getRequestUri ();
7682 $ group = $ this ->getCacheGroup ();
7783 $ key = $ this ->getCacheKey ($ request_uri , $ server , $ request );
7884
79- // Return the result if it's a non-readable (GET) method or it's been cached.
85+ /*
86+ * Return the result if:
87+ * It's a non-readable (GET) method.
88+ * It's been cached already.
89+ * The request has an authorization header.
90+ */
8091 if ($ request ->get_method () !== WP_REST_Server::READABLE ||
81- (! empty (self ::$ cached [$ this ->cleanKey ($ key )]) && self ::$ cached [$ this ->cleanKey ($ key )] === true )
92+ (! empty (self ::$ cached [$ this ->cleanKey ($ key )]) && self ::$ cached [$ this ->cleanKey ($ key )] === true ) ||
93+ ! empty ($ request ->get_header ('authorization ' ))
8294 ) {
8395 return $ result ;
8496 }
@@ -220,7 +232,7 @@ protected function getCachedResult(
220232 Settings::PERIOD => MINUTE_IN_SECONDS ,
221233 ],
222234 ];
223- $ options = \get_option (Admin:: OPTION_KEY , $ defaults );
235+ $ options = $ this -> getOptions ( $ defaults );
224236 /**
225237 * Filter for cache expiration time.
226238 * @param int Expiration time.
@@ -247,6 +259,7 @@ protected function getCachedResult(
247259 * a cached request from an authenticated request happens before cache flush.
248260 */
249261 if ($ this ->queryParamContextIsEdit ($ request ) && ! $ this ->isUserAuthenticated ($ request )) {
262+ \wp_cache_delete ($ this ->cleanKey ($ key ), $ group );
250263 return $ this ->dispatchRequest ($ server , $ request );
251264 }
252265
@@ -356,4 +369,14 @@ private function isUserAuthenticated(WP_REST_Request $request) : bool
356369 */
357370 return \apply_filters (self ::FILTER_CACHE_VALIDATE_AUTH , false , $ request ) !== false ;
358371 }
372+
373+ /**
374+ * Get the options.
375+ * @param mixed $defaults
376+ * @return mixed
377+ */
378+ private function getOptions ($ defaults )
379+ {
380+ return \get_option (Admin::OPTION_KEY , $ defaults );
381+ }
359382}
0 commit comments