bugfix + refactoring
This commit is contained in:
@ -6,6 +6,8 @@ if (!defined('ABSPATH')) exit;
|
||||
class Setting extends Model {
|
||||
public static $_table = MP_SETTINGS_TABLE;
|
||||
|
||||
public static $defaults = null;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
@ -14,8 +16,26 @@ class Setting extends Model {
|
||||
));
|
||||
}
|
||||
|
||||
public static function getDefaults() {
|
||||
if(self::$defaults === null) {
|
||||
self::loadDefaults();
|
||||
}
|
||||
return self::$defaults;
|
||||
}
|
||||
|
||||
public static function loadDefaults() {
|
||||
self::$defaults = array(
|
||||
'signup_confirmation' => array(
|
||||
'enabled' => true,
|
||||
'subject' => sprintf(__('Confirm your subscription to %1$s'), get_option('blogname')),
|
||||
'body' => __("Hello!\n\nHurray! You've subscribed to our site.\nWe need you to activate your subscription to the list(s): [lists_to_confirm] by clicking the link below: \n\n[activation_link]Click here to confirm your subscription.[/activation_link]\n\nThank you,\n\nThe team!")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getValue($key, $default = null) {
|
||||
$keys = explode('.', $key);
|
||||
$defaults = self::getDefaults();
|
||||
|
||||
if(count($keys) === 1) {
|
||||
$setting = Setting::where('name', $key)->findOne();
|
||||
@ -23,9 +43,14 @@ class Setting extends Model {
|
||||
return $default;
|
||||
} else {
|
||||
if(is_serialized($setting->value)) {
|
||||
return unserialize($setting->value);
|
||||
$value = unserialize($setting->value);
|
||||
} else {
|
||||
return $setting->value;
|
||||
$value = $setting->value;
|
||||
}
|
||||
if(is_array($value) && array_key_exists($key, $defaults)) {
|
||||
return array_replace_recursive($defaults[$key], $value);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -93,7 +118,7 @@ class Setting extends Model {
|
||||
$settings[$setting->name] = $value;
|
||||
}
|
||||
}
|
||||
return $settings;
|
||||
return array_replace_recursive(self::getDefaults(), $settings);
|
||||
}
|
||||
|
||||
public static function createOrUpdate($data = array()) {
|
||||
|
@ -115,6 +115,7 @@ class Subscriber extends Model {
|
||||
$segment_names = array_map(function($segment) {
|
||||
return $segment->name;
|
||||
}, $segments);
|
||||
|
||||
$body = nl2br($signup_confirmation['body']);
|
||||
|
||||
// replace list of segments shortcode
|
||||
@ -152,14 +153,14 @@ class Subscriber extends Model {
|
||||
// set from
|
||||
$from = (
|
||||
!empty($signup_confirmation['from'])
|
||||
&& !empty($signup_confirmation['email'])
|
||||
&& !empty($signup_confirmation['from']['email'])
|
||||
) ? $signup_confirmation['from']
|
||||
: false;
|
||||
|
||||
// set reply to
|
||||
$reply_to = (
|
||||
!empty($signup_confirmation['reply_to'])
|
||||
&& !empty($signup_confirmation['reply_to'])
|
||||
&& !empty($signup_confirmation['reply_to']['email'])
|
||||
) ? $signup_confirmation['reply_to']
|
||||
: false;
|
||||
|
||||
@ -168,6 +169,7 @@ class Subscriber extends Model {
|
||||
$mailer = new Mailer(false, $from, $reply_to);
|
||||
return $mailer->send($email, $subscriber);
|
||||
} catch(\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -88,18 +88,10 @@ class Subscribers {
|
||||
}
|
||||
|
||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||
|
||||
$result = false;
|
||||
if($subscriber === false || !$subscriber->id()) {
|
||||
$errors = array_merge($errors, $subscriber->getValidationErrors());
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
|
||||
if(!empty($errors)) {
|
||||
if($subscriber->getErrors() !== false) {
|
||||
return array(
|
||||
'result' => false,
|
||||
'errors' => $errors
|
||||
'errors' => $subscriber->getErrors()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,8 @@ use \MailPoet\Models\Segment;
|
||||
use \MailPoet\Util\Helpers;
|
||||
|
||||
class Pages {
|
||||
const DEMO_EMAIL = 'demo@mailpoet.com';
|
||||
|
||||
function __construct() {
|
||||
|
||||
}
|
||||
@ -24,38 +26,54 @@ class Pages {
|
||||
// TODO
|
||||
}
|
||||
|
||||
function getSubscriber() {
|
||||
$token = (isset($_GET['mailpoet_token']))
|
||||
? $_GET['mailpoet_token']
|
||||
: null;
|
||||
$email = (isset($_GET['mailpoet_email']))
|
||||
? $_GET['mailpoet_email']
|
||||
: null;
|
||||
|
||||
if(empty($token) || empty($email)) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->email = 'demo@mailpoet.com';
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
if(md5(AUTH_KEY.$email) === $token) {
|
||||
$subscriber = Subscriber::findOne($email);
|
||||
if($subscriber !== false) {
|
||||
return $subscriber;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function setPageTitle($title = null) {
|
||||
$subscriber = $this->getSubscriber();
|
||||
|
||||
switch($this->getAction()) {
|
||||
case 'confirm':
|
||||
return $this->getConfirmTitle($subscriber);
|
||||
break;
|
||||
|
||||
case 'edit':
|
||||
return $this->getEditTitle($subscriber);
|
||||
break;
|
||||
|
||||
case 'unsubscribe':
|
||||
if($subscriber !== false && $subscriber->email !== self::DEMO_EMAIL) {
|
||||
if($subscriber->status !== Subscriber::STATUS_UNSUBSCRIBED) {
|
||||
$subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
|
||||
$subscriber->save();
|
||||
}
|
||||
}
|
||||
return $this->getUnsubscribeTitle();
|
||||
break;
|
||||
}
|
||||
return $title;
|
||||
}
|
||||
|
||||
function setPageContent($page_content = '[mailpoet_page]') {
|
||||
$content = '';
|
||||
$subscriber = $this->getSubscriber();
|
||||
|
||||
switch($this->getAction()) {
|
||||
case 'confirm':
|
||||
$content = $this->getConfirmContent($subscriber);
|
||||
break;
|
||||
case 'edit':
|
||||
$content = $this->getEditContent($subscriber);
|
||||
break;
|
||||
case 'unsubscribe':
|
||||
$content = $this->getUnsubscribeContent($subscriber);
|
||||
break;
|
||||
}
|
||||
return str_replace('[mailpoet_page]', $content, $page_content);
|
||||
}
|
||||
|
||||
private function getConfirmTitle($subscriber) {
|
||||
if($subscriber === false) {
|
||||
$title = __('Your confirmation link expired, please subscribe again.');
|
||||
} else {
|
||||
if($subscriber->email === 'demo@mailpoet.com') {
|
||||
if($subscriber->email === self::DEMO_EMAIL) {
|
||||
$segment_names = array('demo 1', 'demo 2');
|
||||
} else {
|
||||
if($subscriber->status !== Subscriber::STATUS_SUBSCRIBED) {
|
||||
@ -77,45 +95,32 @@ class Pages {
|
||||
__("You've subscribed to: %s"),
|
||||
join(', ', $segment_names)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'edit':
|
||||
if($subscriber !== false) {
|
||||
$title = sprintf(
|
||||
__('Edit your subscriber profile: %s'),
|
||||
$subscriber->email
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'unsubscribe':
|
||||
if($subscriber !== false) {
|
||||
if($subscriber->status !== Subscriber::STATUS_UNSUBSCRIBED) {
|
||||
$subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
|
||||
$subscriber->save();
|
||||
}
|
||||
}
|
||||
$title = __("You've unsubscribed!");
|
||||
break;
|
||||
}
|
||||
return $title;
|
||||
}
|
||||
|
||||
function setPageContent($page_content = '[mailpoet_page]') {
|
||||
$content = '';
|
||||
$subscriber = $this->getSubscriber();
|
||||
|
||||
switch($this->getAction()) {
|
||||
case 'confirm':
|
||||
private function getEditTitle($subscriber) {
|
||||
if($subscriber !== false) {
|
||||
$content = __(
|
||||
"Yup, we've added you to our list. ".
|
||||
"You'll hear from us shortly."
|
||||
return sprintf(
|
||||
__('Edit your subscriber profile: %s'),
|
||||
$subscriber->email
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'edit':
|
||||
}
|
||||
|
||||
private function getUnsubscribeTitle() {
|
||||
return __("You've unsubscribed!");
|
||||
}
|
||||
|
||||
|
||||
private function getConfirmContent($subscriber) {
|
||||
if($subscriber !== false) {
|
||||
return __("Yup, we've added you to our list. You'll hear from us shortly.");
|
||||
}
|
||||
}
|
||||
|
||||
private function getEditContent($subscriber) {
|
||||
if($subscriber !== false) {
|
||||
$subscriber = $subscriber
|
||||
->withCustomFields()
|
||||
@ -234,8 +239,9 @@ class Pages {
|
||||
|
||||
$content = \MailPoet\Form\Renderer::renderBlocks($form);
|
||||
}
|
||||
break;
|
||||
case 'unsubscribe':
|
||||
}
|
||||
|
||||
private function getUnsubscribeContent($subscriber) {
|
||||
$content = '<p>'.__("Great, you'll never hear from us again!").'</p>';
|
||||
if($subscriber !== false) {
|
||||
$content .= '<p><strong>'.
|
||||
@ -246,9 +252,30 @@ class Pages {
|
||||
).
|
||||
'</strong></p>';
|
||||
}
|
||||
break;
|
||||
return $content;
|
||||
}
|
||||
return str_replace('[mailpoet_page]', $content, $page_content);
|
||||
|
||||
private function getSubscriber() {
|
||||
$token = (isset($_GET['mailpoet_token']))
|
||||
? $_GET['mailpoet_token']
|
||||
: null;
|
||||
$email = (isset($_GET['mailpoet_email']))
|
||||
? $_GET['mailpoet_email']
|
||||
: null;
|
||||
|
||||
if(empty($token) || empty($email) || $email === self::DEMO_EMAIL) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->email = self::DEMO_EMAIL;
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
if(md5(AUTH_KEY.$email) === $token) {
|
||||
$subscriber = Subscriber::findOne($email);
|
||||
if($subscriber !== false) {
|
||||
return $subscriber;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getAction() {
|
||||
|
@ -19,7 +19,13 @@ class SettingCest {
|
||||
expect($errors[0])->equals('You need to specify a name.');
|
||||
}
|
||||
|
||||
function itCanGetAllSettings() {
|
||||
function itHasDefaultSettings() {
|
||||
$default_settings = Setting::getDefaults();
|
||||
expect($default_settings)->notEmpty();
|
||||
expect($default_settings['signup_confirmation']['enabled'])->true();
|
||||
}
|
||||
|
||||
function itCanGetAllSettingsIncludingDefaults() {
|
||||
Setting::setValue('key_1', 'value_1');
|
||||
Setting::setValue('key_2', 'value_2');
|
||||
Setting::setValue('key_3', array(
|
||||
@ -28,13 +34,17 @@ class SettingCest {
|
||||
));
|
||||
|
||||
$settings = Setting::getAll();
|
||||
expect(array_keys($settings))->count(3);
|
||||
expect($settings['key_1'])->equals('value_1');
|
||||
expect($settings['key_2'])->equals('value_2');
|
||||
expect($settings['key_3'])->equals(array(
|
||||
'subkey_1' => 'subvalue_1',
|
||||
'subkey_2' => 'subvalue_2'
|
||||
));
|
||||
|
||||
// default settings
|
||||
$default_settings = Setting::getDefaults();
|
||||
expect($settings['signup_confirmation'])
|
||||
->equals($default_settings['signup_confirmation']);
|
||||
}
|
||||
|
||||
function itReturnsDefaultValueIfNotSet() {
|
||||
|
@ -16,7 +16,7 @@ class SettingsCest {
|
||||
|
||||
Setting::deleteMany();
|
||||
$settings = $router->get();
|
||||
expect($settings)->isEmpty();
|
||||
expect($settings)->equals(Setting::getDefaults());
|
||||
}
|
||||
|
||||
function itCanSetSettings() {
|
||||
|
@ -112,11 +112,6 @@
|
||||
name="signup_confirmation[subject]"
|
||||
<% if(settings.signup_confirmation.subject) %>
|
||||
value="<%= settings.signup_confirmation.subject %>"
|
||||
<% else %>
|
||||
value="<%=
|
||||
__('Confirm your subscription to %1$s')
|
||||
| format(get_option('blogname'))
|
||||
%>"
|
||||
<% endif %>
|
||||
/>
|
||||
</td>
|
||||
@ -139,8 +134,6 @@
|
||||
name="signup_confirmation[body]"
|
||||
><% if(settings.signup_confirmation.body) %>
|
||||
<%=- settings.signup_confirmation.body -%>
|
||||
<% else %>
|
||||
<%=- __("Hello!\n\nHurray! You've subscribed to our site.\nWe need you to activate your subscription to the list(s): [lists_to_confirm] by clicking the link below: \n\n[activation_link]Click here to confirm your subscription.[/activation_link]\n\nThank you,\n\nThe team!") -%>
|
||||
<% endif %></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
|
Reference in New Issue
Block a user