Tried that, won't work :-( I think the function for parsing URLs into tracking URLs is called before getCommonTagsSearchReplace, so my markup doesn't have the URLs in there yet.I am away from the code now, but i think the app trats tags that end with _URL] specially, so try to name your custom tags that are transformed into urls like [WHATEVER_DO_THIS_URL]
Ya, I don't want to hook in the behavior file. Is there any other way?that would make sense and you'd have to hook in the behavior file directly, which isn't that desirable.
There's no other way... Maybe we can add a new filter hook inthere and you could then use it?
But you'll have to tell me what exactly you need and where you want the changes to happen in code so that i can take proper decision for the hook.
if (!$onlyPlainText) {
if (($emailFooter = $customer->getGroupOption('campaigns.email_footer')) && strlen(trim($emailFooter)) > 5) {
$emailContent = CampaignHelper::injectEmailFooter($emailContent, $emailFooter, $campaign);
}
if (!empty($campaign->option) && !empty($campaign->option->embed_images) && $campaign->option->embed_images == CampaignOption::TEXT_YES) {
list($emailContent, $embedImages) = CampaignHelper::embedContentImages($emailContent, $campaign);
}
if (!empty($campaign->option) && $campaign->option->xml_feed == CampaignOption::TEXT_YES) {
$emailContent = CampaignXmlFeedParser::parseContent($emailContent, $campaign, $subscriber, true);
}
if (!empty($campaign->option) && $campaign->option->json_feed == CampaignOption::TEXT_YES) {
$emailContent = CampaignJsonFeedParser::parseContent($emailContent, $campaign, $subscriber, true);
}
if (!empty($campaign->option) && $campaign->option->url_tracking == CampaignOption::TEXT_YES) {
$emailContent = CampaignHelper::transformLinksForTracking($emailContent, $campaign, $subscriber, true);
}
$emailData = CampaignHelper::parseContent($emailContent, $campaign, $subscriber, true);
list($toName, $emailSubject, $emailContent) = $emailData;
}
if (!empty($campaign->option) && $campaign->option->url_tracking == CampaignOption::TEXT_YES) {
$emailContent = CampaignHelper::transformLinksForTracking($emailContent, $campaign, $subscriber, true);
}
if (!empty($campaign->option) && $campaign->option->url_tracking == CampaignOption::TEXT_YES) {
$emailContent = Yii::app()->hooks->applyFilters('some_hook_name_before_transform_links_for_tracking', $emailContent, $campaign, $subscriber, true);
$emailContent = CampaignHelper::transformLinksForTracking($emailContent, $campaign, $subscriber, true);
$emailContent = Yii::app()->hooks->applyFilters('some_hook_name_after_transform_links_for_tracking', $emailContent, $campaign, $subscriber, true);
}
Yii::app()->hooks->addFilter('some_hook_name_before_transform_links_for_tracking', function($emailContent, $campaign, $subscriber, $bool){
// alter the email content, i.e:
$emailContent = str_replace($arrayOfTags, $arrayOfValues, $emailContent);
return $emailContent;
});
If I were to throw my custom code in here:@Lakjin - See above.
Yii::app()->hooks->addFilter('some_hook_name_before_transform_links_for_tracking', function($emailContent, $campaign, $subscriber, $bool){
//My code here
return $emailContent;
});
public static function transformLinksForTracking($content, Campaign $campaign, ListSubscriber $subscriber, $canSave = false)
{
$content = StringHelper::decodeSurroundingTags($content);
$list = $campaign->list;
$mustSave = false;
$content = Yii::app()->hooks->applyFilters('campaign_content_before_transform_links_for_tracking', $content, $campaign, $subscriber, $list);
if (!isset(self::$_trackingUrls[$campaign->campaign_id])) {
self::$_trackingUrls[$campaign->campaign_id] = array();
$urlModelsCount = 0;
if ($canSave) {
$urlModelsCount = CampaignUrl::model()->countByAttributes(array(
'campaign_id' => $campaign->campaign_id,
));
}
$mustSave = $urlModelsCount == 0;
$baseUrl = Yii::app()->options->get('system.urls.frontend_absolute_url');
$trackingUrl = $baseUrl . 'campaigns/[CAMPAIGN_UID]/track-url/[SUBSCRIBER_UID]';
// (\042|\047) are octal quotes.
$pattern = '/href(\s+)?=(\s+)?(\042|\047)(\s+)?(.*?)(\s+)?(\042|\047)/i';
if (!preg_match_all($pattern, $content, $matches)) {
return $content;
}
$urls = $matches[5];
$urls = array_map('trim', $urls);
// combine url with markup
$urls = array_combine($urls, $matches[0]);
$foundUrls = array();
foreach ($urls as $url => $markup) {
// external url which may contain one or more tags(sharing maybe?)
if (preg_match('/https?.*/i', $url, $matches) && filter_var($url, FILTER_VALIDATE_URL)) {
$_url = trim($matches[0]);
$foundUrls[$_url] = $markup;
continue;
}
// local tag to be transformed
if (preg_match('/^\[([A-Z_]+)_URL\]$/', $url, $matches)) {
$_url = trim($matches[0]);
$foundUrls[$_url] = $markup;
continue;
}
}
if (empty($foundUrls)) {
$content = Yii::app()->hooks->applyFilters('campaign_content_after_transform_links_for_tracking', $content, $campaign, $subscriber, $list);
return $content;
}
$prefix = $campaign->campaign_uid;
$sort = array();
foreach ($foundUrls as $url => $markup) {
$urlHash = sha1($prefix . $url);
$track = $trackingUrl . '/' . $urlHash;
$length = strlen($url);
self::$_trackingUrls[$campaign->campaign_id][] = array(
'url' => $url,
'hash' => $urlHash,
'track' => $track,
'length' => $length,
'markup' => $markup,
);
$sort[] = $length;
}
unset($foundUrls);
// make sure we order by the longest url to the shortest
array_multisort($sort, SORT_DESC, SORT_NUMERIC, self::$_trackingUrls[$campaign->campaign_id]);
// one final check
if (!$mustSave) {
$mustSave = count(self::$_trackingUrls[$campaign->campaign_id]) != $urlModelsCount;
}
}
if (!empty(self::$_trackingUrls[$campaign->campaign_id])) {
$searchReplace = array();
foreach (self::$_trackingUrls[$campaign->campaign_id] as $urlData) {
$searchReplace[$urlData['markup']] = 'href="'.$urlData['track'].'"';
}
$content = str_replace(array_keys($searchReplace), array_values($searchReplace), $content);
unset($searchReplace);
// save the url tags.
if ($mustSave && $canSave) {
foreach (self::$_trackingUrls[$campaign->campaign_id] as $urlData) {
$urlModel = CampaignUrl::model()->findByAttributes(array(
'campaign_id' => (int)$campaign->campaign_id,
'hash' => $urlData['hash'],
));
if (!empty($urlModel)) {
continue;
}
$urlModel = new CampaignUrl();
$urlModel->campaign_id = $campaign->campaign_id;
$urlModel->destination = $urlData['url'];
$urlModel->hash = $urlData['hash'];
$urlModel->save(false);
}
}
}
$content = Yii::app()->hooks->applyFilters('campaign_content_after_transform_links_for_tracking', $content, $campaign, $subscriber, $list);
// return transformed
return $content;
}
Yii::app()->hooks->addFilter('campaign_content_before_transform_links_for_tracking', function($emailContent, $campaign, $subscriber, $list){
//My code here
return $emailContent;
});
Test it and lemme know how it went.
Yii::app()->hooks->addFilter('campaign_content_before_transform_links_for_tracking', function($emailContent, $campaign, $subscriber, $list){//My code herereturn $emailContent;
});
<?php defined('MW_PATH') || exit('No direct script access allowed');
class MyCampaignTagsExt extends ExtensionInit
{
// name of the extension as shown in the backend panel
public $name = 'My Campaign Tags';
// description of the extension as shown in backend panel
public $description = 'My Campaign Tags';
// current version of this extension
public $version = '1.0';
// the author name
public $author = 'Your name';
// author website
public $website = 'http://www.website.com/';
// contact email address
public $email = 'your.email@domain.com';
// in which apps this extension is allowed to run
public $allowedApps = array('*');
// can this extension be deleted? this only applies to core extensions.
protected $_canBeDeleted = true;
// can this extension be disabled? this only applies to core extensions.
protected $_canBeDisabled = true;
// run the extension, ths is mandatory
public function run()
{
Yii::app()->hooks->addFilter('campaign_content_before_transform_links_for_tracking', function($content, $campaign, $subscriber, $list){
//My code here
return $content;
});
}
}
Put this in a file called MyCampaignTagsExt.php then create a folder named my-campaign-tags and put the .php file in it and zip the folder then upload it into mailwizz as an extension.
Thanks. I actually got it by looking at your code -- I called the getDb(); function. I just didnt want to open another database connection when MailWizz already had one.
<?php defined('MW_PATH') || exit('No direct script access allowed');
class MyCampaignTagsExt extends ExtensionInit
{
// name of the extension as shown in the backend panel
public $name = 'My Campaign Tags';
// description of the extension as shown in backend panel
public $description = 'My Campaign Tags';
// current version of this extension
public $version = '1.0';
// the author name
public $author = 'Your name';
// author website
public $website = 'http://www.website.com/';
// contact email address
public $email = 'your.email@domain.com';
// in which apps this extension is allowed to run
public $allowedApps = array('*');
// can this extension be deleted? this only applies to core extensions.
protected $_canBeDeleted = true;
// can this extension be disabled? this only applies to core extensions.
protected $_canBeDisabled = true;
// run the extension, ths is mandatory
public function run()
{
Yii::app()->hooks->addFilter('campaign_content_before_transform_links_for_tracking', function($content, $campaign, $subscriber, $list){
$content = str_replace('[xxx]','yyy',$content);
return $content;
});
}
}
public static function transformLinksForTracking($content, Campaign $campaign, ListSubscriber $subscriber, $canSave = false)
{
//code here
$content = StringHelper::decodeSurroundingTags($content);$list = $campaign->list;$mustSave = false;
public static function transformLinksForTracking($content, Campaign $campaign, ListSubscriber $subscriber, $canSave = false)
{
$list = $campaign->list;
$content = Yii::app()->hooks->applyFilters('campaign_content_before_transform_links_for_tracking', $content, $campaign, $subscriber, $list);
$content = StringHelper::decodeSurroundingTags($content);$mustSave = false;