AutocompletePostListLoader.php
2 months ago
DateTime.php
1 year ago
Emoji.php
2 months ago
Functions.php
1 week ago
Notice.php
3 years ago
Posts.php
3 years ago
Readme.php
3 years ago
index.php
3 years ago
AutocompletePostListLoader.php
127 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\WP; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Class AutocompletePostListLoader is used to load data for the frontend autocomplete |
| 10 | */ |
| 11 | class AutocompletePostListLoader { |
| 12 | /** @var Functions */ |
| 13 | private $wp; |
| 14 | |
| 15 | public function __construct( |
| 16 | Functions $wp |
| 17 | ) { |
| 18 | $this->wp = $wp; |
| 19 | } |
| 20 | |
| 21 | public function getProducts() { |
| 22 | global $wpdb; |
| 23 | |
| 24 | $products = $wpdb->get_results($wpdb->prepare( |
| 25 | "SELECT `ID`, `post_title` FROM {$wpdb->posts} WHERE `post_type` = %s ORDER BY `post_title` ASC;", |
| 26 | 'product' |
| 27 | )); |
| 28 | return $this->formatPosts($products); |
| 29 | } |
| 30 | |
| 31 | public function getVariableProducts() { |
| 32 | global $wpdb; |
| 33 | |
| 34 | $products = $wpdb->get_results($wpdb->prepare( |
| 35 | "SELECT DISTINCT p.ID, p.post_title |
| 36 | FROM {$wpdb->posts} p |
| 37 | INNER JOIN {$wpdb->term_relationships} tr ON tr.object_id = p.ID |
| 38 | INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = %s |
| 39 | INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id AND t.slug = %s |
| 40 | WHERE p.post_type = %s |
| 41 | ORDER BY p.post_title ASC;", |
| 42 | 'product_type', |
| 43 | 'variable', |
| 44 | 'product' |
| 45 | )); |
| 46 | return $this->formatPosts($products); |
| 47 | } |
| 48 | |
| 49 | public function getMembershipPlans() { |
| 50 | global $wpdb; |
| 51 | $products = $wpdb->get_results($wpdb->prepare( |
| 52 | "SELECT `ID`, `post_title` FROM {$wpdb->posts} WHERE `post_type` = %s AND `post_status` = 'publish' ORDER BY `post_title` ASC;", |
| 53 | 'wc_membership_plan' |
| 54 | )); |
| 55 | return $this->formatPosts($products); |
| 56 | } |
| 57 | |
| 58 | public function getSubscriptionProducts() { |
| 59 | global $wpdb; |
| 60 | $products = $wpdb->get_results($wpdb->prepare( |
| 61 | "SELECT `ID`, `post_title` FROM {$wpdb->posts} AS p |
| 62 | INNER JOIN {$wpdb->term_relationships} AS trel ON trel.object_id = p.id |
| 63 | INNER JOIN {$wpdb->term_taxonomy} AS ttax ON ttax.term_taxonomy_id = trel.term_taxonomy_id |
| 64 | INNER JOIN {$wpdb->terms} AS t ON ttax.term_id = t.term_id AND t.slug IN ('subscription', 'variable-subscription') |
| 65 | WHERE `p`.`post_type` = %s ORDER BY `post_title` ASC;", |
| 66 | 'product' |
| 67 | )); |
| 68 | return $this->formatPosts($products); |
| 69 | } |
| 70 | |
| 71 | public function getWooCommerceCategories() { |
| 72 | return $this->formatTerms($this->wp->getCategories(['taxonomy' => 'product_cat', 'orderby' => 'name'])); |
| 73 | } |
| 74 | |
| 75 | public function getPosts() { |
| 76 | global $wpdb; |
| 77 | $optionList = $wpdb->get_results('SELECT ID, post_title FROM ' . $wpdb->posts . " WHERE post_type='post' ORDER BY `post_title` ASC;"); |
| 78 | return $this->formatPosts($optionList); |
| 79 | } |
| 80 | |
| 81 | public function getPages() { |
| 82 | global $wpdb; |
| 83 | $optionList = $wpdb->get_results('SELECT ID, post_title FROM ' . $wpdb->posts . " WHERE post_type='page' ORDER BY `post_title` ASC;"); |
| 84 | return $this->formatPosts($optionList); |
| 85 | } |
| 86 | |
| 87 | public function getWooCommerceTags() { |
| 88 | return $this->formatTerms($this->wp->getTerms('product_tag')); |
| 89 | } |
| 90 | |
| 91 | public function getCategories() { |
| 92 | return $this->formatTerms($this->wp->getCategories()); |
| 93 | } |
| 94 | |
| 95 | public function getTags() { |
| 96 | return $this->formatTerms($this->wp->getTags()); |
| 97 | } |
| 98 | |
| 99 | private function formatPosts($posts) { |
| 100 | if (empty($posts)) return []; |
| 101 | $result = []; |
| 102 | foreach ($posts as $post) { |
| 103 | $result[] = [ |
| 104 | 'id' => (string)$post->ID, |
| 105 | 'name' => $post->post_title,// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 106 | ]; |
| 107 | } |
| 108 | return $result; |
| 109 | } |
| 110 | |
| 111 | private function formatTerms($terms) { |
| 112 | if (empty($terms)) return []; |
| 113 | if (!is_array($terms)) return []; // there can be instance of WP_Error instead of list of terms if woo commerce is not active |
| 114 | $result = []; |
| 115 | foreach ($terms as $term) { |
| 116 | if (!$term instanceof \WP_Term) { |
| 117 | continue; |
| 118 | } |
| 119 | $result[] = [ |
| 120 | 'id' => (string)$term->term_id, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 121 | 'name' => $term->name, |
| 122 | ]; |
| 123 | } |
| 124 | return $result; |
| 125 | } |
| 126 | } |
| 127 |