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 / Response.php

Response.php in MailPoet – Newsletters, Email Marketing, and Automation 5.38.0, at lib/API/JSON/Response.php

57 lines 1.4 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\WP\Functions as WPFunctions;
9
10 abstract class Response {
11 const STATUS_OK = 200;
12 const REDIRECT = 302;
13 const STATUS_BAD_REQUEST = 400;
14 const STATUS_UNAUTHORIZED = 401;
15 const STATUS_FORBIDDEN = 403;
16 const STATUS_NOT_FOUND = 404;
17 const STATUS_CONFLICT = 409;
18 const STATUS_UNKNOWN = 500;
19
20 public $status;
21 public $meta;
22 public $location;
23
24 public function __construct($status, $meta = [], $location = null) { // phpcs:ignore
25 $this->status = $status;
26 $this->meta = $meta;
27 $this->location = $location;
28 }
29
30 public function send() {
31 if ($this->status === self::REDIRECT && $this->location) {
32 header("Location: " . $this->location, true, $this->status);
33 exit;
34 }
35
36 WPFunctions::get()->statusHeader($this->status);
37
38 $data = $this->getData();
39 $response = [];
40
41 if (!empty($this->meta)) {
42 $response['meta'] = $this->meta;
43 }
44 if ($data === null) {
45 $data = [];
46 }
47 $response = array_merge($response, $data);
48
49 $charset = get_option('blog_charset');
50 @header('Content-Type: application/json; charset=' . (is_string($charset) ? $charset : 'UTF-8'));
51 echo wp_json_encode($response);
52 die();
53 }
54
55 public abstract function getData();
56 }
57