Refactor shortcode categories to doctrine
[MAILPOET-2993]
This commit is contained in:
@ -27,19 +27,19 @@ class Shortcodes {
|
|||||||
/** @var SubscribersRepository $subscribersRepository */
|
/** @var SubscribersRepository $subscribersRepository */
|
||||||
$subscribersRepository = ContainerWrapper::getInstance()->get(SubscribersRepository::class);
|
$subscribersRepository = ContainerWrapper::getInstance()->get(SubscribersRepository::class);
|
||||||
|
|
||||||
if ($queue instanceof Sending || $queue instanceof SendingQueue) {
|
if (($queue instanceof Sending || $queue instanceof SendingQueue) && $queue->id) {
|
||||||
$queue = $sendingQueueRepository->findOneById($queue->id);
|
$queue = $sendingQueueRepository->findOneById($queue->id);
|
||||||
}
|
}
|
||||||
if ($queue instanceof SendingQueueEntity) {
|
if ($queue instanceof SendingQueueEntity) {
|
||||||
$shortcodes->setQueue($queue);
|
$shortcodes->setQueue($queue);
|
||||||
}
|
}
|
||||||
if ($newsletter instanceof \MailPoet\Models\Newsletter) {
|
if ($newsletter instanceof \MailPoet\Models\Newsletter && $newsletter->id) {
|
||||||
$newsletter = $newsletterRepository->findOneById($newsletter->id);
|
$newsletter = $newsletterRepository->findOneById($newsletter->id);
|
||||||
}
|
}
|
||||||
if ($newsletter instanceof NewsletterEntity) {
|
if ($newsletter instanceof NewsletterEntity) {
|
||||||
$shortcodes->setNewsletter($newsletter);
|
$shortcodes->setNewsletter($newsletter);
|
||||||
}
|
}
|
||||||
if ($subscriber instanceof Subscriber) {
|
if ($subscriber instanceof Subscriber && $subscriber->id) {
|
||||||
$subscriber = $subscribersRepository->findOneById($subscriber->id);
|
$subscriber = $subscribersRepository->findOneById($subscriber->id);
|
||||||
}
|
}
|
||||||
if ($subscriber instanceof SubscriberEntity) {
|
if ($subscriber instanceof SubscriberEntity) {
|
||||||
|
@ -309,6 +309,10 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Newsletter\Segment\NewsletterSegmentRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Newsletter\Segment\NewsletterSegmentRepository::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Newsletter\Shortcodes\ShortcodesHelper::class)->setPublic(true);
|
$container->autowire(\MailPoet\Newsletter\Shortcodes\ShortcodesHelper::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Newsletter\Shortcodes\Shortcodes::class)->setPublic(true);
|
$container->autowire(\MailPoet\Newsletter\Shortcodes\Shortcodes::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Newsletter\Shortcodes\Categories\Date::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Newsletter\Shortcodes\Categories\Link::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Newsletter\Shortcodes\Categories\Newsletter::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Newsletter\Shortcodes\Categories\Subscriber::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Newsletter\Scheduler\WelcomeScheduler::class)->setPublic(true);
|
$container->autowire(\MailPoet\Newsletter\Scheduler\WelcomeScheduler::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Newsletter\Scheduler\PostNotificationScheduler::class)->setPublic(true);
|
$container->autowire(\MailPoet\Newsletter\Scheduler\PostNotificationScheduler::class)->setPublic(true);
|
||||||
|
@ -37,6 +37,10 @@ abstract class Repository {
|
|||||||
return $this->doctrineRepository->findBy($criteria, $orderBy, $limit, $offset);
|
return $this->doctrineRepository->findBy($criteria, $orderBy, $limit, $offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function countBy(array $criteria): int {
|
||||||
|
return $this->doctrineRepository->count($criteria);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $criteria
|
* @param array $criteria
|
||||||
* @param array|null $orderBy
|
* @param array|null $orderBy
|
||||||
|
18
lib/Newsletter/Shortcodes/Categories/CategoryInterface.php
Normal file
18
lib/Newsletter/Shortcodes/Categories/CategoryInterface.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
|
||||||
|
interface CategoryInterface {
|
||||||
|
public function process(
|
||||||
|
array $shortcodeDetails,
|
||||||
|
NewsletterEntity $newsletter = null,
|
||||||
|
SubscriberEntity $subscriber = null,
|
||||||
|
SendingQueueEntity $queue = null,
|
||||||
|
string $content = '',
|
||||||
|
bool $wpUserPreview = false
|
||||||
|
): ?string;
|
||||||
|
}
|
@ -2,12 +2,20 @@
|
|||||||
|
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Date {
|
class Date implements CategoryInterface {
|
||||||
public static function process(
|
public function process(
|
||||||
$shortcodeDetails
|
array $shortcodeDetails,
|
||||||
) {
|
NewsletterEntity $newsletter = null,
|
||||||
|
SubscriberEntity $subscriber = null,
|
||||||
|
SendingQueueEntity $queue = null,
|
||||||
|
string $content = '',
|
||||||
|
bool $wpUserPreview = false
|
||||||
|
): ?string {
|
||||||
$actionMapping = [
|
$actionMapping = [
|
||||||
'd' => 'd',
|
'd' => 'd',
|
||||||
'dordinal' => 'jS',
|
'dordinal' => 'jS',
|
||||||
@ -22,6 +30,6 @@ class Date {
|
|||||||
}
|
}
|
||||||
return ($shortcodeDetails['action'] === 'custom' && $shortcodeDetails['action_argument'] === 'format') ?
|
return ($shortcodeDetails['action'] === 'custom' && $shortcodeDetails['action_argument'] === 'format') ?
|
||||||
WPFunctions::get()->dateI18n($shortcodeDetails['action_argument_value'], $wp->currentTime('timestamp')) :
|
WPFunctions::get()->dateI18n($shortcodeDetails['action_argument_value'], $wp->currentTime('timestamp')) :
|
||||||
false;
|
null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,31 +2,52 @@
|
|||||||
|
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Entities\SendingQueueEntity;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Models\Newsletter as NewsletterModel;
|
||||||
|
use MailPoet\Models\SendingQueue;
|
||||||
|
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||||
use MailPoet\Newsletter\Url as NewsletterUrl;
|
use MailPoet\Newsletter\Url as NewsletterUrl;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Subscription\SubscriptionUrlFactory;
|
use MailPoet\Subscription\SubscriptionUrlFactory;
|
||||||
use MailPoet\Tasks\Sending;
|
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Link {
|
class Link implements CategoryInterface {
|
||||||
const CATEGORY_NAME = 'link';
|
const CATEGORY_NAME = 'link';
|
||||||
|
|
||||||
public static function process(
|
/** @var SettingsController */
|
||||||
$shortcodeDetails,
|
private $settings;
|
||||||
$newsletter,
|
|
||||||
$subscriber,
|
/** @var WPFunctions */
|
||||||
$queue,
|
private $wp;
|
||||||
$content,
|
|
||||||
$wpUserPreview
|
public function __construct(
|
||||||
|
SettingsController $settings,
|
||||||
|
WPFunctions $wp
|
||||||
) {
|
) {
|
||||||
|
$this->settings = $settings;
|
||||||
|
$this->wp = $wp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function process(
|
||||||
|
array $shortcodeDetails,
|
||||||
|
NewsletterEntity $newsletter = null,
|
||||||
|
SubscriberEntity $subscriber = null,
|
||||||
|
SendingQueueEntity $queue = null,
|
||||||
|
string $content = '',
|
||||||
|
bool $wpUserPreview = false
|
||||||
|
): ?string {
|
||||||
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
|
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
|
||||||
|
$subscriberModel = $this->getSubscriberModel($subscriber);
|
||||||
|
$newsletterModel = $this->getNewsletterModel($newsletter);
|
||||||
|
$queueModel = $this->getQueueModel($queue);
|
||||||
|
|
||||||
switch ($shortcodeDetails['action']) {
|
switch ($shortcodeDetails['action']) {
|
||||||
case 'subscription_unsubscribe_url':
|
case 'subscription_unsubscribe_url':
|
||||||
return self::processUrl(
|
return self::processUrl(
|
||||||
$shortcodeDetails['action'],
|
$shortcodeDetails['action'],
|
||||||
$subscriptionUrlFactory->getConfirmUnsubscribeUrl($wpUserPreview ? null : $subscriber, self::getSendingQueueId($queue)),
|
$subscriptionUrlFactory->getConfirmUnsubscribeUrl($wpUserPreview ? null : $subscriberModel, self::getSendingQueueId($queue)),
|
||||||
$queue,
|
$queue,
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
@ -34,7 +55,7 @@ class Link {
|
|||||||
case 'subscription_instant_unsubscribe_url':
|
case 'subscription_instant_unsubscribe_url':
|
||||||
return self::processUrl(
|
return self::processUrl(
|
||||||
$shortcodeDetails['action'],
|
$shortcodeDetails['action'],
|
||||||
$subscriptionUrlFactory->getUnsubscribeUrl($wpUserPreview ? null : $subscriber, self::getSendingQueueId($queue)),
|
$subscriptionUrlFactory->getUnsubscribeUrl($wpUserPreview ? null : $subscriberModel, self::getSendingQueueId($queue)),
|
||||||
$queue,
|
$queue,
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
@ -42,96 +63,121 @@ class Link {
|
|||||||
case 'subscription_manage_url':
|
case 'subscription_manage_url':
|
||||||
return self::processUrl(
|
return self::processUrl(
|
||||||
$shortcodeDetails['action'],
|
$shortcodeDetails['action'],
|
||||||
$subscriptionUrlFactory->getManageUrl($wpUserPreview ? null : $subscriber),
|
$subscriptionUrlFactory->getManageUrl($wpUserPreview ? null : $subscriberModel),
|
||||||
$queue,
|
$queue,
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
|
|
||||||
case 'newsletter_view_in_browser_url':
|
case 'newsletter_view_in_browser_url':
|
||||||
$url = NewsletterUrl::getViewInBrowserUrl(
|
$url = NewsletterUrl::getViewInBrowserUrl(
|
||||||
$newsletter,
|
$newsletterModel,
|
||||||
$wpUserPreview ? null : $subscriber,
|
$wpUserPreview ? null : $subscriber,
|
||||||
$queue,
|
$queueModel,
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
return self::processUrl($shortcodeDetails['action'], $url, $queue, $wpUserPreview);
|
return self::processUrl($shortcodeDetails['action'], $url, $queue, $wpUserPreview);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$shortcode = self::getFullShortcode($shortcodeDetails['action']);
|
$shortcode = self::getFullShortcode($shortcodeDetails['action']);
|
||||||
$url = WPFunctions::get()->applyFilters(
|
$url = $this->wp->applyFilters(
|
||||||
'mailpoet_newsletter_shortcode_link',
|
'mailpoet_newsletter_shortcode_link',
|
||||||
$shortcode,
|
$shortcode,
|
||||||
$newsletter,
|
$newsletterModel,
|
||||||
$subscriber,
|
$subscriberModel,
|
||||||
$queue,
|
$queueModel,
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
|
|
||||||
return ($url !== $shortcode) ?
|
return ($url !== $shortcode) ?
|
||||||
self::processUrl($shortcodeDetails['action'], $url, $queue, $wpUserPreview) :
|
self::processUrl($shortcodeDetails['action'], $url, $queue, $wpUserPreview) :
|
||||||
false;
|
null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function processUrl($action, $url, $queue, $wpUserPreview = false) {
|
public function processUrl($action, $url, $queue, $wpUserPreview = false): string {
|
||||||
if ($wpUserPreview) return $url;
|
if ($wpUserPreview) return $url;
|
||||||
$settings = SettingsController::getInstance();
|
return ($queue !== false && (boolean)$this->settings->get('tracking.enabled')) ?
|
||||||
return ($queue !== false && (boolean)$settings->get('tracking.enabled')) ?
|
|
||||||
self::getFullShortcode($action) :
|
self::getFullShortcode($action) :
|
||||||
$url;
|
$url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function processShortcodeAction(
|
public function processShortcodeAction(
|
||||||
$shortcodeAction, $newsletter, $subscriber, $queue, $wpUserPreview
|
$shortcodeAction,
|
||||||
) {
|
NewsletterEntity $newsletter = null,
|
||||||
|
SubscriberEntity $subscriber = null,
|
||||||
|
SendingQueueEntity $queue = null,
|
||||||
|
$wpUserPreview = false
|
||||||
|
): ?string {
|
||||||
|
$subscriberModel = $this->getSubscriberModel($subscriber);
|
||||||
|
$newsletterModel = $this->getNewsletterModel($newsletter);
|
||||||
|
$queueModel = $this->getQueueModel($queue);
|
||||||
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
|
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
|
||||||
switch ($shortcodeAction) {
|
switch ($shortcodeAction) {
|
||||||
case 'subscription_unsubscribe_url':
|
case 'subscription_unsubscribe_url':
|
||||||
$url = $subscriptionUrlFactory->getConfirmUnsubscribeUrl($subscriber, self::getSendingQueueId($queue));
|
$url = $subscriptionUrlFactory->getConfirmUnsubscribeUrl($subscriberModel, self::getSendingQueueId($queue));
|
||||||
break;
|
break;
|
||||||
case 'subscription_instant_unsubscribe_url':
|
case 'subscription_instant_unsubscribe_url':
|
||||||
$url = $subscriptionUrlFactory->getUnsubscribeUrl($subscriber, self::getSendingQueueId($queue));
|
$url = $subscriptionUrlFactory->getUnsubscribeUrl($subscriberModel, self::getSendingQueueId($queue));
|
||||||
break;
|
break;
|
||||||
case 'subscription_manage_url':
|
case 'subscription_manage_url':
|
||||||
$url = $subscriptionUrlFactory->getManageUrl($subscriber);
|
$url = $subscriptionUrlFactory->getManageUrl($subscriberModel);
|
||||||
break;
|
break;
|
||||||
case 'newsletter_view_in_browser_url':
|
case 'newsletter_view_in_browser_url':
|
||||||
$url = NewsletterUrl::getViewInBrowserUrl(
|
$url = NewsletterUrl::getViewInBrowserUrl(
|
||||||
$newsletter,
|
$newsletterModel,
|
||||||
$subscriber,
|
$subscriberModel,
|
||||||
$queue,
|
$queueModel,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$shortcode = self::getFullShortcode($shortcodeAction);
|
$shortcode = self::getFullShortcode($shortcodeAction);
|
||||||
$url = WPFunctions::get()->applyFilters(
|
$url = $this->wp->applyFilters(
|
||||||
'mailpoet_newsletter_shortcode_link',
|
'mailpoet_newsletter_shortcode_link',
|
||||||
$shortcode,
|
$shortcode,
|
||||||
$newsletter,
|
$newsletterModel,
|
||||||
$subscriber,
|
$subscriberModel,
|
||||||
$queue,
|
$queueModel,
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
$url = ($url !== $shortcodeAction) ? $url : false;
|
$url = ($url !== $shortcodeAction) ? $url : null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getFullShortcode($action) {
|
private function getFullShortcode($action): string {
|
||||||
return sprintf('[link:%s]', $action);
|
return sprintf('[link:%s]', $action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getSendingQueueId($queue): ?int {
|
||||||
* @return int|null
|
|
||||||
*/
|
|
||||||
private static function getSendingQueueId($queue) {
|
|
||||||
if ($queue instanceof Sending) {
|
|
||||||
return (int)$queue->id;
|
|
||||||
}
|
|
||||||
if ($queue instanceof SendingQueueEntity) {
|
if ($queue instanceof SendingQueueEntity) {
|
||||||
return $queue->getId();
|
return $queue->getId();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// temporary function until Links are refactored to Doctrine
|
||||||
|
private function getSubscriberModel(SubscriberEntity $subscriber = null): ?SubscriberModel {
|
||||||
|
if (!$subscriber) return null;
|
||||||
|
$subscriberModel = SubscriberModel::where('id', $subscriber->getId())->findOne();
|
||||||
|
if ($subscriberModel) return $subscriberModel;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// temporary function until Links are refactored to Doctrine
|
||||||
|
private function getNewsletterModel(NewsletterEntity $newsletter = null): ?NewsletterModel {
|
||||||
|
if (!$newsletter) return null;
|
||||||
|
$newsletterModel = NewsletterModel::where('id', $newsletter->getId())->findOne();
|
||||||
|
if ($newsletterModel) return $newsletterModel;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// temporary function until Links are refactored to Doctrine
|
||||||
|
private function getQueueModel(SendingQueueEntity $queue = null): ?SendingQueue {
|
||||||
|
if (!$queue) return null;
|
||||||
|
$queueModel = SendingQueue::where('id', $queue->getId())->findOne();
|
||||||
|
if ($queueModel) return $queueModel;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,45 +2,59 @@
|
|||||||
|
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
use MailPoet\Models\Newsletter as NewsletterModel;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Newsletter\NewslettersRepository;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
use MailPoet\WP\Posts as WPPosts;
|
use MailPoet\WP\Posts as WPPosts;
|
||||||
|
|
||||||
class Newsletter {
|
class Newsletter implements CategoryInterface {
|
||||||
public static function process(
|
/** @var NewslettersRepository */
|
||||||
$shortcodeDetails,
|
private $newslettersRepository;
|
||||||
$newsletter,
|
|
||||||
$subscriber,
|
public function __construct(NewslettersRepository $newslettersRepository) {
|
||||||
$queue,
|
$this->newslettersRepository = $newslettersRepository;
|
||||||
$content
|
}
|
||||||
) {
|
|
||||||
|
public function process(
|
||||||
|
array $shortcodeDetails,
|
||||||
|
NewsletterEntity $newsletter = null,
|
||||||
|
SubscriberEntity $subscriber = null,
|
||||||
|
SendingQueueEntity $queue = null,
|
||||||
|
string $content = '',
|
||||||
|
bool $wpUserPreview = false
|
||||||
|
): ?string {
|
||||||
switch ($shortcodeDetails['action']) {
|
switch ($shortcodeDetails['action']) {
|
||||||
case 'subject':
|
case 'subject':
|
||||||
return ($newsletter) ? $newsletter->subject : false;
|
return ($newsletter instanceof NewsletterEntity) ? $newsletter->getSubject() : null;
|
||||||
|
|
||||||
case 'total':
|
case 'total':
|
||||||
return substr_count($content, 'data-post-id');
|
return (string)substr_count($content, 'data-post-id');
|
||||||
|
|
||||||
case 'post_title':
|
case 'post_title':
|
||||||
preg_match_all('/data-post-id="(\d+)"/ism', $content, $posts);
|
preg_match_all('/data-post-id="(\d+)"/ism', $content, $posts);
|
||||||
$postIds = array_unique($posts[1]);
|
$postIds = array_unique($posts[1]);
|
||||||
$latestPost = (!empty($postIds)) ? self::getLatestWPPost($postIds) : false;
|
$latestPost = (!empty($postIds)) ? $this->getLatestWPPost($postIds) : null;
|
||||||
return ($latestPost) ? $latestPost['post_title'] : false;
|
return ($latestPost) ? $latestPost['post_title'] : null;
|
||||||
|
|
||||||
case 'number':
|
case 'number':
|
||||||
if ($newsletter->type !== NewsletterModel::TYPE_NOTIFICATION_HISTORY) return false;
|
if (!($newsletter instanceof NewsletterEntity)) return null;
|
||||||
$sentNewsletters =
|
if ($newsletter->getType() !== NewsletterEntity::TYPE_NOTIFICATION_HISTORY) {
|
||||||
NewsletterModel::where('parent_id', $newsletter->parentId)
|
return null;
|
||||||
->where('status', NewsletterModel::STATUS_SENT)
|
}
|
||||||
->count();
|
$sentNewsletters = $this->newslettersRepository->countBy([
|
||||||
return ++$sentNewsletters;
|
'parent' => $newsletter->getParent(),
|
||||||
|
'status' => NewsletterEntity::STATUS_SENT,
|
||||||
|
]);
|
||||||
|
return (string)++$sentNewsletters;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function ensureConsistentQueryType(\WP_Query $query) {
|
public function ensureConsistentQueryType(\WP_Query $query) {
|
||||||
// Queries with taxonomies are autodetected as 'is_archive=true' and 'is_home=false'
|
// Queries with taxonomies are autodetected as 'is_archive=true' and 'is_home=false'
|
||||||
// while queries without them end up being 'is_archive=false' and 'is_home=true'.
|
// while queries without them end up being 'is_archive=false' and 'is_home=true'.
|
||||||
// This is to fix that by always enforcing constistent behavior.
|
// This is to fix that by always enforcing constistent behavior.
|
||||||
@ -48,10 +62,10 @@ class Newsletter {
|
|||||||
$query->is_home = false; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
$query->is_home = false; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getLatestWPPost($postIds) {
|
private function getLatestWPPost($postIds) {
|
||||||
// set low priority to execute 'ensureConstistentQueryType' before any other filter
|
// set low priority to execute 'ensureConstistentQueryType' before any other filter
|
||||||
$filterPriority = defined('PHP_INT_MIN') ? constant('PHP_INT_MIN') : ~PHP_INT_MAX;
|
$filterPriority = defined('PHP_INT_MIN') ? constant('PHP_INT_MIN') : ~PHP_INT_MAX;
|
||||||
WPFunctions::get()->addAction('pre_get_posts', [get_called_class(), 'ensureConsistentQueryType'], $filterPriority);
|
WPFunctions::get()->addAction('pre_get_posts', [$this, 'ensureConsistentQueryType'], $filterPriority);
|
||||||
$posts = new \WP_Query(
|
$posts = new \WP_Query(
|
||||||
[
|
[
|
||||||
'post_type' => WPPosts::getTypes(),
|
'post_type' => WPPosts::getTypes(),
|
||||||
@ -62,7 +76,7 @@ class Newsletter {
|
|||||||
'order' => 'DESC',
|
'order' => 'DESC',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
WPFunctions::get()->removeAction('pre_get_posts', [get_called_class(), 'ensureConsistentQueryType'], $filterPriority);
|
WPFunctions::get()->removeAction('pre_get_posts', [$this, 'ensureConsistentQueryType'], $filterPriority);
|
||||||
return (!empty($posts->posts[0])) ?
|
return (!empty($posts->posts[0])) ?
|
||||||
$posts->posts[0]->to_array() :
|
$posts->posts[0]->to_array() :
|
||||||
false;
|
false;
|
||||||
|
@ -2,52 +2,67 @@
|
|||||||
|
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
use MailPoet\Models\Subscriber as SubscriberModel;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Models\SubscriberCustomField;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\SubscriberCustomFieldEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Subscribers\SubscriberCustomFieldRepository;
|
||||||
|
use MailPoet\Subscribers\SubscribersRepository;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Subscriber {
|
class Subscriber implements CategoryInterface {
|
||||||
/**
|
|
||||||
* @param \MailPoet\Models\Subscriber|false|mixed $subscriber
|
/** @var SubscribersRepository */
|
||||||
*/
|
private $subscribersRepository;
|
||||||
public static function process(
|
|
||||||
$shortcodeDetails,
|
/** @var SubscriberCustomFieldRepository */
|
||||||
$newsletter,
|
private $subscriberCustomFieldRepository;
|
||||||
$subscriber
|
|
||||||
|
public function __construct(
|
||||||
|
SubscribersRepository $subscribersRepository,
|
||||||
|
SubscriberCustomFieldRepository $subscriberCustomFieldRepository
|
||||||
) {
|
) {
|
||||||
if ($subscriber !== false && !($subscriber instanceof SubscriberModel)) {
|
$this->subscribersRepository = $subscribersRepository;
|
||||||
return $shortcodeDetails['shortcode'];
|
$this->subscriberCustomFieldRepository = $subscriberCustomFieldRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function process(
|
||||||
|
array $shortcodeDetails,
|
||||||
|
NewsletterEntity $newsletter = null,
|
||||||
|
SubscriberEntity $subscriber = null,
|
||||||
|
SendingQueueEntity $queue = null,
|
||||||
|
string $content = '',
|
||||||
|
bool $wpUserPreview = false
|
||||||
|
): ?string {
|
||||||
$defaultValue = ($shortcodeDetails['action_argument'] === 'default') ?
|
$defaultValue = ($shortcodeDetails['action_argument'] === 'default') ?
|
||||||
$shortcodeDetails['action_argument_value'] :
|
$shortcodeDetails['action_argument_value'] :
|
||||||
'';
|
'';
|
||||||
switch ($shortcodeDetails['action']) {
|
switch ($shortcodeDetails['action']) {
|
||||||
case 'firstname':
|
case 'firstname':
|
||||||
return (!empty($subscriber->firstName)) ? $subscriber->firstName : $defaultValue;
|
return (($subscriber instanceof SubscriberEntity) && !empty($subscriber->getFirstName())) ? $subscriber->getFirstName() : $defaultValue;
|
||||||
case 'lastname':
|
case 'lastname':
|
||||||
return (!empty($subscriber->lastName)) ? $subscriber->lastName : $defaultValue;
|
return (($subscriber instanceof SubscriberEntity) && !empty($subscriber->getLastName())) ? $subscriber->getLastName() : $defaultValue;
|
||||||
case 'email':
|
case 'email':
|
||||||
return ($subscriber) ? $subscriber->email : false;
|
return ($subscriber instanceof SubscriberEntity) ? $subscriber->getEmail() : $defaultValue;
|
||||||
case 'displayname':
|
case 'displayname':
|
||||||
if ($subscriber && $subscriber->wpUserId) {
|
if (($subscriber instanceof SubscriberEntity) && $subscriber->getWpUserId()) {
|
||||||
$wpUser = WPFunctions::get()->getUserdata($subscriber->wpUserId);
|
$wpUser = WPFunctions::get()->getUserdata($subscriber->getWpUserId());
|
||||||
return $wpUser->user_login; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
return $wpUser->user_login; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
}
|
}
|
||||||
return $defaultValue;
|
return $defaultValue;
|
||||||
case 'count':
|
case 'count':
|
||||||
return SubscriberModel::filter('subscribed')
|
return (string)$this->subscribersRepository->getTotalSubscribers();
|
||||||
->count();
|
|
||||||
default:
|
default:
|
||||||
if (preg_match('/cf_(\d+)/', $shortcodeDetails['action'], $customField) &&
|
if (preg_match('/cf_(\d+)/', $shortcodeDetails['action'], $customField) &&
|
||||||
!empty($subscriber->id)
|
!empty($subscriber->getId())
|
||||||
) {
|
) {
|
||||||
$customField = SubscriberCustomField
|
$customField = $this->subscriberCustomFieldRepository->findOneBy([
|
||||||
::where('subscriber_id', $subscriber->id)
|
'subscriber' => $subscriber,
|
||||||
->where('custom_field_id', $customField[1])
|
'customField' => $customField[1],
|
||||||
->findOne();
|
]);
|
||||||
return ($customField instanceof SubscriberCustomField) ? $customField->value : false;
|
return ($customField instanceof SubscriberCustomFieldEntity) ? $customField->getValue() : null;
|
||||||
}
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,11 @@ namespace MailPoet\Newsletter\Shortcodes;
|
|||||||
use MailPoet\Entities\NewsletterEntity;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Entities\SendingQueueEntity;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\Categories\CategoryInterface;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\Categories\Date;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\Categories\Link;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\Categories\Newsletter;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\Categories\Subscriber;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Shortcodes {
|
class Shortcodes {
|
||||||
@ -22,6 +27,30 @@ class Shortcodes {
|
|||||||
/** @var bool */
|
/** @var bool */
|
||||||
private $wpUserPreview = false;
|
private $wpUserPreview = false;
|
||||||
|
|
||||||
|
/** @var Date */
|
||||||
|
private $dateCategory;
|
||||||
|
|
||||||
|
/** @var Link */
|
||||||
|
private $linkCategory;
|
||||||
|
|
||||||
|
/** @var Newsletter */
|
||||||
|
private $newsletterCategory;
|
||||||
|
|
||||||
|
/** @var Subscriber */
|
||||||
|
private $subscriberCategory;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
Date $dateCategory,
|
||||||
|
Link $linkCategory,
|
||||||
|
Newsletter $newsletterCategory,
|
||||||
|
Subscriber $subscriberCategory
|
||||||
|
) {
|
||||||
|
$this->dateCategory = $dateCategory;
|
||||||
|
$this->linkCategory = $linkCategory;
|
||||||
|
$this->newsletterCategory = $newsletterCategory;
|
||||||
|
$this->subscriberCategory = $subscriberCategory;
|
||||||
|
}
|
||||||
|
|
||||||
public function setNewsletter(NewsletterEntity $newsletter): void {
|
public function setNewsletter(NewsletterEntity $newsletter): void {
|
||||||
$this->newsletter = $newsletter;
|
$this->newsletter = $newsletter;
|
||||||
}
|
}
|
||||||
@ -65,26 +94,36 @@ class Shortcodes {
|
|||||||
return $match;
|
return $match;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function process($shortcodes, $content = false) {
|
public function process($shortcodes, $content = '') {
|
||||||
$processedShortcodes = [];
|
$processedShortcodes = [];
|
||||||
foreach ($shortcodes as $shortcode) {
|
foreach ($shortcodes as $shortcode) {
|
||||||
$shortcodeDetails = $this->match($shortcode);
|
$shortcodeDetails = $this->match($shortcode);
|
||||||
$shortcodeDetails['shortcode'] = $shortcode;
|
$shortcodeDetails['shortcode'] = $shortcode;
|
||||||
$shortcodeDetails['category'] = !empty($shortcodeDetails['category']) ?
|
$shortcodeDetails['category'] = !empty($shortcodeDetails['category']) ?
|
||||||
$shortcodeDetails['category'] :
|
$shortcodeDetails['category'] :
|
||||||
false;
|
'';
|
||||||
$shortcodeDetails['action'] = !empty($shortcodeDetails['action']) ?
|
$shortcodeDetails['action'] = !empty($shortcodeDetails['action']) ?
|
||||||
$shortcodeDetails['action'] :
|
$shortcodeDetails['action'] :
|
||||||
false;
|
'';
|
||||||
$shortcodeDetails['action_argument'] = !empty($shortcodeDetails['argument']) ?
|
$shortcodeDetails['action_argument'] = !empty($shortcodeDetails['argument']) ?
|
||||||
$shortcodeDetails['argument'] :
|
$shortcodeDetails['argument'] :
|
||||||
false;
|
'';
|
||||||
$shortcodeDetails['action_argument_value'] = !empty($shortcodeDetails['argument_value']) ?
|
$shortcodeDetails['action_argument_value'] = !empty($shortcodeDetails['argument_value']) ?
|
||||||
$shortcodeDetails['argument_value'] :
|
$shortcodeDetails['argument_value'] :
|
||||||
false;
|
false;
|
||||||
$shortcodeClass =
|
|
||||||
Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . ucfirst($shortcodeDetails['category']);
|
$category = strtolower($shortcodeDetails['category']);
|
||||||
if (!class_exists($shortcodeClass)) {
|
$categoryClass = $this->getCategoryObject($category);
|
||||||
|
if ($categoryClass instanceof CategoryInterface) {
|
||||||
|
$processedShortcodes[] = $categoryClass->process(
|
||||||
|
$shortcodeDetails,
|
||||||
|
$this->newsletter,
|
||||||
|
$this->subscriber,
|
||||||
|
$this->queue,
|
||||||
|
$content,
|
||||||
|
$this->wpUserPreview
|
||||||
|
);
|
||||||
|
} else {
|
||||||
$customShortcode = WPFunctions::get()->applyFilters(
|
$customShortcode = WPFunctions::get()->applyFilters(
|
||||||
'mailpoet_newsletter_shortcode',
|
'mailpoet_newsletter_shortcode',
|
||||||
$shortcode,
|
$shortcode,
|
||||||
@ -98,14 +137,7 @@ class Shortcodes {
|
|||||||
false :
|
false :
|
||||||
$customShortcode;
|
$customShortcode;
|
||||||
}
|
}
|
||||||
$processedShortcodes[] = $shortcodeClass::process(
|
|
||||||
$shortcodeDetails,
|
|
||||||
$this->newsletter,
|
|
||||||
$this->subscriber,
|
|
||||||
$this->queue,
|
|
||||||
$content,
|
|
||||||
$this->wpUserPreview
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return $processedShortcodes;
|
return $processedShortcodes;
|
||||||
}
|
}
|
||||||
@ -125,4 +157,17 @@ class Shortcodes {
|
|||||||
$shortcodes = array_intersect_key($shortcodes, $processedShortcodes);
|
$shortcodes = array_intersect_key($shortcodes, $processedShortcodes);
|
||||||
return str_replace($shortcodes, $processedShortcodes, $content);
|
return str_replace($shortcodes, $processedShortcodes, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCategoryObject($category): ?CategoryInterface {
|
||||||
|
if ($category === 'link') {
|
||||||
|
return $this->linkCategory;
|
||||||
|
} elseif ($category === 'date') {
|
||||||
|
return $this->dateCategory;
|
||||||
|
} elseif ($category === 'newsletter') {
|
||||||
|
return $this->newsletterCategory;
|
||||||
|
} elseif ($category === 'subscriber') {
|
||||||
|
return $this->subscriberCategory;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ namespace MailPoet\Router\Endpoints;
|
|||||||
|
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
use MailPoet\Cron\Workers\StatsNotifications\NewsletterLinkRepository;
|
use MailPoet\Cron\Workers\StatsNotifications\NewsletterLinkRepository;
|
||||||
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Newsletter\Links\Links;
|
use MailPoet\Newsletter\Links\Links;
|
||||||
@ -84,7 +85,10 @@ class Track {
|
|||||||
}
|
}
|
||||||
$data->queue = $this->sendingQueuesRepository->findOneById($data->queue_id);// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
$data->queue = $this->sendingQueuesRepository->findOneById($data->queue_id);// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
$data->subscriber = $this->subscribersRepository->findOneById($data->subscriber_id); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
$data->subscriber = $this->subscribersRepository->findOneById($data->subscriber_id); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
$data->newsletter = $this->newslettersRepository->findOneById($data->newsletter_id); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
$data->newsletter = (isset($data->newsletter_id)) ? $this->newslettersRepository->findOneById($data->newsletter_id) : null; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
|
if (!$data->newsletter && ($data->queue instanceof SendingQueueEntity)) {
|
||||||
|
$data->newsletter = $data->queue->getNewsletter();
|
||||||
|
}
|
||||||
if (!empty($data->link_hash)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
if (!empty($data->link_hash)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
$data->link = $this->newsletterLinkRepository->findOneBy([
|
$data->link = $this->newsletterLinkRepository->findOneBy([
|
||||||
'hash' => $data->link_hash, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
'hash' => $data->link_hash, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
@ -108,6 +112,7 @@ class Track {
|
|||||||
// check if the newsletter was sent to the subscriber
|
// check if the newsletter was sent to the subscriber
|
||||||
$queue = SendingQueue::findOne($data->queue_id); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
$queue = SendingQueue::findOne($data->queue_id); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
|
||||||
if (!$queue instanceof SendingQueue) return false;
|
if (!$queue instanceof SendingQueue) return false;
|
||||||
|
|
||||||
return ($queue->isSubscriberProcessed($data->subscriber->getId())) ?
|
return ($queue->isSubscriberProcessed($data->subscriber->getId())) ?
|
||||||
$data :
|
$data :
|
||||||
false;
|
false;
|
||||||
|
@ -7,7 +7,7 @@ use MailPoet\Entities\NewsletterLinkEntity;
|
|||||||
use MailPoet\Entities\SendingQueueEntity;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Models\StatisticsClicks;
|
use MailPoet\Models\StatisticsClicks;
|
||||||
use MailPoet\Newsletter\Shortcodes\Categories\Link;
|
use MailPoet\Newsletter\Shortcodes\Categories\Link as LinkShortcodeCategory;
|
||||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Util\Cookies;
|
use MailPoet\Util\Cookies;
|
||||||
@ -30,14 +30,19 @@ class Clicks {
|
|||||||
/** @var Shortcodes */
|
/** @var Shortcodes */
|
||||||
private $shortcodes;
|
private $shortcodes;
|
||||||
|
|
||||||
|
/** @var LinkShortcodeCategory */
|
||||||
|
private $linkShortcodeCategory;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SettingsController $settingsController,
|
SettingsController $settingsController,
|
||||||
Cookies $cookies,
|
Cookies $cookies,
|
||||||
Shortcodes $shortcodes
|
Shortcodes $shortcodes,
|
||||||
|
LinkShortcodeCategory $linkShortcodeCategory
|
||||||
) {
|
) {
|
||||||
$this->settingsController = $settingsController;
|
$this->settingsController = $settingsController;
|
||||||
$this->cookies = $cookies;
|
$this->cookies = $cookies;
|
||||||
$this->shortcodes = $shortcodes;
|
$this->shortcodes = $shortcodes;
|
||||||
|
$this->linkShortcodeCategory = $linkShortcodeCategory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,7 +101,7 @@ class Clicks {
|
|||||||
$this->cookies->set(
|
$this->cookies->set(
|
||||||
self::ABANDONED_CART_COOKIE_NAME,
|
self::ABANDONED_CART_COOKIE_NAME,
|
||||||
[
|
[
|
||||||
'subscriber_id' => $subscriber->id,
|
'subscriber_id' => $subscriber->getId(),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'expires' => time() + self::ABANDONED_CART_COOKIE_EXPIRY,
|
'expires' => time() + self::ABANDONED_CART_COOKIE_EXPIRY,
|
||||||
@ -115,7 +120,7 @@ class Clicks {
|
|||||||
) {
|
) {
|
||||||
if (preg_match('/\[link:(?P<action>.*?)\]/', $url, $shortcode)) {
|
if (preg_match('/\[link:(?P<action>.*?)\]/', $url, $shortcode)) {
|
||||||
if (!$shortcode['action']) $this->abort();
|
if (!$shortcode['action']) $this->abort();
|
||||||
$url = Link::processShortcodeAction(
|
$url = $this->linkShortcodeCategory->processShortcodeAction(
|
||||||
$shortcode['action'],
|
$shortcode['action'],
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber,
|
$subscriber,
|
||||||
|
@ -26,9 +26,8 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
$queue = $newsletter = (object)[
|
$queue = $newsletter = (object)[
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
];
|
];
|
||||||
$subscriber = Subscriber::create();
|
$subscriber = Subscriber::createOrUpdate([
|
||||||
$subscriber->hydrate([
|
'email' => 'test@xample.com',
|
||||||
'email' => 'test@xample. com',
|
|
||||||
'first_name' => 'John',
|
'first_name' => 'John',
|
||||||
'last_name' => 'Doe',
|
'last_name' => 'Doe',
|
||||||
]);
|
]);
|
||||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Newsletter\Preview;
|
|||||||
use Codeception\Stub\Expected;
|
use Codeception\Stub\Expected;
|
||||||
use Codeception\Util\Fixtures;
|
use Codeception\Util\Fixtures;
|
||||||
use MailPoet\Entities\NewsletterEntity;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
use MailPoet\Mailer\MailerError;
|
use MailPoet\Mailer\MailerError;
|
||||||
use MailPoet\Mailer\MetaInfo;
|
use MailPoet\Mailer\MetaInfo;
|
||||||
@ -25,6 +26,8 @@ class SendPreviewControllerTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
|
$this->truncateEntity(NewsletterEntity::class);
|
||||||
|
$this->truncateEntity(SubscriberEntity::class);
|
||||||
$this->subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
|
$this->subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
|
||||||
$newsletter = new NewsletterEntity();
|
$newsletter = new NewsletterEntity();
|
||||||
$newsletter->setType(NewsletterEntity::TYPE_STANDARD);
|
$newsletter->setType(NewsletterEntity::TYPE_STANDARD);
|
||||||
@ -33,10 +36,25 @@ class SendPreviewControllerTest extends \MailPoetTest {
|
|||||||
$newsletter->setBody(json_decode(Fixtures::get('newsletter_body_template'), true));
|
$newsletter->setBody(json_decode(Fixtures::get('newsletter_body_template'), true));
|
||||||
$newsletter->setHash(Security::generateHash());
|
$newsletter->setHash(Security::generateHash());
|
||||||
$this->entityManager->persist($newsletter);
|
$this->entityManager->persist($newsletter);
|
||||||
|
|
||||||
|
$subscriber = new SubscriberEntity();
|
||||||
|
$subscriber->setEmail('test@subscriber.com');
|
||||||
|
$subscriber->setWpUserId(5);
|
||||||
|
$this->entityManager->persist($subscriber);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$wpUser = new \stdClass();
|
||||||
|
$wpUser->ID = 5;
|
||||||
|
$wp = $this->make(WPFunctions::class, ['wpGetCurrentUser' => $wpUser]);
|
||||||
|
WPFunctions::set($wp);
|
||||||
|
|
||||||
$this->newsletter = $newsletter;
|
$this->newsletter = $newsletter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function _after() {
|
||||||
|
WPFunctions::set(new WPFunctions());
|
||||||
|
}
|
||||||
|
|
||||||
public function testItCanSendAPreview() {
|
public function testItCanSendAPreview() {
|
||||||
$mailer = $this->makeEmpty(Mailer::class, [
|
$mailer = $this->makeEmpty(Mailer::class, [
|
||||||
'send' => Expected::once(
|
'send' => Expected::once(
|
||||||
@ -71,8 +89,8 @@ class SendPreviewControllerTest extends \MailPoetTest {
|
|||||||
new MetaInfo(),
|
new MetaInfo(),
|
||||||
$this->diContainer->get(Renderer::class),
|
$this->diContainer->get(Renderer::class),
|
||||||
new WPFunctions(),
|
new WPFunctions(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(SubscribersRepository::class),
|
||||||
$this->diContainer->get(SubscribersRepository::class)
|
$this->diContainer->get(Shortcodes::class)
|
||||||
);
|
);
|
||||||
$sendPreviewController->sendPreview($this->newsletter, 'test@subscriber.com');
|
$sendPreviewController->sendPreview($this->newsletter, 'test@subscriber.com');
|
||||||
}
|
}
|
||||||
@ -100,8 +118,8 @@ class SendPreviewControllerTest extends \MailPoetTest {
|
|||||||
new MetaInfo(),
|
new MetaInfo(),
|
||||||
$this->diContainer->get(Renderer::class),
|
$this->diContainer->get(Renderer::class),
|
||||||
new WPFunctions(),
|
new WPFunctions(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(SubscribersRepository::class),
|
||||||
$this->diContainer->get(SubscribersRepository::class)
|
$this->diContainer->get(Shortcodes::class)
|
||||||
);
|
);
|
||||||
$sendPreviewController->sendPreview($this->newsletter, 'test@subscriber.com');
|
$sendPreviewController->sendPreview($this->newsletter, 'test@subscriber.com');
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ use MailPoet\Entities\SendingQueueEntity;
|
|||||||
use MailPoet\Entities\SubscriberCustomFieldEntity;
|
use MailPoet\Entities\SubscriberCustomFieldEntity;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Models\Newsletter;
|
use MailPoet\Models\Newsletter;
|
||||||
|
use MailPoet\Models\Newsletter as NewsletterModel;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Newsletter\Shortcodes\Categories\Date;
|
use MailPoet\Newsletter\Shortcodes\Categories\Date;
|
||||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||||
@ -36,18 +37,22 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
|
$this->cleanup();
|
||||||
$this->settings = SettingsController::getInstance();
|
$this->settings = SettingsController::getInstance();
|
||||||
$populator = $this->diContainer->get(Populator::class);
|
$populator = $this->diContainer->get(Populator::class);
|
||||||
$populator->up();
|
$populator->up();
|
||||||
$this->wPUser = $this->_createWPUser();
|
$this->wPUser = $this->_createWPUser();
|
||||||
$this->wPPost = $this->_createWPPost();
|
$this->wPPost = $this->_createWPPost();
|
||||||
$this->subscriber = $this->_createSubscriber();
|
$this->subscriber = $this->_createSubscriber();
|
||||||
|
$this->entityManager->persist($this->subscriber);
|
||||||
$this->newsletter = $this->_createNewsletter();
|
$this->newsletter = $this->_createNewsletter();
|
||||||
|
$this->entityManager->persist($this->newsletter);
|
||||||
$this->shortcodesObject = $this->diContainer->get(Shortcodes::class);
|
$this->shortcodesObject = $this->diContainer->get(Shortcodes::class);
|
||||||
$this->shortcodesObject->setNewsletter($this->newsletter);
|
$this->shortcodesObject->setNewsletter($this->newsletter);
|
||||||
$this->shortcodesObject->setSubscriber($this->subscriber);
|
$this->shortcodesObject->setSubscriber($this->subscriber);
|
||||||
$this->settings->set('tracking.enabled', false);
|
$this->settings->set('tracking.enabled', false);
|
||||||
$this->subscriptionUrlFactory = new SubscriptionUrlFactory(WPFunctions::get(), $this->settings, new LinkTokens);
|
$this->subscriptionUrlFactory = new SubscriptionUrlFactory(WPFunctions::get(), $this->settings, new LinkTokens);
|
||||||
|
$this->entityManager->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanExtractShortcodes() {
|
public function testItCanExtractShortcodes() {
|
||||||
@ -98,21 +103,22 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanProcessDateShortcodes() {
|
public function testItCanProcessDateShortcodes() {
|
||||||
|
$date = $this->diContainer->get(Date::class);
|
||||||
$shortcodeDetails = ['action' => 'd'];
|
$shortcodeDetails = ['action' => 'd'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('d', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('d', WPFunctions::get()->currentTime('timestamp')));
|
||||||
$shortcodeDetails = ['action' => 'dordinal'];
|
$shortcodeDetails = ['action' => 'dordinal'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('jS', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('jS', WPFunctions::get()->currentTime('timestamp')));
|
||||||
$shortcodeDetails = ['action' => 'dtext'];
|
$shortcodeDetails = ['action' => 'dtext'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('l', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('l', WPFunctions::get()->currentTime('timestamp')));
|
||||||
$shortcodeDetails = ['action' => 'm'];
|
$shortcodeDetails = ['action' => 'm'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('m', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('m', WPFunctions::get()->currentTime('timestamp')));
|
||||||
$shortcodeDetails = ['action' => 'mtext'];
|
$shortcodeDetails = ['action' => 'mtext'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('F', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('F', WPFunctions::get()->currentTime('timestamp')));
|
||||||
$shortcodeDetails = ['action' => 'y'];
|
$shortcodeDetails = ['action' => 'y'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('Y', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('Y', WPFunctions::get()->currentTime('timestamp')));
|
||||||
// allow custom date formats (http://php.net/manual/en/function.date.php)
|
// allow custom date formats (http://php.net/manual/en/function.date.php)
|
||||||
$shortcodeDetails = ['action' => 'custom', 'action_argument' => 'format', 'action_argument_value' => 'U F'];
|
$shortcodeDetails = ['action' => 'custom', 'action_argument' => 'format', 'action_argument_value' => 'U F'];
|
||||||
expect(Date::process($shortcodeDetails))->equals(date_i18n('U F', WPFunctions::get()->currentTime('timestamp')));
|
expect($date->process($shortcodeDetails))->equals(date_i18n('U F', WPFunctions::get()->currentTime('timestamp')));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanProcessNewsletterShortcodes() {
|
public function testItCanProcessNewsletterShortcodes() {
|
||||||
@ -186,20 +192,16 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
$result =
|
$result =
|
||||||
$shortcodesObject->process(['[subscriber:displayname]']);
|
$shortcodesObject->process(['[subscriber:displayname]']);
|
||||||
expect($result[0])->equals($this->wPUser->user_login);
|
expect($result[0])->equals($this->wPUser->user_login);
|
||||||
$subscribers = Subscriber::where('status', 'subscribed')
|
$subscribers = Subscriber::whereIn('status', [
|
||||||
->findMany();
|
SubscriberEntity::STATUS_SUBSCRIBED,
|
||||||
|
SubscriberEntity::STATUS_UNCONFIRMED,
|
||||||
|
SubscriberEntity::STATUS_INACTIVE,
|
||||||
|
])->findMany();
|
||||||
$subscriberCount = count($subscribers);
|
$subscriberCount = count($subscribers);
|
||||||
$result =
|
$result =
|
||||||
$shortcodesObject->process(['[subscriber:count]']);
|
$shortcodesObject->process(['[subscriber:count]']);
|
||||||
expect($result[0])->equals($subscriberCount);
|
expect($result[0])->equals($subscriberCount);
|
||||||
$this->subscriber->setStatus('unsubscribed');
|
$this->subscriber->setStatus('unsubscribed');
|
||||||
$result =
|
|
||||||
$shortcodesObject->process(['[subscriber:count]']);
|
|
||||||
expect($result[0])->equals($subscriberCount - 1);
|
|
||||||
$this->subscriber->setStatus('bounced');
|
|
||||||
$result =
|
|
||||||
$shortcodesObject->process(['[subscriber:count]']);
|
|
||||||
expect($result[0])->equals($subscriberCount - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanProcessSubscriberCustomFieldShortcodes() {
|
public function testItCanProcessSubscriberCustomFieldShortcodes() {
|
||||||
@ -213,7 +215,7 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
$result = $shortcodesObject->process(
|
$result = $shortcodesObject->process(
|
||||||
['[subscriber:cf_' . $customField->getId() . ']']
|
['[subscriber:cf_' . $customField->getId() . ']']
|
||||||
);
|
);
|
||||||
expect($result[0])->false();
|
expect($result[0])->null();
|
||||||
$subscriberCustomField = new SubscriberCustomFieldEntity($subscriber, $customField, 'custom_field_value');
|
$subscriberCustomField = new SubscriberCustomFieldEntity($subscriber, $customField, 'custom_field_value');
|
||||||
$this->entityManager->persist($subscriberCustomField);
|
$this->entityManager->persist($subscriberCustomField);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
@ -242,6 +244,7 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItReturnsShortcodeWhenTrackingEnabled() {
|
public function testItReturnsShortcodeWhenTrackingEnabled() {
|
||||||
$shortcodesObject = $this->shortcodesObject;
|
$shortcodesObject = $this->shortcodesObject;
|
||||||
|
$shortcodesObject->setWpUserPreview(false);
|
||||||
// Returns URL when tracking is not enabled
|
// Returns URL when tracking is not enabled
|
||||||
$shortcode = '[link:subscription_unsubscribe_url]';
|
$shortcode = '[link:subscription_unsubscribe_url]';
|
||||||
$result =
|
$result =
|
||||||
@ -278,6 +281,8 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
public function testItReturnsDefaultLinksWhenPreviewIsEnabled() {
|
public function testItReturnsDefaultLinksWhenPreviewIsEnabled() {
|
||||||
$shortcodesObject = $this->shortcodesObject;
|
$shortcodesObject = $this->shortcodesObject;
|
||||||
$shortcodesObject->setWpUserPreview(true);
|
$shortcodesObject->setWpUserPreview(true);
|
||||||
|
$shortcodesObject->setSubscriber(null);
|
||||||
|
$newsletterModel = NewsletterModel::where('id', $this->newsletter->getId())->findOne();
|
||||||
$shortcodes = [
|
$shortcodes = [
|
||||||
'[link:subscription_unsubscribe_url]',
|
'[link:subscription_unsubscribe_url]',
|
||||||
'[link:subscription_instant_unsubscribe_url]',
|
'[link:subscription_instant_unsubscribe_url]',
|
||||||
@ -288,7 +293,7 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
$this->subscriptionUrlFactory->getConfirmUnsubscribeUrl(null),
|
$this->subscriptionUrlFactory->getConfirmUnsubscribeUrl(null),
|
||||||
$this->subscriptionUrlFactory->getUnsubscribeUrl(null),
|
$this->subscriptionUrlFactory->getUnsubscribeUrl(null),
|
||||||
$this->subscriptionUrlFactory->getManageUrl(null),
|
$this->subscriptionUrlFactory->getManageUrl(null),
|
||||||
NewsletterUrl::getViewInBrowserUrl($this->newsletter),
|
NewsletterUrl::getViewInBrowserUrl($newsletterModel),
|
||||||
];
|
];
|
||||||
$result = $shortcodesObject->process($shortcodes);
|
$result = $shortcodesObject->process($shortcodes);
|
||||||
// hash is returned
|
// hash is returned
|
||||||
@ -299,19 +304,21 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItCanProcessCustomLinkShortcodes() {
|
public function testItCanProcessCustomLinkShortcodes() {
|
||||||
$shortcodesObject = $this->shortcodesObject;
|
$shortcodesObject = $this->shortcodesObject;
|
||||||
|
$shortcodesObject->setWpUserPreview(false);
|
||||||
$shortcode = '[link:shortcode]';
|
$shortcode = '[link:shortcode]';
|
||||||
$result = $shortcodesObject->process([$shortcode]);
|
$result = $shortcodesObject->process([$shortcode]);
|
||||||
expect($result[0])->false();
|
expect($result[0])->null();
|
||||||
add_filter('mailpoet_newsletter_shortcode_link', function(
|
remove_all_filters('mailpoet_newsletter_shortcode_link');
|
||||||
$shortcode, $newsletter, $subscriber, $queue) {
|
add_filter('mailpoet_newsletter_shortcode_link', function($shortcode, $newsletter, $subscriber, $queue) {
|
||||||
if ($shortcode === '[link:shortcode]') return 'success';
|
if ($shortcode === '[link:shortcode]') return 'success';
|
||||||
}, 10, 4);
|
}, 10, 4);
|
||||||
|
|
||||||
$result = $shortcodesObject->process([$shortcode]);
|
$result = $shortcodesObject->process([$shortcode]);
|
||||||
expect($result[0])->equals('success');
|
expect($result[0])->equals('success');
|
||||||
$this->settings->set('tracking.enabled', true);
|
$this->settings->set('tracking.enabled', true);
|
||||||
// tracking function only works during sending, so queue object must not be false
|
// tracking function only works during sending, so queue object must not be false
|
||||||
$shortcodesObject->setQueue($this->_createQueue());
|
$shortcodesObject->setQueue($this->_createQueue());
|
||||||
$result = $shortcodesObject->process([$shortcode]);
|
$result = $shortcodesObject->process([$shortcode], 'x');
|
||||||
expect($result[0])->equals($shortcode);
|
expect($result[0])->equals($shortcode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,12 +366,16 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function _after() {
|
public function _after() {
|
||||||
|
$this->cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cleanup() {
|
||||||
$this->truncateEntity(NewsletterEntity::class);
|
$this->truncateEntity(NewsletterEntity::class);
|
||||||
$this->truncateEntity(SubscriberEntity::class);
|
$this->truncateEntity(SubscriberEntity::class);
|
||||||
$this->truncateEntity(SendingQueueEntity::class);
|
$this->truncateEntity(SendingQueueEntity::class);
|
||||||
$this->truncateEntity(CustomFieldEntity::class);
|
$this->truncateEntity(CustomFieldEntity::class);
|
||||||
$this->truncateEntity(SubscriberCustomFieldEntity::class);
|
$this->truncateEntity(SubscriberCustomFieldEntity::class);
|
||||||
wp_delete_post($this->wPPost, true);
|
if ($this->wPPost) wp_delete_post($this->wPPost, true);
|
||||||
wp_delete_user($this->wPUser->ID);
|
if ($this->wPUser) wp_delete_user($this->wPUser->ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ class TrackTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
|
$this->cleanup();
|
||||||
// create newsletter
|
// create newsletter
|
||||||
$newsletter = new NewsletterEntity();
|
$newsletter = new NewsletterEntity();
|
||||||
$newsletter->setType('type');
|
$newsletter->setType('type');
|
||||||
@ -48,12 +49,15 @@ class TrackTest extends \MailPoetTest {
|
|||||||
$queue = new SendingQueueEntity();
|
$queue = new SendingQueueEntity();
|
||||||
$queue->setTask($task);
|
$queue->setTask($task);
|
||||||
$queue->setNewsletter($newsletter);
|
$queue->setNewsletter($newsletter);
|
||||||
|
$queue->setSubscribers((string)$subscriber->getId());
|
||||||
$this->queue = $queue;
|
$this->queue = $queue;
|
||||||
$this->entityManager->persist($queue);
|
$this->entityManager->persist($queue);
|
||||||
// create link
|
// create link
|
||||||
$link = new NewsletterLinkEntity($newsletter, $queue, 'url', 'hash');
|
$link = new NewsletterLinkEntity($newsletter, $queue, 'url', 'hash');
|
||||||
$this->link = $link;
|
$this->link = $link;
|
||||||
$this->entityManager->persist($link);
|
$this->entityManager->persist($link);
|
||||||
|
$scheduledTaskSubscriber = new ScheduledTaskSubscriberEntity($task, $subscriber, 1);
|
||||||
|
$this->entityManager->persist($scheduledTaskSubscriber);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
$subscriberModel = Subscriber::findOne($subscriber->getId());
|
$subscriberModel = Subscriber::findOne($subscriber->getId());
|
||||||
$linkTokens = new LinkTokens;
|
$linkTokens = new LinkTokens;
|
||||||
@ -162,10 +166,10 @@ class TrackTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItProcessesTrackData() {
|
public function testItProcessesTrackData() {
|
||||||
$processedData = $this->track->_processTrackData($this->trackData);
|
$processedData = $this->track->_processTrackData($this->trackData);
|
||||||
expect($processedData->queue->id)->equals($this->queue->id);
|
expect($processedData->queue->getId())->equals($this->queue->getId());
|
||||||
expect($processedData->subscriber->id)->equals($this->subscriber->id);
|
expect($processedData->subscriber->getId())->equals($this->subscriber->getId());
|
||||||
expect($processedData->newsletter->id)->equals($this->newsletter->id);
|
expect($processedData->newsletter->getId())->equals($this->newsletter->getId());
|
||||||
expect($processedData->link->id)->equals($this->link->id);
|
expect($processedData->link->getId())->equals($this->link->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItGetsProperHashWhenDuplicateHashesExist() {
|
public function testItGetsProperHashWhenDuplicateHashesExist() {
|
||||||
@ -175,16 +179,16 @@ class TrackTest extends \MailPoetTest {
|
|||||||
$newsletter = $newsletter->save();
|
$newsletter = $newsletter->save();
|
||||||
$queue = SendingTask::create();
|
$queue = SendingTask::create();
|
||||||
$queue->newsletterId = $newsletter->id;
|
$queue->newsletterId = $newsletter->id;
|
||||||
$queue->setSubscribers([$this->subscriber->id]);
|
$queue->setSubscribers([$this->subscriber->getId()]);
|
||||||
$queue->updateProcessedSubscribers([$this->subscriber->id]);
|
$queue->updateProcessedSubscribers([$this->subscriber->getId()]);
|
||||||
$queue->save();
|
$queue->save();
|
||||||
$trackData = $this->trackData;
|
$trackData = $this->trackData;
|
||||||
$trackData['queue_id'] = $queue->id;
|
$trackData['queue_id'] = $queue->id;
|
||||||
$trackData['newsletter_id'] = $newsletter->id;
|
$trackData['newsletter_id'] = $newsletter->id;
|
||||||
// create another link with the same hash but different queue ID
|
// create another link with the same hash but different queue ID
|
||||||
$link = NewsletterLink::create();
|
$link = NewsletterLink::create();
|
||||||
$link->hash = $this->link->hash;
|
$link->hash = $this->link->getHash();
|
||||||
$link->url = $this->link->url;
|
$link->url = $this->link->getUrl();
|
||||||
$link->newsletterId = $trackData['newsletter_id'];
|
$link->newsletterId = $trackData['newsletter_id'];
|
||||||
$link->queueId = $trackData['queue_id'];
|
$link->queueId = $trackData['queue_id'];
|
||||||
$link = $link->save();
|
$link = $link->save();
|
||||||
@ -194,10 +198,14 @@ class TrackTest extends \MailPoetTest {
|
|||||||
|
|
||||||
// assert that the fetched link ID belong to the newly created link
|
// assert that the fetched link ID belong to the newly created link
|
||||||
$processedData = $this->track->_processTrackData($trackData);
|
$processedData = $this->track->_processTrackData($trackData);
|
||||||
expect($processedData->link->id)->equals($link->id);
|
expect($processedData->link->getId())->equals($link->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _after() {
|
public function _after() {
|
||||||
|
$this->cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cleanup() {
|
||||||
$this->truncateEntity(NewsletterEntity::class);
|
$this->truncateEntity(NewsletterEntity::class);
|
||||||
$this->truncateEntity(SubscriberEntity::class);
|
$this->truncateEntity(SubscriberEntity::class);
|
||||||
$this->truncateEntity(NewsletterLinkEntity::class);
|
$this->truncateEntity(NewsletterLinkEntity::class);
|
||||||
|
@ -15,6 +15,7 @@ use MailPoet\Models\SendingQueue;
|
|||||||
use MailPoet\Models\StatisticsClicks;
|
use MailPoet\Models\StatisticsClicks;
|
||||||
use MailPoet\Models\StatisticsOpens;
|
use MailPoet\Models\StatisticsOpens;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\Categories\Link as LinkShortcodeCategory;
|
||||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Statistics\Track\Clicks;
|
use MailPoet\Statistics\Track\Clicks;
|
||||||
@ -84,12 +85,22 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
$this->settingsController = Stub::makeEmpty(SettingsController::class, [
|
$this->settingsController = Stub::makeEmpty(SettingsController::class, [
|
||||||
'get' => false,
|
'get' => false,
|
||||||
], $this);
|
], $this);
|
||||||
$this->clicks = new Clicks($this->settingsController, new Cookies(), $this->diContainer->get(Shortcodes::class));
|
$this->clicks = new Clicks(
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItAbortsWhenTrackDataIsEmptyOrMissingLink() {
|
public function testItAbortsWhenTrackDataIsEmptyOrMissingLink() {
|
||||||
// abort function should be called twice:
|
// abort function should be called twice:
|
||||||
$clicks = Stub::construct($this->clicks, [$this->settingsController, new Cookies()], [
|
$clicks = Stub::construct($this->clicks, [
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
|
], [
|
||||||
'abort' => Expected::exactly(2),
|
'abort' => Expected::exactly(2),
|
||||||
], $this);
|
], $this);
|
||||||
$data = $this->trackData;
|
$data = $this->trackData;
|
||||||
@ -105,7 +116,12 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
$this->subscriber->setWpUserId(99);
|
$this->subscriber->setWpUserId(99);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
$data->preview = true;
|
$data->preview = true;
|
||||||
$clicks = Stub::construct($this->clicks, [$this->settingsController, new Cookies()], [
|
$clicks = Stub::construct($this->clicks, [
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
|
], [
|
||||||
'redirectToUrl' => null,
|
'redirectToUrl' => null,
|
||||||
], $this);
|
], $this);
|
||||||
$clicks->track($data);
|
$clicks->track($data);
|
||||||
@ -115,7 +131,12 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItTracksClickAndOpenEvent() {
|
public function testItTracksClickAndOpenEvent() {
|
||||||
$data = $this->trackData;
|
$data = $this->trackData;
|
||||||
$clicks = Stub::construct($this->clicks, [$this->settingsController, new Cookies()], [
|
$clicks = Stub::construct($this->clicks, [
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
|
], [
|
||||||
'redirectToUrl' => null,
|
'redirectToUrl' => null,
|
||||||
], $this);
|
], $this);
|
||||||
$clicks->track($data);
|
$clicks->track($data);
|
||||||
@ -124,14 +145,24 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testItRedirectsToUrlAfterTracking() {
|
public function testItRedirectsToUrlAfterTracking() {
|
||||||
$clicks = Stub::construct($this->clicks, [$this->settingsController, new Cookies()], [
|
$clicks = Stub::construct($this->clicks, [
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
|
], [
|
||||||
'redirectToUrl' => Expected::exactly(1),
|
'redirectToUrl' => Expected::exactly(1),
|
||||||
], $this);
|
], $this);
|
||||||
$clicks->track($this->trackData);
|
$clicks->track($this->trackData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItIncrementsClickEventCount() {
|
public function testItIncrementsClickEventCount() {
|
||||||
$clicks = Stub::construct($this->clicks, [$this->settingsController, new Cookies()], [
|
$clicks = Stub::construct($this->clicks, [
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
|
], [
|
||||||
'redirectToUrl' => null,
|
'redirectToUrl' => null,
|
||||||
], $this);
|
], $this);
|
||||||
$clicks->track($this->trackData);
|
$clicks->track($this->trackData);
|
||||||
@ -152,7 +183,12 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testItFailsToConvertsInvalidShortcodeToUrl() {
|
public function testItFailsToConvertsInvalidShortcodeToUrl() {
|
||||||
$clicks = Stub::construct($this->clicks, [$this->settingsController, new Cookies()], [
|
$clicks = Stub::construct($this->clicks, [
|
||||||
|
$this->settingsController,
|
||||||
|
new Cookies(),
|
||||||
|
$this->diContainer->get(Shortcodes::class),
|
||||||
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
|
], [
|
||||||
'abort' => Expected::exactly(1),
|
'abort' => Expected::exactly(1),
|
||||||
], $this);
|
], $this);
|
||||||
// should call abort() method if shortcode action does not exist
|
// should call abort() method if shortcode action does not exist
|
||||||
|
@ -4,11 +4,16 @@ namespace MailPoet\Test\Statistics\Track;
|
|||||||
|
|
||||||
use Codeception\Stub;
|
use Codeception\Stub;
|
||||||
use Codeception\Stub\Expected;
|
use Codeception\Stub\Expected;
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\ScheduledTaskEntity;
|
||||||
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Models\Newsletter;
|
use MailPoet\Models\Newsletter;
|
||||||
use MailPoet\Models\ScheduledTask;
|
use MailPoet\Models\ScheduledTask;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\StatisticsOpens;
|
use MailPoet\Models\StatisticsOpens;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||||
use MailPoet\Statistics\Track\Opens;
|
use MailPoet\Statistics\Track\Opens;
|
||||||
use MailPoet\Subscribers\LinkTokens;
|
use MailPoet\Subscribers\LinkTokens;
|
||||||
use MailPoet\Tasks\Sending as SendingTask;
|
use MailPoet\Tasks\Sending as SendingTask;
|
||||||
@ -23,29 +28,43 @@ class OpensTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
|
$this->cleanup();
|
||||||
// create newsletter
|
// create newsletter
|
||||||
$newsletter = Newsletter::create();
|
$newsletter = new NewsletterEntity();
|
||||||
$newsletter->type = 'type';
|
$newsletter->setType('type');
|
||||||
$this->newsletter = $newsletter->save();
|
$newsletter->setSubject('subject');
|
||||||
|
$this->entityManager->persist($newsletter);
|
||||||
|
$this->newsletter = $newsletter;
|
||||||
// create subscriber
|
// create subscriber
|
||||||
$subscriber = Subscriber::create();
|
$subscriber = new SubscriberEntity();
|
||||||
$subscriber->email = 'test@example.com';
|
$subscriber->setEmail('test@example.com');
|
||||||
$subscriber->firstName = 'First';
|
$subscriber->setFirstName('First');
|
||||||
$subscriber->lastName = 'Last';
|
$subscriber->setLastName('Last');
|
||||||
$this->subscriber = $subscriber->save();
|
$subscriber->setLinkToken('token');
|
||||||
|
$this->subscriber = $subscriber;
|
||||||
|
$this->entityManager->persist($subscriber);
|
||||||
// create queue
|
// create queue
|
||||||
$queue = SendingTask::create();
|
$task = new ScheduledTaskEntity();
|
||||||
$queue->newsletterId = $newsletter->id;
|
$task->setType(SendingTask::TASK_TYPE);
|
||||||
$queue->setSubscribers([$subscriber->id]);
|
$task->setStatus(ScheduledTaskEntity::STATUS_COMPLETED);
|
||||||
$queue->updateProcessedSubscribers([$subscriber->id]);
|
$this->entityManager->persist($task);
|
||||||
$this->queue = $queue->save();
|
|
||||||
|
$queue = new SendingQueueEntity();
|
||||||
|
$queue->setNewsletter($newsletter);
|
||||||
|
$queue->setTask($task);
|
||||||
|
$queue->setSubscribers((string)$subscriber->getId());
|
||||||
|
$newsletter->getQueues()->add($queue);
|
||||||
|
$this->entityManager->persist($queue);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$this->queue = $queue;
|
||||||
$linkTokens = new LinkTokens;
|
$linkTokens = new LinkTokens;
|
||||||
// build track data
|
// build track data
|
||||||
$this->trackData = (object)[
|
$this->trackData = (object)[
|
||||||
'queue' => $queue,
|
'queue' => $queue,
|
||||||
'subscriber' => $subscriber,
|
'subscriber' => $subscriber,
|
||||||
'newsletter' => $newsletter,
|
'newsletter' => $newsletter,
|
||||||
'subscriber_token' => $linkTokens->getToken($subscriber),
|
'subscriber_token' => $linkTokens->getToken(SubscriberModel::findOne('test@example.com')),
|
||||||
'preview' => false,
|
'preview' => false,
|
||||||
];
|
];
|
||||||
// instantiate class
|
// instantiate class
|
||||||
@ -62,7 +81,7 @@ class OpensTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function testItDoesNotTrackOpenEventFromWpUserWhenPreviewIsEnabled() {
|
public function testItDoesNotTrackOpenEventFromWpUserWhenPreviewIsEnabled() {
|
||||||
$data = $this->trackData;
|
$data = $this->trackData;
|
||||||
$data->subscriber->wp_user_id = 99;
|
$data->subscriber->setWpUserId(99);
|
||||||
$data->preview = true;
|
$data->preview = true;
|
||||||
$opens = Stub::make($this->opens, [
|
$opens = Stub::make($this->opens, [
|
||||||
'returnResponse' => null,
|
'returnResponse' => null,
|
||||||
@ -101,6 +120,10 @@ class OpensTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function _after() {
|
public function _after() {
|
||||||
|
$this->cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cleanup() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||||
|
Reference in New Issue
Block a user