diff --git a/assets/js/src/subscribers/list.jsx b/assets/js/src/subscribers/list.jsx index 015b919030..3830388332 100644 --- a/assets/js/src/subscribers/list.jsx +++ b/assets/js/src/subscribers/list.jsx @@ -208,6 +208,16 @@ const bulk_actions = [ ); } }, + { + name: 'sendConfirmationEmail', + label: 'Resend confirmation email', + onSuccess: function(response) { + MailPoet.Notice.success( + '%$1d confirmation emails have been sent.' + .replace('%$1d', ~~response) + ); + } + }, { name: 'trash', label: 'Trash', @@ -272,13 +282,13 @@ const SubscriberList = React.createClass({ subscriber.subscriptions.map((subscription) => { const segment = this.getSegmentFromId(subscription.segment_id); + if(segment === false) return; if (subscription.status === 'subscribed') { subscribed_segments.push(segment.name); - } else { + } else if (subscription.status === 'unsubscribed') { unsubscribed_segments.push(segment.name); } }); - segments = ( diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index 963eae0dc2..77a01cb58e 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -1,12 +1,17 @@ findOne(); + } + } + function segments() { return $this->has_many_through( __NAMESPACE__.'\Segment', __NAMESPACE__.'\SubscriberSegment', 'subscriber_id', 'segment_id' - )->where(MP_SUBSCRIBER_SEGMENT_TABLE.'.status', 'subscribed'); + ) + ->where(MP_SUBSCRIBER_SEGMENT_TABLE.'.status', self::STATUS_SUBSCRIBED); } function delete() { @@ -56,10 +70,99 @@ class Subscriber extends Model { } } - function sendConfirmationEmail() { - $this->set('status', 'unconfirmed'); + function getConfirmationUrl() { + $post = get_post(Setting::getValue('signup_confirmation.page')); - // TODO + if($post === null) { + // create page + return ''; + } else { + $url = get_permalink($post); + + $params = array( + 'mailpoet_action=confirm', + 'mailpoet_token='.md5(AUTH_KEY.$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'); + + $segments = $this->segments()->findMany(); + $segment_names = array_map(function($segment) { + return $segment->name; + }, $segments); + $body = nl2br($signup_confirmation['body']); + + // replace list of segments shortcode + $body = str_replace( + '[lists_to_confirm]', + ''.join(', ', $segment_names).'', + $body + ); + + // replace activation link + $body = str_replace( + array( + '[activation_link]', + '[/activation_link]' + ), + array( + '', + '' + ), + $body + ); + + // build email data + $email = array( + 'subject' => $signup_confirmation['subject'], + 'body' => array( + 'html' => $body, + 'text' => $body + ) + ); + + // convert subsdriber to array + $subscriber = $this->asArray(); + + // set from + $from = (!empty($signup_confirmation['from']) + ? $signup_confirmation['from'] + : false + ); + + // set reply to + $reply_to = (!empty($signup_confirmation['reply_to']) + ? $signup_confirmation['reply_to'] + : false + ); + + // send email + $mailer = new Mailer( + false, + $from, + $reply_to + ); + print '
';
+      print_r($mailer);
+      print '
'; + exit(); + return $mailer->send($email, $subscriber); + } + return false; } static function subscribe($subscriber_data = array(), $segment_ids = array()) { @@ -76,11 +179,11 @@ class Subscriber extends Model { } if((bool)Setting::getValue('signup_confirmation.enabled')) { - if($subscriber->status !== 'subscribed') { + if($subscriber->status !== self::STATUS_SUBSCRIBED) { $subscriber->sendConfirmationEmail(); } } else { - $subscriber->set('status', 'subscribed'); + $subscriber->set('status', self::STATUS_SUBSCRIBED); } if($subscriber->save()) { @@ -162,19 +265,19 @@ class Subscriber extends Model { 'count' => self::getPublished()->count() ), array( - 'name' => 'subscribed', + 'name' => self::STATUS_SUBSCRIBED, 'label' => __('Subscribed'), - 'count' => self::filter('subscribed')->count() + 'count' => self::filter(self::STATUS_SUBSCRIBED)->count() ), array( - 'name' => 'unconfirmed', + 'name' => self::STATUS_UNCONFIRMED, 'label' => __('Unconfirmed'), - 'count' => self::filter('unconfirmed')->count() + 'count' => self::filter(self::STATUS_UNCONFIRMED)->count() ), array( - 'name' => 'unsubscribed', + 'name' => self::STATUS_UNSUBSCRIBED, 'label' => __('Unsubscribed'), - 'count' => self::filter('unsubscribed')->count() + 'count' => self::filter(self::STATUS_UNSUBSCRIBED)->count() ), array( 'name' => 'trash', @@ -413,21 +516,23 @@ class Subscriber extends Model { static function bulkConfirmUnconfirmed($orm) { $subscribers = $orm->findResultSet(); - $subscribers->set('status', 'subscribed')->save(); + $subscribers->set('status', self::STATUS_SUBSCRIBED)->save(); return $subscribers->count(); } - static function bulkResendConfirmationEmail($orm) { + static function bulkSendConfirmationEmail($orm) { $subscribers = $orm - ->where('status', 'unconfirmed') - ->findResultSet(); + ->where('status', self::STATUS_UNCONFIRMED) + ->findMany(); + $emails_sent = 0; if(!empty($subscribers)) { foreach($subscribers as $subscriber) { - $subscriber->sendConfirmationEmail(); + if($subscriber->sendConfirmationEmail()) { + $emails_sent++; + } } - - return $subscribers->count(); + return $emails_sent; } return false; } @@ -468,19 +573,19 @@ class Subscriber extends Model { static function subscribed($orm) { return $orm ->whereNull('deleted_at') - ->where('status', 'subscribed'); + ->where('status', self::STATUS_SUBSCRIBED); } static function unsubscribed($orm) { return $orm ->whereNull('deleted_at') - ->where('status', 'unsubscribed'); + ->where('status', self::STATUS_UNSUBSCRIBED); } static function unconfirmed($orm) { return $orm ->whereNull('deleted_at') - ->where('status', 'unconfirmed'); + ->where('status', self::STATUS_UNCONFIRMED); } static function withoutSegments($orm) { diff --git a/lib/Router/Router.php b/lib/Router/Router.php index 3549080802..da7729f582 100644 --- a/lib/Router/Router.php +++ b/lib/Router/Router.php @@ -1,6 +1,7 @@ setupPublic(); } function setup() { @@ -36,6 +38,84 @@ class Router { } } + function setupPublic() { + if(isset($_GET['mailpoet_page'])) { + $mailpoet_page = $_GET['mailpoet_page']; + + add_filter('wp_title', array($this,'setWindowTitle')); + add_filter('the_title', array($this,'setPageTitle')); + add_filter('the_content', array($this,'setPageContent')); + } + } + + function setWindowTitle() { + + } + + function setPageTitle($title) { + $action = (isset($_GET['mailpoet_action'])) + ? $_GET['mailpoet_action'] + : null; + + switch($action) { + case 'confirm': + $token = (isset($_GET['mailpoet_token'])) + ? $_GET['mailpoet_token'] + : null; + $email = (isset($_GET['mailpoet_email'])) + ? $_GET['mailpoet_email'] + : null; + + if(empty($token) || empty($token)) { + $title = sprintf( + __("You've subscribed to: %s"), + 'demo' + ); + } else { + // check token validity + if(md5(AUTH_KEY.$email) !== $token) { + $title = __('Your confirmation link expired, please subscribe again.'); + } else { + $subscriber = Subscriber::findOne($email); + if($subscriber !== false) { + if($subscriber->status !== Subscriber::STATUS_SUBSCRIBED) { + $subscriber->status = Subscriber::STATUS_SUBSCRIBED; + $subscriber->save(); + } + + $segments = $subscriber->segments()->findMany(); + + $segment_names = array_map(function($segment) { + return $segment->name; + }, $segments); + + $title = sprintf( + __("You've subscribed to: %s"), + join(', ', $segment_names) + ); + } + } + } + break; + case 'manage': + // TODO + break; + case 'unsubscribe': + // TODO + break; + } + return $title; + } + + function setPageContent($content) { + + return __( + "Yup, we've added you to our list. ". + "You'll hear from us shortly." + ); + + } + function setToken() { $global = '