Blocks
10 months ago
Coupons
1 month ago
Endpoints
2 months ago
Patterns
1 month ago
PersonalizationTags
3 days ago
ProductCollection
1 month ago
Templates
2 months ago
AutomationEmailContextProvider.php
1 month ago
AutomationEmailPreviewOrderProvider.php
1 month ago
BlockEmailContentDetector.php
1 month ago
Cli.php
2 weeks ago
DependencyNotice.php
10 months ago
EditorPageRenderer.php
1 week ago
EmailApiController.php
1 week ago
EmailEditor.php
2 months ago
EmailEditorPreviewEmail.php
9 months ago
Logger.php
10 months ago
MailPoetCssInliner.php
8 months ago
MailpoetCssInlinerFactory.php
1 year ago
PersonalizationTagManager.php
4 days ago
index.php
2 years ago
AutomationEmailContextProvider.php
122 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 9 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 10 | use MailPoet\Automation\Integrations\MailPoet\Actions\AutomationSendEmailSubjectResolver; |
| 11 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject; |
| 12 | use MailPoet\Entities\NewsletterEntity; |
| 13 | use MailPoet\Entities\NewsletterOptionFieldEntity; |
| 14 | use MailPoet\Entities\SendingQueueEntity; |
| 15 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 16 | use MailPoet\WP\Functions as WPFunctions; |
| 17 | |
| 18 | class AutomationEmailContextProvider { |
| 19 | private AutomationRunStorage $automationRunStorage; |
| 20 | private AutomationStorage $automationStorage; |
| 21 | private AutomationSendEmailSubjectResolver $subjectResolver; |
| 22 | private AutomationEmailPreviewOrderProvider $previewOrderProvider; |
| 23 | private WooCommerceHelper $wooCommerceHelper; |
| 24 | private WPFunctions $wp; |
| 25 | |
| 26 | public function __construct( |
| 27 | AutomationRunStorage $automationRunStorage, |
| 28 | AutomationStorage $automationStorage, |
| 29 | AutomationSendEmailSubjectResolver $subjectResolver, |
| 30 | AutomationEmailPreviewOrderProvider $previewOrderProvider, |
| 31 | WooCommerceHelper $wooCommerceHelper, |
| 32 | WPFunctions $wp |
| 33 | ) { |
| 34 | $this->automationRunStorage = $automationRunStorage; |
| 35 | $this->automationStorage = $automationStorage; |
| 36 | $this->subjectResolver = $subjectResolver; |
| 37 | $this->previewOrderProvider = $previewOrderProvider; |
| 38 | $this->wooCommerceHelper = $wooCommerceHelper; |
| 39 | $this->wp = $wp; |
| 40 | } |
| 41 | |
| 42 | /** @return array<string, mixed> */ |
| 43 | public function build(NewsletterEntity $newsletter, ?SendingQueueEntity $sendingQueue, bool $preview): array { |
| 44 | if ($preview) { |
| 45 | return $this->buildPreviewContext($newsletter); |
| 46 | } |
| 47 | |
| 48 | if (!$sendingQueue) { |
| 49 | return []; |
| 50 | } |
| 51 | |
| 52 | return $this->buildRealSendContext($sendingQueue); |
| 53 | } |
| 54 | |
| 55 | /** @return array<string, mixed> */ |
| 56 | private function buildPreviewContext(NewsletterEntity $newsletter): array { |
| 57 | $automationId = $newsletter->getOptionValue(NewsletterOptionFieldEntity::NAME_AUTOMATION_ID); |
| 58 | if (!is_numeric($automationId)) { |
| 59 | return []; |
| 60 | } |
| 61 | |
| 62 | $automation = $this->automationStorage->getAutomation((int)$automationId); |
| 63 | if (!$automation) { |
| 64 | return []; |
| 65 | } |
| 66 | |
| 67 | $subjectKeys = $this->subjectResolver->getGuaranteedSubjectKeysForEmail($automation, $newsletter); |
| 68 | $context = [ |
| 69 | 'automation_subject_keys' => $subjectKeys, |
| 70 | ]; |
| 71 | |
| 72 | if (in_array(OrderSubject::KEY, $subjectKeys, true)) { |
| 73 | $order = $this->previewOrderProvider->getOrder(); |
| 74 | if ($order instanceof \WC_Order) { |
| 75 | $context['order'] = $order; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $filteredContext = $this->wp->applyFilters('mailpoet_automation_email_preview_sample_data', $context); |
| 80 | return is_array($filteredContext) ? $filteredContext : $context; |
| 81 | } |
| 82 | |
| 83 | /** @return array<string, mixed> */ |
| 84 | private function buildRealSendContext(SendingQueueEntity $sendingQueue): array { |
| 85 | $meta = $sendingQueue->getMeta(); |
| 86 | $runId = is_array($meta) ? ($meta['automation']['run_id'] ?? null) : null; |
| 87 | if (!is_numeric($runId)) { |
| 88 | return []; |
| 89 | } |
| 90 | |
| 91 | $automationRun = $this->automationRunStorage->getAutomationRun((int)$runId); |
| 92 | if (!$automationRun) { |
| 93 | return []; |
| 94 | } |
| 95 | |
| 96 | $context = []; |
| 97 | $subjectKeys = []; |
| 98 | foreach ($automationRun->getSubjects() as $subject) { |
| 99 | $subjectKeys[] = $subject->getKey(); |
| 100 | if ($subject->getKey() !== OrderSubject::KEY) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | $orderId = $subject->getArgs()['order_id'] ?? null; |
| 105 | if (!is_numeric($orderId)) { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | $order = $this->wooCommerceHelper->wcGetOrder((int)$orderId); |
| 110 | if ($order instanceof \WC_Order) { |
| 111 | $context['order'] = $order; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $subjectKeys = array_values(array_unique($subjectKeys)); |
| 116 | sort($subjectKeys); |
| 117 | $context['automation_subject_keys'] = $subjectKeys; |
| 118 | |
| 119 | return $context; |
| 120 | } |
| 121 | } |
| 122 |