diff --git a/mailpoet/lib/Newsletter/NewslettersRepository.php b/mailpoet/lib/Newsletter/NewslettersRepository.php index 3d5b8e78ab..63c3bd458e 100644 --- a/mailpoet/lib/Newsletter/NewslettersRepository.php +++ b/mailpoet/lib/Newsletter/NewslettersRepository.php @@ -526,6 +526,65 @@ class NewslettersRepository extends Repository { ->getResult(); } + /** + * Returns standard newsletters ordered by sentAt + * filter by status STATUS_SCHEDULED, STATUS_SENDING, STATUS_SENT + * @return NewsletterEntity[] + */ + public function getStandardNewsletterListWithMultipleStatuses($limit): array { + $statuses = [ + NewsletterEntity::STATUS_SCHEDULED, + NewsletterEntity::STATUS_SENDING, + NewsletterEntity::STATUS_SENT, + ]; + + $query = $this->entityManager->createQueryBuilder() + ->select('PARTIAL n.{id,subject,sentAt}') + ->addSelect('CASE WHEN n.sentAt IS NULL THEN 1 ELSE 0 END as HIDDEN sent_at_is_null') + ->from(NewsletterEntity::class, 'n') + ->where('n.type = :typeStandard') + ->andWhere('n.status IN (:statuses)') + ->andWhere('n.deletedAt IS NULL') + ->orderBy('sent_at_is_null', 'DESC') + ->addOrderBy('n.sentAt', 'DESC') + ->setParameter('typeStandard', NewsletterEntity::TYPE_STANDARD) + ->setParameter('statuses', $statuses); + + if (is_int($limit)) { + $query->setMaxResults($limit); + } + + $result = $query->getQuery()->getResult(); + + return is_array($result) ? $result : []; + } + + /** + * Returns sent post-notification history newsletters ordered by sentAt + * @return NewsletterEntity[] + */ + public function getNotificationHistoryItems($limit): array { + $query = $this->entityManager->createQueryBuilder() + ->select('PARTIAL n.{id,subject,sentAt}') + ->addSelect('CASE WHEN n.sentAt IS NULL THEN 1 ELSE 0 END as HIDDEN sent_at_is_null') + ->from(NewsletterEntity::class, 'n') + ->where('n.type = :typeNotificationHistory') + ->andWhere('n.status = :status') + ->andWhere('n.deletedAt IS NULL') + ->orderBy('sent_at_is_null', 'DESC') + ->addOrderBy('n.sentAt', 'DESC') + ->setParameter('typeNotificationHistory', NewsletterEntity::TYPE_NOTIFICATION_HISTORY) + ->setParameter('status', NewsletterEntity::STATUS_SENT); + + if (is_int($limit)) { + $query->setMaxResults($limit); + } + + $result = $query->getQuery()->getResult(); + + return is_array($result) ? $result : []; + } + public function prefetchOptions(array $newsletters) { $this->entityManager->createQueryBuilder() ->select('PARTIAL n.{id}, o, opf') diff --git a/mailpoet/lib/WooCommerce/MultichannelMarketing/MPMarketingChannel.php b/mailpoet/lib/WooCommerce/MultichannelMarketing/MPMarketingChannel.php index 134e52f4f6..fd1304aef0 100644 --- a/mailpoet/lib/WooCommerce/MultichannelMarketing/MPMarketingChannel.php +++ b/mailpoet/lib/WooCommerce/MultichannelMarketing/MPMarketingChannel.php @@ -7,7 +7,6 @@ use Automattic\WooCommerce\Admin\Marketing\MarketingCampaignType; use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface; use Automattic\WooCommerce\Admin\Marketing\Price; use MailPoet\Config\Menu; -use MailPoet\Entities\NewsletterEntity; use MailPoet\Newsletter\NewslettersRepository; use MailPoet\Services\AuthorizedEmailsController; use MailPoet\Services\Bridge; @@ -241,8 +240,10 @@ class MPMarketingChannel implements MarketingChannelInterface { } protected function getStandardNewsletterList(): array { - $result = []; - foreach ($this->newsletterRepository->getStandardNewsletterList() as $newsletter) { + $result = []; + + // fetch the most recent newsletters limited to ten + foreach ($this->newsletterRepository->getStandardNewsletterListWithMultipleStatuses(10) as $newsletter) { $newsLetterId = (string)$newsletter->getId(); $result[] = [ 'id' => $newsLetterId, @@ -256,29 +257,30 @@ class MPMarketingChannel implements MarketingChannelInterface { ], ]; } - return $result; + + return $result; } protected function getPostNotificationNewsletters(): array { - $newsletters = $this->newsletterRepository->findActiveByTypes([NewsletterEntity::TYPE_NOTIFICATION]); + $result = []; - $result = []; - - foreach ($newsletters as $newsletter) { - $newsLetterId = (string)$newsletter->getId(); - $result[] = [ - 'id' => $newsLetterId, - 'name' => $newsletter->getSubject(), - 'campaignType' => $this->campaignTypes[self::CAMPAIGN_TYPE_POST_NOTIFICATIONS], - 'url' => admin_url('admin.php?page=' . Menu::EMAILS_PAGE_SLUG . '/#/stats/' . $newsLetterId), - 'price' => [ - // TODO: fetch the correct value - 'amount' => 0, - 'currency' => $this->woocommerceHelper->getWoocommerceCurrency(), - ], - ]; + // fetch the most recently sent post-notification history newsletters limited to ten + foreach ($this->newsletterRepository->getNotificationHistoryItems(10) as $newsletter) { + $newsLetterId = (string)$newsletter->getId(); + $result[] = [ + 'id' => $newsLetterId, + 'name' => $newsletter->getSubject(), + 'campaignType' => $this->campaignTypes[self::CAMPAIGN_TYPE_POST_NOTIFICATIONS], + 'url' => admin_url('admin.php?page=' . Menu::EMAILS_PAGE_SLUG . '/#/stats/' . $newsLetterId), + 'price' => [ + // TODO: fetch the correct value + 'amount' => 0, + 'currency' => $this->woocommerceHelper->getWoocommerceCurrency(), + ], + ]; } - return $result; + + return $result; } protected function getAutomationNewsletters(): array {