Нет описания

AutocompletePostListLoader.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace MailPoet\WP;
  3. if (!defined('ABSPATH')) exit;
  4. /**
  5. * Class AutocompletePostListLoader is used to load data for the frontend autocomplete
  6. */
  7. class AutocompletePostListLoader {
  8. /** @var Functions */
  9. private $wp;
  10. public function __construct(
  11. Functions $wp
  12. ) {
  13. $this->wp = $wp;
  14. }
  15. public function getProducts() {
  16. $products = $this->wp->getResultsFromWpDb(
  17. "SELECT `ID`, `post_title` FROM {$this->wp->getWPTableName('posts')} WHERE `post_type` = %s ORDER BY `post_title` ASC;",
  18. 'product'
  19. );
  20. return $this->formatPosts($products);
  21. }
  22. public function getSubscriptionProducts() {
  23. $products = $this->wp->getResultsFromWpDb(
  24. "SELECT `ID`, `post_title` FROM {$this->wp->getWPTableName('posts')} AS p
  25. INNER JOIN {$this->wp->getWPTableName('term_relationships')} AS trel ON trel.object_id = p.id
  26. INNER JOIN {$this->wp->getWPTableName('term_taxonomy')} AS ttax ON ttax.term_taxonomy_id = trel.term_taxonomy_id
  27. INNER JOIN {$this->wp->getWPTableName('terms')} AS t ON ttax.term_id = t.term_id AND t.slug IN ('subscription', 'variable-subscription')
  28. WHERE `p`.`post_type` = %s ORDER BY `post_title` ASC;",
  29. 'product'
  30. );
  31. return $this->formatPosts($products);
  32. }
  33. public function getWooCommerceCategories() {
  34. return $this->formatTerms($this->wp->getCategories(['taxonomy' => 'product_cat', 'orderby' => 'name']));
  35. }
  36. public function getPosts() {
  37. global $wpdb;
  38. $optionList = $wpdb->get_results('SELECT ID, post_title FROM ' . $wpdb->posts . " WHERE post_type='post' ORDER BY `post_title` ASC;");
  39. return $this->formatPosts($optionList);
  40. }
  41. public function getPages() {
  42. global $wpdb;
  43. $optionList = $wpdb->get_results('SELECT ID, post_title FROM ' . $wpdb->posts . " WHERE post_type='page' ORDER BY `post_title` ASC;");
  44. return $this->formatPosts($optionList);
  45. }
  46. public function getWooCommerceTags() {
  47. return $this->formatTerms($this->wp->getTerms('product_tag'));
  48. }
  49. public function getCategories() {
  50. return $this->formatTerms($this->wp->getCategories());
  51. }
  52. public function getTags() {
  53. return $this->formatTerms($this->wp->getTags());
  54. }
  55. private function formatPosts($posts) {
  56. if (empty($posts)) return [];
  57. $result = [];
  58. foreach ($posts as $post) {
  59. $result[] = [
  60. 'id' => (string)$post->ID,
  61. 'name' => $post->post_title,// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  62. ];
  63. }
  64. return $result;
  65. }
  66. private function formatTerms($terms) {
  67. if (empty($terms)) return [];
  68. if (!is_array($terms)) return []; // there can be instance of WP_Error instead of list of terms if woo commerce is not active
  69. $result = [];
  70. foreach ($terms as $term) {
  71. $result[] = [
  72. 'id' => (string)$term->term_id,// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  73. 'name' => $term->name,
  74. ];
  75. }
  76. return $result;
  77. }
  78. }