- Refactors API
- Updates existing classes to use the refactored API methods
This commit is contained in:
71
lib/API/API.php
Normal file
71
lib/API/API.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace MailPoet\API;
|
||||
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class API {
|
||||
public $api_request;
|
||||
public $endpoint;
|
||||
public $action;
|
||||
public $data;
|
||||
const API_NAME = 'mailpoet_api';
|
||||
const ENDPOINT_NAMESCAPE = '\MailPoet\API\Endpoints\\';
|
||||
|
||||
function __construct() {
|
||||
$this->api_request = isset($_GET[self::API_NAME]) ? true : false;
|
||||
$this->endpoint = isset($_GET['endpoint']) ?
|
||||
Helpers::underscoreToCamelCase($_GET['endpoint']) :
|
||||
false;
|
||||
$this->endpoint = self::ENDPOINT_NAMESCAPE . ucfirst($this->endpoint);
|
||||
$this->action = isset($_GET['action']) ?
|
||||
Helpers::underscoreToCamelCase($_GET['action']) :
|
||||
false;
|
||||
$this->data = $this->validateRequestData();
|
||||
}
|
||||
|
||||
function init() {
|
||||
if(!$this->api_request) return;
|
||||
if(!$this->endpoint) {
|
||||
$this->terminateRequest(404, __('Invalid API endpoint.'));
|
||||
}
|
||||
$this->callEndpoint($this->endpoint, $this->action, $this->data);
|
||||
}
|
||||
|
||||
function callEndpoint($endpoint, $action, $data) {
|
||||
if(!method_exists($endpoint, $action)) {
|
||||
$this->terminateRequest(404, __('Invalid API action.'));
|
||||
}
|
||||
call_user_func(
|
||||
array(
|
||||
$endpoint,
|
||||
$action
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
function validateRequestData() {
|
||||
if(!isset($_GET['data'])) return false;
|
||||
$data = base64_decode($_GET['data']);
|
||||
return (is_serialized($data)) ?
|
||||
unserialize($data) :
|
||||
$this->terminateRequest(404, __('Invalid API data format.'));
|
||||
}
|
||||
|
||||
static function buildRequest($endpoint, $action, $data) {
|
||||
$params = array(
|
||||
self::API_NAME => '',
|
||||
'endpoint' => $endpoint,
|
||||
'action' => $action,
|
||||
'data' => $data
|
||||
);
|
||||
return add_query_arg($params, home_url());
|
||||
}
|
||||
|
||||
function terminateRequest($code, $message) {
|
||||
status_header($code, $message);
|
||||
exit;
|
||||
}
|
||||
}
|
16
lib/API/Endpoints/Queue.php
Normal file
16
lib/API/Endpoints/Queue.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Cron\Daemon;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Queue {
|
||||
const ENDPOINT = 'queue';
|
||||
const ACTION_RUN = 'run';
|
||||
|
||||
static function run($data) {
|
||||
$queue = new Daemon($data);
|
||||
$queue->run();
|
||||
}
|
||||
}
|
22
lib/API/Endpoints/Subscription.php
Normal file
22
lib/API/Endpoints/Subscription.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Subscription as UserSubscription;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Subscription {
|
||||
const ENDPOINT = 'subscription';
|
||||
|
||||
static function confirm($data) {
|
||||
$subscription = new UserSubscription\Pages('confirm', $data);
|
||||
}
|
||||
|
||||
static function manage($data) {
|
||||
$subscription = new UserSubscription\Pages('manage', $data);
|
||||
}
|
||||
|
||||
static function unsubscribe($data) {
|
||||
$subscription = new UserSubscription\Pages('unsubscribe', $data);
|
||||
}
|
||||
}
|
23
lib/API/Endpoints/Track.php
Normal file
23
lib/API/Endpoints/Track.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Statistics\Track\Clicks;
|
||||
use MailPoet\Statistics\Track\Opens;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Track {
|
||||
const ENDPOINT = 'track';
|
||||
const ACTION_CLICK = 'click';
|
||||
const ACTION_OPEN = 'open';
|
||||
|
||||
static function click($data) {
|
||||
$clicks = new Clicks($data);
|
||||
$clicks->track();
|
||||
}
|
||||
|
||||
static function open($data) {
|
||||
$opens = new Opens($data);
|
||||
$opens->track();
|
||||
}
|
||||
}
|
16
lib/API/Endpoints/ViewInBrowser.php
Normal file
16
lib/API/Endpoints/ViewInBrowser.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
|
||||
use MailPoet\Newsletter\ViewInBrowser as NewsletterViewInBrowser;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class ViewInBrowser {
|
||||
const ENDPOINT = 'view_in_browser';
|
||||
const ACTION_VIEW = 'view';
|
||||
|
||||
static function view($data) {
|
||||
$viewer = new NewsletterViewInBrowser($data);
|
||||
$viewer->view();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user