- 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() { function track() {
try { try {
$clicks = new Clicks($this->data); if ($this->action === 'click') {
$clicks->track(); $track_class = new Clicks($this->data);
}
if (!isset($track_class)) return;
$track_class->track();
} catch(\Exception $e) { } catch(\Exception $e) {
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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