assets
3 weeks ago
ajax-select2-api.php
3 weeks ago
ajax-select2.php
2 years ago
control-manager.php
4 years ago
image-choose.php
2 years ago
init.php
4 years ago
widget-area-modal.php
3 years ago
widget-area-utils.php
6 months ago
widget-area.php
2 years ago
ajax-select2-api.php
291 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | class Controls_Ajax_Select2_Api extends Core\Handler_Api { |
| 7 | |
| 8 | public function config() { |
| 9 | $this->prefix = 'ajaxselect2'; |
| 10 | } |
| 11 | |
| 12 | public function get_post_list() { |
| 13 | |
| 14 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | $query_args = array( |
| 19 | 'post_type' => 'post', |
| 20 | 'post_status' => 'publish', |
| 21 | 'posts_per_page' => 15, |
| 22 | ); |
| 23 | |
| 24 | if ( isset( $this->request['ids'] ) ) { |
| 25 | $ids = explode( ',', $this->request['ids'] ); |
| 26 | $query_args['post__in'] = $ids; |
| 27 | } |
| 28 | if ( isset( $this->request['s'] ) ) { |
| 29 | $query_args['s'] = $this->request['s']; |
| 30 | } |
| 31 | |
| 32 | $query = new \WP_Query( $query_args ); |
| 33 | $options = array(); |
| 34 | if ( $query->have_posts() ) : |
| 35 | while ( $query->have_posts() ) { |
| 36 | $query->the_post(); |
| 37 | $options[] = array( |
| 38 | 'id' => get_the_ID(), |
| 39 | 'text' => get_the_title(), |
| 40 | ); |
| 41 | } |
| 42 | endif; |
| 43 | |
| 44 | return array( 'results' => $options ); |
| 45 | wp_reset_postdata(); |
| 46 | } |
| 47 | |
| 48 | public function get_page_list() { |
| 49 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 50 | return; |
| 51 | } |
| 52 | $query_args = array( |
| 53 | 'post_type' => 'page', |
| 54 | 'post_status' => 'publish', |
| 55 | 'posts_per_page' => 15, |
| 56 | ); |
| 57 | |
| 58 | if ( isset( $this->request['ids'] ) ) { |
| 59 | $ids = explode( ',', $this->request['ids'] ); |
| 60 | $query_args['post__in'] = $ids; |
| 61 | } |
| 62 | if ( isset( $this->request['s'] ) ) { |
| 63 | $query_args['s'] = $this->request['s']; |
| 64 | } |
| 65 | |
| 66 | $query = new \WP_Query( $query_args ); |
| 67 | $options = array(); |
| 68 | if ( $query->have_posts() ) : |
| 69 | while ( $query->have_posts() ) { |
| 70 | $query->the_post(); |
| 71 | $options[] = array( |
| 72 | 'id' => get_the_ID(), |
| 73 | 'text' => get_the_title(), |
| 74 | ); |
| 75 | } |
| 76 | endif; |
| 77 | |
| 78 | return array( 'results' => $options ); |
| 79 | wp_reset_postdata(); |
| 80 | } |
| 81 | |
| 82 | public function get_singular_list() { |
| 83 | $query_args = array( |
| 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 | $query_args['posts_per_page'] = 200; |
| 93 | } |
| 94 | if ( isset( $this->request['s'] ) ) { |
| 95 | $query_args['s'] = $this->request['s']; |
| 96 | } |
| 97 | |
| 98 | $query = new \WP_Query( $query_args ); |
| 99 | $options = array(); |
| 100 | if ( $query->have_posts() ) : |
| 101 | while ( $query->have_posts() ) { |
| 102 | $query->the_post(); |
| 103 | $options[] = array( |
| 104 | 'id' => get_the_ID(), |
| 105 | 'text' => get_the_title(), |
| 106 | ); |
| 107 | } |
| 108 | endif; |
| 109 | |
| 110 | return array( 'results' => $options ); |
| 111 | wp_reset_postdata(); |
| 112 | } |
| 113 | |
| 114 | public function get_taxonomy_list() { |
| 115 | $query_args = array( |
| 116 | 'orderby' => 'name', |
| 117 | 'order' => 'DESC', |
| 118 | 'hide_empty' => false, |
| 119 | 'number' => 10, |
| 120 | ); |
| 121 | |
| 122 | // set taxonomy if provided |
| 123 | if ( isset( $this->request['taxonomy'] ) ) { |
| 124 | $query_args['taxonomy'] = $this->request['taxonomy']; |
| 125 | } else { |
| 126 | $query_args['taxonomy'] = 'category'; |
| 127 | } |
| 128 | |
| 129 | // filter by ids or search term |
| 130 | if ( isset( $this->request['ids'] ) ) { |
| 131 | $ids = explode( ',', $this->request['ids'] ); |
| 132 | $query_args['include'] = $ids; |
| 133 | } |
| 134 | |
| 135 | // search term |
| 136 | if ( isset( $this->request['s'] ) ) { |
| 137 | $query_args['name__like'] = $this->request['s']; |
| 138 | } |
| 139 | |
| 140 | $terms = get_terms( $query_args ); |
| 141 | |
| 142 | $options = array(); |
| 143 | |
| 144 | if ( is_countable( $terms ) && count( $terms ) > 0 ) : |
| 145 | foreach ( $terms as $term ) { |
| 146 | $options[] = array( |
| 147 | 'id' => $term->term_id, |
| 148 | 'text' => $term->name, |
| 149 | ); |
| 150 | } |
| 151 | endif; |
| 152 | |
| 153 | return array( 'results' => $options ); |
| 154 | } |
| 155 | |
| 156 | public function get_category() { |
| 157 | |
| 158 | $taxonomy = 'category'; |
| 159 | $query_args = array( |
| 160 | 'taxonomy' => array( 'category' ), // taxonomy name |
| 161 | 'orderby' => 'name', |
| 162 | 'order' => 'DESC', |
| 163 | 'hide_empty' => true, |
| 164 | 'number' => 10, |
| 165 | ); |
| 166 | |
| 167 | if ( isset( $this->request['ids'] ) ) { |
| 168 | $ids = explode( ',', $this->request['ids'] ); |
| 169 | $query_args['include'] = $ids; |
| 170 | } |
| 171 | if ( isset( $this->request['s'] ) ) { |
| 172 | $query_args['name__like'] = $this->request['s']; |
| 173 | } |
| 174 | |
| 175 | $terms = get_terms( $query_args ); |
| 176 | |
| 177 | $options = array(); |
| 178 | |
| 179 | if ( is_countable( $terms ) && count( $terms ) > 0 ) : |
| 180 | |
| 181 | foreach ( $terms as $term ) { |
| 182 | $options[] = array( |
| 183 | 'id' => $term->term_id, |
| 184 | 'text' => $term->name, |
| 185 | ); |
| 186 | } |
| 187 | endif; |
| 188 | return array( 'results' => $options ); |
| 189 | } |
| 190 | |
| 191 | public function get_product_list() { |
| 192 | $query_args = array( |
| 193 | 'post_type' => 'product', |
| 194 | 'post_status' => 'publish', |
| 195 | 'posts_per_page' => 15, |
| 196 | ); |
| 197 | |
| 198 | if ( isset( $this->request['ids'] ) ) { |
| 199 | $ids = explode( ',', $this->request['ids'] ); |
| 200 | $query_args['post__in'] = $ids; |
| 201 | } |
| 202 | if ( isset( $this->request['s'] ) ) { |
| 203 | $query_args['s'] = $this->request['s']; |
| 204 | } |
| 205 | |
| 206 | $query = new \WP_Query( $query_args ); |
| 207 | $options = array(); |
| 208 | if ( $query->have_posts() ) : |
| 209 | while ( $query->have_posts() ) { |
| 210 | $query->the_post(); |
| 211 | $options[] = array( |
| 212 | 'id' => get_the_ID(), |
| 213 | 'text' => get_the_title(), |
| 214 | ); |
| 215 | } |
| 216 | endif; |
| 217 | |
| 218 | return array( 'results' => $options ); |
| 219 | wp_reset_postdata(); |
| 220 | } |
| 221 | |
| 222 | public function get_product_cat() { |
| 223 | $query_args = array( |
| 224 | 'taxonomy' => array( 'product_cat' ), // taxonomy name |
| 225 | 'orderby' => 'name', |
| 226 | 'order' => 'DESC', |
| 227 | 'hide_empty' => false, |
| 228 | 'number' => 6, |
| 229 | ); |
| 230 | |
| 231 | if ( isset( $this->request['ids'] ) ) { |
| 232 | $ids = explode( ',', $this->request['ids'] ); |
| 233 | $query_args['include'] = $ids; |
| 234 | } |
| 235 | if ( isset( $this->request['s'] ) ) { |
| 236 | $query_args['name__like'] = $this->request['s']; |
| 237 | } |
| 238 | |
| 239 | $terms = get_terms( $query_args ); |
| 240 | |
| 241 | $options = array(); |
| 242 | |
| 243 | if ( is_countable( $terms ) && count( $terms ) > 0 ) : |
| 244 | foreach ( $terms as $term ) { |
| 245 | $options[] = array( |
| 246 | 'id' => $term->term_id, |
| 247 | 'text' => $term->name, |
| 248 | ); |
| 249 | } |
| 250 | endif; |
| 251 | |
| 252 | return array( 'results' => $options ); |
| 253 | } |
| 254 | |
| 255 | public function get_elementor_template_list(){ |
| 256 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $query_args = array( |
| 261 | 'post_type' => 'elementor_library', |
| 262 | 'post_status' => 'publish', |
| 263 | 'posts_per_page' => 15, |
| 264 | ); |
| 265 | if ( isset( $this->request['ids'] ) ) { |
| 266 | $ids = explode( ',', $this->request['ids'] ); |
| 267 | $query_args['post__in'] = $ids; |
| 268 | } |
| 269 | |
| 270 | if ( isset( $this->request['s'] ) ) { |
| 271 | $query_args['s'] = $this->request['s']; |
| 272 | } |
| 273 | |
| 274 | $query = new \WP_Query( $query_args ); |
| 275 | $options = array(); |
| 276 | if ( $query->have_posts() ) : |
| 277 | while ( $query->have_posts() ) { |
| 278 | $query->the_post(); |
| 279 | $options[] = array( |
| 280 | 'id' => get_the_ID(), |
| 281 | 'text' => get_the_title(), |
| 282 | ); |
| 283 | } |
| 284 | endif; |
| 285 | return array( 'results' => $options ); |
| 286 | wp_reset_postdata(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | new Controls_Ajax_Select2_Api(); |
| 291 |