- Updates code based on Taut's comments

This commit is contained in:
Vlad
2016-04-14 19:36:12 -04:00
parent 84294b7ee6
commit 599661e028
6 changed files with 36 additions and 25 deletions

View File

@ -40,8 +40,11 @@ class PublicAPI {
function track() {
try {
$clicks = new Clicks($this->data);
$clicks->track();
if ($this->action === 'click') {
$track_class = new Clicks($this->data);
}
if (!isset($track_class)) return;
$track_class->track();
} catch(\Exception $e) {
}
}

View File

@ -76,7 +76,7 @@ class SendingQueue {
if($queue->newsletter_rendered_body === null) {
// render newsletter
$rendered_newsletter = $this->renderNewsletter($newsletter);
if((int) Setting::getValue('tracking.enabled') === 1) {
if((boolean) Setting::getValue('tracking.enabled') === true) {
// extract and replace links
$processed_newsletter = $this->processLinks(
$this->joinObject($rendered_newsletter),
@ -205,7 +205,7 @@ class SendingQueue {
$subscriber,
$this->joinObject($data_for_shortcodes)
);
if((int) Setting::getValue('tracking.enabled') === 1) {
if((boolean) Setting::getValue('tracking.enabled') === true) {
$processed_newsletter = $this->replaceLinks(
$newsletter['id'],
$subscriber['id'],

View File

@ -1,6 +1,7 @@
<?php
namespace MailPoet\Newsletter\Links;
use MailPoet\Newsletter\Shortcodes\Shortcodes;
use MailPoet\Util\Security;
class Links {
@ -19,10 +20,14 @@ class Links {
function extract($text) {
// adopted from WP's wp_extract_urls() function & modified to work on hrefs
# match href=' or href="
$regex = '#(?:href.*?=.*?)(["\']?)('
# match http://
. '(?:([\w-]+:)?//?)'
# match everything except for special characters # until .
. '[^\s()<>]+'
. '[.]'
# conditionally match everything except for special characters after .
. '(?:'
. '\([\w\d]+\)|'
. '(?:'
@ -32,7 +37,7 @@ class Links {
. ')'
. ')\\1#';
preg_match_all($regex, $text, $links);
preg_match_all('/\[\w+:\w+\]/', $text, $shortcodes);
preg_match_all(Shortcodes::$shortcodes_regex, $text, $shortcodes);
return array_merge(
array_unique($links[2]),
array_unique($shortcodes[0])
@ -49,7 +54,7 @@ class Links {
'url' => $link
);
$encoded_link = sprintf(
'%s/?mailpoet&endpoint=track&data=%s',
'%s/?mailpoet&endpoint=track&action=click&data=%s',
home_url(),
'[mailpoet_data]-'.$hash
);

View File

@ -18,37 +18,39 @@ class Subscription {
$action,
$default_value = false,
$newsletter = false,
$subscriber = false
$subscriber = false,
$text = false,
$shortcode
) {
switch($action) {
case 'unsubscribe':
return '<a target="_blank" href="['.
self::processUrl(
$action,
return '<a target="_blank" href="'.
self::getShortcodeUrl(
$shortcode,
esc_attr(SubscriptionUrl::getUnsubscribeUrl($subscriber))
)
.'">'.__('Unsubscribe').'</a>';
break;
case 'unsubscribe_url':
return self::processUrl(
$action,
return self::getShortcodeUrl(
$shortcode,
SubscriptionUrl::getUnsubscribeUrl($subscriber)
);
break;
case 'manage':
return '<a target="_blank" href="'.
self::processUrl(
$action,
self::getShortcodeUrl(
$shortcode,
esc_attr(SubscriptionUrl::getManageUrl($subscriber))
)
.'">'.__('Manage subscription').'</a>';
break;
case 'manage_url':
return self::processUrl(
$action,
return self::getShortcodeUrl(
$shortcode,
SubscriptionUrl::getManageUrl($subscriber)
);
break;
@ -59,10 +61,9 @@ class Subscription {
}
}
static function processUrl($action, $url) {
if((int) Setting::getValue('tracking.enabled') === 1) {
return sprintf('[subscription:%s]', $action);
}
return $url;
static function getShortcodeUrl($shortcode, $url) {
return ((boolean) Setting::getValue('tracking.enabled') === true) ?
$shortcode :
$url;
}
}

View File

@ -4,6 +4,7 @@ namespace MailPoet\Newsletter\Shortcodes;
class Shortcodes {
public $newsletter;
public $subscriber;
static $shortcodes_regex = '/\[(?:\w+):.*?\]/ism';
function __construct(
$newsletter = false,
@ -14,7 +15,7 @@ class Shortcodes {
}
function extract($text) {
preg_match_all('/\[(?:\w+):.*?\]/', $text, $shortcodes);
preg_match_all(self::$shortcodes_regex, $text, $shortcodes);
return array_unique($shortcodes[0]);
}
@ -43,7 +44,8 @@ class Shortcodes {
$shortcode_default_value,
$this->newsletter,
$this->subscriber,
$text
$text,
$shortcode
);
}, $shortcodes);
return $processed_shortcodes;

View File

@ -5,6 +5,7 @@ use MailPoet\Models\NewsletterLink;
use MailPoet\Models\StatisticsClicks;
use MailPoet\Models\Subscriber;
use MailPoet\Subscription\Url as SubscriptionUrl;
use MailPoet\Util\Helpers;
if(!defined('ABSPATH')) exit;
@ -37,7 +38,7 @@ class Clicks {
$statistics->count = 1;
$statistics->save();
} else {
$statistics->count = (int) $statistics->count++;
$statistics->count++;
$statistics->save();
}
$url = (preg_match('/\[subscription:.*?\]/', $link->url)) ?
@ -55,7 +56,6 @@ class Clicks {
if(preg_match('/manage/', $action)) {
$url = SubscriptionUrl::getManageUrl($subscriber);
}
!ddd($url);
return $url;
}