class-auxels-admin-assets.php
5 months ago
class-auxels-archive-menu-links.php
5 months ago
class-auxels-envato-elements.php
5 months ago
class-auxels-import-parser.php
3 years ago
class-auxels-import.php
3 years ago
class-auxels-search-post-type.php
5 months ago
class-auxels-wc-attribute-nav-menu.php
5 months ago
class-auxin-admin-dashboard.php
5 months ago
class-auxin-demo-importer.php
2 days ago
class-auxin-dependency-sorting.php
3 years ago
class-auxin-import.php
5 months ago
class-auxin-install.php
5 months ago
class-auxin-master-nav-menu-admin.php
5 months ago
class-auxin-page-template.php
5 months ago
class-auxin-permalink.php
5 months ago
class-auxin-plugin-requirements.php
3 years ago
class-auxin-post-type-base.php
5 months ago
class-auxin-svg-support-allowedattributes.php
7 years ago
class-auxin-svg-support-allowedtags.php
7 years ago
class-auxin-svg-support.php
1 year ago
class-auxin-walker-nav-menu-back.php
5 months ago
class-auxin-welcome-sections.php
1 year ago
class-auxin-welcome.php
1 month ago
class-auxin-whitelabel.php
3 years ago
class-auxin-widget-indie.php
5 months ago
class-auxin-widget-shortcode-map.php
5 months ago
class-auxin-widget.php
5 months ago
class-auxels-search-post-type.php
322 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Search Post Type Class. |
| 4 | * |
| 5 | * |
| 6 | * @package Auxin |
| 7 | * @license LICENSE.txt |
| 8 | * @author averta |
| 9 | * @link http://phlox.pro/ |
| 10 | * @copyright (c) 2010-2026 averta |
| 11 | */ |
| 12 | |
| 13 | // no direct access allowed |
| 14 | if ( ! defined('ABSPATH') ) { |
| 15 | die(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class to handle searching post types |
| 20 | */ |
| 21 | class Auxels_Search_Post_Type { |
| 22 | |
| 23 | /** |
| 24 | * Search Phrase |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $s; |
| 29 | |
| 30 | /** |
| 31 | * Search category |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public $cat; |
| 36 | |
| 37 | /** |
| 38 | * Search Post Type |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $post_type; |
| 43 | |
| 44 | /** |
| 45 | * Number of serach Items per page |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | public $per_page; |
| 50 | |
| 51 | /** |
| 52 | * Search query args |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
| 56 | public $query_args = array(); |
| 57 | |
| 58 | public $search_item_wrapper_start = "<div class='aux-search-item %s'>"; |
| 59 | public $search_item_container_start = "<a href='%s' class='aux-item-container'>"; |
| 60 | public $search_item_description_start = "<span class='aux-item-desc'>"; |
| 61 | public $search_item_title_start = "<span class='aux-item-title'>"; |
| 62 | public $search_item_details_start = "<span class='aux-item-details'>"; |
| 63 | |
| 64 | public $search_item_details_end = "</span>"; |
| 65 | public $search_item_title_end = "</span>"; |
| 66 | public $search_item_description_end = "</span>"; |
| 67 | public $search_item_container_end = "</a>"; |
| 68 | public $search_item_wrapper_end = "</div>"; |
| 69 | |
| 70 | /** |
| 71 | * __construct |
| 72 | */ |
| 73 | public function __construct( $search_phrase = '', $search_category = '', $search_post_type = '', $number_per_page = '' ) { |
| 74 | |
| 75 | $this->s = $search_phrase; |
| 76 | $this->cat = $search_category; |
| 77 | $this->post_type = $search_post_type; |
| 78 | $this->per_page = $number_per_page; |
| 79 | |
| 80 | $query_args = array ( |
| 81 | 's' => $this->s, |
| 82 | 'post_type' => $this->post_type, |
| 83 | 'no_found_rows' => 1, |
| 84 | 'posts_per_page' => $this->per_page, |
| 85 | 'post_status' => 'publish' |
| 86 | ); |
| 87 | |
| 88 | // Get category slug for each post type |
| 89 | |
| 90 | if ( !empty( $this->cat ) && function_exists( 'auxin_get_categories_slug_by_post_type' ) ) { |
| 91 | $category_slug = auxin_get_categories_slug_by_post_type( $this->post_type ); |
| 92 | if ( !empty( $category_slug ) ) { |
| 93 | $query_args['tax_query'] = array( |
| 94 | array( |
| 95 | 'taxonomy' => $category_slug, |
| 96 | 'field' => 'slug', |
| 97 | 'terms' => array( $this->cat ) |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | $this->set_query_args($query_args); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Change Query Args - append or modify query args parameter |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function set_query_args ($args = array()) { |
| 111 | if ( !empty( $args ) ) { |
| 112 | foreach ( $args as $key => $value ) { |
| 113 | $this->query_args[$key] = $value; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Search Products Post type |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public function search_products() { |
| 124 | |
| 125 | if ( !class_exists( 'WooCommerce' ) ) |
| 126 | return; |
| 127 | |
| 128 | $product_args = array( 'post_type' => 'product' ); |
| 129 | $product_variation_args = array( 'post_type' => 'product_variation' ); |
| 130 | |
| 131 | // search all products |
| 132 | $search_products_for_name = $this->search(); |
| 133 | |
| 134 | $this->set_query_args($product_args); |
| 135 | // Search all product for sku meta fields |
| 136 | $sku_args = array( |
| 137 | 's' => '', |
| 138 | 'meta_query' => array( |
| 139 | array( |
| 140 | 'key' => '_sku', |
| 141 | 'value' => $this->s, |
| 142 | 'compare' => 'like', |
| 143 | ), |
| 144 | ), |
| 145 | 'suppress_filters' => 0, |
| 146 | ); |
| 147 | |
| 148 | $this->set_query_args( $sku_args ); |
| 149 | $search_products_for_sku = $this->search(); |
| 150 | |
| 151 | // Search All Variations for sku meta field |
| 152 | $this->set_query_args($product_variation_args); |
| 153 | $search_variations_for_sku = $this->search(); |
| 154 | |
| 155 | //set search phrase query argument after searching for sku without search phrase |
| 156 | $this->set_query_args( array( 's' => $this->s ) ); |
| 157 | |
| 158 | // Merge All search results and remove the same results |
| 159 | $all_search_results = array_merge( |
| 160 | $search_products_for_sku->posts, |
| 161 | $search_variations_for_sku->posts, |
| 162 | $search_products_for_name->posts |
| 163 | ); |
| 164 | |
| 165 | $products_id = array(); |
| 166 | foreach ($all_search_results as $key => $product) { |
| 167 | $id = $product->ID; |
| 168 | if ( in_array( $id, $products_id) ) { |
| 169 | unset( $all_search_results[$key] ); |
| 170 | } else { |
| 171 | $products_id[] = $id; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Generate output dom |
| 176 | if ( !empty( $all_search_results ) ) { |
| 177 | global $post; |
| 178 | foreach ( $all_search_results as $post ) { |
| 179 | setup_postdata( $post ); |
| 180 | $product = wc_get_product( get_the_ID() ); |
| 181 | $categories = get_the_terms( $product->get_id(), 'product_cat' ); |
| 182 | $cat = $categories[0]->name; |
| 183 | $results[] = sprintf( |
| 184 | $this->search_item_wrapper_start. |
| 185 | $this->search_item_container_start. |
| 186 | '%s'. |
| 187 | $this->search_item_description_start. |
| 188 | $this->search_item_title_start. |
| 189 | '%s'. |
| 190 | $this->search_item_title_end. |
| 191 | $this->search_item_details_start. |
| 192 | '%s'. |
| 193 | $this->search_item_details_end. |
| 194 | '%s'. |
| 195 | $this->search_item_description_end. |
| 196 | $this->search_item_container_end. |
| 197 | $this->search_item_wrapper_end, |
| 198 | 'product', |
| 199 | get_permalink(), |
| 200 | ( ( has_post_thumbnail() ) ? woocommerce_get_product_thumbnail( 'shop_thumbnail' ) : '' ), |
| 201 | $product->get_title(), |
| 202 | $cat, |
| 203 | "<span class='aux-price'>".$product->get_price_html()."</span>" |
| 204 | ); |
| 205 | } |
| 206 | unset($this->query_args['meta_query']); |
| 207 | unset($this->query_args['suppress_filters']); |
| 208 | wp_reset_postdata(); |
| 209 | |
| 210 | return $this->search_results( 'product', $results); |
| 211 | |
| 212 | } else { |
| 213 | |
| 214 | unset($this->query_args['meta_query']); |
| 215 | unset($this->query_args['suppress_filters']); |
| 216 | |
| 217 | return $this->search_results( 'product' ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Search General Post types |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function search_general_post_types() { |
| 227 | $search_result = $this->search(); |
| 228 | if ($search_result->have_posts()) { |
| 229 | global $post; |
| 230 | while($search_result->have_posts()) { |
| 231 | $search_result->the_post(); |
| 232 | $results[] = sprintf( |
| 233 | $this->search_item_wrapper_start. |
| 234 | $this->search_item_container_start. |
| 235 | '<img src="%s">'. |
| 236 | $this->search_item_description_start. |
| 237 | $this->search_item_title_start. |
| 238 | '%s'. |
| 239 | $this->search_item_title_end. |
| 240 | $this->search_item_details_start. |
| 241 | '%s'. |
| 242 | $this->search_item_details_end. |
| 243 | $this->search_item_description_end. |
| 244 | $this->search_item_container_end. |
| 245 | $this->search_item_wrapper_end, |
| 246 | 'blog', |
| 247 | get_permalink(), |
| 248 | get_the_post_thumbnail_url(), |
| 249 | get_the_title(), |
| 250 | get_the_date() |
| 251 | ); |
| 252 | } |
| 253 | wp_reset_postdata(); |
| 254 | return $this->search_results( $this->query_args['post_type'], $results ); |
| 255 | } else { |
| 256 | return $this->search_results( $this->query_args['post_type'] ); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Search Portfolio Post type |
| 262 | * |
| 263 | * @return string |
| 264 | */ |
| 265 | public function search_portfolio() { |
| 266 | $search_result = $this->search(); |
| 267 | |
| 268 | if ( $search_result->have_posts() ) { |
| 269 | global $post; |
| 270 | while ( $search_result->have_posts() ) { |
| 271 | $search_result->the_post(); |
| 272 | $results[] = sprintf( |
| 273 | $this->search_item_wrapper_start. |
| 274 | $this->search_item_container_start. |
| 275 | '<img src="%s">'. |
| 276 | $this->search_item_container_end. |
| 277 | $this->search_item_wrapper_end, |
| 278 | 'portfolio', |
| 279 | get_permalink(), |
| 280 | get_the_post_thumbnail_url() |
| 281 | ); |
| 282 | } |
| 283 | wp_reset_postdata(); |
| 284 | |
| 285 | return $this->search_results( 'portfloio', $results ); |
| 286 | } else { |
| 287 | return $this->search_results( 'portfolio' ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Main Query Method |
| 293 | */ |
| 294 | public function search() { |
| 295 | $search_result = new WP_Query( $this->query_args ); |
| 296 | return $search_result; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Return Search results as array |
| 301 | * |
| 302 | * @param string $post_type |
| 303 | * @param array $results |
| 304 | * |
| 305 | * @return array |
| 306 | */ |
| 307 | public function search_results( $post_type = 'post', $results = array() ) { |
| 308 | return array( |
| 309 | 'postType' => $post_type, |
| 310 | 'results' => $results, |
| 311 | 'searchLink' => add_query_arg( |
| 312 | array( |
| 313 | 's' => $this->s, |
| 314 | 'post_type' => $post_type |
| 315 | ), |
| 316 | get_site_url() |
| 317 | ) |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | } |
| 322 |