stics}> */ protected function getNewsletters(): array { $result = []; $newsletters = $this->repository->findActiveByTypes( [NewsletterEntity::TYPE_AUTOMATIC, NewsletterEntity::TYPE_WELCOME, NewsletterEntity::TYPE_AUTOMATION] ); $activeAutomationNewsletterIds = $this->getActiveAutomationNewsletterIds(); foreach ($newsletters as $newsletter) { if ( $newsletter->getType() === NewsletterEntity::TYPE_AUTOMATION && !in_array($newsletter->getId(), $activeAutomationNewsletterIds, true) ) { continue; } $statistics = $this->newsletterStatisticsRepository->getStatistics($newsletter); if ($statistics->getTotalSentCount()) { $result[] = [ 'statistics' => $statistics, 'newsletter' => $newsletter, ]; } } return $result; } /** @return int[] */ private function getActiveAutomationNewsletterIds(): array { $ids = []; $automations = $this->automationStorage->getAutomations([Automation::STATUS_ACTIVE]); foreach ($automations as $automation) { foreach ($automation->getSteps() as $step) { if ($step->getKey() === SendEmailAction::KEY) { $args = $step->getArgs(); if (isset($args['email_id'])) { $ids[] = (int)$args['email_id']; } } } } return $ids; } /** * @param array $newsletters * @return array */ private function prepareContext(array $newsletters): array { $context = [ 'linkSettings' => WPFunctions::get()->applyFilters( 'mailpoet_stats_notification_link_settings', WPFunctions::get()->getSiteUrl(null, '/wp-admin/admin.php?page=mailpoet-settings#basics') ), 'newsletters' => [], ]; foreach ($newsletters as $row) { $statistics = $row['statistics']; $newsletter = $row['newsletter']; $totalSentCount = $statistics->getTotalSentCount() ?: 1; // Opens and clicks over the recipients sent with tracking; unsubscribes and bounces over // everyone. With nobody tracked the rate is 0, not a count divided by 1. $trackedSentCount = $statistics->getTrackedSentCount(); $clicked = $trackedSentCount > 0 ? ($statistics->getClickCount() * 100) / $trackedSentCount : 0; $opened = $trackedSentCount > 0 ? ($statistics->getOpenCount() * 100) / $trackedSentCount : 0; $machineOpened = $trackedSentCount > 0 ? ($statistics->getMachineOpenCount() * 100) / $trackedSentCount : 0; $unsubscribed = ($statistics->getUnsubscribeCount() * 100) / $totalSentCount; $bounced = ($statistics->getBounceCount() * 100) / $totalSentCount; $context['newsletters'][] = [ 'linkStats' => WPFunctions::get()->applyFilters( 'mailpoet_stats_notification_link_stats', WPFunctions::get()->getSiteUrl(null, '/wp-admin/admin.php?page=mailpoet-newsletters#/stats/' . $newsletter->getId()), $newsletter->getId() ), 'clicked' => $clicked, 'opened' => $opened, 'machineOpened' => $machineOpened, 'unsubscribed' => $unsubscribed, 'bounced' => $bounced, 'notTracked' => $statistics->getNotTrackedCount(), 'trackedSent' => $statistics->getTrackedSentCount(), 'subject' => $newsletter->getSubject(), ]; } return $context; } public function getNextRunDate() { $currentDateTime = $this->wpDateTime->getCurrentDateTime(); $date = Carbon::instance($currentDateTime)->millisecond(0); // Get first Monday of next month at midnight $nextMonday = $date->endOfMonth()->next(Carbon::MONDAY)->startOfDay(); // Add random time across the entire day (0-23 hours, 0-59 minutes, 0-59 seconds) return $nextMonday ->addHours(random_int(0, 23)) ->addMinutes(random_int(0, 59)) ->addSeconds(random_int(0, 59)); } }