TemplatePlugin.php 5.9 KB

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