API.php
6 months ago
AbstractListingEndpoint.php
3 weeks ago
ApiException.php
2 months ago
Endpoint.php
3 months ago
EndpointContainer.php
3 years ago
ErrorResponse.php
3 years ago
Exception.php
3 years ago
ListingRequestValidationTrait.php
1 month ago
Request.php
3 years ago
Response.php
1 year ago
index.php
3 years ago
API.php
108 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\REST; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Validator\Schema; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | use Throwable; |
| 11 | use WP_REST_Request; |
| 12 | |
| 13 | class API { |
| 14 | public const REST_API_INIT_ACTION = 'mailpoet/rest-api/init'; |
| 15 | |
| 16 | private const PREFIX = 'mailpoet/v1'; |
| 17 | private const WP_REST_API_INIT_ACTION = 'rest_api_init'; |
| 18 | |
| 19 | /** @var EndpointContainer */ |
| 20 | private $endpointContainer; |
| 21 | |
| 22 | /** @var WPFunctions */ |
| 23 | private $wp; |
| 24 | |
| 25 | public function __construct( |
| 26 | EndpointContainer $endpointContainer, |
| 27 | WPFunctions $wp |
| 28 | ) { |
| 29 | $this->endpointContainer = $endpointContainer; |
| 30 | $this->wp = $wp; |
| 31 | } |
| 32 | |
| 33 | public function init(): void { |
| 34 | $this->wp->addAction(self::WP_REST_API_INIT_ACTION, function () { |
| 35 | $this->wp->doAction(self::REST_API_INIT_ACTION, [$this]); |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | public function registerGetRoute(string $route, string $endpoint): void { |
| 40 | $this->registerRoute($route, $endpoint, 'GET'); |
| 41 | } |
| 42 | |
| 43 | public function registerPostRoute(string $route, string $endpoint): void { |
| 44 | $this->registerRoute($route, $endpoint, 'POST'); |
| 45 | } |
| 46 | |
| 47 | public function registerPutRoute(string $route, string $endpoint): void { |
| 48 | $this->registerRoute($route, $endpoint, 'PUT'); |
| 49 | } |
| 50 | |
| 51 | public function registerPatchRoute(string $route, string $endpoint): void { |
| 52 | $this->registerRoute($route, $endpoint, 'PATCH'); |
| 53 | } |
| 54 | |
| 55 | public function registerDeleteRoute(string $route, string $endpoint): void { |
| 56 | $this->registerRoute($route, $endpoint, 'DELETE'); |
| 57 | } |
| 58 | |
| 59 | protected function registerRoute(string $route, string $endpointClass, string $method): void { |
| 60 | $schema = array_map(function (Schema $field) { |
| 61 | return $field->toArray(); |
| 62 | }, $endpointClass::getRequestSchema()); |
| 63 | |
| 64 | $this->wp->registerRestRoute(self::PREFIX, $route, [ |
| 65 | 'methods' => $method, |
| 66 | 'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass, $schema) { |
| 67 | try { |
| 68 | $endpoint = $this->endpointContainer->get($endpointClass); |
| 69 | $wpRequest = $this->sanitizeUnknownParams($wpRequest, $schema); |
| 70 | $request = new Request($wpRequest); |
| 71 | return $endpoint->handle($request); |
| 72 | } catch (Throwable $e) { |
| 73 | return $this->convertToErrorResponse($e); |
| 74 | } |
| 75 | }, |
| 76 | 'permission_callback' => function () use ($endpointClass) { |
| 77 | $endpoint = $this->endpointContainer->get($endpointClass); |
| 78 | return $endpoint->checkPermissions(); // nosemgrep: scanner.php.wp.security.rest-route.permission-callback.incorrect-return |
| 79 | }, |
| 80 | 'args' => $schema, |
| 81 | ]); |
| 82 | } |
| 83 | |
| 84 | private function convertToErrorResponse(Throwable $e): ErrorResponse { |
| 85 | $response = $e instanceof Exception |
| 86 | ? new ErrorResponse($e->getStatusCode(), $e->getMessage(), $e->getErrorCode(), $e->getErrors()) |
| 87 | : new ErrorResponse(500, __('An unknown error occurred.', 'mailpoet'), 'mailpoet_automation_unknown_error'); |
| 88 | |
| 89 | if ($response->get_status() >= 500 && function_exists('error_log')) { |
| 90 | // phpcs:disable QITStandard.PHP.DebugCode.DebugFunctionFound |
| 91 | error_log((string)$e); // phpcs:ignore Squiz.PHP.DiscouragedFunctions |
| 92 | // phpcs:enable QITStandard.PHP.DebugCode.DebugFunctionFound |
| 93 | } |
| 94 | return $response; |
| 95 | } |
| 96 | |
| 97 | private function sanitizeUnknownParams(WP_REST_Request $wpRequest, array $args): WP_REST_Request { |
| 98 | // Remove all params that are not declared in the schema, so we use just the validated ones. |
| 99 | // Note that this doesn't work recursively for object properties as it is harder to solve |
| 100 | // with features like oneOf, anyOf, additional properties, or pattern properties. |
| 101 | $extraParams = array_diff(array_keys($wpRequest->get_params()), array_keys($args)); |
| 102 | foreach ($extraParams as $extraParam) { |
| 103 | unset($wpRequest[(string)$extraParam]); |
| 104 | } |
| 105 | return $wpRequest; |
| 106 | } |
| 107 | } |
| 108 |