1
|
|
|
<?php |
2
|
|
|
namespace Azine\EmailBundle\Services; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
5
|
|
|
|
6
|
|
|
class AzineEmailTwigExtension extends \Twig_Extension |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var TemplateProviderInterface |
10
|
|
|
*/ |
11
|
|
|
private $templateProvider; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var TranslatorInterface |
15
|
|
|
*/ |
16
|
|
|
private $translator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $domainsToTrack; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param TemplateProviderInterface $templateProvider |
25
|
|
|
* @param array of string $domainsToTrack |
26
|
|
|
*/ |
27
|
11 |
|
public function __construct(TemplateProviderInterface $templateProvider, TranslatorInterface $translator, array $domainsToTrack = array()){ |
28
|
11 |
|
$this->templateProvider = $templateProvider; |
29
|
11 |
|
$this->translator = $translator; |
30
|
11 |
|
$this->domainsToTrack = $domainsToTrack; |
31
|
11 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
1 |
|
public function getFilters() |
37
|
|
|
{ |
38
|
1 |
|
$filters[] = new \Twig_SimpleFilter('textWrap', array($this, 'textWrap')); |
|
|
|
|
39
|
1 |
|
$filters[] = new \Twig_SimpleFilter('urlEncodeText', array($this, 'urlEncodeText'), array('is_safe' => array('html'))); |
40
|
1 |
|
$filters[] = new \Twig_SimpleFilter('addCampaignParamsForTemplate', array($this, 'addCampaignParamsForTemplate'), array('is_safe' => array('html'))); |
41
|
1 |
|
$filters[] = new \Twig_SimpleFilter('stripAndConvertTags', array($this, 'stripAndConvertTags'), array('is_safe' => array('html'))); |
42
|
1 |
|
return $filters; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function urlEncodeText($text) |
46
|
|
|
{ |
47
|
1 |
|
$text = str_replace("%","%25", $text); |
48
|
1 |
|
$text = str_replace(array( "\n", |
49
|
1 |
|
" ", |
50
|
1 |
|
"&", |
51
|
1 |
|
"\\", |
52
|
1 |
|
"<", |
53
|
1 |
|
">", |
54
|
1 |
|
'"', |
55
|
1 |
|
" ", |
56
|
1 |
|
), |
57
|
1 |
|
array( "%0D%0A", |
58
|
1 |
|
"%20", |
59
|
1 |
|
"%26", |
60
|
1 |
|
"%5C", |
61
|
1 |
|
"%3D", |
62
|
1 |
|
"%3E", |
63
|
1 |
|
"%23", |
64
|
1 |
|
"%09", |
65
|
1 |
|
), $text); |
66
|
|
|
|
67
|
1 |
|
return $text; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Wrap the text to the lineLength is not exeeded. |
72
|
|
|
* @param string $text |
73
|
|
|
* @param integer $lineLength default: 75 |
74
|
|
|
* @return string the wrapped string |
75
|
|
|
*/ |
76
|
3 |
|
public function textWrap($text, $lineLength = 75) |
77
|
|
|
{ |
78
|
3 |
|
return wordwrap($text, $lineLength); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the name of the extension. |
83
|
|
|
* |
84
|
|
|
* @return string The extension name |
85
|
|
|
*/ |
86
|
1 |
|
public function getName() |
87
|
|
|
{ |
88
|
1 |
|
return 'azine_email_bundle_twig_extension'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function addCampaignParamsForTemplate($html, $templateId, $templateParams){ |
92
|
|
|
$campaignParams = $this->templateProvider->getCampaignParamsFor($templateId, $templateParams); |
93
|
|
|
return $this->addCampaignParamsToAllUrls($html, $campaignParams); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add the campaign-parameters to all URLs in the html |
98
|
|
|
* @param string $html |
99
|
|
|
* @param array $campaignParams |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
3 |
|
public function addCampaignParamsToAllUrls($html, $campaignParams) |
103
|
|
|
{ |
104
|
|
|
|
105
|
3 |
|
$urlPattern = '/(href=[\'|"])(http[s]?\:\/\/\S*)([\'|"])/'; |
106
|
|
|
|
107
|
|
|
$filteredHtml = preg_replace_callback($urlPattern, function ($matches) use ($campaignParams) { |
108
|
3 |
|
$start = $matches[1]; |
109
|
3 |
|
$url = $matches[2]; |
110
|
3 |
|
$end = $matches[3]; |
111
|
3 |
|
$domain = parse_url($url, PHP_URL_HOST); |
112
|
|
|
|
113
|
|
|
// if the url is not in the list of domains to track then |
114
|
3 |
|
if(array_search($domain, $this->domainsToTrack) === false){ |
115
|
|
|
// don't append tracking parameters to the url |
116
|
3 |
|
return $start.$url.$end; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// avoid duplicate params and don't replace existing params |
120
|
|
|
$params = array(); |
121
|
|
|
foreach($campaignParams as $nextKey => $nextValue){ |
122
|
|
|
if(strpos($url, $nextKey) === false){ |
123
|
|
|
$params[$nextKey] = $nextValue; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$urlParams = http_build_query($params); |
128
|
|
|
|
129
|
|
|
if (strpos($url,"?") === false) { |
130
|
|
|
$urlParams = "?".$urlParams; |
131
|
|
|
} else { |
132
|
|
|
$urlParams = "&".$urlParams; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$replacement = $start.$url.$urlParams.$end; |
136
|
|
|
|
137
|
|
|
return $replacement; |
138
|
|
|
|
139
|
3 |
|
}, $html); |
140
|
|
|
|
141
|
3 |
|
return $filteredHtml; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Convert: |
146
|
|
|
* - a-tags to show the link and if the link-text is not contained in the link, also the link-text |
147
|
|
|
* - remove double-whitespaces and whitespaces at line beginnings and ends. |
148
|
|
|
* - html-special chars to their original representation (php => htmlspecialchars_decode) |
149
|
|
|
* and then remove all html-tags (php => strip_tags) |
150
|
|
|
*/ |
151
|
1 |
|
public function stripAndConvertTags($html){ |
152
|
|
|
|
153
|
1 |
|
$linkConvertedHtml = preg_replace_callback('/<a.*?href=[\'|"](.+?)[\'|"].*?>(.*?)<\/a>/s', function ($matches) { |
154
|
1 |
|
$url = $matches[1]; |
155
|
1 |
|
$text = trim(strip_tags($matches[2])); |
156
|
|
|
|
157
|
1 |
|
if(strlen($text) == 0 || stripos($url, $text) !== false){ |
158
|
1 |
|
$replacement = $url; |
159
|
1 |
|
} else { |
160
|
1 |
|
$replacement = $text . ": " . $url; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
return $replacement; |
164
|
|
|
|
165
|
1 |
|
}, $html); |
166
|
|
|
|
167
|
1 |
|
$txt = strip_tags($linkConvertedHtml); |
168
|
1 |
|
$txt = preg_replace('/[[:blank:]]+/', ' ', $txt); |
169
|
1 |
|
$txt = preg_replace("/\n[[:blank:]]/", "\n", $txt); |
170
|
1 |
|
$txt = preg_replace("/[[:blank:]]\n/", "\n", $txt); |
171
|
1 |
|
$txt = html_entity_decode($txt); |
172
|
1 |
|
return $txt; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.