Extract building category and tag field options to a service
[MAILPOET-5169]
This commit is contained in:
@@ -9,8 +9,6 @@ use MailPoet\Automation\Engine\WordPress;
|
|||||||
use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload;
|
use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload;
|
||||||
use MailPoet\Automation\Integrations\WooCommerce\WooCommerce;
|
use MailPoet\Automation\Integrations\WooCommerce\WooCommerce;
|
||||||
use WC_Customer;
|
use WC_Customer;
|
||||||
use WP_Error;
|
|
||||||
use WP_Term;
|
|
||||||
|
|
||||||
class CustomerOrderFieldsFactory {
|
class CustomerOrderFieldsFactory {
|
||||||
/** @var WooCommerce */
|
/** @var WooCommerce */
|
||||||
@@ -19,12 +17,17 @@ class CustomerOrderFieldsFactory {
|
|||||||
/** @var WordPress */
|
/** @var WordPress */
|
||||||
private $wordPress;
|
private $wordPress;
|
||||||
|
|
||||||
|
/** @var TermOptionsBuilder */
|
||||||
|
private $termOptionsBuilder;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
WordPress $wordPress,
|
WordPress $wordPress,
|
||||||
WooCommerce $wooCommerce
|
WooCommerce $wooCommerce,
|
||||||
|
TermOptionsBuilder $termOptionsBuilder
|
||||||
) {
|
) {
|
||||||
$this->wordPress = $wordPress;
|
$this->wordPress = $wordPress;
|
||||||
$this->wooCommerce = $wooCommerce;
|
$this->wooCommerce = $wooCommerce;
|
||||||
|
$this->termOptionsBuilder = $termOptionsBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return Field[] */
|
/** @return Field[] */
|
||||||
@@ -92,7 +95,7 @@ class CustomerOrderFieldsFactory {
|
|||||||
return $customer ? $this->getOrderProductTermIds($customer, 'product_cat') : [];
|
return $customer ? $this->getOrderProductTermIds($customer, 'product_cat') : [];
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
'options' => $this->getTermsList('product_cat'),
|
'options' => $this->termOptionsBuilder->getCategoryOptions(),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
new Field(
|
new Field(
|
||||||
@@ -104,7 +107,7 @@ class CustomerOrderFieldsFactory {
|
|||||||
return $customer ? $this->getOrderProductTermIds($customer, 'product_tag') : [];
|
return $customer ? $this->getOrderProductTermIds($customer, 'product_tag') : [];
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
'options' => $this->getTermsList('product_tag'),
|
'options' => $this->termOptionsBuilder->getTagOptions(),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
@@ -187,32 +190,4 @@ class CustomerOrderFieldsFactory {
|
|||||||
|
|
||||||
return array_map('intval', $wpdb->get_col($statement));
|
return array_map('intval', $wpdb->get_col($statement));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTermsList(string $taxonomy): array {
|
|
||||||
$terms = $this->wordPress->getTerms(['taxonomy' => $taxonomy, 'hide_empty' => false]);
|
|
||||||
if ($terms instanceof WP_Error) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return $this->buildTermsList((array)$terms);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function buildTermsList(array $terms, int $parentId = 0): array {
|
|
||||||
$parents = array_filter($terms, function ($term) use ($parentId) {
|
|
||||||
return $term instanceof WP_Term && $term->parent === $parentId;
|
|
||||||
});
|
|
||||||
|
|
||||||
usort($parents, function (WP_Term $a, WP_Term $b) {
|
|
||||||
return $a->name <=> $b->name;
|
|
||||||
});
|
|
||||||
|
|
||||||
$list = [];
|
|
||||||
foreach ($parents as $term) {
|
|
||||||
$id = $term->term_id; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
|
||||||
$list[] = ['id' => $id, 'name' => $term->name];
|
|
||||||
foreach ($this->buildTermsList($terms, $id) as $child) {
|
|
||||||
$list[] = ['id' => $child['id'], 'name' => "$term->name | {$child['name']}"];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $list;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,58 @@
|
|||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace MailPoet\Automation\Integrations\WooCommerce\Fields;
|
||||||
|
|
||||||
|
use MailPoet\Automation\Engine\WordPress;
|
||||||
|
use WP_Error;
|
||||||
|
use WP_Term;
|
||||||
|
|
||||||
|
class TermOptionsBuilder {
|
||||||
|
/** @var WordPress */
|
||||||
|
private $wordPress;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
WordPress $wordPress
|
||||||
|
) {
|
||||||
|
$this->wordPress = $wordPress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<array{id: int, name: string}> */
|
||||||
|
public function getCategoryOptions(): array {
|
||||||
|
return $this->getTermOptions('product_cat');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<array{id: int, name: string}> */
|
||||||
|
public function getTagOptions(): array {
|
||||||
|
return $this->getTermOptions('product_tag');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<array{id: int, name: string}> */
|
||||||
|
private function getTermOptions(string $taxonomy): array {
|
||||||
|
$terms = $this->wordPress->getTerms(['taxonomy' => $taxonomy, 'hide_empty' => false]);
|
||||||
|
if ($terms instanceof WP_Error) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return $this->buildTermsList((array)$terms);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<array{id: int, name: string}> */
|
||||||
|
private function buildTermsList(array $terms, int $parentId = 0): array {
|
||||||
|
$parents = array_filter($terms, function ($term) use ($parentId) {
|
||||||
|
return $term instanceof WP_Term && $term->parent === $parentId;
|
||||||
|
});
|
||||||
|
|
||||||
|
usort($parents, function (WP_Term $a, WP_Term $b) {
|
||||||
|
return $a->name <=> $b->name;
|
||||||
|
});
|
||||||
|
|
||||||
|
$list = [];
|
||||||
|
foreach ($parents as $term) {
|
||||||
|
$id = $term->term_id; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
||||||
|
$list[] = ['id' => (int)$id, 'name' => $term->name];
|
||||||
|
foreach ($this->buildTermsList($terms, $id) as $child) {
|
||||||
|
$list[] = ['id' => (int)$child['id'], 'name' => "$term->name | {$child['name']}"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
}
|
@@ -186,6 +186,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\CustomerFieldsFactory::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\CustomerFieldsFactory::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\CustomerOrderFieldsFactory::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\CustomerOrderFieldsFactory::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\CustomerReviewFieldsFactory::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\CustomerReviewFieldsFactory::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Fields\TermOptionsBuilder::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Triggers\OrderStatusChangedTrigger::class)->setPublic(true)->setShared(false);
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Triggers\OrderStatusChangedTrigger::class)->setPublic(true)->setShared(false);
|
||||||
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject::class)->setPublic(true)->setShared(false);
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject::class)->setPublic(true)->setShared(false);
|
||||||
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderStatusChangeSubject::class)->setPublic(true)->setShared(false);
|
$container->autowire(\MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderStatusChangeSubject::class)->setPublic(true)->setShared(false);
|
||||||
|
Reference in New Issue
Block a user