- Updates open/click link generation logic to utilize API's buildRequest

method
This commit is contained in:
Vlad
2016-07-06 21:03:45 -04:00
parent 8ec094089f
commit 8dba4727c4
3 changed files with 31 additions and 34 deletions

View File

@ -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,

View File

@ -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;
}

View File

@ -1,8 +1,6 @@
<?php
namespace MailPoet\Newsletter\Renderer\PostProcess;
use MailPoet\API\API;
use MailPoet\API\Endpoints\Track as TrackAPI;
use MailPoet\Newsletter\Links\Links;
use MailPoet\Newsletter\Renderer\Renderer;
@ -11,15 +9,9 @@ class OpenTracking {
$DOM = new \pQuery();
$DOM = $DOM->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(
'<img alt="" class="" src="%s"/>',
$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;