|
10 | 10 |
|
11 | 11 | namespace EightshiftLibs\Rest\Routes;
|
12 | 12 |
|
| 13 | +use EightshiftLibs\Helpers\Helpers; |
13 | 14 | use EightshiftLibs\Services\ServiceInterface;
|
14 | 15 | use EightshiftLibs\Rest\RouteInterface;
|
| 16 | +use WP_REST_Request; |
15 | 17 | use WP_REST_Server;
|
16 | 18 |
|
17 | 19 | /**
|
@@ -148,4 +150,86 @@ protected function overrideRoute(): bool
|
148 | 150 | {
|
149 | 151 | return false;
|
150 | 152 | }
|
| 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 | + } |
151 | 235 | }
|
0 commit comments