Skip to content

Commit 4441d31

Browse files
committed
Remove helper functions and add a new replace method.
1 parent 69da5e2 commit 4441d31

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

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
}

0 commit comments

Comments
 (0)