diff --git a/mailpoet/lib/AutomaticEmails/WooCommerce/Events/FirstPurchase.php b/mailpoet/lib/AutomaticEmails/WooCommerce/Events/FirstPurchase.php index c5875c6726..56213f0099 100644 --- a/mailpoet/lib/AutomaticEmails/WooCommerce/Events/FirstPurchase.php +++ b/mailpoet/lib/AutomaticEmails/WooCommerce/Events/FirstPurchase.php @@ -39,7 +39,7 @@ class FirstPurchase { WCHelper $helper = null ) { if ($helper === null) { - $helper = new WCHelper(); + $helper = ContainerWrapper::getInstance()->get(WCHelper::class); } $this->helper = $helper; $this->scheduler = ContainerWrapper::getInstance()->get(AutomaticEmailScheduler::class); diff --git a/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedInCategory.php b/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedInCategory.php index 8c58285fa0..c2974fcec0 100644 --- a/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedInCategory.php +++ b/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedInCategory.php @@ -37,7 +37,7 @@ class PurchasedInCategory { WCHelper $woocommerceHelper = null ) { if ($woocommerceHelper === null) { - $woocommerceHelper = new WCHelper(); + $woocommerceHelper = ContainerWrapper::getInstance()->get(WCHelper::class); } $this->woocommerceHelper = $woocommerceHelper; $this->scheduler = ContainerWrapper::getInstance()->get(AutomaticEmailScheduler::class); diff --git a/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php b/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php index a6fa90a5e4..4c5e8e30bd 100644 --- a/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php +++ b/mailpoet/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php @@ -38,7 +38,7 @@ class PurchasedProduct { WCHelper $helper = null ) { if ($helper === null) { - $helper = new WCHelper(); + $helper = ContainerWrapper::getInstance()->get(WCHelper::class); } $this->helper = $helper; $this->scheduler = ContainerWrapper::getInstance()->get(AutomaticEmailScheduler::class); diff --git a/mailpoet/lib/Config/PrivacyPolicy.php b/mailpoet/lib/Config/PrivacyPolicy.php index 04f253bf60..232b80e9df 100644 --- a/mailpoet/lib/Config/PrivacyPolicy.php +++ b/mailpoet/lib/Config/PrivacyPolicy.php @@ -38,7 +38,7 @@ class PrivacyPolicy { __('No identifiable information is otherwise tracked outside this website except for the email address.', 'mailpoet') . '

' ); - $helper = new WooCommerceHelper(); + $helper = new WooCommerceHelper(WPFunctions::get()); if ($helper->isWooCommerceActive()) { $content .= ( '

' . diff --git a/mailpoet/lib/Models/Segment.php b/mailpoet/lib/Models/Segment.php index dfb914111b..c42615f72a 100644 --- a/mailpoet/lib/Models/Segment.php +++ b/mailpoet/lib/Models/Segment.php @@ -4,6 +4,7 @@ namespace MailPoet\Models; use MailPoet\Entities\SegmentEntity; use MailPoet\WooCommerce\Helper as WCHelper; +use MailPoet\WP\Functions; /** * @property array $subscribersCount @@ -170,7 +171,7 @@ class Segment extends Model { * @deprecated Use the non static implementation in \MailPoet\Segments\WooCommerce::shouldShowWooCommerceSegment instead */ public static function shouldShowWooCommerceSegment() { - $woocommerceHelper = new WCHelper(); + $woocommerceHelper = new WCHelper(Functions::get()); $isWoocommerceActive = $woocommerceHelper->isWooCommerceActive(); $woocommerceUserExists = Segment::tableAlias('segment') ->where('segment.type', Segment::TYPE_WC_USERS) diff --git a/mailpoet/lib/Newsletter/Editor/PostContentManager.php b/mailpoet/lib/Newsletter/Editor/PostContentManager.php index ea2c971cb8..7064d5b231 100644 --- a/mailpoet/lib/Newsletter/Editor/PostContentManager.php +++ b/mailpoet/lib/Newsletter/Editor/PostContentManager.php @@ -22,7 +22,7 @@ class PostContentManager { ) { $this->wp = new WPFunctions; $this->maxExcerptLength = $this->wp->applyFilters('mailpoet_newsletter_post_excerpt_length', $this->maxExcerptLength); - $this->woocommerceHelper = $woocommerceHelper ?: new WooCommerceHelper(); + $this->woocommerceHelper = $woocommerceHelper ?: new WooCommerceHelper($this->wp); } public function getContent($post, $displayType) { diff --git a/mailpoet/lib/Newsletter/Editor/PostTransformerContentsExtractor.php b/mailpoet/lib/Newsletter/Editor/PostTransformerContentsExtractor.php index b61bd3172c..790e7575da 100644 --- a/mailpoet/lib/Newsletter/Editor/PostTransformerContentsExtractor.php +++ b/mailpoet/lib/Newsletter/Editor/PostTransformerContentsExtractor.php @@ -21,7 +21,7 @@ class PostTransformerContentsExtractor { ) { $this->args = $args; $this->wp = new WPFunctions(); - $this->woocommerceHelper = new WooCommerceHelper(); + $this->woocommerceHelper = new WooCommerceHelper($this->wp); } public function getContent($post, $withPostClass, $displayType) { diff --git a/mailpoet/lib/Twig/Functions.php b/mailpoet/lib/Twig/Functions.php index dd6a26a08b..58542071c8 100644 --- a/mailpoet/lib/Twig/Functions.php +++ b/mailpoet/lib/Twig/Functions.php @@ -27,7 +27,7 @@ class Functions extends AbstractExtension { private function getWooCommerceHelper(): WooCommerceHelper { if ($this->woocommerceHelper === null) { - $this->woocommerceHelper = new WooCommerceHelper(); + $this->woocommerceHelper = new WooCommerceHelper($this->getWp()); } return $this->woocommerceHelper; } diff --git a/mailpoet/lib/WooCommerce/Helper.php b/mailpoet/lib/WooCommerce/Helper.php index 1421bfc52f..c74ca0e40f 100644 --- a/mailpoet/lib/WooCommerce/Helper.php +++ b/mailpoet/lib/WooCommerce/Helper.php @@ -8,6 +8,15 @@ use MailPoet\RuntimeException; use MailPoet\WP\Functions as WPFunctions; class Helper { + /** @var WPFunctions */ + private $wp; + + public function __construct( + WPFunctions $wp + ) { + $this->wp = $wp; + } + public function isWooCommerceActive() { return class_exists('WooCommerce'); } @@ -169,4 +178,18 @@ class Helper { public function createWcCoupon($data) { return new \WC_Coupon($data); } + + public function getCouponList(): array { + $couponPosts = $this->wp->getPosts([ + 'posts_per_page' => -1, + 'orderby' => 'name', + 'order' => 'asc', + 'post_type' => 'shop_coupon', + 'post_status' => 'publish', + ]); + + return array_map(function(\WP_Post $post): string { + return $post->post_title; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + }, $couponPosts); + } } diff --git a/mailpoet/tests/integration/API/JSON/v1/AutomaticEmailsTest.php b/mailpoet/tests/integration/API/JSON/v1/AutomaticEmailsTest.php index c34b152c5f..3fb1b0bcb2 100644 --- a/mailpoet/tests/integration/API/JSON/v1/AutomaticEmailsTest.php +++ b/mailpoet/tests/integration/API/JSON/v1/AutomaticEmailsTest.php @@ -20,7 +20,7 @@ class AutomaticEmailsTest extends \MailPoetTest { $wooCommerceEventFactory = $this->diContainer->get(WooCommerceEventFactory::class); $automaticEmailFactory = $this->makeEmpty(AutomaticEmailFactory::class, [ - 'createWooCommerceEmail' => new WooCommerce($this->wp, new Helper(), $wooCommerceEventFactory), + 'createWooCommerceEmail' => new WooCommerce($this->wp, new Helper($this->wp), $wooCommerceEventFactory), ]); $this->api = new AutomaticEmails(new AutomaticEmailsController($this->wp, $automaticEmailFactory), $this->wp); } diff --git a/mailpoet/tests/integration/AutomaticEmails/AutomaticEmailsTest.php b/mailpoet/tests/integration/AutomaticEmails/AutomaticEmailsTest.php index 6ae2ddd058..47c22c2a79 100644 --- a/mailpoet/tests/integration/AutomaticEmails/AutomaticEmailsTest.php +++ b/mailpoet/tests/integration/AutomaticEmails/AutomaticEmailsTest.php @@ -21,7 +21,7 @@ class AutomaticEmailsTest extends \MailPoetTest { $this->wp = new WPFunctions(); $this->wooCommerceEventFactory = $this->diContainer->get(WooCommerceEventFactory::class); $this->automaticEmailFactory = $this->makeEmpty(AutomaticEmailFactory::class, [ - 'createWooCommerceEmail' => new WooCommerce($this->wp, new Helper(), $this->wooCommerceEventFactory), + 'createWooCommerceEmail' => new WooCommerce($this->wp, new Helper($this->wp), $this->wooCommerceEventFactory), ]); $this->AM = new AutomaticEmails($this->wp, $this->automaticEmailFactory); } diff --git a/mailpoet/tests/integration/AutomaticEmails/WooCommerce/Events/AbandonedCartTest.php b/mailpoet/tests/integration/AutomaticEmails/WooCommerce/Events/AbandonedCartTest.php index 9a7e6ddb22..4ef9bd84df 100644 --- a/mailpoet/tests/integration/AutomaticEmails/WooCommerce/Events/AbandonedCartTest.php +++ b/mailpoet/tests/integration/AutomaticEmails/WooCommerce/Events/AbandonedCartTest.php @@ -114,7 +114,7 @@ class AbandonedCartTest extends \MailPoetTest { public function testItGetsEventDetails() { $settings = $this->diContainer->get(SettingsController::class); $wp = new WPFunctions(); - $wcHelper = new WooCommerceHelper(); + $wcHelper = new WooCommerceHelper($wp); $cookies = new Cookies(); $subscriberCookie = new SubscriberCookie($cookies, new TrackingConfig($settings)); $event = new AbandonedCart( diff --git a/mailpoet/tests/integration/AutomaticEmails/WooCommerce/WooCommerceTest.php b/mailpoet/tests/integration/AutomaticEmails/WooCommerce/WooCommerceTest.php index b4762627e1..b0ca2f6381 100644 --- a/mailpoet/tests/integration/AutomaticEmails/WooCommerce/WooCommerceTest.php +++ b/mailpoet/tests/integration/AutomaticEmails/WooCommerce/WooCommerceTest.php @@ -22,9 +22,10 @@ class WooCommerceTest extends \MailPoetTest { private $automaticEmailFactory; public function _before() { + $wp = new WPFunctions(); $this->wooCommerceEventFactory = $this->diContainer->get(WooCommerceEventFactory::class); $this->automaticEmailFactory = $this->makeEmpty(AutomaticEmailFactory::class, [ - 'createWooCommerceEmail' => new WooCommerce(new WPFunctions(), new Helper(), $this->wooCommerceEventFactory), + 'createWooCommerceEmail' => new WooCommerce($wp, new Helper($wp), $this->wooCommerceEventFactory), ]); } @@ -109,8 +110,9 @@ class WooCommerceTest extends \MailPoetTest { } private function createWooCommerceEmailMock(bool $isWoocommerceEnabled = true): WooCommerce { + $wp = new WPFunctions(); $mock = $this->make(WooCommerce::class, ['isWoocommerceEnabled' => $isWoocommerceEnabled]); - $mock->__construct(new WPFunctions(), new Helper(), $this->wooCommerceEventFactory); + $mock->__construct($wp, new Helper($wp), $this->wooCommerceEventFactory); return $mock; } }