Add method for getting WC coupons

[MAILPOET-4761]
This commit is contained in:
Jan Lysý
2023-01-16 20:24:42 +01:00
committed by Aschepikov
parent fe536fcdd0
commit 40f4216ff8
13 changed files with 39 additions and 13 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -38,7 +38,7 @@ class PrivacyPolicy {
__('No identifiable information is otherwise tracked outside this website except for the email address.', 'mailpoet') .
'</p>'
);
$helper = new WooCommerceHelper();
$helper = new WooCommerceHelper(WPFunctions::get());
if ($helper->isWooCommerceActive()) {
$content .= (
'<p> ' .

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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(

View File

@@ -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;
}
}