fixed shortcodes replacement for "global:" in newsletters
- extracted "get subscription pages urls" from models\Subscriber - added unit tests for subscription\url class (not working because of WP/Codeception issue)
This commit is contained in:
@ -9,6 +9,7 @@ settings:
|
||||
bootstrap: _bootstrap.php
|
||||
colors: true
|
||||
memory_limit: 1024M
|
||||
log: true
|
||||
extensions:
|
||||
enabled:
|
||||
- Codeception\Extension\RunFailed
|
||||
|
@ -70,8 +70,12 @@ class Populator {
|
||||
|
||||
if($page === null) {
|
||||
$mailpoet_page_id = Pages::createMailPoetPage();
|
||||
Setting::setValue('subscription.page', $mailpoet_page_id);
|
||||
} else {
|
||||
$mailpoet_page_id = $page->ID;
|
||||
}
|
||||
|
||||
Setting::setValue('subscription.page', $mailpoet_page_id);
|
||||
Setting::setValue('signup_confirmation.page', $mailpoet_page_id);
|
||||
}
|
||||
|
||||
private function createDefaultSettings() {
|
||||
|
@ -8,6 +8,9 @@ class Setting extends Model {
|
||||
|
||||
public static $defaults = null;
|
||||
|
||||
const DEFAULT_SENDING_FREQUENCY_EMAILS = 25;
|
||||
const DEFAULT_SENDING_FREQUENCY_INTERVAL = 15; // in minutes
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
@ -25,6 +28,14 @@ class Setting extends Model {
|
||||
|
||||
public static function loadDefaults() {
|
||||
self::$defaults = array(
|
||||
'mta_group' => 'website',
|
||||
'mta' => array(
|
||||
'method' => 'PHPMail',
|
||||
'frequency' => array(
|
||||
'emails' => self::DEFAULT_SENDING_FREQUENCY_EMAILS,
|
||||
'interval' => self::DEFAULT_SENDING_FREQUENCY_INTERVAL
|
||||
)
|
||||
),
|
||||
'signup_confirmation' => array(
|
||||
'enabled' => true,
|
||||
'subject' => sprintf(__('Confirm your subscription to %1$s'), get_option('blogname')),
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace MailPoet\Models;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\Subscription;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -70,42 +71,6 @@ class Subscriber extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
function getConfirmationUrl() {
|
||||
$post = get_post(Setting::getValue('signup_confirmation.page'));
|
||||
return $this->getSubscriptionUrl($post, 'confirm');
|
||||
}
|
||||
|
||||
function getEditSubscriptionUrl() {
|
||||
$post = get_post(Setting::getValue('subscription.page'));
|
||||
return $this->getSubscriptionUrl($post, 'edit');
|
||||
}
|
||||
|
||||
function getUnsubscribeUrl() {
|
||||
$post = get_post(Setting::getValue('subscription.page'));
|
||||
return $this->getSubscriptionUrl($post, 'unsubscribe');
|
||||
}
|
||||
|
||||
private function getSubscriptionUrl($post = null, $action = null) {
|
||||
if($post === null || $action === null) return;
|
||||
|
||||
$url = get_permalink($post);
|
||||
|
||||
$params = array(
|
||||
'mailpoet_action='.$action,
|
||||
'mailpoet_token='.self::generateToken($this->email),
|
||||
'mailpoet_email='.$this->email
|
||||
);
|
||||
// add parameters
|
||||
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?').join('&', $params);
|
||||
|
||||
$url_params = parse_url($url);
|
||||
if(empty($url_params['scheme'])) {
|
||||
$url = get_bloginfo('url').$url;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
function sendConfirmationEmail() {
|
||||
if($this->status === self::STATUS_UNCONFIRMED) {
|
||||
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||
@ -131,7 +96,7 @@ class Subscriber extends Model {
|
||||
'[/activation_link]'
|
||||
),
|
||||
array(
|
||||
'<a href="'.htmlentities($this->getConfirmationUrl()).'">',
|
||||
'<a href="'.esc_attr(Subscription\Url::getConfirmationUrl($this)).'">',
|
||||
'</a>'
|
||||
),
|
||||
$body
|
||||
|
@ -43,9 +43,12 @@ class Renderer {
|
||||
}
|
||||
|
||||
function renderBody($content) {
|
||||
$content = array_map(function ($content_block) {
|
||||
$content = array_map(function($content_block) {
|
||||
$column_count = count($content_block['blocks']);
|
||||
$column_data = $this->blocks_renderer->render($content_block, $column_count);
|
||||
$column_data = $this->blocks_renderer->render(
|
||||
$content_block,
|
||||
$column_count
|
||||
);
|
||||
return $this->columns_renderer->render(
|
||||
$content_block['styles'],
|
||||
$column_count,
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||
use MailPoet\Subscription;
|
||||
|
||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||
|
||||
@ -18,12 +19,27 @@ class Link {
|
||||
shortcode: 'global:browser',
|
||||
}
|
||||
*/
|
||||
static function process($action) {
|
||||
// TODO: implement
|
||||
static function process(
|
||||
$action,
|
||||
$default_value,
|
||||
$newsletter = false,
|
||||
$subscriber = false
|
||||
) {
|
||||
|
||||
$actions = array(
|
||||
'unsubscribe' => '',
|
||||
'manage' => '',
|
||||
'browser' => ''
|
||||
'unsubscribe' =>
|
||||
'<a
|
||||
target="_blank"
|
||||
href="'.esc_attr(Subscription\Url::getUnsubscribeUrl($subscriber)).'">'.
|
||||
__('Unsubscribe').
|
||||
'</a>',
|
||||
'manage' =>
|
||||
'<a
|
||||
target="_blank"
|
||||
href="'.esc_attr(Subscription\Url::getManageUrl($subscriber)).'">'.
|
||||
__('Manage subscription').
|
||||
'</a>',
|
||||
'browser' => 'TODO'
|
||||
);
|
||||
return (isset($actions[$action])) ? $actions[$action] : false;
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ class User {
|
||||
if($subscriber && $subscriber['wp_user_id']) {
|
||||
$wp_user = get_userdata($subscriber['wp_user_id']);
|
||||
return $wp_user->user_login;
|
||||
};
|
||||
}
|
||||
return $default_value;
|
||||
case 'count':
|
||||
return Subscriber::count();
|
||||
return Subscriber::filter('subscribed')->count();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
@ -23,13 +23,20 @@ class Shortcodes {
|
||||
function process($shortcodes) {
|
||||
$processed_shortcodes = array_map(
|
||||
function ($shortcode) {
|
||||
// TODO: discuss renaming "global". It is a reserved name in PHP.
|
||||
if($shortcode === 'global') $shortcode = 'link';
|
||||
preg_match(
|
||||
'/\[(?P<type>\w+):(?P<action>\w+)(?:.*?default:(?P<default>.*?))?\]/',
|
||||
$shortcode,
|
||||
$shortcode_details
|
||||
);
|
||||
|
||||
// TODO: discuss renaming "global". It is a reserved name in PHP.
|
||||
if(
|
||||
isset($shortcode_details['type'])
|
||||
&& $shortcode_details['type'] === 'global'
|
||||
) {
|
||||
$shortcode_details['type'] = 'link';
|
||||
}
|
||||
|
||||
$shortcode_class =
|
||||
__NAMESPACE__ . '\\Categories\\' . ucfirst($shortcode_details['type']);
|
||||
if(!class_exists($shortcode_class)) return false;
|
||||
@ -41,7 +48,7 @@ class Shortcodes {
|
||||
$this->subscriber
|
||||
);
|
||||
}, $shortcodes);
|
||||
return array_filter($processed_shortcodes);
|
||||
return $processed_shortcodes;
|
||||
}
|
||||
|
||||
function replace() {
|
||||
|
@ -8,6 +8,7 @@ use \MailPoet\Models\Setting;
|
||||
use \MailPoet\Models\Segment;
|
||||
use \MailPoet\Util\Helpers;
|
||||
use \MailPoet\Util\Url;
|
||||
use \MailPoet\Subscription;
|
||||
|
||||
class Pages {
|
||||
const DEMO_EMAIL = 'demo@mailpoet.com';
|
||||
@ -67,8 +68,8 @@ class Pages {
|
||||
return $this->getConfirmTitle($subscriber);
|
||||
break;
|
||||
|
||||
case 'edit':
|
||||
return $this->getEditTitle($subscriber);
|
||||
case 'manage':
|
||||
return $this->getManageTitle($subscriber);
|
||||
break;
|
||||
|
||||
case 'unsubscribe':
|
||||
@ -92,8 +93,8 @@ class Pages {
|
||||
case 'confirm':
|
||||
$content = $this->getConfirmContent($subscriber);
|
||||
break;
|
||||
case 'edit':
|
||||
$content = $this->getEditContent($subscriber);
|
||||
case 'manage':
|
||||
$content = $this->getManageContent($subscriber);
|
||||
break;
|
||||
case 'unsubscribe':
|
||||
$content = $this->getUnsubscribeContent($subscriber);
|
||||
@ -132,7 +133,7 @@ class Pages {
|
||||
return $title;
|
||||
}
|
||||
|
||||
private function getEditTitle($subscriber) {
|
||||
private function getManageTitle($subscriber) {
|
||||
if($this->isPreview()) {
|
||||
return sprintf(
|
||||
__('Edit your subscriber profile: %s'),
|
||||
@ -159,7 +160,7 @@ class Pages {
|
||||
}
|
||||
}
|
||||
|
||||
private function getEditContent($subscriber) {
|
||||
private function getManageContent($subscriber) {
|
||||
if($this->isPreview()) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->hydrate(array(
|
||||
@ -310,7 +311,7 @@ class Pages {
|
||||
$content .= '<p><strong>'.
|
||||
str_replace(
|
||||
array('[link]', '[/link]'),
|
||||
array('<a href="'.$subscriber->getConfirmationUrl().'">', '</a>'),
|
||||
array('<a href="'.Subscription\Url::getConfirmationUrl($subscriber).'">', '</a>'),
|
||||
__('You made a mistake? [link]Undo unsubscribe.[/link]')
|
||||
).
|
||||
'</strong></p>';
|
||||
|
52
lib/Subscription/Url.php
Normal file
52
lib/Subscription/Url.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace MailPoet\Subscription;
|
||||
|
||||
use \MailPoet\Models\Subscriber;
|
||||
use \MailPoet\Models\Setting;
|
||||
|
||||
class Url {
|
||||
static function getConfirmationUrl($subscriber = false) {
|
||||
$post = get_post(Setting::getValue('signup_confirmation.page'));
|
||||
return self::getSubscriptionUrl($post, 'confirm', $subscriber);
|
||||
}
|
||||
|
||||
static function getManageUrl($subscriber = false) {
|
||||
$post = get_post(Setting::getValue('subscription.page'));
|
||||
return self::getSubscriptionUrl($post, 'manage', $subscriber);
|
||||
}
|
||||
|
||||
static function getUnsubscribeUrl($subscriber = false) {
|
||||
$post = get_post(Setting::getValue('subscription.page'));
|
||||
return self::getSubscriptionUrl($post, 'unsubscribe', $subscriber);
|
||||
}
|
||||
|
||||
private static function getSubscriptionUrl(
|
||||
$post = null, $action = null, $subscriber = false
|
||||
) {
|
||||
if($post === null || $action === null) return;
|
||||
|
||||
$url = get_permalink($post);
|
||||
|
||||
if($subscriber !== false) {
|
||||
$params = array(
|
||||
'mailpoet_action='.$action,
|
||||
'mailpoet_token='.Subscriber::generateToken($subscriber->email),
|
||||
'mailpoet_email='.$subscriber->email
|
||||
);
|
||||
} else {
|
||||
$params = array(
|
||||
'mailpoet_action='.$action,
|
||||
'preview'
|
||||
);
|
||||
}
|
||||
// add parameters
|
||||
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?').join('&', $params);
|
||||
|
||||
$url_params = parse_url($url);
|
||||
if(empty($url_params['scheme'])) {
|
||||
$url = get_bloginfo('url').$url;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
use MailPoet\Models\Subscriber;
|
||||
|
||||
class ShortcodesCest {
|
||||
/* public $rendered_newsletter;
|
||||
public $rendered_newsletter;
|
||||
public $newsletter;
|
||||
public $subscriber;
|
||||
|
||||
function __construct() {
|
||||
function _before() {
|
||||
$this->wp_user = $this->_createWPUser();
|
||||
$this->subscriber = $this->_createSubscriber();
|
||||
$this->newsletter['subject'] = 'some subject';
|
||||
@ -30,7 +30,7 @@ class ShortcodesCest {
|
||||
Month text: [date:mtext].
|
||||
Year: [date:y]
|
||||
|
||||
You can usubscribe here: [global:unsubscribe].
|
||||
You can unsubscribe here: [global:unsubscribe].
|
||||
Manage your subscription here: [global:manage].
|
||||
View this newsletter in browser: [global:browser].';
|
||||
$this->shortcodes_object = new MailPoet\Newsletter\Shortcodes\Shortcodes(
|
||||
@ -71,7 +71,7 @@ class ShortcodesCest {
|
||||
Month text: {$date->format('F')}.
|
||||
Year: {$date->format('Y')}
|
||||
|
||||
You can usubscribe here: [global:unsubscribe].
|
||||
You can unsubscribe here: [global:unsubscribe].
|
||||
Manage your subscription here: [global:manage].
|
||||
View this newsletter in browser: [global:browser].");
|
||||
}
|
||||
@ -92,6 +92,7 @@ class ShortcodesCest {
|
||||
'first_name' => 'Donald',
|
||||
'last_name' => 'Trump',
|
||||
'email' => 'mister@trump.com',
|
||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||
'wp_user_id' => $this->wp_user
|
||||
)
|
||||
);
|
||||
@ -100,6 +101,6 @@ class ShortcodesCest {
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||
}*/
|
||||
}
|
||||
Subscriber::deleteMany();
|
||||
}
|
||||
}*/
|
67
tests/unit/Subscription/UrlCest.php
Normal file
67
tests/unit/Subscription/UrlCest.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/*
|
||||
use \MailPoet\Subscription\Url;
|
||||
use \MailPoet\Models\Subscriber;
|
||||
use \MailPoet\Models\Setting;
|
||||
use \MailPoet\Config\Populator;
|
||||
|
||||
class UrlCest {
|
||||
function _before() {
|
||||
$populator = new Populator();
|
||||
$populator->up();
|
||||
}
|
||||
|
||||
function itReturnsTheConfirmationUrl() {
|
||||
// preview
|
||||
$url = Url::getConfirmationUrl(false);
|
||||
expect($url)->contains('mailpoet_action=confirm');
|
||||
expect($url)->contains('preview');
|
||||
|
||||
// actual subscriber
|
||||
$subscriber = Subscriber::createOrUpdate(array(
|
||||
'email' => 'john@mailpoet.com'
|
||||
));
|
||||
$url = Url::getConfirmationUrl($subscriber);
|
||||
expect($url)->contains('mailpoet_action=confirm');
|
||||
expect($url)->contains('mailpoet_token=');
|
||||
expect($url)->contains('mailpoet_email=');
|
||||
}
|
||||
|
||||
function itReturnsTheManageSubscriptionUrl() {
|
||||
// preview
|
||||
$url = Url::getManageUrl(false);
|
||||
expect($url)->contains('mailpoet_action=manage');
|
||||
expect($url)->contains('preview');
|
||||
|
||||
// actual subscriber
|
||||
$subscriber = Subscriber::createOrUpdate(array(
|
||||
'email' => 'john@mailpoet.com'
|
||||
));
|
||||
$url = Url::getManageUrl($subscriber);
|
||||
expect($url)->contains('mailpoet_action=manage');
|
||||
expect($url)->contains('mailpoet_token=');
|
||||
expect($url)->contains('mailpoet_email=');
|
||||
}
|
||||
|
||||
function itReturnsTheUnsubscribeUrl() {
|
||||
// preview
|
||||
$url = Url::getUnsubscribeUrl(false);
|
||||
expect($url)->contains('mailpoet_action=unsubscribe');
|
||||
expect($url)->contains('preview');
|
||||
|
||||
// actual subscriber
|
||||
$subscriber = Subscriber::createOrUpdate(array(
|
||||
'email' => 'john@mailpoet.com'
|
||||
));
|
||||
$url = Url::getUnsubscribeUrl($subscriber);
|
||||
expect($url)->contains('mailpoet_action=unsubscribe');
|
||||
expect($url)->contains('mailpoet_token=');
|
||||
expect($url)->contains('mailpoet_email=');
|
||||
}
|
||||
|
||||
function _after() {
|
||||
Setting::deleteMany();
|
||||
Subscriber::deleteMany();
|
||||
}
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user