Add endpoint for subscriber statistics
[MAILPOET-3069]
This commit is contained in:
49
lib/API/JSON/v1/SubscriberStats.php
Normal file
49
lib/API/JSON/v1/SubscriberStats.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?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\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository;
|
||||||
|
use MailPoet\Subscribers\SubscribersRepository;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
|
class SubscriberStats extends APIEndpoint {
|
||||||
|
public $permissions = [
|
||||||
|
'global' => AccessControl::PERMISSION_MANAGE_SUBSCRIBERS,
|
||||||
|
];
|
||||||
|
|
||||||
|
/** @var SubscribersRepository */
|
||||||
|
private $subscribersRepository;
|
||||||
|
|
||||||
|
/** @var SubscriberStatisticsRepository */
|
||||||
|
private $subscribersStatisticsRepository;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
SubscribersRepository $subscribersRepository,
|
||||||
|
SubscriberStatisticsRepository $subscribersStatisticsRepository
|
||||||
|
) {
|
||||||
|
$this->subscribersRepository = $subscribersRepository;
|
||||||
|
$this->subscribersStatisticsRepository = $subscribersStatisticsRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($data) {
|
||||||
|
$subscriber = isset($data['subscriber_id'])
|
||||||
|
? $this->subscribersRepository->findOneById((int)$data['subscriber_id'])
|
||||||
|
: null;
|
||||||
|
if (!$subscriber instanceof SubscriberEntity) {
|
||||||
|
return $this->errorResponse([
|
||||||
|
APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
$statistics = $this->subscribersStatisticsRepository->getStatistics($subscriber);
|
||||||
|
return $this->successResponse([
|
||||||
|
'email' => $subscriber->getEmail(),
|
||||||
|
'total_sent' => $statistics->getTotalSentCount(),
|
||||||
|
'open' => $statistics->getOpenCount(),
|
||||||
|
'click' => $statistics->getClickCount(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -81,6 +81,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\API\JSON\v1\UserFlags::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\SendingTaskSubscribers::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\API\JSON\v1\Setup::class)->setPublic(true);
|
$container->autowire(\MailPoet\API\JSON\v1\Setup::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\API\JSON\v1\SubscriberStats::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\API\JSON\v1\Subscribers::class)->setPublic(true);
|
$container->autowire(\MailPoet\API\JSON\v1\Subscribers::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\API\JSON\v1\WoocommerceSettings::class)->setPublic(true);
|
$container->autowire(\MailPoet\API\JSON\v1\WoocommerceSettings::class)->setPublic(true);
|
||||||
// API response builders
|
// API response builders
|
||||||
@ -241,6 +242,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Subscribers\SubscriberListingRepository::class);
|
$container->autowire(\MailPoet\Subscribers\SubscriberListingRepository::class);
|
||||||
$container->autowire(\MailPoet\Subscribers\SubscriberSegmentRepository::class);
|
$container->autowire(\MailPoet\Subscribers\SubscriberSegmentRepository::class);
|
||||||
$container->autowire(\MailPoet\Subscribers\SubscriberCustomFieldRepository::class);
|
$container->autowire(\MailPoet\Subscribers\SubscriberCustomFieldRepository::class);
|
||||||
|
$container->autowire(\MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository::class);
|
||||||
// Segments
|
// Segments
|
||||||
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
|
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
|
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
|
||||||
|
42
lib/Subscribers/Statistics/SubscriberStatistics.php
Normal file
42
lib/Subscribers/Statistics/SubscriberStatistics.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscribers\Statistics;
|
||||||
|
|
||||||
|
class SubscriberStatistics {
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $clickCount;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $openCount;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $totalSentCount;
|
||||||
|
|
||||||
|
public function __construct($clickCount, $openCount, $totalSentCount) {
|
||||||
|
$this->clickCount = $clickCount;
|
||||||
|
$this->openCount = $openCount;
|
||||||
|
$this->totalSentCount = $totalSentCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getClickCount(): int {
|
||||||
|
return $this->clickCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getOpenCount(): int {
|
||||||
|
return $this->openCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getTotalSentCount(): int {
|
||||||
|
return $this->totalSentCount;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscribers\Statistics;
|
||||||
|
|
||||||
|
use MailPoet\Doctrine\Repository;
|
||||||
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
|
use MailPoet\Entities\StatisticsNewsletterEntity;
|
||||||
|
use MailPoet\Entities\StatisticsOpenEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Repository<SubscriberEntity>
|
||||||
|
*/
|
||||||
|
class SubscriberStatisticsRepository extends Repository {
|
||||||
|
protected function getEntityClassName() {
|
||||||
|
return SubscriberEntity::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getStatistics(SubscriberEntity $subscriber) {
|
||||||
|
return new SubscriberStatistics(
|
||||||
|
$this->getStatisticsClickCount($subscriber),
|
||||||
|
$this->getStatisticsOpenCount($subscriber),
|
||||||
|
$this->getTotalSentCount($subscriber)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStatisticsClickCount(SubscriberEntity $subscriber): int {
|
||||||
|
return $this->getStatisticsCount(StatisticsClickEntity::class, $subscriber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStatisticsOpenCount(SubscriberEntity $subscriber): int {
|
||||||
|
return $this->getStatisticsCount(StatisticsOpenEntity::class, $subscriber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTotalSentCount(SubscriberEntity $subscriber): int {
|
||||||
|
return $this->getStatisticsCount(StatisticsNewsletterEntity::class, $subscriber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStatisticsCount($entityName, SubscriberEntity $subscriber): int {
|
||||||
|
return (int)$this->entityManager->createQueryBuilder()
|
||||||
|
->select('COUNT(DISTINCT stats.newsletter) as cnt')
|
||||||
|
->from($entityName, 'stats')
|
||||||
|
->where('stats.subscriber = :subscriber')
|
||||||
|
->setParameter('subscriber', $subscriber)
|
||||||
|
->getQuery()
|
||||||
|
->getSingleScalarResult();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user