From 8dba4727c4b00629c0a59eff9e261d7f938df5ce Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 6 Jul 2016 21:03:45 -0400 Subject: [PATCH] - Updates open/click link generation logic to utilize API's buildRequest method --- lib/API/API.php | 6 +-- lib/Newsletter/Links/Links.php | 43 +++++++++++-------- .../Renderer/PostProcess/OpenTracking.php | 16 ++----- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/lib/API/API.php b/lib/API/API.php index a2944869a0..71c5a402e7 100644 --- a/lib/API/API.php +++ b/lib/API/API.php @@ -59,10 +59,8 @@ class API { return rtrim(base64_encode(serialize($data)), '='); } - static function buildRequest($endpoint, $action, $data, $encode_data = true) { - if($encode_data) { - $data = base64_encode(serialize($data)); - } + static function buildRequest($endpoint, $action, $data) { + $data = base64_encode(serialize($data)); $params = array( self::API_NAME => '', 'endpoint' => $endpoint, diff --git a/lib/Newsletter/Links/Links.php b/lib/Newsletter/Links/Links.php index 07ef8a55a8..69c857458f 100644 --- a/lib/Newsletter/Links/Links.php +++ b/lib/Newsletter/Links/Links.php @@ -8,7 +8,8 @@ use MailPoet\Newsletter\Shortcodes\Shortcodes; use MailPoet\Util\Security; class Links { - const DATA_TAG = '[mailpoet_data]'; + const DATA_TAG_CLICK = '[mailpoet_click_data]'; + const DATA_TAG_OPEN = '[mailpoet_open_data]'; const HASH_LENGTH = 5; static function extract($content) { @@ -33,7 +34,7 @@ class Links { // extract shortcodes with [link:*] format $shortcodes = new Shortcodes(); $shortcodes = $shortcodes->extract($content, $categories = array('link')); - $extracted_links = array_map(function($shortcode) { + $extracted_links = array_map(function ($shortcode) { return array( 'html' => $shortcode, 'link' => $shortcode @@ -62,15 +63,9 @@ class Links { 'hash' => $hash, 'url' => $extracted_link['link'] ); - $data = self::DATA_TAG . '-' . $hash; - // do not encode data; it's replaced with subscriber-specific data - // and encoded during send operation (Links::replaceSubscriberData()) - $tracked_link = API::buildRequest( - TrackAPI::ENDPOINT, - TrackAPI::ACTION_CLICK, - $data, - $encode_data = false - ); + // replace link with a temporary data tag + hash + // it will be further replaced with the proper track API URL during sending + $tracked_link = self::DATA_TAG_CLICK . '-' . $hash; // first, replace URL in the extracted HTML source with encoded link $tracked_link_html_source = str_replace( $extracted_link['link'], $tracked_link, @@ -102,12 +97,17 @@ class Links { $queue_id, $content ) { - $regex = sprintf('/data=(%s(?:-\w+)?)/', preg_quote(self::DATA_TAG)); - preg_match_all($regex, $content, $links); - foreach($links[1] as $link) { + // match [mailpoet_click]-urlHash or [mailpoet_open] + $regex = sprintf( + '/((%s|%s)(?:-\w+)?)/', + preg_quote(self::DATA_TAG_CLICK), + preg_quote(self::DATA_TAG_OPEN) + ); + preg_match_all($regex, $content, $matches); + foreach($matches[1] as $index => $match) { $hash = null; - if(preg_match('/-/', $link)) { - list(, $hash) = explode('-', $link); + if(preg_match('/-/', $match)) { + list(, $hash) = explode('-', $match); } $data = array( 'newsletter' => $newsletter_id, @@ -115,8 +115,15 @@ class Links { 'queue' => $queue_id, 'hash' => $hash ); - $data = API::encodeRequestData($data); - $content = str_replace($link, $data, $content); + $API_action = ($matches[2][$index] === self::DATA_TAG_CLICK) ? + TrackAPI::ACTION_CLICK : + TrackAPI::ACTION_OPEN; + $link = API::buildRequest( + TrackAPI::ENDPOINT, + $API_action, + $data + ); + $content = str_replace($match, $link, $content); } return $content; } diff --git a/lib/Newsletter/Renderer/PostProcess/OpenTracking.php b/lib/Newsletter/Renderer/PostProcess/OpenTracking.php index 617e0b507b..a0749d483e 100644 --- a/lib/Newsletter/Renderer/PostProcess/OpenTracking.php +++ b/lib/Newsletter/Renderer/PostProcess/OpenTracking.php @@ -1,8 +1,6 @@ parseStr($template); $template = $DOM->query('body'); - $data = Links::DATA_TAG; - // do not encode data; it's replaced with subscriber-specific data - // and encoded during send operation (Links::replaceSubscriberData()) - $url = API::buildRequest( - TrackAPI::ENDPOINT, - TrackAPI::ACTION_OPEN, - $data, - $encode_data = false - ); + // url is a temporary data tag that will be further replaced with + // the proper track API URL during sending + $url = Links::DATA_TAG_OPEN; $open_tracking_image = sprintf( '', $url @@ -29,7 +21,7 @@ class OpenTracking { } static function addTrackingImage() { - add_filter(Renderer::POST_PROCESS_FILTER, function($template) { + add_filter(Renderer::POST_PROCESS_FILTER, function ($template) { return OpenTracking::process($template); }); return true;