Add SendingTaskSubscribers endpoint
This commit is contained in:
committed by
M. Shull
parent
feba9ce146
commit
292e7a50be
72
lib/API/JSON/v1/SendingTaskSubscribers.php
Normal file
72
lib/API/JSON/v1/SendingTaskSubscribers.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\Listing;
|
||||
use MailPoet\Cron\CronHelper;
|
||||
use MailPoet\Config\AccessControl;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoet\Tasks\Sending as SendingTask;
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class SendingTaskSubscribers extends APIEndpoint {
|
||||
public $permissions = array(
|
||||
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
|
||||
);
|
||||
|
||||
/** @var Listing\Handler */
|
||||
private $listing_handler;
|
||||
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(
|
||||
Listing\Handler $listing_handler,
|
||||
SettingsController $settings,
|
||||
WPFunctions $wp
|
||||
) {
|
||||
$this->listing_handler = $listing_handler;
|
||||
$this->settings = $settings;
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
public function listing($data = array()) {
|
||||
$newsletter_id = !empty($data['params']['id']) ? (int)$data['params']['id'] : false;
|
||||
$tasks_ids = SendingQueue::select('task_id')
|
||||
->where('newsletter_id', $newsletter_id)
|
||||
->findArray();
|
||||
if (empty($tasks_ids)) {
|
||||
return $this->errorResponse(array(
|
||||
APIError::NOT_FOUND => __('This newsletter is not being sent to any subcriber yet.', 'mailpoet')
|
||||
));
|
||||
}
|
||||
$data['params']['task_ids'] = array_map(function($item) {
|
||||
return $item['task_id'];
|
||||
}, $tasks_ids);
|
||||
$listing_data = $this->listing_handler->get('\MailPoet\Models\ScheduledTaskSubscriber', $data);
|
||||
|
||||
$items = [];
|
||||
foreach ($listing_data['items'] as $item) {
|
||||
$items[] = $item->asArray();
|
||||
}
|
||||
|
||||
return $this->successResponse($items, array(
|
||||
'count' => $listing_data['count'],
|
||||
'filters' => $listing_data['filters'],
|
||||
'groups' => $listing_data['groups'],
|
||||
'mta_log' => $this->settings->get('mta_log'),
|
||||
'mta_method' => $this->settings->get('mta.method'),
|
||||
'cron_accessible' => CronHelper::isDaemonAccessible(),
|
||||
'current_time' => $this->wp->currentTime('mysql')
|
||||
));
|
||||
}
|
||||
}
|
@ -51,6 +51,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Services::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Settings::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\UserFlags::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\SendingTaskSubscribers::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Setup::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Subscribers::class)->setPublic(true);
|
||||
// Config
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class ScheduledTaskSubscriber extends Model {
|
||||
@ -10,6 +12,10 @@ class ScheduledTaskSubscriber extends Model {
|
||||
const FAIL_STATUS_OK = 0;
|
||||
const FAIL_STATUS_FAILED = 1;
|
||||
|
||||
const SENDING_STATUS_SENT = 'sent';
|
||||
const SENDING_STATUS_FAILED = 'failed';
|
||||
const SENDING_STATUS_UNPROCESSED = 'unprocessed';
|
||||
|
||||
public static $_table = MP_SCHEDULED_TASK_SUBSCRIBERS_TABLE;
|
||||
public static $_id_column = ['task_id', 'subscriber_id'];
|
||||
|
||||
@ -62,6 +68,67 @@ class ScheduledTaskSubscriber extends Model {
|
||||
return self::getCount($task_id);
|
||||
}
|
||||
|
||||
static function listingQuery($data) {
|
||||
$group = isset($data['group']) ? $data['group'] : 'all';
|
||||
return self::join(Subscriber::$_table, array("subscriber_id", "=", "subscribers.id"), "subscribers")
|
||||
->filter($group, $data['params'])
|
||||
->select('error', 'error')
|
||||
->select('failed', 'failed')
|
||||
->select('task_id', 'taskId')
|
||||
->select('processed', 'processed')
|
||||
->select('subscribers.email', 'email')
|
||||
->select('subscribers.id', 'subscriberId')
|
||||
->select('subscribers.last_name', 'lastName')
|
||||
->select('subscribers.first_name', 'firstName');
|
||||
}
|
||||
|
||||
static function groups($data) {
|
||||
$params = $data['params'];
|
||||
return [
|
||||
[
|
||||
'name' => 'all',
|
||||
'label' => WPFunctions::get()->__('All', 'mailpoet'),
|
||||
'count' => self::filter('all', $params)->count(),
|
||||
],
|
||||
[
|
||||
'name' => self::SENDING_STATUS_SENT,
|
||||
'label' => WPFunctions::get()->x('Sent', 'status when a newsletter has been sent', 'mailpoet'),
|
||||
'count' => self::filter(self::SENDING_STATUS_SENT, $params)->count(),
|
||||
],
|
||||
[
|
||||
'name' => self::SENDING_STATUS_FAILED,
|
||||
'label' => WPFunctions::get()->x('Failed', 'status when the sending of a newsletter has failed', 'mailpoet'),
|
||||
'count' => self::filter(self::SENDING_STATUS_FAILED, $params)->count(),
|
||||
],
|
||||
[
|
||||
'name' => self::SENDING_STATUS_UNPROCESSED,
|
||||
'label' => WPFunctions::get()->x('Unprocessed', 'status when the sending of a newsletter has not been processed', 'mailpoet'),
|
||||
'count' => self::filter(self::SENDING_STATUS_UNPROCESSED, $params)->count(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
static function all($orm, $params) {
|
||||
return $orm->whereIn('task_id', $params['task_ids']);
|
||||
}
|
||||
|
||||
static function sent($orm, $params) {
|
||||
return $orm->filter('all', $params)
|
||||
->where('processed', self::STATUS_PROCESSED)
|
||||
->where('failed', self::FAIL_STATUS_OK);
|
||||
}
|
||||
|
||||
static function failed($orm, $params) {
|
||||
return $orm->filter('all', $params)
|
||||
->where('processed', self::STATUS_PROCESSED)
|
||||
->where('failed', self::FAIL_STATUS_FAILED);
|
||||
}
|
||||
|
||||
static function unprocessed($orm, $params) {
|
||||
return $orm->filter('all', $params)
|
||||
->where('processed', self::STATUS_UNPROCESSED);
|
||||
}
|
||||
|
||||
private static function getCount($task_id, $processed = null) {
|
||||
$orm = self::where('task_id', $task_id);
|
||||
if (!is_null($processed)) {
|
||||
|
Reference in New Issue
Block a user