Subscribe in comments
- added Subscriber::subscribe($user, $segment_ids) - refactored Router\Subscribers->subscribe() method to account for new method - added Form\Subscribe class to handle subscription in comments - updated Basics settings page (changed "list" to "segment")
This commit is contained in:
@@ -19,6 +19,8 @@ a:focus
|
||||
|
||||
// select 2
|
||||
.select2-container
|
||||
width: 25em !important
|
||||
|
||||
// textareas
|
||||
textarea.regular-text
|
||||
width: 25em !important
|
||||
|
@@ -1,11 +1,39 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
use \MailPoet\Models\Setting;
|
||||
|
||||
class Hooks {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function init() {
|
||||
$subscribe_settings = Setting::getValue('subscribe');
|
||||
|
||||
if($subscribe_settings !== null) {
|
||||
// Subscribe in comments
|
||||
if(
|
||||
isset($subscribe_settings['on_comment']['enabled'])
|
||||
&& $subscribe_settings['on_comment']['enabled']
|
||||
) {
|
||||
add_action(
|
||||
'comment_form',
|
||||
'\MailPoet\Form\Subscribe::inComments'
|
||||
);
|
||||
add_action(
|
||||
'comment_post',
|
||||
'\MailPoet\Form\Subscribe::onCommentSubmit',
|
||||
60,
|
||||
2
|
||||
);
|
||||
add_action(
|
||||
'wp_set_comment_status',
|
||||
'\MailPoet\Form\Subscribe::onCommentStatusUpdate',
|
||||
60,
|
||||
2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// WP Users synchronization
|
||||
add_action(
|
||||
'user_register',
|
||||
|
@@ -240,6 +240,7 @@ class Menu {
|
||||
|
||||
function settings() {
|
||||
$settings = Setting::getAll();
|
||||
$flags = $this->_getFlags();
|
||||
|
||||
// dkim: check if public/private keys have been generated
|
||||
if(
|
||||
@@ -258,10 +259,9 @@ class Menu {
|
||||
|
||||
$data = array(
|
||||
'settings' => $settings,
|
||||
'segments' => Segment::getPublished()
|
||||
->findArray(),
|
||||
'segments' => Segment::getPublic()->findArray(),
|
||||
'pages' => Pages::getAll(),
|
||||
'flags' => $this->_getFlags(),
|
||||
'flags' => $flags,
|
||||
'charsets' => Charsets::getAll(),
|
||||
'current_user' => wp_get_current_user(),
|
||||
'permissions' => Permissions::getAll(),
|
||||
|
92
lib/Form/Subscribe.php
Normal file
92
lib/Form/Subscribe.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
namespace MailPoet\Form;
|
||||
use \MailPoet\Models\Setting;
|
||||
use \MailPoet\Models\Subscriber;
|
||||
|
||||
class Subscribe {
|
||||
static function inComments() {
|
||||
$label = (isset($subscribe_settings['on_comment']['label'])
|
||||
? $subscribe_settings['on_comment']['label']
|
||||
: __('Yes, add me to your mailing list.')
|
||||
);
|
||||
|
||||
$html = <<<EOL
|
||||
<p class="comment-form-mailpoet">
|
||||
<label for="mailpoet_subscribe_on_comment">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="mailpoet_subscribe_on_comment"
|
||||
value="1"
|
||||
name="mailpoet[subscribe_on_comment]"
|
||||
/> $label
|
||||
</label>
|
||||
</p>
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -32,6 +32,65 @@ class Subscriber extends Model {
|
||||
return parent::delete();
|
||||
}
|
||||
|
||||
function addToSegments(array $segment_ids = array()) {
|
||||
$segments = Segment::whereIn('id', $segment_ids)->findMany();
|
||||
foreach($segments as $segment) {
|
||||
$association = SubscriberSegment::create();
|
||||
$association->subscriber_id = $this->id;
|
||||
$association->segment_id = $segment->id;
|
||||
$association->save();
|
||||
}
|
||||
}
|
||||
|
||||
static function subscribe($subscriber_data = array(), $segment_ids = array()) {
|
||||
if(empty($subscriber_data) or empty($segment_ids)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$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 {
|
||||
// reset status of existing subscribers if signup confirmation
|
||||
// is turned on
|
||||
if($subscriber->isNew() === false) {
|
||||
// existing subscriber
|
||||
if($subscriber->status !== 'subscribed') {
|
||||
$subscriber->set('status', 'unconfirmed');
|
||||
}
|
||||
}
|
||||
|
||||
// send confirmation email to unconfirmed subscribers
|
||||
if($subscriber->status === 'unconfirmed') {
|
||||
// TODO: send signup confirmation email
|
||||
}
|
||||
}
|
||||
|
||||
if($subscriber->save()) {
|
||||
$subscriber->addToSegments($segment_ids);
|
||||
}
|
||||
}
|
||||
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
static function search($orm, $search = '') {
|
||||
if(strlen(trim($search) === 0)) {
|
||||
return $orm;
|
||||
@@ -181,17 +240,52 @@ class Subscriber extends Model {
|
||||
$subscriber = false;
|
||||
|
||||
if(isset($data['id']) && (int)$data['id'] > 0) {
|
||||
$subscriber = self::findOne((int)$data['id']);
|
||||
$subscriber = static::findOne((int)$data['id']);
|
||||
unset($data['id']);
|
||||
}
|
||||
|
||||
if($subscriber === false && !empty($data['email'])) {
|
||||
$subscriber = static::where('email', $data['email'])->findOne();
|
||||
if($subscriber !== false) {
|
||||
unset($data['email']);
|
||||
}
|
||||
}
|
||||
|
||||
if($subscriber === false) {
|
||||
$subscriber = self::create();
|
||||
$subscriber = static::create();
|
||||
$subscriber->hydrate($data);
|
||||
} else {
|
||||
$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;
|
||||
}
|
||||
|
@@ -68,10 +68,10 @@ class Subscribers {
|
||||
function save($data = array()) {
|
||||
$errors = array();
|
||||
$result = false;
|
||||
$segments = false;
|
||||
$segment_ids = array();
|
||||
|
||||
if(array_key_exists('segments', $data)) {
|
||||
$segments = $data['segments'];
|
||||
$segment_ids = (array)$data['segments'];
|
||||
unset($data['segments']);
|
||||
}
|
||||
|
||||
@@ -82,18 +82,8 @@ class Subscribers {
|
||||
} else {
|
||||
$result = true;
|
||||
|
||||
if($segments !== false) {
|
||||
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||
->deleteMany();
|
||||
|
||||
if(!empty($segments)) {
|
||||
foreach($segments as $segment_id) {
|
||||
$relation = SubscriberSegment::create();
|
||||
$relation->segment_id = $segment_id;
|
||||
$relation->subscriber_id = $subscriber->id;
|
||||
$relation->save();
|
||||
}
|
||||
}
|
||||
if(!empty($segment_ids)) {
|
||||
$subscriber->addToSegments($segment_ids);
|
||||
}
|
||||
}
|
||||
wp_send_json(array(
|
||||
@@ -112,109 +102,31 @@ class Subscribers {
|
||||
$errors[] = __('This form does not exist.');
|
||||
}
|
||||
|
||||
if(empty($data['segments'])) {
|
||||
$errors[] = __('You need to select a list');
|
||||
} else {
|
||||
$segments = Segment::whereIn('id', (array)$data['segments'])->findMany();
|
||||
|
||||
if(empty($segments)) {
|
||||
$errors[] = __('You need to select a list');
|
||||
}
|
||||
}
|
||||
$segment_ids = (!empty($data['segments'])
|
||||
? (array)$data['segments']
|
||||
: array()
|
||||
);
|
||||
unset($data['segments']);
|
||||
|
||||
$subscriber = false;
|
||||
if(empty($segment_ids)) {
|
||||
$errors[] = __('You need to select a list');
|
||||
}
|
||||
|
||||
if(!empty($errors)) {
|
||||
wp_send_json(array('errors' => $errors));
|
||||
} else {
|
||||
if(!empty($data['email'])) {
|
||||
$subscriber = Subscriber::where('email', $data['email'])->findOne();
|
||||
}
|
||||
}
|
||||
|
||||
$signup_confirmation = Setting::getValue('signup_confirmation', array());
|
||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||
|
||||
if($subscriber === false) {
|
||||
// create new subscriber
|
||||
$data['status'] = (
|
||||
(!empty($signup_confirmation['enabled']))
|
||||
? 'unconfirmed' : 'subscribed'
|
||||
);
|
||||
if($subscriber === false || !$subscriber->id()) {
|
||||
$errors = array_merge($errors, $subscriber->getValidationErrors());
|
||||
}
|
||||
|
||||
// custom fields
|
||||
$custom_fields = array();
|
||||
foreach($data as $key => $value) {
|
||||
if(strpos($key, 'cf_') === 0) {
|
||||
$custom_fields[substr($key, 3)] = $value;
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// insert new subscriber
|
||||
$subscriber = Subscriber::createOrUpdate($data);
|
||||
|
||||
if($subscriber === false || !$subscriber->id()) {
|
||||
$errors = array_merge($errors, $subscriber->getValidationErrors());
|
||||
} else {
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$subscriber->set('status', (
|
||||
!empty($signup_confirmation['enabled'])
|
||||
? 'unconfirmed' : 'subscribed'
|
||||
if(!empty($errors)) {
|
||||
wp_send_json(array(
|
||||
'result' => false,
|
||||
'errors' => $errors
|
||||
));
|
||||
|
||||
// restore deleted subscriber
|
||||
if($subscriber->deleted_at !== NULL) {
|
||||
$subscriber->setExpr('deleted_at', 'NULL');
|
||||
}
|
||||
|
||||
if(!$subscriber->save()) {
|
||||
$errors[] = __('An error occurred. Please try again later.');
|
||||
}
|
||||
}
|
||||
|
||||
// get segments
|
||||
// IDEA: $subscriptions->addToSegments($data['segments']);
|
||||
$segments_subscribed = array();
|
||||
foreach($segments as $segment) {
|
||||
if($segment->addSubscriber($subscriber->id())) {
|
||||
$segments_subscribed[] = $segment->id;
|
||||
}
|
||||
}
|
||||
|
||||
// if signup confirmation is enabled and the subscriber is unconfirmed
|
||||
if(!empty($signup_confirmation['enabled'])
|
||||
&& !empty($segments_subscribed)
|
||||
&& $subscriber->status !== 'subscribed'
|
||||
) {
|
||||
// TODO: send confirmation email
|
||||
// resend confirmation email
|
||||
$is_sent = true;
|
||||
/*$is_sent = static::sendSignupConfirmation(
|
||||
$subscriber->asArray(),
|
||||
$segments->asArray()
|
||||
);*/
|
||||
|
||||
// error message if the email could not be sent
|
||||
if($is_sent === false) {
|
||||
$errors[] = __('The signup confirmation email could not be sent. Please check your settings.');
|
||||
}
|
||||
}
|
||||
|
||||
// get success message to display after subscription
|
||||
@@ -223,15 +135,6 @@ class Subscribers {
|
||||
? unserialize($form->settings) : null
|
||||
);
|
||||
|
||||
if(!empty($errors)) {
|
||||
wp_send_json(array(
|
||||
'result' => false,
|
||||
'errors' => $errors
|
||||
));
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
|
||||
if($form_settings !== null) {
|
||||
$message = $form_settings['success_message'];
|
||||
|
||||
@@ -274,7 +177,7 @@ class Subscribers {
|
||||
// response depending on context
|
||||
if($doing_ajax === true) {
|
||||
wp_send_json(array(
|
||||
'result' => $result,
|
||||
'result' => true,
|
||||
'message' => $message
|
||||
));
|
||||
} else {
|
||||
|
@@ -104,15 +104,15 @@
|
||||
</p>
|
||||
<p>
|
||||
<select
|
||||
id="mailpoet_subscribe_on_comment_lists"
|
||||
name="subscribe[on_comment][lists][]"
|
||||
id="mailpoet_subscribe_on_comment_segments"
|
||||
name="subscribe[on_comment][segments][]"
|
||||
placeholder="<%= __('Choose a list') %>"
|
||||
multiple
|
||||
>
|
||||
<% for segment in segments %>
|
||||
<option
|
||||
value="<%= segment.id %>"
|
||||
<% if(segment.id in settings.subscribe.on_comment.lists) %>
|
||||
<% if(segment.id in settings.subscribe.on_comment.segments) %>
|
||||
selected="selected"
|
||||
<% endif %>
|
||||
><%= segment.name %></option>
|
||||
@@ -165,15 +165,15 @@
|
||||
</p>
|
||||
<p>
|
||||
<select
|
||||
id="mailpoet_subscribe_on_register_lists"
|
||||
name="subscribe[on_register][lists][]"
|
||||
id="mailpoet_subscribe_on_register_segments"
|
||||
name="subscribe[on_register][segments][]"
|
||||
placeholder="<%= __('Choose a list') %>"
|
||||
multiple
|
||||
>
|
||||
<% for segment in segments %>
|
||||
<option
|
||||
value="<%= segment.id %>"
|
||||
<% if(segment.id in settings.subscribe.on_register.lists) %>
|
||||
<% if(segment.id in settings.subscribe.on_register.segments) %>
|
||||
selected="selected"
|
||||
<% endif %>
|
||||
><%= segment.name %></option>
|
||||
@@ -231,15 +231,15 @@
|
||||
</p>
|
||||
<p>
|
||||
<select
|
||||
id="mailpoet_subscription_edit_lists"
|
||||
name="subscription[lists][]"
|
||||
id="mailpoet_subscription_edit_segments"
|
||||
name="subscription[segments][]"
|
||||
placeholder="<%= __('Leave empty to show all lists') %>"
|
||||
multiple
|
||||
>
|
||||
<% for segment in segments %>
|
||||
<option
|
||||
value="<%= segment.id %>"
|
||||
<% if(segment.id in settings.subscription.lists) %>
|
||||
<% if(segment.id in settings.subscription.segments) %>
|
||||
selected="selected"
|
||||
<% endif %>
|
||||
><%= segment.name %></option>
|
||||
@@ -307,7 +307,7 @@
|
||||
</p>
|
||||
<p>
|
||||
<select
|
||||
id="mailpoet_shortcode_subscribers_list"
|
||||
id="mailpoet_shortcode_subscribers_count"
|
||||
data-shortcode="mailpoet_subscribers_count"
|
||||
data-output="mailpoet_shortcode_subscribers"
|
||||
placeholder="<%= __('Leave empty to show all lists') %>"
|
||||
@@ -328,25 +328,26 @@
|
||||
// on dom loaded
|
||||
$(function() {
|
||||
// select2 instances
|
||||
$('#mailpoet_subscribe_on_comment_lists').select2();
|
||||
$('#mailpoet_subscribe_on_register_lists').select2();
|
||||
$('#mailpoet_subscription_edit_lists').select2();
|
||||
$('#mailpoet_subscribe_on_comment_segments').select2();
|
||||
$('#mailpoet_subscribe_on_register_segments').select2();
|
||||
$('#mailpoet_subscription_edit_segments').select2();
|
||||
$('#mailpoet_shortcode_archives_list').select2();
|
||||
$('#mailpoet_shortcode_subscribers_list').select2();
|
||||
$('#mailpoet_shortcode_subscribers_count').select2();
|
||||
|
||||
// shortcodes
|
||||
$('#mailpoet_shortcode_archives_list, #mailpoet_shortcode_subscribers_list')
|
||||
$('#mailpoet_shortcode_archives_list, #mailpoet_shortcode_subscribers_count')
|
||||
.on('change', function() {
|
||||
var shortcode = $(this).data('shortcode'),
|
||||
values = $(this).val() || [];
|
||||
|
||||
if(values.length > 0) {
|
||||
if (values.length > 0) {
|
||||
shortcode += ' list_id="';
|
||||
shortcode += values.join(',');
|
||||
shortcode += '"';
|
||||
}
|
||||
|
||||
$('#' + $(this).data('output')).val('[' + shortcode + ']');
|
||||
$('#' + $(this).data('output'))
|
||||
.val('[' + shortcode + ']');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user