diff --git a/lib/API/JSON/v1/Newsletters.php b/lib/API/JSON/v1/Newsletters.php index 5391f580e4..8631a95266 100644 --- a/lib/API/JSON/v1/Newsletters.php +++ b/lib/API/JSON/v1/Newsletters.php @@ -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 || diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index 3ebab65895..3cde923a58 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -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); } /** diff --git a/lib/Cron/Workers/WorkersFactory.php b/lib/Cron/Workers/WorkersFactory.php index 810ba6fe91..570762d119 100644 --- a/lib/Cron/Workers/WorkersFactory.php +++ b/lib/Cron/Workers/WorkersFactory.php @@ -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 */ diff --git a/lib/Models/Newsletter.php b/lib/Models/Newsletter.php index fb4a5ed510..064b04849d 100644 --- a/lib/Models/Newsletter.php +++ b/lib/Models/Newsletter.php @@ -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') diff --git a/tests/integration/API/JSON/v1/NewslettersTest.php b/tests/integration/API/JSON/v1/NewslettersTest.php index 986767010b..12b14c54a8 100644 --- a/tests/integration/API/JSON/v1/NewslettersTest.php +++ b/tests/integration/API/JSON/v1/NewslettersTest.php @@ -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() ); diff --git a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php index 2941f604c1..34858daff3 100644 --- a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php +++ b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php @@ -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' diff --git a/tests/integration/Models/NewsletterTest.php b/tests/integration/Models/NewsletterTest.php index 047aaa5886..678ac18866 100644 --- a/tests/integration/Models/NewsletterTest.php +++ b/tests/integration/Models/NewsletterTest.php @@ -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);