- 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();
|
||||
}
|
||||
}
|
@ -49,7 +49,7 @@ class Initializer {
|
||||
|
||||
function onInit() {
|
||||
$this->setupRouter();
|
||||
$this->setupPublicAPI();
|
||||
$this->setupAPI();
|
||||
$this->setupPages();
|
||||
}
|
||||
|
||||
@ -171,13 +171,9 @@ class Initializer {
|
||||
$hooks->init();
|
||||
}
|
||||
|
||||
function setupPublicAPI() {
|
||||
try {
|
||||
$publicAPI = new PublicAPI();
|
||||
$publicAPI->init();
|
||||
} catch(\Exception $e) {
|
||||
// continue execution
|
||||
}
|
||||
function setupAPI() {
|
||||
$API = new \MailPoet\API\API();
|
||||
$API->init();
|
||||
}
|
||||
|
||||
function runQueueSupervisor() {
|
||||
|
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Cron\Daemon;
|
||||
use MailPoet\Newsletter\ViewInBrowser;
|
||||
use MailPoet\Statistics\Track\Clicks;
|
||||
use MailPoet\Statistics\Track\Opens;
|
||||
use MailPoet\Subscription;
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class PublicAPI {
|
||||
public $api;
|
||||
public $endpoint;
|
||||
public $action;
|
||||
public $data;
|
||||
|
||||
function __construct() {
|
||||
# http://example.com/?mailpoet&endpoint=&action=&data=
|
||||
$this->api = isset($_GET['mailpoet']) ? true : false;
|
||||
$this->endpoint = isset($_GET['endpoint']) ?
|
||||
Helpers::underscoreToCamelCase($_GET['endpoint']) :
|
||||
false;
|
||||
$this->action = isset($_GET['action']) ?
|
||||
Helpers::underscoreToCamelCase($_GET['action']) :
|
||||
false;
|
||||
$this->data = $this->getAndValidateData();
|
||||
}
|
||||
|
||||
function init() {
|
||||
if(!$this->api || !$this->endpoint) return;
|
||||
$this->_checkAndCallMethod($this, $this->endpoint, $terminate_request = true);
|
||||
}
|
||||
|
||||
function queue() {
|
||||
$queue = new Daemon($this->data);
|
||||
$this->_checkAndCallMethod($queue, $this->action);
|
||||
}
|
||||
|
||||
function subscription() {
|
||||
$subscription = new Subscription\Pages($this->action, $this->data);
|
||||
$this->_checkAndCallMethod($subscription, $this->action);
|
||||
}
|
||||
|
||||
function track() {
|
||||
if($this->action === 'click') {
|
||||
$track_class = new Clicks($this->data);
|
||||
}
|
||||
if($this->action === 'open') {
|
||||
$track_class = new Opens($this->data);
|
||||
}
|
||||
if(!isset($track_class)) return;
|
||||
$track_class->track();
|
||||
}
|
||||
|
||||
function viewInBrowser() {
|
||||
$viewer = new ViewInBrowser($this->data);
|
||||
$viewer->view();
|
||||
}
|
||||
|
||||
private function _checkAndCallMethod($class, $method, $terminate_request = false) {
|
||||
if(!method_exists($class, $method)) {
|
||||
if(!$terminate_request) return;
|
||||
header('HTTP/1.0 404 Not Found');
|
||||
exit;
|
||||
}
|
||||
call_user_func(
|
||||
array(
|
||||
$class,
|
||||
$method
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function getAndValidateData() {
|
||||
if (!isset($_GET['data'])) return false;
|
||||
$data = base64_decode($_GET['data']);
|
||||
if (!is_serialized($data)) {
|
||||
throw new \Exception(__('Invalid data format.'));
|
||||
}
|
||||
return unserialize($data);
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace MailPoet\Cron;
|
||||
|
||||
use MailPoet\API\API;
|
||||
use MailPoet\API\Endpoints\Queue as QueueAPI;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Util\Security;
|
||||
|
||||
@ -39,8 +41,11 @@ class CronHelper {
|
||||
|
||||
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
||||
$data = serialize(array('token' => $token));
|
||||
$url = '/?mailpoet&endpoint=queue&action=run&data=' .
|
||||
base64_encode($data);
|
||||
$url = API::buildRequest(
|
||||
QueueAPI::ENDPOINT,
|
||||
QueueAPI::ACTION_RUN,
|
||||
base64_encode($data)
|
||||
);
|
||||
$args = array(
|
||||
'timeout' => $timeout,
|
||||
'user-agent' => 'MailPoet (www.mailpoet.com) Cron'
|
||||
|
@ -56,7 +56,7 @@ class Daemon {
|
||||
// its status has changed
|
||||
$daemon = CronHelper::getDaemon();
|
||||
if(!$daemon || $daemon['token'] !== $this->data['token']) {
|
||||
self::terminate();
|
||||
$this->terminateRequest();
|
||||
}
|
||||
$daemon['counter']++;
|
||||
$this->abortIfStopped($daemon);
|
||||
@ -70,12 +70,12 @@ class Daemon {
|
||||
|
||||
function abortIfStopped($daemon) {
|
||||
if($daemon['status'] === self::STATUS_STOPPED) {
|
||||
self::terminate();
|
||||
$this->terminateRequest();
|
||||
}
|
||||
if($daemon['status'] === self::STATUS_STOPPING) {
|
||||
$daemon['status'] = self::STATUS_STOPPED;
|
||||
CronHelper::saveDaemon($daemon);
|
||||
self::terminate();
|
||||
$this->terminateRequest();
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,10 +85,10 @@ class Daemon {
|
||||
|
||||
function callSelf() {
|
||||
CronHelper::accessDaemon($this->token, self::REQUEST_TIMEOUT);
|
||||
self::terminate();
|
||||
$this->terminateRequest();
|
||||
}
|
||||
|
||||
function terminate() {
|
||||
function terminateRequest() {
|
||||
exit;
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Links;
|
||||
|
||||
use MailPoet\API\API;
|
||||
use MailPoet\API\Endpoints\Track as TrackAPI;
|
||||
use MailPoet\Models\NewsletterLink;
|
||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\Util\Security;
|
||||
|
||||
class Links {
|
||||
@ -61,13 +62,11 @@ class Links {
|
||||
'hash' => $hash,
|
||||
'url' => $extracted_link['link']
|
||||
);
|
||||
$params = array(
|
||||
'mailpoet' => '',
|
||||
'endpoint' => 'track',
|
||||
'action' => 'click',
|
||||
'data' => self::DATA_TAG . '-' . $hash
|
||||
$tracked_link = API::buildRequest(
|
||||
TrackAPI::ENDPOINT,
|
||||
TrackAPI::ACTION_CLICK,
|
||||
self::DATA_TAG . '-' . $hash
|
||||
);
|
||||
$tracked_link = add_query_arg($params, home_url());
|
||||
// first, replace URL in the extracted HTML source with encoded link
|
||||
$tracked_link_html_source = str_replace(
|
||||
$extracted_link['link'], $tracked_link,
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Renderer\PostProcess;
|
||||
|
||||
use MailPoet\API\API;
|
||||
use MailPoet\API\Endpoints\Track as TrackAPI;
|
||||
use MailPoet\Newsletter\Links\Links;
|
||||
use MailPoet\Newsletter\Renderer\Renderer;
|
||||
|
||||
@ -9,10 +11,14 @@ class OpenTracking {
|
||||
$DOM = new \pQuery();
|
||||
$DOM = $DOM->parseStr($template);
|
||||
$template = $DOM->query('body');
|
||||
$url = API::buildRequest(
|
||||
TrackAPI::ENDPOINT,
|
||||
TrackAPI::ACTION_OPEN,
|
||||
Links::DATA_TAG
|
||||
);
|
||||
$open_tracking_image = sprintf(
|
||||
'<img alt="" class="" src="%s/%s"/>',
|
||||
home_url(),
|
||||
esc_attr('?mailpoet&endpoint=track&action=open&data=' . Links::DATA_TAG)
|
||||
'<img alt="" class="" src="%s"/>',
|
||||
$url
|
||||
);
|
||||
$template->html($template->html() . $open_tracking_image);
|
||||
return $DOM->__toString();
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter;
|
||||
|
||||
use MailPoet\API\API;
|
||||
use MailPoet\API\Endpoints\ViewInBrowser;
|
||||
use MailPoet\Models\Subscriber;
|
||||
|
||||
class Url {
|
||||
@ -33,8 +35,9 @@ class Url {
|
||||
$queue
|
||||
);
|
||||
$params = array(
|
||||
'mailpoet' => '',
|
||||
'endpoint' => 'view_in_browser',
|
||||
API::API_NAME,
|
||||
'endpoint' => ViewInBrowser::ENDPOINT,
|
||||
'action' => ViewInBrowser::ACTION_VIEW,
|
||||
'data' => base64_encode(serialize($data))
|
||||
);
|
||||
return add_query_arg($params, home_url());
|
||||
|
@ -70,7 +70,7 @@ class ViewInBrowser {
|
||||
}
|
||||
|
||||
private function abort() {
|
||||
header('HTTP/1.0 404 Not Found');
|
||||
status_header(404);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
@ -89,7 +89,7 @@ class Clicks {
|
||||
}
|
||||
|
||||
function abort() {
|
||||
header('HTTP/1.0 404 Not Found');
|
||||
status_header(404);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ use \MailPoet\Form\Renderer as FormRenderer;
|
||||
use \MailPoet\Form\Block\Date as FormBlockDate;
|
||||
|
||||
class Pages {
|
||||
// TODO: possible refactor
|
||||
const DEMO_EMAIL = 'demo@mailpoet.com';
|
||||
|
||||
private $action;
|
||||
|
@ -1,10 +1,13 @@
|
||||
<?php
|
||||
namespace MailPoet\Subscription;
|
||||
|
||||
use \MailPoet\Models\Subscriber;
|
||||
use \MailPoet\Models\Setting;
|
||||
use MailPoet\API\API;
|
||||
use MailPoet\API\Endpoints\Subscription;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
class Url {
|
||||
// TODO: possible refactor
|
||||
static function getConfirmationUrl($subscriber = false) {
|
||||
$post = get_post(Setting::getValue('subscription.pages.confirmation'));
|
||||
return self::getSubscriptionUrl($post, 'confirm', $subscriber);
|
||||
@ -43,7 +46,8 @@ class Url {
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'endpoint=subscription',
|
||||
API::API_NAME,
|
||||
'endpoint='.Subscription::ENDPOINT,
|
||||
'action='.$action,
|
||||
'data='.rtrim(base64_encode(serialize($data)), '=')
|
||||
);
|
||||
|
Reference in New Issue
Block a user