Merge pull request #694 from mailpoet/newsletter_number_shortcode_fix

Newsletter number shortcode fix
This commit is contained in:
Tautvidas Sipavičius
2016-11-07 14:03:01 +02:00
committed by GitHub
9 changed files with 71 additions and 46 deletions

View File

@@ -68,7 +68,7 @@ const NewsletterListNotificationHistory = React.createClass({
<a <a
href={ newsletter.preview_url } href={ newsletter.preview_url }
target="_blank" target="_blank"
>{ newsletter.subject }</a> >{ newsletter.queue.newsletter_rendered_subject }</a>
</strong> </strong>
{ actions } { actions }
</td> </td>

View File

@@ -25,8 +25,7 @@ class Newsletter {
if(!$newsletter) { if(!$newsletter) {
return false; return false;
} }
// if the newsletter was previously rendered, return it // return the newsletter if it was previously rendered
// otherwise, process/render it
if(!is_null($queue->getNewsletterRenderedBody())) { if(!is_null($queue->getNewsletterRenderedBody())) {
return $newsletter; return $newsletter;
} }
@@ -34,7 +33,7 @@ class Newsletter {
if($this->tracking_enabled) { if($this->tracking_enabled) {
// hook to the newsletter post-processing filter and add tracking image // hook to the newsletter post-processing filter and add tracking image
$this->tracking_image_inserted = OpenTracking::addTrackingImage(); $this->tracking_image_inserted = OpenTracking::addTrackingImage();
// render newsletter and save its // render newsletter
$rendered_newsletter = $newsletter->render(); $rendered_newsletter = $newsletter->render();
// hash and save all links // hash and save all links
$rendered_newsletter = LinksTask::process($rendered_newsletter, $newsletter, $queue); $rendered_newsletter = LinksTask::process($rendered_newsletter, $newsletter, $queue);

View File

@@ -14,4 +14,4 @@ class Date {
); );
return (isset($actions[$action])) ? $actions[$action] : false; return (isset($actions[$action])) ? $actions[$action] : false;
} }
} }

View File

@@ -125,4 +125,4 @@ class Link {
private static function getShortcode($action) { private static function getShortcode($action) {
return sprintf('[link:%s]', $action); return sprintf('[link:%s]', $action);
} }
} }

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace MailPoet\Newsletter\Shortcodes\Categories; namespace MailPoet\Newsletter\Shortcodes\Categories;
use MailPoet\Models\SendingQueue;
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; use MailPoet\Models\Newsletter as NewsletterModel;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
require_once( ABSPATH . "wp-includes/pluggable.php" ); require_once(ABSPATH . "wp-includes/pluggable.php");
class Newsletter { class Newsletter {
static function process($action, static function process($action,
@@ -16,7 +16,7 @@ class Newsletter {
) { ) {
switch($action) { switch($action) {
case 'subject': case 'subject':
return ($newsletter) ? $newsletter['subject'] : false; return ($newsletter) ? $newsletter->subject : false;
case 'total': case 'total':
return substr_count($content, 'data-post-id'); return substr_count($content, 'data-post-id');
@@ -28,10 +28,10 @@ class Newsletter {
return ($latest_post) ? $latest_post['post_title'] : false; return ($latest_post) ? $latest_post['post_title'] : false;
case 'number': case 'number':
if($newsletter['type'] !== 'notification') return false; if($newsletter->type !== NewsletterModel::TYPE_NOTIFICATION_HISTORY) return false;
$sent_newsletters = $sent_newsletters =
SendingQueue::where('newsletter_id', $newsletter['id']) NewsletterModel::where('parent_id', $newsletter->parent_id)
->where('status', 'completed') ->where('status', NewsletterModel::STATUS_SENT)
->count(); ->count();
return ++$sent_newsletters; return ++$sent_newsletters;

View File

@@ -1,5 +1,6 @@
<?php <?php
namespace MailPoet\Newsletter\Shortcodes\Categories; namespace MailPoet\Newsletter\Shortcodes\Categories;
use MailPoet\Models\Subscriber as SubscriberModel; use MailPoet\Models\Subscriber as SubscriberModel;
use MailPoet\Models\SubscriberCustomField; use MailPoet\Models\SubscriberCustomField;
@@ -15,14 +16,14 @@ class Subscriber {
) { ) {
switch($action) { switch($action) {
case 'firstname': case 'firstname':
return ($subscriber) ? $subscriber['first_name'] : $default_value; return ($subscriber) ? $subscriber->first_name : $default_value;
case 'lastname': case 'lastname':
return ($subscriber) ? $subscriber['last_name'] : $default_value; return ($subscriber) ? $subscriber->last_name : $default_value;
case 'email': case 'email':
return ($subscriber) ? $subscriber['email'] : false; return ($subscriber) ? $subscriber->email : false;
case 'displayname': case 'displayname':
if($subscriber && $subscriber['wp_user_id']) { if($subscriber && $subscriber->wp_user_id) {
$wp_user = get_userdata($subscriber['wp_user_id']); $wp_user = get_userdata($subscriber->wp_user_id);
return $wp_user->user_login; return $wp_user->user_login;
} }
return $default_value; return $default_value;
@@ -31,10 +32,10 @@ class Subscriber {
->count(); ->count();
default: default:
if(preg_match('/cf_(\d+)/', $action, $custom_field) && if(preg_match('/cf_(\d+)/', $action, $custom_field) &&
!empty($subscriber['id']) !empty($subscriber->id)
) { ) {
$custom_field = SubscriberCustomField $custom_field = SubscriberCustomField
::where('subscriber_id', $subscriber['id']) ::where('subscriber_id', $subscriber->id)
->where('custom_field_id', $custom_field[1]) ->where('custom_field_id', $custom_field[1])
->findOne(); ->findOne();
return ($custom_field) ? $custom_field->value : false; return ($custom_field) ? $custom_field->value : false;

View File

@@ -12,15 +12,9 @@ class Shortcodes {
$subscriber = false, $subscriber = false,
$queue = false $queue = false
) { ) {
$this->newsletter = (is_object($newsletter)) ? $this->newsletter = $newsletter;
$newsletter->asArray() : $this->subscriber = $subscriber;
$newsletter; $this->queue = $queue;
$this->subscriber = (is_object($subscriber)) ?
$subscriber->asArray() :
$subscriber;
$this->queue = (is_object($queue)) ?
$queue->asArray() :
$queue;
} }
function extract($content, $categories = false) { function extract($content, $categories = false) {

View File

@@ -7,11 +7,10 @@ if(!defined('ABSPATH')) exit;
class ShortcodesTaskTest extends MailPoetTest { class ShortcodesTaskTest extends MailPoetTest {
function testItCanReplaceShortcodes() { function testItCanReplaceShortcodes() {
$queue = $newsletter = array( $queue = $newsletter = (object)array(
'id' => 1 'id' => 1
); );
$subscriber = array( $subscriber = (object)array(
'email' => 'test@xample. com', 'email' => 'test@xample. com',
'first_name' => 'John', 'first_name' => 'John',
'last_name' => 'Doe' 'last_name' => 'Doe'

View File

@@ -2,6 +2,7 @@
use MailPoet\Config\Populator; use MailPoet\Config\Populator;
use MailPoet\Models\CustomField; use MailPoet\Models\CustomField;
use MailPoet\Models\Newsletter;
use MailPoet\Models\SendingQueue; use MailPoet\Models\SendingQueue;
use MailPoet\Models\Setting; use MailPoet\Models\Setting;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
@@ -22,11 +23,7 @@ class ShortcodesTest extends MailPoetTest {
$this->WP_user = $this->_createWPUser(); $this->WP_user = $this->_createWPUser();
$this->WP_post = $this->_createWPPost(); $this->WP_post = $this->_createWPPost();
$this->subscriber = $this->_createSubscriber(); $this->subscriber = $this->_createSubscriber();
$this->newsletter = array( $this->newsletter = $this->_createNewsletter();
'subject' => 'some subject',
'type' => 'notification',
'id' => 2
);
$this->shortcodes_object = new MailPoet\Newsletter\Shortcodes\Shortcodes( $this->shortcodes_object = new MailPoet\Newsletter\Shortcodes\Shortcodes(
$this->newsletter, $this->newsletter,
$this->subscriber $this->subscriber
@@ -72,7 +69,7 @@ class ShortcodesTest extends MailPoetTest {
$shortcode = array('[some:shortcode]'); $shortcode = array('[some:shortcode]');
$result = $shortcodes_object->process($shortcode); $result = $shortcodes_object->process($shortcode);
expect($result[0])->false(); expect($result[0])->false();
add_filter('mailpoet_newsletter_shortcode', function ( add_filter('mailpoet_newsletter_shortcode', function(
$shortcode, $newsletter, $subscriber, $queue, $content) { $shortcode, $newsletter, $subscriber, $queue, $content) {
if($shortcode === '[some:shortcode]') return 'success'; if($shortcode === '[some:shortcode]') return 'success';
}, 10, 5); }, 10, 5);
@@ -98,7 +95,7 @@ class ShortcodesTest extends MailPoetTest {
'<a href="#">not post</a>'; '<a href="#">not post</a>';
$result = $result =
$shortcodes_object->process(array('[newsletter:subject]')); $shortcodes_object->process(array('[newsletter:subject]'));
expect($result[0])->equals($this->newsletter['subject']); expect($result[0])->equals($this->newsletter->subject);
$result = $result =
$shortcodes_object->process(array('[newsletter:total]'), $content); $shortcodes_object->process(array('[newsletter:total]'), $content);
expect($result[0])->equals(2); expect($result[0])->equals(2);
@@ -106,12 +103,32 @@ class ShortcodesTest extends MailPoetTest {
$shortcodes_object->process(array('[newsletter:post_title]')); $shortcodes_object->process(array('[newsletter:post_title]'));
$wp_post = get_post($this->WP_post); $wp_post = get_post($this->WP_post);
expect($result['0'])->equals($wp_post->post_title); expect($result['0'])->equals($wp_post->post_title);
$result = }
$shortcodes_object->process(array('[newsletter:number]'));
function itCanProcessPostNotificationNewsletterNumberShortcode() {
// create first post notification
$post_notification_history = $this->_createNewsletter(
$parent_id = $this->newsletter_id,
$type = Newsletter::TYPE_NOTIFICATION_HISTORY
);
$shortcodes_object = new MailPoet\Newsletter\Shortcodes\Shortcodes(
$post_notification_history,
$this->subscriber
);
$result = $shortcodes_object->process(array('[newsletter:number]'));
expect($result['0'])->equals(1); expect($result['0'])->equals(1);
$queue = $this->_createQueue();
$result = // create another post notification
$shortcodes_object->process(array('[newsletter:number]')); $post_notification_history = $this->_createNewsletter(
$parent_id = $this->newsletter_id,
$type = Newsletter::TYPE_NOTIFICATION_HISTORY
);
$shortcodes_object = new MailPoet\Newsletter\Shortcodes\Shortcodes(
$post_notification_history,
$this->subscriber
);
$result = $shortcodes_object->process(array('[newsletter:number]'));
expect($result['0'])->equals(2); expect($result['0'])->equals(2);
} }
@@ -228,7 +245,7 @@ class ShortcodesTest extends MailPoetTest {
$shortcode = '[link:shortcode]'; $shortcode = '[link:shortcode]';
$result = $shortcodes_object->process(array($shortcode)); $result = $shortcodes_object->process(array($shortcode));
expect($result[0])->false(); expect($result[0])->false();
add_filter('mailpoet_newsletter_shortcode_link', function ( add_filter('mailpoet_newsletter_shortcode_link', function(
$shortcode, $newsletter, $subscriber, $queue) { $shortcode, $newsletter, $subscriber, $queue) {
if($shortcode === '[link:shortcode]') return 'success'; if($shortcode === '[link:shortcode]') return 'success';
}, 10, 4); }, 10, 4);
@@ -271,15 +288,30 @@ class ShortcodesTest extends MailPoetTest {
return Subscriber::findOne($subscriber->id); return Subscriber::findOne($subscriber->id);
} }
function _createNewsletter($parent_id = null, $type = Newsletter::TYPE_NOTIFICATION) {
$newsletter = Newsletter::create();
$newsletter->hydrate(
array(
'subject' => 'some subject',
'type' => $type,
'status' => Newsletter::STATUS_SENT,
'parent_id' => $parent_id,
)
);
$newsletter->save();
return Newsletter::findOne($newsletter->id);
}
function _createQueue() { function _createQueue() {
$queue = SendingQueue::create(); $queue = SendingQueue::create();
$queue->newsletter_id = $this->newsletter['id']; $queue->newsletter_id = $this->newsletter['id'];
$queue->status = 'completed'; $queue->status = 'completed';
$queue->save(); $queue->save();
return $queue; return SendingQueue::findOne($queue->id);
} }
function _after() { function _after() {
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table); ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
ORM::raw_execute('TRUNCATE ' . CustomField::$_table); ORM::raw_execute('TRUNCATE ' . CustomField::$_table);