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

@ -57,8 +57,8 @@ export const EventOptions = ({
(data) => ({ id: data.id, name: data.text })
),
selected: () => selected,
getLabel: undefined,
getValue: undefined,
getLabel: _.property('name'),
getValue: _.property('id'),
},
onValueChange: handleEventOptionChange,
item: {
@ -67,15 +67,11 @@ export const EventOptions = ({
};
if (eventOptions.endpoint === 'product_categories') {
fieldProps.field.getLabel = _.property('cat_name');
fieldProps.field.name = 'category_id';
fieldProps.field.getValue = _.property('term_id');
fieldProps.item = { action: 'purchasedCategory' };
}
if (eventOptions.endpoint === 'products') {
fieldProps.field.getLabel = _.property('title');
fieldProps.field.getValue = _.property('ID');
fieldProps.field.name = 'product_id';
fieldProps.item = { action: 'purchasedProduct' };
}

View File

@ -18,8 +18,8 @@ const categoriesField = {
resetSelect2OnUpdate: true,
placeholder: MailPoet.I18n.t('selectWooPurchasedCategory'),
forceSelect2: true,
getLabel: _.property('cat_name'),
getValue: _.property('term_id'),
getLabel: _.property('name'),
getValue: _.property('id'),
};
const productsField = {
@ -29,8 +29,8 @@ const productsField = {
resetSelect2OnUpdate: true,
placeholder: MailPoet.I18n.t('selectWooPurchasedProduct'),
forceSelect2: true,
getLabel: _.property('title'),
getValue: _.property('ID'),
getLabel: _.property('name'),
getValue: _.property('id'),
};
export default (formItems) => {

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

View File

@ -346,6 +346,7 @@ class ContainerConfigurator implements IContainerConfigurator {
// WordPress
$container->autowire(\MailPoet\WP\Emoji::class)->setPublic(true);
$container->autowire(\MailPoet\WP\Functions::class)->setPublic(true);
$container->autowire(\MailPoet\WP\AutocompletePostListLoader::class)->setPublic(true);
// Third party classes
$container->autowire(\MailPoetVendor\CSS::class)->setClass(\MailPoetVendor\CSS::class)->setPublic(true);
$container->autowire(\MailPoetVendor\csstidy::class)->setClass(\MailPoetVendor\csstidy::class);

View File

@ -0,0 +1,52 @@
<?php
namespace MailPoet\WP;
/**
* Class AutocompletePostListLoader is used to load data for the frontend autocomplete
*/
class AutocompletePostListLoader {
/** @var Functions */
private $wp;
public function __construct(Functions $wp) {
$this->wp = $wp;
}
public 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 $this->formatPosts($products);
}
public function getWooCommerceCategories() {
return $this->formatTerms($this->wp->getCategories(['taxonomy' => 'product_cat', 'orderby' => 'name']));
}
private function formatPosts($posts) {
if (empty($posts)) return [];
$result = [];
foreach ($posts as $post) {
$result[] = [
'id' => $post->ID,
'name' => $post->post_title,// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
];
}
return $result;
}
private function formatTerms($terms) {
if (empty($terms)) return [];
if (!is_array($terms)) return []; // there can be instance of WP_Error instead of list of terms if woo commerce is not active
$result = [];
foreach ($terms as $term) {
$result[] = [
'id' => $term->term_id,// phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps
'name' => $term->name,
];
}
return $result;
}
}

View File

@ -30,15 +30,7 @@
var mailpoet_transactional_emails_opt_in_notice_dismissed = '<%= transactional_emails_opt_in_notice_dismissed %>';
var mailpoet_mss_key_invalid = <%= mss_key_invalid ? 'true' : 'false' %>;
var mailpoet_product_categories = <%= json_encode(product_categories) %>;
mailpoet_product_categories = mailpoet_product_categories.map(function(category) {
category.id = category.cat_ID;
return category;
});
var mailpoet_products = <%= json_encode(products) %>;
mailpoet_products = mailpoet_products.map(function(product) {
product.id = product.ID;
return product;
});
var has_mss_key_specified = <%= json_encode(has_mss_key_specified) %>;
var mailpoet_account_url = '<%= add_referral_id("https://account.mailpoet.com/?s=" ~ subscriber_count ~ "&email=" ~ current_wp_user.user_email) | escape('js') %>';

View File

@ -28,15 +28,7 @@
var wordpress_editable_roles_list = <%= json_encode(wordpress_editable_roles_list) %>;
var mailpoet_newsletters_list = <%= json_encode(newsletters_list) %>;
var mailpoet_product_categories = <%= json_encode(product_categories) %>;
mailpoet_product_categories = mailpoet_product_categories.map(function(category) {
category.id = category.cat_ID;
return category;
});
var mailpoet_products = <%= json_encode(products) %>;
mailpoet_products = mailpoet_products.map(function(product) {
product.id = product.ID;
return product;
});
var is_woocommerce_active = <%= json_encode(is_woocommerce_active) %>;
</script>