base.php
217 lines
| 1 | <?php |
| 2 | namespace ShopEngine\Libs\Select_Api; |
| 3 | |
| 4 | defined('ABSPATH') || exit; |
| 5 | |
| 6 | /** |
| 7 | * Class Api |
| 8 | * |
| 9 | * @package ShopEngine\Core\Builders |
| 10 | */ |
| 11 | class Base extends \ShopEngine\Base\Api { |
| 12 | |
| 13 | public function config() { |
| 14 | |
| 15 | $this->prefix = 'shopengine_ajaxselect2'; |
| 16 | } |
| 17 | |
| 18 | public function get_post_list(){ |
| 19 | |
| 20 | if(!current_user_can('edit_posts')){ |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $query_args = [ |
| 25 | 'post_type' => 'post', |
| 26 | 'post_status' => 'publish', |
| 27 | 'posts_per_page' => 15, |
| 28 | ]; |
| 29 | |
| 30 | if(isset($this->request['ids'])){ |
| 31 | $ids = explode(',', $this->request['ids']); |
| 32 | $query_args['post__in'] = $ids; |
| 33 | } |
| 34 | if(isset($this->request['s'])){ |
| 35 | $query_args['s'] = $this->request['s']; |
| 36 | } |
| 37 | |
| 38 | $query = new \WP_Query($query_args); |
| 39 | $options = []; |
| 40 | if($query->have_posts()): |
| 41 | while ($query->have_posts()) { |
| 42 | $query->the_post(); |
| 43 | $options[] = [ 'id' => get_the_ID(), 'text' => get_the_title() ]; |
| 44 | } |
| 45 | endif; |
| 46 | |
| 47 | return ['results' => $options]; |
| 48 | wp_reset_postdata(); |
| 49 | } |
| 50 | |
| 51 | public function get_page_list(){ |
| 52 | if(!current_user_can('edit_posts')){ |
| 53 | return; |
| 54 | } |
| 55 | $query_args = [ |
| 56 | 'post_type' => 'page', |
| 57 | 'post_status' => 'publish', |
| 58 | 'posts_per_page' => 15, |
| 59 | ]; |
| 60 | |
| 61 | if(isset($this->request['ids'])){ |
| 62 | $ids = explode(',', $this->request['ids']); |
| 63 | $query_args['post__in'] = $ids; |
| 64 | } |
| 65 | if(isset($this->request['s'])){ |
| 66 | $query_args['s'] = $this->request['s']; |
| 67 | } |
| 68 | |
| 69 | $query = new \WP_Query($query_args); |
| 70 | $options = []; |
| 71 | if($query->have_posts()): |
| 72 | while ($query->have_posts()) { |
| 73 | $query->the_post(); |
| 74 | $options[] = [ 'id' => get_the_ID(), 'text' => get_the_title() ]; |
| 75 | } |
| 76 | endif; |
| 77 | |
| 78 | return ['results' => $options]; |
| 79 | wp_reset_postdata(); |
| 80 | } |
| 81 | |
| 82 | public function get_singular_list(){ |
| 83 | $query_args = [ |
| 84 | 'post_status' => 'publish', |
| 85 | 'posts_per_page' => 15, |
| 86 | 'post_type' => 'any' |
| 87 | ]; |
| 88 | |
| 89 | if(isset($this->request['ids'])){ |
| 90 | $ids = explode(',', $this->request['ids']); |
| 91 | $query_args['post__in'] = $ids; |
| 92 | } |
| 93 | if(isset($this->request['s'])){ |
| 94 | $query_args['s'] = $this->request['s']; |
| 95 | } |
| 96 | |
| 97 | $query = new \WP_Query($query_args); |
| 98 | $options = []; |
| 99 | if($query->have_posts()): |
| 100 | while ($query->have_posts()) { |
| 101 | $query->the_post(); |
| 102 | $options[] = [ 'id' => get_the_ID(), 'text' => get_the_title() ]; |
| 103 | } |
| 104 | endif; |
| 105 | |
| 106 | return ['results' => $options]; |
| 107 | wp_reset_postdata(); |
| 108 | } |
| 109 | |
| 110 | public function get_product_list(){ |
| 111 | $query_args = [ |
| 112 | 'post_type' => 'product', |
| 113 | 'post_status' => 'publish', |
| 114 | 'posts_per_page' => apply_filters('shopengine_filterable_products_per_page', 15) |
| 115 | ]; |
| 116 | |
| 117 | if(isset($this->request['ids'])){ |
| 118 | $ids = explode(',', $this->request['ids']); |
| 119 | $query_args['post__in'] = $ids; |
| 120 | } |
| 121 | if(isset($this->request['s'])){ |
| 122 | $query_args['s'] = $this->request['s']; |
| 123 | } |
| 124 | |
| 125 | $query = new \WP_Query($query_args); |
| 126 | $options = []; |
| 127 | if($query->have_posts()): |
| 128 | while ($query->have_posts()) { |
| 129 | $query->the_post(); |
| 130 | $options[] = [ 'id' => get_the_ID(), 'text' => get_the_title() ]; |
| 131 | } |
| 132 | endif; |
| 133 | |
| 134 | return ['results' => $options]; |
| 135 | wp_reset_postdata(); |
| 136 | } |
| 137 | |
| 138 | public function get_category(){ |
| 139 | return $this->terms(['category'], true); |
| 140 | } |
| 141 | |
| 142 | public function get_product_cat(){ |
| 143 | return $this->terms(['product_cat']); |
| 144 | } |
| 145 | |
| 146 | public function get_product_tags() { |
| 147 | return $this->terms(['product_tag']); |
| 148 | } |
| 149 | |
| 150 | public function get_product_brand() { |
| 151 | return $this->terms(['product_brand']); |
| 152 | } |
| 153 | |
| 154 | public function get_product_terms() { |
| 155 | if(isset($this->request['taxonomy'])){ |
| 156 | return $this->terms([$this->request['taxonomy']]); |
| 157 | } elseif(isset($this->request['ids'])) { |
| 158 | return $this->terms([]); |
| 159 | } |
| 160 | return ['results' => []]; |
| 161 | } |
| 162 | |
| 163 | public function get_product_pa_list() { |
| 164 | |
| 165 | global $wpdb; |
| 166 | |
| 167 | $search = ''; |
| 168 | if(isset($this->request['s'])) { |
| 169 | $search = $this->request['s']; |
| 170 | } |
| 171 | |
| 172 | $attributes = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name LIKE %s ORDER BY attribute_name ASC LIMIT 10", "%{$search}%")); |
| 173 | |
| 174 | $options = []; |
| 175 | |
| 176 | foreach ($attributes as $attribute) { |
| 177 | $options[] = ['id' => 'pa_' .$attribute->attribute_name, 'text' => $attribute->attribute_label]; |
| 178 | } |
| 179 | |
| 180 | return ['results' => $options]; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param $taxonomies |
| 185 | * @param $hide_empty |
| 186 | */ |
| 187 | public function terms($taxonomies, $hide_empty = false) { |
| 188 | $query_args = [ |
| 189 | 'taxonomy' => $taxonomies, // taxonomy name |
| 190 | 'orderby' => 'name', |
| 191 | 'order' => 'DESC', |
| 192 | 'hide_empty' => $hide_empty, |
| 193 | 'number' => 10 |
| 194 | ]; |
| 195 | |
| 196 | if(isset($this->request['ids'])){ |
| 197 | $ids = explode(',', $this->request['ids']); |
| 198 | $query_args['include'] = $ids; |
| 199 | } |
| 200 | if(isset($this->request['s'])){ |
| 201 | $query_args['name__like'] = $this->request['s']; |
| 202 | } |
| 203 | |
| 204 | $terms = get_terms( $query_args ); |
| 205 | |
| 206 | |
| 207 | $options = []; |
| 208 | |
| 209 | if(is_countable($terms) && count($terms) > 0): |
| 210 | foreach ($terms as $term) { |
| 211 | $options[] = [ 'id' => $term->term_id, 'text' => $term->name ]; |
| 212 | } |
| 213 | endif; |
| 214 | |
| 215 | return ['results' => $options]; |
| 216 | } |
| 217 | } |