ResponseBuilders
1 month ago
v1
1 month ago
API.php
2 months ago
Endpoint.php
1 year ago
Error.php
2 months ago
ErrorHandler.php
1 year ago
ErrorResponse.php
3 years ago
Response.php
2 months ago
SuccessResponse.php
3 years ago
index.php
3 years ago
Endpoint.php
61 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\v1\RedirectResponse; |
| 9 | use MailPoet\Config\AccessControl; |
| 10 | |
| 11 | abstract class Endpoint { |
| 12 | const TYPE_POST = 'POST'; |
| 13 | const TYPE_GET = 'GET'; |
| 14 | |
| 15 | public $permissions = [ |
| 16 | 'global' => AccessControl::PERMISSION_MANAGE_SETTINGS, |
| 17 | 'methods' => [], |
| 18 | ]; |
| 19 | |
| 20 | protected static $getMethods = []; |
| 21 | |
| 22 | public function successResponse( |
| 23 | $data = [], $meta = [], $status = Response::STATUS_OK |
| 24 | ) { |
| 25 | return new SuccessResponse($data, $meta, $status); |
| 26 | } |
| 27 | |
| 28 | public function errorResponse( |
| 29 | $errors = [], $meta = [], $status = Response::STATUS_NOT_FOUND |
| 30 | ) { |
| 31 | if (empty($errors)) { |
| 32 | $errors = [ |
| 33 | Error::UNKNOWN => __('An unknown error occurred.', 'mailpoet'), |
| 34 | ]; |
| 35 | } |
| 36 | return new ErrorResponse($errors, $meta, $status); |
| 37 | } |
| 38 | |
| 39 | public function badRequest($errors = [], $meta = []) { |
| 40 | if (empty($errors)) { |
| 41 | $errors = [ |
| 42 | Error::BAD_REQUEST => __('Invalid request parameters', 'mailpoet'), |
| 43 | ]; |
| 44 | } |
| 45 | return new ErrorResponse($errors, $meta, Response::STATUS_BAD_REQUEST); |
| 46 | } |
| 47 | |
| 48 | public function redirectResponse($url) { |
| 49 | return new RedirectResponse($url); |
| 50 | } |
| 51 | |
| 52 | public function isMethodAllowed($name, $type) { |
| 53 | // Block GET requests on POST endpoints, but allow POST requests on GET endpoints (some plugins |
| 54 | // change REQUEST_METHOD to POST on GET requests, which caused them to be blocked) |
| 55 | if ($type === self::TYPE_GET && !in_array($name, static::$getMethods)) { |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 |