/** * Embed campaigns into the newsletter and return the updated html * * @param string $html the email content * @param array $campaign the general campaign for the newsletter * @param array $additionalCampaigns additional campaigns to be replaced * @return string $html html with updated hrefs */ public static function embedCampaigns($html, $campaign = [], $additionalCampaigns = []) { $pattern = '/<a(\\s[^>]*)href="([^"]*)"([^>]*)>/si'; $html = preg_replace_callback($pattern, function ($matches) use($campaign, $additionalCampaigns) { $href = GoogleCampaignPlugin::replaceLink($matches[2], $campaign, $additionalCampaigns); return "<a{$matches[1]}href=\"{$href}\"{$matches[3]}>"; }, $html); return $html; }
/** * @covers Openbuildings\Swiftmailer\GoogleCampaignPlugin::replaceLink */ public function test_replace_link() { $href = 'http://example.com'; $href = GoogleCampaignPlugin::replaceLink($href, ['utm_source' => 'newsletter', 'utm_campaign' => 'my_campaign']); $this->assertEquals('http://example.com?utm_source=newsletter&utm_campaign=my_campaign', $href); $href = GoogleCampaignPlugin::replaceLink($href, ['utm_source' => 'newsletter', 'utm_campaign' => 'my_second_campaign']); $this->assertEquals('http://example.com?utm_source=newsletter&utm_campaign=my_campaign', $href, 'Should not replace link with existing campaign params'); $href = 'http://example.com?test_param=test_value&google_campaign=share'; $href = GoogleCampaignPlugin::replaceLink($href, ['utm_source' => 'newsletter', 'utm_campaign' => 'my_general_campaign'], ['share' => ['utm_source' => 'newsletter', 'utm_campaign' => 'my_share_campaign']]); $this->assertEquals('http://example.com?test_param=test_value&utm_source=newsletter&utm_campaign=my_share_campaign', $href, 'Should replace link with share campaign'); }