AutomationEditorLoadingHooks.php
1 month ago
CreateAutomationRunHook.php
1 year ago
index.php
3 years ago
AutomationEditorLoadingHooks.php
141 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Hooks; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\Step; |
| 10 | use MailPoet\Automation\Engine\Hooks; |
| 11 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 12 | use MailPoet\Automation\Engine\WordPress; |
| 13 | use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction; |
| 14 | use MailPoet\Automation\Integrations\MailPoet\Templates\EmailFactory; |
| 15 | use MailPoet\DI\ContainerWrapper; |
| 16 | use MailPoet\EmailEditor\Integrations\MailPoet\BlockEmailContentDetector; |
| 17 | use MailPoet\Newsletter\NewsletterDeleteController; |
| 18 | use MailPoet\Newsletter\NewslettersRepository; |
| 19 | |
| 20 | class AutomationEditorLoadingHooks { |
| 21 | |
| 22 | /** @var WordPress */ |
| 23 | private $wp; |
| 24 | |
| 25 | /** @var AutomationStorage */ |
| 26 | private $automationStorage; |
| 27 | |
| 28 | /** @var NewslettersRepository */ |
| 29 | private $newslettersRepository; |
| 30 | |
| 31 | private NewsletterDeleteController $newsletterDeleteController; |
| 32 | |
| 33 | private BlockEmailContentDetector $blockEmailContentDetector; |
| 34 | |
| 35 | public function __construct( |
| 36 | WordPress $wp, |
| 37 | AutomationStorage $automationStorage, |
| 38 | NewslettersRepository $newslettersRepository, |
| 39 | NewsletterDeleteController $newsletterDeleteController, |
| 40 | BlockEmailContentDetector $blockEmailContentDetector |
| 41 | ) { |
| 42 | $this->wp = $wp; |
| 43 | $this->automationStorage = $automationStorage; |
| 44 | $this->newslettersRepository = $newslettersRepository; |
| 45 | $this->newsletterDeleteController = $newsletterDeleteController; |
| 46 | $this->blockEmailContentDetector = $blockEmailContentDetector; |
| 47 | } |
| 48 | |
| 49 | public function init(): void { |
| 50 | $this->wp->addAction(Hooks::EDITOR_BEFORE_LOAD, [$this, 'beforeEditorLoad']); |
| 51 | } |
| 52 | |
| 53 | public function beforeEditorLoad(int $automationId): void { |
| 54 | $automation = $this->automationStorage->getAutomation($automationId); |
| 55 | if (!$automation) { |
| 56 | return; |
| 57 | } |
| 58 | $this->disconnectEmptyEmailsFromSendEmailStep($automation); |
| 59 | $this->setAutomationIdForEmails($automation); |
| 60 | } |
| 61 | |
| 62 | private function setAutomationIdForEmails(Automation $automation): void { |
| 63 | $emailFactory = ContainerWrapper::getInstance()->get(EmailFactory::class); |
| 64 | $emailFactory->setAutomationIdForEmails($automation); |
| 65 | } |
| 66 | |
| 67 | private function disconnectEmptyEmailsFromSendEmailStep(Automation $automation): void { |
| 68 | $sendEmailSteps = array_filter( |
| 69 | $automation->getSteps(), |
| 70 | function(Step $step): bool { |
| 71 | return $step->getKey() === SendEmailAction::KEY; |
| 72 | } |
| 73 | ); |
| 74 | $automationChanged = false; |
| 75 | $newsletterIdsToDelete = []; |
| 76 | foreach ($sendEmailSteps as $step) { |
| 77 | $args = $step->getArgs(); |
| 78 | $emailId = $args['email_id'] ?? 0; |
| 79 | if (!$emailId) { |
| 80 | continue; |
| 81 | } |
| 82 | $newsletterEntity = $this->newslettersRepository->findOneById($emailId); |
| 83 | $disconnectEmail = !$newsletterEntity; |
| 84 | |
| 85 | if ($newsletterEntity) { |
| 86 | $wpPostId = $newsletterEntity->getWpPostId(); |
| 87 | if ($wpPostId) { |
| 88 | $wpPost = $this->wp->getPost($wpPostId); |
| 89 | $disconnectEmail = !($wpPost instanceof \WP_Post) || !$this->blockEmailContentDetector->hasMeaningfulContent($wpPost); |
| 90 | if (!$disconnectEmail && (int)($args['email_wp_post_id'] ?? 0) !== (int)$wpPostId) { |
| 91 | $args['email_wp_post_id'] = $wpPostId; |
| 92 | } |
| 93 | } else { |
| 94 | $disconnectEmail = $newsletterEntity->getBody() === null; |
| 95 | if (!$disconnectEmail && isset($args['email_wp_post_id'])) { |
| 96 | unset($args['email_wp_post_id']); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if ($disconnectEmail) { |
| 102 | $newsletterIdsToDelete[] = (int)$emailId; |
| 103 | unset($args['email_id']); |
| 104 | unset($args['email_wp_post_id']); |
| 105 | } |
| 106 | |
| 107 | if ($args === $step->getArgs()) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $updatedStep = new Step( |
| 112 | $step->getId(), |
| 113 | $step->getType(), |
| 114 | $step->getKey(), |
| 115 | $args, |
| 116 | $step->getNextSteps(), |
| 117 | $step->getFilters() |
| 118 | ); |
| 119 | |
| 120 | $steps = $automation->getSteps(); |
| 121 | $steps[$updatedStep->getId()] = $updatedStep; |
| 122 | $automation->setSteps($steps); |
| 123 | $automationChanged = true; |
| 124 | |
| 125 | if ($disconnectEmail && $automation->getStatus() === Automation::STATUS_ACTIVE) { |
| 126 | $automation->setStatus(Automation::STATUS_DRAFT); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if ($automationChanged) { |
| 131 | $this->automationStorage->updateAutomation($automation); |
| 132 | } |
| 133 | |
| 134 | if ($newsletterIdsToDelete !== []) { |
| 135 | $this->newsletterDeleteController->bulkDelete( |
| 136 | array_values(array_unique($newsletterIdsToDelete)) |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 |