- Updates open/click link generation logic to utilize API's buildRequest
method
This commit is contained in:
@ -59,10 +59,8 @@ class API {
|
|||||||
return rtrim(base64_encode(serialize($data)), '=');
|
return rtrim(base64_encode(serialize($data)), '=');
|
||||||
}
|
}
|
||||||
|
|
||||||
static function buildRequest($endpoint, $action, $data, $encode_data = true) {
|
static function buildRequest($endpoint, $action, $data) {
|
||||||
if($encode_data) {
|
|
||||||
$data = base64_encode(serialize($data));
|
$data = base64_encode(serialize($data));
|
||||||
}
|
|
||||||
$params = array(
|
$params = array(
|
||||||
self::API_NAME => '',
|
self::API_NAME => '',
|
||||||
'endpoint' => $endpoint,
|
'endpoint' => $endpoint,
|
||||||
|
@ -8,7 +8,8 @@ use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
|||||||
use MailPoet\Util\Security;
|
use MailPoet\Util\Security;
|
||||||
|
|
||||||
class Links {
|
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;
|
const HASH_LENGTH = 5;
|
||||||
|
|
||||||
static function extract($content) {
|
static function extract($content) {
|
||||||
@ -62,15 +63,9 @@ class Links {
|
|||||||
'hash' => $hash,
|
'hash' => $hash,
|
||||||
'url' => $extracted_link['link']
|
'url' => $extracted_link['link']
|
||||||
);
|
);
|
||||||
$data = self::DATA_TAG . '-' . $hash;
|
// replace link with a temporary data tag + hash
|
||||||
// do not encode data; it's replaced with subscriber-specific data
|
// it will be further replaced with the proper track API URL during sending
|
||||||
// and encoded during send operation (Links::replaceSubscriberData())
|
$tracked_link = self::DATA_TAG_CLICK . '-' . $hash;
|
||||||
$tracked_link = API::buildRequest(
|
|
||||||
TrackAPI::ENDPOINT,
|
|
||||||
TrackAPI::ACTION_CLICK,
|
|
||||||
$data,
|
|
||||||
$encode_data = false
|
|
||||||
);
|
|
||||||
// first, replace URL in the extracted HTML source with encoded link
|
// first, replace URL in the extracted HTML source with encoded link
|
||||||
$tracked_link_html_source = str_replace(
|
$tracked_link_html_source = str_replace(
|
||||||
$extracted_link['link'], $tracked_link,
|
$extracted_link['link'], $tracked_link,
|
||||||
@ -102,12 +97,17 @@ class Links {
|
|||||||
$queue_id,
|
$queue_id,
|
||||||
$content
|
$content
|
||||||
) {
|
) {
|
||||||
$regex = sprintf('/data=(%s(?:-\w+)?)/', preg_quote(self::DATA_TAG));
|
// match [mailpoet_click]-urlHash or [mailpoet_open]
|
||||||
preg_match_all($regex, $content, $links);
|
$regex = sprintf(
|
||||||
foreach($links[1] as $link) {
|
'/((%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;
|
$hash = null;
|
||||||
if(preg_match('/-/', $link)) {
|
if(preg_match('/-/', $match)) {
|
||||||
list(, $hash) = explode('-', $link);
|
list(, $hash) = explode('-', $match);
|
||||||
}
|
}
|
||||||
$data = array(
|
$data = array(
|
||||||
'newsletter' => $newsletter_id,
|
'newsletter' => $newsletter_id,
|
||||||
@ -115,8 +115,15 @@ class Links {
|
|||||||
'queue' => $queue_id,
|
'queue' => $queue_id,
|
||||||
'hash' => $hash
|
'hash' => $hash
|
||||||
);
|
);
|
||||||
$data = API::encodeRequestData($data);
|
$API_action = ($matches[2][$index] === self::DATA_TAG_CLICK) ?
|
||||||
$content = str_replace($link, $data, $content);
|
TrackAPI::ACTION_CLICK :
|
||||||
|
TrackAPI::ACTION_OPEN;
|
||||||
|
$link = API::buildRequest(
|
||||||
|
TrackAPI::ENDPOINT,
|
||||||
|
$API_action,
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$content = str_replace($match, $link, $content);
|
||||||
}
|
}
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Newsletter\Renderer\PostProcess;
|
namespace MailPoet\Newsletter\Renderer\PostProcess;
|
||||||
|
|
||||||
use MailPoet\API\API;
|
|
||||||
use MailPoet\API\Endpoints\Track as TrackAPI;
|
|
||||||
use MailPoet\Newsletter\Links\Links;
|
use MailPoet\Newsletter\Links\Links;
|
||||||
use MailPoet\Newsletter\Renderer\Renderer;
|
use MailPoet\Newsletter\Renderer\Renderer;
|
||||||
|
|
||||||
@ -11,15 +9,9 @@ class OpenTracking {
|
|||||||
$DOM = new \pQuery();
|
$DOM = new \pQuery();
|
||||||
$DOM = $DOM->parseStr($template);
|
$DOM = $DOM->parseStr($template);
|
||||||
$template = $DOM->query('body');
|
$template = $DOM->query('body');
|
||||||
$data = Links::DATA_TAG;
|
// url is a temporary data tag that will be further replaced with
|
||||||
// do not encode data; it's replaced with subscriber-specific data
|
// the proper track API URL during sending
|
||||||
// and encoded during send operation (Links::replaceSubscriberData())
|
$url = Links::DATA_TAG_OPEN;
|
||||||
$url = API::buildRequest(
|
|
||||||
TrackAPI::ENDPOINT,
|
|
||||||
TrackAPI::ACTION_OPEN,
|
|
||||||
$data,
|
|
||||||
$encode_data = false
|
|
||||||
);
|
|
||||||
$open_tracking_image = sprintf(
|
$open_tracking_image = sprintf(
|
||||||
'<img alt="" class="" src="%s"/>',
|
'<img alt="" class="" src="%s"/>',
|
||||||
$url
|
$url
|
||||||
|
Reference in New Issue
Block a user