Skip to content

Commit 8e6fc58

Browse files
committed
Fixed rest
Fixed rest internal access
1 parent d006a23 commit 8e6fc58

File tree

5 files changed

+79
-9
lines changed

5 files changed

+79
-9
lines changed

src/inc/Format.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public static function hook(string $hook) : string
133133
'rest-api-response' => 'rest_pre_echo_response',
134134
'rest-api-dispatch' => 'rest_pre_dispatch',
135135
'rest-api-callback' => 'rest_request_before_callbacks',
136+
'rest-api-nocache' => 'rest_send_nocache_headers',
136137
];
137138

138139
if ( isset($format[$name]) ) {

src/inc/Restful.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static function getAttributes(RestfulRequest $request) : array
143143
}
144144

145145
/**
146-
* Get request params.
146+
* Get request params (POST).
147147
*
148148
* @access public
149149
* @param RestfulRequest $request

src/int/RestfulInterface.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ function noRoute(array $except = []) : self;
7373
function noPadding() : self;
7474

7575
/**
76-
* Override REST 404.
76+
* Override REST response.
7777
* [Action: init].
7878
* [Filter: rest-api-response].
7979
*
8080
* @return object
8181
*/
82-
function notFound() : self;
82+
function override() : self;
8383

8484
/**
8585
* Disable REST.
@@ -110,20 +110,28 @@ function restrict(array $rules);
110110
function addRoutes($server);
111111

112112
/**
113-
* Set endpoint default action callback.
113+
* Set endpoint action callback.
114114
*
115115
* @param object $request
116116
* @return mixed
117117
*/
118118
function action($request);
119119

120120
/**
121-
* Set endpoint default access callback.
121+
* Set endpoint access callback.
122122
*
123123
* @param object $request
124124
* @return mixed
125125
*/
126126
function access($request);
127+
128+
/**
129+
* Set endpoint internal access callback.
130+
*
131+
* @param object $request
132+
* @return mixed
133+
*/
134+
function internal($request);
127135

128136
/**
129137
* Add auth token.

src/lib/RestAPI.php

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,31 @@ public function noPadding() : self
153153
/**
154154
* @inheritdoc
155155
*/
156-
public function notFound() : self
156+
public function override() : self
157157
{
158158
$this->addFilter('rest-api-response', function($response) {
159-
159+
160160
if ( isset($response['code']) ) {
161-
if ( $response['code'] == 'rest_no_route' ) {
161+
162+
if ( $response['code'] == $this->undash('rest-no-route') ) {
162163
$this->setResponse('REST API route not found', [], 'error', 404);
163164
}
165+
if ( $response['code'] == $this->undash('rest-forbidden') ) {
166+
$this->setResponse('REST API route forbidden', [], 'error', 403);
167+
}
168+
if ( $response['code'] == $this->undash('rest-cookie-invalid-nonce') ) {
169+
$this->setResponse('REST API route invalid cookie', [], 'error', 403);
170+
}
171+
if ( $response['code'] == $this->undash('rest-invalid-type') ) {
172+
$this->setResponse('REST API route invalid type', [], 'error', 422);
173+
}
174+
if ( $response['code'] == $this->undash('rest-invalid-pattern') ) {
175+
$this->setResponse('REST API route invalid pattern', [], 'error', 422);
176+
}
177+
if ( $response['code'] == $this->undash('rest-invalid-json') ) {
178+
$this->setResponse('REST API route invalid json', [], 'error', 422);
179+
}
180+
164181
}
165182

166183
return $response;
@@ -229,6 +246,17 @@ public function access($request)
229246
return true;
230247
}
231248

249+
/**
250+
* @inheritdoc
251+
*/
252+
public function internal($request)
253+
{
254+
if ( $this->isLoggedIn() ) {
255+
return $this->hasCap('manage-options');
256+
}
257+
return false;
258+
}
259+
232260
/**
233261
* Fetch response body.
234262
*
@@ -339,7 +367,7 @@ protected function getAttributes($request) : array
339367
}
340368

341369
/**
342-
* Get request params.
370+
* Get request params (POST).
343371
*
344372
* @access protected
345373
* @inheritdoc
@@ -448,6 +476,28 @@ protected function isValidParam($request, string $key) : bool
448476
return Restful::isValidParam($request, $key);
449477
}
450478

479+
/**
480+
* Check POST method.
481+
*
482+
* @access protected
483+
* @inheritdoc
484+
*/
485+
protected function isPost($request) : bool
486+
{
487+
return ($this->getMethod($request) == 'POST');
488+
}
489+
490+
/**
491+
* Check GET method.
492+
*
493+
* @access protected
494+
* @inheritdoc
495+
*/
496+
protected function isGet($request) : bool
497+
{
498+
return ($this->getMethod($request) == 'GET');
499+
}
500+
451501
/**
452502
* Register plugin route item.
453503
*

src/tr/TraitFormattable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,4 +907,15 @@ protected function toKey($data) : string
907907
{
908908
return Converter::toKey($data);
909909
}
910+
911+
/**
912+
* Convert dynamic types.
913+
*
914+
* @access protected
915+
* @inheritdoc
916+
*/
917+
protected function toTypes($value)
918+
{
919+
return Converter::toTypes($value);
920+
}
910921
}

0 commit comments

Comments
 (0)