AbstractAutomationEmbed.php
5 months ago
Automation.php
2 years ago
AutomationAnalytics.php
2 months ago
AutomationEditor.php
2 months ago
AutomationFlowEmbed.php
5 months ago
AutomationPreviewEmbed.php
2 months ago
AutomationTemplates.php
2 years ago
CustomFields.php
2 months ago
DynamicSegments.php
2 months ago
ExperimentalFeatures.php
3 years ago
FormEditor.php
1 week ago
Forms.php
2 months ago
Help.php
2 months ago
Homepage.php
2 years ago
Landingpage.php
2 years ago
Logs.php
1 month ago
NewsletterEditor.php
2 months ago
Newsletters.php
2 months ago
Settings.php
5 months ago
StaticSegments.php
2 months ago
Subscribers.php
1 week ago
SubscribersExport.php
3 years ago
SubscribersImport.php
2 months ago
Tags.php
2 months ago
Upgrade.php
1 year ago
WelcomeWizard.php
2 months ago
WooCommerceSetup.php
2 months ago
index.php
3 years ago
Help.php
172 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\AdminPages\Pages; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\AdminPages\PageRenderer; |
| 9 | use MailPoet\Cron\ActionScheduler\Actions\DaemonRun; |
| 10 | use MailPoet\Cron\ActionScheduler\Actions\DaemonTrigger; |
| 11 | use MailPoet\Cron\CronHelper; |
| 12 | use MailPoet\Cron\Workers\SendingQueue\SendingQueue; |
| 13 | use MailPoet\Entities\ScheduledTaskEntity; |
| 14 | use MailPoet\Mailer\MailerLog; |
| 15 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 16 | use MailPoet\Newsletter\Sending\SendingQueuesRepository; |
| 17 | use MailPoet\Newsletter\Url as NewsletterURL; |
| 18 | use MailPoet\Router\Endpoints\CronDaemon; |
| 19 | use MailPoet\Services\Bridge; |
| 20 | use MailPoet\SystemReport\SystemReportCollector; |
| 21 | use MailPoet\WP\DateTime; |
| 22 | use MailPoet\WP\Functions as WPFunctions; |
| 23 | |
| 24 | class Help { |
| 25 | private PageRenderer $pageRenderer; |
| 26 | private CronHelper $cronHelper; |
| 27 | private SystemReportCollector $systemReportCollector; |
| 28 | private Bridge $bridge; |
| 29 | private ScheduledTasksRepository $scheduledTasksRepository; |
| 30 | private SendingQueuesRepository $sendingQueuesRepository; |
| 31 | private NewsletterURL $newsletterUrl; |
| 32 | |
| 33 | public function __construct( |
| 34 | PageRenderer $pageRenderer, |
| 35 | CronHelper $cronHelper, |
| 36 | SystemReportCollector $systemReportCollector, |
| 37 | Bridge $bridge, |
| 38 | ScheduledTasksRepository $scheduledTasksRepository, |
| 39 | SendingQueuesRepository $sendingQueuesRepository, |
| 40 | NewsletterURL $newsletterUrl |
| 41 | ) { |
| 42 | $this->pageRenderer = $pageRenderer; |
| 43 | $this->cronHelper = $cronHelper; |
| 44 | $this->systemReportCollector = $systemReportCollector; |
| 45 | $this->bridge = $bridge; |
| 46 | $this->scheduledTasksRepository = $scheduledTasksRepository; |
| 47 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 48 | $this->newsletterUrl = $newsletterUrl; |
| 49 | } |
| 50 | |
| 51 | public function render() { |
| 52 | /** |
| 53 | * Filter the system info. |
| 54 | * |
| 55 | * @param array<string, string> $systemInfoData The system info data array. |
| 56 | */ |
| 57 | $systemInfoData = WPFunctions::get()->applyFilters('mailpoet_system_info_data', $this->systemReportCollector->getData(true)); |
| 58 | |
| 59 | try { |
| 60 | $cronPingUrl = $this->cronHelper->getCronUrl(CronDaemon::ACTION_PING); |
| 61 | $cronPingResponse = $this->systemReportCollector->getCronPingResponse(); |
| 62 | } catch (\Exception $e) { |
| 63 | $cronPingResponse = __('Can‘t generate cron URL.', 'mailpoet') . ' (' . $e->getMessage() . ')'; |
| 64 | $cronPingUrl = $cronPingResponse; |
| 65 | } |
| 66 | |
| 67 | $mailerLog = MailerLog::getMailerLog(); |
| 68 | $mailerLog['sent'] = MailerLog::sentSince(); |
| 69 | $bridgePingResponse = $this->systemReportCollector->getBridgePingResponse(); |
| 70 | $systemStatusData = [ |
| 71 | 'cron' => [ |
| 72 | 'url' => $cronPingUrl, |
| 73 | 'isReachable' => $this->cronHelper->validatePingResponse($cronPingResponse), |
| 74 | 'pingResponse' => $cronPingResponse, |
| 75 | ], |
| 76 | 'mss' => [ |
| 77 | 'enabled' => $this->bridge->isMailpoetSendingServiceEnabled(), |
| 78 | 'isReachable' => $this->bridge->validateBridgePingResponse($bridgePingResponse), |
| 79 | ], |
| 80 | 'cronStatus' => $this->cronHelper->getDaemon(), |
| 81 | 'queueStatus' => $mailerLog, |
| 82 | 'activePlugins' => $this->systemReportCollector->getActivePluginsData(), |
| 83 | ]; |
| 84 | |
| 85 | $systemStatusData['cronStatus']['accessible'] = $this->cronHelper->isDaemonAccessible(); |
| 86 | $systemStatusData['queueStatus']['tasksStatusCounts'] = $this->scheduledTasksRepository->getCountsPerStatus(); |
| 87 | |
| 88 | $scheduledTasks = $this->scheduledTasksRepository->getLatestTasks(SendingQueue::TASK_TYPE); |
| 89 | $systemStatusData['queueStatus']['latestTasks'] = array_map(fn($task) => $this->buildTaskData($task), $scheduledTasks); |
| 90 | |
| 91 | $this->pageRenderer->displayPage( |
| 92 | 'help.html', |
| 93 | [ |
| 94 | 'systemInfoData' => $systemInfoData, |
| 95 | 'systemStatusData' => $systemStatusData, |
| 96 | 'actionSchedulerData' => $this->getActionSchedulerData(), |
| 97 | ] |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | private function getActionSchedulerData(): ?array { |
| 102 | if (!class_exists(\ActionScheduler_Store::class)) { |
| 103 | return null; |
| 104 | } |
| 105 | $actionSchedulerData = []; |
| 106 | $actionSchedulerData['version'] = \ActionScheduler_Versions::instance()->latest_version(); |
| 107 | $actionSchedulerData['storage'] = str_replace('ActionScheduler_', '', get_class(\ActionScheduler_Store::instance())); |
| 108 | $actionSchedulerData['latestTrigger'] = $this->getLatestActionSchedulerActionDate(DaemonTrigger::NAME); |
| 109 | $actionSchedulerData['latestCompletedTrigger'] = $this->getLatestActionSchedulerActionDate(DaemonTrigger::NAME, 'complete'); |
| 110 | $actionSchedulerData['latestCompletedRun'] = $this->getLatestActionSchedulerActionDate(DaemonRun::NAME, 'complete'); |
| 111 | return $actionSchedulerData; |
| 112 | } |
| 113 | |
| 114 | private function getLatestActionSchedulerActionDate(string $hook, ?string $status = null): ?string { |
| 115 | $query = [ |
| 116 | 'per_page' => 1, |
| 117 | 'order' => 'DESC', |
| 118 | 'hook' => $hook, |
| 119 | ]; |
| 120 | if ($status) { |
| 121 | $query['status'] = $status; |
| 122 | } |
| 123 | $store = \ActionScheduler_Store::instance(); |
| 124 | $action = $store->query_actions($query); |
| 125 | if (!empty($action)) { |
| 126 | $dateObject = $store->get_date($action[0]); |
| 127 | return $dateObject->format('Y-m-d H:i:s'); |
| 128 | } |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | public function buildTaskData(ScheduledTaskEntity $task): array { |
| 133 | $queue = $newsletter = $subscriber = null; |
| 134 | if ($task->getType() === SendingQueue::TASK_TYPE) { |
| 135 | $queue = $this->sendingQueuesRepository->findOneBy(['task' => $task]); |
| 136 | $newsletter = $queue ? $queue->getNewsletter() : null; |
| 137 | $subscribers = $task->getSubscribers(); |
| 138 | // We only show subscriber's email for 1:1 emails (e.g. automations) and not bulk campaigns |
| 139 | if ($subscribers->count() === 1) { |
| 140 | $subscriber = $subscribers->first() ? $subscribers->first()->getSubscriber() : null; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return [ |
| 145 | 'id' => $task->getId(), |
| 146 | 'type' => $task->getType(), |
| 147 | 'priority' => $task->getPriority(), |
| 148 | 'updatedAt' => $task->getUpdatedAt() ? |
| 149 | $task->getUpdatedAt()->format(DateTime::DEFAULT_DATE_TIME_FORMAT) : null, |
| 150 | 'scheduledAt' => $task->getScheduledAt() ? |
| 151 | $task->getScheduledAt()->format(DateTime::DEFAULT_DATE_TIME_FORMAT) |
| 152 | : null, |
| 153 | 'cancelledAt' => $task->getCancelledAt() ? |
| 154 | $task->getCancelledAt()->format(DateTime::DEFAULT_DATE_TIME_FORMAT) |
| 155 | : null, |
| 156 | 'status' => $task->getStatus(), |
| 157 | 'newsletter' => $queue && $newsletter ? [ |
| 158 | 'newsletterId' => $newsletter->getId(), |
| 159 | 'queueId' => $queue->getId(), |
| 160 | 'subject' => $queue->getNewsletterRenderedSubject() ?: $newsletter->getSubject(), |
| 161 | 'previewUrl' => $this->newsletterUrl->getViewInBrowserUrl($newsletter, null, $queue), |
| 162 | ] : [ |
| 163 | 'newsletterId' => null, |
| 164 | 'queueId' => null, |
| 165 | 'subject' => null, |
| 166 | 'previewUrl' => null, |
| 167 | ], |
| 168 | 'subscriberEmail' => $subscriber ? $subscriber->getEmail() : null, |
| 169 | ]; |
| 170 | } |
| 171 | } |
| 172 |