Extract automation API to MailPoet REST API
[MAILPOET-4523]
This commit is contained in:
committed by
John Oleksowicz
parent
1b5d6bd974
commit
ba35ddf6e6
102
mailpoet/lib/API/REST/API.php
Normal file
102
mailpoet/lib/API/REST/API.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use Throwable;
|
||||
use WP_REST_Request;
|
||||
|
||||
class API {
|
||||
public const REST_API_INIT_ACTION = 'mailpoet/rest-api/init';
|
||||
|
||||
private const PREFIX = 'mailpoet/v1';
|
||||
private const WP_REST_API_INIT_ACTION = 'rest_api_init';
|
||||
|
||||
/** @var EndpointContainer */
|
||||
private $endpointContainer;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(
|
||||
EndpointContainer $endpointContainer,
|
||||
WPFunctions $wp
|
||||
) {
|
||||
$this->endpointContainer = $endpointContainer;
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
public function init(): void {
|
||||
$this->wp->addAction(self::WP_REST_API_INIT_ACTION, function () {
|
||||
$this->wp->doAction(self::REST_API_INIT_ACTION, [$this]);
|
||||
});
|
||||
}
|
||||
|
||||
public function registerGetRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'GET');
|
||||
}
|
||||
|
||||
public function registerPostRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'POST');
|
||||
}
|
||||
|
||||
public function registerPutRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PUT');
|
||||
}
|
||||
|
||||
public function registerPatchRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PATCH');
|
||||
}
|
||||
|
||||
public function registerDeleteRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'DELETE');
|
||||
}
|
||||
|
||||
protected function registerRoute(string $route, string $endpointClass, string $method): void {
|
||||
$schema = array_map(function (Schema $field) {
|
||||
return $field->toArray();
|
||||
}, $endpointClass::getRequestSchema());
|
||||
|
||||
$this->wp->registerRestRoute(self::PREFIX, $route, [
|
||||
'methods' => $method,
|
||||
'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass, $schema) {
|
||||
try {
|
||||
$endpoint = $this->endpointContainer->get($endpointClass);
|
||||
$wpRequest = $this->sanitizeUnknownParams($wpRequest, $schema);
|
||||
$request = new Request($wpRequest);
|
||||
return $endpoint->handle($request);
|
||||
} catch (Throwable $e) {
|
||||
return $this->convertToErrorResponse($e);
|
||||
}
|
||||
},
|
||||
'permission_callback' => function () use ($endpointClass) {
|
||||
$endpoint = $this->endpointContainer->get($endpointClass);
|
||||
return $endpoint->checkPermissions();
|
||||
},
|
||||
'args' => $schema,
|
||||
]);
|
||||
}
|
||||
|
||||
private function convertToErrorResponse(Throwable $e): ErrorResponse {
|
||||
$response = $e instanceof Exception
|
||||
? new ErrorResponse($e->getStatusCode(), $e->getMessage(), $e->getErrorCode())
|
||||
: new ErrorResponse(500, __('An unknown error occurred.', 'mailpoet'), 'mailpoet_automation_unknown_error');
|
||||
|
||||
if ($response->get_status() >= 500) {
|
||||
error_log((string)$e); // phpcs:ignore Squiz.PHP.DiscouragedFunctions
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function sanitizeUnknownParams(WP_REST_Request $wpRequest, array $args): WP_REST_Request {
|
||||
// Remove all params that are not declared in the schema, so we use just the validated ones.
|
||||
// Note that this doesn't work recursively for object properties as it is harder to solve
|
||||
// with features like oneOf, anyOf, additional properties, or pattern properties.
|
||||
$extraParams = array_diff(array_keys($wpRequest->get_params()), array_keys($args));
|
||||
foreach ($extraParams as $extraParam) {
|
||||
unset($wpRequest[(string)$extraParam]);
|
||||
}
|
||||
return $wpRequest;
|
||||
}
|
||||
}
|
18
mailpoet/lib/API/REST/Endpoint.php
Normal file
18
mailpoet/lib/API/REST/Endpoint.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
abstract class Endpoint {
|
||||
abstract public function handle(Request $request): Response;
|
||||
|
||||
public function checkPermissions(): bool {
|
||||
return current_user_can('admin');
|
||||
}
|
||||
|
||||
/** @return array<string, Schema> */
|
||||
public static function getRequestSchema(): array {
|
||||
return [];
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
use MailPoet\InvalidStateException;
|
||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
class ErrorResponse extends Response {
|
||||
public function __construct(
|
11
mailpoet/lib/API/REST/Exception.php
Normal file
11
mailpoet/lib/API/REST/Exception.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
interface Exception {
|
||||
public function getStatusCode();
|
||||
|
||||
public function getErrorCode(): string;
|
||||
|
||||
public function getErrors(): array;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
use WP_REST_Request;
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
namespace MailPoet\API\REST;
|
||||
|
||||
use WP_REST_Response;
|
||||
|
@ -2,101 +2,50 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use MailPoet\Automation\Engine\Exceptions\Exception;
|
||||
use MailPoet\API\REST\API as MailPoetApi;
|
||||
use MailPoet\Automation\Engine\Hooks;
|
||||
use MailPoet\Automation\Engine\WordPress;
|
||||
use MailPoet\Validator\Schema;
|
||||
use Throwable;
|
||||
use WP_REST_Request;
|
||||
|
||||
class API {
|
||||
private const PREFIX = 'mailpoet/v1/automation';
|
||||
private const WP_REST_API_INIT_ACTION = 'rest_api_init';
|
||||
private const PREFIX = 'automation/';
|
||||
|
||||
/** @var EndpointContainer */
|
||||
private $endpointContainer;
|
||||
/** @var MailPoetApi */
|
||||
private $api;
|
||||
|
||||
/** @var WordPress */
|
||||
private $wordPress;
|
||||
|
||||
public function __construct(
|
||||
EndpointContainer $endpointContainer,
|
||||
MailPoetApi $api,
|
||||
WordPress $wordPress
|
||||
) {
|
||||
$this->endpointContainer = $endpointContainer;
|
||||
$this->api = $api;
|
||||
$this->wordPress = $wordPress;
|
||||
}
|
||||
|
||||
public function initialize(): void {
|
||||
$this->wordPress->addAction(self::WP_REST_API_INIT_ACTION, function () {
|
||||
$this->wordPress->addAction(MailPoetApi::REST_API_INIT_ACTION, function () {
|
||||
$this->wordPress->doAction(Hooks::API_INITIALIZE, [$this]);
|
||||
});
|
||||
}
|
||||
|
||||
public function registerGetRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'GET');
|
||||
$this->api->registerGetRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerPostRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'POST');
|
||||
$this->api->registerPostRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerPutRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PUT');
|
||||
$this->api->registerPutRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerPatchRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PATCH');
|
||||
$this->api->registerPatchRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerDeleteRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'DELETE');
|
||||
}
|
||||
|
||||
private function registerRoute(string $route, string $endpointClass, string $method): void {
|
||||
$schema = array_map(function (Schema $field) {
|
||||
return $field->toArray();
|
||||
}, $endpointClass::getRequestSchema());
|
||||
|
||||
$this->wordPress->registerRestRoute(self::PREFIX, $route, [
|
||||
'methods' => $method,
|
||||
'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass, $schema) {
|
||||
try {
|
||||
$endpoint = $this->endpointContainer->get($endpointClass);
|
||||
$wpRequest = $this->sanitizeUnknownParams($wpRequest, $schema);
|
||||
$request = new Request($wpRequest);
|
||||
return $endpoint->handle($request);
|
||||
} catch (Throwable $e) {
|
||||
return $this->convertToErrorResponse($e);
|
||||
}
|
||||
},
|
||||
'permission_callback' => function () use ($endpointClass) {
|
||||
$endpoint = $this->endpointContainer->get($endpointClass);
|
||||
return $endpoint->checkPermissions();
|
||||
},
|
||||
'args' => $schema,
|
||||
]);
|
||||
}
|
||||
|
||||
private function convertToErrorResponse(Throwable $e): ErrorResponse {
|
||||
$response = $e instanceof Exception
|
||||
? new ErrorResponse($e->getStatusCode(), $e->getMessage(), $e->getErrorCode())
|
||||
: new ErrorResponse(500, __('An unknown error occurred.', 'mailpoet'), 'mailpoet_automation_unknown_error');
|
||||
|
||||
if ($response->get_status() >= 500) {
|
||||
error_log((string)$e); // phpcs:ignore Squiz.PHP.DiscouragedFunctions
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function sanitizeUnknownParams(WP_REST_Request $wpRequest, array $args): WP_REST_Request {
|
||||
// Remove all params that are not declared in the schema, so we use just the validated ones.
|
||||
// Note that this doesn't work recursively for object properties as it is harder to solve
|
||||
// with features like oneOf, anyOf, additional properties, or pattern properties.
|
||||
$extraParams = array_diff(array_keys($wpRequest->get_params()), array_keys($args));
|
||||
foreach ($extraParams as $extraParam) {
|
||||
unset($wpRequest[(string)$extraParam]);
|
||||
}
|
||||
return $wpRequest;
|
||||
$this->api->registerDeleteRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
}
|
||||
|
@ -2,20 +2,11 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use MailPoet\API\REST\Endpoint as MailPoetEndpoint;
|
||||
use MailPoet\Automation\Engine\Engine;
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
use function current_user_can;
|
||||
|
||||
abstract class Endpoint {
|
||||
abstract public function handle(Request $request): Response;
|
||||
|
||||
abstract class Endpoint extends MailPoetEndpoint {
|
||||
public function checkPermissions(): bool {
|
||||
return current_user_can(Engine::CAPABILITY_MANAGE_AUTOMATIONS);
|
||||
}
|
||||
|
||||
/** @return array<string, Schema> */
|
||||
public static function getRequestSchema(): array {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Endpoints\System;
|
||||
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Automation\Engine\API\Endpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\Automation\Engine\Migrations\Migrator;
|
||||
use MailPoet\Features\FeatureFlagsController;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Endpoints\System;
|
||||
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Automation\Engine\API\Endpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\Automation\Engine\Migrations\Migrator;
|
||||
|
||||
class DatabasePostEndpoint extends Endpoint {
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Automation\Engine\API\Endpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\Automation\Engine\Data\WorkflowTemplate;
|
||||
use MailPoet\Automation\Engine\Storage\WorkflowTemplateStorage;
|
||||
use MailPoet\Validator\Builder;
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Automation\Engine\API\Endpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\Automation\Engine\Builder\CreateWorkflowFromTemplateController;
|
||||
use MailPoet\Validator\Builder;
|
||||
|
||||
|
@ -3,9 +3,9 @@
|
||||
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Automation\Engine\API\Endpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\Automation\Engine\Data\Workflow;
|
||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||
use MailPoet\Validator\Builder;
|
||||
|
@ -3,9 +3,9 @@
|
||||
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Automation\Engine\API\Endpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\Automation\Engine\Builder\UpdateWorkflowController;
|
||||
use MailPoet\Automation\Engine\Data\NextStep;
|
||||
use MailPoet\Automation\Engine\Data\Step;
|
||||
|
@ -2,12 +2,14 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\Exceptions;
|
||||
|
||||
use Exception as PhpException;
|
||||
use MailPoet\API\REST\Exception as RestException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Frames all MailPoet Automation exceptions ("$e instanceof MailPoet\Automation\Exception").
|
||||
*/
|
||||
abstract class Exception extends \Exception {
|
||||
abstract class Exception extends PhpException implements RestException {
|
||||
/** @var int */
|
||||
protected $statusCode = 500;
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\API\JSON\API;
|
||||
use MailPoet\API\REST\API as RestApi;
|
||||
use MailPoet\AutomaticEmails\AutomaticEmails;
|
||||
use MailPoet\Automation\Engine\Engine;
|
||||
use MailPoet\Automation\Engine\Hooks as AutomationHooks;
|
||||
@ -37,6 +38,9 @@ class Initializer {
|
||||
/** @var API */
|
||||
private $api;
|
||||
|
||||
/** @var RestApi */
|
||||
private $restApi;
|
||||
|
||||
/** @var Activator */
|
||||
private $activator;
|
||||
|
||||
@ -115,6 +119,7 @@ class Initializer {
|
||||
RendererFactory $rendererFactory,
|
||||
AccessControl $accessControl,
|
||||
API $api,
|
||||
RestApi $restApi,
|
||||
Activator $activator,
|
||||
SettingsController $settings,
|
||||
Router\Router $router,
|
||||
@ -143,6 +148,7 @@ class Initializer {
|
||||
$this->rendererFactory = $rendererFactory;
|
||||
$this->accessControl = $accessControl;
|
||||
$this->api = $api;
|
||||
$this->restApi = $restApi;
|
||||
$this->activator = $activator;
|
||||
$this->settings = $settings;
|
||||
$this->router = $router;
|
||||
@ -383,6 +389,7 @@ class Initializer {
|
||||
if (!defined(self::INITIALIZED)) return;
|
||||
try {
|
||||
$this->api->init();
|
||||
$this->restApi->init();
|
||||
$this->router->init();
|
||||
$this->setupUserLocale();
|
||||
} catch (\Exception $e) {
|
||||
|
@ -94,6 +94,11 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\ScheduledTaskSubscriberResponseBuilder::class)->setPublic(true);
|
||||
// REST API
|
||||
$container->autowire(\MailPoet\API\REST\API::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\REST\EndpointContainer::class)
|
||||
->setPublic(true)
|
||||
->setArgument('$container', new Reference(ContainerWrapper::class));
|
||||
// Automatic emails
|
||||
$container->autowire(\MailPoet\AutomaticEmails\AutomaticEmails::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\AutomaticEmails\AutomaticEmailFactory::class)->setPublic(true);
|
||||
@ -105,9 +110,6 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedProduct::class)->setPublic(true);
|
||||
// Automation
|
||||
$container->autowire(\MailPoet\Automation\Engine\API\API::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Automation\Engine\API\EndpointContainer::class)
|
||||
->setPublic(true)
|
||||
->setArgument('$container', new Reference(ContainerWrapper::class));
|
||||
$container->autowire(\MailPoet\Automation\Engine\Builder\CreateWorkflowFromTemplateController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Automation\Engine\Builder\UpdateStepsController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Automation\Engine\Builder\UpdateWorkflowController::class)->setPublic(true);
|
||||
|
@ -762,6 +762,10 @@ class Functions {
|
||||
return rest_url($path, $scheme);
|
||||
}
|
||||
|
||||
public function registerRestRoute(string $namespace, string $route, array $args = [], bool $override = false): bool {
|
||||
return register_rest_route($namespace, $route, $args, $override);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return true|WP_Error
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace MailPoet\REST\Automation\API\Endpoints;
|
||||
|
||||
use MailPoet\Automation\Engine\API\Endpoint as APIEndpoint;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\API\Response;
|
||||
use MailPoet\API\REST\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\API\REST\Response;
|
||||
use MailPoet\Validator\Builder;
|
||||
|
||||
class Endpoint extends APIEndpoint {
|
@ -2,19 +2,19 @@
|
||||
|
||||
namespace MailPoet\REST\Automation\API;
|
||||
|
||||
require_once __DIR__ . '/../../Test.php';
|
||||
require_once __DIR__ . '/../Test.php';
|
||||
require_once __DIR__ . '/Endpoint.php';
|
||||
|
||||
use MailPoet\Automation\Engine\API\API;
|
||||
use MailPoet\Automation\Engine\API\EndpointContainer;
|
||||
use MailPoet\Automation\Engine\API\Request;
|
||||
use MailPoet\Automation\Engine\WordPress;
|
||||
use MailPoet\API\REST\API;
|
||||
use MailPoet\API\REST\EndpointContainer;
|
||||
use MailPoet\API\REST\Request;
|
||||
use MailPoet\REST\Automation\API\Endpoints\Endpoint;
|
||||
use MailPoet\REST\Test;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class EndpointTest extends Test {
|
||||
/** @var string */
|
||||
private $prefix = '/mailpoet/v1/automation/mailpoet-api-testing-route';
|
||||
private $prefix = '/mailpoet/v1/mailpoet-api-testing-route';
|
||||
|
||||
public function testGetParams(): void {
|
||||
$path = strtolower(__FUNCTION__);
|
||||
@ -127,7 +127,7 @@ class EndpointTest extends Test {
|
||||
return new Endpoint($requestCallback);
|
||||
}
|
||||
]),
|
||||
$this->diContainer->get(WordPress::class)
|
||||
$this->diContainer->get(WPFunctions::class)
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user