Files
piratepoet/lib/WP/AutocompletePostListLoader.php
Pavel Dohnal 344a338376 Move data selection to a reusable service
[MAILPOET-2553]
2020-12-03 10:09:51 +01:00

53 lines
1.4 KiB
PHP

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