From 0c5beb25110d271c8e525dea32d28a0dba0006d8 Mon Sep 17 00:00:00 2001
From: Jonathan Labreuille
Date: Thu, 14 Jan 2016 14:11:25 +0100
Subject: [PATCH] Refactor & Improvements - fixed position of checkbox in
comment form - refactored Subscriber::subscribe() method - removed
Form\Subscribe class in favor of Subscription\Comment (I'll create a similar
class for Registration) - added labels in Settings > Basics for Subscribe in
comments & registration - added method in Setting model to check whether
signup confirmation is enabled
---
lib/Config/Hooks.php | 10 ++--
lib/Form/Subscribe.php | 92 ------------------------------------
lib/Models/Setting.php | 12 +++++
lib/Models/Subscriber.php | 60 +++++------------------
lib/Subscription/Comment.php | 91 +++++++++++++++++++++++++++++++++++
views/settings/basics.html | 8 +++-
6 files changed, 127 insertions(+), 146 deletions(-)
delete mode 100644 lib/Form/Subscribe.php
create mode 100644 lib/Subscription/Comment.php
diff --git a/lib/Config/Hooks.php b/lib/Config/Hooks.php
index 6090b900f5..d2cf7ad45c 100644
--- a/lib/Config/Hooks.php
+++ b/lib/Config/Hooks.php
@@ -16,18 +16,20 @@ class Hooks {
&& $subscribe_settings['on_comment']['enabled']
) {
add_action(
- 'comment_form',
- '\MailPoet\Form\Subscribe::inComments'
+ 'comment_form_after_fields',
+ '\MailPoet\Subscription\Comment::extendForm'
);
+
add_action(
'comment_post',
- '\MailPoet\Form\Subscribe::onCommentSubmit',
+ '\MailPoet\Subscription\Comment::onSubmit',
60,
2
);
+
add_action(
'wp_set_comment_status',
- '\MailPoet\Form\Subscribe::onCommentStatusUpdate',
+ '\MailPoet\Subscription\Comment::onStatusUpdate',
60,
2
);
diff --git a/lib/Form/Subscribe.php b/lib/Form/Subscribe.php
deleted file mode 100644
index 3e7fa8f00b..0000000000
--- a/lib/Form/Subscribe.php
+++ /dev/null
@@ -1,92 +0,0 @@
-
-
-
-EOL;
- echo $html;
- }
-
- static function onCommentSubmit($comment_id, $comment_approved) {
- if($comment_approved === 'spam') return;
-
- if(
- isset($_POST['mailpoet']['subscribe_on_comment'])
- &&
- filter_var(
- $_POST['mailpoet']['subscribe_on_comment'],
- FILTER_VALIDATE_BOOLEAN
- ) === true
- ) {
- if($comment_approved === 0) {
- add_comment_meta(
- $comment_id,
- 'mailpoet',
- 'subscribe_on_comment',
- true
- );
- } else {
- $subscribe_settings = Setting::getValue('subscribe');
- $segment_ids = (array)$subscribe_settings['on_comment']['segments'];
-
- if($subscribe_settings !== null) {
- $comment = get_comment($comment_id);
-
- $result = Subscriber::subscribe(
- array(
- 'email' => $comment->comment_author_email,
- 'first_name' => $comment->comment_author
- ),
- $segment_ids
- );
- }
- }
- }
- }
-
- static function onCommentStatusUpdate($comment_id, $comment_status) {
- $comment_meta = get_comment_meta(
- $comment_id,
- 'mailpoet',
- true
- );
-
- if(
- $comment_status ==='approve'
- && $comment_meta === 'subscribe_on_comment'
- ) {
- $subscribe_settings = Setting::getValue('subscribe');
- $segment_ids = (array)$subscribe_settings['on_comment']['segments'];
-
- if($subscribe_settings !== null) {
- $comment = get_comment($comment_id);
-
- Subscriber::subscribe(
- array(
- 'email' => $comment->comment_author_email,
- 'first_name' => $comment->comment_author
- ),
- $segment_ids
- );
- }
- }
- }
-}
\ No newline at end of file
diff --git a/lib/Models/Setting.php b/lib/Models/Setting.php
index 43a1bda78c..93b6cc3eb6 100644
--- a/lib/Models/Setting.php
+++ b/lib/Models/Setting.php
@@ -67,4 +67,16 @@ class Setting extends Model {
$exists->value = $model['value'];
return $exists->save();
}
+
+ public static function hasSignupConfirmation() {
+ $signup_confirmation = Setting::getValue('signup_confirmation', array());
+ $has_signup_confirmation = true;
+ if(array_key_exists('enabled', $signup_confirmation)) {
+ $has_signup_confirmation = filter_var(
+ $signup_confirmation['enabled'],
+ FILTER_VALIDATE_BOOLEAN
+ );
+ }
+ return $has_signup_confirmation;
+ }
}
diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php
index 55256782c5..b81f1bbd50 100644
--- a/lib/Models/Subscriber.php
+++ b/lib/Models/Subscriber.php
@@ -42,6 +42,12 @@ class Subscriber extends Model {
}
}
+ function sendConfirmationEmail() {
+ $subscriber->set('status', 'unconfirmed');
+
+ // TODO
+ }
+
static function subscribe($subscriber_data = array(), $segment_ids = array()) {
if(empty($subscriber_data) or empty($segment_ids)) {
return false;
@@ -50,34 +56,19 @@ class Subscriber extends Model {
$subscriber = static::createOrUpdate($subscriber_data);
if($subscriber !== false && $subscriber->id() > 0) {
- $signup_confirmation = Setting::getValue('signup_confirmation', array());
- $has_signup_confirmation = true;
- if(array_key_exists('enabled', $signup_confirmation)) {
- $has_signup_confirmation = filter_var(
- $signup_confirmation['enabled'],
- FILTER_VALIDATE_BOOLEAN
- );
- }
-
// restore deleted subscriber
if($subscriber->deleted_at !== NULL) {
$subscriber->setExpr('deleted_at', 'NULL');
}
- if($has_signup_confirmation === false) {
- // auto subscribe when signup confirmation is turned off
- $subscriber->set('status', 'subscribed');
- } else {
+ if(Setting::hasSignupConfirmation()) {
// reset status of existing subscribers if signup confirmation
// is turned on
if($subscriber->status !== 'subscribed') {
- $subscriber->set('status', 'unconfirmed');
- }
-
- // send confirmation email to unconfirmed subscribers
- if($subscriber->status === 'unconfirmed') {
- // TODO: send signup confirmation email
+ $subscriber->sendConfirmationEmail();
}
+ } else {
+ $subscriber->set('status', 'subscribed');
}
if($subscriber->save()) {
@@ -255,34 +246,6 @@ class Subscriber extends Model {
$subscriber->set($data);
}
- // TODO: Cf
- /*
- // custom fields
- $custom_fields = array();
- foreach($data as $key => $value) {
- if(strpos($key, 'cf_') === 0) {
- $custom_fields[substr($key, 3)] = $value;
- unset($data[$key]);
- }
- }
-
- // add custom fields
- if(!empty($custom_fields)) {
- foreach($custom_fields as $custom_field_id => $value) {
- if(is_array($value)) {
- // date
- $value = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
- }
- $subscriber_custom_field = SubscriberCustomField::create();
- $subscriber_custom_field->hydrate(array(
- 'subscriber_id' => $subscriber->id(),
- 'custom_field_id' => $custom_field_id,
- 'value' => $value
- ));
- $subscriber_custom_field->save();
- }
- }*/
-
$subscriber->save();
return $subscriber;
}
@@ -364,8 +327,7 @@ class Subscriber extends Model {
if(!empty($subscribers)) {
foreach($subscribers as $subscriber) {
- // TODO: send confirmation email
- // $subscriber->sendConfirmationEmail()
+ $subscriber->sendConfirmationEmail();
}
return $subscribers->count();
diff --git a/lib/Subscription/Comment.php b/lib/Subscription/Comment.php
new file mode 100644
index 0000000000..4bacfe0f85
--- /dev/null
+++ b/lib/Subscription/Comment.php
@@ -0,0 +1,91 @@
+
+
+ ';
+ }
+
+ static function onSubmit($comment_id, $comment_status) {
+ if($comment_status === Comment::SPAM) return;
+
+ if(
+ isset($_POST['mailpoet']['subscribe_on_comment'])
+ &&
+ filter_var(
+ $_POST['mailpoet']['subscribe_on_comment'],
+ FILTER_VALIDATE_BOOLEAN
+ ) === true
+ ) {
+ if($comment_status === Comment::PENDING_APPROVAL) {
+ // add a comment meta to remember to subscribe the user
+ // once the comment gets approved
+ add_comment_meta(
+ $comment_id,
+ 'mailpoet',
+ 'subscribe_on_comment',
+ true
+ );
+ } else if($comment_status === Comment::APPROVED) {
+ static::subscribeAuthorOfComment($comment_id);
+ }
+ }
+ }
+
+ static function onStatusUpdate($comment_id, $action) {
+ if($action === 'approve') {
+ // check if the comment's author wants to subscribe
+ $do_subscribe = (
+ get_comment_meta(
+ $comment_id,
+ 'mailpoet',
+ true
+ ) === 'subscribe_on_comment'
+ );
+
+ if($do_subscribe === true) {
+ static::subscribeAuthorOfComment($comment_id);
+
+ delete_comment_meta($comment_id, 'mailpoet');
+ }
+ }
+ }
+
+ private static function subscribeAuthorOfComment($comment_id) {
+ $subscribe_settings = Setting::getValue('subscribe');
+ $segment_ids = (array)$subscribe_settings['on_comment']['segments'];
+
+ if($subscribe_settings !== null) {
+ $comment = get_comment($comment_id);
+
+ $result = Subscriber::subscribe(
+ array(
+ 'email' => $comment->comment_author_email,
+ 'first_name' => $comment->comment_author
+ ),
+ $segment_ids
+ );
+ }
+ }
+}
\ No newline at end of file
diff --git a/views/settings/basics.html b/views/settings/basics.html
index 24d73e89c5..e40d98b35f 100644
--- a/views/settings/basics.html
+++ b/views/settings/basics.html
@@ -102,6 +102,9 @@
<% endif %>
/>
+
+
+
+
+
+
-
+