Move data selection to a reusable service
[MAILPOET-2553]
This commit is contained in:
@ -57,8 +57,8 @@ export const EventOptions = ({
|
|||||||
(data) => ({ id: data.id, name: data.text })
|
(data) => ({ id: data.id, name: data.text })
|
||||||
),
|
),
|
||||||
selected: () => selected,
|
selected: () => selected,
|
||||||
getLabel: undefined,
|
getLabel: _.property('name'),
|
||||||
getValue: undefined,
|
getValue: _.property('id'),
|
||||||
},
|
},
|
||||||
onValueChange: handleEventOptionChange,
|
onValueChange: handleEventOptionChange,
|
||||||
item: {
|
item: {
|
||||||
@ -67,15 +67,11 @@ export const EventOptions = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (eventOptions.endpoint === 'product_categories') {
|
if (eventOptions.endpoint === 'product_categories') {
|
||||||
fieldProps.field.getLabel = _.property('cat_name');
|
|
||||||
fieldProps.field.name = 'category_id';
|
fieldProps.field.name = 'category_id';
|
||||||
fieldProps.field.getValue = _.property('term_id');
|
|
||||||
fieldProps.item = { action: 'purchasedCategory' };
|
fieldProps.item = { action: 'purchasedCategory' };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (eventOptions.endpoint === 'products') {
|
if (eventOptions.endpoint === 'products') {
|
||||||
fieldProps.field.getLabel = _.property('title');
|
|
||||||
fieldProps.field.getValue = _.property('ID');
|
|
||||||
fieldProps.field.name = 'product_id';
|
fieldProps.field.name = 'product_id';
|
||||||
fieldProps.item = { action: 'purchasedProduct' };
|
fieldProps.item = { action: 'purchasedProduct' };
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ const categoriesField = {
|
|||||||
resetSelect2OnUpdate: true,
|
resetSelect2OnUpdate: true,
|
||||||
placeholder: MailPoet.I18n.t('selectWooPurchasedCategory'),
|
placeholder: MailPoet.I18n.t('selectWooPurchasedCategory'),
|
||||||
forceSelect2: true,
|
forceSelect2: true,
|
||||||
getLabel: _.property('cat_name'),
|
getLabel: _.property('name'),
|
||||||
getValue: _.property('term_id'),
|
getValue: _.property('id'),
|
||||||
};
|
};
|
||||||
|
|
||||||
const productsField = {
|
const productsField = {
|
||||||
@ -29,8 +29,8 @@ const productsField = {
|
|||||||
resetSelect2OnUpdate: true,
|
resetSelect2OnUpdate: true,
|
||||||
placeholder: MailPoet.I18n.t('selectWooPurchasedProduct'),
|
placeholder: MailPoet.I18n.t('selectWooPurchasedProduct'),
|
||||||
forceSelect2: true,
|
forceSelect2: true,
|
||||||
getLabel: _.property('title'),
|
getLabel: _.property('name'),
|
||||||
getValue: _.property('ID'),
|
getValue: _.property('id'),
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (formItems) => {
|
export default (formItems) => {
|
||||||
|
@ -23,6 +23,7 @@ use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
|
|||||||
use MailPoet\Util\License\License;
|
use MailPoet\Util\License\License;
|
||||||
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
||||||
use MailPoet\WooCommerce\TransactionalEmails;
|
use MailPoet\WooCommerce\TransactionalEmails;
|
||||||
|
use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader;
|
||||||
use MailPoet\WP\DateTime;
|
use MailPoet\WP\DateTime;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
@ -66,6 +67,9 @@ class Newsletters {
|
|||||||
/** @var AutomaticEmails */
|
/** @var AutomaticEmails */
|
||||||
private $automaticEmails;
|
private $automaticEmails;
|
||||||
|
|
||||||
|
/** @var WPPostListLoader */
|
||||||
|
private $wpPostListLoader;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PageRenderer $pageRenderer,
|
PageRenderer $pageRenderer,
|
||||||
PageLimit $listingPageLimit,
|
PageLimit $listingPageLimit,
|
||||||
@ -79,6 +83,7 @@ class Newsletters {
|
|||||||
ServicesChecker $servicesChecker,
|
ServicesChecker $servicesChecker,
|
||||||
NewsletterTemplatesRepository $newsletterTemplatesRepository,
|
NewsletterTemplatesRepository $newsletterTemplatesRepository,
|
||||||
AddToNewslettersSegments $addToNewslettersSegments,
|
AddToNewslettersSegments $addToNewslettersSegments,
|
||||||
|
WPPostListLoader $wpPostListLoader,
|
||||||
AutomaticEmails $automaticEmails
|
AutomaticEmails $automaticEmails
|
||||||
) {
|
) {
|
||||||
$this->pageRenderer = $pageRenderer;
|
$this->pageRenderer = $pageRenderer;
|
||||||
@ -94,6 +99,7 @@ class Newsletters {
|
|||||||
$this->newsletterTemplatesRepository = $newsletterTemplatesRepository;
|
$this->newsletterTemplatesRepository = $newsletterTemplatesRepository;
|
||||||
$this->addToNewslettersSegments = $addToNewslettersSegments;
|
$this->addToNewslettersSegments = $addToNewslettersSegments;
|
||||||
$this->automaticEmails = $automaticEmails;
|
$this->automaticEmails = $automaticEmails;
|
||||||
|
$this->wpPostListLoader = $wpPostListLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render() {
|
public function render() {
|
||||||
@ -168,27 +174,10 @@ class Newsletters {
|
|||||||
$data['display_detailed_stats'] = Installer::getPremiumStatus()['premium_plugin_initialized'];
|
$data['display_detailed_stats'] = Installer::getPremiumStatus()['premium_plugin_initialized'];
|
||||||
$data['newsletters_templates_recently_sent_count'] = $this->newsletterTemplatesRepository->getRecentlySentCount();
|
$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) {
|
$data['products'] = $this->wpPostListLoader->getProducts();
|
||||||
return strcmp($a->catName, $b->catName);
|
|
||||||
});
|
|
||||||
|
|
||||||
$data['products'] = $this->getProducts();
|
|
||||||
|
|
||||||
$this->pageRenderer->displayPage('newsletters.html', $data);
|
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ use MailPoet\Models\Subscriber;
|
|||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
|
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
|
||||||
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
||||||
|
use MailPoet\WP\AutocompletePostListLoader as WPPostListLoader;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Segments {
|
class Segments {
|
||||||
@ -32,12 +33,16 @@ class Segments {
|
|||||||
/** @var WooCommerceHelper */
|
/** @var WooCommerceHelper */
|
||||||
private $woocommerceHelper;
|
private $woocommerceHelper;
|
||||||
|
|
||||||
|
/** @var WPPostListLoader */
|
||||||
|
private $wpPostListLoader;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PageRenderer $pageRenderer,
|
PageRenderer $pageRenderer,
|
||||||
PageLimit $listingPageLimit,
|
PageLimit $listingPageLimit,
|
||||||
ServicesChecker $servicesChecker,
|
ServicesChecker $servicesChecker,
|
||||||
WPFunctions $wp,
|
WPFunctions $wp,
|
||||||
WooCommerceHelper $woocommerceHelper,
|
WooCommerceHelper $woocommerceHelper,
|
||||||
|
WPPostListLoader $wpPostListLoader,
|
||||||
SubscribersFeature $subscribersFeature
|
SubscribersFeature $subscribersFeature
|
||||||
) {
|
) {
|
||||||
$this->pageRenderer = $pageRenderer;
|
$this->pageRenderer = $pageRenderer;
|
||||||
@ -46,6 +51,7 @@ class Segments {
|
|||||||
$this->servicesChecker = $servicesChecker;
|
$this->servicesChecker = $servicesChecker;
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
$this->woocommerceHelper = $woocommerceHelper;
|
$this->woocommerceHelper = $woocommerceHelper;
|
||||||
|
$this->wpPostListLoader = $wpPostListLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render() {
|
public function render() {
|
||||||
@ -85,28 +91,11 @@ class Segments {
|
|||||||
->where('type', Newsletter::TYPE_STANDARD)
|
->where('type', Newsletter::TYPE_STANDARD)
|
||||||
->orderByExpr('ISNULL(sent_at) DESC, sent_at DESC')->findArray();
|
->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) {
|
$data['products'] = $this->wpPostListLoader->getProducts();
|
||||||
return strcmp($a->catName, $b->catName);
|
|
||||||
});
|
|
||||||
|
|
||||||
$data['products'] = $this->getProducts();
|
|
||||||
$data['is_woocommerce_active'] = $this->woocommerceHelper->isWooCommerceActive();
|
$data['is_woocommerce_active'] = $this->woocommerceHelper->isWooCommerceActive();
|
||||||
|
|
||||||
$this->pageRenderer->displayPage('segments.html', $data);
|
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -346,6 +346,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
// WordPress
|
// WordPress
|
||||||
$container->autowire(\MailPoet\WP\Emoji::class)->setPublic(true);
|
$container->autowire(\MailPoet\WP\Emoji::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\WP\Functions::class)->setPublic(true);
|
$container->autowire(\MailPoet\WP\Functions::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\WP\AutocompletePostListLoader::class)->setPublic(true);
|
||||||
// Third party classes
|
// Third party classes
|
||||||
$container->autowire(\MailPoetVendor\CSS::class)->setClass(\MailPoetVendor\CSS::class)->setPublic(true);
|
$container->autowire(\MailPoetVendor\CSS::class)->setClass(\MailPoetVendor\CSS::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoetVendor\csstidy::class)->setClass(\MailPoetVendor\csstidy::class);
|
$container->autowire(\MailPoetVendor\csstidy::class)->setClass(\MailPoetVendor\csstidy::class);
|
||||||
|
52
lib/WP/AutocompletePostListLoader.php
Normal file
52
lib/WP/AutocompletePostListLoader.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -30,15 +30,7 @@
|
|||||||
var mailpoet_transactional_emails_opt_in_notice_dismissed = '<%= transactional_emails_opt_in_notice_dismissed %>';
|
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_mss_key_invalid = <%= mss_key_invalid ? 'true' : 'false' %>;
|
||||||
var mailpoet_product_categories = <%= json_encode(product_categories) %>;
|
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) %>;
|
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 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') %>';
|
var mailpoet_account_url = '<%= add_referral_id("https://account.mailpoet.com/?s=" ~ subscriber_count ~ "&email=" ~ current_wp_user.user_email) | escape('js') %>';
|
||||||
|
@ -28,15 +28,7 @@
|
|||||||
var wordpress_editable_roles_list = <%= json_encode(wordpress_editable_roles_list) %>;
|
var wordpress_editable_roles_list = <%= json_encode(wordpress_editable_roles_list) %>;
|
||||||
var mailpoet_newsletters_list = <%= json_encode(newsletters_list) %>;
|
var mailpoet_newsletters_list = <%= json_encode(newsletters_list) %>;
|
||||||
var mailpoet_product_categories = <%= json_encode(product_categories) %>;
|
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) %>;
|
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) %>;
|
var is_woocommerce_active = <%= json_encode(is_woocommerce_active) %>;
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Reference in New Issue
Block a user