wp = $wp; $this->workflowStorage = $workflowStorage; $this->newslettersRepository = $newslettersRepository; } public function init(): void { $this->wp->addAction(Hooks::EDITOR_BEFORE_LOAD, [$this, 'beforeEditorLoad']); } public function beforeEditorLoad(int $workflowId): void { $workflow = $this->workflowStorage->getWorkflow($workflowId); if (!$workflow) { return; } $this->disconnectEmptyEmailsFromSendEmailStep($workflow); } private function disconnectEmptyEmailsFromSendEmailStep(Workflow $workflow): void { $sendEmailSteps = array_filter( $workflow->getSteps(), function(Step $step): bool { return $step->getKey() === 'mailpoet:send-email'; } ); foreach ($sendEmailSteps as $step) { $emailId = $step->getArgs()['email_id'] ?? 0; if (!$emailId) { continue; } $newsletterEntity = $this->newslettersRepository->findOneById($emailId); if ($newsletterEntity && $newsletterEntity->getBody() !== null) { continue; } $this->newslettersRepository->bulkDelete([$emailId]); $args = $step->getArgs(); unset($args['email_id']); $updatedStep = new Step( $step->getId(), $step->getType(), $step->getKey(), $args, $step->getNextSteps() ); $steps = array_merge( $workflow->getSteps(), [$updatedStep->getId() => $updatedStep] ); $workflow->setSteps($steps); //To be valid, an email would need to be associated to an active workflow. if ($workflow->getStatus() === Workflow::STATUS_ACTIVE) { $workflow->setStatus(Workflow::STATUS_DRAFT); } $this->workflowStorage->updateWorkflow($workflow); } } }