| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Services\Libs; |
| 4 |
|
| 5 |
use FluentCommunity\App\App; |
| 6 |
use FluentCommunity\App\Functions\Utility; |
| 7 |
use FluentCommunity\App\Services\Helper; |
| 8 |
use FluentCommunity\Framework\Support\Arr; |
| 9 |
|
| 10 |
class EmailComposer |
| 11 |
{ |
| 12 |
private $blocks = []; |
| 13 |
|
| 14 |
private $headingBlocks = []; |
| 15 |
|
| 16 |
private $footerBlocks = []; |
| 17 |
|
| 18 |
private $logo = ''; |
| 19 |
|
| 20 |
public function addBlock($type, $content, $options = []) |
| 21 |
{ |
| 22 |
if ($content) { |
| 23 |
$this->blocks[] = [ |
| 24 |
'type' => $type, |
| 25 |
'content' => $content, |
| 26 |
'options' => $options |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
return $this; |
| 31 |
} |
| 32 |
|
| 33 |
public function addHeadingBlock($type, $content, $options = []) |
| 34 |
{ |
| 35 |
$this->headingBlocks[] = [ |
| 36 |
'type' => $type, |
| 37 |
'content' => $content, |
| 38 |
'options' => $options |
| 39 |
]; |
| 40 |
|
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
private function compileBody() |
| 45 |
{ |
| 46 |
$html = ''; |
| 47 |
|
| 48 |
foreach ($this->blocks as $block) { |
| 49 |
$html .= $this->compileBlock($block); |
| 50 |
} |
| 51 |
|
| 52 |
return $html; |
| 53 |
} |
| 54 |
|
| 55 |
private function complileHeadingBlocks() |
| 56 |
{ |
| 57 |
$html = ''; |
| 58 |
|
| 59 |
foreach ($this->headingBlocks as $block) { |
| 60 |
$html .= $this->compileBlock($block); |
| 61 |
} |
| 62 |
|
| 63 |
return $html; |
| 64 |
} |
| 65 |
|
| 66 |
public function compileBlock($block) |
| 67 |
{ |
| 68 |
if ($block['type'] == 'paragraph') { |
| 69 |
return '<p style="font-family: Arial, sans-serif; line-height: 1.6; font-size: 16px; font-weight: normal; margin: 0; margin-bottom: 10px;">' . $block['content'] . '</p>'; |
| 70 |
} |
| 71 |
|
| 72 |
if ($block['type'] == 'heading') { |
| 73 |
return '<h2 style="font-family: Arial, sans-serif; font-size: 20px; font-weight: bold; margin: 0; margin-bottom: 10px;">' . $block['content'] . '</h2>'; |
| 74 |
} |
| 75 |
|
| 76 |
if ($block['type'] == 'button') { |
| 77 |
return (string)App::make('view')->make('email.Default._button', [ |
| 78 |
'link' => Arr::get($block['options'], 'link'), |
| 79 |
'btnText' => $block['content'], |
| 80 |
'linkColor' => Utility::getThemeColor(), |
| 81 |
'btnTextColor' => Utility::getThemeColor('theme_button_text') |
| 82 |
]); |
| 83 |
} |
| 84 |
|
| 85 |
if ($block['type'] == 'boxed_content') { |
| 86 |
return (string)App::make('view')->make('email.Default._user_box_content', [ |
| 87 |
'user_name' => $block['options']['user']->getPublicDisplayName(), |
| 88 |
'user_avatar' => !(empty($block['options']['user']->xprofile->avatar)) ? $block['options']['user']->xprofile->avatar : $block['options']['user']->photo, |
| 89 |
'content' => $block['content'], |
| 90 |
'permalink' => $block['options']['permalink'], |
| 91 |
'post_content' => $block['options']['post_content'], |
| 92 |
'linkColor' => Utility::getThemeColor() |
| 93 |
]); |
| 94 |
} |
| 95 |
|
| 96 |
if ($block['type'] == 'post_boxed_content') { |
| 97 |
return (string)App::make('view')->make('email.Default._user_post_content', [ |
| 98 |
'user_name' => $block['options']['user']->getPublicDisplayName(), |
| 99 |
'user_avatar' => !(empty($block['options']['user']->xprofile->avatar)) ? $block['options']['user']->xprofile->avatar : $block['options']['user']->photo, |
| 100 |
'content' => $block['content'], |
| 101 |
'title' => Arr::get($block['options'], 'title'), |
| 102 |
'permalink' => $block['options']['permalink'], |
| 103 |
'space_name' => $block['options']['space_name'], |
| 104 |
'linkColor' => Utility::getThemeColor() |
| 105 |
]); |
| 106 |
} |
| 107 |
|
| 108 |
if ($block['type'] == 'html_content') { |
| 109 |
return (string)$block['content']; |
| 110 |
} |
| 111 |
|
| 112 |
return $block['content']; |
| 113 |
} |
| 114 |
|
| 115 |
public function setLogo($logo) |
| 116 |
{ |
| 117 |
if ($logo) { |
| 118 |
$this->logo = $logo; |
| 119 |
} |
| 120 |
|
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
public function setDefaultLogo() |
| 125 |
{ |
| 126 |
$emailSettings = Utility::getEmailNotificationSettings(); |
| 127 |
if (!empty($emailSettings['logo'])) { |
| 128 |
$this->logo = $emailSettings['logo']; |
| 129 |
} |
| 130 |
|
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
public function addFooterLine($type, $line) |
| 135 |
{ |
| 136 |
$this->footerBlocks[] = [ |
| 137 |
'type' => $type, |
| 138 |
'content' => $line |
| 139 |
]; |
| 140 |
|
| 141 |
return $this; |
| 142 |
} |
| 143 |
|
| 144 |
public function setDefaultFooter($withPlaceHolder = true) |
| 145 |
{ |
| 146 |
$settings = Utility::getEmailNotificationSettings(); |
| 147 |
$footerTextHtml = Arr::get($settings, 'email_footer_rendered', ''); |
| 148 |
|
| 149 |
if (!$footerTextHtml) { |
| 150 |
if ($settings['disable_powered_by'] == 'no') { |
| 151 |
$utmParams = ['utm_campaign' => 'email', 'utm_source' => 'footer', 'utm_medium' => 'email']; |
| 152 |
$poweredBy = '<a href="' . Utility::getProductUrl(true, $utmParams) . '" target="_blank" style="font-size: 12px; margin: 15px 0; display: block; text-decoration: none;color: #9a9ea6;">Powered by FluentCommunity</a>'; |
| 153 |
$this->addFooterLine('paragraph', $poweredBy); |
| 154 |
} |
| 155 |
return $this; |
| 156 |
} |
| 157 |
|
| 158 |
$generalSettings = Helper::generalSettings(); |
| 159 |
|
| 160 |
$themeColor = Utility::getThemeColor(); |
| 161 |
|
| 162 |
$replaces = [ |
| 163 |
'{{site_name_with_url}}' => '<a target="_blank" href="' . Helper::baseUrl('/') . '" style="color: ' . esc_attr($themeColor) . '; text-decoration: none;">' . Arr::get($generalSettings, 'site_title') . '</a>', |
| 164 |
'{{site_name}}' => Arr::get($generalSettings, 'site_title') |
| 165 |
]; |
| 166 |
|
| 167 |
$footerTextHtml = str_replace(array_keys($replaces), array_values($replaces), $footerTextHtml); |
| 168 |
|
| 169 |
// find pattern like this: {{manage_email_notification_url|Manage Your Email Notifications Preference}} |
| 170 |
$footerTextHtml = preg_replace_callback('/{{(.*?)\|(.*?)}}/', function ($matches) use ($withPlaceHolder, $themeColor) { |
| 171 |
$match1 = $matches[1]; |
| 172 |
if ($match1 != 'manage_email_notification_url') { |
| 173 |
return $matches[0]; |
| 174 |
} |
| 175 |
|
| 176 |
if ($withPlaceHolder) { |
| 177 |
$url = '##email_notification_url##'; |
| 178 |
} else { |
| 179 |
$url = Helper::baseUrl('fcom_route?route=user_notification_settings&auth=yes'); |
| 180 |
} |
| 181 |
|
| 182 |
$text = $matches[2]; |
| 183 |
return '<a target="_blank" rel="noopener noreferrer" style="color: ' . esc_attr($themeColor) . '; text-decoration: underline;" href="' . $url . '">' . $text . '</a>'; |
| 184 |
}, $footerTextHtml); |
| 185 |
|
| 186 |
$this->addFooterLine('paragraph', $footerTextHtml); |
| 187 |
|
| 188 |
if ($settings['disable_powered_by'] == 'no') { |
| 189 |
$utmParams = ['utm_campaign' => 'email', 'utm_source' => 'footer', 'utm_medium' => 'email']; |
| 190 |
$poweredBy = '<p style="margin-top: 10px;" class="powered_by"><a href="' . Utility::getProductUrl(true, $utmParams) . '" target="_blank" style="font-size: 12px; margin: 15px 0; text-decoration: none;color: #9a9ea6; display: block;">Powered by FluentCommunity</a></p>'; |
| 191 |
$this->addFooterLine('paragraph', $poweredBy); |
| 192 |
} |
| 193 |
|
| 194 |
return $this; |
| 195 |
} |
| 196 |
|
| 197 |
public function getHtml() |
| 198 |
{ |
| 199 |
$generalSettings = Helper::generalSettings(); |
| 200 |
|
| 201 |
$data = [ |
| 202 |
'logo' => [], |
| 203 |
'bodyContent' => $this->compileBody() |
| 204 |
]; |
| 205 |
|
| 206 |
if ($this->logo) { |
| 207 |
$data['logo'] = [ |
| 208 |
'url' => $this->logo, |
| 209 |
'alt' => Arr::get($generalSettings, 'site_title') |
| 210 |
]; |
| 211 |
} |
| 212 |
|
| 213 |
$footerLines = []; |
| 214 |
foreach ($this->footerBlocks as $footerBlock) { |
| 215 |
$footerLines[] = $footerBlock['content']; |
| 216 |
} |
| 217 |
|
| 218 |
$data['footerLines'] = $footerLines; |
| 219 |
|
| 220 |
if ($this->headingBlocks) { |
| 221 |
$data['headingContent'] = $this->complileHeadingBlocks(); |
| 222 |
} |
| 223 |
|
| 224 |
$html = (string)App::make('view')->make('email.template', $data); |
| 225 |
|
| 226 |
// Inline the <head><style> rules onto each element so they survive clients and |
| 227 |
// log viewers that strip the document head (Outlook, FluentSMTP's DOMPurify |
| 228 |
// preview). @media/:hover rules can't be inlined and are kept in a <style> tag. |
| 229 |
try { |
| 230 |
$html = (new Emogrifier\Emogrifier($html))->emogrify(); |
| 231 |
} catch (\Throwable $e) { |
| 232 |
// Fall back to the un-inlined HTML; the <head><style> still covers Gmail. |
| 233 |
} |
| 234 |
|
| 235 |
return $html; |
| 236 |
} |
| 237 |
|
| 238 |
} |
| 239 |
|