Move data selection to a reusable service

[MAILPOET-2553]
This commit is contained in:
Pavel Dohnal
2020-12-01 17:12:10 +01:00
committed by Veljko V
parent daaeada3da
commit 344a338376
8 changed files with 75 additions and 64 deletions

View File

@ -23,6 +23,7 @@ use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Util\License\License;
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
use MailPoet\WooCommerce\TransactionalEmails;
use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader;
use MailPoet\WP\DateTime;
use MailPoet\WP\Functions as WPFunctions;
@ -66,6 +67,9 @@ class Newsletters {
/** @var AutomaticEmails */
private $automaticEmails;
/** @var WPPostListLoader */
private $wpPostListLoader;
public function __construct(
PageRenderer $pageRenderer,
PageLimit $listingPageLimit,
@ -79,6 +83,7 @@ class Newsletters {
ServicesChecker $servicesChecker,
NewsletterTemplatesRepository $newsletterTemplatesRepository,
AddToNewslettersSegments $addToNewslettersSegments,
WPPostListLoader $wpPostListLoader,
AutomaticEmails $automaticEmails
) {
$this->pageRenderer = $pageRenderer;
@ -94,6 +99,7 @@ class Newsletters {
$this->newsletterTemplatesRepository = $newsletterTemplatesRepository;
$this->addToNewslettersSegments = $addToNewslettersSegments;
$this->automaticEmails = $automaticEmails;
$this->wpPostListLoader = $wpPostListLoader;
}
public function render() {
@ -168,27 +174,10 @@ class Newsletters {
$data['display_detailed_stats'] = Installer::getPremiumStatus()['premium_plugin_initialized'];
$data['newsletters_templates_recently_sent_count'] = $this->newsletterTemplatesRepository->getRecentlySentCount();
$data['product_categories'] = $this->wp->getCategories(['taxonomy' => 'product_cat']);
$data['product_categories'] = $this->wpPostListLoader->getWooCommerceCategories();
usort($data['product_categories'], function ($a, $b) {
return strcmp($a->catName, $b->catName);
});
$data['products'] = $this->getProducts();
$data['products'] = $this->wpPostListLoader->getProducts();
$this->pageRenderer->displayPage('newsletters.html', $data);
}
private function getProducts() {
$products = $this->wp->getResultsFromWpDb(
"SELECT `ID`, `post_title` FROM {$this->wp->getWPTableName('posts')} WHERE `post_type` = %s ORDER BY `post_title` ASC;",
'product'
);
return array_map(function ($product) {
return [
'title' => $product->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
'ID' => $product->ID,
];
}, $products);
}
}

View File

@ -11,6 +11,7 @@ use MailPoet\Models\Subscriber;
use MailPoet\Services\Bridge;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader;
use MailPoet\WP\Functions as WPFunctions;
class Segments {
@ -32,12 +33,16 @@ class Segments {
/** @var WooCommerceHelper */
private $woocommerceHelper;
/** @var WPPostListLoader */
private $wpPostListLoader;
public function __construct(
PageRenderer $pageRenderer,
PageLimit $listingPageLimit,
ServicesChecker $servicesChecker,
WPFunctions $wp,
WooCommerceHelper $woocommerceHelper,
WPPostListLoader $wpPostListLoader,
SubscribersFeature $subscribersFeature
) {
$this->pageRenderer = $pageRenderer;
@ -46,6 +51,7 @@ class Segments {
$this->servicesChecker = $servicesChecker;
$this->wp = $wp;
$this->woocommerceHelper = $woocommerceHelper;
$this->wpPostListLoader = $wpPostListLoader;
}
public function render() {
@ -85,28 +91,11 @@ class Segments {
->where('type', Newsletter::TYPE_STANDARD)
->orderByExpr('ISNULL(sent_at) DESC, sent_at DESC')->findArray();
$data['product_categories'] = $this->wp->getCategories(['taxonomy' => 'product_cat']);
$data['product_categories'] = $this->wpPostListLoader->getWooCommerceCategories();
usort($data['product_categories'], function ($a, $b) {
return strcmp($a->catName, $b->catName);
});
$data['products'] = $this->getProducts();
$data['products'] = $this->wpPostListLoader->getProducts();
$data['is_woocommerce_active'] = $this->woocommerceHelper->isWooCommerceActive();
$this->pageRenderer->displayPage('segments.html', $data);
}
private function getProducts() {
$products = $this->wp->getResultsFromWpDb(
"SELECT `ID`, `post_title` FROM {$this->wp->getWPTableName('posts')} WHERE `post_type` = %s ORDER BY `post_title` ASC;",
'product'
);
return array_map(function ($product) {
return [
'title' => $product->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
'ID' => $product->ID,
];
}, $products);
}
}