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