1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Dotdigitalgroup\Email\Plugin;
use Dotdigitalgroup\Email\Helper\Transactional;
/**
* Class TemplatePlugin
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
class TemplatePlugin
{
/**
* @var Transactional
*/
public $transactionalHelper;
/**
* @var
*/
private $templateCode;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* TemplatePlugin constructor.
* @param Transactional $transactionalHelper
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
\Dotdigitalgroup\Email\Helper\Transactional $transactionalHelper,
\Magento\Framework\Registry $registry
) {
$this->transactionalHelper = $transactionalHelper;
$this->registry = $registry;
}
/**
* @param \Magento\Email\Model\Template $subject
* @param array $result
* @param array ...$args
* @return mixed
*/
public function afterGetData(\Magento\Email\Model\Template $subject, $result, ...$args)
{
//get the template code value
if (! empty($args)) {
if ($args[0] == 'template_code') {
$this->templateCode = $result;
}
}
if ($this->registry->registry('dotmailer_saving_data')) {
$result = $this->getProcessedTemplateBeforeSave($result);
} else {
$result = $this->getProcessedTemplatePreviewAndOther($result, $args);
}
return $result;
}
/**
* Get data before saving
*
* @param mixed $result
*
* @return mixed
*/
private function getProcessedTemplateBeforeSave($result)
{
//saving array values
if (empty($args)) {
$this->getResultIfArgsEmptyForBeforeSave($result);
} else {
//saving string value
$field = $args[0];
//compress the text body when is a dotmailer template
if ($field == 'template_text' && ! $this->isStringCompressed($result) &&
$this->transactionalHelper->isDotmailerTemplate($this->templateCode)
) {
$result = $this->compressString($result);
}
if ($field == 'template_id') {
$this->saveTemplateIdInRegistry($result);
}
}
return $result;
}
/**
* @param array $result
*
* @return mixed
*/
private function getResultIfArgsEmptyForBeforeSave($result)
{
//save template id for email sending to update the sender name and sender email saved on template level.
if (isset($result['template_id'])) {
$result = $this->saveTemplateIdInRegistry($result['template_id']);
}
if (isset($result['template_text'])) {
$templateText = $result['template_text'];
//compress text
if (!$this->isStringCompressed($templateText) &&
$this->transactionalHelper->isDotmailerTemplate($result['template_code'])) {
$result['template_text'] = $this->compressString($templateText);
}
}
return $result;
}
/**
* preview/other/load
*
* @param mixed $result
* @param array $args
*
* @return mixed
*/
private function getProcessedTemplatePreviewAndOther($result, $args)
{
if (empty($args)) {
$result = $this->getResultIfArgsEmptyForPreviewAndOther($result);
} else {
if (isset($args[0])) {
$field = $args[0];
//check for correct field
if ($field == 'template_text' && $this->isStringCompressed($result)) {
$result = $this->decompressString($result);
}
if ($field == 'template_id') {
$this->saveTemplateIdInRegistry($result);
}
}
}
return $result;
}
/**
* @param array $result
*
* @return mixed
*/
private function getResultIfArgsEmptyForPreviewAndOther($result)
{
if (isset($result['template_id'])) {
$this->saveTemplateIdInRegistry($result['template_id']);
}
if (isset($result['template_text'])) {
$templateText = $result['template_text'];
if ($this->isStringCompressed($templateText)) {
$result['template_text'] = $this->decompressString($templateText);
}
}
return $result;
}
/**
* @param \Magento\Email\Model\AbstractTemplate $subject
* @param array $result
*/
public function afterBeforeSave(\Magento\Email\Model\AbstractTemplate $subject, $result)
{
//dotmailer key for saving compressed data
if (! $this->registry->registry('dotmailer_saving_data')) {
$this->registry->register('dotmailer_saving_data', 'saving');
}
}
/**
* @param string $string
* @return bool
*/
private function isStringCompressed($string)
{
//check if the data is compressed
if (substr($string, 0, 1) == 'e' && substr_count($string, ' ') == 0) {
return true;
}
return false;
}
/**
* @param string $templateText
* @return string
*/
private function compressString($templateText)
{
return base64_encode(gzcompress($templateText, 9));
}
/**
* @param string $templateText
* @return string
*/
private function decompressString($templateText)
{
return gzuncompress(base64_decode($templateText));
}
/**
* Template id register for email sending.
* @param int $templateId
*/
private function saveTemplateIdInRegistry($templateId)
{
if (! $this->registry->registry('dotmailer_current_template_id')) {
$this->registry->register('dotmailer_current_template_id', $templateId);
}
}
}