PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 5.38.0
MailPoet – Newsletters, Email Marketing, and Automation v5.38.0
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / API / JSON / Endpoint.php
Endpoint.php
61 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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