| 1 |
<?php //phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* Search Utility |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @subpackage App\Utility |
| 8 |
* @since 1.0.0 |
| 9 |
* @category Utility |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Disco\App\Utility; |
| 13 |
|
| 14 |
use WC_Countries; |
| 15 |
use WC_Coupon; |
| 16 |
use WC_Data_Store; |
| 17 |
use WP_Query; |
| 18 |
use WP_Term_Query; |
| 19 |
use WP_User_Query; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Search |
| 23 |
* |
| 24 |
* @package Disco |
| 25 |
* @subpackage App\Utility |
| 26 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 27 |
* @link https://webappick.com |
| 28 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 29 |
* @category Utility |
| 30 |
*/ |
| 31 |
class Search { //phpcs:ignore |
| 32 |
|
| 33 |
/** |
| 34 |
* Search Products by Product Title or SKU. |
| 35 |
* |
| 36 |
* @param string $search_term Search Term. |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public static function products( $search_term = '' ) { |
| 40 |
try { |
| 41 |
$data_store = WC_Data_Store::load( 'product' ); |
| 42 |
$get_products = $data_store->search_products( $search_term, 'product', true, false, 200, array(), array() ); // @phpstan-ignore-line |
| 43 |
} catch ( \Throwable $e ) { |
| 44 |
$get_products = array(); |
| 45 |
} |
| 46 |
|
| 47 |
$products = array(); |
| 48 |
|
| 49 |
if ( ! empty( $get_products ) ) { |
| 50 |
foreach ( $get_products as $pid ) { |
| 51 |
if ( ! $pid ) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
$product = wc_get_product( $pid ); |
| 56 |
|
| 57 |
if ( ! ( $product instanceof \WC_Product ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$products[] = array( |
| 62 |
'id' => $product->get_id(), |
| 63 |
'sku' => $product->get_sku(), |
| 64 |
'name' => $product->get_name(), |
| 65 |
'image' => wp_get_attachment_url( (int) $product->get_image_id() ), |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $products; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Search Categories by Category Name. |
| 75 |
* |
| 76 |
* @param string $search_term Search Term. |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
public static function categories( $search_term = '' ) { |
| 80 |
$args = array( |
| 81 |
'taxonomy' => 'product_cat', |
| 82 |
'orderby' => 'name', |
| 83 |
'order' => 'ASC', |
| 84 |
'hide_empty' => false, |
| 85 |
'number' => 20, |
| 86 |
); |
| 87 |
|
| 88 |
if ( ! empty( $search_term ) ) { |
| 89 |
$args['search'] = $search_term; |
| 90 |
} |
| 91 |
|
| 92 |
$args = (array) $args; |
| 93 |
$get_categories = ( new WP_Term_Query( $args ) )->get_terms(); |
| 94 |
|
| 95 |
$categories = array(); |
| 96 |
|
| 97 |
if ( is_array( $get_categories ) && ! empty( $get_categories ) ) { |
| 98 |
foreach ( $get_categories as $category ) { |
| 99 |
if ( ! ( $category instanceof \WP_Term ) ) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
$categories[] = array( |
| 104 |
'id' => $category->term_id, |
| 105 |
'name' => $category->name, |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $categories; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Search Tags by Tag Name. |
| 115 |
* |
| 116 |
* @param string $search_term Search Term. |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public static function tags( $search_term = '' ) { |
| 120 |
$args = array( |
| 121 |
'taxonomy' => 'product_tag', |
| 122 |
'orderby' => 'name', |
| 123 |
'order' => 'ASC', |
| 124 |
'hide_empty' => false, |
| 125 |
'number' => 20, |
| 126 |
); |
| 127 |
|
| 128 |
if ( ! empty( $search_term ) ) { |
| 129 |
$args['search'] = $search_term; |
| 130 |
} |
| 131 |
|
| 132 |
$get_tags = ( new WP_Term_Query( $args ) )->get_terms(); |
| 133 |
|
| 134 |
$tags = array(); |
| 135 |
|
| 136 |
if ( is_array( $get_tags ) && ! empty( $get_tags ) ) { |
| 137 |
foreach ( $get_tags as $tag ) { |
| 138 |
if ( ! ( $tag instanceof \WP_Term ) ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
$tags[] = array( |
| 143 |
'id' => $tag->term_id, |
| 144 |
'name' => $tag->name, |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $tags; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Search coupon by coupon name. |
| 154 |
* |
| 155 |
* @param string $search_term Search Term. |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public static function coupons( $search_term = '' ) { |
| 159 |
$args = array( |
| 160 |
'post_type' => 'shop_coupon', |
| 161 |
'post_status' => 'publish', |
| 162 |
'fields' => 'post_title', |
| 163 |
'number' => 20, |
| 164 |
|
| 165 |
); |
| 166 |
|
| 167 |
if ( ! empty( $search_term ) ) { |
| 168 |
$args['s'] = $search_term; |
| 169 |
} |
| 170 |
|
| 171 |
$get_coupons = ( new WP_Query( $args ) )->get_posts(); |
| 172 |
|
| 173 |
$coupons = array(); |
| 174 |
|
| 175 |
if ( ! empty( $get_coupons ) ) { |
| 176 |
foreach ( $get_coupons as $coupon ) { |
| 177 |
if ( ! ( $coupon instanceof \WP_Post ) ) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
$coupon = new WC_Coupon( $coupon->post_title ); |
| 182 |
|
| 183 |
if ( ! $coupon->is_valid_for_cart() ) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
$coupons[] = array( |
| 188 |
'id' => $coupon->get_id(), |
| 189 |
'name' => $coupon->get_code(), |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $coupons; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Search Customer by Name or Email. |
| 199 |
* |
| 200 |
* @param string $search_term Search Term. |
| 201 |
* @return array |
| 202 |
* @throws \Exception If the search term is invalid. |
| 203 |
*/ |
| 204 |
public static function customers( $search_term = '' ) { |
| 205 |
$args = array( |
| 206 |
'role' => 'customer', |
| 207 |
'order' => 'ASC', |
| 208 |
'orderby' => 'display_name', |
| 209 |
'number' => 20, |
| 210 |
); |
| 211 |
|
| 212 |
if ( ! empty( $search_term ) ) { |
| 213 |
$args['search'] = '*' . esc_attr( $search_term ) . '*'; |
| 214 |
} |
| 215 |
|
| 216 |
if ( filter_var( $search_term, FILTER_VALIDATE_EMAIL ) ) { |
| 217 |
// valid email address |
| 218 |
$args['search_columns'] = array( 'user_email' ); |
| 219 |
} else { |
| 220 |
// invalid address |
| 221 |
$args['meta_query'] = array( //phpcs:ignore |
| 222 |
'relation' => 'OR', |
| 223 |
array( |
| 224 |
'key' => 'first_name', |
| 225 |
'value' => $search_term, |
| 226 |
'compare' => 'LIKE', |
| 227 |
), |
| 228 |
array( |
| 229 |
'key' => 'last_name', |
| 230 |
'value' => $search_term, |
| 231 |
'compare' => 'LIKE', |
| 232 |
), |
| 233 |
array( |
| 234 |
'key' => 'user_email', |
| 235 |
'value' => $search_term, |
| 236 |
'compare' => 'LIKE', |
| 237 |
), |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
// Create the WP_User_Query object |
| 242 |
$get_customers = ( new WP_User_Query( $args ) )->get_results(); |
| 243 |
|
| 244 |
$customers = array(); |
| 245 |
|
| 246 |
if ( ! empty( $get_customers ) ) { |
| 247 |
foreach ( $get_customers as $customer ) { |
| 248 |
$customers[] = array( |
| 249 |
'id' => $customer->ID, |
| 250 |
'name' => $customer->first_name . ' ' . $customer->last_name . ' (' . $customer->user_email . ')', |
| 251 |
); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return $customers; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Get States by Country Code. |
| 260 |
* |
| 261 |
* @param string $search_term Search Term. |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
public static function states( $search_term ) { |
| 265 |
// Initialize WooCommerce Countries object |
| 266 |
$countries = WC()->countries; |
| 267 |
|
| 268 |
// Get all states (indexed by country) |
| 269 |
$states_by_country = $countries->get_states(); |
| 270 |
|
| 271 |
// Initialize an array to hold matching states |
| 272 |
$matching_states = array(); |
| 273 |
|
| 274 |
// Check if the state array is empty |
| 275 |
if ( empty( $states_by_country ) ) { |
| 276 |
return $matching_states; |
| 277 |
} |
| 278 |
|
| 279 |
// Loop through each country and its states |
| 280 |
foreach ( $states_by_country as $country_code => $states ) { |
| 281 |
$country_names = $countries->get_countries(); |
| 282 |
$country_name = $country_names[$country_code]; |
| 283 |
|
| 284 |
if ( !is_array( $states ) ) { |
| 285 |
continue; |
| 286 |
} |
| 287 |
|
| 288 |
foreach ( $states as $state_code => $state_name ) { |
| 289 |
// Check if the state name contains the search term (case-insensitive) |
| 290 |
if ( stripos( $state_name, $search_term ) === false ) { |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
$matching_states[] = array( |
| 295 |
'id' => $state_code, |
| 296 |
'name' => $country_name . ' - ' . $state_name, |
| 297 |
); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return $matching_states; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Get States by Country Code. |
| 306 |
* |
| 307 |
* @param string $search_term Country or state name. |
| 308 |
* @return array |
| 309 |
*/ |
| 310 |
public static function countries( $search_term ) { |
| 311 |
if ( empty( $search_term ) ) { |
| 312 |
return array(); |
| 313 |
} |
| 314 |
|
| 315 |
$counties = array(); |
| 316 |
$get_counties = ( new WC_Countries )->get_countries(); |
| 317 |
|
| 318 |
foreach ( $get_counties as $country_key => $country ) { |
| 319 |
$counties[ $country_key ] = array( |
| 320 |
'id' => $country_key, |
| 321 |
'name' => $country, |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
return array_filter( |
| 326 |
$counties, |
| 327 |
static function ( $item ) use ( $search_term ) { |
| 328 |
$value = $item['id'] . '-' . $item['name']; |
| 329 |
|
| 330 |
return stripos( $value, $search_term ) !== false; |
| 331 |
} |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Search Attributes by Attribute Name. |
| 337 |
* |
| 338 |
* @param string $search_term Search Term. |
| 339 |
* @return array |
| 340 |
*/ |
| 341 |
public static function attributes( $search_term = ' ' ) { |
| 342 |
$attributes = self::get_global_attributes( $search_term ); |
| 343 |
$custom_attributes = self::get_custom_attributes( $search_term ); |
| 344 |
|
| 345 |
return array_merge( $attributes, $custom_attributes ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Search Attributes by Attribute Name. |
| 350 |
* |
| 351 |
* @param string $search_term Search Term. |
| 352 |
* @return array |
| 353 |
*/ |
| 354 |
protected static function get_global_attributes( $search_term ) { |
| 355 |
$global_attributes = Cache::remember( |
| 356 |
'wc_global_attributes', |
| 357 |
array( |
| 358 |
new Model, |
| 359 |
'wc_global_attribute_query', |
| 360 |
), |
| 361 |
array( $search_term ) |
| 362 |
); |
| 363 |
$result = array(); |
| 364 |
|
| 365 |
if ( is_array( $global_attributes ) && ! empty( $global_attributes ) ) { |
| 366 |
foreach ( $global_attributes as $attribute ) { |
| 367 |
if ( ! isset( $attribute->attribute_name, $attribute->attribute_label ) ) { |
| 368 |
continue; |
| 369 |
} |
| 370 |
|
| 371 |
$result[] = array( |
| 372 |
'id' => $attribute->attribute_name, |
| 373 |
'name' => $attribute->attribute_label, |
| 374 |
); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return $result; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Get all product custom attributes. |
| 383 |
* |
| 384 |
* @param string $search_term Search Term. |
| 385 |
* @return array |
| 386 |
*/ |
| 387 |
protected static function get_custom_attributes( $search_term ) { |
| 388 |
$custom_attributes = Cache::remember( |
| 389 |
'wc_custom_attributes', |
| 390 |
array( |
| 391 |
new \Disco\App\Utility\Model, |
| 392 |
'wc_custom_attribute_query', |
| 393 |
), |
| 394 |
array( $search_term ) |
| 395 |
); |
| 396 |
$result = array(); |
| 397 |
|
| 398 |
if ( is_array( $custom_attributes ) && ! empty( $custom_attributes ) ) { |
| 399 |
foreach ( $custom_attributes as $value ) { |
| 400 |
$product_attr = maybe_unserialize( $value->type ); |
| 401 |
|
| 402 |
if ( ! is_array( $product_attr ) ) { |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
$result = array_merge( $result, self::filter_product_attributes( $product_attr, $search_term ) ); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return $result; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Filter Product Attributes. |
| 415 |
* |
| 416 |
* @param array $product_attr Product Attributes. |
| 417 |
* @param string $search_term Search Term. |
| 418 |
* @return array |
| 419 |
*/ |
| 420 |
protected static function filter_product_attributes( $product_attr, $search_term ) { |
| 421 |
$filtered_attrs = array(); |
| 422 |
|
| 423 |
foreach ( $product_attr as $key => $arr_value ) { |
| 424 |
if ( strpos( $key, 'pa_' ) !== false ) { |
| 425 |
continue; |
| 426 |
} |
| 427 |
|
| 428 |
if ( ! empty( $search_term ) && ( stripos( $arr_value['name'], $search_term ) === false ) ) { |
| 429 |
continue; |
| 430 |
} |
| 431 |
|
| 432 |
$filtered_attrs[] = array( |
| 433 |
'id' => $key, |
| 434 |
'name' => ucwords( str_replace( '-', ' ', $arr_value['name'] ) ), |
| 435 |
); |
| 436 |
} |
| 437 |
|
| 438 |
return $filtered_attrs; |
| 439 |
} |
| 440 |
|
| 441 |
} |
| 442 |
|