Skip to content

Commit 6bed836

Browse files
authored
updating route helpers (#455)
1 parent 4aaab32 commit 6bed836

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

55
This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/).
66

7+
## [10.2.0]
8+
9+
### Added
10+
11+
- New `getRequestParams` helper to extract params from request based on the request type.
12+
- New `prepareSimpleApiParams` helper to prepare and secure params for simple API endpoints.
13+
- New `checkUserPermission` helper to check user permission for route action.
14+
715
## [10.1.0]
816

917
### Fixed
@@ -839,6 +847,7 @@ Init setup
839847
- Gutenberg Blocks Registration.
840848
- Assets Manifest data.
841849

850+
[10.2.0]: https://github.com/infinum/eightshift-libs/compare/10.1.0...10.2.0
842851
[10.1.0]: https://github.com/infinum/eightshift-libs/compare/10.0.0...10.1.0
843852
[10.0.0]: https://github.com/infinum/eightshift-libs/compare/9.3.5...10.0.0
844853
[9.3.5]: https://github.com/infinum/eightshift-libs/compare/9.3.4...9.3.5

src/Helpers/StoreBlocksTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function getComponents(): array
106106
/**
107107
* Get component details.
108108
*
109-
* @param string $component Componennt name to get.
109+
* @param string $component Component name to get.
110110
*
111111
* @throws InvalidBlock If component is missing.
112112
*

src/Rest/Routes/AbstractRoute.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
namespace EightshiftLibs\Rest\Routes;
1212

13+
use EightshiftLibs\Helpers\Helpers;
1314
use EightshiftLibs\Services\ServiceInterface;
1415
use EightshiftLibs\Rest\RouteInterface;
16+
use WP_REST_Request;
1517
use WP_REST_Server;
1618

1719
/**
@@ -148,4 +150,86 @@ protected function overrideRoute(): bool
148150
{
149151
return false;
150152
}
153+
154+
155+
/**
156+
* Extract params from request.
157+
* Check if array then output only value that is not empty.
158+
*
159+
* @param WP_REST_Request $request $request Data got from endpoint url.
160+
* @param string $type Request type.
161+
*
162+
* @return array<string, mixed>
163+
*/
164+
protected function getRequestParams(WP_REST_Request $request, string $type = self::CREATABLE): array
165+
{
166+
// Check type of request and extract params.
167+
switch ($type) {
168+
case self::CREATABLE:
169+
$params = $request->get_body_params();
170+
break;
171+
case self::READABLE:
172+
$params = $request->get_params();
173+
break;
174+
default:
175+
$params = [];
176+
break;
177+
}
178+
179+
// Check if request maybe has json params usually sent by the Block editor.
180+
if ($request->get_json_params()) {
181+
$params = \array_merge(
182+
$params,
183+
$request->get_json_params(),
184+
);
185+
}
186+
187+
return $params;
188+
}
189+
190+
/**
191+
* Convert JS FormData object to usable data in php.
192+
*
193+
* @param WP_REST_Request $request $request Data got from endpoint url.
194+
* @param string $type Request type.
195+
*
196+
* @return array<string, mixed>
197+
*/
198+
protected function prepareSimpleApiParams(WP_REST_Request $request, string $type = self::CREATABLE): array
199+
{
200+
// Get params.
201+
$params = $this->getRequestParams($request, $type);
202+
203+
// Bailout if there are no params.
204+
if (!$params) {
205+
return [];
206+
}
207+
208+
return \array_map(
209+
static function ($item) {
210+
return \sanitize_text_field($item);
211+
},
212+
$params
213+
);
214+
}
215+
216+
/**
217+
* Check user permission for route action.
218+
*
219+
* @param string $permission Permission to check.
220+
* @param array<string, mixed> $additional Additional data to pass.
221+
*
222+
* @return array<string, mixed>
223+
*/
224+
protected function checkUserPermission(string $permission, array $additional = []): array
225+
{
226+
if (\current_user_can($permission)) {
227+
return [];
228+
}
229+
230+
return Helpers::getApiErrorPublicOutput(
231+
\__('You don\'t have enough permissions to perform this action!', 'eightshift-libs'),
232+
$additional
233+
);
234+
}
151235
}

0 commit comments

Comments
 (0)