Files
piratepoet/lib/API/JSON/v1/AutomaticEmails.php
Amine Ben hammou 43df66d162 Add public keyword to methods
[MAILPOET-2413]
2019-12-26 18:09:45 +03:00

81 lines
2.7 KiB
PHP

<?php
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\Config\AccessControl;
use MailPoet\WP\Functions as WPFunctions;
class AutomaticEmails extends APIEndpoint {
public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS,
];
private $wp;
public function __construct() {
$this->wp = new WPFunctions;
}
public function getEventOptions($data) {
$query = (!empty($data['query'])) ? $data['query'] : null;
$filter = (!empty($data['filter'])) ? $data['filter'] : null;
$email_slug = (!empty($data['email_slug'])) ? $data['email_slug'] : null;
$event_slug = (!empty($data['event_slug'])) ? $data['event_slug'] : null;
if (!$query || !$filter || !$email_slug || !$event_slug) {
return $this->errorResponse(
[
APIError::BAD_REQUEST => WPFunctions::get()->__('Improperly formatted request.', 'mailpoet'),
]
);
}
$automatic_emails = new \MailPoet\AutomaticEmails\AutomaticEmails();
$event = $automatic_emails->getAutomaticEmailEventBySlug($email_slug, $event_slug);
$event_filter = (!empty($event['options']['remoteQueryFilter'])) ? $event['options']['remoteQueryFilter'] : null;
return ($event_filter === $filter && WPFunctions::get()->hasFilter($event_filter)) ?
$this->successResponse($this->wp->applyFilters($event_filter, $query)) :
$this->errorResponse(
[
APIError::BAD_REQUEST => WPFunctions::get()->__('Automatic email event filter does not exist.', 'mailpoet'),
]
);
}
public function getEventShortcodes($data) {
$email_slug = (!empty($data['email_slug'])) ? $data['email_slug'] : null;
$event_slug = (!empty($data['event_slug'])) ? $data['event_slug'] : null;
if (!$email_slug || !$event_slug) {
return $this->errorResponse(
[
APIError::BAD_REQUEST => WPFunctions::get()->__('Improperly formatted request.', 'mailpoet'),
]
);
}
$automatic_emails = new \MailPoet\AutomaticEmails\AutomaticEmails();
$automatic_email = $automatic_emails->getAutomaticEmailBySlug($email_slug);
$event = $automatic_emails->getAutomaticEmailEventBySlug($email_slug, $event_slug);
if (!$event) {
return $this->errorResponse(
[
APIError::BAD_REQUEST => WPFunctions::get()->__('Automatic email event does not exist.', 'mailpoet'),
]
);
}
$event_shortcodes = (!empty($event['shortcodes']) && is_array($event['shortcodes'])) ?
[
$automatic_email['title'] => $event['shortcodes'],
] :
null;
return $this->successResponse($event_shortcodes);
}
}