Inject WooCommerce helper for statistics
[MAILPOET-1853]
This commit is contained in:
@ -22,6 +22,7 @@ use MailPoet\Newsletter\Scheduler\Scheduler;
|
||||
use MailPoet\Newsletter\Url as NewsletterUrl;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
@ -36,6 +37,9 @@ class Newsletters extends APIEndpoint {
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var WCHelper */
|
||||
private $woocommerce_helper;
|
||||
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
@ -47,11 +51,13 @@ class Newsletters extends APIEndpoint {
|
||||
Listing\BulkActionController $bulk_action,
|
||||
Listing\Handler $listing_handler,
|
||||
WPFunctions $wp,
|
||||
WCHelper $woocommerce_helper,
|
||||
SettingsController $settings
|
||||
) {
|
||||
$this->bulk_action = $bulk_action;
|
||||
$this->listing_handler = $listing_handler;
|
||||
$this->wp = $wp;
|
||||
$this->woocommerce_helper = $woocommerce_helper;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
@ -437,13 +443,13 @@ class Newsletters extends APIEndpoint {
|
||||
$newsletter
|
||||
->withSegments(true)
|
||||
->withSendingQueue()
|
||||
->withStatistics();
|
||||
->withStatistics($this->woocommerce_helper);
|
||||
} else if ($newsletter->type === Newsletter::TYPE_WELCOME || $newsletter->type === Newsletter::TYPE_AUTOMATIC) {
|
||||
$newsletter
|
||||
->withOptions()
|
||||
->withTotalSent()
|
||||
->withScheduledToBeSent()
|
||||
->withStatistics();
|
||||
->withStatistics($this->woocommerce_helper);
|
||||
} else if ($newsletter->type === Newsletter::TYPE_NOTIFICATION) {
|
||||
$newsletter
|
||||
->withOptions()
|
||||
@ -453,7 +459,7 @@ class Newsletters extends APIEndpoint {
|
||||
$newsletter
|
||||
->withSegments(true)
|
||||
->withSendingQueue()
|
||||
->withStatistics();
|
||||
->withStatistics($this->woocommerce_helper);
|
||||
}
|
||||
|
||||
if ($newsletter->status === Newsletter::STATUS_SENT ||
|
||||
|
@ -13,6 +13,7 @@ use MailPoet\Models\StatsNotification;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Tasks\Sending;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||
|
||||
class Worker {
|
||||
|
||||
@ -31,11 +32,21 @@ class Worker {
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
function __construct(Mailer $mailer, Renderer $renderer, SettingsController $settings, $timer = false) {
|
||||
/** @var WCHelper */
|
||||
private $woocommerce_helper;
|
||||
|
||||
function __construct(
|
||||
Mailer $mailer,
|
||||
Renderer $renderer,
|
||||
SettingsController $settings,
|
||||
WCHelper $woocommerce_helper,
|
||||
$timer = false
|
||||
) {
|
||||
$this->timer = $timer ?: microtime(true);
|
||||
$this->renderer = $renderer;
|
||||
$this->mailer = $mailer;
|
||||
$this->settings = $settings;
|
||||
$this->woocommerce_helper = $woocommerce_helper;
|
||||
}
|
||||
|
||||
/** @throws \Exception */
|
||||
@ -92,7 +103,7 @@ class Worker {
|
||||
return $newsletter
|
||||
->withSendingQueue()
|
||||
->withTotalSent()
|
||||
->withStatistics();
|
||||
->withStatistics($this->woocommerce_helper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,7 +78,7 @@ class WorkersFactory {
|
||||
}
|
||||
|
||||
function createStatsNotificationsWorker($timer) {
|
||||
return new StatsNotificationsWorker($this->mailer, $this->renderer, $this->settings, $timer);
|
||||
return new StatsNotificationsWorker($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $timer);
|
||||
}
|
||||
|
||||
/** @return SendingServiceKeyCheckWorker */
|
||||
|
@ -549,8 +549,8 @@ class Newsletter extends Model {
|
||||
return $this;
|
||||
}
|
||||
|
||||
function withStatistics() {
|
||||
$statistics = $this->getStatistics();
|
||||
function withStatistics(WCHelper $woocommerce_helper) {
|
||||
$statistics = $this->getStatistics($woocommerce_helper);
|
||||
$this->statistics = $statistics;
|
||||
return $this;
|
||||
}
|
||||
@ -560,7 +560,7 @@ class Newsletter extends Model {
|
||||
return $renderer->render();
|
||||
}
|
||||
|
||||
function getStatistics() {
|
||||
function getStatistics(WCHelper $woocommerce_helper) {
|
||||
if (($this->type !== self::TYPE_WELCOME) && ($this->queue === false)) {
|
||||
return false;
|
||||
}
|
||||
@ -589,7 +589,6 @@ class Newsletter extends Model {
|
||||
}
|
||||
|
||||
// WooCommerce revenues
|
||||
$woocommerce_helper = new WCHelper();
|
||||
if ($woocommerce_helper->isWooCommerceActive()) {
|
||||
$currency = $woocommerce_helper->getWoocommerceCurrency();
|
||||
$row = StatisticsWooCommercePurchases::selectExpr('SUM(order_price_total) AS total')
|
||||
|
@ -26,6 +26,7 @@ use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\Subscription\Url as SubscriptionUrl;
|
||||
use MailPoet\Tasks\Sending as SendingTask;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||
|
||||
class NewslettersTest extends \MailPoetTest {
|
||||
/** @var Newsletters */
|
||||
@ -110,6 +111,7 @@ class NewslettersTest extends \MailPoetTest {
|
||||
ContainerWrapper::getInstance()->get(BulkActionController::class),
|
||||
ContainerWrapper::getInstance()->get(Handler::class),
|
||||
$wp,
|
||||
$this->makeEmpty(WCHelper::class),
|
||||
new SettingsController()
|
||||
);
|
||||
$response = $this->endpoint->get(array('id' => $this->newsletter->id));
|
||||
@ -148,6 +150,7 @@ class NewslettersTest extends \MailPoetTest {
|
||||
ContainerWrapper::getInstance()->get(BulkActionController::class),
|
||||
ContainerWrapper::getInstance()->get(Handler::class),
|
||||
$wp,
|
||||
$this->makeEmpty(WCHelper::class),
|
||||
new SettingsController()
|
||||
);
|
||||
|
||||
@ -513,6 +516,7 @@ class NewslettersTest extends \MailPoetTest {
|
||||
ContainerWrapper::getInstance()->get(BulkActionController::class),
|
||||
ContainerWrapper::getInstance()->get(Handler::class),
|
||||
$wp,
|
||||
$this->makeEmpty(WCHelper::class),
|
||||
new SettingsController()
|
||||
);
|
||||
|
||||
|
@ -14,6 +14,7 @@ use MailPoet\Models\StatisticsUnsubscribes;
|
||||
use MailPoet\Models\StatsNotification;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||
|
||||
class WorkerTest extends \MailPoetTest {
|
||||
|
||||
@ -38,7 +39,7 @@ class WorkerTest extends \MailPoetTest {
|
||||
$this->mailer = $this->createMock(Mailer::class);
|
||||
$this->renderer = $this->createMock(Renderer::class);
|
||||
$this->settings = new SettingsController();
|
||||
$this->stats_notifications = new Worker($this->mailer, $this->renderer, $this->settings);
|
||||
$this->stats_notifications = new Worker($this->mailer, $this->renderer, $this->settings, $this->makeEmpty(WCHelper::class));
|
||||
$this->settings->set(Worker::SETTINGS_KEY, [
|
||||
'enabled' => true,
|
||||
'address' => 'email@example.com'
|
||||
|
@ -15,6 +15,7 @@ use MailPoet\Models\StatisticsClicks;
|
||||
use MailPoet\Models\StatisticsUnsubscribes;
|
||||
use MailPoet\Util\Security;
|
||||
use MailPoet\Tasks\Sending as SendingTask;
|
||||
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||
|
||||
class NewsletterTest extends \MailPoetTest {
|
||||
function _before() {
|
||||
@ -169,7 +170,7 @@ class NewsletterTest extends \MailPoetTest {
|
||||
$unsubscribes->save();
|
||||
|
||||
$newsletter->queue = $newsletter->getQueue()->asArray();
|
||||
$statistics = $newsletter->getStatistics( $sending_queue->id);
|
||||
$statistics = $newsletter->getStatistics($this->makeEmpty(WCHelper::class));
|
||||
expect($statistics['opened'])->equals(1);
|
||||
expect($statistics['clicked'])->equals(1);
|
||||
expect($statistics['unsubscribed'])->equals(1);
|
||||
|
Reference in New Issue
Block a user